RadioGroupTests.cs 9.0 KB

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