CheckBoxTests.cs 19 KB

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