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:
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 class WpfHost
|
||||||
{
|
{
|
||||||
private static readonly Lazy<Dispatcher> Dispatcher = new(Start);
|
public static void Run(Action action) => Windows.WpfApplication.Dispatcher.Invoke(action);
|
||||||
|
|
||||||
public static void Run(Action action) => Dispatcher.Value.Invoke(action);
|
|
||||||
|
|
||||||
/// <summary>Processes queued dispatcher work (async continuations) on the current dispatcher.</summary>
|
/// <summary>Processes queued dispatcher work (async continuations) on the current dispatcher.</summary>
|
||||||
public static void DoEvents()
|
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.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background, () => frame.Continue = false);
|
||||||
System.Windows.Threading.Dispatcher.PushFrame(frame);
|
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>
|
/// <summary>
|
||||||
/// The STA dispatcher of the process-wide WPF <see cref="Application"/> (only one may exist per process): reuses the
|
/// The dispatcher of the shared WPF <see cref="Application"/> with the client's styles and section templates merged,
|
||||||
/// one an earlier WPF test created, otherwise starts it with the Titano theme; then merges the client's styles and
|
/// as <c>App.xaml</c> does.
|
||||||
/// section templates, as <c>App.xaml</c> does.
|
|
||||||
/// </summary>
|
/// </summary>
|
||||||
internal static class ScreenHost
|
internal static class ScreenHost
|
||||||
{
|
{
|
||||||
@@ -296,28 +295,10 @@ internal static class ScreenHost
|
|||||||
|
|
||||||
private static Dispatcher Start()
|
private static Dispatcher Start()
|
||||||
{
|
{
|
||||||
var app = Application.Current;
|
var dispatcher = WpfApplication.Dispatcher;
|
||||||
Dispatcher? dispatcher = app?.Dispatcher;
|
dispatcher.Invoke(() =>
|
||||||
if (dispatcher is null)
|
|
||||||
{
|
{
|
||||||
using var ready = new ManualResetEventSlim();
|
// pack:// URIs parse only once an Application exists (it registers the scheme), so this one is built here.
|
||||||
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.
|
|
||||||
var sectionTemplates = new Uri("pack://application:,,,/Titano;component/Shell/SectionTemplates.xaml", UriKind.Absolute);
|
var sectionTemplates = new Uri("pack://application:,,,/Titano;component/Shell/SectionTemplates.xaml", UriKind.Absolute);
|
||||||
var current = Application.Current;
|
var current = Application.Current;
|
||||||
AppResources.Merge(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!;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user