test(postit): introduce Avalonia UI collection fixture
Own the headless session lifecycle, centralize window cleanup, migrate subscription page bindings, and guard shared runtime access during the progressive migration. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
parent
972b1a5cce
commit
adb4a8bc42
6 changed files with 219 additions and 19 deletions
69
src/PostIt/PostIt.Tests/Fixtures/AvaloniaUiFixture.cs
Normal file
69
src/PostIt/PostIt.Tests/Fixtures/AvaloniaUiFixture.cs
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
using Avalonia.Controls;
|
||||
using Avalonia.Headless;
|
||||
using Avalonia.Threading;
|
||||
|
||||
namespace PostIt.Tests;
|
||||
|
||||
[CollectionDefinition(Name, DisableParallelization = true)]
|
||||
public sealed class AvaloniaUiCollection : ICollectionFixture<AvaloniaUiFixture>
|
||||
{
|
||||
public const string Name = "Avalonia UI";
|
||||
}
|
||||
|
||||
public sealed class AvaloniaUiFixture : IAsyncLifetime
|
||||
{
|
||||
private HeadlessUnitTestSession? _session;
|
||||
|
||||
public async ValueTask InitializeAsync()
|
||||
{
|
||||
_session = HeadlessUnitTestSession.StartNew(typeof(TestAppBuilder), AvaloniaTestIsolationLevel.PerTest);
|
||||
try
|
||||
{
|
||||
await RunAsync(() => Task.CompletedTask, CancellationToken.None);
|
||||
}
|
||||
catch
|
||||
{
|
||||
await DisposeAsync();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task RunAsync(Func<Task> action, CancellationToken ct)
|
||||
{
|
||||
var session = _session ?? throw new InvalidOperationException("La fixture Avalonia n'est pas initialisee.");
|
||||
await session.Dispatch(async () =>
|
||||
{
|
||||
await action();
|
||||
return true;
|
||||
}, ct);
|
||||
}
|
||||
|
||||
public Task WithWindowAsync(Func<Control> createContent, Func<Window, Task> test, CancellationToken ct,
|
||||
double width = 800, double height = 600) => RunAsync(async () =>
|
||||
{
|
||||
var window = new Window { Width = width, Height = height };
|
||||
try
|
||||
{
|
||||
window.Content = createContent();
|
||||
window.Show();
|
||||
Dispatcher.UIThread.RunJobs();
|
||||
window.UpdateLayout();
|
||||
await test(window);
|
||||
}
|
||||
finally
|
||||
{
|
||||
window.Close();
|
||||
if (window.Content is Control content) content.DataContext = null;
|
||||
window.Content = null;
|
||||
window.DataContext = null;
|
||||
Dispatcher.UIThread.RunJobs();
|
||||
}
|
||||
}, ct);
|
||||
|
||||
public async ValueTask DisposeAsync()
|
||||
{
|
||||
if (_session is not { } session) return;
|
||||
_session = null;
|
||||
await session.DisposeAsync();
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue