Files
YKanBan/YKanBan.Tests/TestUtilities/ScriptedDialogService.cs
T
doyaGu c9523d0953 fix(export): write through the storage provider when the target has no local path
The save picker used to return only TryGetLocalPath(), so a target without a
local path looked like a cancel and nothing was written. The dialog service now
hands back an opener for the chosen file. The picker call is inside the error
handler, and the workspace is read before the target is opened, so a failed read
leaves an existing file untouched.
2026-10-03 08:59:40 -04:00

53 lines
1.7 KiB
C#

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;
}
public Task<Func<Task<Stream>>?> PickSaveFileAsync(string suggestedFileName)
{
SuggestedFileNames.Add(suggestedFileName);
string? path = SaveFilePath;
return Task.FromResult<Func<Task<Stream>>?>(
path is null ? null : () => Task.FromResult<Stream>(File.Create(path)));
}
/// <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;
};
}