|
| 1 | +// Copyright (c) 2026 SIL International |
| 2 | +// This software is licensed under the LGPL, version 2.1 or later |
| 3 | +// (http://www.gnu.org/licenses/lgpl-2.1.html) |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.Linq; |
| 7 | +using Avalonia.Controls; |
| 8 | +using Avalonia.Headless.NUnit; |
| 9 | +using Avalonia.VisualTree; |
| 10 | +using NUnit.Framework; |
| 11 | +using SIL.FieldWorks.Common.FwAvalonia.Region; |
| 12 | +using FwAvaloniaDialogsTests; // DialogLayoutAssert |
| 13 | + |
| 14 | +namespace FwAvaloniaTests.VisualChecks |
| 15 | +{ |
| 16 | + /// <summary> |
| 17 | + /// avalonia-interlinear-editor (task 2.3) — T5 visual stages and T3 edge cases for the read-only |
| 18 | + /// interlinear control. Each is captured as a Skia PNG for subjective alignment/crowding review and run |
| 19 | + /// through the shared <see cref="DialogLayoutAssert"/> crowding tripwire. All LCModel-free: the |
| 20 | + /// <see cref="InterlinearAnalysisModel"/> DTOs are built directly (the projection is tested over a real |
| 21 | + /// cache in xWorksTests). Edges: empty analysis (bare wordform), a multi-analysis wordform, and an |
| 22 | + /// RTL/complex-script morpheme run. |
| 23 | + /// </summary> |
| 24 | + [TestFixture] |
| 25 | + public class InterlinearVisualTests |
| 26 | + { |
| 27 | + private static InterlinearBundle Bundle(string morph, string gloss, string gram, string lexEntry = null) |
| 28 | + => new InterlinearBundle(morph, gloss, gram, Guid.NewGuid(), Guid.NewGuid(), Guid.NewGuid(), |
| 29 | + lexEntry ?? morph); |
| 30 | + |
| 31 | + private static InterlinearAnalysisModel Kapula() |
| 32 | + => new InterlinearAnalysisModel("kapula", new[] |
| 33 | + { |
| 34 | + new InterlinearLine("kapula", new[] |
| 35 | + { |
| 36 | + Bundle("ka-", "1Sg.Subj", "pfx", "ka-"), |
| 37 | + Bundle("pul", "see", "v", "pula"), |
| 38 | + Bundle("-a", "Indic", "sfx", "-a") |
| 39 | + }, Guid.NewGuid()) |
| 40 | + }, Guid.NewGuid()); |
| 41 | + |
| 42 | + [AvaloniaTest] |
| 43 | + public void ReadOnlyInterlinear_AlignsColumns() |
| 44 | + { |
| 45 | + var control = new InterlinearRegionEditor(Kapula()); |
| 46 | + DialogSnapshot.Capture(control, "Interlinear-01-readonly", width: 460, height: 180); |
| 47 | + DialogLayoutAssert.AssertNoCrowding(control); |
| 48 | + |
| 49 | + // All three lines plus the wordform render their text (4 rows × 3 columns + wordform). |
| 50 | + var texts = control.GetVisualDescendants().OfType<TextBlock>().Select(t => t.Text).ToList(); |
| 51 | + Assert.That(texts, Does.Contain("kapula")); |
| 52 | + Assert.That(texts, Does.Contain("pul")); |
| 53 | + Assert.That(texts, Does.Contain("see")); |
| 54 | + Assert.That(texts, Does.Contain("Indic")); |
| 55 | + } |
| 56 | + |
| 57 | + [AvaloniaTest] |
| 58 | + public void EmptyAnalysis_RendersTheBareWordform() |
| 59 | + { |
| 60 | + var model = new InterlinearAnalysisModel("kapula", Array.Empty<InterlinearLine>(), Guid.NewGuid()); |
| 61 | + var control = new InterlinearRegionEditor(model); |
| 62 | + |
| 63 | + DialogSnapshot.Capture(control, "Interlinear-02-bare-wordform", width: 420, height: 120); |
| 64 | + DialogLayoutAssert.AssertNoCrowding(control); |
| 65 | + |
| 66 | + Assert.That(model.HasAnalysis, Is.False); |
| 67 | + var texts = control.GetVisualDescendants().OfType<TextBlock>().Select(t => t.Text).ToList(); |
| 68 | + Assert.That(texts, Does.Contain("kapula"), "the bare wordform still shows"); |
| 69 | + } |
| 70 | + |
| 71 | + [AvaloniaTest] |
| 72 | + public void MultiAnalysisWordform_RendersOneLinePerAnalysis() |
| 73 | + { |
| 74 | + var model = new InterlinearAnalysisModel("kapula", new[] |
| 75 | + { |
| 76 | + new InterlinearLine("kapula", new[] { Bundle("ka-", "1Sg", "pfx"), Bundle("pula", "rain", "n") }, |
| 77 | + Guid.NewGuid()), |
| 78 | + new InterlinearLine("kapula", new[] { Bundle("kapula", "downpour", "n") }, Guid.NewGuid()) |
| 79 | + }, Guid.NewGuid()); |
| 80 | + var control = new InterlinearRegionEditor(model); |
| 81 | + |
| 82 | + DialogSnapshot.Capture(control, "Interlinear-03-multi-analysis", width: 460, height: 280); |
| 83 | + DialogLayoutAssert.AssertNoCrowding(control); |
| 84 | + |
| 85 | + var texts = control.GetVisualDescendants().OfType<TextBlock>().Select(t => t.Text).ToList(); |
| 86 | + Assert.That(texts, Does.Contain("rain")); |
| 87 | + Assert.That(texts, Does.Contain("downpour"), "the second analysis line renders too"); |
| 88 | + } |
| 89 | + |
| 90 | + [AvaloniaTest] |
| 91 | + public void EditableMode_RendersSenseAndMsaChoosers_AndInvokesTheCallbacks() |
| 92 | + { |
| 93 | + // W-5 (task 3.1): when the adapter supplies edit choices, both the gloss (sense) and |
| 94 | + // grammatical-info (MSA) cells render editable choosers (buttons opening an FwOptionPicker) |
| 95 | + // instead of static TextBlocks. The control stays LCModel-free — it gets RegionChoiceOption DTOs |
| 96 | + // + callbacks. (The morph line stays read-only — PARITY: re-segmentation is deferred.) |
| 97 | + var model = Kapula(); |
| 98 | + string chosenSenseKey = null; |
| 99 | + string chosenMsaKey = null; |
| 100 | + string chosenMorphKey = null; |
| 101 | + var senseKey = Guid.NewGuid().ToString(); |
| 102 | + var msaKey = Guid.NewGuid().ToString(); |
| 103 | + var morphKey = Guid.NewGuid().ToString(); |
| 104 | + Func<int, int, InterlinearBundleEditChoices> editChoices = (line, bundle) => |
| 105 | + new InterlinearBundleEditChoices( |
| 106 | + new[] { new RegionChoiceOption(senseKey, "rain"), new RegionChoiceOption(Guid.NewGuid().ToString(), "storm") }, |
| 107 | + key => chosenSenseKey = key, |
| 108 | + new[] { new RegionChoiceOption(msaKey, "n"), new RegionChoiceOption(Guid.NewGuid().ToString(), "v") }, |
| 109 | + key => chosenMsaKey = key, |
| 110 | + new[] { new RegionChoiceOption(morphKey, "pula₁"), new RegionChoiceOption(Guid.NewGuid().ToString(), "pula₂") }, |
| 111 | + key => chosenMorphKey = key); |
| 112 | + |
| 113 | + var control = new InterlinearRegionEditor(model, "InterlinearEditor", editChoices: editChoices); |
| 114 | + Assert.That(control.IsEditable, Is.True); |
| 115 | + |
| 116 | + DialogSnapshot.Capture(control, "Interlinear-05-editable", width: 560, height: 220); |
| 117 | + DialogLayoutAssert.AssertNoCrowding(control); |
| 118 | + |
| 119 | + // Three chooser buttons per bundle (lex-entry + sense + MSA) × 3 bundles in Kapula = 9. |
| 120 | + var buttons = control.GetVisualDescendants().OfType<Button>().ToList(); |
| 121 | + Assert.That(buttons, Has.Count.EqualTo(9), |
| 122 | + "an editable lex-entry + gloss + grammatical-info chooser per bundle (morph line stays read-only)"); |
| 123 | + |
| 124 | + // Each chooser routes its chosen key to the right adapter callback. |
| 125 | + CommitFirstOption(buttons.First(b => AutomationId(b) == "InterlinearEditor.Entry0")); |
| 126 | + Assert.That(chosenMorphKey, Is.EqualTo(morphKey), "picking a different entry routes its morph key"); |
| 127 | + |
| 128 | + CommitFirstOption(buttons.First(b => AutomationId(b) == "InterlinearEditor.Gloss0")); |
| 129 | + Assert.That(chosenSenseKey, Is.EqualTo(senseKey), "picking a sense routes its key to the adapter"); |
| 130 | + |
| 131 | + CommitFirstOption(buttons.First(b => AutomationId(b) == "InterlinearEditor.Gram0")); |
| 132 | + Assert.That(chosenMsaKey, Is.EqualTo(msaKey), "picking a grammatical-info/MSA routes its key to the adapter"); |
| 133 | + } |
| 134 | + |
| 135 | + private static string AutomationId(Control c) => Avalonia.Automation.AutomationProperties.GetAutomationId(c); |
| 136 | + |
| 137 | + private static void CommitFirstOption(Button chooserButton) |
| 138 | + { |
| 139 | + var picker = (chooserButton.Flyout as Flyout)?.Content as FwOptionPicker; |
| 140 | + Assert.That(picker, Is.Not.Null, "the chooser button opens an FwOptionPicker flyout"); |
| 141 | + picker.OptionsList.SelectedIndex = 0; |
| 142 | + picker.CommitHighlighted(); |
| 143 | + } |
| 144 | + |
| 145 | + [AvaloniaTest] |
| 146 | + public void RtlComplexScript_RendersRightToLeft_WithoutCrowding() |
| 147 | + { |
| 148 | + // A right-to-left vernacular run (Arabic-script morphemes) with analysis glosses. |
| 149 | + var model = new InterlinearAnalysisModel("كتبها", new[] |
| 150 | + { |
| 151 | + new InterlinearLine("كتبها", new[] |
| 152 | + { |
| 153 | + Bundle("كتب", "write", "v"), |
| 154 | + Bundle("ها", "3Sg.Fem.Obj", "sfx") |
| 155 | + }, Guid.NewGuid()) |
| 156 | + }, Guid.NewGuid()); |
| 157 | + var control = new InterlinearRegionEditor(model, "InterlinearEditor", rightToLeft: true); |
| 158 | + |
| 159 | + DialogSnapshot.Capture(control, "Interlinear-04-rtl-complex", width: 460, height: 180); |
| 160 | + DialogLayoutAssert.AssertNoCrowding(control); |
| 161 | + |
| 162 | + Assert.That(control.FlowDirection, Is.EqualTo(Avalonia.Media.FlowDirection.RightToLeft)); |
| 163 | + var texts = control.GetVisualDescendants().OfType<TextBlock>().Select(t => t.Text).ToList(); |
| 164 | + Assert.That(texts, Does.Contain("write")); |
| 165 | + } |
| 166 | + } |
| 167 | +} |
0 commit comments