SplitContainerTests.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. using System;
  2. using Terminal.Gui;
  3. using Xunit;
  4. using Xunit.Abstractions;
  5. namespace UnitTests {
  6. public class SplitContainerTests {
  7. readonly ITestOutputHelper output;
  8. public SplitContainerTests (ITestOutputHelper output)
  9. {
  10. this.output = output;
  11. }
  12. [Fact, AutoInitShutdown]
  13. public void TestSplitContainer_Vertical ()
  14. {
  15. var splitContainer = Get11By3SplitContainer ();
  16. splitContainer.Redraw (splitContainer.Bounds);
  17. string looksLike =
  18. @"
  19. 11111│22222
  20. │ ";
  21. TestHelpers.AssertDriverContentsAre (looksLike, output);
  22. // Keyboard movement on splitter should have no effect if it is not focused
  23. splitContainer.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ()));
  24. splitContainer.SetNeedsDisplay ();
  25. splitContainer.Redraw (splitContainer.Bounds);
  26. TestHelpers.AssertDriverContentsAre (looksLike, output);
  27. }
  28. [Fact, AutoInitShutdown]
  29. public void TestSplitContainer_Vertical_Focused ()
  30. {
  31. var splitContainer = Get11By3SplitContainer ();
  32. splitContainer.EnsureFocus ();
  33. splitContainer.FocusFirst ();
  34. splitContainer.Redraw (splitContainer.Bounds);
  35. string looksLike =
  36. @"
  37. 11111│22222
  38. │ ";
  39. TestHelpers.AssertDriverContentsAre (looksLike, output);
  40. // Now while focused move the splitter 1 unit right
  41. splitContainer.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ()));
  42. splitContainer.Redraw (splitContainer.Bounds);
  43. looksLike =
  44. @"
  45. 111111│2222
  46. │ ";
  47. TestHelpers.AssertDriverContentsAre (looksLike, output);
  48. // and 2 to the left
  49. splitContainer.ProcessKey (new KeyEvent (Key.CursorLeft, new KeyModifiers ()));
  50. splitContainer.ProcessKey (new KeyEvent (Key.CursorLeft, new KeyModifiers ()));
  51. splitContainer.Redraw (splitContainer.Bounds);
  52. looksLike =
  53. @"
  54. 1111│222222
  55. │ ";
  56. TestHelpers.AssertDriverContentsAre (looksLike, output);
  57. }
  58. [Fact, AutoInitShutdown]
  59. public void TestSplitContainer_Vertical_Focused_50PercentSplit ()
  60. {
  61. var splitContainer = Get11By3SplitContainer ();
  62. splitContainer.EnsureFocus ();
  63. splitContainer.FocusFirst ();
  64. splitContainer.SplitterDistance = Pos.Percent (50);
  65. Assert.IsType<Pos.PosFactor> (splitContainer.SplitterDistance);
  66. splitContainer.Redraw (splitContainer.Bounds);
  67. string looksLike =
  68. @"
  69. 11111│22222
  70. │ ";
  71. TestHelpers.AssertDriverContentsAre (looksLike, output);
  72. // Now while focused move the splitter 1 unit right
  73. splitContainer.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ()));
  74. splitContainer.Redraw (splitContainer.Bounds);
  75. looksLike =
  76. @"
  77. 111111│2222
  78. │ ";
  79. TestHelpers.AssertDriverContentsAre (looksLike, output);
  80. // Even when moving the splitter location it should stay a Percentage based one
  81. Assert.IsType<Pos.PosFactor> (splitContainer.SplitterDistance);
  82. // and 2 to the left
  83. splitContainer.ProcessKey (new KeyEvent (Key.CursorLeft, new KeyModifiers ()));
  84. splitContainer.ProcessKey (new KeyEvent (Key.CursorLeft, new KeyModifiers ()));
  85. splitContainer.Redraw (splitContainer.Bounds);
  86. looksLike =
  87. @"
  88. 1111│222222
  89. │ ";
  90. TestHelpers.AssertDriverContentsAre (looksLike, output);
  91. // Even when moving the splitter location it should stay a Percentage based one
  92. Assert.IsType<Pos.PosFactor> (splitContainer.SplitterDistance);
  93. }
  94. [Fact, AutoInitShutdown]
  95. public void TestSplitContainer_Horizontal ()
  96. {
  97. var splitContainer = Get11By3SplitContainer ();
  98. splitContainer.Orientation = Terminal.Gui.Graphs.Orientation.Horizontal;
  99. splitContainer.Redraw (splitContainer.Bounds);
  100. string looksLike =
  101. @"
  102. 11111111111
  103. ───────────
  104. 22222222222";
  105. TestHelpers.AssertDriverContentsAre (looksLike, output);
  106. // Keyboard movement on splitter should have no effect if it is not focused
  107. splitContainer.ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ()));
  108. splitContainer.SetNeedsDisplay ();
  109. splitContainer.Redraw (splitContainer.Bounds);
  110. TestHelpers.AssertDriverContentsAre (looksLike, output);
  111. }
  112. [Fact, AutoInitShutdown]
  113. public void TestSplitContainer_Vertical_Panel1MinSize_Absolute ()
  114. {
  115. var splitContainer = Get11By3SplitContainer ();
  116. splitContainer.EnsureFocus ();
  117. splitContainer.FocusFirst ();
  118. splitContainer.Panel1MinSize = 6;
  119. // distance is too small (below 6)
  120. splitContainer.SplitterDistance = 2;
  121. // Should bound the value to the minimum distance
  122. Assert.Equal (6, splitContainer.SplitterDistance);
  123. splitContainer.Redraw (splitContainer.Bounds);
  124. // so should ignore the 2 distance and stick to 6
  125. string looksLike =
  126. @"
  127. 111111│2222
  128. │ ";
  129. TestHelpers.AssertDriverContentsAre (looksLike, output);
  130. // Keyboard movement on splitter should have no effect because it
  131. // would take us below the minimum splitter size
  132. splitContainer.ProcessKey (new KeyEvent (Key.CursorLeft, new KeyModifiers ()));
  133. splitContainer.SetNeedsDisplay ();
  134. splitContainer.Redraw (splitContainer.Bounds);
  135. TestHelpers.AssertDriverContentsAre (looksLike, output);
  136. // but we can continue to move the splitter right if we want
  137. splitContainer.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ()));
  138. splitContainer.SetNeedsDisplay ();
  139. splitContainer.Redraw (splitContainer.Bounds);
  140. looksLike =
  141. @"
  142. 1111111│222
  143. │ ";
  144. TestHelpers.AssertDriverContentsAre (looksLike, output);
  145. }
  146. [Fact, AutoInitShutdown]
  147. public void TestSplitContainer_Horizontal_Focused ()
  148. {
  149. var splitContainer = Get11By3SplitContainer ();
  150. splitContainer.Orientation = Terminal.Gui.Graphs.Orientation.Horizontal;
  151. splitContainer.EnsureFocus ();
  152. splitContainer.FocusFirst ();
  153. splitContainer.Redraw (splitContainer.Bounds);
  154. string looksLike =
  155. @"
  156. 11111111111
  157. ─────◊─────
  158. 22222222222";
  159. TestHelpers.AssertDriverContentsAre (looksLike, output);
  160. // Now move splitter line down
  161. splitContainer.ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ()));
  162. splitContainer.Redraw (splitContainer.Bounds);
  163. looksLike =
  164. @"
  165. 11111111111
  166. ─────◊─────";
  167. TestHelpers.AssertDriverContentsAre (looksLike, output);
  168. // And 2 up
  169. splitContainer.ProcessKey (new KeyEvent (Key.CursorUp, new KeyModifiers ()));
  170. splitContainer.ProcessKey (new KeyEvent (Key.CursorUp, new KeyModifiers ()));
  171. splitContainer.Redraw (splitContainer.Bounds);
  172. looksLike =
  173. @"
  174. ─────◊─────
  175. 22222222222";
  176. TestHelpers.AssertDriverContentsAre (looksLike, output);
  177. }
  178. [Fact, AutoInitShutdown]
  179. public void TestSplitContainer_Horizontal_Panel1MinSize_Absolute ()
  180. {
  181. var splitContainer = Get11By3SplitContainer ();
  182. splitContainer.Orientation = Terminal.Gui.Graphs.Orientation.Horizontal;
  183. splitContainer.EnsureFocus ();
  184. splitContainer.FocusFirst ();
  185. splitContainer.Panel1MinSize = 1;
  186. // 0 should not be allowed because it brings us below minimum size of Panel1
  187. splitContainer.SplitterDistance = 0;
  188. Assert.Equal ((Pos)1, splitContainer.SplitterDistance);
  189. splitContainer.Redraw (splitContainer.Bounds);
  190. string looksLike =
  191. @"
  192. 11111111111
  193. ─────◊─────
  194. 22222222222";
  195. TestHelpers.AssertDriverContentsAre (looksLike, output);
  196. // Now move splitter line down (allowed
  197. splitContainer.ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ()));
  198. splitContainer.Redraw (splitContainer.Bounds);
  199. looksLike =
  200. @"
  201. 11111111111
  202. ─────◊─────";
  203. TestHelpers.AssertDriverContentsAre (looksLike, output);
  204. // And up 2 (only 1 is allowed because of minimum size of 1 on panel1)
  205. splitContainer.ProcessKey (new KeyEvent (Key.CursorUp, new KeyModifiers ()));
  206. splitContainer.ProcessKey (new KeyEvent (Key.CursorUp, new KeyModifiers ()));
  207. splitContainer.Redraw (splitContainer.Bounds);
  208. looksLike =
  209. @"
  210. 11111111111
  211. ─────◊─────
  212. 22222222222";
  213. TestHelpers.AssertDriverContentsAre (looksLike, output);
  214. }
  215. [Fact, AutoInitShutdown]
  216. public void TestSplitContainer_CannotSetSplitterPosToFuncEtc ()
  217. {
  218. var splitContainer = Get11By3SplitContainer ();
  219. var ex = Assert.Throws<ArgumentException> (() => splitContainer.SplitterDistance = Pos.Right (splitContainer));
  220. Assert.Equal ("Only Percent and Absolute values are supported for SplitterDistance property. Passed value was PosCombine", ex.Message);
  221. ex = Assert.Throws<ArgumentException> (() => splitContainer.SplitterDistance = Pos.Function (() => 1));
  222. Assert.Equal ("Only Percent and Absolute values are supported for SplitterDistance property. Passed value was PosFunc", ex.Message);
  223. // Also not allowed because this results in a PosCombine
  224. ex = Assert.Throws<ArgumentException> (() => splitContainer.SplitterDistance = Pos.Percent (50) - 1);
  225. Assert.Equal ("Only Percent and Absolute values are supported for SplitterDistance property. Passed value was PosCombine", ex.Message);
  226. }
  227. private SplitContainer Get11By3SplitContainer ()
  228. {
  229. var container = new SplitContainer () {
  230. Width = 11,
  231. Height = 3,
  232. };
  233. container.Panel1.Add (new Label (new string ('1', 100)));
  234. container.Panel2.Add (new Label (new string ('2', 100)));
  235. Application.Top.Add (container);
  236. container.ColorScheme = new ColorScheme ();
  237. container.LayoutSubviews ();
  238. container.BeginInit ();
  239. container.EndInit ();
  240. return container;
  241. }
  242. }
  243. }