SplitContainerTests.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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_Horizontal ()
  60. {
  61. var splitContainer = Get11By3SplitContainer ();
  62. splitContainer.Orientation = Terminal.Gui.Graphs.Orientation.Horizontal;
  63. splitContainer.Redraw (splitContainer.Bounds);
  64. string looksLike =
  65. @"
  66. 11111111111
  67. ───────────
  68. 22222222222";
  69. TestHelpers.AssertDriverContentsAre (looksLike, output);
  70. // Keyboard movement on splitter should have no effect if it is not focused
  71. splitContainer.ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ()));
  72. splitContainer.SetNeedsDisplay ();
  73. splitContainer.Redraw (splitContainer.Bounds);
  74. TestHelpers.AssertDriverContentsAre (looksLike, output);
  75. }
  76. [Fact, AutoInitShutdown]
  77. public void TestSplitContainer_Vertical_Panel1MinSize_Absolute ()
  78. {
  79. var splitContainer = Get11By3SplitContainer ();
  80. splitContainer.EnsureFocus ();
  81. splitContainer.FocusFirst ();
  82. splitContainer.Panel1MinSize = 6;
  83. // distance is too small (below 6)
  84. splitContainer.SplitterDistance = 2;
  85. // Should bound the value to the minimum distance
  86. Assert.Equal(6,splitContainer.SplitterDistance);
  87. splitContainer.Redraw (splitContainer.Bounds);
  88. // so should ignore the 2 distance and stick to 6
  89. string looksLike =
  90. @"
  91. 111111│2222
  92. │ ";
  93. TestHelpers.AssertDriverContentsAre (looksLike, output);
  94. // Keyboard movement on splitter should have no effect because it
  95. // would take us below the minimum splitter size
  96. splitContainer.ProcessKey (new KeyEvent (Key.CursorLeft, new KeyModifiers ()));
  97. splitContainer.SetNeedsDisplay ();
  98. splitContainer.Redraw (splitContainer.Bounds);
  99. TestHelpers.AssertDriverContentsAre (looksLike, output);
  100. // but we can continue to move the splitter right if we want
  101. splitContainer.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ()));
  102. splitContainer.SetNeedsDisplay ();
  103. splitContainer.Redraw (splitContainer.Bounds);
  104. looksLike =
  105. @"
  106. 1111111│222
  107. │ ";
  108. TestHelpers.AssertDriverContentsAre (looksLike, output);
  109. }
  110. [Fact, AutoInitShutdown]
  111. public void TestSplitContainer_Horizontal_Focused ()
  112. {
  113. var splitContainer = Get11By3SplitContainer ();
  114. splitContainer.Orientation = Terminal.Gui.Graphs.Orientation.Horizontal;
  115. splitContainer.EnsureFocus();
  116. splitContainer.FocusFirst();
  117. splitContainer.Redraw (splitContainer.Bounds);
  118. string looksLike =
  119. @"
  120. 11111111111
  121. ─────◊─────
  122. 22222222222";
  123. TestHelpers.AssertDriverContentsAre (looksLike, output);
  124. // Now move splitter line down
  125. splitContainer.ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ()));
  126. splitContainer.Redraw (splitContainer.Bounds);
  127. looksLike =
  128. @"
  129. 11111111111
  130. ─────◊─────";
  131. TestHelpers.AssertDriverContentsAre (looksLike, output);
  132. // And 2 up
  133. splitContainer.ProcessKey (new KeyEvent (Key.CursorUp, new KeyModifiers ()));
  134. splitContainer.ProcessKey (new KeyEvent (Key.CursorUp, new KeyModifiers ()));
  135. splitContainer.Redraw (splitContainer.Bounds);
  136. looksLike =
  137. @"
  138. ─────◊─────
  139. 22222222222";
  140. TestHelpers.AssertDriverContentsAre (looksLike, output);
  141. }
  142. [Fact, AutoInitShutdown]
  143. public void TestSplitContainer_Horizontal_Panel1MinSize_Absolute ()
  144. {
  145. var splitContainer = Get11By3SplitContainer ();
  146. splitContainer.Orientation = Terminal.Gui.Graphs.Orientation.Horizontal;
  147. splitContainer.EnsureFocus ();
  148. splitContainer.FocusFirst ();
  149. splitContainer.Panel1MinSize = 1;
  150. // 0 should not be allowed because it brings us below minimum size of Panel1
  151. splitContainer.SplitterDistance = 0;
  152. Assert.Equal((Pos)1,splitContainer.SplitterDistance);
  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 (allowed
  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 up 2 (only 1 is allowed because of minimum size of 1 on panel1)
  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. 11111111111
  175. ─────◊─────
  176. 22222222222";
  177. TestHelpers.AssertDriverContentsAre (looksLike, output);
  178. }
  179. private SplitContainer Get11By3SplitContainer ()
  180. {
  181. var container = new SplitContainer () {
  182. Width = 11,
  183. Height = 3,
  184. };
  185. container.Panel1.Add (new Label (new string ('1', 100)));
  186. container.Panel2.Add (new Label (new string ('2', 100)));
  187. Application.Top.Add (container);
  188. container.ColorScheme = new ColorScheme ();
  189. container.LayoutSubviews ();
  190. container.BeginInit ();
  191. container.EndInit ();
  192. return container;
  193. }
  194. }
  195. }