Skip to content
Merged
Show file tree
Hide file tree
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
10 changes: 5 additions & 5 deletions tests/Avalonia.Controls.WebView.Tests/HeadlessAdapterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public async Task NativeWebView_Navigation_And_Events()

var uri = new Uri("https://example.com");
webView.Source = uri;
await DoDelay();
await WaitFor(() => navCompleted);

Assert.True(navStarted);
Assert.True(navCompleted);
Expand All @@ -89,14 +89,14 @@ public async Task NativeWebView_NavigateToString_And_Refresh()
window.Show();

webView.NavigateToString("<html>test</html>");
await DoDelay();
await WaitFor(() => navCompleted);

Assert.True(navCompleted);
Assert.Equal(WebViewHelper.EmptyPage, webView.Source);

navCompleted = false;
Assert.True(webView.Refresh());
await DoDelay();
await WaitFor(() => navCompleted);
Assert.True(navCompleted);
}

Expand Down Expand Up @@ -390,7 +390,7 @@ private async Task ReattachAsync(Panel panel, NativeWebView webView)
await DoDelay();
}

[AvaloniaFact(Skip = "Flaky test")]
[AvaloniaFact]
public async Task NativeWebView_NavigateToString_With_BaseUri()
{
var window = new Window();
Expand All @@ -403,7 +403,7 @@ public async Task NativeWebView_NavigateToString_With_BaseUri()

var baseUri = new Uri("https://example.com/");
webView.NavigateToString("<html>test</html>", baseUri);
await DoDelay();
await WaitFor(() => navCompleted);

Assert.True(navCompleted);
Assert.Equal(baseUri, webView.Source);
Expand Down
18 changes: 18 additions & 0 deletions tests/Avalonia.Controls.WebView.Tests/HeadlessTestsBase.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using Avalonia.Threading;

Expand All @@ -18,6 +19,23 @@ public async Task DoDelay()
Dispatcher.UIThread.RunJobs();
}

/// <summary>
/// Pumps the dispatcher until <paramref name="condition"/> is true or the timeout runs out.
/// The headless adapter completes navigation on the thread pool and reports it back through the dispatcher,
/// so a fixed delay is not enough on a busy machine. On timeout this returns and the caller's assert fails.
/// </summary>
public async Task WaitFor(Func<bool> condition, int timeoutMs = 5000)
{
var stopwatch = Stopwatch.StartNew();
while (true)
{
Dispatcher.UIThread.RunJobs();
if (condition() || stopwatch.ElapsedMilliseconds > timeoutMs)
return;
await Task.Delay(5);
}
}

protected Task WaitForAdapterCreation(NativeWebView webView)
{
var tcs = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
Expand Down
4 changes: 2 additions & 2 deletions tests/Avalonia.Controls.WebView.Tests/NativeWebViewTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public async Task Should_Navigate_If_Navigate_Called_Before_Window_Show()
webView.Navigate(uri); // before adding to window
window.Content = webView;
window.Show();
await DoDelay();
await WaitFor(() => navCompleted);

Assert.Equal(uri, webView.Source);
Assert.True(navCompleted);
Expand All @@ -107,7 +107,7 @@ public async Task Should_NavigateToString_If_Called_Before_Window_Show()
webView.NavigateToString("<html>abc</html>"); // before adding to window
window.Content = webView;
window.Show();
await DoDelay();
await WaitFor(() => navCompleted);

Assert.Equal(WebViewHelper.EmptyPage, webView.Source);
Assert.True(navCompleted);
Expand Down
Loading