CheckboxTests.cs 15 KB

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