Skip to content

Commit

Permalink
move test logic into CefSharp.Example, run all example tests in wpf
Browse files Browse the repository at this point in the history
  • Loading branch information
ataranto committed Feb 25, 2012
1 parent f76b0d8 commit 0079dd4
Show file tree
Hide file tree
Showing 17 changed files with 766 additions and 31 deletions.
166 changes: 166 additions & 0 deletions CefSharp.Example/BoundObject.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
using System;

namespace CefSharp.Example
{
class BoundObject
{
public string Repeat(string str, int n)
{
string result = String.Empty;
for (int i = 0; i < n; i++)
{
result += str;
}
return result;
}

public void EchoVoid()
{
}

public Boolean EchoBoolean(Boolean arg0)
{
return arg0;
}

public Boolean? EchoNullableBoolean(Boolean? arg0)
{
return arg0;
}

public SByte EchoSByte(SByte arg0)
{
return arg0;
}

public SByte? EchoNullableSByte(SByte? arg0)
{
return arg0;
}

public Int16 EchoInt16(Int16 arg0)
{
return arg0;
}

public Int16? EchoNullableInt16(Int16? arg0)
{
return arg0;
}

public Int32 EchoInt32(Int32 arg0)
{
return arg0;
}

public Int32? EchoNullableInt32(Int32? arg0)
{
return arg0;
}

public Int64 EchoInt64(Int64 arg0)
{
return arg0;
}

public Int64? EchoNullableInt64(Int64? arg0)
{
return arg0;
}

public Byte EchoByte(Byte arg0)
{
return arg0;
}

public Byte? EchoNullableByte(Byte? arg0)
{
return arg0;
}

public UInt16 EchoUInt16(UInt16 arg0)
{
return arg0;
}

public UInt16? EchoNullableUInt16(UInt16? arg0)
{
return arg0;
}

public UInt32 EchoUInt32(UInt32 arg0)
{
return arg0;
}

public UInt32? EchoNullableUInt32(UInt32? arg0)
{
return arg0;
}

public UInt64 EchoUInt64(UInt64 arg0)
{
return arg0;
}

public UInt64? EchoNullableUInt64(UInt64? arg0)
{
return arg0;
}

public Single EchoSingle(Single arg0)
{
return arg0;
}

public Single? EchoNullableSingle(Single? arg0)
{
return arg0;
}

public Double EchoDouble(Double arg0)
{
return arg0;
}

public Double? EchoNullableDouble(Double? arg0)
{
return arg0;
}

public Char EchoChar(Char arg0)
{
return arg0;
}

public Char? EchoNullableChar(Char? arg0)
{
return arg0;
}

public DateTime EchoDateTime(DateTime arg0)
{
return arg0;
}

public DateTime? EchoNullableDateTime(DateTime? arg0)
{
return arg0;
}

public Decimal EchoDecimal(Decimal arg0)
{
return arg0;
}

public Decimal? EchoNullableDecimal(Decimal? arg0)
{
return arg0;
}

public String EchoString(String arg0)
{
return arg0;
}
}
}
21 changes: 21 additions & 0 deletions CefSharp.Example/CefSharp.Example.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,39 @@
<Reference Include="System.Core">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="BoundObject.cs" />
<Compile Include="ExamplePresenter.cs" />
<Compile Include="IExampleView.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Properties\Resources.Designer.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<Compile Include="SchemeHandler.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\CefSharp\CefSharp.vcproj">
<Project>{7B495581-2271-4F41-9476-ACB86E8C864F}</Project>
<Name>CefSharp</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<Content Include="Resources\BindingTest.html" />
<Content Include="Resources\PopupTest.html" />
<Content Include="Resources\SchemeTest.html" />
<Content Include="Resources\TooltipTest.html" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
101 changes: 95 additions & 6 deletions CefSharp.Example/ExamplePresenter.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -18,16 +42,22 @@ public ExamplePresenter(IWebBrowser model, IExampleView view,

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)
{
Expand Down Expand Up @@ -73,9 +103,68 @@ private void view_ForwardActivated(object sender, EventArgs e)
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");
}
}
}
}
10 changes: 10 additions & 0 deletions CefSharp.Example/IExampleView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ public interface IExampleView
event Action<object, string> UrlActivated;
event EventHandler BackActivated;
event EventHandler ForwardActivated;
event EventHandler TestResourceLoadActivated;
event EventHandler TestSchemeLoadActivated;
event EventHandler TestExecuteScriptActivated;
event EventHandler TestEvaluateScriptActivated;
event EventHandler TestBindActivated;
event EventHandler TestConsoleMessageActivated;
event EventHandler ExitActivated;

void SetTitle(string title);
Expand All @@ -15,5 +21,9 @@ public interface IExampleView
void SetCanGoBack(bool can_go_back);
void SetCanGoForward(bool can_go_forward);
void SetIsLoading(bool is_loading);

void ExecuteScript(string script);
object EvaluateScript(string script);
void DisplayOutput(string output);
}
}
Loading

0 comments on commit 0079dd4

Please sign in to comment.