|
| 1 | +using System.Linq; |
| 2 | +using System.Windows; |
| 3 | +using System.Windows.Automation; |
| 4 | +using Text_Grab.Models; |
| 5 | +using Text_Grab.Utilities; |
| 6 | + |
| 7 | +namespace Tests; |
| 8 | + |
| 9 | +public class UiAutomationUtilitiesTests |
| 10 | +{ |
| 11 | + [Fact] |
| 12 | + public void NormalizeText_TrimsWhitespaceAndCollapsesEmptyLines() |
| 13 | + { |
| 14 | + string normalized = UIAutomationUtilities.NormalizeText(" Hello world \r\n\r\n Second\tline "); |
| 15 | + |
| 16 | + Assert.Equal($"Hello world{Environment.NewLine}Second line", normalized); |
| 17 | + } |
| 18 | + |
| 19 | + [Fact] |
| 20 | + public void TryAddUniqueText_DeduplicatesNormalizedValues() |
| 21 | + { |
| 22 | + HashSet<string> seen = []; |
| 23 | + List<string> output = []; |
| 24 | + |
| 25 | + bool addedFirst = UIAutomationUtilities.TryAddUniqueText(" Hello world ", seen, output); |
| 26 | + bool addedSecond = UIAutomationUtilities.TryAddUniqueText("Hello world", seen, output); |
| 27 | + |
| 28 | + Assert.True(addedFirst); |
| 29 | + Assert.False(addedSecond); |
| 30 | + Assert.Single(output); |
| 31 | + } |
| 32 | + |
| 33 | + [Fact] |
| 34 | + public void FindTargetWindowCandidate_PrefersCenterPointHit() |
| 35 | + { |
| 36 | + WindowSelectionCandidate first = new((nint)1, new Rect(0, 0, 80, 80), "First", 1); |
| 37 | + WindowSelectionCandidate second = new((nint)2, new Rect(90, 0, 80, 80), "Second", 2); |
| 38 | + |
| 39 | + WindowSelectionCandidate? candidate = UIAutomationUtilities.FindTargetWindowCandidate( |
| 40 | + new Rect(100, 10, 20, 20), |
| 41 | + [first, second]); |
| 42 | + |
| 43 | + Assert.Same(second, candidate); |
| 44 | + } |
| 45 | + |
| 46 | + [Fact] |
| 47 | + public void FindTargetWindowCandidate_FallsBackToLargestIntersection() |
| 48 | + { |
| 49 | + WindowSelectionCandidate first = new((nint)1, new Rect(0, 0, 50, 50), "First", 1); |
| 50 | + WindowSelectionCandidate second = new((nint)2, new Rect(60, 0, 80, 80), "Second", 2); |
| 51 | + |
| 52 | + WindowSelectionCandidate? candidate = UIAutomationUtilities.FindTargetWindowCandidate( |
| 53 | + new Rect(40, 40, 30, 30), |
| 54 | + [first, second]); |
| 55 | + |
| 56 | + Assert.Same(second, candidate); |
| 57 | + } |
| 58 | + |
| 59 | + [Fact] |
| 60 | + public void ShouldUseNameFallback_SkipsStructuralControls() |
| 61 | + { |
| 62 | + Assert.False(UIAutomationUtilities.ShouldUseNameFallback(ControlType.Window)); |
| 63 | + Assert.False(UIAutomationUtilities.ShouldUseNameFallback(ControlType.Group)); |
| 64 | + Assert.False(UIAutomationUtilities.ShouldUseNameFallback(ControlType.Pane)); |
| 65 | + Assert.False(UIAutomationUtilities.ShouldUseNameFallback(ControlType.Custom)); |
| 66 | + Assert.False(UIAutomationUtilities.ShouldUseNameFallback(ControlType.Button)); |
| 67 | + Assert.False(UIAutomationUtilities.ShouldUseNameFallback(ControlType.SplitButton)); |
| 68 | + Assert.False(UIAutomationUtilities.ShouldUseNameFallback(ControlType.ComboBox)); |
| 69 | + } |
| 70 | + |
| 71 | + [Fact] |
| 72 | + public void ShouldUseNameFallback_AllowsVisibleTextContainers() |
| 73 | + { |
| 74 | + Assert.True(UIAutomationUtilities.ShouldUseNameFallback(ControlType.Text)); |
| 75 | + Assert.True(UIAutomationUtilities.ShouldUseNameFallback(ControlType.ListItem)); |
| 76 | + Assert.True(UIAutomationUtilities.ShouldUseNameFallback(ControlType.MenuItem)); |
| 77 | + Assert.True(UIAutomationUtilities.ShouldUseNameFallback(ControlType.TabItem)); |
| 78 | + } |
| 79 | + |
| 80 | + [Fact] |
| 81 | + public void GetSamplePoints_UsesCenterPointForSmallSelections() |
| 82 | + { |
| 83 | + IReadOnlyList<Point> samplePoints = UIAutomationUtilities.GetSamplePoints(new Rect(10, 20, 40, 30)); |
| 84 | + |
| 85 | + Point samplePoint = Assert.Single(samplePoints); |
| 86 | + Assert.Equal(new Point(30, 35), samplePoint); |
| 87 | + } |
| 88 | + |
| 89 | + [Fact] |
| 90 | + public void GetSamplePoints_UsesGridForLargerSelections() |
| 91 | + { |
| 92 | + IReadOnlyList<Point> samplePoints = UIAutomationUtilities.GetSamplePoints(new Rect(0, 0, 100, 100)); |
| 93 | + |
| 94 | + Assert.Equal(9, samplePoints.Count); |
| 95 | + Assert.Contains(new Point(50, 50), samplePoints); |
| 96 | + Assert.Contains(new Point(20, 20), samplePoints); |
| 97 | + Assert.Contains(new Point(80, 80), samplePoints); |
| 98 | + } |
| 99 | + |
| 100 | + [Fact] |
| 101 | + public void GetPointProbePoints_ReturnsCenterThenCrosshairNeighbors() |
| 102 | + { |
| 103 | + IReadOnlyList<Point> probePoints = UIAutomationUtilities.GetPointProbePoints(new Point(25, 40)); |
| 104 | + |
| 105 | + Assert.Equal(5, probePoints.Count); |
| 106 | + Assert.Equal(new Point(25, 40), probePoints[0]); |
| 107 | + Assert.Contains(new Point(23, 40), probePoints); |
| 108 | + Assert.Contains(new Point(27, 40), probePoints); |
| 109 | + Assert.Contains(new Point(25, 38), probePoints); |
| 110 | + Assert.Contains(new Point(25, 42), probePoints); |
| 111 | + } |
| 112 | + |
| 113 | + [Fact] |
| 114 | + public void TryClipBounds_ReturnsIntersectionForOverlappingRects() |
| 115 | + { |
| 116 | + bool clipped = UIAutomationUtilities.TryClipBounds( |
| 117 | + new Rect(10, 10, 50, 50), |
| 118 | + new Rect(30, 25, 50, 50), |
| 119 | + out Rect result); |
| 120 | + |
| 121 | + Assert.True(clipped); |
| 122 | + Assert.Equal(new Rect(30, 25, 30, 35), result); |
| 123 | + } |
| 124 | + |
| 125 | + [Fact] |
| 126 | + public void TryClipBounds_ReturnsFalseWhenBoundsDoNotIntersect() |
| 127 | + { |
| 128 | + bool clipped = UIAutomationUtilities.TryClipBounds( |
| 129 | + new Rect(10, 10, 20, 20), |
| 130 | + new Rect(100, 100, 20, 20), |
| 131 | + out Rect result); |
| 132 | + |
| 133 | + Assert.False(clipped); |
| 134 | + Assert.Equal(Rect.Empty, result); |
| 135 | + } |
| 136 | + |
| 137 | + [Fact] |
| 138 | + public void TryAddUniqueOverlayItem_DeduplicatesNormalizedTextAndBounds() |
| 139 | + { |
| 140 | + HashSet<string> seen = []; |
| 141 | + List<UiAutomationOverlayItem> output = []; |
| 142 | + UiAutomationOverlayItem first = new(" Hello world ", new Rect(10.01, 20.01, 30.01, 40.01), UiAutomationOverlaySource.ElementBounds); |
| 143 | + UiAutomationOverlayItem second = new("Hello world", new Rect(10.04, 20.04, 30.04, 40.04), UiAutomationOverlaySource.VisibleTextRange); |
| 144 | + |
| 145 | + bool addedFirst = UIAutomationUtilities.TryAddUniqueOverlayItem(first, seen, output); |
| 146 | + bool addedSecond = UIAutomationUtilities.TryAddUniqueOverlayItem(second, seen, output); |
| 147 | + |
| 148 | + Assert.True(addedFirst); |
| 149 | + Assert.False(addedSecond); |
| 150 | + Assert.Single(output); |
| 151 | + } |
| 152 | + |
| 153 | + [Fact] |
| 154 | + public void SortOverlayItems_OrdersTopThenLeft() |
| 155 | + { |
| 156 | + IReadOnlyList<UiAutomationOverlayItem> sorted = UIAutomationUtilities.SortOverlayItems( |
| 157 | + [ |
| 158 | + new UiAutomationOverlayItem("Bottom", new Rect(40, 30, 10, 10), UiAutomationOverlaySource.ElementBounds), |
| 159 | + new UiAutomationOverlayItem("Right", new Rect(25, 10, 10, 10), UiAutomationOverlaySource.ElementBounds), |
| 160 | + new UiAutomationOverlayItem("Left", new Rect(10, 10, 10, 10), UiAutomationOverlaySource.ElementBounds), |
| 161 | + ]); |
| 162 | + |
| 163 | + Assert.Equal(["Left", "Right", "Bottom"], sorted.Select(item => item.Text)); |
| 164 | + } |
| 165 | +} |
0 commit comments