using System.IO; using System.Windows; using System.Windows.Controls; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Threading; using OpenCvSharp; using Prism.Dialogs; using SopHelper.Engine.Camera; using SopHelper.Engine.Sop; using SopHelper.VVMs.RecipeEditor; using Point = System.Windows.Point; using Rect = System.Windows.Rect; using Size = System.Windows.Size; internal static class Program { [STAThread] private static int Main(string[] args) { var app = new Application(); app.Resources.MergedDictionaries.Add(new ResourceDictionary { Source = new Uri("pack://application:,,,/SopHelper;component/Themes/Styles.xaml"), }); SynchronizationContext.SetSynchronizationContext(new DispatcherSynchronizationContext()); var run = CheckAsync(args); _ = run.ContinueWith(_ => app.Dispatcher.BeginInvokeShutdown(DispatcherPriority.Background)); Dispatcher.Run(); try { run.GetAwaiter().GetResult(); return 0; } catch (Exception ex) { Console.Error.WriteLine(ex); return 1; } } private static async Task CheckAsync(string[] args) { var imageSize = new Size(4000, 2000); var display = RegionCanvas.FitImage(new Size(800, 600), imageSize); if (display != new Rect(0, 100, 800, 400)) throw new Exception("横向留白计算错误"); var first = RegionCanvas.ToImage(new Point(160, 180), display, imageSize); var last = RegionCanvas.ToImage(new Point(640, 420), display, imageSize); var bounds = RegionCanvas.PixelBounds(last, first, imageSize); if (bounds != new Int32Rect(800, 400, 2400, 1200)) throw new Exception("反向画框或像素换算错误"); var clipped = RegionCanvas.ToImage(new Point(900, -50), display, imageSize); if (clipped != new Point(4000, 0)) throw new Exception("边界截取错误"); var portrait = RegionCanvas.FitImage(new Size(800, 600), new Size(1000, 2000)); if (portrait != new Rect(250, 0, 300, 600)) throw new Exception("纵向图像留白计算错误"); Console.WriteLine("PASS: 原图像素换算、横竖图留白、反向画框及越界截取"); await CheckCompletionResetAsync(); var saveDirectory = Path.Combine(Path.GetTempPath(), "SopHelper-save-" + Guid.NewGuid().ToString("N")); Directory.CreateDirectory(saveDirectory); var savePath = Path.Combine(saveDirectory, "repeat-save.yaml"); var notifications = new List(); var saveVm = new RecipeEditorViewModel(); try { saveVm.OnDialogOpened(new DialogParameters { { "directory", saveDirectory }, { "new", true }, { "onSaved", new Action(notifications.Add) }, }); saveVm.Recipe.Name = "首次保存"; saveVm.Recipe.FileName = "repeat-save"; saveVm.Recipe.Settings.ExposureTimeUs = 12500; var editedRecipe = saveVm.Recipe; // 未注册关闭监听器:保存如果仍尝试关闭弹窗,就不能正常完成。 saveVm.SaveCommand.Execute(); if (!saveVm.SaveStatus.StartsWith("已保存") || saveVm.Recipe.CanEditFileName || saveVm.Title != "编辑配方 - repeat-save.yaml" || notifications.Count != 1 || SopConfigLoader.Load(savePath).Sop.Name != "首次保存") throw new Exception("首次保存后的状态或文件不正确:" + string.Join(";", saveVm.Warnings)); saveVm.Recipe.Name = "继续修改并保存"; var restoredSettings = new RecipeSettingsNode(); restoredSettings.CopyFrom(SopConfigLoader.Load(savePath).Sop.Settings); if (restoredSettings.ExposureTimeUs != 12500) throw new Exception("曝光保存重开丢失"); saveVm.Recipe.Settings.ExposureTimeUs = null; saveVm.SaveCommand.Execute(); if (!saveVm.SaveStatus.StartsWith("已保存") || !ReferenceEquals(editedRecipe, saveVm.Recipe) || notifications.Count != 2 || notifications.Any(name => name != "repeat-save.yaml") || SopConfigLoader.Load(savePath).Sop.Name != "继续修改并保存" || Directory.GetFiles(saveDirectory).Length != 1) throw new Exception("连续保存未更新原配方文件"); if (SopConfigLoader.Load(savePath).Sop.Settings.ExposureTimeUs is not null) throw new Exception("曝光留空后仍保留旧值"); saveVm.Recipe.Settings.ExposureTimeUs = -1; saveVm.SaveCommand.Execute(); if (!saveVm.Warnings.Any(w => w.Contains("相机曝光必须大于"))) throw new Exception("允许保存无效曝光值"); saveVm.Recipe.Settings.ExposureTimeUs = null; Console.WriteLine("PASS: 保存不请求关闭、编辑状态保留、新建转为编辑、连续保存更新同一文件并通知主页"); saveVm.Recipe.AddStepCommand.Execute(); if (!saveVm.Warnings.Any(w => w.Contains("未设置完成条件"))) throw new Exception("新增空步骤未提示设置完成条件"); var stepView = new RecipeEditor { DataContext = saveVm }; stepView.Measure(new Size(1020, 720)); stepView.Arrange(new Rect(0, 0, 1020, 720)); stepView.UpdateLayout(); var editorTabs = Descendants(stepView).OfType().Single(); editorTabs.SelectedItem = editorTabs.Items.OfType().Single(t => Equals(t.Header, "检测模型")); stepView.UpdateLayout(); var exposureInput = (TextBox)stepView.FindName("CameraExposureInput"); exposureInput.Text = "18000"; exposureInput.GetBindingExpression(TextBox.TextProperty)!.UpdateSource(); if (saveVm.Recipe.Settings.ExposureTimeUs != 18000) throw new Exception("检测模型页曝光输入未绑定配方"); saveVm.Recipe.Settings.ExposureTimeUs = null; editorTabs.SelectedIndex = 0; stepView.UpdateLayout(); SelectMethod(stepView, ConditionMethod.TimeElapsed); if (saveVm.Warnings.Any(w => w.Contains("未设置完成条件"))) throw new Exception("选择方法后缺少判定的提示未实时更新"); saveVm.SaveCommand.Execute(); if (SopConfigLoader.Load(savePath).Sop.Steps.Single().Detection?.Method != ConditionMethod.TimeElapsed) throw new Exception("新增步骤在界面选择方法后未保存"); SelectMethod(stepView, ""); if (!saveVm.Warnings.Any(w => w.Contains("未设置完成条件"))) throw new Exception("清空方法后未重新提示设置完成条件"); saveVm.SaveCommand.Execute(); var reopenedVm = new RecipeEditorViewModel(); reopenedVm.OnDialogOpened(new DialogParameters { { "path", savePath }, { "config", SopConfigLoader.Load(savePath) }, }); stepView.DataContext = reopenedVm; stepView.UpdateLayout(); SelectMethod(stepView, ConditionMethod.TimeElapsed); reopenedVm.SaveCommand.Execute(); if (SopConfigLoader.Load(savePath).Sop.Steps.Single().Detection?.Method != ConditionMethod.TimeElapsed) throw new Exception("未配置判定的步骤重新打开后选择方法无法保存"); var reopenedAgain = RecipeNode.From(SopConfigLoader.Load(savePath), "repeat-save", false); if (reopenedAgain.SelectedStep?.Detection?.Method != ConditionMethod.TimeElapsed) throw new Exception("保存后再次打开丢失判定方法"); var addRule = Descendants(stepView).OfType