CheckBoxTests.cs 17 KB

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