CheckBoxTests.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  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. //Application.RunMainLoopIteration (ref runstate, ref first);
  124. // BUGBUG - v2 - Autosize is busted; disabling tests for now
  125. // Assert.Equal (new Rect (1, 1, 19, 1), checkBox.Frame);
  126. // expected = @"
  127. //┌┤Test Demo 你├──────────────┐
  128. //│ │
  129. //│ √ Check this out 你 │
  130. //│ │
  131. //└────────────────────────────┘
  132. //";
  133. // 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.RunMainLoopIteration (ref runstate, ref first);
  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. //";
  148. // pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  149. // Assert.Equal (new Rect (0, 0, 30, 5), pos);
  150. // checkBox.AutoSize = true;
  151. // Application.RunMainLoopIteration (ref runstate, ref first);
  152. // Assert.Equal (new Rect (1, 1, 27, 1), checkBox.Frame);
  153. // expected = @"
  154. //┌┤Test Demo 你├──────────────┐
  155. //│ │
  156. //│ √ Check this out 你 changed│
  157. //│ │
  158. //└────────────────────────────┘
  159. //";
  160. // pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  161. // Assert.Equal (new Rect (0, 0, 30, 5), pos);
  162. }
  163. [Fact, AutoInitShutdown]
  164. public void TextAlignment_Left ()
  165. {
  166. var checkBox = new CheckBox () {
  167. X = 1,
  168. Y = Pos.Center (),
  169. Text = "Check this out 你",
  170. AutoSize = false,
  171. Width = 25
  172. };
  173. var win = new Window () {
  174. Width = Dim.Fill (),
  175. Height = Dim.Fill (),
  176. Title = "Test Demo 你"
  177. };
  178. win.Add (checkBox);
  179. Application.Top.Add (win);
  180. Application.Begin (Application.Top);
  181. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  182. Assert.Equal (TextAlignment.Left, checkBox.TextAlignment);
  183. Assert.Equal (new Rect (1, 1, 25, 1), checkBox.Frame);
  184. Assert.Equal (new Size (25, 1), checkBox.TextFormatter.Size);
  185. var expected = @$"
  186. ┌┤Test Demo 你├──────────────┐
  187. │ │
  188. │ {CM.Glyphs.UnChecked} Check this out 你 │
  189. │ │
  190. └────────────────────────────┘
  191. ";
  192. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  193. Assert.Equal (new Rect (0, 0, 30, 5), pos);
  194. checkBox.Checked = true;
  195. Application.Refresh ();
  196. expected = @$"
  197. ┌┤Test Demo 你├──────────────┐
  198. │ │
  199. │ {CM.Glyphs.Checked} Check this out 你 │
  200. │ │
  201. └────────────────────────────┘
  202. ";
  203. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  204. Assert.Equal (new Rect (0, 0, 30, 5), pos);
  205. }
  206. [Fact, AutoInitShutdown]
  207. public void TextAlignment_Centered ()
  208. {
  209. var checkBox = new CheckBox () {
  210. X = 1,
  211. Y = Pos.Center (),
  212. Text = "Check this out 你",
  213. TextAlignment = TextAlignment.Centered,
  214. AutoSize = false,
  215. Width = 25
  216. };
  217. var win = new Window () {
  218. Width = Dim.Fill (),
  219. Height = Dim.Fill (),
  220. Title = "Test Demo 你"
  221. };
  222. win.Add (checkBox);
  223. Application.Top.Add (win);
  224. Application.Begin (Application.Top);
  225. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  226. Assert.Equal (TextAlignment.Centered, checkBox.TextAlignment);
  227. Assert.Equal (new Rect (1, 1, 25, 1), checkBox.Frame);
  228. Assert.Equal (new Size (25, 1), checkBox.TextFormatter.Size);
  229. Assert.False (checkBox.AutoSize);
  230. var expected = @$"
  231. ┌┤Test Demo 你├──────────────┐
  232. │ │
  233. │ {CM.Glyphs.UnChecked} Check this out 你 │
  234. │ │
  235. └────────────────────────────┘
  236. ";
  237. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  238. Assert.Equal (new Rect (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 Rect (0, 0, 30, 5), pos);
  250. }
  251. [Fact, AutoInitShutdown]
  252. public void TextAlignment_Justified ()
  253. {
  254. var checkBox1 = new CheckBox () {
  255. X = 1,
  256. Y = Pos.Center (),
  257. Text = "Check first out 你",
  258. TextAlignment = TextAlignment.Justified,
  259. AutoSize = false,
  260. Width = 25
  261. };
  262. var checkBox2 = new CheckBox () {
  263. X = 1,
  264. Y = Pos.Bottom (checkBox1),
  265. Text = "Check second out 你",
  266. TextAlignment = TextAlignment.Justified,
  267. AutoSize = false,
  268. Width = 25
  269. };
  270. var win = new Window () {
  271. Width = Dim.Fill (),
  272. Height = Dim.Fill (),
  273. Title = "Test Demo 你"
  274. };
  275. win.Add (checkBox1, checkBox2);
  276. Application.Top.Add (win);
  277. Application.Begin (Application.Top);
  278. ((FakeDriver)Application.Driver).SetBufferSize (30, 6);
  279. Assert.Equal (TextAlignment.Justified, checkBox1.TextAlignment);
  280. Assert.Equal (new Rect (1, 1, 25, 1), checkBox1.Frame);
  281. Assert.Equal (new Size (25, 1), checkBox1.TextFormatter.Size);
  282. Assert.Equal (TextAlignment.Justified, checkBox2.TextAlignment);
  283. Assert.Equal (new Rect (1, 2, 25, 1), checkBox2.Frame);
  284. Assert.Equal (new Size (25, 1), checkBox2.TextFormatter.Size);
  285. var expected = @$"
  286. ┌┤Test Demo 你├──────────────┐
  287. │ │
  288. │ {CM.Glyphs.UnChecked} Check first out 你 │
  289. │ {CM.Glyphs.UnChecked} Check second out 你 │
  290. │ │
  291. └────────────────────────────┘
  292. ";
  293. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  294. Assert.Equal (new Rect (0, 0, 30, 6), pos);
  295. checkBox1.Checked = true;
  296. Assert.Equal (new Rect (1, 1, 25, 1), checkBox1.Frame);
  297. //Assert.Equal (new Size (25, 1), checkBox1.TextFormatter.Size);
  298. checkBox2.Checked = true;
  299. Assert.Equal (new Rect (1, 2, 25, 1), checkBox2.Frame);
  300. //Assert.Equal (new Size (25, 1), checkBox2.TextFormatter.Size);
  301. Application.Refresh ();
  302. expected = @$"
  303. ┌┤Test Demo 你├──────────────┐
  304. │ │
  305. │ {CM.Glyphs.Checked} Check first out 你 │
  306. │ {CM.Glyphs.Checked} Check second out 你 │
  307. │ │
  308. └────────────────────────────┘
  309. ";
  310. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  311. Assert.Equal (new Rect (0, 0, 30, 6), pos);
  312. }
  313. [Fact, AutoInitShutdown]
  314. public void TextAlignment_Right ()
  315. {
  316. var checkBox = new CheckBox () {
  317. X = 1,
  318. Y = Pos.Center (),
  319. Text = "Check this out 你",
  320. TextAlignment = TextAlignment.Right,
  321. AutoSize = false,
  322. Width = 25
  323. };
  324. var win = new Window () {
  325. Width = Dim.Fill (),
  326. Height = Dim.Fill (),
  327. Title = "Test Demo 你"
  328. };
  329. win.Add (checkBox);
  330. Application.Top.Add (win);
  331. Application.Begin (Application.Top);
  332. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  333. Assert.Equal (TextAlignment.Right, checkBox.TextAlignment);
  334. Assert.Equal (new Rect (1, 1, 25, 1), checkBox.Frame);
  335. Assert.Equal (new Size (25, 1), checkBox.TextFormatter.Size);
  336. Assert.False (checkBox.AutoSize);
  337. var expected = @$"
  338. ┌┤Test Demo 你├──────────────┐
  339. │ │
  340. │ Check this out 你 {CM.Glyphs.UnChecked} │
  341. │ │
  342. └────────────────────────────┘
  343. ";
  344. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  345. Assert.Equal (new Rect (0, 0, 30, 5), pos);
  346. checkBox.Checked = true;
  347. Application.Refresh ();
  348. expected = @$"
  349. ┌┤Test Demo 你├──────────────┐
  350. │ │
  351. │ Check this out 你 {CM.Glyphs.Checked} │
  352. │ │
  353. └────────────────────────────┘
  354. ";
  355. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  356. Assert.Equal (new Rect (0, 0, 30, 5), pos);
  357. }
  358. [Fact, AutoInitShutdown]
  359. public void AutoSize_Stays_True_AnchorEnd_Without_HotKeySpecifier ()
  360. {
  361. var checkBox = new CheckBox () {
  362. Y = Pos.Center (),
  363. Text = "Check this out 你"
  364. };
  365. checkBox.X = Pos.AnchorEnd () - Pos.Function (() => checkBox.GetSizeNeededForTextWithoutHotKey ().Width);
  366. var win = new Window () {
  367. Width = Dim.Fill (),
  368. Height = Dim.Fill (),
  369. Title = "Test Demo 你"
  370. };
  371. win.Add (checkBox);
  372. Application.Top.Add (win);
  373. Assert.True (checkBox.AutoSize);
  374. Application.Begin (Application.Top);
  375. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  376. var expected = @$"
  377. ┌┤Test Demo 你├──────────────┐
  378. │ │
  379. │ {CM.Glyphs.UnChecked} Check this out 你│
  380. │ │
  381. └────────────────────────────┘
  382. ";
  383. TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  384. Assert.True (checkBox.AutoSize);
  385. checkBox.Text = "Check this out 你 changed";
  386. Assert.True (checkBox.AutoSize);
  387. Application.Refresh ();
  388. expected = @$"
  389. ┌┤Test Demo 你├──────────────┐
  390. │ │
  391. │ {CM.Glyphs.UnChecked} Check this out 你 changed│
  392. │ │
  393. └────────────────────────────┘
  394. ";
  395. TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  396. }
  397. [Fact, AutoInitShutdown]
  398. public void AutoSize_Stays_True_AnchorEnd_With_HotKeySpecifier ()
  399. {
  400. var checkBox = new CheckBox () {
  401. Y = Pos.Center (),
  402. Text = "C_heck this out 你"
  403. };
  404. checkBox.X = Pos.AnchorEnd () - Pos.Function (() => checkBox.GetSizeNeededForTextWithoutHotKey ().Width);
  405. var win = new Window () {
  406. Width = Dim.Fill (),
  407. Height = Dim.Fill (),
  408. Title = "Test Demo 你"
  409. };
  410. win.Add (checkBox);
  411. Application.Top.Add (win);
  412. Assert.True (checkBox.AutoSize);
  413. Application.Begin (Application.Top);
  414. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  415. var expected = @$"
  416. ┌┤Test Demo 你├──────────────┐
  417. │ │
  418. │ {CM.Glyphs.UnChecked} Check this out 你│
  419. │ │
  420. └────────────────────────────┘
  421. ";
  422. TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  423. Assert.True (checkBox.AutoSize);
  424. checkBox.Text = "Check this out 你 changed";
  425. Assert.True (checkBox.AutoSize);
  426. Application.Refresh ();
  427. expected = @$"
  428. ┌┤Test Demo 你├──────────────┐
  429. │ │
  430. │ {CM.Glyphs.UnChecked} Check this out 你 changed│
  431. │ │
  432. └────────────────────────────┘
  433. ";
  434. TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  435. }
  436. [Fact, AutoInitShutdown]
  437. public void AllowNullChecked_Get_Set ()
  438. {
  439. var checkBox = new CheckBox ("Check this out 你");
  440. var top = Application.Top;
  441. top.Add (checkBox);
  442. Application.Begin (top);
  443. Assert.False (checkBox.Checked);
  444. Assert.True (checkBox.NewKeyDownEvent (new (KeyCode.Space)));
  445. Assert.True (checkBox.Checked);
  446. Assert.True (checkBox.MouseEvent (new MouseEvent () { X = 0, Y = 0, Flags = MouseFlags.Button1Clicked }));
  447. Assert.False (checkBox.Checked);
  448. checkBox.AllowNullChecked = true;
  449. Assert.True (checkBox.NewKeyDownEvent (new (KeyCode.Space)));
  450. Assert.Null (checkBox.Checked);
  451. Application.Refresh ();
  452. TestHelpers.AssertDriverContentsWithFrameAre (@$"
  453. {CM.Glyphs.NullChecked} Check this out 你", output);
  454. Assert.True (checkBox.MouseEvent (new MouseEvent () { X = 0, Y = 0, Flags = MouseFlags.Button1Clicked }));
  455. Assert.True (checkBox.Checked);
  456. Assert.True (checkBox.NewKeyDownEvent (new (KeyCode.Space)));
  457. Assert.False (checkBox.Checked);
  458. Assert.True (checkBox.MouseEvent (new MouseEvent () { X = 0, Y = 0, Flags = MouseFlags.Button1Clicked }));
  459. Assert.Null (checkBox.Checked);
  460. checkBox.AllowNullChecked = false;
  461. Assert.False (checkBox.Checked);
  462. }
  463. }
  464. }