2026-10-03 07:34:34 -04:00
|
|
|
using YKanBan.ViewModels.Dialogs;
|
|
|
|
|
|
|
|
|
|
namespace YKanBan.Tests.TestUtilities;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Records every dialog shown and lets a test script answer it. The result
|
|
|
|
|
/// is whatever the dialog closed with; a dialog the script leaves open
|
|
|
|
|
/// counts as dismissed. The save-file dialog answers with
|
|
|
|
|
/// <see cref="SaveFilePath"/>.
|
|
|
|
|
/// </summary>
|
|
|
|
|
internal sealed class ScriptedDialogService : IDialogService
|
|
|
|
|
{
|
|
|
|
|
public List<DialogViewModelBase> Shown { get; } = [];
|
|
|
|
|
|
|
|
|
|
public Func<DialogViewModelBase, Task> Script { get; set; } = _ => Task.CompletedTask;
|
|
|
|
|
|
|
|
|
|
public string? SaveFilePath { get; set; }
|
|
|
|
|
|
|
|
|
|
public List<string> SuggestedFileNames { get; } = [];
|
|
|
|
|
|
|
|
|
|
public async Task<bool> ShowAsync(DialogViewModelBase dialog)
|
|
|
|
|
{
|
|
|
|
|
Shown.Add(dialog);
|
|
|
|
|
bool? result = null;
|
|
|
|
|
dialog.CloseRequested += (_, closed) => result ??= closed;
|
|
|
|
|
await Script(dialog);
|
|
|
|
|
return result ?? false;
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-03 08:59:40 -04:00
|
|
|
public Task<Func<Task<Stream>>?> PickSaveFileAsync(string suggestedFileName)
|
2026-10-03 07:34:34 -04:00
|
|
|
{
|
|
|
|
|
SuggestedFileNames.Add(suggestedFileName);
|
2026-10-03 08:59:40 -04:00
|
|
|
string? path = SaveFilePath;
|
|
|
|
|
return Task.FromResult<Func<Task<Stream>>?>(
|
|
|
|
|
path is null ? null : () => Task.FromResult<Stream>(File.Create(path)));
|
2026-10-03 07:34:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Answers every confirmation dialog with the given choice.</summary>
|
|
|
|
|
public static Func<DialogViewModelBase, Task> AnswerConfirmations(bool confirm) => dialog =>
|
|
|
|
|
{
|
|
|
|
|
if (dialog is ConfirmDialogViewModel confirmation)
|
|
|
|
|
{
|
|
|
|
|
if (confirm)
|
|
|
|
|
{
|
|
|
|
|
confirmation.ConfirmCommand.Execute(null);
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
return confirmation.CancelCommand.ExecuteAsync(null);
|
|
|
|
|
}
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
};
|
|
|
|
|
}
|