CheckBoxTests.cs 17 KB

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