diff --git a/tests/Titano.App.Tests/Visuals/VisualsSmokeTests.cs b/tests/Titano.App.Tests/Visuals/VisualsSmokeTests.cs index 408c2020..1f1ef2a9 100644 --- a/tests/Titano.App.Tests/Visuals/VisualsSmokeTests.cs +++ b/tests/Titano.App.Tests/Visuals/VisualsSmokeTests.cs @@ -294,12 +294,10 @@ public sealed class VisualsSmokeTests } } - /// One STA dispatcher with an Application and the Titano theme, shared by the WPF tests. + /// Runs on the dispatcher of the shared WPF . private static class WpfHost { - private static readonly Lazy Dispatcher = new(Start); - - public static void Run(Action action) => Dispatcher.Value.Invoke(action); + public static void Run(Action action) => Windows.WpfApplication.Dispatcher.Invoke(action); /// Processes queued dispatcher work (async continuations) on the current dispatcher. 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!; - } } } diff --git a/tests/Titano.App.Tests/Windows/ScreenSmokeTests.cs b/tests/Titano.App.Tests/Windows/ScreenSmokeTests.cs index a1ab74eb..8fe4619b 100644 --- a/tests/Titano.App.Tests/Windows/ScreenSmokeTests.cs +++ b/tests/Titano.App.Tests/Windows/ScreenSmokeTests.cs @@ -284,9 +284,8 @@ public sealed class ScreenSmokeTests } /// -/// The STA dispatcher of the process-wide WPF (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 App.xaml does. +/// The dispatcher of the shared WPF with the client's styles and section templates merged, +/// as App.xaml does. /// 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); diff --git a/tests/Titano.App.Tests/Windows/WpfApplication.cs b/tests/Titano.App.Tests/Windows/WpfApplication.cs new file mode 100644 index 00000000..894dadba --- /dev/null +++ b/tests/Titano.App.Tests/Windows/WpfApplication.cs @@ -0,0 +1,35 @@ +using System.Windows; +using System.Windows.Threading; +using Titano.Ui.Theming; + +namespace Titano.App.Tests.Windows; + +/// +/// The process-wide WPF (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. +/// +internal static class WpfApplication +{ + private static readonly Lazy 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!; + } +}