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