Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add tests for more coverage of WebView2 #18706

Merged
merged 4 commits into from
Nov 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -419,5 +419,77 @@ function sendWebMessage(){

Assert.AreEqual(@"{""some"":[""values"",""in"",""json"",1]}", message);
}

[TestMethod]
[Ignore("WebResourceResponseReceived is not yet implemented")]
public async Task When_Navigate_Error()
{
var border = new Border();
var webView = new WebView2();
webView.Width = 200;
webView.Height = 200;
border.Child = webView;
TestServices.WindowHelper.WindowContent = border;
await TestServices.WindowHelper.WaitForLoaded(border);
var uri = new Uri("https://httpbin.org/status/444");
await webView.EnsureCoreWebView2Async();
bool navigationStarting = false;
int statusCode = -1;
CoreWebView2WebErrorStatus navigationStatus = CoreWebView2WebErrorStatus.Unknown;
// TODO: WebResourceResponseReceived is not implemented
webView.CoreWebView2.WebResourceResponseReceived += (s, e) =>
statusCode = e.Response.StatusCode;
webView.NavigationStarting += (s, e) => navigationStarting = true;
webView.NavigationCompleted += (s, e) =>
navigationStatus = e.WebErrorStatus;
webView.CoreWebView2.Navigate(uri.ToString());
Assert.IsNull(webView.Source);
await TestServices.WindowHelper.WaitFor(() => navigationStarting, 3000);
await TestServices.WindowHelper.WaitFor(() => navigationStatus != CoreWebView2WebErrorStatus.Unknown, 3000);
Assert.IsNotNull(webView.Source);
Assert.IsTrue(webView.Source.OriginalString.StartsWith("https://httpbin.org/status/444", StringComparison.OrdinalIgnoreCase));
}

#if !WINAPPSDK && !__ANDROID__
[TestMethod]
[DataRow(true)]
[DataRow(false)]
public async Task When_Navigate_Unsupported_Scheme(bool handled)
{
var border = new Border();
var webView = new WebView2();
webView.Width = 200;
webView.Height = 200;
border.Child = webView;
TestServices.WindowHelper.WindowContent = border;
await TestServices.WindowHelper.WaitForLoaded(border);
var uri = new Uri("notsupported://httpbin.org/");
await webView.EnsureCoreWebView2Async();
bool navigationStarting = false;
bool navigationDone = false;
string scheme = null;
webView.NavigationStarting += (s, e) => navigationStarting = true;
webView.NavigationCompleted += (s, e) => navigationDone = true;
webView.CoreWebView2.UnsupportedUriSchemeIdentified += (s, e) =>
spouliot marked this conversation as resolved.
Show resolved Hide resolved
{
scheme = e.Uri.Scheme;
e.Handled = handled;
};
webView.CoreWebView2.Navigate(uri.ToString());
Assert.IsNull(webView.Source);
await TestServices.WindowHelper.WaitFor(() => scheme == "notsupported", 3000);
if (handled)
{
Assert.IsFalse(navigationStarting);
Assert.IsFalse(navigationDone);
}
else
{
await TestServices.WindowHelper.WaitFor(() => navigationStarting, 3000);
await TestServices.WindowHelper.WaitFor(() => navigationDone, 3000);
}
}
#endif // !WINAPPSDK && !__ANDROID__
}

#endif
Loading