From 67568ae36ff461f445a7c04106e62dfa3d9fe75b Mon Sep 17 00:00:00 2001 From: Alberto Balbo Date: Tue, 20 Oct 2026 17:39:22 +0200 Subject: [PATCH] Prova con Chromium vero: la vista remota avviata prima del commit di una pagina MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Nessuna prova con un browser vero arrivava al ritentativo di Page.startScreencast: TwoViews… prova solo il permesso di passaggio e il ritentativo girava solo su FakeCdpSession. Se Chromium cambiasse testo o codice del rifiuto, la CI resterebbe verde e Alberto ritroverebbe «Il browser di Febe non risponde». La pagina /occupata del sito locale delle prove tiene occupato il renderer per 2 s: la navigazione verso un'altra pagina dello stesso sito resta fra «ready to commit» e il commit, e l'avvio dei fotogrammi ci cade apposta. In ogni giro la vista deve aprirsi e mostrare un JPEG. Se nessun giro incontra il rifiuto, la prova è inconcludente: il ritentativo non è stato messo alla prova. Co-Authored-By: Claude Opus 5.5 --- .../RemoteViewDuringNavigationTests.cs | 129 ++++++++++++++++++ .../Browser/TestSupport/LocalTestSite.cs | 4 + 2 files changed, 133 insertions(+) create mode 100644 tests/Febe.Tests/Browser/RemoteViewDuringNavigationTests.cs diff --git a/tests/Febe.Tests/Browser/RemoteViewDuringNavigationTests.cs b/tests/Febe.Tests/Browser/RemoteViewDuringNavigationTests.cs new file mode 100644 index 0000000..5dd2d0f --- /dev/null +++ b/tests/Febe.Tests/Browser/RemoteViewDuringNavigationTests.cs @@ -0,0 +1,129 @@ +using System.Collections.Concurrent; +using System.Diagnostics; +using Febe.Browser.Cdp; +using Febe.Browser.Diagnostics; +using Febe.Browser.RemoteView; +using Febe.Tests.Browser.TestSupport; + +namespace Febe.Tests.Browser; + +/// +/// The remote view started while the page is between two documents (Gitea issue #3), on a real Chromium on Xvfb and the +/// local test site. From ReadyToCommitNavigation of a navigation in a new RenderFrameHost until its commit, +/// DevTools points at the new, not yet active document and Chromium refuses Page.startScreencast with -32000 +/// «Not attached to an active page». That moment lasts milliseconds in real use; here a page keeps its renderer busy, so +/// a navigation away from it to a page of the same site waits to commit for over a second and the start lands in it on +/// purpose. The view must open once the new document has committed and show its frames. If Chromium ever answers that +/// moment with another text or code, the start fails here, on the CI, and not in Alberto's hands. +/// +/// +/// Seen with Chrome 155 on the development machine (a standalone CDP experiment, same pages): the start was refused in +/// every round to a page of the same site and went through once the new document had committed. A navigation to +/// about:blank from a site page commits in another renderer, which is not busy, so it is not used here. And a view opened +/// through the host after a navigation has begun does not meet the refusal at all: its Page.getFrameTree is answered +/// only once that navigation has committed (see ServerEndToEndTests.AViewOpenedWhileThePageIsNavigating…). +/// +[TestClass] +[DoNotParallelize] +public sealed class RemoteViewDuringNavigationTests +{ + public TestContext TestContext { get; set; } = null!; + + [TestInitialize] + public void RequireBrowser() + { + if (RealBrowser.UnavailableReason() is { } reason) + { + Assert.Inconclusive(reason); + } + } + + [TestMethod] + [Timeout(180_000, CooperativeCancellation = true)] + public async Task AViewStartedBeforeTheNewDocumentCommitsOpensOnceItHasCommitted() + { + var ct = TestContext.CancellationToken; + await using var browser = await RealBrowser.StartAsync(); + var cdp = browser.Cdp; + + const int rounds = 6; + var refusedRounds = 0; + for (var round = 0; round < rounds; round++) + { + // Page.navigate answers once the new document is ready to commit; the busy page commits a moment later and its + // script holds the renderer from 0.1 s after that, for 2 s. + var busy = await cdp.NavigateAsync(browser.Site.Url("/occupata"), ct); + Assert.IsTrue(busy.Succeeded, busy.ErrorText); + await Task.Delay(TimeSpan.FromMilliseconds(500), ct); + + // The login page of the site, as «Accedi a Instagram» opens it: ready to commit at once, committed only when + // the busy script lets go. + var navigation = cdp.NavigateAsync(browser.Site.Url($"/accesso?giro={round}"), ct); + await Task.Delay(TimeSpan.FromMilliseconds(150), ct); + + var notes = new ConcurrentQueue(); + var log = new DelegateBrowserLog((level, message, _) => + { + if (level == BrowserLogLevel.Debug) + { + notes.Enqueue(message); + } + }); + var view = new RemoteViewSession(cdp, browser.Host.UserDriving, log); + try + { + var watch = Stopwatch.StartNew(); + await view.StartAsync(new ScreencastOptions(Quality: 60, MaxWidth: 1280, MaxHeight: 900), ct); + watch.Stop(); + var navigated = await navigation; + Assert.IsTrue(navigated.Succeeded, $"Round {round}: {navigated.ErrorText}"); + Assert.IsTrue(browser.Host.UserIsDriving, $"Round {round}: the open view holds the browser."); + + // Only the attempt number is in the note (ScreencastSession): «… al tentativo N, dopo un cambio di pagina.» + var retry = notes.FirstOrDefault(n => n.Contains("tentativo", StringComparison.Ordinal)); + if (retry is not null) + { + refusedRounds++; + } + + TestContext.WriteLine($"Giro {round}: vista aperta in {watch.ElapsedMilliseconds} ms; {retry ?? "fotogrammi avviati al primo tentativo."}"); + + // The frames of the new document: an animated page repaints all the time. + var animated = await cdp.NavigateAsync(browser.Site.Url("/animata"), ct); + Assert.IsTrue(animated.Succeeded, animated.ErrorText); + Assert.IsTrue(await JpegArrivesAsync(view.Screencast, ct), $"Round {round}: frames after the view opened."); + } + finally + { + await view.DisposeAsync(); + } + + Assert.IsFalse(browser.Host.UserIsDriving, $"Round {round}: the closed view released the browser."); + } + + TestContext.WriteLine($"Giri in cui Chromium ha rifiutato il primo avvio con «{CdpCommandException.InactivePageMessage}»: {refusedRounds} su {rounds}."); + if (refusedRounds == 0) + { + // Every view opened: the product is fine, but the start never met the refusal, so the retry was not exercised. + Assert.Inconclusive("La vista si è aperta in ogni giro, ma Chromium non ha mai rifiutato il primo avvio: la prova non ha raggiunto il momento fra «ready to commit» e il commit (pagina /occupata, versione di Chromium) e il ritentativo non è stato messo alla prova."); + } + } + + private static async Task JpegArrivesAsync(ScreencastSession screencast, CancellationToken ct) + { + using var timeout = CancellationTokenSource.CreateLinkedTokenSource(ct); + timeout.CancelAfter(TimeSpan.FromSeconds(15)); + try + { + await foreach (var frame in screencast.ReadFramesAsync(timeout.Token)) + { + return frame.Jpeg is [0xFF, 0xD8, ..]; + } + } + catch (OperationCanceledException) when (!ct.IsCancellationRequested) + { + } + + return false; + } +} diff --git a/tests/Febe.Tests/Browser/TestSupport/LocalTestSite.cs b/tests/Febe.Tests/Browser/TestSupport/LocalTestSite.cs index 4c76bd4..866285e 100644 --- a/tests/Febe.Tests/Browser/TestSupport/LocalTestSite.cs +++ b/tests/Febe.Tests/Browser/TestSupport/LocalTestSite.cs @@ -202,6 +202,10 @@ public sealed class LocalTestSite : IDisposable "/spa" => "

Applicazione

", "/verifica" => "

Verifica

", "/accesso" => "

Accesso

", + + // Keeps its renderer busy for 2 s, from 0.1 s after it has committed: a navigation away from it to a page of the + // same site (same renderer) reaches «ready to commit» at once but commits only when the script lets go. + "/occupata" => "

Occupata

", _ => "

Pagina di prova

", };