CheckBoxTests.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. using Xunit.Abstractions;
  2. namespace Terminal.Gui.ViewsTests;
  3. public class CheckBoxTests
  4. {
  5. private readonly ITestOutputHelper _output;
  6. public CheckBoxTests (ITestOutputHelper output) { _output = output; }
  7. [Fact]
  8. [AutoInitShutdown]
  9. public void AllowNullChecked_Get_Set ()
  10. {
  11. var checkBox = new CheckBox { Text = "Check this out 你" };
  12. Toplevel top = Application.Top;
  13. top.Add (checkBox);
  14. Application.Begin (top);
  15. Assert.False (checkBox.Checked);
  16. Assert.True (checkBox.NewKeyDownEvent (new Key (KeyCode.Space)));
  17. Assert.True (checkBox.Checked);
  18. Assert.True (checkBox.MouseEvent (new MouseEvent { X = 0, Y = 0, Flags = MouseFlags.Button1Clicked }));
  19. Assert.False (checkBox.Checked);
  20. checkBox.AllowNullChecked = true;
  21. Assert.True (checkBox.NewKeyDownEvent (new Key (KeyCode.Space)));
  22. Assert.Null (checkBox.Checked);
  23. Application.Refresh ();
  24. TestHelpers.AssertDriverContentsWithFrameAre (
  25. @$"
  26. {
  27. CM.Glyphs.NullChecked
  28. } Check this out 你",
  29. _output
  30. );
  31. Assert.True (checkBox.MouseEvent (new MouseEvent { X = 0, Y = 0, Flags = MouseFlags.Button1Clicked }));
  32. Assert.True (checkBox.Checked);
  33. Assert.True (checkBox.NewKeyDownEvent (new Key (KeyCode.Space)));
  34. Assert.False (checkBox.Checked);
  35. Assert.True (checkBox.MouseEvent (new MouseEvent { X = 0, Y = 0, Flags = MouseFlags.Button1Clicked }));
  36. Assert.Null (checkBox.Checked);
  37. checkBox.AllowNullChecked = false;
  38. Assert.False (checkBox.Checked);
  39. }
  40. [Fact]
  41. [AutoInitShutdown]
  42. public void AutoSize_Stays_True_AnchorEnd_With_HotKeySpecifier ()
  43. {
  44. var checkBox = new CheckBox { Y = Pos.Center (), Text = "C_heck this out 你" };
  45. checkBox.X = Pos.AnchorEnd () - Pos.Function (() => checkBox.GetSizeNeededForTextWithoutHotKey ().Width);
  46. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill (), Title = "Test Demo 你" };
  47. win.Add (checkBox);
  48. Application.Top.Add (win);
  49. Assert.True (checkBox.AutoSize);
  50. Application.Begin (Application.Top);
  51. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  52. var expected = @$"
  53. ┌┤Test Demo 你├──────────────┐
  54. │ │
  55. │ {
  56. CM.Glyphs.UnChecked
  57. } Check this out 你│
  58. │ │
  59. └────────────────────────────┘
  60. ";
  61. TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  62. Assert.True (checkBox.AutoSize);
  63. checkBox.Text = "Check this out 你 changed";
  64. Assert.True (checkBox.AutoSize);
  65. Application.Refresh ();
  66. expected = @$"
  67. ┌┤Test Demo 你├──────────────┐
  68. │ │
  69. │ {
  70. CM.Glyphs.UnChecked
  71. } Check this out 你 changed│
  72. │ │
  73. └────────────────────────────┘
  74. ";
  75. TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  76. }
  77. [Fact]
  78. [AutoInitShutdown]
  79. public void AutoSize_Stays_True_AnchorEnd_Without_HotKeySpecifier ()
  80. {
  81. var checkBox = new CheckBox { Y = Pos.Center (), Text = "Check this out 你" };
  82. checkBox.X = Pos.AnchorEnd () - Pos.Function (() => checkBox.GetSizeNeededForTextWithoutHotKey ().Width);
  83. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill (), Title = "Test Demo 你" };
  84. win.Add (checkBox);
  85. Application.Top.Add (win);
  86. Assert.True (checkBox.AutoSize);
  87. Application.Begin (Application.Top);
  88. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  89. var expected = @$"
  90. ┌┤Test Demo 你├──────────────┐
  91. │ │
  92. │ {
  93. CM.Glyphs.UnChecked
  94. } Check this out 你│
  95. │ │
  96. └────────────────────────────┘
  97. ";
  98. TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  99. Assert.True (checkBox.AutoSize);
  100. checkBox.Text = "Check this out 你 changed";
  101. Assert.True (checkBox.AutoSize);
  102. Application.Refresh ();
  103. expected = @$"
  104. ┌┤Test Demo 你├──────────────┐
  105. │ │
  106. │ {
  107. CM.Glyphs.UnChecked
  108. } Check this out 你 changed│
  109. │ │
  110. └────────────────────────────┘
  111. ";
  112. TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  113. }
  114. [Fact]
  115. [AutoInitShutdown]
  116. public void AutoSize_StaysVisible ()
  117. {
  118. var checkBox = new CheckBox { X = 1, Y = Pos.Center (), Text = "Check this out 你" };
  119. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill (), Title = "Test Demo 你" };
  120. win.Add (checkBox);
  121. Application.Top.Add (win);
  122. Assert.False (checkBox.IsInitialized);
  123. RunState runstate = Application.Begin (Application.Top);
  124. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  125. Assert.True (checkBox.IsInitialized);
  126. Assert.Equal (new Rectangle (1, 1, 19, 1), checkBox.Frame);
  127. Assert.Equal ("Check this out 你", checkBox.Text);
  128. Assert.Equal ($"{CM.Glyphs.UnChecked} Check this out 你", checkBox.TextFormatter.Text);
  129. Assert.True (checkBox.AutoSize);
  130. Assert.Equal ("Absolute(19)", checkBox.Width.ToString ());
  131. checkBox.Checked = true;
  132. Assert.Equal ($"{CM.Glyphs.Checked} Check this out 你", checkBox.TextFormatter.Text);
  133. checkBox.AutoSize = false;
  134. // It isn't auto-size so the height is guaranteed by the SetMinWidthHeight
  135. checkBox.Text = "Check this out 你 changed";
  136. var firstIteration = false;
  137. Application.RunIteration (ref runstate, ref firstIteration);
  138. // BUGBUG - v2 - Autosize is busted; disabling tests for now
  139. Assert.Equal (new Rectangle (1, 1, 19, 1), checkBox.Frame);
  140. var expected = @"
  141. ┌┤Test Demo 你├──────────────┐
  142. │ │
  143. │ ☑ Check this out 你 │
  144. │ │
  145. └────────────────────────────┘";
  146. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  147. Assert.Equal (new Rectangle (0, 0, 30, 5), pos);
  148. checkBox.Width = 19;
  149. // It isn't auto-size so the height is guaranteed by the SetMinWidthHeight
  150. checkBox.Text = "Check this out 你 changed";
  151. Application.RunIteration (ref runstate, ref firstIteration);
  152. Assert.False (checkBox.AutoSize);
  153. Assert.Equal (new Rectangle (1, 1, 19, 1), checkBox.Frame);
  154. expected = @"
  155. ┌┤Test Demo 你├──────────────┐
  156. │ │
  157. │ ☑ Check this out 你 │
  158. │ │
  159. └────────────────────────────┘";
  160. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  161. Assert.Equal (new Rectangle (0, 0, 30, 5), pos);
  162. checkBox.AutoSize = true;
  163. Application.RunIteration (ref runstate, ref firstIteration);
  164. Assert.Equal (new Rectangle (1, 1, 27, 1), checkBox.Frame);
  165. expected = @"
  166. ┌┤Test Demo 你├──────────────┐
  167. │ │
  168. │ ☑ Check this out 你 changed│
  169. │ │
  170. └────────────────────────────┘";
  171. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  172. Assert.Equal (new Rectangle (0, 0, 30, 5), pos);
  173. }
  174. [Fact]
  175. public void Constructors_Defaults ()
  176. {
  177. var ckb = new CheckBox ();
  178. Assert.True (ckb.AutoSize);
  179. Assert.False (ckb.Checked);
  180. Assert.False (ckb.AllowNullChecked);
  181. Assert.Equal (string.Empty, ckb.Text);
  182. Assert.Equal ($"{CM.Glyphs.UnChecked} ", ckb.TextFormatter.Text);
  183. Assert.True (ckb.CanFocus);
  184. Assert.Equal (new Rectangle (0, 0, 2, 1), ckb.Frame);
  185. ckb = new CheckBox { Text = "Test", Checked = true };
  186. Assert.True (ckb.AutoSize);
  187. Assert.True (ckb.Checked);
  188. Assert.False (ckb.AllowNullChecked);
  189. Assert.Equal ("Test", ckb.Text);
  190. Assert.Equal ($"{CM.Glyphs.Checked} Test", ckb.TextFormatter.Text);
  191. Assert.True (ckb.CanFocus);
  192. Assert.Equal (new Rectangle (0, 0, 6, 1), ckb.Frame);
  193. ckb = new CheckBox { Text = "Test", X = 1, Y = 2 };
  194. Assert.True (ckb.AutoSize);
  195. Assert.False (ckb.Checked);
  196. Assert.False (ckb.AllowNullChecked);
  197. Assert.Equal ("Test", ckb.Text);
  198. Assert.Equal ($"{CM.Glyphs.UnChecked} Test", ckb.TextFormatter.Text);
  199. Assert.True (ckb.CanFocus);
  200. Assert.Equal (new Rectangle (1, 2, 6, 1), ckb.Frame);
  201. ckb = new CheckBox { Text = "Test", X = 3, Y = 4, Checked = true };
  202. Assert.True (ckb.AutoSize);
  203. Assert.True (ckb.Checked);
  204. Assert.False (ckb.AllowNullChecked);
  205. Assert.Equal ("Test", ckb.Text);
  206. Assert.Equal ($"{CM.Glyphs.Checked} Test", ckb.TextFormatter.Text);
  207. Assert.True (ckb.CanFocus);
  208. Assert.Equal (new Rectangle (3, 4, 6, 1), ckb.Frame);
  209. }
  210. [Fact]
  211. [AutoInitShutdown]
  212. public void KeyBindings_Command ()
  213. {
  214. var toggled = false;
  215. var ckb = new CheckBox ();
  216. ckb.Toggled += (s, e) => toggled = true;
  217. Application.Top.Add (ckb);
  218. Application.Begin (Application.Top);
  219. Assert.False (ckb.Checked);
  220. Assert.False (toggled);
  221. Assert.Equal (KeyCode.Null, ckb.HotKey);
  222. ckb.Text = "_Test";
  223. Assert.Equal (KeyCode.T, ckb.HotKey);
  224. Assert.True (Application.Top.NewKeyDownEvent (new Key (KeyCode.T)));
  225. Assert.True (ckb.Checked);
  226. Assert.True (toggled);
  227. ckb.Text = "T_est";
  228. toggled = false;
  229. Assert.Equal (KeyCode.E, ckb.HotKey);
  230. Assert.True (Application.Top.NewKeyDownEvent (new Key (KeyCode.E | KeyCode.AltMask)));
  231. Assert.True (toggled);
  232. Assert.False (ckb.Checked);
  233. toggled = false;
  234. Assert.Equal (KeyCode.E, ckb.HotKey);
  235. Assert.True (Application.Top.NewKeyDownEvent (new Key (KeyCode.E)));
  236. Assert.True (toggled);
  237. Assert.True (ckb.Checked);
  238. toggled = false;
  239. Assert.True (Application.Top.NewKeyDownEvent (new Key ((KeyCode)' ')));
  240. Assert.True (toggled);
  241. Assert.False (ckb.Checked);
  242. toggled = false;
  243. Assert.True (Application.Top.NewKeyDownEvent (new Key (KeyCode.Space)));
  244. Assert.True (toggled);
  245. Assert.True (ckb.Checked);
  246. Assert.True (ckb.AutoSize);
  247. Application.Refresh ();
  248. var expected = @$"
  249. {
  250. CM.Glyphs.Checked
  251. } Test
  252. ";
  253. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  254. Assert.Equal (new Rectangle (0, 0, 6, 1), pos);
  255. }
  256. [Fact]
  257. [AutoInitShutdown]
  258. public void TextAlignment_Centered ()
  259. {
  260. var checkBox = new CheckBox
  261. {
  262. X = 1,
  263. Y = Pos.Center (),
  264. Text = "Check this out 你",
  265. TextAlignment = TextAlignment.Centered,
  266. AutoSize = false,
  267. Width = 25
  268. };
  269. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill (), Title = "Test Demo 你" };
  270. win.Add (checkBox);
  271. Application.Top.Add (win);
  272. Application.Begin (Application.Top);
  273. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  274. Assert.Equal (TextAlignment.Centered, checkBox.TextAlignment);
  275. Assert.Equal (new Rectangle (1, 1, 25, 1), checkBox.Frame);
  276. Assert.Equal (new Size (25, 1), checkBox.TextFormatter.Size);
  277. Assert.False (checkBox.AutoSize);
  278. var expected = @$"
  279. ┌┤Test Demo 你├──────────────┐
  280. │ │
  281. │ {
  282. CM.Glyphs.UnChecked
  283. } Check this out 你 │
  284. │ │
  285. └────────────────────────────┘
  286. ";
  287. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  288. Assert.Equal (new Rectangle (0, 0, 30, 5), pos);
  289. checkBox.Checked = true;
  290. Application.Refresh ();
  291. expected = @$"
  292. ┌┤Test Demo 你├──────────────┐
  293. │ │
  294. │ {
  295. CM.Glyphs.Checked
  296. } Check this out 你 │
  297. │ │
  298. └────────────────────────────┘
  299. ";
  300. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  301. Assert.Equal (new Rectangle (0, 0, 30, 5), pos);
  302. }
  303. [Fact]
  304. [AutoInitShutdown]
  305. public void TextAlignment_Justified ()
  306. {
  307. var checkBox1 = new CheckBox
  308. {
  309. X = 1,
  310. Y = Pos.Center (),
  311. Text = "Check first out 你",
  312. TextAlignment = TextAlignment.Justified,
  313. AutoSize = false,
  314. Width = 25
  315. };
  316. var checkBox2 = new CheckBox
  317. {
  318. X = 1,
  319. Y = Pos.Bottom (checkBox1),
  320. Text = "Check second out 你",
  321. TextAlignment = TextAlignment.Justified,
  322. AutoSize = false,
  323. Width = 25
  324. };
  325. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill (), Title = "Test Demo 你" };
  326. win.Add (checkBox1, checkBox2);
  327. Application.Top.Add (win);
  328. Application.Begin (Application.Top);
  329. ((FakeDriver)Application.Driver).SetBufferSize (30, 6);
  330. Assert.Equal (TextAlignment.Justified, checkBox1.TextAlignment);
  331. Assert.Equal (new Rectangle (1, 1, 25, 1), checkBox1.Frame);
  332. Assert.Equal (new Size (25, 1), checkBox1.TextFormatter.Size);
  333. Assert.Equal (TextAlignment.Justified, checkBox2.TextAlignment);
  334. Assert.Equal (new Rectangle (1, 2, 25, 1), checkBox2.Frame);
  335. Assert.Equal (new Size (25, 1), checkBox2.TextFormatter.Size);
  336. var expected = @$"
  337. ┌┤Test Demo 你├──────────────┐
  338. │ │
  339. │ {
  340. CM.Glyphs.UnChecked
  341. } Check first out 你 │
  342. │ {
  343. CM.Glyphs.UnChecked
  344. } Check second out 你 │
  345. │ │
  346. └────────────────────────────┘
  347. ";
  348. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  349. Assert.Equal (new Rectangle (0, 0, 30, 6), pos);
  350. checkBox1.Checked = true;
  351. Assert.Equal (new Rectangle (1, 1, 25, 1), checkBox1.Frame);
  352. Assert.Equal (new Size (25, 1), checkBox1.TextFormatter.Size);
  353. checkBox2.Checked = true;
  354. Assert.Equal (new Rectangle (1, 2, 25, 1), checkBox2.Frame);
  355. Assert.Equal (new Size (25, 1), checkBox2.TextFormatter.Size);
  356. Application.Refresh ();
  357. expected = @$"
  358. ┌┤Test Demo 你├──────────────┐
  359. │ │
  360. │ {
  361. CM.Glyphs.Checked
  362. } Check first out 你 │
  363. │ {
  364. CM.Glyphs.Checked
  365. } Check second out 你 │
  366. │ │
  367. └────────────────────────────┘
  368. ";
  369. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  370. Assert.Equal (new Rectangle (0, 0, 30, 6), pos);
  371. }
  372. [Fact]
  373. [AutoInitShutdown]
  374. public void TextAlignment_Left ()
  375. {
  376. var checkBox = new CheckBox
  377. {
  378. X = 1,
  379. Y = Pos.Center (),
  380. Text = "Check this out 你",
  381. AutoSize = false,
  382. Width = 25
  383. };
  384. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill (), Title = "Test Demo 你" };
  385. win.Add (checkBox);
  386. Application.Top.Add (win);
  387. Application.Begin (Application.Top);
  388. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  389. Assert.Equal (TextAlignment.Left, checkBox.TextAlignment);
  390. Assert.Equal (new Rectangle (1, 1, 25, 1), checkBox.Frame);
  391. Assert.Equal (new Size (25, 1), checkBox.TextFormatter.Size);
  392. var expected = @$"
  393. ┌┤Test Demo 你├──────────────┐
  394. │ │
  395. │ {
  396. CM.Glyphs.UnChecked
  397. } Check this out 你 │
  398. │ │
  399. └────────────────────────────┘
  400. ";
  401. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  402. Assert.Equal (new Rectangle (0, 0, 30, 5), pos);
  403. checkBox.Checked = true;
  404. Application.Refresh ();
  405. expected = @$"
  406. ┌┤Test Demo 你├──────────────┐
  407. │ │
  408. │ {
  409. CM.Glyphs.Checked
  410. } Check this out 你 │
  411. │ │
  412. └────────────────────────────┘
  413. ";
  414. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  415. Assert.Equal (new Rectangle (0, 0, 30, 5), pos);
  416. }
  417. [Fact]
  418. [AutoInitShutdown]
  419. public void TextAlignment_Right ()
  420. {
  421. var checkBox = new CheckBox
  422. {
  423. X = 1,
  424. Y = Pos.Center (),
  425. Text = "Check this out 你",
  426. TextAlignment = TextAlignment.Right,
  427. AutoSize = false,
  428. Width = 25
  429. };
  430. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill (), Title = "Test Demo 你" };
  431. win.Add (checkBox);
  432. Application.Top.Add (win);
  433. Application.Begin (Application.Top);
  434. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  435. Assert.Equal (TextAlignment.Right, checkBox.TextAlignment);
  436. Assert.Equal (new Rectangle (1, 1, 25, 1), checkBox.Frame);
  437. Assert.Equal (new Size (25, 1), checkBox.TextFormatter.Size);
  438. Assert.False (checkBox.AutoSize);
  439. var expected = @$"
  440. ┌┤Test Demo 你├──────────────┐
  441. │ │
  442. │ Check this out 你 {
  443. CM.Glyphs.UnChecked
  444. } │
  445. │ │
  446. └────────────────────────────┘
  447. ";
  448. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  449. Assert.Equal (new Rectangle (0, 0, 30, 5), pos);
  450. checkBox.Checked = true;
  451. Application.Refresh ();
  452. expected = @$"
  453. ┌┤Test Demo 你├──────────────┐
  454. │ │
  455. │ Check this out 你 {
  456. CM.Glyphs.Checked
  457. } │
  458. │ │
  459. └────────────────────────────┘
  460. ";
  461. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  462. Assert.Equal (new Rectangle (0, 0, 30, 5), pos);
  463. }
  464. }