|
| 1 | +using System; |
| 2 | +using Xunit; |
| 3 | +using Tedd.TUI; |
| 4 | +using System.Reflection; |
| 5 | + |
| 6 | +namespace Tedd.TUI.Tests; |
| 7 | + |
| 8 | +public class RadioButtonCoverageTests |
| 9 | +{ |
| 10 | + // Theory for DependencyProperties: TuiColor is a struct, which isn't allowed in InlineData |
| 11 | + [Theory] |
| 12 | + [InlineData((int)ConsoleColor.Blue, (int)ConsoleColor.Red, (int)ConsoleColor.Cyan, 'x', '-')] |
| 13 | + [InlineData((int)ConsoleColor.Green, (int)ConsoleColor.Yellow, (int)ConsoleColor.Gray, 'X', 'O')] |
| 14 | + public void RadioButton_DependencyProperties_GetSet(int focusedForegroundIndex, int checkColorIndex, int bracketColorIndex, char checkedChar, char uncheckedChar) |
| 15 | + { |
| 16 | + var focusedForeground = (TuiColor)(ConsoleColor)focusedForegroundIndex; |
| 17 | + var checkColor = (TuiColor)(ConsoleColor)checkColorIndex; |
| 18 | + var bracketColor = (TuiColor)(ConsoleColor)bracketColorIndex; |
| 19 | + |
| 20 | + var rb = new RadioButton(); |
| 21 | + |
| 22 | + rb.FocusedForeground = focusedForeground; |
| 23 | + Assert.Equal(focusedForeground, rb.FocusedForeground); |
| 24 | + |
| 25 | + rb.CheckColor = checkColor; |
| 26 | + Assert.Equal(checkColor, rb.CheckColor); |
| 27 | + |
| 28 | + rb.BracketColor = bracketColor; |
| 29 | + Assert.Equal(bracketColor, rb.BracketColor); |
| 30 | + |
| 31 | + rb.CheckedChar = checkedChar; |
| 32 | + Assert.Equal(checkedChar, rb.CheckedChar); |
| 33 | + |
| 34 | + rb.UncheckedChar = uncheckedChar; |
| 35 | + Assert.Equal(uncheckedChar, rb.UncheckedChar); |
| 36 | + } |
| 37 | + |
| 38 | + [Theory] |
| 39 | + [InlineData(ConsoleKey.Enter)] |
| 40 | + [InlineData(ConsoleKey.Spacebar)] |
| 41 | + public void RadioButton_OnToggle_UncheckedToChecked(ConsoleKey keyToPress) |
| 42 | + { |
| 43 | + var rb = new RadioButton(); |
| 44 | + |
| 45 | + rb.IsChecked = false; |
| 46 | + |
| 47 | + rb.OnKeyDown(new KeyEventArgs { Key = keyToPress }); |
| 48 | + rb.OnKeyUp(new KeyEventArgs { Key = keyToPress }); |
| 49 | + Assert.True(rb.IsChecked); |
| 50 | + } |
| 51 | + |
| 52 | + [Theory] |
| 53 | + [InlineData("Option", 10, 1)] |
| 54 | + [InlineData("Yes", 7, 1)] |
| 55 | + [InlineData(null, 4, 1)] |
| 56 | + public void RadioButton_MeasureOverride_CalculatesCorrectSize(string? content, int expectedWidth, int expectedHeight) |
| 57 | + { |
| 58 | + var rb = new RadioButton(); |
| 59 | + rb.Content = content; |
| 60 | + |
| 61 | + rb.Measure(new Size(100, 100)); |
| 62 | + |
| 63 | + Assert.Equal(expectedWidth, rb.DesiredSize.Width); |
| 64 | + Assert.Equal(expectedHeight, rb.DesiredSize.Height); |
| 65 | + } |
| 66 | + |
| 67 | + [Theory] |
| 68 | + [InlineData("Test", true, 'o', 't')] |
| 69 | + [InlineData("No", false, ' ', 'o')] |
| 70 | + public void RadioButton_Render_OutputsExpectedCharacters(string content, bool isChecked, char expectedCheckChar, char lastChar) |
| 71 | + { |
| 72 | + var rb = new RadioButton(); |
| 73 | + rb.Content = content; |
| 74 | + rb.IsChecked = isChecked; |
| 75 | + rb.Measure(new Size(10, 1)); |
| 76 | + rb.Arrange(new Rect(0, 0, 10, 1)); |
| 77 | + |
| 78 | + var buffer = new VirtualBuffer(10, 1); |
| 79 | + rb.Render(buffer, 0, 0); |
| 80 | + |
| 81 | + Assert.Equal('(', buffer.GetPixel(0, 0).Character); |
| 82 | + Assert.Equal(expectedCheckChar, buffer.GetPixel(1, 0).Character); |
| 83 | + Assert.Equal(')', buffer.GetPixel(2, 0).Character); |
| 84 | + |
| 85 | + if (content.Length > 0) |
| 86 | + { |
| 87 | + Assert.Equal(content[0], buffer.GetPixel(4, 0).Character); |
| 88 | + int lastIndex = 4 + content.Length - 1; |
| 89 | + Assert.Equal(lastChar, buffer.GetPixel(lastIndex, 0).Character); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + [Theory] |
| 94 | + [InlineData((int)ConsoleColor.Blue)] |
| 95 | + [InlineData((int)ConsoleColor.Red)] |
| 96 | + public void RadioButton_Render_Focused_OutputsExpectedForeground(int expectedColorIndex) |
| 97 | + { |
| 98 | + var expectedColor = (TuiColor)(ConsoleColor)expectedColorIndex; |
| 99 | + var p = new StackPanel(); |
| 100 | + var w = new TuiWindow(); |
| 101 | + w.Content = p; |
| 102 | + |
| 103 | + var rb = new RadioButton(); |
| 104 | + p.AddChild(rb); |
| 105 | + |
| 106 | + rb.Content = "T"; |
| 107 | + rb.IsChecked = false; |
| 108 | + rb.FocusedForeground = expectedColor; |
| 109 | + |
| 110 | + w.Measure(new Size(100, 100)); |
| 111 | + w.Arrange(new Rect(0, 0, 100, 100)); |
| 112 | + |
| 113 | + rb.Focus(); |
| 114 | + |
| 115 | + var buffer = new VirtualBuffer(10, 1); |
| 116 | + rb.Render(buffer, 0, 0); |
| 117 | + |
| 118 | + Assert.Equal(expectedColor, buffer.GetPixel(4, 0).Foreground); |
| 119 | + } |
| 120 | + |
| 121 | + [Theory] |
| 122 | + [InlineData(ConsoleKey.DownArrow)] |
| 123 | + [InlineData(ConsoleKey.UpArrow)] |
| 124 | + public void RadioButton_NavigateToSibling_NotStackPanel_Returns(ConsoleKey keyToPress) |
| 125 | + { |
| 126 | + var grid = new Grid(); |
| 127 | + var rb1 = new RadioButton { GroupName = "Group" }; |
| 128 | + var rb2 = new RadioButton { GroupName = "Group" }; |
| 129 | + grid.AddChild(rb1); |
| 130 | + grid.AddChild(rb2); |
| 131 | + |
| 132 | + rb1.Focus(); |
| 133 | + rb1.OnKeyDown(new KeyEventArgs { Key = keyToPress }); |
| 134 | + |
| 135 | + Assert.False(rb2.IsChecked); |
| 136 | + Assert.False(rb2.IsFocused); |
| 137 | + } |
| 138 | + |
| 139 | + private class MaliciousRadioButton : RadioButton |
| 140 | + { |
| 141 | + public Action? BeforeNavigate; |
| 142 | + public override void OnKeyDown(KeyEventArgs e) |
| 143 | + { |
| 144 | + BeforeNavigate?.Invoke(); |
| 145 | + base.OnKeyDown(e); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + [Theory] |
| 150 | + [InlineData(ConsoleKey.DownArrow)] |
| 151 | + [InlineData(ConsoleKey.RightArrow)] |
| 152 | + public void RadioButton_NavigateToSibling_EmptyPanel_Returns(ConsoleKey keyToPress) |
| 153 | + { |
| 154 | + var panel = new StackPanel(); |
| 155 | + var rb1 = new MaliciousRadioButton { GroupName = "Group" }; |
| 156 | + |
| 157 | + panel.AddChild(rb1); |
| 158 | + |
| 159 | + rb1.BeforeNavigate = () => panel.Children.Clear(); |
| 160 | + |
| 161 | + rb1.Focus(); |
| 162 | + rb1.OnKeyDown(new KeyEventArgs { Key = keyToPress }); |
| 163 | + |
| 164 | + Assert.False(rb1.IsChecked); |
| 165 | + } |
| 166 | + |
| 167 | + [Theory] |
| 168 | + [InlineData(ConsoleKey.DownArrow)] |
| 169 | + [InlineData(ConsoleKey.RightArrow)] |
| 170 | + public void RadioButton_NavigateToSibling_NoStart_Returns(ConsoleKey keyToPress) |
| 171 | + { |
| 172 | + var panel = new StackPanel(); |
| 173 | + var rb1 = new MaliciousRadioButton { GroupName = "Group" }; |
| 174 | + var rb2 = new RadioButton { GroupName = "Group" }; |
| 175 | + |
| 176 | + panel.AddChild(rb1); |
| 177 | + panel.AddChild(rb2); |
| 178 | + |
| 179 | + rb1.BeforeNavigate = () => { |
| 180 | + panel.Children.Clear(); |
| 181 | + panel.AddChild(new Button()); |
| 182 | + }; |
| 183 | + |
| 184 | + rb1.Focus(); |
| 185 | + rb1.OnKeyDown(new KeyEventArgs { Key = keyToPress }); |
| 186 | + |
| 187 | + Assert.False(rb2.IsChecked); |
| 188 | + } |
| 189 | + |
| 190 | + [Theory] |
| 191 | + [InlineData(ConsoleKey.DownArrow)] |
| 192 | + [InlineData(ConsoleKey.RightArrow)] |
| 193 | + public void RadioButton_NavigateToSibling_LoopAround_Returns(ConsoleKey keyToPress) |
| 194 | + { |
| 195 | + var panel = new StackPanel(); |
| 196 | + var rb1 = new RadioButton { GroupName = "Group" }; |
| 197 | + var other = new Button(); |
| 198 | + panel.AddChild(rb1); |
| 199 | + panel.AddChild(other); |
| 200 | + |
| 201 | + rb1.Focus(); |
| 202 | + rb1.OnKeyDown(new KeyEventArgs { Key = keyToPress }); |
| 203 | + |
| 204 | + Assert.False(rb1.IsChecked); |
| 205 | + } |
| 206 | + |
| 207 | + [Theory] |
| 208 | + [InlineData(ConsoleKey.UpArrow)] |
| 209 | + [InlineData(ConsoleKey.LeftArrow)] |
| 210 | + public void RadioButton_NavigateToSibling_UpArrow_WrapsToLast(ConsoleKey keyToPress) |
| 211 | + { |
| 212 | + var w = new TuiWindow(); |
| 213 | + var panel = new StackPanel(); |
| 214 | + w.Content = panel; |
| 215 | + |
| 216 | + var rb1 = new RadioButton { GroupName = "Group" }; |
| 217 | + var rb2 = new RadioButton { GroupName = "Group" }; |
| 218 | + |
| 219 | + panel.AddChild(rb1); |
| 220 | + panel.AddChild(rb2); |
| 221 | + w.Measure(new Size(100, 100)); |
| 222 | + w.Arrange(new Rect(0, 0, 100, 100)); |
| 223 | + |
| 224 | + rb1.Focus(); |
| 225 | + rb1.IsChecked = true; |
| 226 | + |
| 227 | + rb1.OnKeyDown(new KeyEventArgs { Key = keyToPress }); |
| 228 | + |
| 229 | + Assert.True(rb2.IsChecked); |
| 230 | + Assert.True(rb2.IsFocused); |
| 231 | + } |
| 232 | + |
| 233 | + [Theory] |
| 234 | + [InlineData(ConsoleKey.A)] |
| 235 | + [InlineData(ConsoleKey.B)] |
| 236 | + public void RadioButton_OnKeyDown_UnhandledKey_CallsBase(ConsoleKey keyToPress) |
| 237 | + { |
| 238 | + var rb = new RadioButton(); |
| 239 | + var args = new KeyEventArgs { Key = keyToPress }; |
| 240 | + rb.OnKeyDown(args); |
| 241 | + |
| 242 | + Assert.False(args.Handled); |
| 243 | + } |
| 244 | + |
| 245 | + [Theory] |
| 246 | + [InlineData(ConsoleKey.DownArrow)] |
| 247 | + [InlineData(ConsoleKey.RightArrow)] |
| 248 | + public void RadioButton_NavigateToSibling_FocusesAndChecksAlreadyUnchecked(ConsoleKey keyToPress) |
| 249 | + { |
| 250 | + var w = new TuiWindow(); |
| 251 | + var panel = new StackPanel(); |
| 252 | + w.Content = panel; |
| 253 | + var rb1 = new RadioButton { GroupName = "Group" }; |
| 254 | + var rb2 = new RadioButton { GroupName = "Group" }; |
| 255 | + |
| 256 | + panel.AddChild(rb1); |
| 257 | + panel.AddChild(rb2); |
| 258 | + w.Measure(new Size(100, 100)); |
| 259 | + w.Arrange(new Rect(0, 0, 100, 100)); |
| 260 | + |
| 261 | + rb2.IsChecked = false; |
| 262 | + |
| 263 | + rb1.Focus(); |
| 264 | + rb1.IsChecked = true; |
| 265 | + |
| 266 | + rb1.OnKeyDown(new KeyEventArgs { Key = keyToPress }); |
| 267 | + |
| 268 | + Assert.True(rb2.IsChecked); |
| 269 | + Assert.True(rb2.IsFocused); |
| 270 | + } |
| 271 | + |
| 272 | + [Theory] |
| 273 | + [InlineData(ConsoleKey.DownArrow)] |
| 274 | + [InlineData(ConsoleKey.RightArrow)] |
| 275 | + public void RadioButton_NavigateToSibling_DifferentGroup_Ignored(ConsoleKey keyToPress) |
| 276 | + { |
| 277 | + var w = new TuiWindow(); |
| 278 | + var panel = new StackPanel(); |
| 279 | + w.Content = panel; |
| 280 | + var rb1 = new RadioButton { GroupName = "Group1" }; |
| 281 | + var rb2 = new RadioButton { GroupName = "Group2" }; |
| 282 | + var rb3 = new RadioButton { GroupName = "Group1" }; |
| 283 | + |
| 284 | + panel.AddChild(rb1); |
| 285 | + panel.AddChild(rb2); |
| 286 | + panel.AddChild(rb3); |
| 287 | + w.Measure(new Size(100, 100)); |
| 288 | + w.Arrange(new Rect(0, 0, 100, 100)); |
| 289 | + |
| 290 | + rb1.Focus(); |
| 291 | + rb1.IsChecked = true; |
| 292 | + |
| 293 | + rb1.OnKeyDown(new KeyEventArgs { Key = keyToPress }); |
| 294 | + |
| 295 | + Assert.False(rb2.IsChecked); |
| 296 | + Assert.True(rb3.IsChecked); |
| 297 | + Assert.True(rb3.IsFocused); |
| 298 | + } |
| 299 | + |
| 300 | + [Theory] |
| 301 | + [InlineData(ConsoleKey.DownArrow)] |
| 302 | + [InlineData(ConsoleKey.RightArrow)] |
| 303 | + public void RadioButton_NavigateToSibling_NotCheckedNotSelf_HitsContinue(ConsoleKey keyToPress) |
| 304 | + { |
| 305 | + var panel = new StackPanel(); |
| 306 | + var rb1 = new RadioButton { GroupName = "Group" }; |
| 307 | + var rb2 = new RadioButton { GroupName = "Group" }; |
| 308 | + |
| 309 | + panel.AddChild(rb1); |
| 310 | + panel.AddChild(rb2); |
| 311 | + |
| 312 | + rb1.IsChecked = false; |
| 313 | + rb2.IsChecked = false; |
| 314 | + |
| 315 | + rb1.Focus(); |
| 316 | + rb1.OnKeyDown(new KeyEventArgs { Key = keyToPress }); |
| 317 | + |
| 318 | + Assert.True(rb2.IsChecked); |
| 319 | + } |
| 320 | + |
| 321 | + [Theory] |
| 322 | + [InlineData(ConsoleKey.DownArrow)] |
| 323 | + [InlineData(ConsoleKey.RightArrow)] |
| 324 | + public void RadioButton_NavigateToSibling_EmptyPanel_Hits136(ConsoleKey keyToPress) |
| 325 | + { |
| 326 | + var panel = new StackPanel(); |
| 327 | + var rb1 = new RadioButton { GroupName = "Group" }; |
| 328 | + |
| 329 | + typeof(UIElement).GetProperty("Parent", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance) |
| 330 | + ?.SetValue(rb1, panel); |
| 331 | + |
| 332 | + rb1.Focus(); |
| 333 | + rb1.OnKeyDown(new KeyEventArgs { Key = keyToPress }); |
| 334 | + |
| 335 | + Assert.False(rb1.IsChecked); |
| 336 | + } |
| 337 | + |
| 338 | + [Theory] |
| 339 | + [InlineData(ConsoleKey.DownArrow)] |
| 340 | + [InlineData(ConsoleKey.RightArrow)] |
| 341 | + public void RadioButton_NavigateToSibling_NoStart_Hits163(ConsoleKey keyToPress) |
| 342 | + { |
| 343 | + var panel = new StackPanel(); |
| 344 | + var rb1 = new RadioButton { GroupName = "Group" }; |
| 345 | + var btn = new Button(); |
| 346 | + |
| 347 | + panel.AddChild(btn); |
| 348 | + |
| 349 | + typeof(UIElement).GetProperty("Parent", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance) |
| 350 | + ?.SetValue(rb1, panel); |
| 351 | + |
| 352 | + rb1.Focus(); |
| 353 | + rb1.OnKeyDown(new KeyEventArgs { Key = keyToPress }); |
| 354 | + |
| 355 | + Assert.False(rb1.IsChecked); |
| 356 | + } |
| 357 | +} |
0 commit comments