RadioGroupTests.cs 11 KB

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