CheckboxTests.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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.Views {
  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 = GraphViewTests.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 = GraphViewTests.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. GraphViewTests.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 = GraphViewTests.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 = GraphViewTests.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 = GraphViewTests.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 = GraphViewTests.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 = GraphViewTests.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 = GraphViewTests.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 = GraphViewTests.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 = GraphViewTests.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 checkBox = new CheckBox () {
  277. X = 1,
  278. Y = Pos.Center (),
  279. Text = "Check this out 你",
  280. TextAlignment = TextAlignment.Justified,
  281. AutoSize = false,
  282. Width = 25
  283. };
  284. var win = new Window () {
  285. Width = Dim.Fill (),
  286. Height = Dim.Fill (),
  287. Title = "Test Demo 你"
  288. };
  289. win.Add (checkBox);
  290. Application.Top.Add (win);
  291. Application.Begin (Application.Top);
  292. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  293. Assert.Equal (TextAlignment.Justified, checkBox.TextAlignment);
  294. Assert.Equal (new Rect (1, 1, 25, 1), checkBox.Frame);
  295. Assert.Equal (new Size (25, 1), checkBox.TextFormatter.Size);
  296. Assert.Equal ("Check this out 你", checkBox.Text);
  297. Assert.Equal ("╴ Check this out 你", checkBox.TextFormatter.Text);
  298. Assert.False (checkBox.AutoSize);
  299. var expected = @"
  300. ┌ Test Demo 你 ──────────────┐
  301. │ │
  302. │ ╴ Check this out 你 │
  303. │ │
  304. └────────────────────────────┘
  305. ";
  306. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  307. Assert.Equal (new Rect (0, 0, 30, 5), pos);
  308. checkBox.Checked = true;
  309. Assert.Equal (new Rect (1, 1, 25, 1), checkBox.Frame);
  310. Assert.Equal (new Size (25, 1), checkBox.TextFormatter.Size);
  311. Application.Refresh ();
  312. expected = @"
  313. ┌ Test Demo 你 ──────────────┐
  314. │ │
  315. │ √ Check this out 你 │
  316. │ │
  317. └────────────────────────────┘
  318. ";
  319. pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  320. Assert.Equal (new Rect (0, 0, 30, 5), pos);
  321. }
  322. [Fact, AutoInitShutdown]
  323. public void TextAlignment_Right ()
  324. {
  325. var checkBox = new CheckBox () {
  326. X = 1,
  327. Y = Pos.Center (),
  328. Text = "Check this out 你",
  329. TextAlignment = TextAlignment.Right,
  330. AutoSize = false,
  331. Width = 25
  332. };
  333. var win = new Window () {
  334. Width = Dim.Fill (),
  335. Height = Dim.Fill (),
  336. Title = "Test Demo 你"
  337. };
  338. win.Add (checkBox);
  339. Application.Top.Add (win);
  340. Application.Begin (Application.Top);
  341. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  342. Assert.Equal (TextAlignment.Right, checkBox.TextAlignment);
  343. Assert.Equal (new Rect (1, 1, 25, 1), checkBox.Frame);
  344. Assert.Equal (new Size (25, 1), checkBox.TextFormatter.Size);
  345. Assert.Equal ("Check this out 你", checkBox.Text);
  346. Assert.Equal ("Check this out 你 ╴", checkBox.TextFormatter.Text);
  347. Assert.False (checkBox.AutoSize);
  348. var expected = @"
  349. ┌ Test Demo 你 ──────────────┐
  350. │ │
  351. │ Check this out 你 ╴ │
  352. │ │
  353. └────────────────────────────┘
  354. ";
  355. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  356. Assert.Equal (new Rect (0, 0, 30, 5), pos);
  357. checkBox.Checked = true;
  358. Application.Refresh ();
  359. expected = @"
  360. ┌ Test Demo 你 ──────────────┐
  361. │ │
  362. │ Check this out 你 √ │
  363. │ │
  364. └────────────────────────────┘
  365. ";
  366. pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  367. Assert.Equal (new Rect (0, 0, 30, 5), pos);
  368. }
  369. [Fact, AutoInitShutdown]
  370. public void AutoSize_Stays_True_AnchorEnd_Without_HotKeySpecifier ()
  371. {
  372. var checkBox = new CheckBox () {
  373. Y = Pos.Center (),
  374. Text = "Check this out 你"
  375. };
  376. checkBox.X = Pos.AnchorEnd () - Pos.Function (() => checkBox.GetTextFormatterBoundsSize ().Width);
  377. var win = new Window () {
  378. Width = Dim.Fill (),
  379. Height = Dim.Fill (),
  380. Title = "Test Demo 你"
  381. };
  382. win.Add (checkBox);
  383. Application.Top.Add (win);
  384. Assert.True (checkBox.AutoSize);
  385. Application.Begin (Application.Top);
  386. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  387. var expected = @"
  388. ┌ Test Demo 你 ──────────────┐
  389. │ │
  390. │ ╴ Check this out 你│
  391. │ │
  392. └────────────────────────────┘
  393. ";
  394. GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  395. Assert.True (checkBox.AutoSize);
  396. checkBox.Text = "Check this out 你 changed";
  397. Assert.True (checkBox.AutoSize);
  398. Application.Refresh ();
  399. expected = @"
  400. ┌ Test Demo 你 ──────────────┐
  401. │ │
  402. │ ╴ Check this out 你 changed│
  403. │ │
  404. └────────────────────────────┘
  405. ";
  406. GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  407. }
  408. [Fact, AutoInitShutdown]
  409. public void AutoSize_Stays_True_AnchorEnd_With_HotKeySpecifier ()
  410. {
  411. var checkBox = new CheckBox () {
  412. Y = Pos.Center (),
  413. Text = "C_heck this out 你"
  414. };
  415. checkBox.X = Pos.AnchorEnd () - Pos.Function (() => checkBox.GetTextFormatterBoundsSize ().Width);
  416. var win = new Window () {
  417. Width = Dim.Fill (),
  418. Height = Dim.Fill (),
  419. Title = "Test Demo 你"
  420. };
  421. win.Add (checkBox);
  422. Application.Top.Add (win);
  423. Assert.True (checkBox.AutoSize);
  424. Application.Begin (Application.Top);
  425. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  426. var expected = @"
  427. ┌ Test Demo 你 ──────────────┐
  428. │ │
  429. │ ╴ Check this out 你│
  430. │ │
  431. └────────────────────────────┘
  432. ";
  433. GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  434. Assert.True (checkBox.AutoSize);
  435. checkBox.Text = "Check this out 你 changed";
  436. Assert.True (checkBox.AutoSize);
  437. Application.Refresh ();
  438. expected = @"
  439. ┌ Test Demo 你 ──────────────┐
  440. │ │
  441. │ ╴ Check this out 你 changed│
  442. │ │
  443. └────────────────────────────┘
  444. ";
  445. GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  446. }
  447. }
  448. }