CheckBoxTests.cs 10 KB

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