using System.Windows;
using System.Windows.Automation;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Threading;
using Chronos.App.Editor;
using Chronos.App.Infrastructure;
using Chronos.App.Sections;
using Chronos.App.Shell;
using Chronos.App.Tests.Windows;
using Chronos.Core.Export;
using Chronos.Core.Localization;
using Chronos.Testing;
namespace Chronos.App.Tests.Editor;
///
/// The Edit path in the real main window at the 1200 × 788 capture size (owner review 1, R7): the step bar for screen
/// readers and the keyboard, Previous and Next, the findings' way to their step, the step memory and right to left.
///
[WindowsOnly]
[Serial]
[Timeout(300_000)]
public sealed class EditPathSmokeTests
{
[Test]
public Task TheStepBarNamesEachStepItsStateAndItsShortcut() => ScreenHost.RunAsync(async () =>
{
await using var app = await ScreenSmokeTests.SampleApp.StartAsync();
var (editor, view) = await OpenAsync(app);
await using (editor)
{
var steps = StepItems(view);
Assert.Count(8, steps);
for (var i = 0; i < steps.Count; i++)
{
var tab = (EditorTab)steps[i].DataContext;
Assert.Equal(i + 1, tab.Number);
Assert.Equal(tab.Header, AutomationProperties.GetName(steps[i]));
Assert.Equal(tab.StateText, AutomationProperties.GetItemStatus(steps[i]));
Assert.Equal($"Alt+{i + 1}", AutomationProperties.GetAcceleratorKey(steps[i]));
Assert.True(((string)steps[i].ToolTip).EndsWith($"Alt+{i + 1}", StringComparison.Ordinal), $"{tab.Key}: the tooltip names the shortcut");
Assert.False(string.IsNullOrEmpty(tab.StateText), $"{tab.Key}: a state");
}
Assert.Equal(EditPath.Frames, editor.SelectedTab.Key, "an analysed sequence opens on Frames");
Assert.True(editor.Frames.IsVisited);
Assert.False(editor.Exposure.IsVisited);
// Previous waits on the first step; Next names where it goes.
editor.SelectedTab = editor.Analysis;
Assert.False(editor.PreviousStepCommand.CanExecute(null));
Assert.Equal(Loc.L("Tooltips.Editor.Step.First"), editor.PreviousToolTip);
Assert.Equal(Loc.L("Tooltips.Editor.Step.Next", editor.Frames.Header), editor.NextToolTip);
Assert.Equal("1 / 8", editor.StepCounter);
editor.NextStepCommand.Execute(null);
Assert.Same(editor.Frames, editor.SelectedTab);
Assert.Equal(Loc.L("Editor.Step.Announce", 2, 8, editor.Frames.Header, editor.Frames.StateText), editor.StepAnnouncement);
// Alt+3 opens the third step; Ctrl+PgDn and Ctrl+PgUp move one step and stop at the ends.
var root = Find(view).Single(g => g.Name == "EditorRoot");
var alt3 = root.InputBindings.OfType().Single(b => b.Key == Key.D3 && b.Modifiers == ModifierKeys.Alt);
alt3.Command.Execute(alt3.CommandParameter);
Assert.Same(editor.Exposure, editor.SelectedTab);
Assert.Equal(1, SequenceView.StepDelta(Key.PageDown, ModifierKeys.Control));
Assert.Equal(-1, SequenceView.StepDelta(Key.PageUp, ModifierKeys.Control));
Assert.Null(SequenceView.StepDelta(Key.PageDown, ModifierKeys.None), "Page Down alone stays the filmstrip's");
editor.MoveStep(-10);
Assert.Same(editor.Exposure, editor.SelectedTab, "a move past the first step does nothing");
Assert.False(root.InputBindings.OfType().Any(b => b.Modifiers == ModifierKeys.Alt && b.Key is >= Key.NumPad0 and <= Key.NumPad9),
"no Alt+NumPad binding: those type characters into text fields");
// On the last step, Next chooses the next sequence: the picker covers the editor, which stays open.
editor.SelectedTab = editor.Tabs[^1];
Assert.Equal(Loc.L("Editor.Step.NextSequence"), editor.NextText);
editor.NextStepCommand.Execute(null);
Assert.True(app.Shell.Sequence.IsPicking);
Assert.Same(editor, app.Shell.Sequence.Editor);
app.Shell.Sequence.CancelPickCommand.Execute(null);
Assert.False(app.Shell.Sequence.IsPicking);
}
});
[Test]
public Task AFindingOpensTheStepThatDealsWithIt() => ScreenHost.RunAsync(async () =>
{
await using var app = await ScreenSmokeTests.SampleApp.StartAsync();
var (editor, view) = await OpenAsync(app);
await using (editor)
{
editor.SelectedTab = editor.Analysis;
await SettleAsync(app.Window);
var finding = Assert.Contains(editor.Analysis.Findings, f => f.Step is not null && !ReferenceEquals(f.Step, editor.Analysis));
var row = Find(view).Single(i => ReferenceEquals(i.DataContext, finding));
var goTo = Find