using System.Windows;
using System.Windows.Automation;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Media;
using Titano.App.Import;
using Titano.App.Infrastructure;
using Titano.App.Sections;
using Titano.App.Shell;
using Titano.App.Tests.Windows;
using Titano.Core.Localization;
using Titano.Testing;
using Titano.Ui.Controls;
namespace Titano.App.Tests.Import;
///
/// Every Import state of --capture () on the simulated cards, rendered in both themes
/// in the real main window at the 1200 × 788 capture size, with the figures and gates each state must show — and the
/// buttons really enabled, named for screen readers, nothing that matters cut off, and paths, numbers and times
/// readable in a right-to-left language.
///
[WindowsOnly]
[Serial]
[Timeout(300_000)]
public sealed class ImportScreenSmokeTests
{
[Test]
public Task EveryImportStateRendersInBothThemes() => ScreenHost.RunAsync(async () =>
{
await using var app = await ScreenSmokeTests.SampleApp.StartAsync();
await app.Sample.FillAsync();
var import = app.Shell.Import;
await ShowAsync(app, "inserted");
await UseCaptureSizeAsync(app);
Assert.NotEmpty(app.FindAll().SelectMany(host => host.Toasts), "the insertion toast");
Assert.False(import.FolderPath.HasErrors, "the folder path is not checked while Archive is the destination");
await app.ExpectAsync("import-inserted");
await ShowAsync(app, "plan");
Assert.Equal(ImportPhase.Ready, import.Phase);
Assert.Equal("EOS R5 · card A", import.SourceTitle);
Assert.Equal(3, import.Sessions.Count, "night, dawn and sunset");
Assert.Equal(50, import.Plan!.CopyCount);
Assert.Equal("Harbour sunset", import.Plan.Sessions[^1].Title);
Assert.Null(import.StartBlocker);
Assert.Empty(app.FindAll().SelectMany(host => host.Toasts));
await app.ExpectAsync("import-plan");
Assert.True(ButtonFor(app, import.StartCommand).IsEnabled, "the Import button is enabled once the plan is ready");
Assert.True(ButtonFor(app, import.Sessions[1].MergeCommand).IsEnabled, "a session can merge into the one before");
Assert.False(Validation.GetHasError(Named(app, "FolderPathBox")), "the folder path is not checked while Archive is the destination");
foreach (var session in import.Sessions)
Assert.True(Fits(TextFor(app, session.Row.TimeText)), $"the session time '{session.Row.TimeText}' is cut at 1200 px");
foreach (var item in DeviceItems(app))
{
Assert.NotNull(item.FocusVisualStyle, "a device shows keyboard focus");
Assert.Equal(((ImportSource)item.DataContext).ToolTip, item.ToolTip as string);
}
AssertEveryButtonIsNamed(app);
await AssertFourDigitsFitAsync(app, import);
await AssertFolderPathIsCheckedOnlyForFolderAsync(app, import);
await ShowAsync(app, "showplan");
Assert.True(import.IsDetailsOpen);
Assert.Equal(50 + 3, import.Details!.Rows.Count);
await app.ExpectAsync("import-showplan");
var file = import.Details.Rows[2];
Assert.True(Fits(TextFor(app, file.SourceText)), "a file's name on the card is shown whole");
Assert.True(Fits(TextFor(app, file.DestinationText)), "a file's new name is shown whole");
Assert.True(Fits(TextFor(app, import.Details.Rows[0].SourceText)), "a session heading is shown whole");
Assert.Within(Left(app, TextFor(app, Loc.L("Import.Details.Column.Action"))), 0, Left(app, TextFor(app, file.ActionText)), 0, 2,
"the Action header lines up with its cells");
AssertEveryButtonIsNamed(app);
await ShowAsync(app, "running");
Assert.True(import.IsRunning);
Assert.False(import.IsFormEnabled);
Assert.False(import.Sessions[0].IsEditable, "titles lock while copying");
await app.ExpectAsync("import-running");
Assert.True(ButtonFor(app, import.CancelCommand).IsEnabled, "Cancel is enabled while copying");
foreach (var item in DeviceItems(app))
{
Assert.False(item.IsEnabled, "the source of a running import stays selected");
Assert.Equal(Loc.L("Import.Blocked.Copying"), item.ToolTip as string);
}
Assert.Equal(0.4, TextFor(app, Loc.L("Import.Scan.Frames")).Opacity, "unit words dim with their field");
var progress = Named(app, "ProgressLine");
Assert.Equal(AutomationLiveSetting.Polite, AutomationProperties.GetLiveSetting(progress));
Assert.Same(progress, AutomationProperties.GetLabeledBy(app.FindAll().First(p => p.IsVisible && IsInImport(p))));
await ShowAsync(app, "failed");
Assert.True(import.IsFailed);
Assert.Equal("EOS R5 · card A is no longer available.", import.ErrorText);
Assert.Contains(SampleCards.Card(app.Services.Environment.DataDirectory).Root, import.FailureToolTip);
await app.ExpectAsync("import-failed");
Assert.True(ButtonFor(app, import.RetryCommand).IsEnabled, "Retry is enabled after a failure");
var failure = Named(app, "FailureLine");
Assert.Equal(AutomationLiveSetting.Assertive, AutomationProperties.GetLiveSetting(failure));
Assert.True(Fits(failure), "the failure sentence is shown whole");
Assert.Null(app.FindAll