aisop/tests/Training.ShellSmoke/Program.cs

78 lines
3.5 KiB
C#

using System.IO;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Media;
using Prism.Ioc;
using Prism.Navigation.Regions;
using SopHelper.Models;
using SopHelper.Training;
internal static class Program
{
[STAThread]
private static int Main(string[] args)
{
var app = new ShellProbe(args.Contains("--idle"));
app.Resources.MergedDictionaries.Add(new ResourceDictionary { Source = new Uri("/SopHelper;component/Themes/Styles.xaml", UriKind.Relative) });
return app.Run();
}
}
internal sealed class ShellProbe(bool idle) : SopHelper.App
{
private MainViewModel? _trainer;
protected override void OnInitialized()
{
base.OnInitialized();
Dispatcher.BeginInvoke(async () =>
{
try
{
var navigation = Container.Resolve<NavigationModel>();
var regions = Container.Resolve<IRegionManager>();
if (!navigation.Pages.Any(p => p.ViewName == nameof(TrainingPage))) throw new Exception("Training navigation missing");
navigation.SelectedIndex = 1;
await Dispatcher.InvokeAsync(() => { }, System.Windows.Threading.DispatcherPriority.ApplicationIdle);
var page = regions.Regions[NavigationModel.ContentRegion].ActiveViews.OfType<TrainingPage>().Single();
_trainer = (MainViewModel)page.DataContext;
if (_trainer.Root != Path.GetFullPath(Path.Combine(AppContext.BaseDirectory, "../../../../..")))
throw new Exception("Training root does not resolve to SopHelper repository");
if (!File.Exists(Path.Combine(_trainer.Root, "tools", "worker.py"))) throw new Exception("Worker missing");
_trainer.Label = "导航状态检查";
navigation.SelectedIndex = 0;
navigation.SelectedIndex = 1;
var restored = regions.Regions[NavigationModel.ContentRegion].ActiveViews.OfType<TrainingPage>().Single();
if (!ReferenceEquals(page, restored) || ((MainViewModel)restored.DataContext).Label != "导航状态检查")
throw new Exception("Navigation lost training state");
MainWindow.UpdateLayout();
var image = new RenderTargetBitmap((int)MainWindow.ActualWidth, (int)MainWindow.ActualHeight, 96, 96, PixelFormats.Pbgra32);
image.Render(MainWindow);
var encoder = new PngBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(image));
using (var output = File.Create(Path.Combine(Path.GetTempPath(), "sophelper-training-shell.png"))) encoder.Save(output);
Console.WriteLine("PASS: actual Prism shell, navigation registration, DI, repository root, page state retained, shell render");
// Start a real background worker, navigate away, then close the shell.
if (!idle) _trainer.DoctorCommand.Execute();
navigation.SelectedIndex = 0;
MainWindow.Close();
}
catch (Exception error)
{
Console.Error.WriteLine(error);
Shutdown(1);
}
});
}
protected override void OnExit(ExitEventArgs e)
{
if (_trainer?.IsBusy == true)
{
Console.Error.WriteLine("FAIL: shell exited before training worker stopped");
e.ApplicationExitCode = 1;
}
else Console.WriteLine("PASS: shell close after navigating away waits for worker cancellation");
base.OnExit(e);
}
}