Files
Febe/tests/Febe.Tests.Windows/Support/BrowserBed.cs
T
Alby96andClaude Opus 5.5 ccfd8b5c08 Browser di Febe per Windows: ripartenze robuste, WebView2 toccato solo dal suo thread
Dopo la correzione dell'avvio, la revisione di tutto il percorso del browser per Windows ha
trovato altri punti della stessa famiglia: lavoro che, se va storto, lascia il browser morto o
riprova per sempre.

- WebView2Host: avvio, ripartenza, chiusura dopo un avvio fallito e arresto passano uno alla
  volta (ognuno aspetta WebView2 sul thread dell'interfaccia e prima potevano intrecciarsi);
  un arresto del processo del browser durante un avvio fa fallire quell'avvio invece di
  lasciare un WebView morto dato per buono; l'arresto del processo si annuncia solo dopo che il
  gestore dell'evento di WebView2 è tornato (WebView2 non regge chiusure e ricreazioni dentro i
  suoi gestori) e solo per il WebView in uso; versione e numero del processo sono valori
  semplici letti sul thread dell'interfaccia; MoveFocus, Bounds, Indietro e Ricarica non fanno
  più cadere eccezioni nell'interfaccia se il processo è appena morto; in primo piano il
  permesso di guida si prende prima di spostare il fuoco.
- WebView2Browser: dopo un arresto del processo e dopo un «Scollega» la cui ripartenza fallisce,
  il browser riparte con gli stessi tentativi del primo avvio (prima: un tentativo solo, poi
  morto fino al riavvio di Febe; e con «Scollega» anche il motore restava fermo, perché
  l'errore non era di cartella); un solo ciclo di ripartenza alla volta, sul pool e mai dentro
  un gestore di WebView2; la cancellazione del profilo non blocca più la finestra.

Prove nuove su WebView2 vero: un avvio che fallisce davvero si riprova una volta sola e poi
va; la finestra viene in primo piano e torna fuori vista con il permesso di guida, anche
chiudendola; «Scollega» cancella profilo e sessione e il browser riparte vuoto; ucciso (per
numero di processo) il browser avviato dalla prova, Febe lo fa ripartire da solo; l'ospitalità
«composizione» di S2 parte e disegna.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-20 10:29:59 +02:00

124 lines
4.4 KiB
C#

using Febe.App;
using Febe.Core.Abstractions;
using Febe.Core.Library;
using Febe.Data;
using Febe.Desktop.Browser;
using Febe.Desktop.Core;
using Febe.Web.Engine;
namespace Febe.Tests.Windows.Support;
/// <summary>
/// The browser of Febe for Windows on its own, as the composition builds it (<see cref="DesktopPlatform"/> through
/// <c>DesktopShell</c>): a <see cref="WebView2Browser"/> with its window «Browser di Febe» and WebView2 on a real UI
/// thread, a profile in a temporary Data folder, the switches of S2 and the log of Febe captured.
/// </summary>
internal sealed class BrowserBed : IAsyncDisposable
{
private BrowserBed(TestFolder folder, UiThread ui, LogLines lines, FebeLog log, FebeDatabase database, BrowserUseTracker tracker, WebView2Browser browser)
{
Folder = folder;
Ui = ui;
Lines = lines;
Log = log;
Database = database;
Tracker = tracker;
Browser = browser;
}
public TestFolder Folder { get; }
public UiThread Ui { get; }
/// <summary>What Febe wrote in its log.</summary>
public LogLines Lines { get; }
public FebeLog Log { get; }
public FebeDatabase Database { get; }
/// <summary>When Alberto last used the browser («Riprendi» after a verification).</summary>
public BrowserUseTracker Tracker { get; }
public WebView2Browser Browser { get; }
public static async Task<BrowserBed> CreateAsync(string name, BrowserHosting hosting = BrowserHosting.Window)
{
var folder = new TestFolder(name);
var ui = await UiThread.StartAsync();
var lines = new LogLines();
var clock = new SystemClock();
var log = new FebeLog(null, lines, clock);
var data = new DataFolderLayout(folder.Combine("dati"));
var database = await FebeDatabase.OpenAsync(data.Root);
var options = new FebeAppOptions { DataFolder = data.Root, LibraryFolder = folder.Combine("libreria") };
var tracker = new BrowserUseTracker();
var context = new FebePlatformContext(options, data, database, clock, log, tracker);
var settings = new WebView2BrowserSettings(data.BrowserProfile, 1280, 900, hosting, BrowserArguments.ForWebView2());
// Built off the UI thread, as DesktopPlatform.CreateBrowserAsync does after its awaits.
var browser = new WebView2Browser(ui.Dispatcher, settings, context, icon: null);
return new BrowserBed(folder, ui, lines, log, database, tracker, browser);
}
/// <summary>Waits (real time) until <paramref name="condition"/> holds; fails with <paramref name="what"/> and the log otherwise.</summary>
public async Task WaitUntilAsync(Func<bool> condition, TimeSpan timeout, string what, CancellationToken ct)
{
var deadline = DateTime.UtcNow + timeout;
while (!condition())
{
if (DateTime.UtcNow > deadline)
{
Assert.Fail($"Tempo scaduto: {what}.\n{Lines.Text}");
}
await Task.Delay(50, ct);
}
}
/// <summary>Reads a value of the UI thread (the window, its position) through the dispatcher.</summary>
public Task<T> OnUiAsync<T>(Func<T> read) => Ui.InvokeAsync(read);
/// <summary>
/// Starts the browser from a thread-pool thread, like <c>FebeApp.BringUpBrowserAsync</c>, and waits until
/// <see cref="WebView2Browser.RunAsync"/> returns. Fails at once when the log reports a failed start: the loop of
/// retries would otherwise go on for ever.
/// </summary>
public async Task StartAsync(CancellationToken ct, TimeSpan? timeout = null)
{
var run = Task.Run(() => Browser.RunAsync(ct), ct);
var deadline = DateTime.UtcNow + (timeout ?? TimeSpan.FromSeconds(60));
while (!run.IsCompleted)
{
if (Lines.Any("non parte"))
{
Assert.Fail("Il browser di Febe non è partito al primo tentativo:\n" + Lines.Text);
}
if (DateTime.UtcNow > deadline)
{
Assert.Fail("Il browser di Febe non è diventato disponibile in tempo:\n" + Lines.Text);
}
await Task.Delay(50, ct);
}
await run;
}
public async ValueTask DisposeAsync()
{
try
{
await Browser.DisposeAsync();
}
finally
{
await Ui.DisposeAsync();
await Database.DisposeAsync();
Log.Dispose();
Folder.Dispose();
}
}
}