Skip to content

Commit 981bc28

Browse files
authored
Merge eb34e61 into 4f74f60
2 parents 4f74f60 + eb34e61 commit 981bc28

File tree

489 files changed

+31597
-107
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

489 files changed

+31597
-107
lines changed

CefSharp.Example/CefSharp.Example.csproj

-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@
7676
<ItemGroup>
7777
<Compile Include="Callback\RunFileDialogCallback.cs" />
7878
<Compile Include="DevTools\DevToolsExtensions.cs" />
79-
<Compile Include="DevTools\TaskMethodDevToolsMessageObserver.cs" />
8079
<Compile Include="Handlers\AudioHandler.cs" />
8180
<Compile Include="Handlers\ExampleResourceRequestHandler.cs" />
8281
<Compile Include="Handlers\ExtensionHandler.cs" />

CefSharp.Example/DevTools/DevToolsExtensions.cs

+7-39
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
using System;
2-
using System.Text;
3-
using System.Threading;
42
using System.Threading.Tasks;
53
using Newtonsoft.Json;
64

75
namespace CefSharp.Example.DevTools
86
{
97
public static class DevToolsExtensions
108
{
11-
private static int LastMessageId = 600000;
129
/// <summary>
1310
/// Calls Page.captureScreenshot without any optional params
1411
/// (Results in PNG image of default viewport)
@@ -23,58 +20,29 @@ public static async Task<byte[]> CaptureScreenShotAsPng(this IWebBrowser chromiu
2320
// throw new System.Exception("Page hasn't loaded");
2421
//}
2522

26-
var host = chromiumWebBrowser.GetBrowserHost();
23+
var browser = chromiumWebBrowser.GetBrowser();
2724

28-
if (host == null || host.IsDisposed)
25+
if (browser == null || browser.IsDisposed)
2926
{
30-
throw new Exception("BrowserHost is Null or Disposed");
27+
throw new Exception("browser is Null or Disposed");
3128
}
3229

3330
//var param = new Dictionary<string, object>
3431
//{
3532
// { "format", "png" },
3633
//}
3734

38-
var msgId = Interlocked.Increment(ref LastMessageId);
39-
40-
var observer = new TaskMethodDevToolsMessageObserver(msgId);
41-
4235
//Make sure to dispose of our observer registration when done
43-
//TODO: Create a single observer that maps tasks to Id's
44-
//Or at least create one for each type, events and method
45-
using (var observerRegistration = host.AddDevToolsMessageObserver(observer))
36+
using (var devToolsClient = browser.GetDevToolsClient())
4637
{
47-
//Page.captureScreenshot defaults to PNG, all params are optional
48-
//for this DevTools method
49-
int id = 0;
5038
const string methodName = "Page.captureScreenshot";
5139

52-
//TODO: Simplify this, we can use an Func to reduce code duplication
53-
if (Cef.CurrentlyOnThread(CefThreadIds.TID_UI))
54-
{
55-
id = host.ExecuteDevToolsMethod(msgId, methodName);
56-
}
57-
else
58-
{
59-
id = await Cef.UIThreadTaskFactory.StartNew(() =>
60-
{
61-
return host.ExecuteDevToolsMethod(msgId, methodName);
62-
});
63-
}
64-
65-
if (id != msgId)
66-
{
67-
throw new Exception("Message Id doesn't match the provided Id");
68-
}
69-
70-
var result = await observer.Task;
71-
72-
var success = result.Item1;
40+
var result = await devToolsClient.ExecuteDevToolsMethodAsync(methodName);
7341

74-
dynamic response = JsonConvert.DeserializeObject<dynamic>(Encoding.UTF8.GetString(result.Item2));
42+
dynamic response = JsonConvert.DeserializeObject<dynamic>(result.ResponseAsJsonString);
7543

7644
//Success
77-
if (success)
45+
if (result.Success)
7846
{
7947
return Convert.FromBase64String((string)response.data);
8048
}

CefSharp.Example/DevTools/TaskMethodDevToolsMessageObserver.cs

-67
This file was deleted.

CefSharp.OffScreen.Example/Program.cs

+9
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,15 @@ private static async void MainAsync(string cachePath, double zoomLevel)
7777
}
7878
await LoadPageAsync(browser);
7979

80+
using (var devToolsClient = browser.GetDevToolsClient())
81+
{
82+
var response = await devToolsClient.Browser.GetVersionAsync();
83+
var jsVersion = response.Revision;
84+
85+
var historyResponse = await devToolsClient.Page.GetNavigationHistoryAsync();
86+
//var success = await devToolsClient.Network.ClearBrowserCacheAsync();
87+
}
88+
8089
//Check preferences on the CEF UI Thread
8190
await Cef.UIThreadTaskFactory.StartNew(delegate
8291
{

CefSharp.Test/CefSharp.Test.csproj

+2
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@
125125
</ItemGroup>
126126
<ItemGroup>
127127
<Compile Include="BindingRedirectAssemblyResolver.cs" />
128+
<Compile Include="DevTools\DevToolsClientFacts.cs" />
128129
<Compile Include="Framework\BinderFacts.cs" />
129130
<Compile Include="Framework\AsyncExtensionFacts.cs" />
130131
<Compile Include="Framework\ConcurrentMethodRunnerQueueFacts.cs" />
@@ -138,6 +139,7 @@
138139
<Compile Include="CefSharpXunitTestFramework.cs" />
139140
<Compile Include="CefSharpFixture.cs" />
140141
<Compile Include="CefSharpFixtureCollection.cs" />
142+
<Compile Include="PostMessage\IntegrationTestFacts.cs" />
141143
<Compile Include="Properties\AssemblyInfo.cs" />
142144
<Compile Include="WebBrowserTestExtensions.cs" />
143145
<Compile Include="WinForms\WinFormsBrowserBasicFacts.cs" />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
// Copyright © 2017 The CefSharp Authors. All rights reserved.
2+
//
3+
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
4+
5+
using System.Threading.Tasks;
6+
using CefSharp.DevTools.Browser;
7+
using CefSharp.DevTools.Network;
8+
using CefSharp.Example;
9+
using CefSharp.OffScreen;
10+
using Xunit;
11+
using Xunit.Abstractions;
12+
13+
namespace CefSharp.Test.DevTools
14+
{
15+
//NOTE: All Test classes must be part of this collection as it manages the Cef Initialize/Shutdown lifecycle
16+
[Collection(CefSharpFixtureCollection.Key)]
17+
public class DevToolsClientFacts
18+
{
19+
private readonly ITestOutputHelper output;
20+
private readonly CefSharpFixture fixture;
21+
22+
public DevToolsClientFacts(ITestOutputHelper output, CefSharpFixture fixture)
23+
{
24+
this.fixture = fixture;
25+
this.output = output;
26+
}
27+
28+
[Fact]
29+
public void CanConvertDevToolsObjectToDictionary()
30+
{
31+
var bounds = new Bounds
32+
{
33+
Height = 1,
34+
Width = 1,
35+
Left = 1,
36+
Top = 1,
37+
WindowState = WindowState.Fullscreen
38+
};
39+
40+
var dict = bounds.ToDictionary();
41+
42+
Assert.Equal(bounds.Height, (int)dict["height"]);
43+
Assert.Equal(bounds.Width, (int)dict["width"]);
44+
Assert.Equal(bounds.Top, (int)dict["top"]);
45+
Assert.Equal(bounds.Left, (int)dict["left"]);
46+
Assert.Equal("fullscreen", (string)dict["windowState"]);
47+
}
48+
49+
50+
[Fact]
51+
public async Task CanGetDevToolsProtocolVersion()
52+
{
53+
using (var browser = new ChromiumWebBrowser("www.google.com"))
54+
{
55+
await browser.LoadPageAsync();
56+
57+
using (var devToolsClient = browser.GetDevToolsClient())
58+
{
59+
var response = await devToolsClient.Browser.GetVersionAsync();
60+
var jsVersion = response.JsVersion;
61+
var revision = response.Revision;
62+
63+
Assert.NotNull(jsVersion);
64+
Assert.NotNull(revision);
65+
66+
output.WriteLine("DevTools Revision {0}", revision);
67+
}
68+
}
69+
}
70+
71+
[Fact]
72+
public async Task CanCanEmulate()
73+
{
74+
using (var browser = new ChromiumWebBrowser("www.google.com"))
75+
{
76+
await browser.LoadPageAsync();
77+
78+
using (var devToolsClient = browser.GetDevToolsClient())
79+
{
80+
var response = await devToolsClient.Emulation.CanEmulateAsync();
81+
82+
Assert.True(response.Result);
83+
}
84+
}
85+
}
86+
87+
[Fact]
88+
public async Task CanGetPageNavigationHistory()
89+
{
90+
using (var browser = new ChromiumWebBrowser("www.google.com"))
91+
{
92+
await browser.LoadPageAsync();
93+
94+
using (var devToolsClient = browser.GetDevToolsClient())
95+
{
96+
var response = await devToolsClient.Page.GetNavigationHistoryAsync();
97+
var currentIndex = response.CurrentIndex;
98+
var entries = response.Entries;
99+
100+
Assert.Equal(0, currentIndex);
101+
Assert.NotNull(entries);
102+
Assert.True(entries.Count > 0);
103+
Assert.Equal(CefSharp.DevTools.Page.TransitionType.Typed, entries[0].TransitionType);
104+
}
105+
}
106+
}
107+
108+
[Theory]
109+
[InlineData("CefSharpTest", "CefSharp Test Cookie", CefExample.ExampleDomain, CookieSameSite.None)]
110+
[InlineData("CefSharpTest1", "CefSharp Test Cookie2", CefExample.ExampleDomain, CookieSameSite.Lax)]
111+
public async Task CanSetCookieForDomain(string name, string value, string domain, CookieSameSite sameSite)
112+
{
113+
using (var browser = new ChromiumWebBrowser("www.google.com"))
114+
{
115+
await browser.LoadPageAsync();
116+
117+
using (var devToolsClient = browser.GetDevToolsClient())
118+
{
119+
var response = await devToolsClient.Network.SetCookieAsync(name, value, domain: domain, sameSite:sameSite);
120+
Assert.True(response.Success);
121+
}
122+
}
123+
}
124+
125+
}
126+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Copyright © 2020 The CefSharp Authors. All rights reserved.
2+
//
3+
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
4+
5+
using System.Threading.Tasks;
6+
using CefSharp.OffScreen;
7+
using CefSharp.Web;
8+
using Xunit;
9+
using Xunit.Abstractions;
10+
11+
namespace CefSharp.Test.PostMessage
12+
{
13+
/// <summary>
14+
/// This is more a set of integration tests than it is unit tests, for now we need to
15+
/// run our QUnit tests in an automated fashion and some other testing.
16+
/// </summary>
17+
//TODO: Improve Test Naming, we need a naming scheme that fits these cases that's consistent
18+
//(Ideally we implement consistent naming accross all test classes, though I'm open to a different
19+
//naming convention as these are more integration tests than unit tests).
20+
//NOTE: All Test classes must be part of this collection as it manages the Cef Initialize/Shutdown lifecycle
21+
[Collection(CefSharpFixtureCollection.Key)]
22+
public class IntegrationTestFacts
23+
{
24+
private readonly ITestOutputHelper output;
25+
private readonly CefSharpFixture fixture;
26+
27+
public IntegrationTestFacts(ITestOutputHelper output, CefSharpFixture fixture)
28+
{
29+
this.fixture = fixture;
30+
this.output = output;
31+
}
32+
33+
[Theory]
34+
[InlineData("Event", "Event1")]
35+
[InlineData("Event", "Event2")]
36+
[InlineData("CustomEvent", "Event1")]
37+
[InlineData("CustomEvent", "Event2")]
38+
public async Task JavascriptCustomEvent(string jsEventObject, string eventToRaise)
39+
{
40+
const string Script = @"
41+
const postMessageHandler = e => { cefSharp.postMessage(e.type); };
42+
window.addEventListener(""Event1"", postMessageHandler, false);
43+
window.addEventListener(""Event2"", postMessageHandler, false);";
44+
45+
string rawHtml = $"<html><head><script>window.dispatchEvent(new {jsEventObject}(\"{eventToRaise}\"));</script></head><body><h1>testing</h1></body></html>";
46+
int scriptId = 0;
47+
48+
var tcs = new TaskCompletionSource<string>(TaskCreationOptions.RunContinuationsAsynchronously);
49+
50+
//Load a dummy page initially so we can then add our script using
51+
//Page.AddScriptToEvaluateOnNewDocument (via DevTools)
52+
using (var browser = new ChromiumWebBrowser(new HtmlString("Initial Load")))
53+
{
54+
await browser.LoadPageAsync();
55+
56+
using (var devToolsClient = browser.GetDevToolsClient())
57+
{
58+
var result = await devToolsClient.Page.AddScriptToEvaluateOnNewDocumentAsync(Script);
59+
scriptId = int.Parse(result.Identifier);
60+
61+
//We must use Page.Enable for the script to be added
62+
await devToolsClient.Page.EnableAsync();
63+
}
64+
65+
browser.LoadHtml(rawHtml);
66+
67+
browser.JavascriptMessageReceived += (o, e) =>
68+
{
69+
tcs.SetResult((string)e.Message);
70+
};
71+
72+
var responseFromJavascript = await tcs.Task;
73+
74+
Assert.True(scriptId > 0);
75+
Assert.Equal(eventToRaise, responseFromJavascript);
76+
}
77+
}
78+
}
79+
}

0 commit comments

Comments
 (0)