ScrollBarViewTests.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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 _scrollBar;
  13. private bool _added;
  14. public ScrollBarViewTests ()
  15. {
  16. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  17. var top = Application.Top;
  18. _hostView = new HostView () {
  19. Width = Dim.Fill (),
  20. Height = Dim.Fill (),
  21. Top = 0,
  22. Lines = 30,
  23. Left = 0,
  24. Cols = 100
  25. };
  26. top.Add (_hostView);
  27. }
  28. private void AddHandlers ()
  29. {
  30. if (!_added) {
  31. _hostView.DrawContent += _hostView_DrawContent;
  32. _scrollBar.ChangedPosition += _scrollBar_ChangedPosition;
  33. _scrollBar.OtherScrollBarView.ChangedPosition += _scrollBar_OtherScrollBarView_ChangedPosition;
  34. }
  35. _added = true;
  36. }
  37. private void RemoveHandlers ()
  38. {
  39. if (_added) {
  40. _hostView.DrawContent -= _hostView_DrawContent;
  41. _scrollBar.ChangedPosition -= _scrollBar_ChangedPosition;
  42. _scrollBar.OtherScrollBarView.ChangedPosition -= _scrollBar_OtherScrollBarView_ChangedPosition;
  43. }
  44. _added = false;
  45. }
  46. private void _hostView_DrawContent (Rect obj)
  47. {
  48. _scrollBar.Size = _hostView.Lines;
  49. _scrollBar.Position = _hostView.Top;
  50. _scrollBar.OtherScrollBarView.Size = _hostView.Cols;
  51. _scrollBar.OtherScrollBarView.Position = _hostView.Left;
  52. _scrollBar.Refresh ();
  53. }
  54. private void _scrollBar_ChangedPosition ()
  55. {
  56. _hostView.Top = _scrollBar.Position;
  57. if (_hostView.Top != _scrollBar.Position) {
  58. _scrollBar.Position = _hostView.Top;
  59. }
  60. _hostView.SetNeedsDisplay ();
  61. }
  62. private void _scrollBar_OtherScrollBarView_ChangedPosition ()
  63. {
  64. _hostView.Left = _scrollBar.OtherScrollBarView.Position;
  65. if (_hostView.Left != _scrollBar.OtherScrollBarView.Position) {
  66. _scrollBar.OtherScrollBarView.Position = _hostView.Left;
  67. }
  68. _hostView.SetNeedsDisplay ();
  69. }
  70. [Fact]
  71. public void Hosting_A_Null_View_To_A_ScrollBarView_Throws_ArgumentNullException ()
  72. {
  73. Assert.Throws<ArgumentNullException> ("The host parameter can't be null.",
  74. () => new ScrollBarView (null, true));
  75. Assert.Throws<ArgumentNullException> ("The host parameter can't be null.",
  76. () => new ScrollBarView (null, false));
  77. }
  78. [Fact]
  79. public void Hosting_A_Null_SuperView_View_To_A_ScrollBarView_Throws_ArgumentNullException ()
  80. {
  81. Assert.Throws<ArgumentNullException> ("The host SuperView parameter can't be null.",
  82. () => new ScrollBarView (new View (), true));
  83. Assert.Throws<ArgumentNullException> ("The host SuperView parameter can't be null.",
  84. () => new ScrollBarView (new View (), false));
  85. }
  86. [Fact]
  87. public void Hosting_Two_Vertical_ScrollBarView_Throws_ArgumentException ()
  88. {
  89. var top = new Toplevel ();
  90. var host = new View ();
  91. top.Add (host);
  92. var v = new ScrollBarView (host, true);
  93. var h = new ScrollBarView (host, true);
  94. Assert.Throws<ArgumentException> (null, () => v.OtherScrollBarView = h);
  95. Assert.Throws<ArgumentException> (null, () => h.OtherScrollBarView = v);
  96. }
  97. [Fact]
  98. public void Hosting_Two_Horizontal_ScrollBarView_Throws_ArgumentException ()
  99. {
  100. var top = new Toplevel ();
  101. var host = new View ();
  102. top.Add (host);
  103. var v = new ScrollBarView (host, false);
  104. var h = new ScrollBarView (host, false);
  105. Assert.Throws<ArgumentException> (null, () => v.OtherScrollBarView = h);
  106. Assert.Throws<ArgumentException> (null, () => h.OtherScrollBarView = v);
  107. }
  108. [Fact]
  109. public void Scrolling_With_Default_Constructor_Do_Not_Scroll ()
  110. {
  111. var sbv = new ScrollBarView {
  112. Position = 1
  113. };
  114. Assert.NotEqual (1, sbv.Position);
  115. Assert.Equal (0, sbv.Position);
  116. }
  117. [Fact]
  118. public void Hosting_A_View_To_A_ScrollBarView ()
  119. {
  120. RemoveHandlers ();
  121. _scrollBar = new ScrollBarView (_hostView, true);
  122. Assert.True (_scrollBar.IsVertical);
  123. Assert.False (_scrollBar.OtherScrollBarView.IsVertical);
  124. Assert.Equal (_scrollBar.Position, _hostView.Top);
  125. Assert.NotEqual (_scrollBar.Size, _hostView.Lines);
  126. Assert.Equal (_scrollBar.OtherScrollBarView.Position, _hostView.Left);
  127. Assert.NotEqual (_scrollBar.OtherScrollBarView.Size, _hostView.Cols);
  128. AddHandlers ();
  129. _hostView.SuperView.LayoutSubviews ();
  130. _hostView.Redraw (_hostView.Bounds);
  131. Assert.Equal (_scrollBar.Position, _hostView.Top);
  132. Assert.Equal (_scrollBar.Size, _hostView.Lines + 1);
  133. Assert.Equal (_scrollBar.OtherScrollBarView.Position, _hostView.Left);
  134. Assert.Equal (_scrollBar.OtherScrollBarView.Size, _hostView.Cols + 1);
  135. }
  136. [Fact]
  137. public void ChangedPosition_Update_The_Hosted_View ()
  138. {
  139. Hosting_A_View_To_A_ScrollBarView ();
  140. AddHandlers ();
  141. _scrollBar.Position = 2;
  142. Assert.Equal (_scrollBar.Position, _hostView.Top);
  143. _scrollBar.OtherScrollBarView.Position = 5;
  144. Assert.Equal (_scrollBar.OtherScrollBarView.Position, _hostView.Left);
  145. }
  146. [Fact]
  147. public void ChangedPosition_Scrolling ()
  148. {
  149. Hosting_A_View_To_A_ScrollBarView ();
  150. AddHandlers ();
  151. for (int i = 0; i < _scrollBar.Size; i++) {
  152. _scrollBar.Position += 1;
  153. Assert.Equal (_scrollBar.Position, _hostView.Top);
  154. }
  155. for (int i = _scrollBar.Size - 1; i >= 0; i--) {
  156. _scrollBar.Position -= 1;
  157. Assert.Equal (_scrollBar.Position, _hostView.Top);
  158. }
  159. for (int i = 0; i < _scrollBar.OtherScrollBarView.Size; i++) {
  160. _scrollBar.OtherScrollBarView.Position += i;
  161. Assert.Equal (_scrollBar.OtherScrollBarView.Position, _hostView.Left);
  162. }
  163. for (int i = _scrollBar.OtherScrollBarView.Size - 1; i >= 0; i--) {
  164. _scrollBar.OtherScrollBarView.Position -= 1;
  165. Assert.Equal (_scrollBar.OtherScrollBarView.Position, _hostView.Left);
  166. }
  167. }
  168. [Fact]
  169. public void ChangedPosition_Negative_Value ()
  170. {
  171. Hosting_A_View_To_A_ScrollBarView ();
  172. AddHandlers ();
  173. _scrollBar.Position = -20;
  174. Assert.Equal (0, _scrollBar.Position);
  175. Assert.Equal (_scrollBar.Position, _hostView.Top);
  176. _scrollBar.OtherScrollBarView.Position = -50;
  177. Assert.Equal (0, _scrollBar.OtherScrollBarView.Position);
  178. Assert.Equal (_scrollBar.OtherScrollBarView.Position, _hostView.Left);
  179. }
  180. [Fact]
  181. public void DrawContent_Update_The_ScrollBarView_Position ()
  182. {
  183. Hosting_A_View_To_A_ScrollBarView ();
  184. AddHandlers ();
  185. _hostView.Top = 3;
  186. _hostView.Redraw (_hostView.Bounds);
  187. Assert.Equal (_scrollBar.Position, _hostView.Top);
  188. _hostView.Left = 6;
  189. _hostView.Redraw (_hostView.Bounds);
  190. Assert.Equal (_scrollBar.OtherScrollBarView.Position, _hostView.Left);
  191. }
  192. [Fact]
  193. public void OtherScrollBarView_Not_Null ()
  194. {
  195. Hosting_A_View_To_A_ScrollBarView ();
  196. AddHandlers ();
  197. Assert.NotNull (_scrollBar.OtherScrollBarView);
  198. Assert.NotEqual (_scrollBar, _scrollBar.OtherScrollBarView);
  199. Assert.Equal (_scrollBar.OtherScrollBarView.OtherScrollBarView, _scrollBar);
  200. }
  201. [Fact]
  202. public void ShowScrollIndicator_Check ()
  203. {
  204. Hosting_A_View_To_A_ScrollBarView ();
  205. AddHandlers ();
  206. Assert.True (_scrollBar.ShowScrollIndicator);
  207. Assert.True (_scrollBar.OtherScrollBarView.ShowScrollIndicator);
  208. }
  209. [Fact]
  210. public void KeepContentAlwaysInViewport_True ()
  211. {
  212. Hosting_A_View_To_A_ScrollBarView ();
  213. AddHandlers ();
  214. Assert.Equal (80, _hostView.Bounds.Width);
  215. Assert.Equal (25, _hostView.Bounds.Height);
  216. Assert.Equal (79, _scrollBar.OtherScrollBarView.Bounds.Width);
  217. Assert.Equal (24, _scrollBar.Bounds.Height);
  218. Assert.Equal (31, _scrollBar.Size);
  219. Assert.Equal (101, _scrollBar.OtherScrollBarView.Size);
  220. Assert.True (_scrollBar.ShowScrollIndicator);
  221. Assert.True (_scrollBar.OtherScrollBarView.ShowScrollIndicator);
  222. Assert.True (_scrollBar.Visible);
  223. Assert.True (_scrollBar.OtherScrollBarView.Visible);
  224. _scrollBar.Position = 50;
  225. Assert.Equal (_scrollBar.Position, _scrollBar.Size - _scrollBar.Bounds.Height);
  226. Assert.Equal (_scrollBar.Position, _hostView.Top);
  227. Assert.Equal (7, _scrollBar.Position);
  228. Assert.Equal (7, _hostView.Top);
  229. Assert.True (_scrollBar.ShowScrollIndicator);
  230. Assert.True (_scrollBar.OtherScrollBarView.ShowScrollIndicator);
  231. Assert.True (_scrollBar.Visible);
  232. Assert.True (_scrollBar.OtherScrollBarView.Visible);
  233. _scrollBar.OtherScrollBarView.Position = 150;
  234. Assert.Equal (_scrollBar.OtherScrollBarView.Position, _scrollBar.OtherScrollBarView.Size - _scrollBar.OtherScrollBarView.Bounds.Width);
  235. Assert.Equal (_scrollBar.OtherScrollBarView.Position, _hostView.Left);
  236. Assert.Equal (22, _scrollBar.OtherScrollBarView.Position);
  237. Assert.Equal (22, _hostView.Left);
  238. Assert.True (_scrollBar.ShowScrollIndicator);
  239. Assert.True (_scrollBar.OtherScrollBarView.ShowScrollIndicator);
  240. Assert.True (_scrollBar.Visible);
  241. Assert.True (_scrollBar.OtherScrollBarView.Visible);
  242. }
  243. [Fact]
  244. public void KeepContentAlwaysInViewport_False ()
  245. {
  246. Hosting_A_View_To_A_ScrollBarView ();
  247. AddHandlers ();
  248. _scrollBar.KeepContentAlwaysInViewport = false;
  249. _scrollBar.Position = 50;
  250. Assert.Equal (_scrollBar.Position, _scrollBar.Size - 1);
  251. Assert.Equal (_scrollBar.Position, _hostView.Top);
  252. Assert.Equal (30, _scrollBar.Position);
  253. Assert.Equal (30, _hostView.Top);
  254. _scrollBar.OtherScrollBarView.Position = 150;
  255. Assert.Equal (_scrollBar.OtherScrollBarView.Position, _scrollBar.OtherScrollBarView.Size - 1);
  256. Assert.Equal (_scrollBar.OtherScrollBarView.Position, _hostView.Left);
  257. Assert.Equal (100, _scrollBar.OtherScrollBarView.Position);
  258. Assert.Equal (100, _hostView.Left);
  259. }
  260. [Fact]
  261. public void AutoHideScrollBars_Check ()
  262. {
  263. Hosting_A_View_To_A_ScrollBarView ();
  264. AddHandlers ();
  265. _hostView.Redraw (_hostView.Bounds);
  266. Assert.True (_scrollBar.ShowScrollIndicator);
  267. Assert.True (_scrollBar.Visible);
  268. Assert.Equal ("Dim.Absolute(1)", _scrollBar.Width.ToString ());
  269. Assert.Equal (1, _scrollBar.Bounds.Width);
  270. Assert.Equal ("Dim.Combine(DimView(side=Height, target=HostView()({X=0,Y=0,Width=80,Height=25}))-Dim.Absolute(1))",
  271. _scrollBar.Height.ToString ());
  272. Assert.Equal (24, _scrollBar.Bounds.Height);
  273. Assert.True (_scrollBar.OtherScrollBarView.ShowScrollIndicator);
  274. Assert.True (_scrollBar.OtherScrollBarView.Visible);
  275. Assert.Equal ("Dim.Combine(DimView(side=Width, target=HostView()({X=0,Y=0,Width=80,Height=25}))-Dim.Absolute(1))",
  276. _scrollBar.OtherScrollBarView.Width.ToString ());
  277. Assert.Equal (79, _scrollBar.OtherScrollBarView.Bounds.Width);
  278. Assert.Equal ("Dim.Absolute(1)", _scrollBar.OtherScrollBarView.Height.ToString ());
  279. Assert.Equal (1, _scrollBar.OtherScrollBarView.Bounds.Height);
  280. _hostView.Lines = 10;
  281. _hostView.Redraw (_hostView.Bounds);
  282. Assert.False (_scrollBar.ShowScrollIndicator);
  283. Assert.False (_scrollBar.Visible);
  284. Assert.Equal ("Dim.Absolute(1)", _scrollBar.Width.ToString ());
  285. Assert.Equal (1, _scrollBar.Bounds.Width);
  286. Assert.Equal ("Dim.Combine(DimView(side=Height, target=HostView()({X=0,Y=0,Width=80,Height=25}))-Dim.Absolute(1))",
  287. _scrollBar.Height.ToString ());
  288. Assert.Equal (24, _scrollBar.Bounds.Height);
  289. Assert.True (_scrollBar.OtherScrollBarView.ShowScrollIndicator);
  290. Assert.True (_scrollBar.OtherScrollBarView.Visible);
  291. Assert.Equal ("Dim.Combine(DimView(side=Width, target=HostView()({X=0,Y=0,Width=80,Height=25}))-Dim.Absolute(0))",
  292. _scrollBar.OtherScrollBarView.Width.ToString ());
  293. Assert.Equal (80, _scrollBar.OtherScrollBarView.Bounds.Width);
  294. Assert.Equal ("Dim.Absolute(1)", _scrollBar.OtherScrollBarView.Height.ToString ());
  295. Assert.Equal (1, _scrollBar.OtherScrollBarView.Bounds.Height);
  296. _hostView.Cols = 60;
  297. _hostView.Redraw (_hostView.Bounds);
  298. Assert.False (_scrollBar.ShowScrollIndicator);
  299. Assert.False (_scrollBar.Visible);
  300. Assert.Equal ("Dim.Absolute(1)", _scrollBar.Width.ToString ());
  301. Assert.Equal (1, _scrollBar.Bounds.Width);
  302. Assert.Equal ("Dim.Combine(DimView(side=Height, target=HostView()({X=0,Y=0,Width=80,Height=25}))-Dim.Absolute(1))",
  303. _scrollBar.Height.ToString ());
  304. Assert.Equal (24, _scrollBar.Bounds.Height);
  305. Assert.False (_scrollBar.OtherScrollBarView.ShowScrollIndicator);
  306. Assert.False (_scrollBar.OtherScrollBarView.Visible);
  307. Assert.Equal ("Dim.Combine(DimView(side=Width, target=HostView()({X=0,Y=0,Width=80,Height=25}))-Dim.Absolute(0))",
  308. _scrollBar.OtherScrollBarView.Width.ToString ());
  309. Assert.Equal (80, _scrollBar.OtherScrollBarView.Bounds.Width);
  310. Assert.Equal ("Dim.Absolute(1)", _scrollBar.OtherScrollBarView.Height.ToString ());
  311. Assert.Equal (1, _scrollBar.OtherScrollBarView.Bounds.Height);
  312. _hostView.Lines = 40;
  313. _hostView.Redraw (_hostView.Bounds);
  314. Assert.True (_scrollBar.ShowScrollIndicator);
  315. Assert.True (_scrollBar.Visible);
  316. Assert.Equal ("Dim.Absolute(1)", _scrollBar.Width.ToString ());
  317. Assert.Equal (1, _scrollBar.Bounds.Width);
  318. Assert.Equal ("Dim.Combine(DimView(side=Height, target=HostView()({X=0,Y=0,Width=80,Height=25}))-Dim.Absolute(0))",
  319. _scrollBar.Height.ToString ());
  320. Assert.Equal (25, _scrollBar.Bounds.Height);
  321. Assert.False (_scrollBar.OtherScrollBarView.ShowScrollIndicator);
  322. Assert.False (_scrollBar.OtherScrollBarView.Visible);
  323. Assert.Equal ("Dim.Combine(DimView(side=Width, target=HostView()({X=0,Y=0,Width=80,Height=25}))-Dim.Absolute(0))",
  324. _scrollBar.OtherScrollBarView.Width.ToString ());
  325. Assert.Equal (80, _scrollBar.OtherScrollBarView.Bounds.Width);
  326. Assert.Equal ("Dim.Absolute(1)", _scrollBar.OtherScrollBarView.Height.ToString ());
  327. Assert.Equal (1, _scrollBar.OtherScrollBarView.Bounds.Height);
  328. _hostView.Cols = 120;
  329. _hostView.Redraw (_hostView.Bounds);
  330. Assert.True (_scrollBar.ShowScrollIndicator);
  331. Assert.True (_scrollBar.Visible);
  332. Assert.Equal ("Dim.Absolute(1)", _scrollBar.Width.ToString ());
  333. Assert.Equal (1, _scrollBar.Bounds.Width);
  334. Assert.Equal ("Dim.Combine(DimView(side=Height, target=HostView()({X=0,Y=0,Width=80,Height=25}))-Dim.Absolute(1))",
  335. _scrollBar.Height.ToString ());
  336. Assert.Equal (24, _scrollBar.Bounds.Height);
  337. Assert.True (_scrollBar.OtherScrollBarView.ShowScrollIndicator);
  338. Assert.True (_scrollBar.OtherScrollBarView.Visible);
  339. Assert.Equal ("Dim.Combine(DimView(side=Width, target=HostView()({X=0,Y=0,Width=80,Height=25}))-Dim.Absolute(1))",
  340. _scrollBar.OtherScrollBarView.Width.ToString ());
  341. Assert.Equal (79, _scrollBar.OtherScrollBarView.Bounds.Width);
  342. Assert.Equal ("Dim.Absolute(1)", _scrollBar.OtherScrollBarView.Height.ToString ());
  343. Assert.Equal (1, _scrollBar.OtherScrollBarView.Bounds.Height);
  344. }
  345. }
  346. }