CheckBoxTests.cs 17 KB

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