112 lines
5.8 KiB
C#
112 lines
5.8 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;
|
|
}
|
|
|
|
// 新建默认值、回退到原值及手动保存后的内容,都应直接关闭。
|
|
void CloseWithoutPrompt(RecipeEditorViewModel editor)
|
|
{
|
|
var cleanWindow = new Window { Content = new RecipeEditor { DataContext = editor } };
|
|
cleanWindow.Show();
|
|
cleanWindow.Close();
|
|
Until(() => !cleanWindow.IsVisible || cleanWindow.OwnedWindows.Any());
|
|
Check(!cleanWindow.IsVisible && !cleanWindow.OwnedWindows.Any(), "没有未保存修改时直接关闭");
|
|
editor.OnDialogClosed();
|
|
}
|
|
|
|
CloseWithoutPrompt(Create(true));
|
|
Check(savedCalls == 0 && !File.Exists(store.ConfigPath(config.Sop.Name)), "未编辑的新配方直接关闭且不落盘");
|
|
|
|
var model = Create(true);
|
|
model.Recipe.Description = "新配方内容";
|
|
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();
|
|
|
|
model = Create(false);
|
|
var originalDescription = model.Recipe.Description;
|
|
model.Recipe.Description = "临时修改";
|
|
model.Recipe.Description = originalDescription;
|
|
CloseWithoutPrompt(model);
|
|
|
|
model = Create(false);
|
|
model.Recipe.Description = "手动保存";
|
|
model.SaveCommand.Execute();
|
|
CloseWithoutPrompt(model);
|
|
Check(savedCalls == 2, "手动保存后关闭不会重复写盘");
|
|
|
|
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 == 2,
|
|
"选择不保存时关闭窗口且保留原配方");
|
|
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 == 3, "解除占用后可以重试保存关闭,当前步骤修改持久化");
|
|
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); }
|
|
}
|