Files
Febe/tests/Febe.Tests/Desktop/BrowserStartPageTests.cs
T
Alby96andClaude Opus 5.5 c49df4ca7b Browser mai bianco né muto: pagina iniziale, finestra che resta, fotogrammi che ripartono (#52)
Il browser di Febe a volte era bianco o non mostrava nulla. Le cause erano
diverse e si correggono insieme.

- Pagina iniziale. Se il browser è a riposo su about:blank e Alberto lo apre
  («Usa il browser», «Apri il browser»), Febe lo porta sulla home del sito
  quando è collegato, altrimenti sulla pagina di accesso. È un'azione di
  Alberto, sotto il suo permesso di guida, non del motore: Febe aspetta che
  la sessione finisca e prende il turno della pagina.
  Il nuovo pulsante «Pagina iniziale» sta nella barra della finestra di
  Windows e nella vista remota. Su about:blank la barra dice «Il browser è a
  riposo su una pagina vuota». «Indietro» non torna più sulla pagina di
  riposo.
- Disconnessione. «Apri il browser» nella striscia rossa apre la pagina di
  accesso. Con --accesso-di-prova il primo controllo all'avvio non segnala
  più una disconnessione finta.
- Windows: la finestra resta.
  - Lasciare una pagina di Febe non nasconde più la finestra «Browser di
    Febe» e non la rimette a riposo: finisce solo il permesso tenuto dalla
    pagina (cancel con pageLeft, IConnectionService.ReleaseAsync).
  - Sullo schermo la finestra ha il suo pulsante nella barra delle
    applicazioni, così ridurla a icona non la fa sparire. Dopo 2 minuti
    ridotta a icona torna fuori vista (S2).
  - Se lo schermo è troppo piccolo, la finestra si restringe all'area di
    lavoro e la pagina si rimpicciolisce con ZoomFactor, con le stesse misure
    CSS.
  - I bordi della pagina si riapplicano, con NotifyParentWindowPositionChanged,
    quando la finestra si mostra, si sposta o riparte il processo GPU.
  - Una pagina che non risponde mentre Alberto guida resta com'è.
  - IBrowserWindow.ShowAsync lascia il posto a
    IConnectionService.ShowBrowserAsync.
- Docker: fotogrammi che ripartono. La vista remota ha un watchdog. Riavvia
  i fotogrammi dopo una pagina nuova caricata senza fotogrammi e dopo circa
  2 s di silenzio. L'attesa raddoppia fino a 30 s finché la pagina resta
  ferma. Se il riavvio non porta nulla, la vista riceve un'immagine ferma
  (Page.captureScreenshot, già fra i comandi ammessi).
  RemoteViewSession ha ora un registro vero: avvii, riavvii e silenzi vanno
  a livello Info, categoria «vista», senza indirizzi.
  Se dopo 8 s non arriva nessuna immagine, l'interfaccia mostra «Nessuna
  immagine dal browser» con «Riprova».

Nessun comando CDP nuovo e nessuna dipendenza nuova. Aggiornati PROTOCOLLO.md
e la guida (Primo collegamento, Attività e arresti, Installazione Windows,
Problemi comuni).

Prove nuove:
- ScreencastWatchdogTests, RemoteViewFramesTests (Chromium vero, solo CI
  Linux)
- Home e Indietro in RemoteViewStartTests
- RemoteViewSocketTests
- ConnectionApiTests
- BrowserStartPageTests
- BrowserWindowDecisionTests e BrowserWindowTests (WebView2 vero)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 12:51:12 +01:00

197 lines
9.5 KiB
C#

using System.Net;
using Febe.Core.Abstractions;
using Febe.Tests.Desktop.Support;
using Febe.Tests.Support;
namespace Febe.Tests.Desktop;
/// <summary>
/// The browser that showed nothing (Gitea issue #52), in the shared composition with the fake window «Browser di Febe»:
/// opened from the empty rest page it goes to a page of its own, «Apri il browser» of a disconnection opens the login,
/// leaving a page of Febe leaves the window as it is, and the test site's automatic login raises no false alarm.
/// Never Instagram: the fake browser answers by itself.
/// </summary>
[TestClass]
public sealed class BrowserStartPageTests
{
private const string Login = "https://www.instagram.com/accounts/login/";
private const string Home = "https://www.instagram.com/";
public TestContext TestContext { get; set; } = null!;
[TestMethod]
[Timeout(60_000, CooperativeCancellation = true)]
public async Task UsaIlBrowserFromTheRestPageOpensTheLoginOrTheHomeAndAnyOtherPageStays()
{
var ct = TestContext.CancellationToken;
await using var bed = await DesktopTestBed.StartAsync();
await bed.App.Running.WaitAsync(TimeSpan.FromSeconds(20), ct);
var browser = bed.Browser!;
Assert.AreEqual("about:blank", browser.FakeCdp.Url);
// Not connected: never a white window, the login page.
await BeginAsync(bed, openLogin: false);
Assert.AreEqual(Login, browser.FakeCdp.Url);
Assert.AreEqual(1, browser.Shown, "the window came to the front");
browser.FakeCdp.LoggedIn = true;
var finish = await bed.JsonAsync(HttpMethod.Post, "/api/connection/finish");
Assert.IsTrue(finish.GetProperty("connected").GetBoolean());
await DesktopTestBed.WaitUntilAsync(() => browser.FakeCdp.Url == "about:blank", TimeSpan.FromSeconds(10), "la pagina di riposo dopo «Ho finito»");
// Connected: the site's home.
await BeginAsync(bed, openLogin: false);
Assert.AreEqual(Home, browser.FakeCdp.Url);
// A page he opened stays as it is.
browser.FakeCdp.Navigated("https://www.instagram.com/marta.prova/");
var navigations = Navigations(browser);
await BeginAsync(bed, openLogin: false);
Assert.AreEqual("https://www.instagram.com/marta.prova/", browser.FakeCdp.Url);
Assert.AreEqual(navigations, Navigations(browser), "no navigation of his page");
var status = await bed.JsonAsync(HttpMethod.Get, "/api/connection");
Assert.IsTrue(status.GetProperty("userIsDriving").GetBoolean());
}
[TestMethod]
[Timeout(60_000, CooperativeCancellation = true)]
public async Task ApriIlBrowserBringsTheWindowWithItsStartPageAndTheLoginAfterADisconnection()
{
var ct = TestContext.CancellationToken;
await using var bed = await DesktopTestBed.StartAsync();
await bed.App.Running.WaitAsync(TimeSpan.FromSeconds(20), ct);
var browser = bed.Browser!;
// Connected, at rest: «Apri il browser» (Attività) opens the home, the window in front.
await LoginAsync(bed);
await DesktopTestBed.WaitUntilAsync(() => browser.FakeCdp.Url == "about:blank", TimeSpan.FromSeconds(10), "la pagina di riposo dopo «Ho finito»");
var shown = browser.Shown;
var result = await bed.JsonAsync(HttpMethod.Post, "/api/browser/show");
StringAssert.Contains(result.GetProperty("message").GetString(), "primo piano");
Assert.AreEqual(Home, browser.FakeCdp.Url);
Assert.AreEqual(shown + 1, browser.Shown);
Assert.IsTrue(browser.UserIsDriving, "the window on screen holds the engine still");
// Closed with its X: out of view, the engine puts the page to rest.
await browser.HideFromUserAsync(ct);
await DesktopTestBed.WaitUntilAsync(() => browser.FakeCdp.Url == "about:blank", TimeSpan.FromSeconds(10), "la pagina di riposo dopo la chiusura");
// The session goes while nobody looks: a disconnection. The red strip's «Apri il browser» asks for the login.
browser.FakeCdp.LoggedIn = false;
await bed.App.Connection!.RefreshAsync(ct);
var breaker = bed.App.Engine!.Services.Breaker;
await breaker.FlushAsync();
Assert.AreEqual(BreakerStatus.Disconnected, breaker.State.Status);
Assert.AreEqual(Login, bed.App.Connection.StartPageUrl, "disconnected: the start page is the login");
using (var show = await bed.SendAsync(HttpMethod.Post, "/api/browser/show", """{"openLogin":true}"""))
{
Assert.AreEqual(HttpStatusCode.OK, show.StatusCode, await show.Content.ReadAsStringAsync(ct));
}
Assert.AreEqual(Login, browser.FakeCdp.Url);
}
[TestMethod]
[Timeout(60_000, CooperativeCancellation = true)]
public async Task LeavingThePageOfFebeLeavesTheWindowOnScreenButAnnullaPutsItAway()
{
var ct = TestContext.CancellationToken;
await using var bed = await DesktopTestBed.StartAsync();
await bed.App.Running.WaitAsync(TimeSpan.FromSeconds(20), ct);
var browser = bed.Browser!;
await LoginAsync(bed);
await DesktopTestBed.WaitUntilAsync(() => browser.FakeCdp.Url == "about:blank", TimeSpan.FromSeconds(10), "la pagina di riposo dopo «Ho finito»");
await BeginAsync(bed, openLogin: false);
Assert.AreEqual(Home, browser.FakeCdp.Url);
var hidden = browser.Hidden;
// Another page of Febe, the tab closed or reloaded: the page's hold ends, the window stays where it is.
using (var left = await bed.SendAsync(HttpMethod.Post, "/api/connection/cancel", """{"pageLeft":true}"""))
{
Assert.AreEqual(HttpStatusCode.NoContent, left.StatusCode);
}
await Task.Delay(TimeSpan.FromSeconds(1), ct);
Assert.AreEqual(hidden, browser.Hidden, "not put out of view");
Assert.AreEqual(Home, browser.FakeCdp.Url, "not put to rest");
Assert.IsTrue(browser.UserIsDriving, "on screen, the window still holds the engine");
Assert.IsTrue(bed.App.Engine!.Services.Runtime.UserIsDriving);
// «Annulla» from the page is still «Annulla»: out of view and at rest.
await BeginAsync(bed, openLogin: false);
using (var cancel = await bed.SendAsync(HttpMethod.Post, "/api/connection/cancel"))
{
Assert.AreEqual(HttpStatusCode.NoContent, cancel.StatusCode);
}
Assert.AreEqual(hidden + 1, browser.Hidden);
await DesktopTestBed.WaitUntilAsync(() => !browser.UserIsDriving, TimeSpan.FromSeconds(10), "la fine della guida dopo «Annulla»");
Assert.AreEqual("about:blank", browser.FakeCdp.Url);
}
[TestMethod]
[Timeout(60_000, CooperativeCancellation = true)]
public async Task HoFinitoWithoutTheLoginSaysTheSessionIsNotThere()
{
var ct = TestContext.CancellationToken;
await using var bed = await DesktopTestBed.StartAsync();
await bed.App.Running.WaitAsync(TimeSpan.FromSeconds(20), ct);
await BeginAsync(bed, openLogin: true);
var finish = await bed.JsonAsync(HttpMethod.Post, "/api/connection/finish");
Assert.IsFalse(finish.GetProperty("connected").GetBoolean());
StringAssert.Contains(finish.GetProperty("message").GetString(), "non trova la sessione");
}
[TestMethod]
[Timeout(90_000, CooperativeCancellation = true)]
public async Task TheTestSitesAutomaticLoginIsNoDisconnectionAtTheNextStart()
{
var ct = TestContext.CancellationToken;
const string site = "http://127.0.0.1:8481";
string[] args = ["--sito-di-prova", site, "--accesso-di-prova"];
using var temp = new TempFolder("accesso-di-prova");
var locations = new MemoryLocationStore();
FakeDesktopShell Shell() => new() { OnCreated = b => b.FakeCdp.LogsInOnVisit = site };
await using (var bed = await DesktopTestBed.StartAsync(args, Shell(), temp, locations))
{
await bed.App.Running.WaitAsync(TimeSpan.FromSeconds(20), ct);
Assert.IsTrue(bed.App.Connection!.IsConnected, "logged in by the automatic login");
await bed.StopAsync();
}
// The next start: the new browser has no session yet (the test site forgot it); the automatic login opens it again.
await using (var bed = await DesktopTestBed.StartAsync(args, Shell(), temp, locations))
{
await bed.App.Running.WaitAsync(TimeSpan.FromSeconds(20), ct);
var breaker = bed.App.Engine!.Services.Breaker;
await breaker.FlushAsync();
Assert.IsTrue(bed.App.Connection!.IsConnected);
Assert.AreEqual(BreakerStatus.Closed, breaker.State.Status, "not a disconnection: the session the login was about to open");
Assert.IsEmpty(await bed.App.Database.Incidents.ListOpenAsync(ct));
await bed.StopAsync();
}
}
private static int Navigations(FakeBrowser browser) => browser.FakeCdp.Methods.Count(m => m == "Page.navigate");
private static async Task BeginAsync(DesktopTestBed bed, bool openLogin)
{
using var begin = await bed.SendAsync(HttpMethod.Post, "/api/connection/begin", openLogin ? """{"openLogin":true}""" : """{"openLogin":false}""");
Assert.AreEqual(HttpStatusCode.NoContent, begin.StatusCode);
}
private static async Task LoginAsync(DesktopTestBed bed)
{
await BeginAsync(bed, openLogin: true);
bed.Browser!.FakeCdp.LoggedIn = true;
var finish = await bed.JsonAsync(HttpMethod.Post, "/api/connection/finish");
Assert.IsTrue(finish.GetProperty("connected").GetBoolean(), finish.GetProperty("message").GetString());
}
}