using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using Chronos.App.Editor;
using Chronos.App.Shell;
using Chronos.Core.Localization;
using Chronos.Testing;
namespace Chronos.App.Tests.Editor;
///
/// The Camera step's Straighten slider reports its use to the editor (owner review 1, R6): the NumberRow template of
/// Editor/EditorPanels.xaml turns on for the row that shows the grid, and binds its
/// one way to the source Editor.IsStraightening of the step. Built off
/// screen on the test dispatcher (no window): the slider's activity is set as sets it when the
/// slider takes the focus or the mouse capture.
///
[WindowsOnly]
[Serial]
public sealed class StraightenGridBindingTests
{
[Test]
public async Task UsingTheStraightenSliderTellsTheEditor()
{
var content = EditorFixture.Content(EditorFixture.Sunset);
var session = new EditorSession(new FakeSequenceSource(EditorFixture.Sunset, content), content, [], TimeSpan.FromMilliseconds(20));
await using (session)
{
var rows = CameraTab.FramingRows(session);
Windows.WpfApplication.Dispatcher.Invoke(() =>
{
AppResources.Merge(Application.Current);
var step = new FakeStep();
var items = new ItemsControl { ItemsSource = rows, DataContext = step };
items.Resources.MergedDictionaries.Add(new ResourceDictionary
{
Source = new Uri("pack://application:,,,/Chronos;component/Editor/EditorPanels.xaml", UriKind.Absolute),
});
var host = new Border { Child = items };
host.Measure(new Size(320, 2000));
host.Arrange(new Rect(0, 0, 320, 2000));
host.UpdateLayout();
var sliders = Descendants(host).ToList();
Assert.Equal(rows.OfType().Count(), sliders.Count);
var straighten = Assert.NotNull(sliders.SingleOrDefault(s => s.DataContext is NumberRow { ShowsGrid: true }));
Assert.Equal(Loc.L("Editor.Camera.Straighten"), ((NumberRow)straighten.DataContext).Header);
Assert.True(SliderActivity.GetTracks(straighten), "the Straighten slider is tracked");
Assert.False(sliders.Where(s => !ReferenceEquals(s, straighten)).Any(SliderActivity.GetTracks), "no other slider is");
Assert.False(step.Editor.IsStraightening);
straighten.SetCurrentValue(SliderActivity.IsActiveProperty, true);
Assert.True(step.Editor.IsStraightening, "focused or dragged: the editor shows the grid");
straighten.SetCurrentValue(SliderActivity.IsActiveProperty, false);
Assert.False(step.Editor.IsStraightening, "let go: the grid goes");
});
}
}
private static IEnumerable Descendants(DependencyObject root)
where T : DependencyObject
{
for (var i = 0; i < VisualTreeHelper.GetChildrenCount(root); i++)
{
var child = VisualTreeHelper.GetChild(root, i);
if (child is T match) yield return match;
foreach (var nested in Descendants(child)) yield return nested;
}
}
/// The step's data context as the template reads it: Editor.IsStraightening.
public sealed class FakeStep
{
public FakeEditor Editor { get; } = new();
}
public sealed class FakeEditor
{
public bool IsStraightening { get; set; }
}
}