RadioGroupTests.cs 7.5 KB

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