releases/tests/Paca.Tests/Infrastructure/WpfTestApp.cs

67 lines
2.4 KiB
C#

using System.Windows;
using Paca.App;
namespace Paca.Tests.Infrastructure;
/// <summary>
/// 테스트 프로세스에는 App.xaml 의 리소스가 없으므로, App 어셈블리의 테마 사전을
/// 테스트 전용 Application 에 로드한다(앱의 App.ApplyTheme 과 동일 순서: 색 → 스타일).
/// </summary>
public static class WpfTestApp
{
private static readonly object _lock = new();
private static bool _initialized = false;
public static void Ensure(System.Windows.Threading.Dispatcher dispatcher)
{
dispatcher.Invoke(() =>
{
lock (_lock)
{
if (Application.Current == null)
{
try
{
new Application { ShutdownMode = ShutdownMode.OnExplicitShutdown };
}
catch (InvalidOperationException)
{
// 이미 다른 스레드/테스트에서 생성된 경우
}
}
if (Application.Current != null && !_initialized)
{
try
{
global::Paca.App.App.ApplyTheme("dark");
if (!Application.Current.Resources.Contains("InverseCountToVisibility"))
Application.Current.Resources["InverseCountToVisibility"] = new InverseCountToVisibilityConverter();
_initialized = true;
}
catch { }
}
}
});
}
public static void EnsureWindowResources(FrameworkElement element)
{
try
{
const string asm = "pack://application:,,,/Paca;component";
var md = element.Resources.MergedDictionaries;
bool hasDark = md.Any(d => d.Source?.OriginalString?.Contains("Dark.xaml") == true);
bool hasShared = md.Any(d => d.Source?.OriginalString?.Contains("Shared.xaml") == true);
if (!hasDark)
{
try { md.Add(new ResourceDictionary { Source = new Uri(asm + "/Themes/Dark.xaml", UriKind.Absolute) }); } catch { }
}
if (!hasShared)
{
try { md.Add(new ResourceDictionary { Source = new Uri(asm + "/Themes/Shared.xaml", UriKind.Absolute) }); } catch { }
}
}
catch { }
}
}