using System.Globalization; using Chronos.App.Editor; using Chronos.Core.Imaging.Geometry; using Chronos.Core.Localization; using Chronos.Core.Project; using Chronos.Testing; namespace Chronos.App.Tests.Editor; /// /// The Camera step's rows (owner review 1, R5/R6) and the Exposure step's Look group on an open session of the 96 × 64 /// synthetic sunset: the crop box locked to the aspect through Crop size, X and Y, the orientation controls, Straighten, /// the look written only when it is not the identity, and every row's label and tooltip in English and Italian. /// [Serial] public sealed class CameraTabTests { private static (EditorSession Session, FakeSequenceSource Source) Open(ProjectSettings? project = null) { var content = EditorFixture.Content(EditorFixture.Sunset, project); var source = new FakeSequenceSource(EditorFixture.Sunset, content); return (new EditorSession(source, content, [], TimeSpan.FromMilliseconds(20)), source); } private static ProjectSettings WithAspect(string aspect, string? preset = null) { var project = new ProjectSettings(); project = project with { Camera = project.Camera with { Aspect = project.Camera.Aspect.WithManual(aspect) } }; return preset is null ? project : project with { Export = project.Export with { Preset = project.Export.Preset.WithManual(preset) } }; } private static T Row(IReadOnlyList rows, string headerKey) where T : SettingRow => (T)rows.Single(r => r.Header == Loc.L(headerKey)); private static void Refresh(IReadOnlyList rows) { foreach (var row in rows) row.Refresh(); } [Test] public void TheRotationRowCountsTheTurnsAsShown() { var camera = new CameraSettings(); foreach (var op in new[] { OrientationOp.Mirror, OrientationOp.RotateRight, OrientationOp.RotateRight, OrientationOp.Flip, OrientationOp.RotateRight }) { var before = CameraTab.ShownTurns(camera); camera = OrientationEdit.Apply(camera, op); var expected = op == OrientationOp.RotateRight ? (before + 1) % 4 : (4 - before) % 4; Assert.Equal(expected, CameraTab.ShownTurns(camera), $"after {op}"); } Assert.Equal(3, CameraTab.ShownTurns(new CameraSettings { Rotate = 90, Mirror = true })); Assert.Equal(1, CameraTab.ShownTurns(new CameraSettings { Rotate = 90, Mirror = true, Flip = true })); } [Test] public async Task CropSizeAndPlaceMoveTheBoxLockedToTheAspect() { var (session, _) = Open(WithAspect("9:16")); await using (session) { var rows = CameraTab.FramingRows(session); var size = Row(rows, "Editor.Camera.CropSize"); var x = Row(rows, "Editor.Camera.CropX"); var y = Row(rows, "Editor.Camera.CropY"); Assert.Equal(1.0, size.Value, "no crop: the largest box"); Assert.Equal(0.0, x.Value); Assert.True(x.IsApplicable, "a 9:16 box can move across a 3:2 frame"); Assert.False(y.IsApplicable, "but not up or down"); Assert.Equal(Loc.L("Tooltips.Editor.NotApplicable.NoSlack"), y.NotApplicableReason); var ratio = session.CropLockRatio(session.Project); x.Value = 1; var box = CropFrame.Of(session.Project.Camera.Crop); Assert.Approximately(1, box.Right, 1e-9, "Crop X +1 puts the box at the right edge"); Assert.Approximately(ratio, box.Width / box.Height, 1e-9, "locked to the aspect"); Refresh(rows); size.Value = 0.5; box = CropFrame.Of(session.Project.Camera.Crop); Assert.Approximately(0.5, box.Height, 1e-9, "half the largest box"); Assert.Approximately(1, box.Right, 1e-9, "still at the right edge"); Assert.Approximately(ratio, box.Width / box.Height, 1e-9); Refresh(rows); Assert.True(y.IsApplicable, "a smaller box can move up and down"); Assert.Equal(1.0, x.Value); Assert.Equal("+1.00", x.DisplayValue); Assert.Equal("50 %", size.DisplayValue); y.Value = -1; Assert.Approximately(0, CropFrame.Of(session.Project.Camera.Crop).Y, 1e-9, "Crop Y −1 is the top"); Refresh(rows); size.Value = 0.02; Assert.Approximately(CropFrame.MinimumShare, CropFrame.Of(session.Project.Camera.Crop).Height, 1e-9, "never below a tenth of the largest box"); Assert.True(session.Project.Camera is { OffsetX: 0, OffsetY: 0 }); } } [Test] public async Task TheWholeFrameAtItsOwnProportionsIsNoCrop() { var (session, _) = Open(WithAspect("source", "png-sequence")); await using (session) { var rows = CameraTab.FramingRows(session); var size = Row(rows, "Editor.Camera.CropSize"); size.Value = 0.6; Assert.NotNull(session.Project.Camera.Crop); Refresh(rows); size.Value = 1; Assert.Null(session.Project.Camera.Crop, "the largest box is the whole view: nothing is stored"); Assert.Equal(ProjectSettings.BaseVersion, session.Project.RequiredFormat); } } [Test] public async Task ALegacyCropPlacedByOffsetsIsBakedInAtItsFirstEdit() { var legacy = WithAspect("1:1"); legacy = legacy with { Camera = legacy.Camera with { OffsetX = 1 } }; var (session, _) = Open(legacy); await using (session) { var rows = CameraTab.FramingRows(session); var before = CameraTab.LockedBox(session, session.Project); Assert.Approximately(1, before.Right, 1e-9, "the render puts the square at the right"); Assert.Equal(1.0, Row(rows, "Editor.Camera.CropX").Value, "the rows show it where it renders"); Row(rows, "Editor.Camera.CropSize").Value = 0.8; var after = CropFrame.Of(session.Project.Camera.Crop); Assert.True(session.Project.Camera is { OffsetX: 0, OffsetY: 0 }, "the offsets are baked into the box"); Assert.Approximately(1, after.Right, 1e-9); Assert.Approximately(0.8 * before.Height, after.Height, 1e-9); } } [Test] public async Task TheRotationButtonsTurnAndTheRowShowsTheStoredAngle() { var (session, _) = Open(WithAspect("16:9")); await using (session) { var rows = CameraTab.FramingRows(session); var rotation = Row(rows, "Editor.Camera.Rotation"); Assert.Equal("0°", rotation.Value); Assert.SequenceEqual([Loc.L("Editor.Camera.RotateLeft"), Loc.L("Editor.Camera.RotateRight")], rotation.Actions.Select(a => a.Name)); Assert.SequenceEqual(["Icon.RotateLeft", "Icon.RotateRight"], rotation.Actions.Select(a => a.GlyphKey)); rotation.Actions[1].Command.Execute(null); Refresh(rows); Assert.Equal("90°", rotation.Value); rotation.Actions[0].Command.Execute(null); rotation.Actions[0].Command.Execute(null); Refresh(rows); Assert.Equal("270°", rotation.Value); Assert.Equal("9:16", session.Project.Camera.Aspect.Manual, "three quarter turns: portrait"); } } [Test] public async Task MirrorAndFlipAreSwitchesThatMoveTheCropWithThePicture() { var project = WithAspect("16:9"); project = project with { Camera = project.Camera with { Crop = [0.1, 0.2, 0.3, 0.4] } }; var (session, _) = Open(project); await using (session) { var rows = CameraTab.FramingRows(session); var mirror = Row(rows, "Editor.Camera.Mirror"); var flip = Row(rows, "Editor.Camera.Flip"); Assert.False(mirror.IsOn); mirror.IsOn = true; Assert.True(session.Project.Camera.Mirror); Assert.SequenceEqual([0.6, 0.2, 0.3, 0.4], session.Project.Camera.Crop!, "x becomes 1 − x − w"); flip.IsOn = true; Assert.True(session.Project.Camera.Flip); Assert.SequenceEqual([0.6, 0.4, 0.3, 0.4], session.Project.Camera.Crop!, "y becomes 1 − y − h"); Assert.True(session.Undo()); Refresh(rows); Assert.False(flip.IsOn, "the switch follows undo"); Assert.True(mirror.IsOn); } } [Test] public async Task ASwitchTheProjectDoesNotTakeSpringsBack() { var (session, source) = Open(); await using (session) { var mirror = Row(CameraTab.FramingRows(session), "Editor.Camera.Mirror"); source.IsReadOnly = true; mirror.IsOn = true; Assert.False(mirror.IsOn); Assert.False(session.Project.Camera.Mirror); } } [Test] public async Task StraightenStoresTenthsOfADegreeAndShowsTheGrid() { var (session, _) = Open(); await using (session) { var straighten = Row(CameraTab.FramingRows(session), "Editor.Camera.Straighten"); Assert.True(straighten.ShowsGrid, "using it shows the preview's grid"); Assert.Equal((-15.0, 15.0), (straighten.Minimum, straighten.Maximum)); straighten.Value = -1.23; Assert.Equal(-1.2, session.Project.Camera.Straighten); Assert.Equal("−1.2°", straighten.DisplayValue); Assert.Equal(2, session.Project.RequiredFormat, "a straighten needs format 2"); straighten.Value = 0.02; Assert.Equal(0.0, session.Project.Camera.Straighten); Assert.False(double.IsNegative(session.Project.Camera.Straighten), "never −0"); Assert.Equal(ProjectSettings.BaseVersion, session.Project.RequiredFormat, "level again: format 1"); Assert.True(CameraTab.FramingRows(session).OfType().Count(r => r.ShowsGrid) == 1, "only Straighten"); } } [Test] public async Task TheLookIsWrittenOnlyWhenAFilterIsChosen() { var (session, _) = Open(); await using (session) { var before = session.Project.ToJson(); var rows = ExposureTab.LookRows(session); Assert.True(rows[0] is GroupRow, "the group starts with its name"); var filter = Row(rows, "Editor.Look.Filter"); var strength = Row(rows, "Editor.Look.Strength"); Assert.Equal("none", filter.Selected?.Value); Assert.False(strength.IsApplicable, "no filter: Strength is n/a"); Assert.Equal(Loc.L("Tooltips.Editor.NotApplicable.NoLook"), strength.NotApplicableReason); Assert.SequenceEqual(["None", "Mono", "Sepia", "Warm", "Cool", "Vivid", "Muted", "Fade"], filter.Options.Select(o => o.Label)); filter.Selected = filter.Options.Single(o => Equals(o.Value, "sepia")); Assert.Equal(new LookSettings { Filter = "sepia" }.Filter, session.Project.Look?.Filter); Assert.Null(session.Project.Look!.Strength, "100 % is the default"); Assert.Equal(2, session.Project.RequiredFormat); Refresh(rows); Assert.True(strength.IsApplicable); Assert.Equal(1.0, strength.Value); strength.Value = 0.6; Assert.Equal(0.6, session.Project.Look!.Strength); Assert.Equal("60 %", strength.DisplayValue); strength.Value = 1; Assert.Null(session.Project.Look!.Strength); // 0 % would be a look that changes nothing yet changes the text: None is the one way there. Assert.Equal(ExposureTab.MinimumStrength, strength.Minimum); Assert.Null(ExposureTab.WithLook(session.Project, "sepia", 0).Look, "the identity is written as no look"); Refresh(rows); filter.Selected = filter.Options.Single(o => Equals(o.Value, "none")); Assert.Null(session.Project.Look, "the identity is no look"); Assert.Equal(ProjectSettings.BaseVersion, session.Project.RequiredFormat); Assert.Equal(before, session.Project.ToJson(), "and the text is the one before the look"); } } [Test] public void ALookOfANewerChronosKeepsWhatItDoesNotKnow() { var extra = ProjectSettings.FromJson("""{"look":{"filter":"warm","strength":0.4,"grain":0.2}}"""); var changed = ExposureTab.WithLook(extra, "cool", 0.4); Assert.Equal("cool", changed.Look?.Filter); Assert.Contains("\"grain\":0.2", changed.ToJson().Replace(" ", string.Empty, StringComparison.Ordinal)); } [Test] public async Task EveryRowHasATooltipAndALabelOfAtMostThreeWordsInEnglishAndItalian() { var (session, _) = Open(WithAspect("9:16")); await using (session) { var missing = new List(); void OnMissing(string key) => missing.Add(key); Loc.MissingKey += OnMissing; try { foreach (var culture in new[] { "en", "it" }) { Loc.SetCulture(CultureInfo.GetCultureInfo(culture)); var rows = CameraTab.FramingRows(session).Concat(ExposureTab.LookRows(session)).ToList(); var labels = new List(); foreach (var row in rows) { labels.Add(row.Header); Assert.False(string.IsNullOrWhiteSpace(row.ToolTip), $"{culture}: {row.Header} has a tooltip"); Assert.NotEqual(row.Header, row.ToolTip, $"{culture}: the tooltip explains, it does not repeat the label"); if (row is IconActionsRow icons) { foreach (var action in icons.Actions) { labels.Add(action.Name); Assert.False(string.IsNullOrWhiteSpace(action.ToolTip), $"{culture}: {action.Name} has a tooltip"); } } if (row is ChoiceRow choices) labels.AddRange(choices.Options.Select(o => o.Label)); } labels.AddRange(new[] { "Editor.Camera.ResetFraming", "Editor.Export.OutputSize" }.Select(k => Loc.L(k))); foreach (var key in new[] { "Tooltips.Editor.Camera.ResetFraming", "Tooltips.Editor.Camera.ResetFramingNone", "Tooltips.Editor.Export.OutputSize" }) Assert.False(string.IsNullOrWhiteSpace(Loc.L(key)), key); foreach (var label in labels) Assert.LessOrEqual(label.Split(' ', StringSplitOptions.RemoveEmptyEntries).Length, 3, $"{culture}: \"{label}\" is at most three words"); } } finally { Loc.MissingKey -= OnMissing; Loc.SetCulture(CultureInfo.GetCultureInfo("en")); } Assert.Empty(missing); } } }