RadioGroupTests.cs 11 KB

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