using YKanBan.ViewModels.Dialogs;
namespace YKanBan.Tests.TestUtilities;
///
/// 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
/// .
///
internal sealed class ScriptedDialogService : IDialogService
{
public List Shown { get; } = [];
public Func Script { get; set; } = _ => Task.CompletedTask;
public string? SaveFilePath { get; set; }
public List SuggestedFileNames { get; } = [];
public async Task ShowAsync(DialogViewModelBase dialog)
{
Shown.Add(dialog);
bool? result = null;
dialog.CloseRequested += (_, closed) => result ??= closed;
await Script(dialog);
return result ?? false;
}
public Task>?> PickSaveFileAsync(string suggestedFileName)
{
SuggestedFileNames.Add(suggestedFileName);
string? path = SaveFilePath;
return Task.FromResult>?>(
path is null ? null : () => Task.FromResult(File.Create(path)));
}
/// Answers every confirmation dialog with the given choice.
public static Func 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;
};
}