CheckBoxTests.cs 17 KB

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