test(app): one shared WPF Application host for every WPF test

The Server screen tests now run first and created the process's Application
through ScreenHost; the visuals tests then tried to create a second one and the
process aborted. Both hosts now use the same lazily started Application.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
Claude committed 2026-10-03 10:55:15 +00:00
1 parent b6533b4c1c
commit 93e47c2dee
3 files changed
+42 -47

No files matched your search

@@ -294,12 +294,10 @@ public sealed class VisualsSmokeTests
}
}
/// <summary>One STA dispatcher with an Application and the Titano theme, shared by the WPF tests.</summary>
/// <summary>Runs on the dispatcher of the shared WPF <see cref="Application"/>.</summary>
private static class WpfHost
{
private static readonly Lazy<Dispatcher> Dispatcher = new(Start);
public static void Run(Action action) => Dispatcher.Value.Invoke(action);
public static void Run(Action action) => Windows.WpfApplication.Dispatcher.Invoke(action);
/// <summary>Processes queued dispatcher work (async continuations) on the current dispatcher.</summary>
public static void DoEvents()
@@ -308,24 +306,5 @@ public sealed class VisualsSmokeTests
System.Windows.Threading.Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background, () => frame.Continue = false);
System.Windows.Threading.Dispatcher.PushFrame(frame);
}
private static Dispatcher Start()
{
Dispatcher? dispatcher = null;
using var ready = new ManualResetEventSlim();
var thread = new Thread(() =>
{
var app = new Application { ShutdownMode = ShutdownMode.OnExplicitShutdown };
ThemeManager.Initialize(app, AppTheme.Dark);
dispatcher = System.Windows.Threading.Dispatcher.CurrentDispatcher;
ready.Set();
System.Windows.Threading.Dispatcher.Run();
});
thread.SetApartmentState(ApartmentState.STA);
thread.IsBackground = true;
thread.Start();
ready.Wait();
return dispatcher!;
}
}
}
@@ -284,9 +284,8 @@ public sealed class ScreenSmokeTests
}
/// <summary>
/// The STA dispatcher of the process-wide WPF <see cref="Application"/> (only one may exist per process): reuses the
/// one an earlier WPF test created, otherwise starts it with the Titano theme; then merges the client's styles and
/// section templates, as <c>App.xaml</c> does.
/// The dispatcher of the shared WPF <see cref="Application"/> with the client's styles and section templates merged,
/// as <c>App.xaml</c> does.
/// </summary>
internal static class ScreenHost
{
@@ -296,28 +295,10 @@ internal static class ScreenHost
private static Dispatcher Start()
{
var app = Application.Current;
Dispatcher? dispatcher = app?.Dispatcher;
if (dispatcher is null)
var dispatcher = WpfApplication.Dispatcher;
dispatcher.Invoke(() =>
{
using var ready = new ManualResetEventSlim();
var thread = new Thread(() =>
{
var created = new Application { ShutdownMode = ShutdownMode.OnExplicitShutdown };
ThemeManager.Initialize(created, AppTheme.Dark);
dispatcher = System.Windows.Threading.Dispatcher.CurrentDispatcher;
ready.Set();
System.Windows.Threading.Dispatcher.Run();
});
thread.SetApartmentState(ApartmentState.STA);
thread.IsBackground = true;
thread.Start();
ready.Wait();
}
dispatcher!.Invoke(() =>
{
// pack:// URIs parse only once an Application exists (it registers the scheme), so this one is built here,
// not in a static field: the first WPF test of a run may be the one that starts the Application.
// pack:// URIs parse only once an Application exists (it registers the scheme), so this one is built here.
var sectionTemplates = new Uri("pack://application:,,,/Titano;component/Shell/SectionTemplates.xaml", UriKind.Absolute);
var current = Application.Current;
AppResources.Merge(current);
@@ -0,0 +1,35 @@
using System.Windows;
using System.Windows.Threading;
using Titano.Ui.Theming;
namespace Titano.App.Tests.Windows;
/// <summary>
/// The process-wide WPF <see cref="Application"/> (only one may ever exist per process) on its own STA thread with
/// the Titano theme. Every WPF test reaches it through here, whichever test happens to run first.
/// </summary>
internal static class WpfApplication
{
private static readonly Lazy<Dispatcher> Instance = new(Start, LazyThreadSafetyMode.ExecutionAndPublication);
public static Dispatcher Dispatcher => Instance.Value;
private static Dispatcher Start()
{
Dispatcher? dispatcher = null;
using var ready = new ManualResetEventSlim();
var thread = new Thread(() =>
{
var app = new Application { ShutdownMode = ShutdownMode.OnExplicitShutdown };
ThemeManager.Initialize(app, AppTheme.Dark);
dispatcher = System.Windows.Threading.Dispatcher.CurrentDispatcher;
ready.Set();
System.Windows.Threading.Dispatcher.Run();
});
thread.SetApartmentState(ApartmentState.STA);
thread.IsBackground = true;
thread.Start();
ready.Wait();
return dispatcher!;
}
}