mirror of
https://github.com/cefsharp/CefSharp.git
synced 2026-12-06 04:00:07 +00:00
move test logic into CefSharp.Example, run all example tests in wpf
This commit is contained in:
@@ -1,10 +1,34 @@
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
namespace CefSharp.Example
|
||||
{
|
||||
public class ExamplePresenter
|
||||
public class ExamplePresenter : IBeforeResourceLoad
|
||||
{
|
||||
public static void Init()
|
||||
{
|
||||
Settings settings = new Settings();
|
||||
if (CEF.Initialize(settings))
|
||||
{
|
||||
CEF.RegisterScheme("test", new SchemeHandlerFactory());
|
||||
CEF.RegisterJsObject("bound", new BoundObject());
|
||||
}
|
||||
}
|
||||
|
||||
private const string resource_url = "http://test/resource/load";
|
||||
private const string scheme_url = "test://test/SchemeTest.html";
|
||||
private const string bind_url = "test://test/BindingTest.html";
|
||||
|
||||
private int color_index = 0;
|
||||
private readonly string[] colors =
|
||||
{
|
||||
"red",
|
||||
"blue",
|
||||
"green",
|
||||
};
|
||||
|
||||
private readonly IWebBrowser model;
|
||||
private readonly IExampleView view;
|
||||
private readonly Action<Action> gui_invoke;
|
||||
@@ -18,16 +42,22 @@ namespace CefSharp.Example
|
||||
|
||||
this.model.PropertyChanged += model_PropertyChanged;
|
||||
|
||||
this.view.ExitActivated += view_ExitActivated;
|
||||
this.view.UrlActivated += view_UrlActivated;
|
||||
this.view.ForwardActivated += view_ForwardActivated;
|
||||
this.view.BackActivated += view_BackActivated;
|
||||
this.view.UrlActivated += view_UrlActivated;
|
||||
this.view.TestResourceLoadActivated += view_TestResourceLoadActivated;
|
||||
this.view.TestSchemeLoadActivated += view_TestSchemeLoadActivated;
|
||||
this.view.TestExecuteScriptActivated += view_TestExecuteScriptActivated;
|
||||
this.view.TestEvaluateScriptActivated += view_TestEvaluateScriptActivated;
|
||||
this.view.TestBindActivated += view_TestBindActivated;
|
||||
this.view.TestConsoleMessageActivated += view_TestConsoleMessageActivated;
|
||||
this.view.ExitActivated += view_ExitActivated;
|
||||
}
|
||||
|
||||
private void model_PropertyChanged(object sender, PropertyChangedEventArgs e)
|
||||
{
|
||||
string @string;
|
||||
bool @bool;
|
||||
string @string = null;
|
||||
bool @bool = false;
|
||||
|
||||
switch (e.PropertyName)
|
||||
{
|
||||
@@ -73,9 +103,68 @@ namespace CefSharp.Example
|
||||
model.Forward();
|
||||
}
|
||||
|
||||
private void view_TestResourceLoadActivated(object sender, EventArgs e)
|
||||
{
|
||||
model.Load(resource_url);
|
||||
}
|
||||
|
||||
private void view_TestSchemeLoadActivated(object sender, EventArgs e)
|
||||
{
|
||||
model.Load(scheme_url);
|
||||
}
|
||||
|
||||
private void view_TestExecuteScriptActivated(object sender, EventArgs e)
|
||||
{
|
||||
var script = String.Format("document.body.style.background = '{0}'",
|
||||
colors[color_index++]);
|
||||
if (color_index >= colors.Length)
|
||||
{
|
||||
color_index = 0;
|
||||
}
|
||||
|
||||
view.ExecuteScript(script);
|
||||
}
|
||||
|
||||
private void view_TestEvaluateScriptActivated(object sender, EventArgs e)
|
||||
{
|
||||
var rand = new Random();
|
||||
var x = rand.Next(1, 10);
|
||||
var y = rand.Next(1, 10);
|
||||
|
||||
var script = String.Format("{0} + {1}", x, y);
|
||||
var result = view.EvaluateScript(script);
|
||||
var output = String.Format("{0} => {1}", script, result);
|
||||
|
||||
gui_invoke(() => view.DisplayOutput(output));
|
||||
}
|
||||
|
||||
private void view_TestBindActivated(object sender, EventArgs e)
|
||||
{
|
||||
model.Load(bind_url);
|
||||
}
|
||||
|
||||
private void view_TestConsoleMessageActivated(object sender, EventArgs e)
|
||||
{
|
||||
var script = "console.log('Hello, world!')";
|
||||
view.ExecuteScript(script);
|
||||
}
|
||||
|
||||
private void view_ExitActivated(object sender, EventArgs e)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
CEF.Shutdown();
|
||||
System.Environment.Exit(0);
|
||||
}
|
||||
|
||||
void IBeforeResourceLoad.HandleBeforeResourceLoad(IWebBrowser browserControl,
|
||||
IRequestResponse requestResponse)
|
||||
{
|
||||
IRequest request = requestResponse.Request;
|
||||
if (request.Url.StartsWith(resource_url))
|
||||
{
|
||||
Stream resourceStream = new MemoryStream(Encoding.UTF8.GetBytes(
|
||||
"<html><body><h1>Success</h1><p>This document is loaded from a System.IO.Stream</p></body></html>"));
|
||||
requestResponse.RespondWith(resourceStream, "text/html");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user