RadioGroupTests.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. using Xunit.Abstractions;
  2. namespace Terminal.Gui.ViewsTests;
  3. public class RadioGroupTests
  4. {
  5. private readonly ITestOutputHelper _output;
  6. public RadioGroupTests (ITestOutputHelper output) { _output = output; }
  7. [Fact]
  8. public void Constructors_Defaults ()
  9. {
  10. var rg = new RadioGroup ();
  11. Assert.True (rg.CanFocus);
  12. Assert.Empty (rg.RadioLabels);
  13. Assert.Equal (Rect.Empty, rg.Frame);
  14. Assert.Equal (0, rg.SelectedItem);
  15. rg = new RadioGroup { RadioLabels = new [] { "Test" } };
  16. Assert.True (rg.CanFocus);
  17. Assert.Single (rg.RadioLabels);
  18. Assert.Equal (new Rect (0, 0, 0, 0), rg.Frame);
  19. Assert.Equal (0, rg.SelectedItem);
  20. rg = new RadioGroup
  21. {
  22. X = 1,
  23. Y = 2,
  24. Width = 20,
  25. Height = 5,
  26. RadioLabels = new [] { "Test" }
  27. };
  28. Assert.True (rg.CanFocus);
  29. Assert.Single (rg.RadioLabels);
  30. Assert.Equal (new Rect (1, 2, 20, 5), rg.Frame);
  31. Assert.Equal (0, rg.SelectedItem);
  32. rg = new RadioGroup { X = 1, Y = 2, RadioLabels = new [] { "Test" } };
  33. var view = new View { Width = 30, Height = 40 };
  34. view.Add (rg);
  35. view.BeginInit ();
  36. view.EndInit ();
  37. view.LayoutSubviews ();
  38. Assert.True (rg.CanFocus);
  39. Assert.Single (rg.RadioLabels);
  40. Assert.Equal (new Rect (1, 2, 6, 1), rg.Frame);
  41. Assert.Equal (0, rg.SelectedItem);
  42. }
  43. [Fact]
  44. public void Initialize_SelectedItem_With_Minus_One ()
  45. {
  46. var rg = new RadioGroup { RadioLabels = new [] { "Test" }, SelectedItem = -1 };
  47. Assert.Equal (-1, rg.SelectedItem);
  48. Assert.True (rg.NewKeyDownEvent (new Key (KeyCode.Space)));
  49. Assert.Equal (0, rg.SelectedItem);
  50. }
  51. [Fact]
  52. public void KeyBindings_Are_Added_Correctly ()
  53. {
  54. var rg = new RadioGroup { RadioLabels = new [] { "_Left", "_Right" } };
  55. Assert.NotEmpty (rg.KeyBindings.GetCommands (KeyCode.L));
  56. Assert.NotEmpty (rg.KeyBindings.GetCommands (KeyCode.R));
  57. Assert.NotEmpty (rg.KeyBindings.GetCommands (KeyCode.L | KeyCode.ShiftMask));
  58. Assert.NotEmpty (rg.KeyBindings.GetCommands (KeyCode.L | KeyCode.AltMask));
  59. Assert.NotEmpty (rg.KeyBindings.GetCommands (KeyCode.R | KeyCode.ShiftMask));
  60. Assert.NotEmpty (rg.KeyBindings.GetCommands (KeyCode.R | KeyCode.AltMask));
  61. }
  62. [Fact]
  63. public void KeyBindings_Command ()
  64. {
  65. var rg = new RadioGroup { RadioLabels = new [] { "Test", "New Test" } };
  66. Assert.True (rg.NewKeyDownEvent (new Key (KeyCode.CursorUp)));
  67. Assert.True (rg.NewKeyDownEvent (new Key (KeyCode.CursorDown)));
  68. Assert.True (rg.NewKeyDownEvent (new Key (KeyCode.Home)));
  69. Assert.True (rg.NewKeyDownEvent (new Key (KeyCode.End)));
  70. Assert.True (rg.NewKeyDownEvent (new Key (KeyCode.Space)));
  71. Assert.Equal (1, rg.SelectedItem);
  72. }
  73. [Fact]
  74. public void KeyBindings_HotKeys ()
  75. {
  76. var rg = new RadioGroup { RadioLabels = new [] { "_Left", "_Right", "Cen_tered", "_Justified" } };
  77. Assert.NotEmpty (rg.KeyBindings.GetCommands (KeyCode.L));
  78. Assert.NotEmpty (rg.KeyBindings.GetCommands (KeyCode.L | KeyCode.ShiftMask));
  79. Assert.NotEmpty (rg.KeyBindings.GetCommands (KeyCode.L | KeyCode.AltMask));
  80. // BUGBUG: These tests only test that RG works on it's own, not if it's a subview
  81. Assert.True (rg.NewKeyDownEvent (new Key (KeyCode.T)));
  82. Assert.Equal (2, rg.SelectedItem);
  83. Assert.True (rg.NewKeyDownEvent (new Key (KeyCode.L)));
  84. Assert.Equal (0, rg.SelectedItem);
  85. Assert.True (rg.NewKeyDownEvent (new Key (KeyCode.J)));
  86. Assert.Equal (3, rg.SelectedItem);
  87. Assert.True (rg.NewKeyDownEvent (new Key (KeyCode.R)));
  88. Assert.Equal (1, rg.SelectedItem);
  89. Assert.True (rg.NewKeyDownEvent (new Key (KeyCode.T | KeyCode.AltMask)));
  90. Assert.Equal (2, rg.SelectedItem);
  91. Assert.True (rg.NewKeyDownEvent (new Key (KeyCode.L | KeyCode.AltMask)));
  92. Assert.Equal (0, rg.SelectedItem);
  93. Assert.True (rg.NewKeyDownEvent (new Key (KeyCode.J | KeyCode.AltMask)));
  94. Assert.Equal (3, rg.SelectedItem);
  95. Assert.True (rg.NewKeyDownEvent (new Key (KeyCode.R | KeyCode.AltMask)));
  96. Assert.Equal (1, rg.SelectedItem);
  97. var superView = new View ();
  98. superView.Add (rg);
  99. Assert.True (superView.NewKeyDownEvent (new Key (KeyCode.T)));
  100. Assert.Equal (2, rg.SelectedItem);
  101. Assert.True (superView.NewKeyDownEvent (new Key (KeyCode.L)));
  102. Assert.Equal (0, rg.SelectedItem);
  103. Assert.True (superView.NewKeyDownEvent (new Key (KeyCode.J)));
  104. Assert.Equal (3, rg.SelectedItem);
  105. Assert.True (superView.NewKeyDownEvent (new Key (KeyCode.R)));
  106. Assert.Equal (1, rg.SelectedItem);
  107. Assert.True (superView.NewKeyDownEvent (new Key (KeyCode.T | KeyCode.AltMask)));
  108. Assert.Equal (2, rg.SelectedItem);
  109. Assert.True (superView.NewKeyDownEvent (new Key (KeyCode.L | KeyCode.AltMask)));
  110. Assert.Equal (0, rg.SelectedItem);
  111. Assert.True (superView.NewKeyDownEvent (new Key (KeyCode.J | KeyCode.AltMask)));
  112. Assert.Equal (3, rg.SelectedItem);
  113. Assert.True (superView.NewKeyDownEvent (new Key (KeyCode.R | KeyCode.AltMask)));
  114. Assert.Equal (1, rg.SelectedItem);
  115. }
  116. [Fact]
  117. [AutoInitShutdown]
  118. public void Orientation_Width_Height_Vertical_Horizontal_Space ()
  119. {
  120. var rg = new RadioGroup { RadioLabels = new [] { "Test", "New Test 你" } };
  121. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  122. win.Add (rg);
  123. Application.Top.Add (win);
  124. Application.Begin (Application.Top);
  125. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  126. Assert.Equal (Orientation.Vertical, rg.Orientation);
  127. Assert.Equal (2, rg.RadioLabels.Length);
  128. Assert.Equal (0, rg.X);
  129. Assert.Equal (0, rg.Y);
  130. Assert.Equal (13, rg.Frame.Width);
  131. Assert.Equal (2, rg.Frame.Height);
  132. var expected = @$"
  133. ┌────────────────────────────┐
  134. │{
  135. CM.Glyphs.Selected
  136. } Test │
  137. │{
  138. CM.Glyphs.UnSelected
  139. } New Test 你 │
  140. │ │
  141. └────────────────────────────┘
  142. ";
  143. Rect pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  144. Assert.Equal (new Rect (0, 0, 30, 5), pos);
  145. rg.Orientation = Orientation.Horizontal;
  146. Application.Refresh ();
  147. Assert.Equal (Orientation.Horizontal, rg.Orientation);
  148. Assert.Equal (2, rg.HorizontalSpace);
  149. Assert.Equal (0, rg.X);
  150. Assert.Equal (0, rg.Y);
  151. Assert.Equal (21, rg.Width);
  152. Assert.Equal (1, rg.Height);
  153. expected = @$"
  154. ┌────────────────────────────┐
  155. │{
  156. CM.Glyphs.Selected
  157. } Test {
  158. CM.Glyphs.UnSelected
  159. } New Test 你 │
  160. │ │
  161. │ │
  162. └────────────────────────────┘
  163. ";
  164. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  165. Assert.Equal (new Rect (0, 0, 30, 5), pos);
  166. rg.HorizontalSpace = 4;
  167. Application.Refresh ();
  168. Assert.Equal (Orientation.Horizontal, rg.Orientation);
  169. Assert.Equal (4, rg.HorizontalSpace);
  170. Assert.Equal (0, rg.X);
  171. Assert.Equal (0, rg.Y);
  172. Assert.Equal (23, rg.Width);
  173. Assert.Equal (1, rg.Height);
  174. expected = @$"
  175. ┌────────────────────────────┐
  176. │{
  177. CM.Glyphs.Selected
  178. } Test {
  179. CM.Glyphs.UnSelected
  180. } New Test 你 │
  181. │ │
  182. │ │
  183. └────────────────────────────┘
  184. ";
  185. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  186. Assert.Equal (new Rect (0, 0, 30, 5), pos);
  187. }
  188. [Fact]
  189. public void SelectedItemChanged_Event ()
  190. {
  191. int previousSelectedItem = -1;
  192. int selectedItem = -1;
  193. var rg = new RadioGroup { RadioLabels = new [] { "Test", "New Test" } };
  194. rg.SelectedItemChanged += (s, e) =>
  195. {
  196. previousSelectedItem = e.PreviousSelectedItem;
  197. selectedItem = e.SelectedItem;
  198. };
  199. rg.SelectedItem = 1;
  200. Assert.Equal (0, previousSelectedItem);
  201. Assert.Equal (selectedItem, rg.SelectedItem);
  202. }
  203. }