CheckBoxTests.cs 17 KB

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