Nuovo progetto tests/Febe.Tests.Windows (net10.0-windows, WPF, solo MSTest e riferimenti ai progetti): un thread STA con un Dispatcher di WPF vero, come il thread dell'interfaccia di Febe.exe, e il browser di Febe costruito come lo costruisce la composizione e avviato da un thread del pool, come fa FebeApp. Si compila anche da Linux (EnableWindowsTargeting, come Febe.Desktop) perché la CI compila tutta la soluzione, ma gira solo su Windows; dove manca il runtime WebView2 le prove risultano inconcludenti. La prima prova riproduce il registro di Alberto del 14 e del 19 ottobre: «WebView2 non parte (InvalidOperationException: CoreWebView2Environment members can only be accessed from the UI thread.)» a ogni tentativo. Con il codice di questo commit fallisce così; la correzione è nel commit seguente. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
101 lines
3.5 KiB
C#
101 lines
3.5 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, WebView2Browser browser)
|
|
{
|
|
Folder = folder;
|
|
Ui = ui;
|
|
Lines = lines;
|
|
Log = log;
|
|
Database = database;
|
|
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; }
|
|
|
|
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 context = new FebePlatformContext(options, data, database, clock, log, new BrowserUseTracker());
|
|
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, browser);
|
|
}
|
|
|
|
/// <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();
|
|
}
|
|
}
|
|
}
|