CheckBoxTests.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. using System.ComponentModel;
  2. using UnitTests;
  3. using Xunit.Abstractions;
  4. // ReSharper disable AccessToModifiedClosure
  5. namespace Terminal.Gui.ViewsTests;
  6. public class CheckBoxTests (ITestOutputHelper output)
  7. {
  8. private static readonly Size _size25x1 = new (25, 1);
  9. [Fact]
  10. public void Commands_Select ()
  11. {
  12. Application.Navigation = new ();
  13. Application.Top = new ();
  14. View otherView = new () { CanFocus = true };
  15. var ckb = new CheckBox ();
  16. Application.Top.Add (ckb, otherView);
  17. Application.Top.SetFocus ();
  18. Assert.True (ckb.HasFocus);
  19. var checkedStateChangingCount = 0;
  20. ckb.CheckedStateChanging += (s, e) => checkedStateChangingCount++;
  21. var selectCount = 0;
  22. ckb.Selecting += (s, e) => selectCount++;
  23. var acceptCount = 0;
  24. ckb.Accepting += (s, e) => acceptCount++;
  25. Assert.Equal (CheckState.UnChecked, ckb.CheckedState);
  26. Assert.Equal (0, checkedStateChangingCount);
  27. Assert.Equal (0, selectCount);
  28. Assert.Equal (0, acceptCount);
  29. Assert.Equal (Key.Empty, ckb.HotKey);
  30. // Test while focused
  31. ckb.Text = "_Test";
  32. Assert.Equal (Key.T, ckb.HotKey);
  33. ckb.NewKeyDownEvent (Key.T);
  34. Assert.Equal (CheckState.Checked, ckb.CheckedState);
  35. Assert.Equal (1, checkedStateChangingCount);
  36. Assert.Equal (1, selectCount);
  37. Assert.Equal (0, acceptCount);
  38. ckb.Text = "T_est";
  39. Assert.Equal (Key.E, ckb.HotKey);
  40. ckb.NewKeyDownEvent (Key.E.WithAlt);
  41. Assert.Equal (2, checkedStateChangingCount);
  42. Assert.Equal (2, selectCount);
  43. Assert.Equal (0, acceptCount);
  44. ckb.NewKeyDownEvent (Key.Space);
  45. Assert.Equal (3, checkedStateChangingCount);
  46. Assert.Equal (3, selectCount);
  47. Assert.Equal (0, acceptCount);
  48. ckb.NewKeyDownEvent (Key.Enter);
  49. Assert.Equal (3, checkedStateChangingCount);
  50. Assert.Equal (3, selectCount);
  51. Assert.Equal (1, acceptCount);
  52. Application.Top.Dispose ();
  53. Application.ResetState ();
  54. }
  55. #region Mouse Tests
  56. #endregion Mouse Tests
  57. [Fact]
  58. [AutoInitShutdown]
  59. public void TextAlignment_Centered ()
  60. {
  61. var checkBox = new CheckBox
  62. {
  63. X = 1,
  64. Y = Pos.Center (),
  65. Text = "Check this out 你",
  66. TextAlignment = Alignment.Center,
  67. Width = 25
  68. };
  69. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill (), Title = "Test Demo 你" };
  70. win.Add (checkBox);
  71. var top = new Toplevel ();
  72. top.Add (win);
  73. Application.Begin (top);
  74. AutoInitShutdownAttribute.FakeResize (new Size (30, 5));
  75. Assert.Equal (Alignment.Center, checkBox.TextAlignment);
  76. Assert.Equal (new (1, 1, 25, 1), checkBox.Frame);
  77. Assert.Equal (_size25x1, checkBox.TextFormatter.ConstrainToSize);
  78. var expected = @$"
  79. ┌┤Test Demo 你├──────────────┐
  80. │ │
  81. │ {Glyphs.CheckStateUnChecked} Check this out 你 │
  82. │ │
  83. └────────────────────────────┘
  84. ";
  85. Rectangle pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  86. Assert.Equal (new (0, 0, 30, 5), pos);
  87. checkBox.CheckedState = CheckState.Checked;
  88. Application.LayoutAndDraw ();
  89. expected = @$"
  90. ┌┤Test Demo 你├──────────────┐
  91. │ │
  92. │ {Glyphs.CheckStateChecked} Check this out 你 │
  93. │ │
  94. └────────────────────────────┘
  95. ";
  96. pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  97. Assert.Equal (new (0, 0, 30, 5), pos);
  98. top.Dispose ();
  99. }
  100. [Fact]
  101. [AutoInitShutdown]
  102. public void TextAlignment_Justified ()
  103. {
  104. var checkBox1 = new CheckBox
  105. {
  106. X = 1,
  107. Y = Pos.Center (),
  108. Text = "Check first out 你",
  109. TextAlignment = Alignment.Fill,
  110. Width = 25
  111. };
  112. var checkBox2 = new CheckBox
  113. {
  114. X = 1,
  115. Y = Pos.Bottom (checkBox1),
  116. Text = "Check second out 你",
  117. TextAlignment = Alignment.Fill,
  118. Width = 25
  119. };
  120. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill (), Title = "Test Demo 你" };
  121. win.Add (checkBox1, checkBox2);
  122. var top = new Toplevel ();
  123. top.Add (win);
  124. RunState rs = Application.Begin (top);
  125. AutoInitShutdownAttribute.FakeResize(new Size(30, 6));
  126. Assert.Equal (Alignment.Fill, checkBox1.TextAlignment);
  127. Assert.Equal (new (1, 1, 25, 1), checkBox1.Frame);
  128. Assert.Equal (Alignment.Fill, checkBox2.TextAlignment);
  129. Assert.Equal (new (1, 2, 25, 1), checkBox2.Frame);
  130. var expected = @$"
  131. ┌┤Test Demo 你├──────────────┐
  132. │ │
  133. │ {Glyphs.CheckStateUnChecked} Check first out 你 │
  134. │ {Glyphs.CheckStateUnChecked} Check second out 你 │
  135. │ │
  136. └────────────────────────────┘
  137. ";
  138. Rectangle pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  139. Assert.Equal (new (0, 0, 30, 6), pos);
  140. checkBox1.CheckedState = CheckState.Checked;
  141. AutoInitShutdownAttribute.RunIteration ();
  142. Assert.Equal (new (1, 1, 25, 1), checkBox1.Frame);
  143. Assert.Equal (_size25x1, checkBox1.TextFormatter.ConstrainToSize);
  144. checkBox2.CheckedState = CheckState.Checked;
  145. AutoInitShutdownAttribute.RunIteration ();
  146. Assert.Equal (new (1, 2, 25, 1), checkBox2.Frame);
  147. Assert.Equal (_size25x1, checkBox2.TextFormatter.ConstrainToSize);
  148. AutoInitShutdownAttribute.RunIteration ();
  149. expected = @$"
  150. ┌┤Test Demo 你├──────────────┐
  151. │ │
  152. │ {Glyphs.CheckStateChecked} Check first out 你 │
  153. │ {Glyphs.CheckStateChecked} Check second out 你 │
  154. │ │
  155. └────────────────────────────┘
  156. ";
  157. pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  158. Assert.Equal (new (0, 0, 30, 6), pos);
  159. top.Dispose ();
  160. }
  161. [Fact]
  162. [AutoInitShutdown]
  163. public void TextAlignment_Left ()
  164. {
  165. var checkBox = new CheckBox
  166. {
  167. X = 1,
  168. Y = Pos.Center (),
  169. Text = "Check this out 你",
  170. Width = 25
  171. };
  172. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill (), Title = "Test Demo 你" };
  173. win.Add (checkBox);
  174. var top = new Toplevel ();
  175. top.Add (win);
  176. Application.Begin (top);
  177. AutoInitShutdownAttribute.FakeResize(new Size(30, 5));
  178. Assert.Equal (Alignment.Start, checkBox.TextAlignment);
  179. Assert.Equal (new (1, 1, 25, 1), checkBox.Frame);
  180. Assert.Equal (_size25x1, checkBox.TextFormatter.ConstrainToSize);
  181. var expected = @$"
  182. ┌┤Test Demo 你├──────────────┐
  183. │ │
  184. │ {Glyphs.CheckStateUnChecked} Check this out 你 │
  185. │ │
  186. └────────────────────────────┘
  187. ";
  188. Rectangle pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  189. Assert.Equal (new (0, 0, 30, 5), pos);
  190. checkBox.CheckedState = CheckState.Checked;
  191. AutoInitShutdownAttribute.RunIteration ();
  192. expected = @$"
  193. ┌┤Test Demo 你├──────────────┐
  194. │ │
  195. │ {Glyphs.CheckStateChecked} Check this out 你 │
  196. │ │
  197. └────────────────────────────┘
  198. ";
  199. pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  200. Assert.Equal (new (0, 0, 30, 5), pos);
  201. top.Dispose ();
  202. }
  203. [Fact]
  204. [AutoInitShutdown]
  205. public void TextAlignment_Right ()
  206. {
  207. var checkBox = new CheckBox
  208. {
  209. X = 1,
  210. Y = Pos.Center (),
  211. Text = "Check this out 你",
  212. TextAlignment = Alignment.End,
  213. Width = 25
  214. };
  215. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill (), Title = "Test Demo 你" };
  216. win.Add (checkBox);
  217. var top = new Toplevel ();
  218. top.Add (win);
  219. Application.Begin (top);
  220. AutoInitShutdownAttribute.FakeResize(new Size(30, 5));
  221. Assert.Equal (Alignment.End, checkBox.TextAlignment);
  222. Assert.Equal (new (1, 1, 25, 1), checkBox.Frame);
  223. Assert.Equal (_size25x1, checkBox.TextFormatter.ConstrainToSize);
  224. var expected = @$"
  225. ┌┤Test Demo 你├──────────────┐
  226. │ │
  227. │ Check this out 你 {Glyphs.CheckStateUnChecked} │
  228. │ │
  229. └────────────────────────────┘
  230. ";
  231. Rectangle pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  232. Assert.Equal (new (0, 0, 30, 5), pos);
  233. checkBox.CheckedState = CheckState.Checked;
  234. AutoInitShutdownAttribute.RunIteration ();
  235. expected = @$"
  236. ┌┤Test Demo 你├──────────────┐
  237. │ │
  238. │ Check this out 你 {Glyphs.CheckStateChecked} │
  239. │ │
  240. └────────────────────────────┘
  241. ";
  242. pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  243. Assert.Equal (new (0, 0, 30, 5), pos);
  244. top.Dispose ();
  245. }
  246. [Fact]
  247. public void HotKey_Command_Does_Not_Fire_Accept ()
  248. {
  249. var cb = new CheckBox ();
  250. var accepted = false;
  251. cb.Accepting += CheckBoxOnAccept;
  252. cb.InvokeCommand (Command.HotKey);
  253. Assert.False (accepted);
  254. return;
  255. void CheckBoxOnAccept (object sender, CommandEventArgs e) { accepted = true; }
  256. }
  257. [Theory]
  258. [InlineData (CheckState.Checked)]
  259. [InlineData (CheckState.UnChecked)]
  260. [InlineData (CheckState.None)]
  261. public void Selected_Handle_Event_Does_Not_Prevent_Change (CheckState initialState)
  262. {
  263. var ckb = new CheckBox { AllowCheckStateNone = true };
  264. var checkedInvoked = false;
  265. ckb.CheckedState = initialState;
  266. ckb.Selecting += OnSelecting;
  267. Assert.Equal (initialState, ckb.CheckedState);
  268. bool? ret = ckb.InvokeCommand (Command.Select);
  269. Assert.True (ret);
  270. Assert.True (checkedInvoked);
  271. Assert.NotEqual (initialState, ckb.CheckedState);
  272. return;
  273. void OnSelecting (object sender, CommandEventArgs e)
  274. {
  275. checkedInvoked = true;
  276. e.Handled = true;
  277. }
  278. }
  279. [Theory]
  280. [InlineData (CheckState.Checked)]
  281. [InlineData (CheckState.UnChecked)]
  282. [InlineData (CheckState.None)]
  283. public void CheckedStateChanging_Cancel_Event_Prevents_Change (CheckState initialState)
  284. {
  285. var ckb = new CheckBox { AllowCheckStateNone = true };
  286. var checkedInvoked = false;
  287. ckb.CheckedState = initialState;
  288. ckb.CheckedStateChanging += OnCheckedStateChanging;
  289. Assert.Equal (initialState, ckb.CheckedState);
  290. // AdvanceCheckState returns false if the state was changed, true if it was cancelled, null if it was not changed
  291. bool? ret = ckb.AdvanceCheckState ();
  292. Assert.True (ret);
  293. Assert.True (checkedInvoked);
  294. Assert.Equal (initialState, ckb.CheckedState);
  295. return;
  296. void OnCheckedStateChanging (object sender, ResultEventArgs<CheckState> e)
  297. {
  298. checkedInvoked = true;
  299. e.Handled = true;
  300. }
  301. }
  302. }