CheckBoxTests.cs 19 KB

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