RadioGroupTests.cs 9.2 KB

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