ScrollViewTests.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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 ScrollViewTests {
  10. readonly ITestOutputHelper output;
  11. public ScrollViewTests (ITestOutputHelper output)
  12. {
  13. this.output = output;
  14. }
  15. [Fact]
  16. public void Constructors_Defaults ()
  17. {
  18. var sv = new ScrollView ();
  19. Assert.Equal (LayoutStyle.Computed, sv.LayoutStyle);
  20. Assert.True (sv.CanFocus);
  21. Assert.Equal (new Rect (0, 0, 0, 0), sv.Frame);
  22. Assert.Equal (Rect.Empty, sv.Frame);
  23. Assert.Null (sv.X);
  24. Assert.Null (sv.Y);
  25. Assert.Null (sv.Width);
  26. Assert.Null (sv.Height);
  27. Assert.Equal (Point.Empty, sv.ContentOffset);
  28. Assert.Equal (Size.Empty, sv.ContentSize);
  29. Assert.True (sv.AutoHideScrollBars);
  30. Assert.True (sv.KeepContentAlwaysInViewport);
  31. sv = new ScrollView (new Rect (1, 2, 20, 10));
  32. Assert.Equal (LayoutStyle.Absolute, sv.LayoutStyle);
  33. Assert.True (sv.CanFocus);
  34. Assert.Equal (new Rect (1, 2, 20, 10), sv.Frame);
  35. Assert.Null (sv.X);
  36. Assert.Null (sv.Y);
  37. Assert.Null (sv.Width);
  38. Assert.Null (sv.Height);
  39. Assert.Equal (Point.Empty, sv.ContentOffset);
  40. Assert.Equal (Size.Empty, sv.ContentSize);
  41. Assert.True (sv.AutoHideScrollBars);
  42. Assert.True (sv.KeepContentAlwaysInViewport);
  43. }
  44. [Fact]
  45. public void Adding_Views ()
  46. {
  47. var sv = new ScrollView (new Rect (0, 0, 20, 10)) {
  48. ContentSize = new Size (30, 20)
  49. };
  50. sv.Add (new View () { Width = 10, Height = 5 },
  51. new View () { X = 12, Y = 7, Width = 10, Height = 5 });
  52. Assert.Equal (new Size (30, 20), sv.ContentSize);
  53. Assert.Equal (2, sv.Subviews [0].Subviews.Count);
  54. }
  55. [Fact]
  56. public void KeyBindings_Command ()
  57. {
  58. var sv = new ScrollView (new Rect (0, 0, 20, 10)) {
  59. ContentSize = new Size (40, 20)
  60. };
  61. sv.Add (new View () { Width = 20, Height = 5 },
  62. new View () { X = 22, Y = 7, Width = 10, Height = 5 });
  63. Assert.True (sv.KeepContentAlwaysInViewport);
  64. Assert.True (sv.AutoHideScrollBars);
  65. Assert.Equal (new Point (0, 0), sv.ContentOffset);
  66. Assert.False (sv.ProcessKey (new KeyEvent (Key.CursorUp, new KeyModifiers ())));
  67. Assert.Equal (new Point (0, 0), sv.ContentOffset);
  68. Assert.True (sv.ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ())));
  69. Assert.Equal (new Point (0, -1), sv.ContentOffset);
  70. Assert.True (sv.ProcessKey (new KeyEvent (Key.CursorUp, new KeyModifiers ())));
  71. Assert.Equal (new Point (0, 0), sv.ContentOffset);
  72. Assert.False (sv.ProcessKey (new KeyEvent (Key.PageUp, new KeyModifiers ())));
  73. Assert.Equal (new Point (0, 0), sv.ContentOffset);
  74. Assert.True (sv.ProcessKey (new KeyEvent (Key.PageDown, new KeyModifiers ())));
  75. Assert.Equal (new Point (0, -10), sv.ContentOffset);
  76. Assert.False (sv.ProcessKey (new KeyEvent (Key.PageDown, new KeyModifiers ())));
  77. Assert.Equal (new Point (0, -10), sv.ContentOffset);
  78. Assert.False (sv.ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ())));
  79. Assert.Equal (new Point (0, -10), sv.ContentOffset);
  80. Assert.True (sv.ProcessKey (new KeyEvent ((Key)'v' | Key.AltMask, new KeyModifiers ())));
  81. Assert.Equal (new Point (0, 0), sv.ContentOffset);
  82. Assert.True (sv.ProcessKey (new KeyEvent (Key.V | Key.CtrlMask, new KeyModifiers ())));
  83. Assert.Equal (new Point (0, -10), sv.ContentOffset);
  84. Assert.False (sv.ProcessKey (new KeyEvent (Key.CursorLeft, new KeyModifiers ())));
  85. Assert.Equal (new Point (0, -10), sv.ContentOffset);
  86. Assert.True (sv.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ())));
  87. Assert.Equal (new Point (-1, -10), sv.ContentOffset);
  88. Assert.True (sv.ProcessKey (new KeyEvent (Key.CursorLeft, new KeyModifiers ())));
  89. Assert.Equal (new Point (0, -10), sv.ContentOffset);
  90. Assert.False (sv.ProcessKey (new KeyEvent (Key.PageUp | Key.CtrlMask, new KeyModifiers ())));
  91. Assert.Equal (new Point (0, -10), sv.ContentOffset);
  92. Assert.True (sv.ProcessKey (new KeyEvent (Key.PageDown | Key.CtrlMask, new KeyModifiers ())));
  93. Assert.Equal (new Point (-20, -10), sv.ContentOffset);
  94. Assert.False (sv.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ())));
  95. Assert.Equal (new Point (-20, -10), sv.ContentOffset);
  96. Assert.True (sv.ProcessKey (new KeyEvent (Key.Home, new KeyModifiers ())));
  97. Assert.Equal (new Point (-20, 0), sv.ContentOffset);
  98. Assert.False (sv.ProcessKey (new KeyEvent (Key.Home, new KeyModifiers ())));
  99. Assert.Equal (new Point (-20, 0), sv.ContentOffset);
  100. Assert.True (sv.ProcessKey (new KeyEvent (Key.End, new KeyModifiers ())));
  101. Assert.Equal (new Point (-20, -10), sv.ContentOffset);
  102. Assert.False (sv.ProcessKey (new KeyEvent (Key.End, new KeyModifiers ())));
  103. Assert.Equal (new Point (-20, -10), sv.ContentOffset);
  104. Assert.True (sv.ProcessKey (new KeyEvent (Key.Home | Key.CtrlMask, new KeyModifiers ())));
  105. Assert.Equal (new Point (0, -10), sv.ContentOffset);
  106. Assert.False (sv.ProcessKey (new KeyEvent (Key.Home | Key.CtrlMask, new KeyModifiers ())));
  107. Assert.Equal (new Point (0, -10), sv.ContentOffset);
  108. Assert.True (sv.ProcessKey (new KeyEvent (Key.End | Key.CtrlMask, new KeyModifiers ())));
  109. Assert.Equal (new Point (-20, -10), sv.ContentOffset);
  110. Assert.False (sv.ProcessKey (new KeyEvent (Key.End | Key.CtrlMask, new KeyModifiers ())));
  111. Assert.Equal (new Point (-20, -10), sv.ContentOffset);
  112. Assert.True (sv.ProcessKey (new KeyEvent (Key.Home, new KeyModifiers ())));
  113. Assert.Equal (new Point (-20, 0), sv.ContentOffset);
  114. Assert.True (sv.ProcessKey (new KeyEvent (Key.Home | Key.CtrlMask, new KeyModifiers ())));
  115. Assert.Equal (new Point (0, 0), sv.ContentOffset);
  116. sv.KeepContentAlwaysInViewport = false;
  117. Assert.False (sv.KeepContentAlwaysInViewport);
  118. Assert.True (sv.AutoHideScrollBars);
  119. Assert.Equal (new Point (0, 0), sv.ContentOffset);
  120. Assert.False (sv.ProcessKey (new KeyEvent (Key.CursorUp, new KeyModifiers ())));
  121. Assert.Equal (new Point (0, 0), sv.ContentOffset);
  122. Assert.True (sv.ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ())));
  123. Assert.Equal (new Point (0, -1), sv.ContentOffset);
  124. Assert.True (sv.ProcessKey (new KeyEvent (Key.CursorUp, new KeyModifiers ())));
  125. Assert.Equal (new Point (0, 0), sv.ContentOffset);
  126. Assert.False (sv.ProcessKey (new KeyEvent (Key.PageUp, new KeyModifiers ())));
  127. Assert.Equal (new Point (0, 0), sv.ContentOffset);
  128. Assert.True (sv.ProcessKey (new KeyEvent (Key.PageDown, new KeyModifiers ())));
  129. Assert.Equal (new Point (0, -10), sv.ContentOffset);
  130. Assert.True (sv.ProcessKey (new KeyEvent (Key.PageDown, new KeyModifiers ())));
  131. Assert.Equal (new Point (0, -19), sv.ContentOffset);
  132. Assert.False (sv.ProcessKey (new KeyEvent (Key.PageDown, new KeyModifiers ())));
  133. Assert.Equal (new Point (0, -19), sv.ContentOffset);
  134. Assert.False (sv.ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ())));
  135. Assert.Equal (new Point (0, -19), sv.ContentOffset);
  136. Assert.True (sv.ProcessKey (new KeyEvent ((Key)'v' | Key.AltMask, new KeyModifiers ())));
  137. Assert.Equal (new Point (0, -9), sv.ContentOffset);
  138. Assert.True (sv.ProcessKey (new KeyEvent (Key.V | Key.CtrlMask, new KeyModifiers ())));
  139. Assert.Equal (new Point (0, -19), sv.ContentOffset);
  140. Assert.False (sv.ProcessKey (new KeyEvent (Key.CursorLeft, new KeyModifiers ())));
  141. Assert.Equal (new Point (0, -19), sv.ContentOffset);
  142. Assert.True (sv.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ())));
  143. Assert.Equal (new Point (-1, -19), sv.ContentOffset);
  144. Assert.True (sv.ProcessKey (new KeyEvent (Key.CursorLeft, new KeyModifiers ())));
  145. Assert.Equal (new Point (0, -19), sv.ContentOffset);
  146. Assert.False (sv.ProcessKey (new KeyEvent (Key.PageUp | Key.CtrlMask, new KeyModifiers ())));
  147. Assert.Equal (new Point (0, -19), sv.ContentOffset);
  148. Assert.True (sv.ProcessKey (new KeyEvent (Key.PageDown | Key.CtrlMask, new KeyModifiers ())));
  149. Assert.Equal (new Point (-20, -19), sv.ContentOffset);
  150. Assert.True (sv.ProcessKey (new KeyEvent (Key.PageDown | Key.CtrlMask, new KeyModifiers ())));
  151. Assert.Equal (new Point (-39, -19), sv.ContentOffset);
  152. Assert.False (sv.ProcessKey (new KeyEvent (Key.PageDown | Key.CtrlMask, new KeyModifiers ())));
  153. Assert.Equal (new Point (-39, -19), sv.ContentOffset);
  154. Assert.False (sv.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ())));
  155. Assert.Equal (new Point (-39, -19), sv.ContentOffset);
  156. Assert.True (sv.ProcessKey (new KeyEvent (Key.PageUp | Key.CtrlMask, new KeyModifiers ())));
  157. Assert.Equal (new Point (-19, -19), sv.ContentOffset);
  158. Assert.True (sv.ProcessKey (new KeyEvent (Key.Home, new KeyModifiers ())));
  159. Assert.Equal (new Point (-19, 0), sv.ContentOffset);
  160. Assert.False (sv.ProcessKey (new KeyEvent (Key.Home, new KeyModifiers ())));
  161. Assert.Equal (new Point (-19, 0), sv.ContentOffset);
  162. Assert.True (sv.ProcessKey (new KeyEvent (Key.End, new KeyModifiers ())));
  163. Assert.Equal (new Point (-19, -19), sv.ContentOffset);
  164. Assert.False (sv.ProcessKey (new KeyEvent (Key.End, new KeyModifiers ())));
  165. Assert.Equal (new Point (-19, -19), sv.ContentOffset);
  166. Assert.True (sv.ProcessKey (new KeyEvent (Key.Home | Key.CtrlMask, new KeyModifiers ())));
  167. Assert.Equal (new Point (0, -19), sv.ContentOffset);
  168. Assert.False (sv.ProcessKey (new KeyEvent (Key.Home | Key.CtrlMask, new KeyModifiers ())));
  169. Assert.Equal (new Point (0, -19), sv.ContentOffset);
  170. Assert.True (sv.ProcessKey (new KeyEvent (Key.End | Key.CtrlMask, new KeyModifiers ())));
  171. Assert.Equal (new Point (-39, -19), sv.ContentOffset);
  172. Assert.False (sv.ProcessKey (new KeyEvent (Key.End | Key.CtrlMask, new KeyModifiers ())));
  173. Assert.Equal (new Point (-39, -19), sv.ContentOffset);
  174. }
  175. [Fact, AutoInitShutdown]
  176. public void AutoHideScrollBars_ShowHorizontalScrollIndicator_ShowVerticalScrollIndicator ()
  177. {
  178. var sv = new ScrollView {
  179. Width = 10,
  180. Height = 10
  181. };
  182. Application.Top.Add (sv);
  183. Application.Begin (Application.Top);
  184. Assert.True (sv.AutoHideScrollBars);
  185. Assert.False (sv.ShowHorizontalScrollIndicator);
  186. Assert.False (sv.ShowVerticalScrollIndicator);
  187. TestHelpers.AssertDriverContentsWithFrameAre ("", output);
  188. sv.AutoHideScrollBars = false;
  189. sv.ShowHorizontalScrollIndicator = true;
  190. sv.ShowVerticalScrollIndicator = true;
  191. sv.Redraw (sv.Bounds);
  192. TestHelpers.AssertDriverContentsWithFrameAre (@"
  193. ◄├─────┤►
  194. ", output);
  195. }
  196. [Fact, AutoInitShutdown]
  197. public void ContentSize_AutoHideScrollBars_ShowHorizontalScrollIndicator_ShowVerticalScrollIndicator ()
  198. {
  199. var sv = new ScrollView {
  200. Width = 10,
  201. Height = 10,
  202. ContentSize = new Size (50, 50)
  203. };
  204. Application.Top.Add (sv);
  205. Application.Begin (Application.Top);
  206. Assert.Equal (50, sv.ContentSize.Width);
  207. Assert.Equal (50, sv.ContentSize.Height);
  208. Assert.True (sv.AutoHideScrollBars);
  209. Assert.True (sv.ShowHorizontalScrollIndicator);
  210. Assert.True (sv.ShowVerticalScrollIndicator);
  211. TestHelpers.AssertDriverContentsWithFrameAre (@"
  212. ◄├┤░░░░░►
  213. ", output);
  214. }
  215. [Fact, AutoInitShutdown]
  216. public void ContentOffset_ContentSize_AutoHideScrollBars_ShowHorizontalScrollIndicator_ShowVerticalScrollIndicator ()
  217. {
  218. var sv = new ScrollView {
  219. Width = 10,
  220. Height = 10,
  221. ContentSize = new Size (50, 50),
  222. ContentOffset = new Point (25, 25)
  223. };
  224. Application.Top.Add (sv);
  225. Application.Begin (Application.Top);
  226. Assert.Equal (-25, sv.ContentOffset.X);
  227. Assert.Equal (-25, sv.ContentOffset.Y);
  228. Assert.Equal (50, sv.ContentSize.Width);
  229. Assert.Equal (50, sv.ContentSize.Height);
  230. Assert.True (sv.AutoHideScrollBars);
  231. Assert.True (sv.ShowHorizontalScrollIndicator);
  232. Assert.True (sv.ShowVerticalScrollIndicator);
  233. TestHelpers.AssertDriverContentsWithFrameAre (@"
  234. ◄░░░├─┤░►
  235. ", output);
  236. }
  237. }
  238. }