|
| 1 | +using System.Linq; |
| 2 | +using Avalonia; |
| 3 | +using Avalonia.Automation; |
| 4 | +using Avalonia.Automation.Peers; |
| 5 | +using Avalonia.Controls; |
| 6 | +using Avalonia.Headless; |
| 7 | +using Avalonia.Headless.XUnit; |
| 8 | +using Avalonia.Themes.Fluent; |
| 9 | +using Avalonia.VisualTree; |
| 10 | +using Nikse.SubtitleEdit.Controls; |
| 11 | +using Nikse.SubtitleEdit.Logic; |
| 12 | +using UITests.Logic.Accessibility; |
| 13 | + |
| 14 | +[assembly: AvaloniaTestApplication(typeof(TestAppBuilder))] |
| 15 | + |
| 16 | +namespace UITests.Logic.Accessibility; |
| 17 | + |
| 18 | +public class TestApp : Application |
| 19 | +{ |
| 20 | + public override void Initialize() |
| 21 | + { |
| 22 | + // The custom up/down controls embed a ButtonSpinner + TextBox that need a |
| 23 | + // theme to resolve their templates when shown in a headless window. |
| 24 | + Styles.Add(new FluentTheme()); |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +public static class TestAppBuilder |
| 29 | +{ |
| 30 | + public static AppBuilder BuildAvaloniaApp() => |
| 31 | + AppBuilder.Configure<TestApp>() |
| 32 | + .UseHeadless(new AvaloniaHeadlessPlatformOptions()) |
| 33 | + .WithInterFont(); |
| 34 | +} |
| 35 | + |
| 36 | +/// <summary> |
| 37 | +/// Verifies the editor's time/duration controls expose an accessible Name to the |
| 38 | +/// platform automation layer (what NVDA / JAWS / Narrator / VoiceOver read aloud). |
| 39 | +/// The custom controls receive keyboard focus on an inner PART_TextBox, so the name |
| 40 | +/// set on the outer control must be forwarded to that text box (issue #11553). |
| 41 | +/// </summary> |
| 42 | +public class EditBoxAccessibilityNameTests |
| 43 | +{ |
| 44 | + private static TextBox GetInnerTextBox(Control control) |
| 45 | + { |
| 46 | + // Force the control's template to apply so PART_TextBox exists and the |
| 47 | + // name-forwarding in OnApplyTemplate runs. |
| 48 | + var window = new Window { Content = control, Width = 320, Height = 120 }; |
| 49 | + window.Show(); |
| 50 | + control.ApplyTemplate(); |
| 51 | + |
| 52 | + return control.GetVisualDescendants().OfType<TextBox>().Single(); |
| 53 | + } |
| 54 | + |
| 55 | + private static string? AutomationName(Control control) |
| 56 | + => ControlAutomationPeer.CreatePeerForElement(control)?.GetName(); |
| 57 | + |
| 58 | + [AvaloniaFact] |
| 59 | + public void StartTime_NameReachesAutomationPeer() |
| 60 | + { |
| 61 | + var startTime = new TimeCodeUpDown(); |
| 62 | + AutomationProperties.SetName(startTime, "Start time"); |
| 63 | + |
| 64 | + var inner = GetInnerTextBox(startTime); |
| 65 | + |
| 66 | + Assert.Equal("Start time", AutomationProperties.GetName(inner)); |
| 67 | + Assert.Equal("Start time", AutomationName(inner)); |
| 68 | + } |
| 69 | + |
| 70 | + [AvaloniaFact] |
| 71 | + public void EndTime_NameReachesAutomationPeer() |
| 72 | + { |
| 73 | + var endTime = new TimeCodeUpDown(); |
| 74 | + AutomationProperties.SetName(endTime, "Hide time"); |
| 75 | + |
| 76 | + var inner = GetInnerTextBox(endTime); |
| 77 | + |
| 78 | + Assert.Equal("Hide time", AutomationName(inner)); |
| 79 | + } |
| 80 | + |
| 81 | + [AvaloniaFact] |
| 82 | + public void Duration_NameReachesAutomationPeer() |
| 83 | + { |
| 84 | + var duration = new SecondsUpDown(); |
| 85 | + AutomationProperties.SetName(duration, "Duration"); |
| 86 | + |
| 87 | + var inner = GetInnerTextBox(duration); |
| 88 | + |
| 89 | + Assert.Equal("Duration", AutomationName(inner)); |
| 90 | + } |
| 91 | + |
| 92 | + [AvaloniaFact] |
| 93 | + public void PlainTextBox_NameReachesAutomationPeer() |
| 94 | + { |
| 95 | + // The non-color-tag editor path is a plain TextBox; the name is set directly. |
| 96 | + var textBox = new TextBox(); |
| 97 | + AutomationProperties.SetName(textBox, "Text"); |
| 98 | + |
| 99 | + Assert.Equal("Text", AutomationName(textBox)); |
| 100 | + } |
| 101 | + |
| 102 | + [AvaloniaFact] |
| 103 | + public void NumericUpDown_ForwardsAccessibleNameToInnerTextBox() |
| 104 | + { |
| 105 | + // Built via the real UiUtil factory, which wires up name forwarding to the |
| 106 | + // inner PART_TextBox (the focused element). Covers the Layer field and every |
| 107 | + // other numeric field in the app. |
| 108 | + var nud = UiUtil.MakeNumericUpDownInt(0, 100, 0, double.NaN, new object()); |
| 109 | + AutomationProperties.SetName(nud, "Layer"); |
| 110 | + |
| 111 | + var inner = GetInnerTextBox(nud); |
| 112 | + |
| 113 | + Assert.Equal("Layer", AutomationName(inner)); |
| 114 | + } |
| 115 | +} |
0 commit comments