aisop/tests/VideoFlowTest.Smoke/RecipeCloseChecks.cs

85 lines
4.5 KiB
C#

using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Threading;
using Avalonia.VisualTree;
using Prism.Dialogs;
using SopHelper.Engine.Sop;
using SopHelper.Engine.Recipes;
using SopHelper.VVMs.RecipeEditor;
internal static class RecipeCloseChecks
{
public static void Run(string root)
{
var store = new RecipeWorkspace(root);
var config = new SopConfig(); config.Sop.Name = "关闭保存验证";
var savedCalls = 0;
RecipeEditorViewModel Create(bool isNew) {
var model = new RecipeEditorViewModel();
model.OnDialogOpened(new DialogParameters { { "directory", store.RecipesDirectory }, { "config", config },
{ "path", isNew ? "" : store.ConfigPath(config.Sop.Name) }, { "new", isNew },
{ "onSaved", new Action<string>(_ => savedCalls++) } });
return model;
}
var model = Create(true);
var view = new RecipeEditor { DataContext = model };
var window = new Window { Content = view, Width = 1040, Height = 800 };
window.Show();
window.Close();
Until(() => window.OwnedWindows.Any());
var prompt = window.OwnedWindows.Single();
var buttons = prompt.GetVisualDescendants().OfType<Button>().ToArray();
Check(window.IsVisible && buttons.Length == 2
&& buttons.Any(b => b.Content?.ToString() == "保存并关闭") && buttons.Any(b => b.Content?.ToString() == "不保存")
&& !File.Exists(store.ConfigPath(config.Sop.Name)), "关闭窗口先提示保存,仅提供保存和不保存两个按钮");
buttons.Single(b => b.Name == "SaveRecipeAndCloseButton").RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
Until(() => !window.IsVisible);
Check(savedCalls == 1 && File.Exists(store.ConfigPath(config.Sop.Name)) && model.CanCloseDialog(),
"确认保存后新配方落盘、回调一次并关闭窗口");
model.OnDialogClosed();
var before = File.ReadAllBytes(store.ConfigPath(config.Sop.Name));
model = Create(false);
model.Recipe.AddStepCommand.Execute(); model.Recipe.SelectedStep!.Description = "不应落盘";
view = new RecipeEditor { DataContext = model };
window = new Window { Content = view }; window.Show(); window.Close();
Until(() => window.OwnedWindows.Any());
window.OwnedWindows.Single().GetVisualDescendants().OfType<Button>().Single(b => b.Name == "DiscardRecipeAndCloseButton")
.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
Until(() => !window.IsVisible);
Check(before.SequenceEqual(File.ReadAllBytes(store.ConfigPath(config.Sop.Name))) && savedCalls == 1,
"选择不保存时关闭窗口且保留原配方");
model.OnDialogClosed();
model = Create(false);
model.Recipe.AddStepCommand.Execute(); model.Recipe.SelectedStep!.Description = "确认后应保存";
model.ConfirmCloseAsync = () => Task.FromResult(RecipeCloseChoice.Save);
using (var locked = new FileStream(store.ConfigPath(config.Sop.Name), FileMode.Open, FileAccess.Read, FileShare.None))
{
var close = model.PrepareCloseAsync(); Pump(close);
Check(!close.Result && !model.CanCloseDialog() && model.SaveStatus.Contains("保存失败") && model.CloseCommand.CanExecute(),
"保存失败阻止关闭,显示错误且允许重试");
}
var retry = model.PrepareCloseAsync(); Pump(retry);
Check(retry.Result && SopConfigLoader.Load(store.ConfigPath(config.Sop.Name)).Sop.Steps.Single().Description == "确认后应保存"
&& savedCalls == 2, "解除占用后可以重试保存关闭,当前步骤修改持久化");
model.OnDialogClosed();
Console.WriteLine("PASS: 关闭保存确认、两按钮弹窗、新建与现有配方、放弃修改和保存失败重试");
}
private static void Pump(Task task) { Until(() => task.IsCompleted); task.GetAwaiter().GetResult(); }
private static void Until(Func<bool> ready)
{
var watch = System.Diagnostics.Stopwatch.StartNew();
while (!ready() && watch.Elapsed < TimeSpan.FromSeconds(10))
{
using var cancellation = new CancellationTokenSource(5);
Dispatcher.UIThread.MainLoop(cancellation.Token);
}
if (!ready()) throw new TimeoutException("关闭保存确认检查超时");
Dispatcher.UIThread.RunJobs();
}
private static void Check(bool value, string message) { if (!value) throw new Exception(message); }
}