CheckBoxTests.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. using System.ComponentModel;
  2. using UnitTests;
  3. using Xunit.Abstractions;
  4. // ReSharper disable AccessToModifiedClosure
  5. namespace UnitTests.ViewsTests;
  6. public class CheckBoxTests (ITestOutputHelper output)
  7. {
  8. private static readonly Size _size25x1 = new (25, 1);
  9. [Fact]
  10. public void Commands_Select ()
  11. {
  12. Application.Running = new ();
  13. View otherView = new () { CanFocus = true };
  14. var ckb = new CheckBox ();
  15. Application.Running.Add (ckb, otherView);
  16. Application.Running.SetFocus ();
  17. Assert.True (ckb.HasFocus);
  18. var checkedStateChangingCount = 0;
  19. ckb.CheckedStateChanging += (s, e) => checkedStateChangingCount++;
  20. var selectCount = 0;
  21. ckb.Selecting += (s, e) => selectCount++;
  22. var acceptCount = 0;
  23. ckb.Accepting += (s, e) => acceptCount++;
  24. Assert.Equal (CheckState.UnChecked, ckb.CheckedState);
  25. Assert.Equal (0, checkedStateChangingCount);
  26. Assert.Equal (0, selectCount);
  27. Assert.Equal (0, acceptCount);
  28. Assert.Equal (Key.Empty, ckb.HotKey);
  29. // Test while focused
  30. ckb.Text = "_Test";
  31. Assert.Equal (Key.T, ckb.HotKey);
  32. ckb.NewKeyDownEvent (Key.T);
  33. Assert.Equal (CheckState.Checked, ckb.CheckedState);
  34. Assert.Equal (1, checkedStateChangingCount);
  35. Assert.Equal (1, selectCount);
  36. Assert.Equal (0, acceptCount);
  37. ckb.Text = "T_est";
  38. Assert.Equal (Key.E, ckb.HotKey);
  39. ckb.NewKeyDownEvent (Key.E.WithAlt);
  40. Assert.Equal (2, checkedStateChangingCount);
  41. Assert.Equal (2, selectCount);
  42. Assert.Equal (0, acceptCount);
  43. ckb.NewKeyDownEvent (Key.Space);
  44. Assert.Equal (3, checkedStateChangingCount);
  45. Assert.Equal (3, selectCount);
  46. Assert.Equal (0, acceptCount);
  47. ckb.NewKeyDownEvent (Key.Enter);
  48. Assert.Equal (3, checkedStateChangingCount);
  49. Assert.Equal (3, selectCount);
  50. Assert.Equal (1, acceptCount);
  51. Application.Running.Dispose ();
  52. Application.ResetState ();
  53. }
  54. #region Mouse Tests
  55. #endregion Mouse Tests
  56. [Fact]
  57. [AutoInitShutdown]
  58. public void TextAlignment_Centered ()
  59. {
  60. var checkBox = new CheckBox
  61. {
  62. X = 1,
  63. Y = Pos.Center (),
  64. Text = "Check this out 你",
  65. TextAlignment = Alignment.Center,
  66. Width = 25
  67. };
  68. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill (), Title = "Test Demo 你" };
  69. win.Add (checkBox);
  70. var top = new Toplevel ();
  71. top.Add (win);
  72. Application.Begin (top);
  73. Application.Driver?.SetScreenSize (30, 5);
  74. Assert.Equal (Alignment.Center, checkBox.TextAlignment);
  75. Assert.Equal (new (1, 1, 25, 1), checkBox.Frame);
  76. Assert.Equal (_size25x1, checkBox.TextFormatter.ConstrainToSize);
  77. var expected = @$"
  78. ┌┤Test Demo 你├──────────────┐
  79. │ │
  80. │ {Glyphs.CheckStateUnChecked} Check this out 你 │
  81. │ │
  82. └────────────────────────────┘
  83. ";
  84. Rectangle pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  85. Assert.Equal (new (0, 0, 30, 5), pos);
  86. checkBox.CheckedState = CheckState.Checked;
  87. Application.LayoutAndDraw ();
  88. expected = @$"
  89. ┌┤Test Demo 你├──────────────┐
  90. │ │
  91. │ {Glyphs.CheckStateChecked} Check this out 你 │
  92. │ │
  93. └────────────────────────────┘
  94. ";
  95. pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  96. Assert.Equal (new (0, 0, 30, 5), pos);
  97. top.Dispose ();
  98. }
  99. [Fact]
  100. [AutoInitShutdown]
  101. public void TextAlignment_Justified ()
  102. {
  103. var checkBox1 = new CheckBox
  104. {
  105. X = 1,
  106. Y = Pos.Center (),
  107. Text = "Check first out 你",
  108. TextAlignment = Alignment.Fill,
  109. Width = 25
  110. };
  111. var checkBox2 = new CheckBox
  112. {
  113. X = 1,
  114. Y = Pos.Bottom (checkBox1),
  115. Text = "Check second out 你",
  116. TextAlignment = Alignment.Fill,
  117. Width = 25
  118. };
  119. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill (), Title = "Test Demo 你" };
  120. win.Add (checkBox1, checkBox2);
  121. var top = new Toplevel ();
  122. top.Add (win);
  123. SessionToken rs = Application.Begin (top);
  124. Application.Driver!.SetScreenSize (30, 6);
  125. Assert.Equal (Alignment.Fill, checkBox1.TextAlignment);
  126. Assert.Equal (new (1, 1, 25, 1), checkBox1.Frame);
  127. Assert.Equal (Alignment.Fill, checkBox2.TextAlignment);
  128. Assert.Equal (new (1, 2, 25, 1), checkBox2.Frame);
  129. var expected = @$"
  130. ┌┤Test Demo 你├──────────────┐
  131. │ │
  132. │ {Glyphs.CheckStateUnChecked} Check first out 你 │
  133. │ {Glyphs.CheckStateUnChecked} Check second out 你 │
  134. │ │
  135. └────────────────────────────┘
  136. ";
  137. Rectangle pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  138. Assert.Equal (new (0, 0, 30, 6), pos);
  139. checkBox1.CheckedState = CheckState.Checked;
  140. AutoInitShutdownAttribute.RunIteration ();
  141. Assert.Equal (new (1, 1, 25, 1), checkBox1.Frame);
  142. Assert.Equal (_size25x1, checkBox1.TextFormatter.ConstrainToSize);
  143. checkBox2.CheckedState = CheckState.Checked;
  144. AutoInitShutdownAttribute.RunIteration ();
  145. Assert.Equal (new (1, 2, 25, 1), checkBox2.Frame);
  146. Assert.Equal (_size25x1, checkBox2.TextFormatter.ConstrainToSize);
  147. AutoInitShutdownAttribute.RunIteration ();
  148. expected = @$"
  149. ┌┤Test Demo 你├──────────────┐
  150. │ │
  151. │ {Glyphs.CheckStateChecked} Check first out 你 │
  152. │ {Glyphs.CheckStateChecked} Check second out 你 │
  153. │ │
  154. └────────────────────────────┘
  155. ";
  156. pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  157. Assert.Equal (new (0, 0, 30, 6), pos);
  158. top.Dispose ();
  159. }
  160. [Fact]
  161. [AutoInitShutdown]
  162. public void TextAlignment_Left ()
  163. {
  164. var checkBox = new CheckBox
  165. {
  166. X = 1,
  167. Y = Pos.Center (),
  168. Text = "Check this out 你",
  169. Width = 25
  170. };
  171. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill (), Title = "Test Demo 你" };
  172. win.Add (checkBox);
  173. var top = new Toplevel ();
  174. top.Add (win);
  175. Application.Begin (top);
  176. Application.Driver!.SetScreenSize (30, 5);
  177. Assert.Equal (Alignment.Start, checkBox.TextAlignment);
  178. Assert.Equal (new (1, 1, 25, 1), checkBox.Frame);
  179. Assert.Equal (_size25x1, checkBox.TextFormatter.ConstrainToSize);
  180. var expected = @$"
  181. ┌┤Test Demo 你├──────────────┐
  182. │ │
  183. │ {Glyphs.CheckStateUnChecked} Check this out 你 │
  184. │ │
  185. └────────────────────────────┘
  186. ";
  187. Rectangle pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  188. Assert.Equal (new (0, 0, 30, 5), pos);
  189. checkBox.CheckedState = CheckState.Checked;
  190. AutoInitShutdownAttribute.RunIteration ();
  191. expected = @$"
  192. ┌┤Test Demo 你├──────────────┐
  193. │ │
  194. │ {Glyphs.CheckStateChecked} Check this out 你 │
  195. │ │
  196. └────────────────────────────┘
  197. ";
  198. pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  199. Assert.Equal (new (0, 0, 30, 5), pos);
  200. top.Dispose ();
  201. }
  202. [Fact]
  203. [AutoInitShutdown]
  204. public void TextAlignment_Right ()
  205. {
  206. var checkBox = new CheckBox
  207. {
  208. X = 1,
  209. Y = Pos.Center (),
  210. Text = "Check this out 你",
  211. TextAlignment = Alignment.End,
  212. Width = 25
  213. };
  214. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill (), Title = "Test Demo 你" };
  215. win.Add (checkBox);
  216. var top = new Toplevel ();
  217. top.Add (win);
  218. Application.Begin (top);
  219. Application.Driver!.SetScreenSize (30, 5);
  220. Assert.Equal (Alignment.End, checkBox.TextAlignment);
  221. Assert.Equal (new (1, 1, 25, 1), checkBox.Frame);
  222. Assert.Equal (_size25x1, checkBox.TextFormatter.ConstrainToSize);
  223. var expected = @$"
  224. ┌┤Test Demo 你├──────────────┐
  225. │ │
  226. │ Check this out 你 {Glyphs.CheckStateUnChecked} │
  227. │ │
  228. └────────────────────────────┘
  229. ";
  230. Rectangle pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  231. Assert.Equal (new (0, 0, 30, 5), pos);
  232. checkBox.CheckedState = CheckState.Checked;
  233. AutoInitShutdownAttribute.RunIteration ();
  234. expected = @$"
  235. ┌┤Test Demo 你├──────────────┐
  236. │ │
  237. │ Check this out 你 {Glyphs.CheckStateChecked} │
  238. │ │
  239. └────────────────────────────┘
  240. ";
  241. pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  242. Assert.Equal (new (0, 0, 30, 5), pos);
  243. top.Dispose ();
  244. }
  245. [Fact]
  246. public void HotKey_Command_Does_Not_Fire_Accept ()
  247. {
  248. var cb = new CheckBox ();
  249. var accepted = false;
  250. cb.Accepting += CheckBoxOnAccept;
  251. cb.InvokeCommand (Command.HotKey);
  252. Assert.False (accepted);
  253. return;
  254. void CheckBoxOnAccept (object sender, CommandEventArgs e) { accepted = true; }
  255. }
  256. [Theory]
  257. [InlineData (CheckState.Checked)]
  258. [InlineData (CheckState.UnChecked)]
  259. [InlineData (CheckState.None)]
  260. public void Activated_Handle_Event_Prevents_Change (CheckState initialState)
  261. {
  262. var ckb = new CheckBox { AllowCheckStateNone = true };
  263. var checkedInvoked = false;
  264. ckb.CheckedState = initialState;
  265. ckb.Selecting += OnActivating;
  266. Assert.Equal (initialState, ckb.CheckedState);
  267. bool? ret = ckb.InvokeCommand (Command.Select);
  268. Assert.True (ret);
  269. Assert.True (checkedInvoked);
  270. Assert.Equal (initialState, ckb.CheckedState);
  271. return;
  272. void OnActivating (object sender, CommandEventArgs e)
  273. {
  274. checkedInvoked = true;
  275. e.Handled = true;
  276. }
  277. }
  278. [Theory]
  279. [InlineData (CheckState.Checked)]
  280. [InlineData (CheckState.UnChecked)]
  281. [InlineData (CheckState.None)]
  282. public void CheckedStateChanging_Cancel_Event_Prevents_Change (CheckState initialState)
  283. {
  284. var ckb = new CheckBox { AllowCheckStateNone = true };
  285. var checkedInvoked = false;
  286. ckb.CheckedState = initialState;
  287. ckb.CheckedStateChanging += OnCheckedStateChanging;
  288. Assert.Equal (initialState, ckb.CheckedState);
  289. // AdvanceCheckState returns false if the state was changed, true if it was cancelled, null if it was not changed
  290. bool? ret = ckb.AdvanceCheckState ();
  291. Assert.True (ret);
  292. Assert.True (checkedInvoked);
  293. Assert.Equal (initialState, ckb.CheckedState);
  294. return;
  295. void OnCheckedStateChanging (object sender, ResultEventArgs<CheckState> e)
  296. {
  297. checkedInvoked = true;
  298. e.Handled = true;
  299. }
  300. }
  301. }