ScrollBarViewTests.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Reflection;
  5. using Xunit;
  6. using Xunit.Abstractions;
  7. namespace Terminal.Gui.ViewTests {
  8. public class ScrollBarViewTests {
  9. readonly ITestOutputHelper output;
  10. public ScrollBarViewTests (ITestOutputHelper output)
  11. {
  12. this.output = output;
  13. }
  14. // This class enables test functions annotated with the [InitShutdown] attribute
  15. // to have a function called before the test function is called and after.
  16. //
  17. // This is necessary because a) Application is a singleton and Init/Shutdown must be called
  18. // as a pair, and b) all unit test functions should be atomic.
  19. [AttributeUsage (AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
  20. public class ScrollBarAutoInitShutdownAttribute : AutoInitShutdownAttribute {
  21. public override void Before (MethodInfo methodUnderTest)
  22. {
  23. base.Before (methodUnderTest);
  24. ScrollBarViewTests._hostView = new HostView () {
  25. Width = Dim.Fill (),
  26. Height = Dim.Fill (),
  27. Top = 0,
  28. Lines = 30,
  29. Left = 0,
  30. Cols = 100
  31. };
  32. Application.Top.Add (ScrollBarViewTests._hostView);
  33. }
  34. public override void After (MethodInfo methodUnderTest)
  35. {
  36. ScrollBarViewTests._hostView = null;
  37. base.After (methodUnderTest);
  38. }
  39. }
  40. public class HostView : View {
  41. public int Top { get; set; }
  42. public int Lines { get; set; }
  43. public int Left { get; set; }
  44. public int Cols { get; set; }
  45. }
  46. private static HostView _hostView;
  47. private ScrollBarView _scrollBar;
  48. private bool _added;
  49. private void AddHandlers ()
  50. {
  51. if (!_added) {
  52. _hostView.DrawContent += _hostView_DrawContent;
  53. _scrollBar.ChangedPosition += _scrollBar_ChangedPosition;
  54. _scrollBar.OtherScrollBarView.ChangedPosition += _scrollBar_OtherScrollBarView_ChangedPosition;
  55. }
  56. _added = true;
  57. }
  58. private void RemoveHandlers ()
  59. {
  60. if (_added) {
  61. _hostView.DrawContent -= _hostView_DrawContent;
  62. _scrollBar.ChangedPosition -= _scrollBar_ChangedPosition;
  63. _scrollBar.OtherScrollBarView.ChangedPosition -= _scrollBar_OtherScrollBarView_ChangedPosition;
  64. }
  65. _added = false;
  66. }
  67. private void _hostView_DrawContent (Rect obj)
  68. {
  69. _scrollBar.Size = _hostView.Lines;
  70. _scrollBar.Position = _hostView.Top;
  71. _scrollBar.OtherScrollBarView.Size = _hostView.Cols;
  72. _scrollBar.OtherScrollBarView.Position = _hostView.Left;
  73. _scrollBar.Refresh ();
  74. }
  75. private void _scrollBar_ChangedPosition ()
  76. {
  77. _hostView.Top = _scrollBar.Position;
  78. if (_hostView.Top != _scrollBar.Position) {
  79. _scrollBar.Position = _hostView.Top;
  80. }
  81. _hostView.SetNeedsDisplay ();
  82. }
  83. private void _scrollBar_OtherScrollBarView_ChangedPosition ()
  84. {
  85. _hostView.Left = _scrollBar.OtherScrollBarView.Position;
  86. if (_hostView.Left != _scrollBar.OtherScrollBarView.Position) {
  87. _scrollBar.OtherScrollBarView.Position = _hostView.Left;
  88. }
  89. _hostView.SetNeedsDisplay ();
  90. }
  91. [Fact]
  92. [ScrollBarAutoInitShutdown]
  93. public void Hosting_A_Null_View_To_A_ScrollBarView_Throws_ArgumentNullException ()
  94. {
  95. Assert.Throws<ArgumentNullException> ("The host parameter can't be null.",
  96. () => new ScrollBarView (null, true));
  97. Assert.Throws<ArgumentNullException> ("The host parameter can't be null.",
  98. () => new ScrollBarView (null, false));
  99. }
  100. [Fact]
  101. [ScrollBarAutoInitShutdown]
  102. public void Hosting_A_Null_SuperView_View_To_A_ScrollBarView_Throws_ArgumentNullException ()
  103. {
  104. Assert.Throws<ArgumentNullException> ("The host SuperView parameter can't be null.",
  105. () => new ScrollBarView (new View (), true));
  106. Assert.Throws<ArgumentNullException> ("The host SuperView parameter can't be null.",
  107. () => new ScrollBarView (new View (), false));
  108. }
  109. [Fact]
  110. [ScrollBarAutoInitShutdown]
  111. public void Hosting_Two_Vertical_ScrollBarView_Throws_ArgumentException ()
  112. {
  113. var top = new Toplevel ();
  114. var host = new View ();
  115. top.Add (host);
  116. var v = new ScrollBarView (host, true);
  117. var h = new ScrollBarView (host, true);
  118. Assert.Throws<ArgumentException> (() => v.OtherScrollBarView = h);
  119. Assert.Throws<ArgumentException> (() => h.OtherScrollBarView = v);
  120. }
  121. [Fact]
  122. [ScrollBarAutoInitShutdown]
  123. public void Hosting_Two_Horizontal_ScrollBarView_Throws_ArgumentException ()
  124. {
  125. var top = new Toplevel ();
  126. var host = new View ();
  127. top.Add (host);
  128. var v = new ScrollBarView (host, false);
  129. var h = new ScrollBarView (host, false);
  130. Assert.Throws<ArgumentException> (() => v.OtherScrollBarView = h);
  131. Assert.Throws<ArgumentException> (() => h.OtherScrollBarView = v);
  132. }
  133. [Fact]
  134. [ScrollBarAutoInitShutdown]
  135. public void Scrolling_With_Default_Constructor_Do_Not_Scroll ()
  136. {
  137. var sbv = new ScrollBarView {
  138. Position = 1
  139. };
  140. Assert.NotEqual (1, sbv.Position);
  141. Assert.Equal (0, sbv.Position);
  142. }
  143. [Fact]
  144. [ScrollBarAutoInitShutdown]
  145. public void Hosting_A_View_To_A_ScrollBarView ()
  146. {
  147. RemoveHandlers ();
  148. _scrollBar = new ScrollBarView (_hostView, true);
  149. Assert.True (_scrollBar.IsVertical);
  150. Assert.False (_scrollBar.OtherScrollBarView.IsVertical);
  151. Assert.Equal (_scrollBar.Position, _hostView.Top);
  152. Assert.NotEqual (_scrollBar.Size, _hostView.Lines);
  153. Assert.Equal (_scrollBar.OtherScrollBarView.Position, _hostView.Left);
  154. Assert.NotEqual (_scrollBar.OtherScrollBarView.Size, _hostView.Cols);
  155. AddHandlers ();
  156. _hostView.SuperView.LayoutSubviews ();
  157. _hostView.Redraw (_hostView.Bounds);
  158. Assert.Equal (_scrollBar.Position, _hostView.Top);
  159. Assert.Equal (_scrollBar.Size, _hostView.Lines);
  160. Assert.Equal (_scrollBar.OtherScrollBarView.Position, _hostView.Left);
  161. Assert.Equal (_scrollBar.OtherScrollBarView.Size, _hostView.Cols);
  162. }
  163. [Fact]
  164. [ScrollBarAutoInitShutdown]
  165. public void ChangedPosition_Update_The_Hosted_View ()
  166. {
  167. Hosting_A_View_To_A_ScrollBarView ();
  168. AddHandlers ();
  169. _scrollBar.Position = 2;
  170. Assert.Equal (_scrollBar.Position, _hostView.Top);
  171. _scrollBar.OtherScrollBarView.Position = 5;
  172. Assert.Equal (_scrollBar.OtherScrollBarView.Position, _hostView.Left);
  173. }
  174. [Fact]
  175. [ScrollBarAutoInitShutdown]
  176. public void ChangedPosition_Scrolling ()
  177. {
  178. Hosting_A_View_To_A_ScrollBarView ();
  179. AddHandlers ();
  180. for (int i = 0; i < _scrollBar.Size; i++) {
  181. _scrollBar.Position += 1;
  182. Assert.Equal (_scrollBar.Position, _hostView.Top);
  183. }
  184. for (int i = _scrollBar.Size - 1; i >= 0; i--) {
  185. _scrollBar.Position -= 1;
  186. Assert.Equal (_scrollBar.Position, _hostView.Top);
  187. }
  188. for (int i = 0; i < _scrollBar.OtherScrollBarView.Size; i++) {
  189. _scrollBar.OtherScrollBarView.Position += i;
  190. Assert.Equal (_scrollBar.OtherScrollBarView.Position, _hostView.Left);
  191. }
  192. for (int i = _scrollBar.OtherScrollBarView.Size - 1; i >= 0; i--) {
  193. _scrollBar.OtherScrollBarView.Position -= 1;
  194. Assert.Equal (_scrollBar.OtherScrollBarView.Position, _hostView.Left);
  195. }
  196. }
  197. [Fact]
  198. [ScrollBarAutoInitShutdown]
  199. public void ChangedPosition_Negative_Value ()
  200. {
  201. Hosting_A_View_To_A_ScrollBarView ();
  202. AddHandlers ();
  203. _scrollBar.Position = -20;
  204. Assert.Equal (0, _scrollBar.Position);
  205. Assert.Equal (_scrollBar.Position, _hostView.Top);
  206. _scrollBar.OtherScrollBarView.Position = -50;
  207. Assert.Equal (0, _scrollBar.OtherScrollBarView.Position);
  208. Assert.Equal (_scrollBar.OtherScrollBarView.Position, _hostView.Left);
  209. }
  210. [Fact]
  211. [ScrollBarAutoInitShutdown]
  212. public void DrawContent_Update_The_ScrollBarView_Position ()
  213. {
  214. Hosting_A_View_To_A_ScrollBarView ();
  215. AddHandlers ();
  216. _hostView.Top = 3;
  217. _hostView.Redraw (_hostView.Bounds);
  218. Assert.Equal (_scrollBar.Position, _hostView.Top);
  219. _hostView.Left = 6;
  220. _hostView.Redraw (_hostView.Bounds);
  221. Assert.Equal (_scrollBar.OtherScrollBarView.Position, _hostView.Left);
  222. }
  223. [Fact]
  224. [ScrollBarAutoInitShutdown]
  225. public void OtherScrollBarView_Not_Null ()
  226. {
  227. Hosting_A_View_To_A_ScrollBarView ();
  228. AddHandlers ();
  229. Assert.NotNull (_scrollBar.OtherScrollBarView);
  230. Assert.NotEqual (_scrollBar, _scrollBar.OtherScrollBarView);
  231. Assert.Equal (_scrollBar.OtherScrollBarView.OtherScrollBarView, _scrollBar);
  232. }
  233. [Fact]
  234. [ScrollBarAutoInitShutdown]
  235. public void ShowScrollIndicator_Check ()
  236. {
  237. Hosting_A_View_To_A_ScrollBarView ();
  238. AddHandlers ();
  239. Assert.True (_scrollBar.ShowScrollIndicator);
  240. Assert.True (_scrollBar.OtherScrollBarView.ShowScrollIndicator);
  241. }
  242. [Fact]
  243. [ScrollBarAutoInitShutdown]
  244. public void KeepContentAlwaysInViewport_True ()
  245. {
  246. Hosting_A_View_To_A_ScrollBarView ();
  247. AddHandlers ();
  248. Assert.Equal (80, _hostView.Bounds.Width);
  249. Assert.Equal (25, _hostView.Bounds.Height);
  250. Assert.Equal (79, _scrollBar.OtherScrollBarView.Bounds.Width);
  251. Assert.Equal (24, _scrollBar.Bounds.Height);
  252. Assert.Equal (30, _scrollBar.Size);
  253. Assert.Equal (100, _scrollBar.OtherScrollBarView.Size);
  254. Assert.True (_scrollBar.ShowScrollIndicator);
  255. Assert.True (_scrollBar.OtherScrollBarView.ShowScrollIndicator);
  256. Assert.True (_scrollBar.Visible);
  257. Assert.True (_scrollBar.OtherScrollBarView.Visible);
  258. _scrollBar.Position = 50;
  259. Assert.Equal (_scrollBar.Position, _scrollBar.Size - _scrollBar.Bounds.Height);
  260. Assert.Equal (_scrollBar.Position, _hostView.Top);
  261. Assert.Equal (6, _scrollBar.Position);
  262. Assert.Equal (6, _hostView.Top);
  263. Assert.True (_scrollBar.ShowScrollIndicator);
  264. Assert.True (_scrollBar.OtherScrollBarView.ShowScrollIndicator);
  265. Assert.True (_scrollBar.Visible);
  266. Assert.True (_scrollBar.OtherScrollBarView.Visible);
  267. _scrollBar.OtherScrollBarView.Position = 150;
  268. Assert.Equal (_scrollBar.OtherScrollBarView.Position, _scrollBar.OtherScrollBarView.Size - _scrollBar.OtherScrollBarView.Bounds.Width);
  269. Assert.Equal (_scrollBar.OtherScrollBarView.Position, _hostView.Left);
  270. Assert.Equal (21, _scrollBar.OtherScrollBarView.Position);
  271. Assert.Equal (21, _hostView.Left);
  272. Assert.True (_scrollBar.ShowScrollIndicator);
  273. Assert.True (_scrollBar.OtherScrollBarView.ShowScrollIndicator);
  274. Assert.True (_scrollBar.Visible);
  275. Assert.True (_scrollBar.OtherScrollBarView.Visible);
  276. }
  277. [Fact]
  278. [ScrollBarAutoInitShutdown]
  279. public void KeepContentAlwaysInViewport_False ()
  280. {
  281. Hosting_A_View_To_A_ScrollBarView ();
  282. AddHandlers ();
  283. _scrollBar.KeepContentAlwaysInViewport = false;
  284. _scrollBar.Position = 50;
  285. Assert.Equal (_scrollBar.Position, _scrollBar.Size - 1);
  286. Assert.Equal (_scrollBar.Position, _hostView.Top);
  287. Assert.Equal (29, _scrollBar.Position);
  288. Assert.Equal (29, _hostView.Top);
  289. _scrollBar.OtherScrollBarView.Position = 150;
  290. Assert.Equal (_scrollBar.OtherScrollBarView.Position, _scrollBar.OtherScrollBarView.Size - 1);
  291. Assert.Equal (_scrollBar.OtherScrollBarView.Position, _hostView.Left);
  292. Assert.Equal (99, _scrollBar.OtherScrollBarView.Position);
  293. Assert.Equal (99, _hostView.Left);
  294. }
  295. [Fact]
  296. [ScrollBarAutoInitShutdown]
  297. public void AutoHideScrollBars_Check ()
  298. {
  299. Hosting_A_View_To_A_ScrollBarView ();
  300. AddHandlers ();
  301. _hostView.Redraw (_hostView.Bounds);
  302. Assert.True (_scrollBar.ShowScrollIndicator);
  303. Assert.True (_scrollBar.Visible);
  304. Assert.Equal ("Absolute(1)", _scrollBar.Width.ToString ());
  305. Assert.Equal (1, _scrollBar.Bounds.Width);
  306. Assert.Equal ("Combine(DimView(Height,HostView()({X=0,Y=0,Width=80,Height=25}))-Absolute(1))",
  307. _scrollBar.Height.ToString ());
  308. Assert.Equal (24, _scrollBar.Bounds.Height);
  309. Assert.True (_scrollBar.OtherScrollBarView.ShowScrollIndicator);
  310. Assert.True (_scrollBar.OtherScrollBarView.Visible);
  311. Assert.Equal ("Combine(DimView(Width,HostView()({X=0,Y=0,Width=80,Height=25}))-Absolute(1))",
  312. _scrollBar.OtherScrollBarView.Width.ToString ());
  313. Assert.Equal (79, _scrollBar.OtherScrollBarView.Bounds.Width);
  314. Assert.Equal ("Absolute(1)", _scrollBar.OtherScrollBarView.Height.ToString ());
  315. Assert.Equal (1, _scrollBar.OtherScrollBarView.Bounds.Height);
  316. _hostView.Lines = 10;
  317. _hostView.Redraw (_hostView.Bounds);
  318. Assert.False (_scrollBar.ShowScrollIndicator);
  319. Assert.False (_scrollBar.Visible);
  320. Assert.Equal ("Absolute(1)", _scrollBar.Width.ToString ());
  321. Assert.Equal (1, _scrollBar.Bounds.Width);
  322. Assert.Equal ("Combine(DimView(Height,HostView()({X=0,Y=0,Width=80,Height=25}))-Absolute(1))",
  323. _scrollBar.Height.ToString ());
  324. Assert.Equal (24, _scrollBar.Bounds.Height);
  325. Assert.True (_scrollBar.OtherScrollBarView.ShowScrollIndicator);
  326. Assert.True (_scrollBar.OtherScrollBarView.Visible);
  327. Assert.Equal ("Combine(DimView(Width,HostView()({X=0,Y=0,Width=80,Height=25}))-Absolute(0))",
  328. _scrollBar.OtherScrollBarView.Width.ToString ());
  329. Assert.Equal (80, _scrollBar.OtherScrollBarView.Bounds.Width);
  330. Assert.Equal ("Absolute(1)", _scrollBar.OtherScrollBarView.Height.ToString ());
  331. Assert.Equal (1, _scrollBar.OtherScrollBarView.Bounds.Height);
  332. _hostView.Cols = 60;
  333. _hostView.Redraw (_hostView.Bounds);
  334. Assert.False (_scrollBar.ShowScrollIndicator);
  335. Assert.False (_scrollBar.Visible);
  336. Assert.Equal ("Absolute(1)", _scrollBar.Width.ToString ());
  337. Assert.Equal (1, _scrollBar.Bounds.Width);
  338. Assert.Equal ("Combine(DimView(Height,HostView()({X=0,Y=0,Width=80,Height=25}))-Absolute(1))",
  339. _scrollBar.Height.ToString ());
  340. Assert.Equal (24, _scrollBar.Bounds.Height);
  341. Assert.False (_scrollBar.OtherScrollBarView.ShowScrollIndicator);
  342. Assert.False (_scrollBar.OtherScrollBarView.Visible);
  343. Assert.Equal ("Combine(DimView(Width,HostView()({X=0,Y=0,Width=80,Height=25}))-Absolute(0))",
  344. _scrollBar.OtherScrollBarView.Width.ToString ());
  345. Assert.Equal (80, _scrollBar.OtherScrollBarView.Bounds.Width);
  346. Assert.Equal ("Absolute(1)", _scrollBar.OtherScrollBarView.Height.ToString ());
  347. Assert.Equal (1, _scrollBar.OtherScrollBarView.Bounds.Height);
  348. _hostView.Lines = 40;
  349. _hostView.Redraw (_hostView.Bounds);
  350. Assert.True (_scrollBar.ShowScrollIndicator);
  351. Assert.True (_scrollBar.Visible);
  352. Assert.Equal ("Absolute(1)", _scrollBar.Width.ToString ());
  353. Assert.Equal (1, _scrollBar.Bounds.Width);
  354. Assert.Equal ("Combine(DimView(Height,HostView()({X=0,Y=0,Width=80,Height=25}))-Absolute(0))",
  355. _scrollBar.Height.ToString ());
  356. Assert.Equal (25, _scrollBar.Bounds.Height);
  357. Assert.False (_scrollBar.OtherScrollBarView.ShowScrollIndicator);
  358. Assert.False (_scrollBar.OtherScrollBarView.Visible);
  359. Assert.Equal ("Combine(DimView(Width,HostView()({X=0,Y=0,Width=80,Height=25}))-Absolute(0))",
  360. _scrollBar.OtherScrollBarView.Width.ToString ());
  361. Assert.Equal (80, _scrollBar.OtherScrollBarView.Bounds.Width);
  362. Assert.Equal ("Absolute(1)", _scrollBar.OtherScrollBarView.Height.ToString ());
  363. Assert.Equal (1, _scrollBar.OtherScrollBarView.Bounds.Height);
  364. _hostView.Cols = 120;
  365. _hostView.Redraw (_hostView.Bounds);
  366. Assert.True (_scrollBar.ShowScrollIndicator);
  367. Assert.True (_scrollBar.Visible);
  368. Assert.Equal ("Absolute(1)", _scrollBar.Width.ToString ());
  369. Assert.Equal (1, _scrollBar.Bounds.Width);
  370. Assert.Equal ("Combine(DimView(Height,HostView()({X=0,Y=0,Width=80,Height=25}))-Absolute(1))",
  371. _scrollBar.Height.ToString ());
  372. Assert.Equal (24, _scrollBar.Bounds.Height);
  373. Assert.True (_scrollBar.OtherScrollBarView.ShowScrollIndicator);
  374. Assert.True (_scrollBar.OtherScrollBarView.Visible);
  375. Assert.Equal ("Combine(DimView(Width,HostView()({X=0,Y=0,Width=80,Height=25}))-Absolute(1))",
  376. _scrollBar.OtherScrollBarView.Width.ToString ());
  377. Assert.Equal (79, _scrollBar.OtherScrollBarView.Bounds.Width);
  378. Assert.Equal ("Absolute(1)", _scrollBar.OtherScrollBarView.Height.ToString ());
  379. Assert.Equal (1, _scrollBar.OtherScrollBarView.Bounds.Height);
  380. }
  381. [Fact]
  382. public void Constructor_ShowBothScrollIndicator_False_And_IsVertical_True_Refresh_Does_Not_Throws_An_Object_Null_Exception ()
  383. {
  384. var exception = Record.Exception (() => {
  385. Application.Init (new FakeDriver ());
  386. var top = Application.Top;
  387. var win = new Window () {
  388. X = 0,
  389. Y = 0,
  390. Width = Dim.Fill (),
  391. Height = Dim.Fill ()
  392. };
  393. List<string> source = new List<string> ();
  394. for (int i = 0; i < 50; i++) {
  395. source.Add ($"item {i}");
  396. }
  397. var listView = new ListView (source) {
  398. X = 0,
  399. Y = 0,
  400. Width = Dim.Fill (),
  401. Height = Dim.Fill ()
  402. };
  403. win.Add (listView);
  404. var newScrollBarView = new ScrollBarView (listView, true, false) {
  405. KeepContentAlwaysInViewport = true
  406. };
  407. win.Add (newScrollBarView);
  408. newScrollBarView.ChangedPosition += () => {
  409. listView.TopItem = newScrollBarView.Position;
  410. if (listView.TopItem != newScrollBarView.Position) {
  411. newScrollBarView.Position = listView.TopItem;
  412. }
  413. Assert.Equal (newScrollBarView.Position, listView.TopItem);
  414. listView.SetNeedsDisplay ();
  415. };
  416. listView.DrawContent += (e) => {
  417. newScrollBarView.Size = listView.Source.Count;
  418. Assert.Equal (newScrollBarView.Size, listView.Source.Count);
  419. newScrollBarView.Position = listView.TopItem;
  420. Assert.Equal (newScrollBarView.Position, listView.TopItem);
  421. newScrollBarView.Refresh ();
  422. };
  423. top.Ready += () => {
  424. newScrollBarView.Position = 45;
  425. Assert.Equal (newScrollBarView.Position, newScrollBarView.Size - listView.TopItem + (listView.TopItem - listView.Bounds.Height));
  426. Assert.Equal (newScrollBarView.Position, listView.TopItem);
  427. Assert.Equal (27, newScrollBarView.Position);
  428. Assert.Equal (27, listView.TopItem);
  429. Application.RequestStop ();
  430. };
  431. top.Add (win);
  432. Application.Run ();
  433. Application.Shutdown ();
  434. });
  435. Assert.Null (exception);
  436. }
  437. [Fact]
  438. public void Constructor_ShowBothScrollIndicator_False_And_IsVertical_False_Refresh_Does_Not_Throws_An_Object_Null_Exception ()
  439. {
  440. var exception = Record.Exception (() => {
  441. Application.Init (new FakeDriver ());
  442. var top = Application.Top;
  443. var win = new Window () {
  444. X = 0,
  445. Y = 0,
  446. Width = Dim.Fill (),
  447. Height = Dim.Fill ()
  448. };
  449. List<string> source = new List<string> ();
  450. for (int i = 0; i < 50; i++) {
  451. var text = $"item {i} - ";
  452. for (int j = 0; j < 160; j++) {
  453. var col = j.ToString ();
  454. text += col.Length == 1 ? col [0] : col [1];
  455. }
  456. source.Add (text);
  457. }
  458. var listView = new ListView (source) {
  459. X = 0,
  460. Y = 0,
  461. Width = Dim.Fill (),
  462. Height = Dim.Fill ()
  463. };
  464. win.Add (listView);
  465. var newScrollBarView = new ScrollBarView (listView, false, false) {
  466. KeepContentAlwaysInViewport = true
  467. };
  468. win.Add (newScrollBarView);
  469. newScrollBarView.ChangedPosition += () => {
  470. listView.LeftItem = newScrollBarView.Position;
  471. if (listView.LeftItem != newScrollBarView.Position) {
  472. newScrollBarView.Position = listView.LeftItem;
  473. }
  474. Assert.Equal (newScrollBarView.Position, listView.LeftItem);
  475. listView.SetNeedsDisplay ();
  476. };
  477. listView.DrawContent += (e) => {
  478. newScrollBarView.Size = listView.Maxlength;
  479. Assert.Equal (newScrollBarView.Size, listView.Maxlength);
  480. newScrollBarView.Position = listView.LeftItem;
  481. Assert.Equal (newScrollBarView.Position, listView.LeftItem);
  482. newScrollBarView.Refresh ();
  483. };
  484. top.Ready += () => {
  485. newScrollBarView.Position = 100;
  486. Assert.Equal (newScrollBarView.Position, newScrollBarView.Size - listView.LeftItem + (listView.LeftItem - listView.Bounds.Width));
  487. Assert.Equal (newScrollBarView.Position, listView.LeftItem);
  488. Assert.Equal (92, newScrollBarView.Position);
  489. Assert.Equal (92, listView.LeftItem);
  490. Application.RequestStop ();
  491. };
  492. top.Add (win);
  493. Application.Run ();
  494. Application.Shutdown ();
  495. });
  496. Assert.Null (exception);
  497. }
  498. [Fact]
  499. [AutoInitShutdown]
  500. public void Internal_Tests ()
  501. {
  502. var top = Application.Top;
  503. Assert.Equal (new Rect (0, 0, 80, 25), top.Bounds);
  504. var view = new View () { Width = Dim.Fill (), Height = Dim.Fill () };
  505. top.Add (view);
  506. var sbv = new ScrollBarView (view, true);
  507. top.Add (sbv);
  508. Assert.Equal (view, sbv.Host);
  509. sbv.Size = 40;
  510. sbv.Position = 0;
  511. sbv.OtherScrollBarView.Size = 100;
  512. sbv.OtherScrollBarView.Position = 0;
  513. // Host bounds is empty.
  514. Assert.False (sbv.CanScroll (10, out int max, sbv.IsVertical));
  515. Assert.Equal (0, max);
  516. Assert.False (sbv.OtherScrollBarView.CanScroll (10, out max, sbv.OtherScrollBarView.IsVertical));
  517. Assert.Equal (0, max);
  518. // They aren't visible so they aren't drawn.
  519. Assert.False (sbv.Visible);
  520. Assert.False (sbv.OtherScrollBarView.Visible);
  521. top.LayoutSubviews ();
  522. // Now the host bounds is not empty.
  523. Assert.True (sbv.CanScroll (10, out max, sbv.IsVertical));
  524. Assert.Equal (10, max);
  525. Assert.True (sbv.OtherScrollBarView.CanScroll (10, out max, sbv.OtherScrollBarView.IsVertical));
  526. Assert.Equal (10, max);
  527. Assert.True (sbv.CanScroll (50, out max, sbv.IsVertical));
  528. Assert.Equal (40, sbv.Size);
  529. Assert.Equal (15, max); // 15+25=40
  530. Assert.True (sbv.OtherScrollBarView.CanScroll (150, out max, sbv.OtherScrollBarView.IsVertical));
  531. Assert.Equal (100, sbv.OtherScrollBarView.Size);
  532. Assert.Equal (20, max); // 20+80=100
  533. Assert.False (sbv.Visible);
  534. Assert.False (sbv.OtherScrollBarView.Visible);
  535. sbv.KeepContentAlwaysInViewport = false;
  536. sbv.OtherScrollBarView.KeepContentAlwaysInViewport = false;
  537. Assert.True (sbv.CanScroll (50, out max, sbv.IsVertical));
  538. Assert.Equal (39, max);
  539. Assert.True (sbv.OtherScrollBarView.CanScroll (150, out max, sbv.OtherScrollBarView.IsVertical));
  540. Assert.Equal (99, max);
  541. Assert.True (sbv.Visible);
  542. Assert.True (sbv.OtherScrollBarView.Visible);
  543. }
  544. [Fact, AutoInitShutdown]
  545. public void Hosting_ShowBothScrollIndicator_Invisible ()
  546. {
  547. var textView = new TextView () {
  548. Width = Dim.Fill (),
  549. Height = Dim.Fill (),
  550. Text = "This is the help text for the Second Step.\n\nPress the button to see a message box.\n\nEnter name too."
  551. };
  552. var win = new Window ("Test") {
  553. Width = Dim.Fill (),
  554. Height = Dim.Fill ()
  555. };
  556. win.Add (textView);
  557. var scrollBar = new ScrollBarView (textView, true);
  558. scrollBar.ChangedPosition += () => {
  559. textView.TopRow = scrollBar.Position;
  560. if (textView.TopRow != scrollBar.Position) {
  561. scrollBar.Position = textView.TopRow;
  562. }
  563. textView.SetNeedsDisplay ();
  564. };
  565. scrollBar.OtherScrollBarView.ChangedPosition += () => {
  566. textView.LeftColumn = scrollBar.OtherScrollBarView.Position;
  567. if (textView.LeftColumn != scrollBar.OtherScrollBarView.Position) {
  568. scrollBar.OtherScrollBarView.Position = textView.LeftColumn;
  569. }
  570. textView.SetNeedsDisplay ();
  571. };
  572. scrollBar.VisibleChanged += () => {
  573. if (scrollBar.Visible && textView.RightOffset == 0) {
  574. textView.RightOffset = 1;
  575. } else if (!scrollBar.Visible && textView.RightOffset == 1) {
  576. textView.RightOffset = 0;
  577. }
  578. };
  579. scrollBar.OtherScrollBarView.VisibleChanged += () => {
  580. if (scrollBar.OtherScrollBarView.Visible && textView.BottomOffset == 0) {
  581. textView.BottomOffset = 1;
  582. } else if (!scrollBar.OtherScrollBarView.Visible && textView.BottomOffset == 1) {
  583. textView.BottomOffset = 0;
  584. }
  585. };
  586. textView.DrawContent += (e) => {
  587. scrollBar.Size = textView.Lines;
  588. scrollBar.Position = textView.TopRow;
  589. if (scrollBar.OtherScrollBarView != null) {
  590. scrollBar.OtherScrollBarView.Size = textView.Maxlength;
  591. scrollBar.OtherScrollBarView.Position = textView.LeftColumn;
  592. }
  593. scrollBar.LayoutSubviews ();
  594. scrollBar.Refresh ();
  595. };
  596. Application.Top.Add (win);
  597. Application.Begin (Application.Top);
  598. ((FakeDriver)Application.Driver).SetBufferSize (45, 20);
  599. Assert.True (scrollBar.AutoHideScrollBars);
  600. Assert.Equal (5, textView.Lines);
  601. Assert.Equal (42, textView.Maxlength);
  602. Assert.Equal (0, textView.LeftColumn);
  603. Assert.Equal (0, scrollBar.Position);
  604. Assert.Equal (0, scrollBar.OtherScrollBarView.Position);
  605. var expected = @"
  606. ┌ Test ─────────────────────────────────────┐
  607. │This is the help text for the Second Step. │
  608. │ │
  609. │Press the button to see a message box. │
  610. │ │
  611. │Enter name too. │
  612. │ │
  613. │ │
  614. │ │
  615. │ │
  616. │ │
  617. │ │
  618. │ │
  619. │ │
  620. │ │
  621. │ │
  622. │ │
  623. │ │
  624. │ │
  625. └───────────────────────────────────────────┘
  626. ";
  627. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  628. Assert.Equal (new Rect (0, 0, 45, 20), pos);
  629. textView.WordWrap = true;
  630. ((FakeDriver)Application.Driver).SetBufferSize (26, 20);
  631. Application.Refresh ();
  632. Assert.True (textView.WordWrap);
  633. Assert.True (scrollBar.AutoHideScrollBars);
  634. Assert.Equal (7, textView.Lines);
  635. Assert.Equal (22, textView.Maxlength);
  636. Assert.Equal (0, textView.LeftColumn);
  637. Assert.Equal (0, scrollBar.Position);
  638. Assert.Equal (0, scrollBar.OtherScrollBarView.Position);
  639. expected = @"
  640. ┌ Test ──────────────────┐
  641. │This is the help text │
  642. │for the Second Step. │
  643. │ │
  644. │Press the button to │
  645. │see a message box. │
  646. │ │
  647. │Enter name too. │
  648. │ │
  649. │ │
  650. │ │
  651. │ │
  652. │ │
  653. │ │
  654. │ │
  655. │ │
  656. │ │
  657. │ │
  658. │ │
  659. └────────────────────────┘
  660. ";
  661. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  662. Assert.Equal (new Rect (0, 0, 26, 20), pos);
  663. ((FakeDriver)Application.Driver).SetBufferSize (10, 10);
  664. Application.Refresh ();
  665. Assert.True (textView.WordWrap);
  666. Assert.True (scrollBar.AutoHideScrollBars);
  667. Assert.Equal (20, textView.Lines);
  668. Assert.Equal (7, textView.Maxlength);
  669. Assert.Equal (0, textView.LeftColumn);
  670. Assert.Equal (0, scrollBar.Position);
  671. Assert.Equal (0, scrollBar.OtherScrollBarView.Position);
  672. expected = @"
  673. ┌ Test ──┐
  674. │This ▲│
  675. │is the ┬│
  676. │help ││
  677. │text ┴│
  678. │for ░│
  679. │the ░│
  680. │Second ░│
  681. │Step. ▼│
  682. └────────┘
  683. ";
  684. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  685. Assert.Equal (new Rect (0, 0, 10, 10), pos);
  686. }
  687. [Fact, AutoInitShutdown]
  688. public void ContentBottomRightCorner_Not_Redraw_If_Both_Size_Equal_To_Zero ()
  689. {
  690. var text = "This is a test\nThis is a test\nThis is a test\nThis is a test\nThis is a test\nThis is a test";
  691. var label = new Label (text);
  692. Application.Top.Add (label);
  693. var sbv = new ScrollBarView (label, true, true) {
  694. Size = 100,
  695. };
  696. sbv.OtherScrollBarView.Size = 100;
  697. Application.Begin (Application.Top);
  698. Assert.Equal (100, sbv.Size);
  699. Assert.Equal (100, sbv.OtherScrollBarView.Size);
  700. Assert.True (sbv.ShowScrollIndicator);
  701. Assert.True (sbv.OtherScrollBarView.ShowScrollIndicator);
  702. Assert.True (sbv.Visible);
  703. Assert.True (sbv.OtherScrollBarView.Visible);
  704. TestHelpers.AssertDriverContentsWithFrameAre (@"
  705. This is a tes▲
  706. This is a tes┬
  707. This is a tes┴
  708. This is a tes░
  709. This is a tes▼
  710. ◄├─┤░░░░░░░░►
  711. ", output);
  712. sbv.Size = 0;
  713. sbv.OtherScrollBarView.Size = 0;
  714. Assert.Equal (0, sbv.Size);
  715. Assert.Equal (0, sbv.OtherScrollBarView.Size);
  716. Assert.False (sbv.ShowScrollIndicator);
  717. Assert.False (sbv.OtherScrollBarView.ShowScrollIndicator);
  718. Assert.False (sbv.Visible);
  719. Assert.False (sbv.OtherScrollBarView.Visible);
  720. Application.Top.Redraw (Application.Top.Bounds);
  721. TestHelpers.AssertDriverContentsWithFrameAre (@"
  722. This is a test
  723. This is a test
  724. This is a test
  725. This is a test
  726. This is a test
  727. This is a test
  728. ", output);
  729. sbv.Size = 50;
  730. sbv.OtherScrollBarView.Size = 50;
  731. Assert.Equal (50, sbv.Size);
  732. Assert.Equal (50, sbv.OtherScrollBarView.Size);
  733. Assert.True (sbv.ShowScrollIndicator);
  734. Assert.True (sbv.OtherScrollBarView.ShowScrollIndicator);
  735. Assert.True (sbv.Visible);
  736. Assert.True (sbv.OtherScrollBarView.Visible);
  737. Application.Top.Redraw (Application.Top.Bounds);
  738. TestHelpers.AssertDriverContentsWithFrameAre (@"
  739. This is a tes▲
  740. This is a tes┬
  741. This is a tes┴
  742. This is a tes░
  743. This is a tes▼
  744. ◄├──┤░░░░░░░►
  745. ", output);
  746. }
  747. [Fact, AutoInitShutdown]
  748. public void ContentBottomRightCorner_Not_Redraw_If_One_Size_Equal_To_Zero ()
  749. {
  750. var text = "This is a test\nThis is a test\nThis is a test\nThis is a test\nThis is a test\nThis is a test";
  751. var label = new Label (text);
  752. Application.Top.Add (label);
  753. var sbv = new ScrollBarView (label, true, false) {
  754. Size = 100,
  755. };
  756. Application.Begin (Application.Top);
  757. Assert.Equal (100, sbv.Size);
  758. Assert.Null (sbv.OtherScrollBarView);
  759. Assert.True (sbv.ShowScrollIndicator);
  760. Assert.True (sbv.Visible);
  761. TestHelpers.AssertDriverContentsWithFrameAre (@"
  762. This is a tes▲
  763. This is a tes┬
  764. This is a tes┴
  765. This is a tes░
  766. This is a tes░
  767. This is a tes▼
  768. ", output);
  769. sbv.Size = 0;
  770. Assert.Equal (0, sbv.Size);
  771. Assert.False (sbv.ShowScrollIndicator);
  772. Assert.False (sbv.Visible);
  773. Application.Top.Redraw (Application.Top.Bounds);
  774. TestHelpers.AssertDriverContentsWithFrameAre (@"
  775. This is a test
  776. This is a test
  777. This is a test
  778. This is a test
  779. This is a test
  780. This is a test
  781. ", output);
  782. }
  783. [Fact, AutoInitShutdown]
  784. public void ShowScrollIndicator_False_Must_Also_Set_Visible_To_False_To_Not_Respond_To_Events ()
  785. {
  786. var clicked = false;
  787. var text = "This is a test\nThis is a test\nThis is a test\nThis is a test\nThis is a test";
  788. var label = new Label (text) { Width = 14, Height = 5 };
  789. var btn = new Button (14, 0, "Click Me!");
  790. btn.Clicked += () => clicked = true;
  791. Application.Top.Add (label, btn);
  792. var sbv = new ScrollBarView (label, true, false) {
  793. Size = 5,
  794. };
  795. Application.Begin (Application.Top);
  796. Assert.Equal (5, sbv.Size);
  797. Assert.Null (sbv.OtherScrollBarView);
  798. Assert.False (sbv.ShowScrollIndicator);
  799. Assert.False (sbv.Visible);
  800. TestHelpers.AssertDriverContentsWithFrameAre (@"
  801. This is a test[ Click Me! ]
  802. This is a test
  803. This is a test
  804. This is a test
  805. This is a test
  806. ", output);
  807. ReflectionTools.InvokePrivate (
  808. typeof (Application),
  809. "ProcessMouseEvent",
  810. new MouseEvent () {
  811. X = 15,
  812. Y = 0,
  813. Flags = MouseFlags.Button1Clicked
  814. });
  815. Assert.Null (Application.MouseGrabView);
  816. Assert.True (clicked);
  817. clicked = false;
  818. sbv.Visible = true;
  819. Assert.Equal (5, sbv.Size);
  820. Assert.False (sbv.ShowScrollIndicator);
  821. Assert.True (sbv.Visible);
  822. Application.Top.Redraw (Application.Top.Bounds);
  823. TestHelpers.AssertDriverContentsWithFrameAre (@"
  824. This is a test[ Click Me! ]
  825. This is a test
  826. This is a test
  827. This is a test
  828. This is a test
  829. ", output);
  830. ReflectionTools.InvokePrivate (
  831. typeof (Application),
  832. "ProcessMouseEvent",
  833. new MouseEvent () {
  834. X = 15,
  835. Y = 0,
  836. Flags = MouseFlags.Button1Clicked
  837. });
  838. Assert.Null (Application.MouseGrabView);
  839. Assert.True (clicked);
  840. Assert.Equal (5, sbv.Size);
  841. Assert.False (sbv.ShowScrollIndicator);
  842. Assert.False (sbv.Visible);
  843. }
  844. }
  845. }