using Avalonia.Threading; using OpenCvSharp; using Prism.Dialogs; using SopHelper.Engine.Sop; using SopHelper.Engine.Recipes; using SopHelper.VVMs.RecipeEditor; internal static class StandardVideoImportChecks { public static void Run(string root) { var store = new RecipeWorkspace(root); var config = new SopConfig(); config.Sop.Name = "导入标准视频"; var vm = new RecipeEditorViewModel(); vm.OnDialogOpened(new DialogParameters { { "directory", store.RecipesDirectory }, { "config", config }, { "new", true } }); vm.SaveCommand.Execute(); var model = vm.SopRecording!; var source = Path.Combine(root, "用户选择的.mp4"); WriteVideo(source, VideoWriter.FourCC('m', 'p', '4', 'v'), 20); var sourceBytes = File.ReadAllBytes(source); Check(!model.HasStandardVideo && model.ImportVideoCommand.CanExecute(), "Import is available without a camera or previous standard video"); var selection = new TaskCompletionSource(); model.SelectImportVideoAsync = () => selection.Task; model.ImportVideoCommand.Execute(); Check(model.IsBusy && !model.ImportVideoCommand.CanExecute(), "Import selection disables duplicate operations"); selection.SetResult(source); Until(() => !model.IsBusy); Check(model.HasStandardVideo && File.ReadAllBytes(model.StandardVideoPath).SequenceEqual(sourceBytes) && model.StandardVideoPath.EndsWith(Path.Combine("sop_stand", "sop.avi")) && File.ReadAllBytes(source).SequenceEqual(sourceBytes), "Selected MP4 is copied into the standard folder and renamed without moving the source"); Check(Math.Abs(SopStandardVideoDuration.Get(Path.GetDirectoryName(model.SaveDirectory)!).TotalSeconds - 2) < .01, "Imported duration updates the persistent standard duration"); model.PlayVideoCommand.Execute(); Until(() => !model.IsBusy); Check(model.Playback.IsLoaded && model.DisplayImage is { PixelWidth: 64, PixelHeight: 48 }, "Renamed MP4 decodes and plays from the standard AVI path"); var replacement = Path.Combine(root, "替换.avi"); WriteVideo(replacement, FourCC.MJPG, 30); model.SelectImportVideoAsync = () => Task.FromResult(replacement); model.ImportVideoCommand.Execute(); Until(() => !model.IsBusy); var imported = File.ReadAllBytes(model.StandardVideoPath); Check(imported.SequenceEqual(File.ReadAllBytes(replacement)) && !model.Playback.IsLoaded && model.LastRecording == model.StandardVideoPath, "Import releases current playback and overwrites the old standard video"); Check(Math.Abs(SopStandardVideoDuration.Get(Path.GetDirectoryName(model.SaveDirectory)!).TotalSeconds - 3) < .01, "Replacing a video refreshes cached standard duration"); var invalid = Path.Combine(root, "损坏.mp4"); File.WriteAllText(invalid, "invalid video"); foreach (var choice in new[] { invalid, Path.Combine(root, "不存在.mp4"), model.StandardVideoPath, null }) { model.SelectImportVideoAsync = () => Task.FromResult(choice); model.ImportVideoCommand.Execute(); Until(() => !model.IsBusy); Check(File.ReadAllBytes(model.StandardVideoPath).SequenceEqual(imported) && !Directory.EnumerateFiles(model.SaveDirectory, ".import-*").Any(), "Invalid, missing, same-path and cancelled selection preserve the old video and clean temporary files"); } if (OperatingSystem.IsWindows()) { using (var locked = new FileStream(model.StandardVideoPath, FileMode.Open, FileAccess.Read, FileShare.None)) { model.SelectImportVideoAsync = () => Task.FromResult(source); model.ImportVideoCommand.Execute(); Until(() => !model.IsBusy); } Check(File.ReadAllBytes(model.StandardVideoPath).SequenceEqual(imported) && !Directory.EnumerateFiles(model.SaveDirectory, ".import-*").Any(), "Replacement failure preserves the locked old video and removes staging files"); } var shutdown = vm.ShutdownAsync(); Until(() => shutdown.IsCompleted); shutdown.GetAwaiter().GetResult(); vm.OnDialogClosed(); } private static void WriteVideo(string path, int codec, int frames) { using var writer = new VideoWriter(path, codec, 10, new Size(64, 48)); Check(writer.IsOpened(), "Import sample video opens"); for (var i = 0; i < frames; i++) { using var image = new Mat(48, 64, MatType.CV_8UC3, new Scalar(20, 60, i * 5)); writer.Write(image); } } private static void Until(Func ready) { var watch = System.Diagnostics.Stopwatch.StartNew(); while (!ready() && watch.Elapsed < TimeSpan.FromSeconds(15)) { using var cancellation = new CancellationTokenSource(10); Dispatcher.UIThread.MainLoop(cancellation.Token); } Check(ready(), "Import completes"); Dispatcher.UIThread.RunJobs(); } private static void Check(bool value, string message) { if (!value) throw new Exception(message); Console.WriteLine("PASS: " + message); } }