RadioGroupTests.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. rg.SetFocus();
  65. Assert.True (rg.NewKeyDownEvent (Key.CursorUp));
  66. Assert.True (rg.NewKeyDownEvent (Key.CursorDown));
  67. Assert.True (rg.NewKeyDownEvent (Key.Home));
  68. Assert.True (rg.NewKeyDownEvent (Key.End));
  69. Assert.True (rg.NewKeyDownEvent (Key.Space));
  70. Assert.Equal (1, rg.SelectedItem);
  71. }
  72. [Fact]
  73. public void HotKeys_Select_RadioLabels ()
  74. {
  75. var rg = new RadioGroup { RadioLabels = new [] { "_Left", "_Right", "Cen_tered", "_Justified" } };
  76. Assert.NotEmpty (rg.KeyBindings.GetCommands (KeyCode.L));
  77. Assert.NotEmpty (rg.KeyBindings.GetCommands (KeyCode.L | KeyCode.ShiftMask));
  78. Assert.NotEmpty (rg.KeyBindings.GetCommands (KeyCode.L | KeyCode.AltMask));
  79. // BUGBUG: These tests only test that RG works on it's own, not if it's a subview
  80. Assert.True (rg.NewKeyDownEvent (Key.T));
  81. Assert.Equal (2, rg.SelectedItem);
  82. Assert.True (rg.NewKeyDownEvent (Key.L));
  83. Assert.Equal (0, rg.SelectedItem);
  84. Assert.True (rg.NewKeyDownEvent (Key.J));
  85. Assert.Equal (3, rg.SelectedItem);
  86. Assert.True (rg.NewKeyDownEvent (Key.R));
  87. Assert.Equal (1, rg.SelectedItem);
  88. Assert.True (rg.NewKeyDownEvent (Key.T.WithAlt));
  89. Assert.Equal (2, rg.SelectedItem);
  90. Assert.True (rg.NewKeyDownEvent (Key.L.WithAlt));
  91. Assert.Equal (0, rg.SelectedItem);
  92. Assert.True (rg.NewKeyDownEvent (Key.J.WithAlt));
  93. Assert.Equal (3, rg.SelectedItem);
  94. Assert.True (rg.NewKeyDownEvent (Key.R.WithAlt));
  95. Assert.Equal (1, rg.SelectedItem);
  96. var superView = new View ();
  97. superView.Add (rg);
  98. Assert.True (superView.NewKeyDownEvent (Key.T));
  99. Assert.Equal (2, rg.SelectedItem);
  100. Assert.True (superView.NewKeyDownEvent (Key.L));
  101. Assert.Equal (0, rg.SelectedItem);
  102. Assert.True (superView.NewKeyDownEvent (Key.J));
  103. Assert.Equal (3, rg.SelectedItem);
  104. Assert.True (superView.NewKeyDownEvent (Key.R));
  105. Assert.Equal (1, rg.SelectedItem);
  106. Assert.True (superView.NewKeyDownEvent (Key.T.WithAlt));
  107. Assert.Equal (2, rg.SelectedItem);
  108. Assert.True (superView.NewKeyDownEvent (Key.L.WithAlt));
  109. Assert.Equal (0, rg.SelectedItem);
  110. Assert.True (superView.NewKeyDownEvent (Key.J.WithAlt));
  111. Assert.Equal (3, rg.SelectedItem);
  112. Assert.True (superView.NewKeyDownEvent (Key.R.WithAlt));
  113. Assert.Equal (1, rg.SelectedItem);
  114. }
  115. [Fact]
  116. public void HotKey_For_View_SetsFocus ()
  117. {
  118. var superView = new View
  119. {
  120. CanFocus = true
  121. };
  122. superView.Add (new View { CanFocus = true });
  123. var group = new RadioGroup
  124. {
  125. Title = "Radio_Group",
  126. RadioLabels = new [] { "_Left", "_Right", "Cen_tered", "_Justified" }
  127. };
  128. superView.Add (group);
  129. Assert.False (group.HasFocus);
  130. Assert.Equal (0, group.SelectedItem);
  131. group.NewKeyDownEvent (Key.G.WithAlt);
  132. Assert.Equal (0, group.SelectedItem);
  133. Assert.True (group.HasFocus);
  134. }
  135. [Fact]
  136. public void HotKey_For_Item_SetsFocus ()
  137. {
  138. var superView = new View
  139. {
  140. CanFocus = true
  141. };
  142. superView.Add (new View { CanFocus = true });
  143. var group = new RadioGroup { RadioLabels = new [] { "_Left", "_Right", "Cen_tered", "_Justified" } };
  144. superView.Add (group);
  145. Assert.False (group.HasFocus);
  146. Assert.Equal (0, group.SelectedItem);
  147. group.NewKeyDownEvent (Key.R);
  148. Assert.Equal (1, group.SelectedItem);
  149. Assert.True (group.HasFocus);
  150. }
  151. [Fact]
  152. public void HotKey_Command_Does_Not_Accept ()
  153. {
  154. var group = new RadioGroup { RadioLabels = new [] { "_Left", "_Right", "Cen_tered", "_Justified" } };
  155. var accepted = false;
  156. group.Accept += OnAccept;
  157. group.InvokeCommand (Command.HotKey);
  158. Assert.False (accepted);
  159. return;
  160. void OnAccept (object sender, HandledEventArgs e) { accepted = true; }
  161. }
  162. [Fact]
  163. public void Accept_Command_Fires_Accept ()
  164. {
  165. var group = new RadioGroup { RadioLabels = new [] { "_Left", "_Right", "Cen_tered", "_Justified" } };
  166. var accepted = false;
  167. group.Accept += OnAccept;
  168. group.InvokeCommand (Command.Accept);
  169. Assert.True (accepted);
  170. return;
  171. void OnAccept (object sender, HandledEventArgs e) { accepted = true; }
  172. }
  173. [Fact]
  174. [AutoInitShutdown]
  175. public void Orientation_Width_Height_Vertical_Horizontal_Space ()
  176. {
  177. var rg = new RadioGroup { RadioLabels = new [] { "Test", "New Test 你" } };
  178. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  179. win.Add (rg);
  180. var top = new Toplevel ();
  181. top.Add (win);
  182. Application.Begin (top);
  183. ((FakeDriver)Application.Driver!).SetBufferSize (30, 5);
  184. Assert.Equal (Orientation.Vertical, rg.Orientation);
  185. Assert.Equal (2, rg.RadioLabels.Length);
  186. Assert.Equal (0, rg.X);
  187. Assert.Equal (0, rg.Y);
  188. Assert.Equal (13, rg.Frame.Width);
  189. Assert.Equal (2, rg.Frame.Height);
  190. var expected = @$"
  191. ┌────────────────────────────┐
  192. │{CM.Glyphs.Selected} Test │
  193. │{CM.Glyphs.UnSelected} New Test 你 │
  194. │ │
  195. └────────────────────────────┘
  196. ";
  197. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  198. Assert.Equal (new (0, 0, 30, 5), pos);
  199. rg.Orientation = Orientation.Horizontal;
  200. Application.Refresh ();
  201. Assert.Equal (Orientation.Horizontal, rg.Orientation);
  202. Assert.Equal (2, rg.HorizontalSpace);
  203. Assert.Equal (0, rg.X);
  204. Assert.Equal (0, rg.Y);
  205. Assert.Equal (21, rg.Frame.Width);
  206. Assert.Equal (1, rg.Frame.Height);
  207. expected = @$"
  208. ┌────────────────────────────┐
  209. │{CM.Glyphs.Selected} Test {CM.Glyphs.UnSelected} New Test 你 │
  210. │ │
  211. │ │
  212. └────────────────────────────┘
  213. ";
  214. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  215. Assert.Equal (new (0, 0, 30, 5), pos);
  216. rg.HorizontalSpace = 4;
  217. Application.Refresh ();
  218. Assert.Equal (Orientation.Horizontal, rg.Orientation);
  219. Assert.Equal (4, rg.HorizontalSpace);
  220. Assert.Equal (0, rg.X);
  221. Assert.Equal (0, rg.Y);
  222. Assert.Equal (23, rg.Frame.Width);
  223. Assert.Equal (1, rg.Frame.Height);
  224. expected = @$"
  225. ┌────────────────────────────┐
  226. │{CM.Glyphs.Selected} Test {CM.Glyphs.UnSelected} New Test 你 │
  227. │ │
  228. │ │
  229. └────────────────────────────┘
  230. ";
  231. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  232. Assert.Equal (new (0, 0, 30, 5), pos);
  233. top.Dispose ();
  234. }
  235. [Fact]
  236. public void SelectedItemChanged_Event ()
  237. {
  238. int previousSelectedItem = -1;
  239. int selectedItem = -1;
  240. var rg = new RadioGroup { RadioLabels = new [] { "Test", "New Test" } };
  241. rg.SelectedItemChanged += (s, e) =>
  242. {
  243. previousSelectedItem = e.PreviousSelectedItem;
  244. selectedItem = e.SelectedItem;
  245. };
  246. rg.SelectedItem = 1;
  247. Assert.Equal (0, previousSelectedItem);
  248. Assert.Equal (selectedItem, rg.SelectedItem);
  249. }
  250. }