using System.Diagnostics;
using Febe.Browser.Cdp;
using Febe.Browser.RemoteView;
using Febe.Tests.Browser.TestSupport;
namespace Febe.Tests.Browser;
///
/// The remote view that stayed white (Gitea issue #52), on a real Chromium on Xvfb and the local test site: a page that
/// does not move still shows itself, and the frames keep coming when the page changes renderer (another site, the rest
/// page and back). Only in the Linux CI; never Instagram. The tests ask that a picture arrives, not how fast: the waits are
/// generous for a busy runner (issue #46), and the time it took goes to the test's output (about two seconds, the
/// watchdog's silence, at most five on a calm machine).
///
[TestClass]
[DoNotParallelize]
public sealed class RemoteViewFramesTests
{
/// How long a picture may take on a busy runner before the view counts as white.
private static readonly TimeSpan Patience = TimeSpan.FromSeconds(20);
public TestContext TestContext { get; set; } = null!;
[TestInitialize]
public void RequireBrowser()
{
if (RealBrowser.UnavailableReason() is { } reason)
{
Assert.Inconclusive(reason);
}
}
[TestMethod]
[Timeout(120_000, CooperativeCancellation = true)]
public async Task AStillPageAndTheRestPageShowAPicture()
{
var ct = TestContext.CancellationToken;
await using var browser = await RealBrowser.StartAsync();
var cdp = browser.Cdp;
var opened = await cdp.NavigateAsync(browser.Site.Url("/accesso"), ct);
Assert.IsTrue(opened.Succeeded, opened.ErrorText);
await cdp.WaitForLoadAsync(new Febe.Core.Abstractions.SystemClock(), TimeSpan.FromSeconds(15), ct);
var view = new RemoteViewSession(cdp, browser.Host.UserDriving);
try
{
await view.StartAsync(new ScreencastOptions(Quality: 60, MaxWidth: 1280, MaxHeight: 900, Watchdog: ScreencastWatchdog.Default), ct);
var watch = Stopwatch.StartNew();
Assert.IsTrue(await PictureAfterAsync(view.Screencast, null, Patience, ct), "a picture of the login page");
TestContext.WriteLine($"pagina di accesso: immagine dopo {watch.ElapsedMilliseconds} ms.");
var last = view.Screencast.LastFrame;
var rest = await cdp.RestAsync(ct);
Assert.IsTrue(rest.Succeeded, rest.ErrorText);
watch.Restart();
Assert.IsTrue(await PictureAfterAsync(view.Screencast, last, Patience, ct), "a picture of the rest page too");
TestContext.WriteLine($"pagina di riposo: immagine dopo {watch.ElapsedMilliseconds} ms, riavvii {view.Screencast.Restarts}.");
}
finally
{
await view.DisposeAsync();
}
}
[TestMethod]
[Timeout(180_000, CooperativeCancellation = true)]
public async Task FramesKeepComingWhenThePageChangesRenderer()
{
var ct = TestContext.CancellationToken;
await using var browser = await RealBrowser.StartAsync();
var cdp = browser.Cdp;
var first = await cdp.NavigateAsync(browser.Site.Url("/animata"), ct);
Assert.IsTrue(first.Succeeded, first.ErrorText);
var view = new RemoteViewSession(cdp, browser.Host.UserDriving);
try
{
await view.StartAsync(new ScreencastOptions(Quality: 60, MaxWidth: 1280, MaxHeight: 900, Watchdog: ScreencastWatchdog.Default), ct);
Assert.IsTrue(await FramesAfterAsync(view.Screencast, ct), "frames of the animated page");
foreach (var (address, moving) in new[]
{
(browser.Site.LocalhostUrl("/animata"), true),
("about:blank", false),
(browser.Site.Url("/animata"), true),
})
{
var last = view.Screencast.LastFrame;
var watch = Stopwatch.StartNew();
var navigated = await cdp.NavigateAsync(address, ct);
Assert.IsTrue(navigated.Succeeded, $"{address}: {navigated.ErrorText}");
var shown = moving
? await FramesAfterAsync(view.Screencast, ct)
: await PictureAfterAsync(view.Screencast, last, Patience, ct);
TestContext.WriteLine($"{address}: {(shown ? "immagini" : "niente")} dopo {watch.ElapsedMilliseconds} ms, riavvii {view.Screencast.Restarts}.");
Assert.IsTrue(shown, $"{address}: the view is not white");
}
}
finally
{
await view.DisposeAsync();
}
}
/// A frame or a still picture other than , within .
private static async Task PictureAfterAsync(ScreencastSession screencast, ScreencastFrame? last, TimeSpan timeout, CancellationToken ct)
{
var deadline = Stopwatch.StartNew();
while (deadline.Elapsed < timeout)
{
if (screencast.LastFrame is { Jpeg: [0xFF, 0xD8, ..] } frame && !ReferenceEquals(frame, last))
{
return true;
}
await Task.Delay(50, ct);
}
return false;
}
/// A few real frames of the screencast (not still pictures) after now, within .
private static async Task FramesAfterAsync(ScreencastSession screencast, CancellationToken ct)
{
var before = screencast.FramesReceived;
var deadline = Stopwatch.StartNew();
while (deadline.Elapsed < Patience)
{
if (screencast.FramesReceived >= before + 3)
{
return true;
}
await Task.Delay(50, ct);
}
return false;
}
}