From 4a69e0391fde7ddf2f8e02d5363a3e30beadf1fb Mon Sep 17 00:00:00 2001 From: Max Katz Date: Sun, 11 Oct 2026 22:46:50 -0700 Subject: [PATCH] Wait for navigation to complete in headless tests instead of a fixed delay The headless adapter completes navigation on the thread pool and reports it back through the dispatcher, so the fixed 20 ms DoDelay was not always enough. Tests that assert on NavigationCompleted now pump the dispatcher until it fires, with a timeout. This also re-enables NativeWebView_NavigateToString_With_BaseUri, which was skipped as flaky for the same reason. Co-Authored-By: Claude Opus 5.5 --- .../HeadlessAdapterTests.cs | 10 +++++----- .../HeadlessTestsBase.cs | 18 ++++++++++++++++++ .../NativeWebViewTests.cs | 4 ++-- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/tests/Avalonia.Controls.WebView.Tests/HeadlessAdapterTests.cs b/tests/Avalonia.Controls.WebView.Tests/HeadlessAdapterTests.cs index a8f5429f..fb591b64 100644 --- a/tests/Avalonia.Controls.WebView.Tests/HeadlessAdapterTests.cs +++ b/tests/Avalonia.Controls.WebView.Tests/HeadlessAdapterTests.cs @@ -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); @@ -89,14 +89,14 @@ public async Task NativeWebView_NavigateToString_And_Refresh() window.Show(); webView.NavigateToString("test"); - 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); } @@ -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(); @@ -403,7 +403,7 @@ public async Task NativeWebView_NavigateToString_With_BaseUri() var baseUri = new Uri("https://example.com/"); webView.NavigateToString("test", baseUri); - await DoDelay(); + await WaitFor(() => navCompleted); Assert.True(navCompleted); Assert.Equal(baseUri, webView.Source); diff --git a/tests/Avalonia.Controls.WebView.Tests/HeadlessTestsBase.cs b/tests/Avalonia.Controls.WebView.Tests/HeadlessTestsBase.cs index e3f6c73a..3ab13af2 100644 --- a/tests/Avalonia.Controls.WebView.Tests/HeadlessTestsBase.cs +++ b/tests/Avalonia.Controls.WebView.Tests/HeadlessTestsBase.cs @@ -1,4 +1,5 @@ using System; +using System.Diagnostics; using System.Threading.Tasks; using Avalonia.Threading; @@ -18,6 +19,23 @@ public async Task DoDelay() Dispatcher.UIThread.RunJobs(); } + /// + /// Pumps the dispatcher until 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. + /// + public async Task WaitFor(Func 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(TaskCreationOptions.RunContinuationsAsynchronously); diff --git a/tests/Avalonia.Controls.WebView.Tests/NativeWebViewTests.cs b/tests/Avalonia.Controls.WebView.Tests/NativeWebViewTests.cs index 040f1c84..7a849ac8 100644 --- a/tests/Avalonia.Controls.WebView.Tests/NativeWebViewTests.cs +++ b/tests/Avalonia.Controls.WebView.Tests/NativeWebViewTests.cs @@ -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); @@ -107,7 +107,7 @@ public async Task Should_NavigateToString_If_Called_Before_Window_Show() webView.NavigateToString("abc"); // before adding to window window.Content = webView; window.Show(); - await DoDelay(); + await WaitFor(() => navCompleted); Assert.Equal(WebViewHelper.EmptyPage, webView.Source); Assert.True(navCompleted);