aisop/tests/VideoFlowTest.Smoke/StandardVideoImportChecks.cs
tfarcraw 2553adf03f SOP流程视频支持导入并覆盖标准视频
新增原生文件选择入口,将所选视频复制至当前配方sop_stand/sop.avi。先暂存并验证可解码,再释放播放句柄并替换旧文件;更新标准时长与界面状态,保留用户源文件。

验证:隔离构建及VideoFlowTest.Smoke通过,覆盖真实MP4导入播放、已有视频替换、时长刷新、损坏和缺失文件、取消选择、相同路径及Windows文件占用时保留旧视频与临时文件清理。
2026-11-18 13:54:49 +08:00

76 lines
5.1 KiB
C#

using Avalonia.Threading;
using OpenCvSharp;
using Prism.Dialogs;
using SopHelper.Engine.Sop;
using SopHelper.Engine.Training;
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<string?>(); 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<string?>(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<string?>(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<bool> 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); }
}