RadioGroupTests.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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_For_View_SetsFocus ()
  116. {
  117. var superView = new View
  118. {
  119. CanFocus = true
  120. };
  121. superView.Add (new View { CanFocus = true });
  122. var group = new RadioGroup
  123. {
  124. Title = "Radio_Group",
  125. RadioLabels = new [] { "_Left", "_Right", "Cen_tered", "_Justified" }
  126. };
  127. superView.Add (group);
  128. Assert.False (group.HasFocus);
  129. Assert.Equal (0, group.SelectedItem);
  130. group.NewKeyDownEvent (Key.G.WithAlt);
  131. Assert.Equal (0, group.SelectedItem);
  132. Assert.True (group.HasFocus);
  133. }
  134. [Fact]
  135. public void HotKey_For_Item_SetsFocus ()
  136. {
  137. var superView = new View
  138. {
  139. CanFocus = true
  140. };
  141. superView.Add (new View { CanFocus = true });
  142. var group = new RadioGroup { RadioLabels = new [] { "_Left", "_Right", "Cen_tered", "_Justified" } };
  143. superView.Add (group);
  144. Assert.False (group.HasFocus);
  145. Assert.Equal (0, group.SelectedItem);
  146. group.NewKeyDownEvent (Key.R);
  147. Assert.Equal (1, group.SelectedItem);
  148. Assert.True (group.HasFocus);
  149. }
  150. [Fact]
  151. public void HotKey_Command_Does_Not_Accept ()
  152. {
  153. var group = new RadioGroup { RadioLabels = new [] { "_Left", "_Right", "Cen_tered", "_Justified" } };
  154. var accepted = false;
  155. group.Accept += OnAccept;
  156. group.InvokeCommand (Command.HotKey);
  157. Assert.False (accepted);
  158. return;
  159. void OnAccept (object sender, CancelEventArgs e) { accepted = true; }
  160. }
  161. [Fact]
  162. public void Accept_Command_Fires_Accept ()
  163. {
  164. var group = new RadioGroup { RadioLabels = new [] { "_Left", "_Right", "Cen_tered", "_Justified" } };
  165. var accepted = false;
  166. group.Accept += OnAccept;
  167. group.InvokeCommand (Command.Accept);
  168. Assert.True (accepted);
  169. return;
  170. void OnAccept (object sender, CancelEventArgs e) { accepted = true; }
  171. }
  172. [Fact]
  173. [AutoInitShutdown]
  174. public void Orientation_Width_Height_Vertical_Horizontal_Space ()
  175. {
  176. var rg = new RadioGroup { RadioLabels = new [] { "Test", "New Test 你" } };
  177. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  178. win.Add (rg);
  179. var top = new Toplevel ();
  180. top.Add (win);
  181. Application.Begin (top);
  182. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  183. Assert.Equal (Orientation.Vertical, rg.Orientation);
  184. Assert.Equal (2, rg.RadioLabels.Length);
  185. Assert.Equal (0, rg.X);
  186. Assert.Equal (0, rg.Y);
  187. Assert.Equal (13, rg.Frame.Width);
  188. Assert.Equal (2, rg.Frame.Height);
  189. var expected = @$"
  190. ┌────────────────────────────┐
  191. │{CM.Glyphs.Selected} Test │
  192. │{CM.Glyphs.UnSelected} New Test 你 │
  193. │ │
  194. └────────────────────────────┘
  195. ";
  196. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  197. Assert.Equal (new (0, 0, 30, 5), pos);
  198. rg.Orientation = Orientation.Horizontal;
  199. Application.Refresh ();
  200. Assert.Equal (Orientation.Horizontal, rg.Orientation);
  201. Assert.Equal (2, rg.HorizontalSpace);
  202. Assert.Equal (0, rg.X);
  203. Assert.Equal (0, rg.Y);
  204. Assert.Equal (21, rg.Frame.Width);
  205. Assert.Equal (1, rg.Frame.Height);
  206. expected = @$"
  207. ┌────────────────────────────┐
  208. │{CM.Glyphs.Selected} Test {CM.Glyphs.UnSelected} New Test 你 │
  209. │ │
  210. │ │
  211. └────────────────────────────┘
  212. ";
  213. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  214. Assert.Equal (new (0, 0, 30, 5), pos);
  215. rg.HorizontalSpace = 4;
  216. Application.Refresh ();
  217. Assert.Equal (Orientation.Horizontal, rg.Orientation);
  218. Assert.Equal (4, rg.HorizontalSpace);
  219. Assert.Equal (0, rg.X);
  220. Assert.Equal (0, rg.Y);
  221. Assert.Equal (23, rg.Frame.Width);
  222. Assert.Equal (1, rg.Frame.Height);
  223. expected = @$"
  224. ┌────────────────────────────┐
  225. │{CM.Glyphs.Selected} Test {CM.Glyphs.UnSelected} New Test 你 │
  226. │ │
  227. │ │
  228. └────────────────────────────┘
  229. ";
  230. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  231. Assert.Equal (new (0, 0, 30, 5), pos);
  232. top.Dispose ();
  233. }
  234. [Fact]
  235. public void SelectedItemChanged_Event ()
  236. {
  237. int previousSelectedItem = -1;
  238. int selectedItem = -1;
  239. var rg = new RadioGroup { RadioLabels = new [] { "Test", "New Test" } };
  240. rg.SelectedItemChanged += (s, e) =>
  241. {
  242. previousSelectedItem = e.PreviousSelectedItem;
  243. selectedItem = e.SelectedItem;
  244. };
  245. rg.SelectedItem = 1;
  246. Assert.Equal (0, previousSelectedItem);
  247. Assert.Equal (selectedItem, rg.SelectedItem);
  248. }
  249. }