CheckBoxTests.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  1. using System.ComponentModel;
  2. using Xunit.Abstractions;
  3. namespace Terminal.Gui.ViewsTests;
  4. public class CheckBoxTests
  5. {
  6. private readonly ITestOutputHelper _output;
  7. private static readonly Size _size25x1 = new (25, 1);
  8. public CheckBoxTests (ITestOutputHelper output) { _output = output; }
  9. [Theory]
  10. [InlineData ("01234", 0, 0, 0, 0)]
  11. [InlineData ("01234", 1, 0, 1, 0)]
  12. [InlineData ("01234", 0, 1, 0, 1)]
  13. [InlineData ("01234", 1, 1, 1, 1)]
  14. [InlineData ("01234", 10, 1, 10, 1)]
  15. [InlineData ("01234", 10, 3, 10, 3)]
  16. [InlineData ("0_1234", 0, 0, 0, 0)]
  17. [InlineData ("0_1234", 1, 0, 1, 0)]
  18. [InlineData ("0_1234", 0, 1, 0, 1)]
  19. [InlineData ("0_1234", 1, 1, 1, 1)]
  20. [InlineData ("0_1234", 10, 1, 10, 1)]
  21. [InlineData ("0_12你", 10, 3, 10, 3)]
  22. [InlineData ("0_12你", 0, 0, 0, 0)]
  23. [InlineData ("0_12你", 1, 0, 1, 0)]
  24. [InlineData ("0_12你", 0, 1, 0, 1)]
  25. [InlineData ("0_12你", 1, 1, 1, 1)]
  26. [InlineData ("0_12你", 10, 1, 10, 1)]
  27. [InlineData ("0_12你", 10, 3, 10, 3)]
  28. public void CheckBox_AbsoluteSize_Text (string text, int width, int height, int expectedWidth, int expectedHeight)
  29. {
  30. var checkBox = new CheckBox
  31. {
  32. X = 0,
  33. Y = 0,
  34. Width = width,
  35. Height = height,
  36. Text = text
  37. };
  38. Assert.Equal (new Size (expectedWidth, expectedHeight), checkBox.Frame.Size);
  39. Assert.Equal (new Size (expectedWidth, expectedHeight), checkBox.Viewport.Size);
  40. Assert.Equal (new Size (expectedWidth, expectedHeight), checkBox.TextFormatter.Size);
  41. checkBox.Dispose ();
  42. }
  43. [Theory]
  44. [InlineData (0, 0, 0, 0)]
  45. [InlineData (1, 0, 1, 0)]
  46. [InlineData (0, 1, 0, 1)]
  47. [InlineData (1, 1, 1, 1)]
  48. [InlineData (10, 1, 10, 1)]
  49. [InlineData (10, 3, 10, 3)]
  50. public void CheckBox_AbsoluteSize_DefaultText (int width, int height, int expectedWidth, int expectedHeight)
  51. {
  52. var checkBox = new CheckBox
  53. {
  54. X = 0,
  55. Y = 0,
  56. Width = width,
  57. Height = height,
  58. };
  59. Assert.Equal (new Size (expectedWidth, expectedHeight), checkBox.Frame.Size);
  60. Assert.Equal (new Size (expectedWidth, expectedHeight), checkBox.Viewport.Size);
  61. Assert.Equal (new Size (expectedWidth, expectedHeight), checkBox.TextFormatter.Size);
  62. checkBox.Dispose ();
  63. }
  64. // Test that Title and Text are the same
  65. [Fact]
  66. public void Text_Mirrors_Title ()
  67. {
  68. var view = new CheckBox ();
  69. view.Title = "Hello";
  70. Assert.Equal ("Hello", view.Title);
  71. Assert.Equal ($"Hello", view.TitleTextFormatter.Text);
  72. Assert.Equal ("Hello", view.Text);
  73. Assert.Equal ($"{CM.Glyphs.UnChecked} Hello", view.TextFormatter.Text);
  74. }
  75. [Fact]
  76. public void Title_Mirrors_Text ()
  77. {
  78. var view = new CheckBox ();
  79. view.Text = "Hello";
  80. Assert.Equal ("Hello", view.Text);
  81. Assert.Equal ($"{CM.Glyphs.UnChecked} Hello", view.TextFormatter.Text);
  82. Assert.Equal ("Hello", view.Title);
  83. Assert.Equal ($"Hello", view.TitleTextFormatter.Text);
  84. }
  85. [Fact]
  86. [AutoInitShutdown]
  87. public void AllowNullChecked_Get_Set ()
  88. {
  89. var checkBox = new CheckBox { Text = "Check this out 你" };
  90. Toplevel top = new ();
  91. top.Add (checkBox);
  92. Application.Begin (top);
  93. Assert.False (checkBox.Checked);
  94. Assert.True (checkBox.NewKeyDownEvent (Key.Space));
  95. Assert.True (checkBox.Checked);
  96. Assert.True (checkBox.NewMouseEvent (new MouseEvent { X = 0, Y = 0, Flags = MouseFlags.Button1Clicked }));
  97. Assert.False (checkBox.Checked);
  98. checkBox.AllowNullChecked = true;
  99. Assert.True (checkBox.NewKeyDownEvent (Key.Space));
  100. Assert.Null (checkBox.Checked);
  101. Application.Refresh ();
  102. TestHelpers.AssertDriverContentsWithFrameAre (
  103. @$"
  104. {CM.Glyphs.NullChecked} Check this out 你",
  105. _output
  106. );
  107. Assert.True (checkBox.NewMouseEvent (new MouseEvent { X = 0, Y = 0, Flags = MouseFlags.Button1Clicked }));
  108. Assert.True (checkBox.Checked);
  109. Assert.True (checkBox.NewKeyDownEvent (Key.Space));
  110. Assert.False (checkBox.Checked);
  111. Assert.True (checkBox.NewMouseEvent (new MouseEvent { X = 0, Y = 0, Flags = MouseFlags.Button1Clicked }));
  112. Assert.Null (checkBox.Checked);
  113. checkBox.AllowNullChecked = false;
  114. Assert.False (checkBox.Checked);
  115. }
  116. // [Fact]
  117. // [AutoInitShutdown]
  118. // public void AutoSize_Stays_True_AnchorEnd_With_HotKeySpecifier ()
  119. // {
  120. // var checkBox = new CheckBox { Y = Pos.Center (), Text = "C_heck this out 你" };
  121. // checkBox.X = Pos.AnchorEnd (0) - Pos.Function (() => checkBox.GetSizeNeededForTextWithoutHotKey ().Width);
  122. // var win = new Window { Width = Dim.Fill (), Height = Dim.Fill (), Title = "Test Demo 你" };
  123. // win.Add (checkBox);
  124. // var top = new Toplevel ();
  125. // top.Add (win);
  126. // //Assert.True (checkBox.AutoSize);
  127. // Application.Begin (top);
  128. // ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  129. // var expected = @$"
  130. //┌┤Test Demo 你├──────────────┐
  131. //│ │
  132. //│ {CM.Glyphs.UnChecked} Check this out 你│
  133. //│ │
  134. //└────────────────────────────┘
  135. //";
  136. // TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  137. // //Assert.True (checkBox.AutoSize);
  138. // checkBox.Text = "Check this out 你 changed";
  139. //// Assert.True (checkBox.AutoSize);
  140. // Application.Refresh ();
  141. // expected = @$"
  142. //┌┤Test Demo 你├──────────────┐
  143. //│ │
  144. //│ {CM.Glyphs.UnChecked} Check this out 你 changed│
  145. //│ │
  146. //└────────────────────────────┘
  147. //";
  148. // TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  149. // }
  150. // [Fact]
  151. // [AutoInitShutdown]
  152. // public void AutoSize_Stays_True_AnchorEnd_Without_HotKeySpecifier ()
  153. // {
  154. // var checkBox = new CheckBox { Y = Pos.Center (), Text = "Check this out 你" };
  155. // checkBox.X = Pos.AnchorEnd (0) - Pos.Function (() => checkBox.GetSizeNeededForTextWithoutHotKey ().Width);
  156. // var win = new Window { Width = Dim.Fill (), Height = Dim.Fill (), Title = "Test Demo 你" };
  157. // win.Add (checkBox);
  158. // var top = new Toplevel ();
  159. // top.Add (win);
  160. //// Assert.True (checkBox.AutoSize);
  161. // Application.Begin (top);
  162. // ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  163. // var expected = @$"
  164. //┌┤Test Demo 你├──────────────┐
  165. //│ │
  166. //│ {CM.Glyphs.UnChecked} Check this out 你│
  167. //│ │
  168. //└────────────────────────────┘
  169. //";
  170. // TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  171. // // Assert.True (checkBox.AutoSize);
  172. // checkBox.Text = "Check this out 你 changed";
  173. //// Assert.True (checkBox.AutoSize);
  174. // Application.Refresh ();
  175. // expected = @$"
  176. //┌┤Test Demo 你├──────────────┐
  177. //│ │
  178. //│ {CM.Glyphs.UnChecked} Check this out 你 changed│
  179. //│ │
  180. //└────────────────────────────┘
  181. //";
  182. // TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  183. // }
  184. // [Fact]
  185. // [AutoInitShutdown]
  186. // public void AutoSize_StaysVisible ()
  187. // {
  188. // var checkBox = new CheckBox { X = 1, Y = Pos.Center (), Text = "Check this out 你" };
  189. // var win = new Window { Width = Dim.Fill (), Height = Dim.Fill (), Title = "Test Demo 你" };
  190. // win.Add (checkBox);
  191. // var top = new Toplevel ();
  192. // top.Add (win);
  193. // Assert.False (checkBox.IsInitialized);
  194. // RunState runstate = Application.Begin (top);
  195. // ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  196. // Assert.True (checkBox.IsInitialized);
  197. // Assert.Equal (new Rectangle (1, 1, 19, 1), checkBox.Frame);
  198. // Assert.Equal ("Check this out 你", checkBox.Text);
  199. // Assert.Equal ($"{CM.Glyphs.UnChecked} Check this out 你", checkBox.TextFormatter.Text);
  200. // Assert.True (checkBox.AutoSize);
  201. // Assert.Equal (19, checkBox.Frame.Width);
  202. // checkBox.Checked = true;
  203. // Assert.Equal ($"{CM.Glyphs.Checked} Check this out 你", checkBox.TextFormatter.Text);
  204. // checkBox.AutoSize = false;
  205. // // It isn't auto-size so the height is guaranteed by the SetMinWidthHeight
  206. // checkBox.Text = "Check this out 你 changed";
  207. // var firstIteration = false;
  208. // Application.RunIteration (ref runstate, ref firstIteration);
  209. // // BUGBUG - v2 - Autosize is busted; disabling tests for now
  210. // Assert.Equal (new Rectangle (1, 1, 19, 1), checkBox.Frame);
  211. // var expected = @"
  212. //┌┤Test Demo 你├──────────────┐
  213. //│ │
  214. //│ ☑ Check this out 你 │
  215. //│ │
  216. //└────────────────────────────┘";
  217. // Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  218. // Assert.Equal (new Rectangle (0, 0, 30, 5), pos);
  219. // checkBox.Width = 19;
  220. // // It isn't auto-size so the height is guaranteed by the SetMinWidthHeight
  221. // checkBox.Text = "Check this out 你 changed";
  222. // Application.RunIteration (ref runstate, ref firstIteration);
  223. // Assert.False (checkBox.AutoSize);
  224. // Assert.Equal (new Rectangle (1, 1, 19, 1), checkBox.Frame);
  225. // expected = @"
  226. //┌┤Test Demo 你├──────────────┐
  227. //│ │
  228. //│ ☑ Check this out 你 │
  229. //│ │
  230. //└────────────────────────────┘";
  231. // pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  232. // Assert.Equal (new Rectangle (0, 0, 30, 5), pos);
  233. // checkBox.AutoSize = true;
  234. // Application.RunIteration (ref runstate, ref firstIteration);
  235. // Assert.Equal (new Rectangle (1, 1, 27, 1), checkBox.Frame);
  236. // expected = @"
  237. //┌┤Test Demo 你├──────────────┐
  238. //│ │
  239. //│ ☑ Check this out 你 changed│
  240. //│ │
  241. //└────────────────────────────┘";
  242. // pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  243. // Assert.Equal (new Rectangle (0, 0, 30, 5), pos);
  244. // }
  245. [Fact]
  246. public void Constructors_Defaults ()
  247. {
  248. var ckb = new CheckBox ();
  249. Assert.True (ckb.Width is Dim.DimAuto);
  250. Assert.Equal (Dim.Sized (1), ckb.Height);
  251. Assert.False (ckb.Checked);
  252. Assert.False (ckb.AllowNullChecked);
  253. Assert.Equal (string.Empty, ckb.Text);
  254. Assert.Equal ($"{CM.Glyphs.UnChecked} ", ckb.TextFormatter.Text);
  255. Assert.True (ckb.CanFocus);
  256. Assert.Equal (new Rectangle (0, 0, 2, 1), ckb.Frame);
  257. ckb = new CheckBox { Text = "Test", Checked = true };
  258. Assert.True (ckb.Width is Dim.DimAuto);
  259. Assert.Equal (Dim.Sized (1), ckb.Height);
  260. Assert.True (ckb.Checked);
  261. Assert.False (ckb.AllowNullChecked);
  262. Assert.Equal ("Test", ckb.Text);
  263. Assert.Equal ($"{CM.Glyphs.Checked} Test", ckb.TextFormatter.Text);
  264. Assert.True (ckb.CanFocus);
  265. Assert.Equal (new Rectangle (0, 0, 6, 1), ckb.Frame);
  266. ckb = new CheckBox { Text = "Test", X = 1, Y = 2 };
  267. Assert.True (ckb.Width is Dim.DimAuto);
  268. Assert.Equal (Dim.Sized (1), ckb.Height);
  269. Assert.False (ckb.Checked);
  270. Assert.False (ckb.AllowNullChecked);
  271. Assert.Equal ("Test", ckb.Text);
  272. Assert.Equal ($"{CM.Glyphs.UnChecked} Test", ckb.TextFormatter.Text);
  273. Assert.True (ckb.CanFocus);
  274. Assert.Equal (new Rectangle (1, 2, 6, 1), ckb.Frame);
  275. ckb = new CheckBox { Text = "Test", X = 3, Y = 4, Checked = true };
  276. Assert.True (ckb.Width is Dim.DimAuto);
  277. Assert.Equal (Dim.Sized (1), ckb.Height);
  278. Assert.True (ckb.Checked);
  279. Assert.False (ckb.AllowNullChecked);
  280. Assert.Equal ("Test", ckb.Text);
  281. Assert.Equal ($"{CM.Glyphs.Checked} Test", ckb.TextFormatter.Text);
  282. Assert.True (ckb.CanFocus);
  283. Assert.Equal (new Rectangle (3, 4, 6, 1), ckb.Frame);
  284. }
  285. [Fact]
  286. public void KeyBindings_Command ()
  287. {
  288. var toggled = false;
  289. var ckb = new CheckBox ();
  290. ckb.Toggled += (s, e) => toggled = true;
  291. Assert.False (ckb.Checked);
  292. Assert.False (toggled);
  293. Assert.Equal (Key.Empty, ckb.HotKey);
  294. ckb.Text = "_Test";
  295. Assert.Equal (Key.T, ckb.HotKey);
  296. Assert.True (ckb.NewKeyDownEvent (Key.T));
  297. Assert.True (ckb.Checked);
  298. Assert.True (toggled);
  299. ckb.Text = "T_est";
  300. toggled = false;
  301. Assert.Equal (Key.E, ckb.HotKey);
  302. Assert.True (ckb.NewKeyDownEvent (Key.E.WithAlt));
  303. Assert.True (toggled);
  304. Assert.False (ckb.Checked);
  305. toggled = false;
  306. Assert.Equal (Key.E, ckb.HotKey);
  307. Assert.True (ckb.NewKeyDownEvent (Key.E));
  308. Assert.True (toggled);
  309. Assert.True (ckb.Checked);
  310. toggled = false;
  311. Assert.True (ckb.NewKeyDownEvent (Key.Space));
  312. Assert.True (toggled);
  313. Assert.False (ckb.Checked);
  314. toggled = false;
  315. Assert.True (ckb.NewKeyDownEvent (Key.Space));
  316. Assert.True (toggled);
  317. Assert.True (ckb.Checked);
  318. toggled = false;
  319. Assert.False (ckb.NewKeyDownEvent (Key.Enter));
  320. Assert.False (toggled);
  321. Assert.True (ckb.Checked);
  322. }
  323. [Fact]
  324. public void Accept_Cancel_Event_OnAccept_Returns_True ()
  325. {
  326. var ckb = new CheckBox ();
  327. var acceptInvoked = false;
  328. ckb.Accept += ViewOnAccept;
  329. var ret = ckb.InvokeCommand (Command.Accept);
  330. Assert.True (ret);
  331. Assert.True (acceptInvoked);
  332. return;
  333. void ViewOnAccept (object sender, CancelEventArgs e)
  334. {
  335. acceptInvoked = true;
  336. e.Cancel = true;
  337. }
  338. }
  339. [Fact]
  340. [AutoInitShutdown]
  341. public void TextAlignment_Centered ()
  342. {
  343. var checkBox = new CheckBox
  344. {
  345. X = 1,
  346. Y = Pos.Center (),
  347. Text = "Check this out 你",
  348. TextAlignment = TextAlignment.Centered,
  349. AutoSize = false,
  350. Width = 25
  351. };
  352. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill (), Title = "Test Demo 你" };
  353. win.Add (checkBox);
  354. var top = new Toplevel ();
  355. top.Add (win);
  356. Application.Begin (top);
  357. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  358. Assert.Equal (TextAlignment.Centered, checkBox.TextAlignment);
  359. Assert.Equal (new (1, 1, 25, 1), checkBox.Frame);
  360. Assert.Equal (_size25x1, checkBox.TextFormatter.Size);
  361. Assert.False (checkBox.AutoSize);
  362. var expected = @$"
  363. ┌┤Test Demo 你├──────────────┐
  364. │ │
  365. │ {CM.Glyphs.UnChecked} Check this out 你 │
  366. │ │
  367. └────────────────────────────┘
  368. ";
  369. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  370. Assert.Equal (new (0, 0, 30, 5), pos);
  371. checkBox.Checked = true;
  372. Application.Refresh ();
  373. expected = @$"
  374. ┌┤Test Demo 你├──────────────┐
  375. │ │
  376. │ {CM.Glyphs.Checked} Check this out 你 │
  377. │ │
  378. └────────────────────────────┘
  379. ";
  380. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  381. Assert.Equal (new (0, 0, 30, 5), pos);
  382. }
  383. [Fact]
  384. [AutoInitShutdown]
  385. public void TextAlignment_Justified ()
  386. {
  387. var checkBox1 = new CheckBox
  388. {
  389. X = 1,
  390. Y = Pos.Center (),
  391. Text = "Check first out 你",
  392. TextAlignment = TextAlignment.Justified,
  393. Width = 25
  394. };
  395. var checkBox2 = new CheckBox
  396. {
  397. X = 1,
  398. Y = Pos.Bottom (checkBox1),
  399. Text = "Check second out 你",
  400. TextAlignment = TextAlignment.Justified,
  401. Width = 25
  402. };
  403. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill (), Title = "Test Demo 你" };
  404. win.Add (checkBox1, checkBox2);
  405. var top = new Toplevel ();
  406. top.Add (win);
  407. Application.Begin (top);
  408. ((FakeDriver)Application.Driver).SetBufferSize (30, 6);
  409. Assert.Equal (TextAlignment.Justified, checkBox1.TextAlignment);
  410. Assert.Equal (new (1, 1, 25, 1), checkBox1.Frame);
  411. Assert.Equal (TextAlignment.Justified, checkBox2.TextAlignment);
  412. Assert.Equal (new (1, 2, 25, 1), checkBox2.Frame);
  413. var expected = @$"
  414. ┌┤Test Demo 你├──────────────┐
  415. │ │
  416. │ {CM.Glyphs.UnChecked} Check first out 你 │
  417. │ {CM.Glyphs.UnChecked} Check second out 你 │
  418. │ │
  419. └────────────────────────────┘
  420. ";
  421. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  422. Assert.Equal (new (0, 0, 30, 6), pos);
  423. checkBox1.Checked = true;
  424. Assert.Equal (new (1, 1, 25, 1), checkBox1.Frame);
  425. Assert.Equal (_size25x1, checkBox1.TextFormatter.Size);
  426. checkBox2.Checked = true;
  427. Assert.Equal (new (1, 2, 25, 1), checkBox2.Frame);
  428. Assert.Equal (_size25x1, checkBox2.TextFormatter.Size);
  429. Application.Refresh ();
  430. expected = @$"
  431. ┌┤Test Demo 你├──────────────┐
  432. │ │
  433. │ {CM.Glyphs.Checked} Check first out 你 │
  434. │ {CM.Glyphs.Checked} Check second out 你 │
  435. │ │
  436. └────────────────────────────┘
  437. ";
  438. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  439. Assert.Equal (new (0, 0, 30, 6), pos);
  440. }
  441. [Fact]
  442. [AutoInitShutdown]
  443. public void TextAlignment_Left ()
  444. {
  445. var checkBox = new CheckBox
  446. {
  447. X = 1,
  448. Y = Pos.Center (),
  449. Text = "Check this out 你",
  450. AutoSize = false,
  451. Width = 25
  452. };
  453. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill (), Title = "Test Demo 你" };
  454. win.Add (checkBox);
  455. var top = new Toplevel ();
  456. top.Add (win);
  457. Application.Begin (top);
  458. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  459. Assert.Equal (TextAlignment.Left, checkBox.TextAlignment);
  460. Assert.Equal (new (1, 1, 25, 1), checkBox.Frame);
  461. Assert.Equal (_size25x1, checkBox.TextFormatter.Size);
  462. var expected = @$"
  463. ┌┤Test Demo 你├──────────────┐
  464. │ │
  465. │ {CM.Glyphs.UnChecked} Check this out 你 │
  466. │ │
  467. └────────────────────────────┘
  468. ";
  469. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  470. Assert.Equal (new (0, 0, 30, 5), pos);
  471. checkBox.Checked = true;
  472. Application.Refresh ();
  473. expected = @$"
  474. ┌┤Test Demo 你├──────────────┐
  475. │ │
  476. │ {CM.Glyphs.Checked} Check this out 你 │
  477. │ │
  478. └────────────────────────────┘
  479. ";
  480. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  481. Assert.Equal (new (0, 0, 30, 5), pos);
  482. }
  483. [Fact]
  484. [AutoInitShutdown]
  485. public void TextAlignment_Right ()
  486. {
  487. var checkBox = new CheckBox
  488. {
  489. X = 1,
  490. Y = Pos.Center (),
  491. Text = "Check this out 你",
  492. TextAlignment = TextAlignment.Right,
  493. AutoSize = false,
  494. Width = 25
  495. };
  496. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill (), Title = "Test Demo 你" };
  497. win.Add (checkBox);
  498. var top = new Toplevel ();
  499. top.Add (win);
  500. Application.Begin (top);
  501. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  502. Assert.Equal (TextAlignment.Right, checkBox.TextAlignment);
  503. Assert.Equal (new (1, 1, 25, 1), checkBox.Frame);
  504. Assert.Equal (_size25x1, checkBox.TextFormatter.Size);
  505. Assert.False (checkBox.AutoSize);
  506. var expected = @$"
  507. ┌┤Test Demo 你├──────────────┐
  508. │ │
  509. │ Check this out 你 {CM.Glyphs.UnChecked} │
  510. │ │
  511. └────────────────────────────┘
  512. ";
  513. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  514. Assert.Equal (new (0, 0, 30, 5), pos);
  515. checkBox.Checked = true;
  516. Application.Refresh ();
  517. expected = @$"
  518. ┌┤Test Demo 你├──────────────┐
  519. │ │
  520. │ Check this out 你 {CM.Glyphs.Checked} │
  521. │ │
  522. └────────────────────────────┘
  523. ";
  524. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  525. Assert.Equal (new (0, 0, 30, 5), pos);
  526. }
  527. [Fact]
  528. public void HotKey_Command_Fires_Accept ()
  529. {
  530. var cb = new CheckBox ();
  531. var accepted = false;
  532. cb.Accept += CheckBoxOnAccept;
  533. cb.InvokeCommand (Command.HotKey);
  534. Assert.True (accepted);
  535. return;
  536. void CheckBoxOnAccept (object sender, CancelEventArgs e) { accepted = true; }
  537. }
  538. [Theory]
  539. [InlineData (true)]
  540. [InlineData (false)]
  541. [InlineData (null)]
  542. public void Toggled_Cancel_Event_Prevents_Toggle (bool? initialState)
  543. {
  544. var ckb = new CheckBox () { AllowNullChecked = true };
  545. var checkedInvoked = false;
  546. ckb.Toggled += CheckBoxToggled;
  547. ckb.Checked = initialState;
  548. Assert.Equal(initialState, ckb.Checked);
  549. var ret = ckb.OnToggled ();
  550. Assert.True (ret);
  551. Assert.True (checkedInvoked);
  552. Assert.Equal (initialState, ckb.Checked);
  553. return;
  554. void CheckBoxToggled (object sender, CancelEventArgs e)
  555. {
  556. checkedInvoked = true;
  557. e.Cancel = true;
  558. }
  559. }
  560. }