2
0

ScrollBarViewTests.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. using System;
  2. using Xunit;
  3. namespace Terminal.Gui {
  4. public class ScrollBarViewTests {
  5. public class HostView : View {
  6. public int Top { get; set; }
  7. public int Lines { get; set; }
  8. public int Left { get; set; }
  9. public int Cols { get; set; }
  10. }
  11. private HostView _hostView;
  12. private ScrollBarView _vertical;
  13. private ScrollBarView _horizontal;
  14. private bool _added;
  15. public ScrollBarViewTests ()
  16. {
  17. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  18. var top = Application.Top;
  19. _hostView = new HostView () {
  20. Width = Dim.Fill (),
  21. Height = Dim.Fill (),
  22. Top = 0,
  23. Lines = 30,
  24. Left = 0,
  25. Cols = 100
  26. };
  27. top.Add (_hostView);
  28. }
  29. private void AddHandlers ()
  30. {
  31. if (!_added) {
  32. _hostView.DrawContent += _hostView_DrawContent;
  33. _vertical.ChangedPosition += _vertical_ChangedPosition;
  34. _horizontal.ChangedPosition += _horizontal_ChangedPosition;
  35. }
  36. _added = true;
  37. }
  38. private void RemoveHandlers ()
  39. {
  40. if (_added) {
  41. _hostView.DrawContent -= _hostView_DrawContent;
  42. _vertical.ChangedPosition -= _vertical_ChangedPosition;
  43. _horizontal.ChangedPosition -= _horizontal_ChangedPosition;
  44. }
  45. _added = false;
  46. }
  47. private void _hostView_DrawContent (Rect obj)
  48. {
  49. _vertical.Size = _hostView.Lines;
  50. _vertical.Position = _hostView.Top;
  51. _horizontal.Size = _hostView.Cols;
  52. _horizontal.Position = _hostView.Left;
  53. _vertical.ColorScheme = _horizontal.ColorScheme = _hostView.ColorScheme;
  54. if (_vertical.ShowScrollIndicator) {
  55. _vertical.Redraw (obj);
  56. }
  57. if (_horizontal.ShowScrollIndicator) {
  58. _horizontal.Redraw (obj);
  59. }
  60. }
  61. private void _vertical_ChangedPosition ()
  62. {
  63. _hostView.Top = _vertical.Position;
  64. if (_hostView.Top != _vertical.Position) {
  65. _vertical.Position = _hostView.Top;
  66. }
  67. _hostView.SetNeedsDisplay ();
  68. }
  69. private void _horizontal_ChangedPosition ()
  70. {
  71. _hostView.Left = _horizontal.Position;
  72. if (_hostView.Left != _horizontal.Position) {
  73. _horizontal.Position = _hostView.Left;
  74. }
  75. _hostView.SetNeedsDisplay ();
  76. }
  77. [Fact]
  78. public void Hosting_A_Null_View_To_A_ScrollBarView_Throws_ArgumentNullException ()
  79. {
  80. Assert.Throws<ArgumentNullException> ("The host parameter can't be null.",
  81. () => new ScrollBarView (null, true));
  82. Assert.Throws<ArgumentNullException> ("The host parameter can't be null.",
  83. () => new ScrollBarView (null, false));
  84. }
  85. [Fact]
  86. public void Hosting_A_Null_SuperView_View_To_A_ScrollBarView_Throws_ArgumentNullException ()
  87. {
  88. Assert.Throws<ArgumentNullException> ("The host SuperView parameter can't be null.",
  89. () => new ScrollBarView (new View (), true));
  90. Assert.Throws<ArgumentNullException> ("The host SuperView parameter can't be null.",
  91. () => new ScrollBarView (new View (), false));
  92. }
  93. [Fact]
  94. public void Hosting_Two_Vertical_ScrollBarView_Throws_ArgumentException ()
  95. {
  96. var top = new Toplevel ();
  97. var host = new View ();
  98. top.Add (host);
  99. var v = new ScrollBarView (host, true);
  100. var h = new ScrollBarView (host, true);
  101. Assert.Throws<ArgumentException> (null, () => v.OtherScrollBarView = h);
  102. Assert.Throws<ArgumentException> (null, () => h.OtherScrollBarView = v);
  103. }
  104. [Fact]
  105. public void Scrolling_With_Default_Constructor_Throws_ArgumentNullException ()
  106. {
  107. var sbv = new ScrollBarView ();
  108. Assert.Throws<ArgumentNullException> ("The host can't be null.", () => sbv.Position = 1);
  109. }
  110. [Fact]
  111. public void Hosting_Two_Horizontal_ScrollBarView_Throws_ArgumentException ()
  112. {
  113. var top = new Toplevel ();
  114. var host = new View ();
  115. top.Add (host);
  116. var v = new ScrollBarView (host, false);
  117. var h = new ScrollBarView (host, false);
  118. Assert.Throws<ArgumentException> (null, () => v.OtherScrollBarView = h);
  119. Assert.Throws<ArgumentException> (null, () => h.OtherScrollBarView = v);
  120. }
  121. [Fact]
  122. public void Hosting_A_View_To_A_ScrollBarView ()
  123. {
  124. RemoveHandlers ();
  125. _vertical = new ScrollBarView (_hostView, true);
  126. _horizontal = new ScrollBarView (_hostView, false);
  127. _vertical.OtherScrollBarView = _horizontal;
  128. _horizontal.OtherScrollBarView = _vertical;
  129. Assert.True (_vertical.IsVertical);
  130. Assert.False (_horizontal.IsVertical);
  131. Assert.Equal (_vertical.Position, _hostView.Top);
  132. Assert.NotEqual (_vertical.Size, _hostView.Lines);
  133. Assert.Equal (_horizontal.Position, _hostView.Left);
  134. Assert.NotEqual (_horizontal.Size, _hostView.Cols);
  135. AddHandlers ();
  136. _hostView.SuperView.LayoutSubviews ();
  137. _hostView.Redraw (_hostView.Bounds);
  138. Assert.Equal (_vertical.Position, _hostView.Top);
  139. Assert.Equal (_vertical.Size, _hostView.Lines);
  140. Assert.Equal (_horizontal.Position, _hostView.Left);
  141. Assert.Equal (_horizontal.Size, _hostView.Cols);
  142. }
  143. [Fact]
  144. public void ChangedPosition_Update_The_Hosted_View ()
  145. {
  146. Hosting_A_View_To_A_ScrollBarView ();
  147. AddHandlers ();
  148. _vertical.Position = 2;
  149. Assert.Equal (_vertical.Position, _hostView.Top);
  150. _horizontal.Position = 5;
  151. Assert.Equal (_horizontal.Position, _hostView.Left);
  152. }
  153. [Fact]
  154. public void ChangedPosition_Scrolling ()
  155. {
  156. Hosting_A_View_To_A_ScrollBarView ();
  157. AddHandlers ();
  158. for (int i = 0; i < _vertical.Size; i++) {
  159. _vertical.Position += 1;
  160. Assert.Equal (_vertical.Position, _hostView.Top);
  161. }
  162. for (int i = _vertical.Size - 1; i >= 0; i--) {
  163. _vertical.Position -= 1;
  164. Assert.Equal (_vertical.Position, _hostView.Top);
  165. }
  166. for (int i = 0; i < _horizontal.Size; i++) {
  167. _horizontal.Position += i;
  168. Assert.Equal (_horizontal.Position, _hostView.Left);
  169. }
  170. for (int i = _horizontal.Size - 1; i >= 0; i--) {
  171. _horizontal.Position -= 1;
  172. Assert.Equal (_horizontal.Position, _hostView.Left);
  173. }
  174. }
  175. [Fact]
  176. public void ChangedPosition_Negative_Value ()
  177. {
  178. Hosting_A_View_To_A_ScrollBarView ();
  179. AddHandlers ();
  180. _vertical.Position = -20;
  181. Assert.Equal (0, _vertical.Position);
  182. Assert.Equal (_vertical.Position, _hostView.Top);
  183. _horizontal.Position = -50;
  184. Assert.Equal (0, _horizontal.Position);
  185. Assert.Equal (_horizontal.Position, _hostView.Left);
  186. }
  187. [Fact]
  188. public void DrawContent_Update_The_ScrollBarView_Position ()
  189. {
  190. Hosting_A_View_To_A_ScrollBarView ();
  191. AddHandlers ();
  192. _hostView.Top = 3;
  193. _hostView.Redraw (_hostView.Bounds);
  194. Assert.Equal (_vertical.Position, _hostView.Top);
  195. _hostView.Left = 6;
  196. _hostView.Redraw (_hostView.Bounds);
  197. Assert.Equal (_horizontal.Position, _hostView.Left);
  198. }
  199. [Fact]
  200. public void OtherScrollBarView_Not_Null ()
  201. {
  202. Hosting_A_View_To_A_ScrollBarView ();
  203. AddHandlers ();
  204. Assert.Equal (_vertical.OtherScrollBarView, _horizontal);
  205. Assert.Equal (_horizontal.OtherScrollBarView, _vertical);
  206. }
  207. [Fact]
  208. public void ShowScrollIndicator_Check ()
  209. {
  210. Hosting_A_View_To_A_ScrollBarView ();
  211. AddHandlers ();
  212. Assert.True (_vertical.ShowScrollIndicator);
  213. Assert.True (_horizontal.ShowScrollIndicator);
  214. }
  215. [Fact]
  216. public void KeepContentAlwaysInViewport_True ()
  217. {
  218. Hosting_A_View_To_A_ScrollBarView ();
  219. AddHandlers ();
  220. _vertical.Position = 50;
  221. Assert.Equal (_vertical.Position, _vertical.Size - _vertical.Bounds.Height + 1);
  222. Assert.Equal (_vertical.Position, _hostView.Top);
  223. _horizontal.Position = 150;
  224. Assert.Equal (_horizontal.Position, _horizontal.Size - _horizontal.Bounds.Width + 1);
  225. Assert.Equal (_horizontal.Position, _hostView.Left);
  226. }
  227. [Fact]
  228. public void KeepContentAlwaysInViewport_False ()
  229. {
  230. Hosting_A_View_To_A_ScrollBarView ();
  231. AddHandlers ();
  232. _vertical.KeepContentAlwaysInViewport = false;
  233. _vertical.Position = 50;
  234. Assert.Equal (_vertical.Position, _vertical.Size - 1);
  235. Assert.Equal (_vertical.Position, _hostView.Top);
  236. _horizontal.Position = 150;
  237. Assert.Equal (_horizontal.Position, _horizontal.Size - 1);
  238. Assert.Equal (_horizontal.Position, _hostView.Left);
  239. }
  240. [Fact]
  241. public void AutoHideScrollBars_Check ()
  242. {
  243. Hosting_A_View_To_A_ScrollBarView ();
  244. AddHandlers ();
  245. _hostView.Lines = 10;
  246. _hostView.Redraw (_hostView.Bounds);
  247. Assert.False (_vertical.ShowScrollIndicator);
  248. _hostView.Cols = 60;
  249. _hostView.Redraw (_hostView.Bounds);
  250. Assert.False (_horizontal.ShowScrollIndicator);
  251. _hostView.Lines = 40;
  252. _hostView.Redraw (_hostView.Bounds);
  253. Assert.True (_vertical.ShowScrollIndicator);
  254. _hostView.Cols = 120;
  255. _hostView.Redraw (_hostView.Bounds);
  256. Assert.True (_horizontal.ShowScrollIndicator);
  257. }
  258. }
  259. }