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 (new Rectangle (0, 0, 0, 0), rg.Frame);
  20. Assert.Equal (0, rg.SelectedItem);
  21. rg = new RadioGroup
  22. {
  23. X = 1,
  24. Y = 2,
  25. Width = 20,
  26. Height = 5,
  27. RadioLabels = new [] { "Test" }
  28. };
  29. Assert.True (rg.CanFocus);
  30. Assert.Single (rg.RadioLabels);
  31. Assert.Equal (new Rectangle (1, 2, 20, 5), rg.Frame);
  32. Assert.Equal (0, rg.SelectedItem);
  33. rg = new RadioGroup { X = 1, Y = 2, RadioLabels = new [] { "Test" } };
  34. var view = new View { Width = 30, Height = 40 };
  35. view.Add (rg);
  36. view.BeginInit ();
  37. view.EndInit ();
  38. view.LayoutSubviews ();
  39. Assert.True (rg.CanFocus);
  40. Assert.Single (rg.RadioLabels);
  41. Assert.Equal (new Rectangle (1, 2, 6, 1), rg.Frame);
  42. Assert.Equal (0, rg.SelectedItem);
  43. }
  44. [Fact]
  45. public void Initialize_SelectedItem_With_Minus_One ()
  46. {
  47. var rg = new RadioGroup { RadioLabels = new [] { "Test" }, SelectedItem = -1 };
  48. Assert.Equal (-1, rg.SelectedItem);
  49. Assert.True (rg.NewKeyDownEvent (Key.Space));
  50. Assert.Equal (0, rg.SelectedItem);
  51. }
  52. [Fact]
  53. public void KeyBindings_Are_Added_Correctly ()
  54. {
  55. var rg = new RadioGroup { RadioLabels = new [] { "_Left", "_Right" } };
  56. Assert.NotEmpty (rg.KeyBindings.GetCommands (Key.L));
  57. Assert.NotEmpty (rg.KeyBindings.GetCommands (Key.R));
  58. Assert.NotEmpty (rg.KeyBindings.GetCommands (Key.L.WithShift));
  59. Assert.NotEmpty (rg.KeyBindings.GetCommands (Key.L.WithAlt));
  60. Assert.NotEmpty (rg.KeyBindings.GetCommands (Key.R.WithShift));
  61. Assert.NotEmpty (rg.KeyBindings.GetCommands (Key.R.WithAlt));
  62. }
  63. [Fact]
  64. public void KeyBindings_Command ()
  65. {
  66. var rg = new RadioGroup { RadioLabels = new [] { "Test", "New Test" } };
  67. Assert.True (rg.NewKeyDownEvent (Key.CursorUp));
  68. Assert.True (rg.NewKeyDownEvent (Key.CursorDown));
  69. Assert.True (rg.NewKeyDownEvent (Key.Home));
  70. Assert.True (rg.NewKeyDownEvent (Key.End));
  71. Assert.True (rg.NewKeyDownEvent (Key.Space));
  72. Assert.Equal (1, rg.SelectedItem);
  73. }
  74. [Fact]
  75. public void HotKeys_Select_RadioLabels ()
  76. {
  77. var rg = new RadioGroup { RadioLabels = new [] { "_Left", "_Right", "Cen_tered", "_Justified" } };
  78. Assert.NotEmpty (rg.KeyBindings.GetCommands (KeyCode.L));
  79. Assert.NotEmpty (rg.KeyBindings.GetCommands (KeyCode.L | KeyCode.ShiftMask));
  80. Assert.NotEmpty (rg.KeyBindings.GetCommands (KeyCode.L | KeyCode.AltMask));
  81. // BUGBUG: These tests only test that RG works on it's own, not if it's a subview
  82. Assert.True (rg.NewKeyDownEvent (Key.T));
  83. Assert.Equal (2, rg.SelectedItem);
  84. Assert.True (rg.NewKeyDownEvent (Key.L));
  85. Assert.Equal (0, rg.SelectedItem);
  86. Assert.True (rg.NewKeyDownEvent (Key.J));
  87. Assert.Equal (3, rg.SelectedItem);
  88. Assert.True (rg.NewKeyDownEvent (Key.R));
  89. Assert.Equal (1, rg.SelectedItem);
  90. Assert.True (rg.NewKeyDownEvent (Key.T.WithAlt));
  91. Assert.Equal (2, rg.SelectedItem);
  92. Assert.True (rg.NewKeyDownEvent (Key.L.WithAlt));
  93. Assert.Equal (0, rg.SelectedItem);
  94. Assert.True (rg.NewKeyDownEvent (Key.J.WithAlt));
  95. Assert.Equal (3, rg.SelectedItem);
  96. Assert.True (rg.NewKeyDownEvent (Key.R.WithAlt));
  97. Assert.Equal (1, rg.SelectedItem);
  98. var superView = new View ();
  99. superView.Add (rg);
  100. Assert.True (superView.NewKeyDownEvent (Key.T));
  101. Assert.Equal (2, rg.SelectedItem);
  102. Assert.True (superView.NewKeyDownEvent (Key.L));
  103. Assert.Equal (0, rg.SelectedItem);
  104. Assert.True (superView.NewKeyDownEvent (Key.J));
  105. Assert.Equal (3, rg.SelectedItem);
  106. Assert.True (superView.NewKeyDownEvent (Key.R));
  107. Assert.Equal (1, rg.SelectedItem);
  108. Assert.True (superView.NewKeyDownEvent (Key.T.WithAlt));
  109. Assert.Equal (2, rg.SelectedItem);
  110. Assert.True (superView.NewKeyDownEvent (Key.L.WithAlt));
  111. Assert.Equal (0, rg.SelectedItem);
  112. Assert.True (superView.NewKeyDownEvent (Key.J.WithAlt));
  113. Assert.Equal (3, rg.SelectedItem);
  114. Assert.True (superView.NewKeyDownEvent (Key.R.WithAlt));
  115. Assert.Equal (1, rg.SelectedItem);
  116. }
  117. [Fact]
  118. public void HotKey_Command_Does_Not_Accept ()
  119. {
  120. var group = new RadioGroup { RadioLabels = new [] { "_Left", "_Right", "Cen_tered", "_Justified" } };
  121. var accepted = false;
  122. group.Accept += OnAccept;
  123. group.InvokeCommand (Command.HotKey);
  124. Assert.False (accepted);
  125. return;
  126. void OnAccept (object sender, CancelEventArgs e) { accepted = true; }
  127. }
  128. [Fact]
  129. public void Accept_Command_Fires_Accept ()
  130. {
  131. var group = new RadioGroup { RadioLabels = new [] { "_Left", "_Right", "Cen_tered", "_Justified" } };
  132. var accepted = false;
  133. group.Accept += OnAccept;
  134. group.InvokeCommand (Command.Accept);
  135. Assert.True (accepted);
  136. return;
  137. void OnAccept (object sender, CancelEventArgs e) { accepted = true; }
  138. }
  139. [Fact]
  140. [AutoInitShutdown]
  141. public void Orientation_Width_Height_Vertical_Horizontal_Space ()
  142. {
  143. var rg = new RadioGroup { RadioLabels = new [] { "Test", "New Test 你" } };
  144. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  145. win.Add (rg);
  146. Application.Top.Add (win);
  147. Application.Begin (Application.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.Width);
  175. Assert.Equal (1, rg.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.Width);
  196. Assert.Equal (1, rg.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. }