CheckBoxTests.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. using System.ComponentModel;
  2. using Xunit.Abstractions;
  3. namespace Terminal.Gui.ViewsTests;
  4. public class CheckBoxTests
  5. {
  6. private readonly ITestOutputHelper _output;
  7. private static readonly Size _size25x1 = new (25, 1);
  8. public CheckBoxTests (ITestOutputHelper output) { _output = output; }
  9. [Theory]
  10. [InlineData ("01234", 0, 0, 0, 0)]
  11. [InlineData ("01234", 1, 0, 1, 0)]
  12. [InlineData ("01234", 0, 1, 0, 1)]
  13. [InlineData ("01234", 1, 1, 1, 1)]
  14. [InlineData ("01234", 10, 1, 10, 1)]
  15. [InlineData ("01234", 10, 3, 10, 3)]
  16. [InlineData ("0_1234", 0, 0, 0, 0)]
  17. [InlineData ("0_1234", 1, 0, 1, 0)]
  18. [InlineData ("0_1234", 0, 1, 0, 1)]
  19. [InlineData ("0_1234", 1, 1, 1, 1)]
  20. [InlineData ("0_1234", 10, 1, 10, 1)]
  21. [InlineData ("0_12你", 10, 3, 10, 3)]
  22. [InlineData ("0_12你", 0, 0, 0, 0)]
  23. [InlineData ("0_12你", 1, 0, 1, 0)]
  24. [InlineData ("0_12你", 0, 1, 0, 1)]
  25. [InlineData ("0_12你", 1, 1, 1, 1)]
  26. [InlineData ("0_12你", 10, 1, 10, 1)]
  27. [InlineData ("0_12你", 10, 3, 10, 3)]
  28. public void CheckBox_AbsoluteSize_Text (string text, int width, int height, int expectedWidth, int expectedHeight)
  29. {
  30. var checkBox = new CheckBox
  31. {
  32. X = 0,
  33. Y = 0,
  34. Width = width,
  35. Height = height,
  36. Text = text
  37. };
  38. Assert.Equal (new Size (expectedWidth, expectedHeight), checkBox.Frame.Size);
  39. Assert.Equal (new Size (expectedWidth, expectedHeight), checkBox.Viewport.Size);
  40. Assert.Equal (new Size (expectedWidth, expectedHeight), checkBox.TextFormatter.Size);
  41. checkBox.Dispose ();
  42. }
  43. [Theory]
  44. [InlineData (0, 0, 0, 0)]
  45. [InlineData (1, 0, 1, 0)]
  46. [InlineData (0, 1, 0, 1)]
  47. [InlineData (1, 1, 1, 1)]
  48. [InlineData (10, 1, 10, 1)]
  49. [InlineData (10, 3, 10, 3)]
  50. public void CheckBox_AbsoluteSize_DefaultText (int width, int height, int expectedWidth, int expectedHeight)
  51. {
  52. var checkBox = new CheckBox
  53. {
  54. X = 0,
  55. Y = 0,
  56. Width = width,
  57. Height = height,
  58. };
  59. Assert.Equal (new Size (expectedWidth, expectedHeight), checkBox.Frame.Size);
  60. Assert.Equal (new Size (expectedWidth, expectedHeight), checkBox.Viewport.Size);
  61. Assert.Equal (new Size (expectedWidth, expectedHeight), checkBox.TextFormatter.Size);
  62. checkBox.Dispose ();
  63. }
  64. // Test that Title and Text are the same
  65. [Fact]
  66. public void Text_Mirrors_Title ()
  67. {
  68. var view = new CheckBox ();
  69. view.Title = "Hello";
  70. Assert.Equal ("Hello", view.Title);
  71. Assert.Equal ($"Hello", view.TitleTextFormatter.Text);
  72. Assert.Equal ("Hello", view.Text);
  73. Assert.Equal ($"{CM.Glyphs.UnChecked} Hello", view.TextFormatter.Text);
  74. }
  75. [Fact]
  76. public void Title_Mirrors_Text ()
  77. {
  78. var view = new CheckBox ();
  79. view.Text = "Hello";
  80. Assert.Equal ("Hello", view.Text);
  81. Assert.Equal ($"{CM.Glyphs.UnChecked} Hello", view.TextFormatter.Text);
  82. Assert.Equal ("Hello", view.Title);
  83. Assert.Equal ($"Hello", view.TitleTextFormatter.Text);
  84. }
  85. [Fact]
  86. [AutoInitShutdown]
  87. public void AllowNullChecked_Get_Set ()
  88. {
  89. var checkBox = new CheckBox { Text = "Check this out 你" };
  90. Toplevel top = new ();
  91. top.Add (checkBox);
  92. Application.Begin (top);
  93. Assert.False (checkBox.Checked);
  94. Assert.True (checkBox.NewKeyDownEvent (Key.Space));
  95. Assert.True (checkBox.Checked);
  96. Assert.True (checkBox.NewMouseEvent (new MouseEvent { X = 0, Y = 0, Flags = MouseFlags.Button1Clicked }));
  97. Assert.False (checkBox.Checked);
  98. checkBox.AllowNullChecked = true;
  99. Assert.True (checkBox.NewKeyDownEvent (Key.Space));
  100. Assert.Null (checkBox.Checked);
  101. Application.Refresh ();
  102. TestHelpers.AssertDriverContentsWithFrameAre (
  103. @$"
  104. {CM.Glyphs.NullChecked} Check this out 你",
  105. _output
  106. );
  107. Assert.True (checkBox.NewMouseEvent (new MouseEvent { X = 0, Y = 0, Flags = MouseFlags.Button1Clicked }));
  108. Assert.True (checkBox.Checked);
  109. Assert.True (checkBox.NewKeyDownEvent (Key.Space));
  110. Assert.False (checkBox.Checked);
  111. Assert.True (checkBox.NewMouseEvent (new MouseEvent { X = 0, Y = 0, Flags = MouseFlags.Button1Clicked }));
  112. Assert.Null (checkBox.Checked);
  113. checkBox.AllowNullChecked = false;
  114. Assert.False (checkBox.Checked);
  115. }
  116. [Fact]
  117. public void Constructors_Defaults ()
  118. {
  119. var ckb = new CheckBox ();
  120. Assert.True (ckb.Width is Dim.DimAuto);
  121. Assert.Equal (Dim.Sized (1), ckb.Height);
  122. Assert.False (ckb.Checked);
  123. Assert.False (ckb.AllowNullChecked);
  124. Assert.Equal (string.Empty, ckb.Text);
  125. Assert.Equal ($"{CM.Glyphs.UnChecked} ", ckb.TextFormatter.Text);
  126. Assert.True (ckb.CanFocus);
  127. Assert.Equal (new Rectangle (0, 0, 2, 1), ckb.Frame);
  128. ckb = new CheckBox { Text = "Test", Checked = true };
  129. Assert.True (ckb.Width is Dim.DimAuto);
  130. Assert.Equal (Dim.Sized (1), ckb.Height);
  131. Assert.True (ckb.Checked);
  132. Assert.False (ckb.AllowNullChecked);
  133. Assert.Equal ("Test", ckb.Text);
  134. Assert.Equal ($"{CM.Glyphs.Checked} Test", ckb.TextFormatter.Text);
  135. Assert.True (ckb.CanFocus);
  136. Assert.Equal (new Rectangle (0, 0, 6, 1), ckb.Frame);
  137. ckb = new CheckBox { Text = "Test", X = 1, Y = 2 };
  138. Assert.True (ckb.Width is Dim.DimAuto);
  139. Assert.Equal (Dim.Sized (1), ckb.Height);
  140. Assert.False (ckb.Checked);
  141. Assert.False (ckb.AllowNullChecked);
  142. Assert.Equal ("Test", ckb.Text);
  143. Assert.Equal ($"{CM.Glyphs.UnChecked} Test", ckb.TextFormatter.Text);
  144. Assert.True (ckb.CanFocus);
  145. Assert.Equal (new Rectangle (1, 2, 6, 1), ckb.Frame);
  146. ckb = new CheckBox { Text = "Test", X = 3, Y = 4, Checked = true };
  147. Assert.True (ckb.Width is Dim.DimAuto);
  148. Assert.Equal (Dim.Sized (1), ckb.Height);
  149. Assert.True (ckb.Checked);
  150. Assert.False (ckb.AllowNullChecked);
  151. Assert.Equal ("Test", ckb.Text);
  152. Assert.Equal ($"{CM.Glyphs.Checked} Test", ckb.TextFormatter.Text);
  153. Assert.True (ckb.CanFocus);
  154. Assert.Equal (new Rectangle (3, 4, 6, 1), ckb.Frame);
  155. }
  156. [Fact]
  157. public void KeyBindings_Command ()
  158. {
  159. var toggled = false;
  160. var ckb = new CheckBox ();
  161. ckb.Toggled += (s, e) => toggled = true;
  162. Assert.False (ckb.Checked);
  163. Assert.False (toggled);
  164. Assert.Equal (Key.Empty, ckb.HotKey);
  165. ckb.Text = "_Test";
  166. Assert.Equal (Key.T, ckb.HotKey);
  167. Assert.True (ckb.NewKeyDownEvent (Key.T));
  168. Assert.True (ckb.Checked);
  169. Assert.True (toggled);
  170. ckb.Text = "T_est";
  171. toggled = false;
  172. Assert.Equal (Key.E, ckb.HotKey);
  173. Assert.True (ckb.NewKeyDownEvent (Key.E.WithAlt));
  174. Assert.True (toggled);
  175. Assert.False (ckb.Checked);
  176. toggled = false;
  177. Assert.Equal (Key.E, ckb.HotKey);
  178. Assert.True (ckb.NewKeyDownEvent (Key.E));
  179. Assert.True (toggled);
  180. Assert.True (ckb.Checked);
  181. toggled = false;
  182. Assert.True (ckb.NewKeyDownEvent (Key.Space));
  183. Assert.True (toggled);
  184. Assert.False (ckb.Checked);
  185. toggled = false;
  186. Assert.True (ckb.NewKeyDownEvent (Key.Space));
  187. Assert.True (toggled);
  188. Assert.True (ckb.Checked);
  189. toggled = false;
  190. Assert.False (ckb.NewKeyDownEvent (Key.Enter));
  191. Assert.False (toggled);
  192. Assert.True (ckb.Checked);
  193. }
  194. [Fact]
  195. public void Accept_Cancel_Event_OnAccept_Returns_True ()
  196. {
  197. var ckb = new CheckBox ();
  198. var acceptInvoked = false;
  199. ckb.Accept += ViewOnAccept;
  200. var ret = ckb.InvokeCommand (Command.Accept);
  201. Assert.True (ret);
  202. Assert.True (acceptInvoked);
  203. return;
  204. void ViewOnAccept (object sender, CancelEventArgs e)
  205. {
  206. acceptInvoked = true;
  207. e.Cancel = true;
  208. }
  209. }
  210. [Fact]
  211. [AutoInitShutdown]
  212. public void TextJustification_Centered ()
  213. {
  214. var checkBox = new CheckBox
  215. {
  216. X = 1,
  217. Y = Pos.Center (),
  218. Text = "Check this out 你",
  219. TextJustification = Alignment.Centered,
  220. Width = 25
  221. };
  222. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill (), Title = "Test Demo 你" };
  223. win.Add (checkBox);
  224. var top = new Toplevel ();
  225. top.Add (win);
  226. Application.Begin (top);
  227. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  228. Assert.Equal (Alignment.Centered, checkBox.TextJustification);
  229. Assert.Equal (new (1, 1, 25, 1), checkBox.Frame);
  230. Assert.Equal (_size25x1, checkBox.TextFormatter.Size);
  231. var expected = @$"
  232. ┌┤Test Demo 你├──────────────┐
  233. │ │
  234. │ {CM.Glyphs.UnChecked} Check this out 你 │
  235. │ │
  236. └────────────────────────────┘
  237. ";
  238. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  239. Assert.Equal (new (0, 0, 30, 5), pos);
  240. checkBox.Checked = true;
  241. Application.Refresh ();
  242. expected = @$"
  243. ┌┤Test Demo 你├──────────────┐
  244. │ │
  245. │ {CM.Glyphs.Checked} Check this out 你 │
  246. │ │
  247. └────────────────────────────┘
  248. ";
  249. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  250. Assert.Equal (new (0, 0, 30, 5), pos);
  251. }
  252. [Fact]
  253. [AutoInitShutdown]
  254. public void TextJustification_Justified ()
  255. {
  256. var checkBox1 = new CheckBox
  257. {
  258. X = 1,
  259. Y = Pos.Center (),
  260. Text = "Check first out 你",
  261. TextJustification = Alignment.Justified,
  262. Width = 25
  263. };
  264. var checkBox2 = new CheckBox
  265. {
  266. X = 1,
  267. Y = Pos.Bottom (checkBox1),
  268. Text = "Check second out 你",
  269. TextJustification = Alignment.Justified,
  270. Width = 25
  271. };
  272. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill (), Title = "Test Demo 你" };
  273. win.Add (checkBox1, checkBox2);
  274. var top = new Toplevel ();
  275. top.Add (win);
  276. Application.Begin (top);
  277. ((FakeDriver)Application.Driver).SetBufferSize (30, 6);
  278. Assert.Equal (Alignment.Justified, checkBox1.TextJustification);
  279. Assert.Equal (new (1, 1, 25, 1), checkBox1.Frame);
  280. Assert.Equal (Alignment.Justified, checkBox2.TextJustification);
  281. Assert.Equal (new (1, 2, 25, 1), checkBox2.Frame);
  282. var expected = @$"
  283. ┌┤Test Demo 你├──────────────┐
  284. │ │
  285. │ {CM.Glyphs.UnChecked} Check first out 你 │
  286. │ {CM.Glyphs.UnChecked} Check second out 你 │
  287. │ │
  288. └────────────────────────────┘
  289. ";
  290. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  291. Assert.Equal (new (0, 0, 30, 6), pos);
  292. checkBox1.Checked = true;
  293. Assert.Equal (new (1, 1, 25, 1), checkBox1.Frame);
  294. Assert.Equal (_size25x1, checkBox1.TextFormatter.Size);
  295. checkBox2.Checked = true;
  296. Assert.Equal (new (1, 2, 25, 1), checkBox2.Frame);
  297. Assert.Equal (_size25x1, checkBox2.TextFormatter.Size);
  298. Application.Refresh ();
  299. expected = @$"
  300. ┌┤Test Demo 你├──────────────┐
  301. │ │
  302. │ {CM.Glyphs.Checked} Check first out 你 │
  303. │ {CM.Glyphs.Checked} Check second out 你 │
  304. │ │
  305. └────────────────────────────┘
  306. ";
  307. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  308. Assert.Equal (new (0, 0, 30, 6), pos);
  309. }
  310. [Fact]
  311. [AutoInitShutdown]
  312. public void TextJustification_Left ()
  313. {
  314. var checkBox = new CheckBox
  315. {
  316. X = 1,
  317. Y = Pos.Center (),
  318. Text = "Check this out 你",
  319. Width = 25
  320. };
  321. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill (), Title = "Test Demo 你" };
  322. win.Add (checkBox);
  323. var top = new Toplevel ();
  324. top.Add (win);
  325. Application.Begin (top);
  326. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  327. Assert.Equal (Alignment.Left, checkBox.TextJustification);
  328. Assert.Equal (new (1, 1, 25, 1), checkBox.Frame);
  329. Assert.Equal (_size25x1, checkBox.TextFormatter.Size);
  330. var expected = @$"
  331. ┌┤Test Demo 你├──────────────┐
  332. │ │
  333. │ {CM.Glyphs.UnChecked} Check this out 你 │
  334. │ │
  335. └────────────────────────────┘
  336. ";
  337. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  338. Assert.Equal (new (0, 0, 30, 5), pos);
  339. checkBox.Checked = true;
  340. Application.Refresh ();
  341. expected = @$"
  342. ┌┤Test Demo 你├──────────────┐
  343. │ │
  344. │ {CM.Glyphs.Checked} Check this out 你 │
  345. │ │
  346. └────────────────────────────┘
  347. ";
  348. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  349. Assert.Equal (new (0, 0, 30, 5), pos);
  350. }
  351. [Fact]
  352. [AutoInitShutdown]
  353. public void TextJustification_Right ()
  354. {
  355. var checkBox = new CheckBox
  356. {
  357. X = 1,
  358. Y = Pos.Center (),
  359. Text = "Check this out 你",
  360. TextJustification = Alignment.Right,
  361. Width = 25
  362. };
  363. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill (), Title = "Test Demo 你" };
  364. win.Add (checkBox);
  365. var top = new Toplevel ();
  366. top.Add (win);
  367. Application.Begin (top);
  368. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  369. Assert.Equal (Alignment.Right, checkBox.TextJustification);
  370. Assert.Equal (new (1, 1, 25, 1), checkBox.Frame);
  371. Assert.Equal (_size25x1, checkBox.TextFormatter.Size);
  372. var expected = @$"
  373. ┌┤Test Demo 你├──────────────┐
  374. │ │
  375. │ Check this out 你 {CM.Glyphs.UnChecked} │
  376. │ │
  377. └────────────────────────────┘
  378. ";
  379. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  380. Assert.Equal (new (0, 0, 30, 5), pos);
  381. checkBox.Checked = true;
  382. Application.Refresh ();
  383. expected = @$"
  384. ┌┤Test Demo 你├──────────────┐
  385. │ │
  386. │ Check this out 你 {CM.Glyphs.Checked} │
  387. │ │
  388. └────────────────────────────┘
  389. ";
  390. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  391. Assert.Equal (new (0, 0, 30, 5), pos);
  392. }
  393. [Fact]
  394. public void HotKey_Command_Fires_Accept ()
  395. {
  396. var cb = new CheckBox ();
  397. var accepted = false;
  398. cb.Accept += CheckBoxOnAccept;
  399. cb.InvokeCommand (Command.HotKey);
  400. Assert.True (accepted);
  401. return;
  402. void CheckBoxOnAccept (object sender, CancelEventArgs e) { accepted = true; }
  403. }
  404. [Theory]
  405. [InlineData (true)]
  406. [InlineData (false)]
  407. [InlineData (null)]
  408. public void Toggled_Cancel_Event_Prevents_Toggle (bool? initialState)
  409. {
  410. var ckb = new CheckBox () { AllowNullChecked = true };
  411. var checkedInvoked = false;
  412. ckb.Toggled += CheckBoxToggled;
  413. ckb.Checked = initialState;
  414. Assert.Equal(initialState, ckb.Checked);
  415. var ret = ckb.OnToggled ();
  416. Assert.True (ret);
  417. Assert.True (checkedInvoked);
  418. Assert.Equal (initialState, ckb.Checked);
  419. return;
  420. void CheckBoxToggled (object sender, CancelEventArgs e)
  421. {
  422. checkedInvoked = true;
  423. e.Cancel = true;
  424. }
  425. }
  426. }