CheckboxTests.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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. Width = 25
  153. };
  154. var win = new Window () {
  155. Width = Dim.Fill (),
  156. Height = Dim.Fill (),
  157. Title = "Test Demo 你"
  158. };
  159. win.Add (checkBox);
  160. Application.Top.Add (win);
  161. Application.Begin (Application.Top);
  162. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  163. Assert.Equal (TextAlignment.Left, checkBox.TextAlignment);
  164. Assert.Equal (new Rect (1, 1, 25, 1), checkBox.Frame);
  165. Assert.Equal ("Check this out 你", checkBox.Text);
  166. Assert.Equal ("╴ Check this out 你", checkBox.TextFormatter.Text);
  167. Assert.False (checkBox.AutoSize);
  168. var expected = @"
  169. ┌ Test Demo 你 ──────────────┐
  170. │ │
  171. │ ╴ Check this out 你 │
  172. │ │
  173. └────────────────────────────┘
  174. ";
  175. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  176. Assert.Equal (new Rect (0, 0, 30, 5), pos);
  177. checkBox.Checked = true;
  178. Application.Refresh ();
  179. expected = @"
  180. ┌ Test Demo 你 ──────────────┐
  181. │ │
  182. │ √ Check this out 你 │
  183. │ │
  184. └────────────────────────────┘
  185. ";
  186. pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  187. Assert.Equal (new Rect (0, 0, 30, 5), pos);
  188. }
  189. [Fact, AutoInitShutdown]
  190. public void TextAlignment_Centered ()
  191. {
  192. var checkBox = new CheckBox () {
  193. X = 1,
  194. Y = Pos.Center (),
  195. Text = "Check this out 你",
  196. Width = 25,
  197. TextAlignment = TextAlignment.Centered
  198. };
  199. var win = new Window () {
  200. Width = Dim.Fill (),
  201. Height = Dim.Fill (),
  202. Title = "Test Demo 你"
  203. };
  204. win.Add (checkBox);
  205. Application.Top.Add (win);
  206. Application.Begin (Application.Top);
  207. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  208. Assert.Equal (TextAlignment.Centered, checkBox.TextAlignment);
  209. Assert.Equal (new Rect (1, 1, 25, 1), checkBox.Frame);
  210. Assert.Equal ("Check this out 你", checkBox.Text);
  211. Assert.Equal ("╴ Check this out 你", checkBox.TextFormatter.Text);
  212. Assert.False (checkBox.AutoSize);
  213. var expected = @"
  214. ┌ Test Demo 你 ──────────────┐
  215. │ │
  216. │ ╴ Check this out 你 │
  217. │ │
  218. └────────────────────────────┘
  219. ";
  220. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  221. Assert.Equal (new Rect (0, 0, 30, 5), pos);
  222. checkBox.Checked = true;
  223. Application.Refresh ();
  224. expected = @"
  225. ┌ Test Demo 你 ──────────────┐
  226. │ │
  227. │ √ Check this out 你 │
  228. │ │
  229. └────────────────────────────┘
  230. ";
  231. pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  232. Assert.Equal (new Rect (0, 0, 30, 5), pos);
  233. }
  234. [Fact, AutoInitShutdown]
  235. public void TextAlignment_Justified ()
  236. {
  237. var checkBox = new CheckBox () {
  238. X = 1,
  239. Y = Pos.Center (),
  240. Text = "Check this out 你",
  241. Width = 25,
  242. TextAlignment = TextAlignment.Justified
  243. };
  244. var win = new Window () {
  245. Width = Dim.Fill (),
  246. Height = Dim.Fill (),
  247. Title = "Test Demo 你"
  248. };
  249. win.Add (checkBox);
  250. Application.Top.Add (win);
  251. Application.Begin (Application.Top);
  252. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  253. Assert.Equal (TextAlignment.Justified, checkBox.TextAlignment);
  254. Assert.Equal (new Rect (1, 1, 25, 1), checkBox.Frame);
  255. Assert.Equal ("Check this out 你", checkBox.Text);
  256. Assert.Equal ("╴ Check this out 你", checkBox.TextFormatter.Text);
  257. Assert.False (checkBox.AutoSize);
  258. var expected = @"
  259. ┌ Test Demo 你 ──────────────┐
  260. │ │
  261. │ ╴ Check this out 你 │
  262. │ │
  263. └────────────────────────────┘
  264. ";
  265. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  266. Assert.Equal (new Rect (0, 0, 30, 5), pos);
  267. checkBox.Checked = true;
  268. Application.Refresh ();
  269. expected = @"
  270. ┌ Test Demo 你 ──────────────┐
  271. │ │
  272. │ √ Check this out 你 │
  273. │ │
  274. └────────────────────────────┘
  275. ";
  276. pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  277. Assert.Equal (new Rect (0, 0, 30, 5), pos);
  278. }
  279. [Fact, AutoInitShutdown]
  280. public void TextAlignment_Right ()
  281. {
  282. var checkBox = new CheckBox () {
  283. X = 1,
  284. Y = Pos.Center (),
  285. Text = "Check this out 你",
  286. Width = 25,
  287. TextAlignment = TextAlignment.Right
  288. };
  289. var win = new Window () {
  290. Width = Dim.Fill (),
  291. Height = Dim.Fill (),
  292. Title = "Test Demo 你"
  293. };
  294. win.Add (checkBox);
  295. Application.Top.Add (win);
  296. Application.Begin (Application.Top);
  297. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  298. Assert.Equal (TextAlignment.Right, checkBox.TextAlignment);
  299. Assert.Equal (new Rect (1, 1, 25, 1), checkBox.Frame);
  300. Assert.Equal ("Check this out 你", checkBox.Text);
  301. Assert.Equal ("Check this out 你 ╴", checkBox.TextFormatter.Text);
  302. Assert.False (checkBox.AutoSize);
  303. var expected = @"
  304. ┌ Test Demo 你 ──────────────┐
  305. │ │
  306. │ Check this out 你 ╴ │
  307. │ │
  308. └────────────────────────────┘
  309. ";
  310. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  311. Assert.Equal (new Rect (0, 0, 30, 5), pos);
  312. checkBox.Checked = true;
  313. Application.Refresh ();
  314. expected = @"
  315. ┌ Test Demo 你 ──────────────┐
  316. │ │
  317. │ Check this out 你 √ │
  318. │ │
  319. └────────────────────────────┘
  320. ";
  321. pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  322. Assert.Equal (new Rect (0, 0, 30, 5), pos);
  323. }
  324. [Fact, AutoInitShutdown]
  325. public void AutoSize_Stays_True_AnchorEnd_Without_HotKeySpecifier ()
  326. {
  327. var checkBox = new CheckBox () {
  328. Y = Pos.Center (),
  329. Text = "Check this out 你"
  330. };
  331. checkBox.X = Pos.AnchorEnd () - Pos.Function (() => checkBox.GetTextFormatterBoundsSize ().Width);
  332. var win = new Window () {
  333. Width = Dim.Fill (),
  334. Height = Dim.Fill (),
  335. Title = "Test Demo 你"
  336. };
  337. win.Add (checkBox);
  338. Application.Top.Add (win);
  339. Assert.True (checkBox.AutoSize);
  340. Application.Begin (Application.Top);
  341. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  342. var expected = @"
  343. ┌ Test Demo 你 ──────────────┐
  344. │ │
  345. │ ╴ Check this out 你│
  346. │ │
  347. └────────────────────────────┘
  348. ";
  349. GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  350. Assert.True (checkBox.AutoSize);
  351. checkBox.Text = "Check this out 你 changed";
  352. Assert.True (checkBox.AutoSize);
  353. Application.Refresh ();
  354. expected = @"
  355. ┌ Test Demo 你 ──────────────┐
  356. │ │
  357. │ ╴ Check this out 你 changed│
  358. │ │
  359. └────────────────────────────┘
  360. ";
  361. GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  362. }
  363. [Fact, AutoInitShutdown]
  364. public void AutoSize_Stays_True_AnchorEnd_With_HotKeySpecifier ()
  365. {
  366. var checkBox = new CheckBox () {
  367. Y = Pos.Center (),
  368. Text = "C_heck this out 你"
  369. };
  370. checkBox.X = Pos.AnchorEnd () - Pos.Function (() => checkBox.GetTextFormatterBoundsSize ().Width);
  371. var win = new Window () {
  372. Width = Dim.Fill (),
  373. Height = Dim.Fill (),
  374. Title = "Test Demo 你"
  375. };
  376. win.Add (checkBox);
  377. Application.Top.Add (win);
  378. Assert.True (checkBox.AutoSize);
  379. Application.Begin (Application.Top);
  380. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  381. var expected = @"
  382. ┌ Test Demo 你 ──────────────┐
  383. │ │
  384. │ ╴ Check this out 你│
  385. │ │
  386. └────────────────────────────┘
  387. ";
  388. GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  389. Assert.True (checkBox.AutoSize);
  390. checkBox.Text = "Check this out 你 changed";
  391. Assert.True (checkBox.AutoSize);
  392. Application.Refresh ();
  393. expected = @"
  394. ┌ Test Demo 你 ──────────────┐
  395. │ │
  396. │ ╴ Check this out 你 changed│
  397. │ │
  398. └────────────────────────────┘
  399. ";
  400. GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  401. }
  402. }
  403. }