ScrollBarViewTests.cs 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375
  1. using System.Reflection;
  2. using Xunit.Abstractions;
  3. namespace Terminal.Gui.ViewsTests;
  4. public class ScrollBarViewTests
  5. {
  6. private static HostView _hostView;
  7. private readonly ITestOutputHelper _output;
  8. private bool _added;
  9. private ScrollBarView _scrollBar;
  10. public ScrollBarViewTests (ITestOutputHelper output) { _output = output; }
  11. [Fact]
  12. [ScrollBarAutoInitShutdown]
  13. public void AutoHideScrollBars_Check ()
  14. {
  15. _scrollBar = new ScrollBarView (_hostView, true);
  16. Application.Begin (_hostView.SuperView as Toplevel);
  17. AddHandlers ();
  18. _hostView.Draw ();
  19. Assert.True (_scrollBar.ShowScrollIndicator);
  20. Assert.True (_scrollBar.Visible);
  21. Assert.Equal ("Absolute(1)", _scrollBar.Width.ToString ());
  22. Assert.Equal (1, _scrollBar.Viewport.Width);
  23. Assert.Equal (
  24. $"Combine(View(Height,HostView(){_hostView.Frame})-Absolute(1))",
  25. _scrollBar.Height.ToString ()
  26. );
  27. Assert.Equal (24, _scrollBar.Viewport.Height);
  28. Assert.True (_scrollBar.OtherScrollBarView.ShowScrollIndicator);
  29. Assert.True (_scrollBar.OtherScrollBarView.Visible);
  30. Assert.Equal (
  31. $"Combine(View(Width,HostView(){_hostView.Frame})-Absolute(1))",
  32. _scrollBar.OtherScrollBarView.Width.ToString ()
  33. );
  34. Assert.Equal (79, _scrollBar.OtherScrollBarView.Viewport.Width);
  35. Assert.Equal ("Absolute(1)", _scrollBar.OtherScrollBarView.Height.ToString ());
  36. Assert.Equal (1, _scrollBar.OtherScrollBarView.Viewport.Height);
  37. _hostView.Lines = 10;
  38. _hostView.Draw ();
  39. Assert.False (_scrollBar.ShowScrollIndicator);
  40. Assert.False (_scrollBar.Visible);
  41. Assert.Equal ("Absolute(1)", _scrollBar.Width.ToString ());
  42. Assert.Equal (1, _scrollBar.Viewport.Width);
  43. Assert.Equal (
  44. $"Combine(View(Height,HostView(){_hostView.Frame})-Absolute(1))",
  45. _scrollBar.Height.ToString ()
  46. );
  47. Assert.Equal (24, _scrollBar.Viewport.Height);
  48. Assert.True (_scrollBar.OtherScrollBarView.ShowScrollIndicator);
  49. Assert.True (_scrollBar.OtherScrollBarView.Visible);
  50. Assert.Equal (
  51. $"View(Width,HostView(){_hostView.Frame})",
  52. _scrollBar.OtherScrollBarView.Width.ToString ()
  53. );
  54. Assert.Equal (80, _scrollBar.OtherScrollBarView.Viewport.Width);
  55. Assert.Equal ("Absolute(1)", _scrollBar.OtherScrollBarView.Height.ToString ());
  56. Assert.Equal (1, _scrollBar.OtherScrollBarView.Viewport.Height);
  57. _hostView.Cols = 60;
  58. _hostView.Draw ();
  59. Assert.False (_scrollBar.ShowScrollIndicator);
  60. Assert.False (_scrollBar.Visible);
  61. Assert.Equal ("Absolute(1)", _scrollBar.Width.ToString ());
  62. Assert.Equal (1, _scrollBar.Viewport.Width);
  63. Assert.Equal (
  64. $"Combine(View(Height,HostView(){_hostView.Frame})-Absolute(1))",
  65. _scrollBar.Height.ToString ()
  66. );
  67. Assert.Equal (24, _scrollBar.Viewport.Height);
  68. Assert.False (_scrollBar.OtherScrollBarView.ShowScrollIndicator);
  69. Assert.False (_scrollBar.OtherScrollBarView.Visible);
  70. Assert.Equal (
  71. $"View(Width,HostView(){_hostView.Frame})",
  72. _scrollBar.OtherScrollBarView.Width.ToString ()
  73. );
  74. Assert.Equal (80, _scrollBar.OtherScrollBarView.Viewport.Width);
  75. Assert.Equal ("Absolute(1)", _scrollBar.OtherScrollBarView.Height.ToString ());
  76. Assert.Equal (1, _scrollBar.OtherScrollBarView.Viewport.Height);
  77. _hostView.Lines = 40;
  78. _hostView.Draw ();
  79. Assert.True (_scrollBar.ShowScrollIndicator);
  80. Assert.True (_scrollBar.Visible);
  81. Assert.Equal ("Absolute(1)", _scrollBar.Width.ToString ());
  82. Assert.Equal (1, _scrollBar.Viewport.Width);
  83. Assert.Equal (
  84. $"View(Height,HostView(){_hostView.Frame})",
  85. _scrollBar.Height.ToString ()
  86. );
  87. Assert.Equal (25, _scrollBar.Viewport.Height);
  88. Assert.False (_scrollBar.OtherScrollBarView.ShowScrollIndicator);
  89. Assert.False (_scrollBar.OtherScrollBarView.Visible);
  90. Assert.Equal (
  91. $"View(Width,HostView(){_hostView.Frame})",
  92. _scrollBar.OtherScrollBarView.Width.ToString ()
  93. );
  94. Assert.Equal (80, _scrollBar.OtherScrollBarView.Viewport.Width);
  95. Assert.Equal ("Absolute(1)", _scrollBar.OtherScrollBarView.Height.ToString ());
  96. Assert.Equal (1, _scrollBar.OtherScrollBarView.Viewport.Height);
  97. _hostView.Cols = 120;
  98. _hostView.Draw ();
  99. Assert.True (_scrollBar.ShowScrollIndicator);
  100. Assert.True (_scrollBar.Visible);
  101. Assert.Equal ("Absolute(1)", _scrollBar.Width.ToString ());
  102. Assert.Equal (1, _scrollBar.Viewport.Width);
  103. Assert.Equal (
  104. $"Combine(View(Height,HostView(){_hostView.Frame})-Absolute(1))",
  105. _scrollBar.Height.ToString ()
  106. );
  107. Assert.Equal (24, _scrollBar.Viewport.Height);
  108. Assert.True (_scrollBar.OtherScrollBarView.ShowScrollIndicator);
  109. Assert.True (_scrollBar.OtherScrollBarView.Visible);
  110. Assert.Equal (
  111. $"Combine(View(Width,HostView(){_hostView.Frame})-Absolute(1))",
  112. _scrollBar.OtherScrollBarView.Width.ToString ()
  113. );
  114. Assert.Equal (79, _scrollBar.OtherScrollBarView.Viewport.Width);
  115. Assert.Equal ("Absolute(1)", _scrollBar.OtherScrollBarView.Height.ToString ());
  116. Assert.Equal (1, _scrollBar.OtherScrollBarView.Viewport.Height);
  117. _hostView.SuperView.Dispose ();
  118. }
  119. [Fact]
  120. [AutoInitShutdown]
  121. public void Both_Default_Draws_Correctly ()
  122. {
  123. var width = 3;
  124. var height = 40;
  125. var super = new Window { Id = "super", Width = Dim.Fill (), Height = Dim.Fill () };
  126. var top = new Toplevel ();
  127. top.Add (super);
  128. var horiz = new ScrollBarView
  129. {
  130. Id = "horiz",
  131. Size = width * 2,
  132. // BUGBUG: ScrollBarView should work if Host is null
  133. Host = super,
  134. ShowScrollIndicator = true,
  135. IsVertical = true
  136. };
  137. super.Add (horiz);
  138. var vert = new ScrollBarView
  139. {
  140. Id = "vert",
  141. Size = height * 2,
  142. // BUGBUG: ScrollBarView should work if Host is null
  143. Host = super,
  144. ShowScrollIndicator = true,
  145. IsVertical = true
  146. };
  147. super.Add (vert);
  148. Application.Begin (top);
  149. ((FakeDriver)Application.Driver).SetBufferSize (width, height);
  150. var expected = @"
  151. ┌─┐
  152. │▲│
  153. │┬│
  154. │││
  155. │││
  156. │││
  157. │││
  158. │││
  159. │││
  160. │││
  161. │││
  162. │││
  163. │││
  164. │││
  165. │││
  166. │││
  167. │││
  168. │││
  169. │││
  170. │┴│
  171. │░│
  172. │░│
  173. │░│
  174. │░│
  175. │░│
  176. │░│
  177. │░│
  178. │░│
  179. │░│
  180. │░│
  181. │░│
  182. │░│
  183. │░│
  184. │░│
  185. │░│
  186. │░│
  187. │░│
  188. │░│
  189. │▼│
  190. └─┘";
  191. _ = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  192. top.Dispose ();
  193. }
  194. [Fact]
  195. [ScrollBarAutoInitShutdown]
  196. public void ChangedPosition_Negative_Value ()
  197. {
  198. _scrollBar = new ScrollBarView (_hostView, true);
  199. Application.Begin (_hostView.SuperView as Toplevel);
  200. AddHandlers ();
  201. _scrollBar.Position = -20;
  202. Assert.Equal (0, _scrollBar.Position);
  203. Assert.Equal (_scrollBar.Position, _hostView.Top);
  204. _scrollBar.OtherScrollBarView.Position = -50;
  205. Assert.Equal (0, _scrollBar.OtherScrollBarView.Position);
  206. Assert.Equal (_scrollBar.OtherScrollBarView.Position, _hostView.Left);
  207. _hostView.SuperView.Dispose ();
  208. }
  209. [Fact]
  210. [ScrollBarAutoInitShutdown]
  211. public void ChangedPosition_Scrolling ()
  212. {
  213. _scrollBar = new ScrollBarView (_hostView, true);
  214. Application.Begin (_hostView.SuperView as Toplevel);
  215. AddHandlers ();
  216. for (var i = 0; i < _scrollBar.Size; i++)
  217. {
  218. _scrollBar.Position += 1;
  219. Assert.Equal (_scrollBar.Position, _hostView.Top);
  220. }
  221. for (int i = _scrollBar.Size - 1; i >= 0; i--)
  222. {
  223. _scrollBar.Position -= 1;
  224. Assert.Equal (_scrollBar.Position, _hostView.Top);
  225. }
  226. for (var i = 0; i < _scrollBar.OtherScrollBarView.Size; i++)
  227. {
  228. _scrollBar.OtherScrollBarView.Position += i;
  229. Assert.Equal (_scrollBar.OtherScrollBarView.Position, _hostView.Left);
  230. }
  231. for (int i = _scrollBar.OtherScrollBarView.Size - 1; i >= 0; i--)
  232. {
  233. _scrollBar.OtherScrollBarView.Position -= 1;
  234. Assert.Equal (_scrollBar.OtherScrollBarView.Position, _hostView.Left);
  235. }
  236. _hostView.SuperView.Dispose ();
  237. }
  238. [Fact]
  239. [ScrollBarAutoInitShutdown]
  240. public void ChangedPosition_Update_The_Hosted_View ()
  241. {
  242. _scrollBar = new ScrollBarView (_hostView, true);
  243. Application.Begin (_hostView.SuperView as Toplevel);
  244. AddHandlers ();
  245. _scrollBar.Position = 2;
  246. Assert.Equal (_scrollBar.Position, _hostView.Top);
  247. _scrollBar.OtherScrollBarView.Position = 5;
  248. Assert.Equal (_scrollBar.OtherScrollBarView.Position, _hostView.Left);
  249. _hostView.SuperView.Dispose ();
  250. }
  251. [Fact]
  252. [AutoInitShutdown]
  253. public void ClearOnVisibleFalse_Gets_Sets ()
  254. {
  255. var text =
  256. "This is a test\nThis is a test\nThis is a test\nThis is a test\nThis is a test\nThis is a test";
  257. var label = new Label { Text = text };
  258. var top = new Toplevel ();
  259. top.Add (label);
  260. var sbv = new ScrollBarView (label, true, false) { Size = 100, ClearOnVisibleFalse = false };
  261. Application.Begin (top);
  262. Assert.True (sbv.Visible);
  263. TestHelpers.AssertDriverContentsWithFrameAre (
  264. @"
  265. This is a tes▲
  266. This is a tes┬
  267. This is a tes┴
  268. This is a tes░
  269. This is a tes░
  270. This is a tes▼
  271. ",
  272. _output
  273. );
  274. sbv.Visible = false;
  275. Assert.False (sbv.Visible);
  276. top.Draw ();
  277. TestHelpers.AssertDriverContentsWithFrameAre (
  278. @"
  279. This is a test
  280. This is a test
  281. This is a test
  282. This is a test
  283. This is a test
  284. This is a test
  285. ",
  286. _output
  287. );
  288. sbv.Visible = true;
  289. Assert.True (sbv.Visible);
  290. top.Draw ();
  291. TestHelpers.AssertDriverContentsWithFrameAre (
  292. @"
  293. This is a tes▲
  294. This is a tes┬
  295. This is a tes┴
  296. This is a tes░
  297. This is a tes░
  298. This is a tes▼
  299. ",
  300. _output
  301. );
  302. sbv.ClearOnVisibleFalse = true;
  303. sbv.Visible = false;
  304. Assert.False (sbv.Visible);
  305. TestHelpers.AssertDriverContentsWithFrameAre (
  306. @"
  307. This is a tes
  308. This is a tes
  309. This is a tes
  310. This is a tes
  311. This is a tes
  312. This is a tes
  313. ",
  314. _output
  315. );
  316. top.Dispose ();
  317. }
  318. [Fact]
  319. public void
  320. Constructor_ShowBothScrollIndicator_False_And_IsVertical_False_Refresh_Does_Not_Throws_An_Object_Null_Exception ()
  321. {
  322. var exception = Record.Exception (
  323. () =>
  324. {
  325. Application.Init (new FakeDriver ());
  326. Toplevel top = new ();
  327. var win = new Window { X = 0, Y = 0, Width = Dim.Fill (), Height = Dim.Fill () };
  328. List<string> source = new ();
  329. for (var i = 0; i < 50; i++)
  330. {
  331. var text = $"item {i} - ";
  332. for (var j = 0; j < 160; j++)
  333. {
  334. var col = j.ToString ();
  335. text += col.Length == 1 ? col [0] : col [1];
  336. }
  337. source.Add (text);
  338. }
  339. var listView = new ListView
  340. {
  341. X = 0,
  342. Y = 0,
  343. Width = Dim.Fill (),
  344. Height = Dim.Fill (),
  345. Source = new ListWrapper (source)
  346. };
  347. win.Add (listView);
  348. var newScrollBarView = new ScrollBarView (listView, false, false) { KeepContentAlwaysInViewport = true };
  349. win.Add (newScrollBarView);
  350. newScrollBarView.ChangedPosition += (s, e) =>
  351. {
  352. listView.LeftItem = newScrollBarView.Position;
  353. if (listView.LeftItem != newScrollBarView.Position)
  354. {
  355. newScrollBarView.Position = listView.LeftItem;
  356. }
  357. Assert.Equal (newScrollBarView.Position, listView.LeftItem);
  358. listView.SetNeedsDisplay ();
  359. };
  360. listView.DrawContent += (s, e) =>
  361. {
  362. newScrollBarView.Size = listView.MaxLength;
  363. Assert.Equal (newScrollBarView.Size, listView.MaxLength);
  364. newScrollBarView.Position = listView.LeftItem;
  365. Assert.Equal (newScrollBarView.Position, listView.LeftItem);
  366. newScrollBarView.Refresh ();
  367. };
  368. top.Ready += (s, e) =>
  369. {
  370. newScrollBarView.Position = 100;
  371. Assert.Equal (
  372. newScrollBarView.Position,
  373. newScrollBarView.Size
  374. - listView.LeftItem
  375. + (listView.LeftItem - listView.Viewport.Width));
  376. Assert.Equal (newScrollBarView.Position, listView.LeftItem);
  377. Assert.Equal (92, newScrollBarView.Position);
  378. Assert.Equal (92, listView.LeftItem);
  379. Application.RequestStop ();
  380. };
  381. top.Add (win);
  382. Application.Run (top);
  383. top.Dispose ();
  384. Application.Shutdown ();
  385. });
  386. Assert.Null (exception);
  387. }
  388. [Fact]
  389. public void
  390. Constructor_ShowBothScrollIndicator_False_And_IsVertical_True_Refresh_Does_Not_Throws_An_Object_Null_Exception ()
  391. {
  392. Exception exception = Record.Exception (
  393. () =>
  394. {
  395. Application.Init (new FakeDriver ());
  396. Toplevel top = new ();
  397. var win = new Window { X = 0, Y = 0, Width = Dim.Fill (), Height = Dim.Fill () };
  398. List<string> source = new ();
  399. for (var i = 0; i < 50; i++)
  400. {
  401. source.Add ($"item {i}");
  402. }
  403. var listView = new ListView
  404. {
  405. X = 0,
  406. Y = 0,
  407. Width = Dim.Fill (),
  408. Height = Dim.Fill (),
  409. Source = new ListWrapper (source)
  410. };
  411. win.Add (listView);
  412. var newScrollBarView = new ScrollBarView (listView, true, false) { KeepContentAlwaysInViewport = true };
  413. win.Add (newScrollBarView);
  414. newScrollBarView.ChangedPosition += (s, e) =>
  415. {
  416. listView.TopItem = newScrollBarView.Position;
  417. if (listView.TopItem != newScrollBarView.Position)
  418. {
  419. newScrollBarView.Position = listView.TopItem;
  420. }
  421. Assert.Equal (newScrollBarView.Position, listView.TopItem);
  422. listView.SetNeedsDisplay ();
  423. };
  424. listView.DrawContent += (s, e) =>
  425. {
  426. newScrollBarView.Size = listView.Source.Count;
  427. Assert.Equal (newScrollBarView.Size, listView.Source.Count);
  428. newScrollBarView.Position = listView.TopItem;
  429. Assert.Equal (newScrollBarView.Position, listView.TopItem);
  430. newScrollBarView.Refresh ();
  431. };
  432. top.Ready += (s, e) =>
  433. {
  434. newScrollBarView.Position = 45;
  435. Assert.Equal (
  436. newScrollBarView.Position,
  437. newScrollBarView.Size
  438. - listView.TopItem
  439. + (listView.TopItem - listView.Viewport.Height)
  440. );
  441. Assert.Equal (newScrollBarView.Position, listView.TopItem);
  442. Assert.Equal (27, newScrollBarView.Position);
  443. Assert.Equal (27, listView.TopItem);
  444. Application.RequestStop ();
  445. };
  446. top.Add (win);
  447. Application.Run (top);
  448. top.Dispose ();
  449. Application.Shutdown ();
  450. }
  451. );
  452. Assert.Null (exception);
  453. }
  454. [Fact]
  455. [AutoInitShutdown]
  456. public void ContentBottomRightCorner_Not_Redraw_If_Both_Size_Equal_To_Zero ()
  457. {
  458. var text =
  459. "This is a test\nThis is a test\nThis is a test\nThis is a test\nThis is a test\nThis is a test";
  460. var label = new Label { Text = text };
  461. var top = new Toplevel ();
  462. top.Add (label);
  463. var sbv = new ScrollBarView (label, true) { Size = 100 };
  464. sbv.OtherScrollBarView.Size = 100;
  465. Application.Begin (top);
  466. Assert.Equal (100, sbv.Size);
  467. Assert.Equal (100, sbv.OtherScrollBarView.Size);
  468. Assert.True (sbv.ShowScrollIndicator);
  469. Assert.True (sbv.OtherScrollBarView.ShowScrollIndicator);
  470. Assert.True (sbv.Visible);
  471. Assert.True (sbv.OtherScrollBarView.Visible);
  472. View contentBottomRightCorner =
  473. label.SuperView.Subviews.First (v => v is ScrollBarView.ContentBottomRightCorner);
  474. Assert.True (contentBottomRightCorner is ScrollBarView.ContentBottomRightCorner);
  475. Assert.True (contentBottomRightCorner.Visible);
  476. TestHelpers.AssertDriverContentsWithFrameAre (
  477. @"
  478. This is a tes▲
  479. This is a tes┬
  480. This is a tes┴
  481. This is a tes░
  482. This is a tes▼
  483. ◄├─┤░░░░░░░░►
  484. ",
  485. _output
  486. );
  487. sbv.Size = 0;
  488. sbv.OtherScrollBarView.Size = 0;
  489. Assert.Equal (0, sbv.Size);
  490. Assert.Equal (0, sbv.OtherScrollBarView.Size);
  491. Assert.False (sbv.ShowScrollIndicator);
  492. Assert.False (sbv.OtherScrollBarView.ShowScrollIndicator);
  493. Assert.False (sbv.Visible);
  494. Assert.False (sbv.OtherScrollBarView.Visible);
  495. top.Draw ();
  496. TestHelpers.AssertDriverContentsWithFrameAre (
  497. @"
  498. This is a test
  499. This is a test
  500. This is a test
  501. This is a test
  502. This is a test
  503. This is a test
  504. ",
  505. _output
  506. );
  507. sbv.Size = 50;
  508. sbv.OtherScrollBarView.Size = 50;
  509. Assert.Equal (50, sbv.Size);
  510. Assert.Equal (50, sbv.OtherScrollBarView.Size);
  511. Assert.True (sbv.ShowScrollIndicator);
  512. Assert.True (sbv.OtherScrollBarView.ShowScrollIndicator);
  513. Assert.True (sbv.Visible);
  514. Assert.True (sbv.OtherScrollBarView.Visible);
  515. top.Draw ();
  516. TestHelpers.AssertDriverContentsWithFrameAre (
  517. @"
  518. This is a tes▲
  519. This is a tes┬
  520. This is a tes┴
  521. This is a tes░
  522. This is a tes▼
  523. ◄├──┤░░░░░░░►
  524. ",
  525. _output
  526. );
  527. top.Dispose ();
  528. }
  529. [Fact]
  530. [AutoInitShutdown]
  531. public void ContentBottomRightCorner_Not_Redraw_If_One_Size_Equal_To_Zero ()
  532. {
  533. var text =
  534. "This is a test\nThis is a test\nThis is a test\nThis is a test\nThis is a test\nThis is a test";
  535. var label = new Label { Text = text };
  536. var top = new Toplevel ();
  537. top.Add (label);
  538. var sbv = new ScrollBarView (label, true, false) { Size = 100 };
  539. Application.Begin (top);
  540. Assert.Equal (100, sbv.Size);
  541. Assert.Null (sbv.OtherScrollBarView);
  542. Assert.True (sbv.ShowScrollIndicator);
  543. Assert.True (sbv.Visible);
  544. TestHelpers.AssertDriverContentsWithFrameAre (
  545. @"
  546. This is a tes▲
  547. This is a tes┬
  548. This is a tes┴
  549. This is a tes░
  550. This is a tes░
  551. This is a tes▼
  552. ",
  553. _output
  554. );
  555. sbv.Size = 0;
  556. Assert.Equal (0, sbv.Size);
  557. Assert.False (sbv.ShowScrollIndicator);
  558. Assert.False (sbv.Visible);
  559. top.Draw ();
  560. TestHelpers.AssertDriverContentsWithFrameAre (
  561. @"
  562. This is a test
  563. This is a test
  564. This is a test
  565. This is a test
  566. This is a test
  567. This is a test
  568. ",
  569. _output
  570. );
  571. top.Dispose ();
  572. }
  573. [Fact]
  574. [ScrollBarAutoInitShutdown]
  575. public void DrawContent_Update_The_ScrollBarView_Position ()
  576. {
  577. _scrollBar = new ScrollBarView (_hostView, true);
  578. Application.Begin (_hostView.SuperView as Toplevel);
  579. AddHandlers ();
  580. _hostView.Top = 3;
  581. _hostView.Draw ();
  582. Assert.Equal (_scrollBar.Position, _hostView.Top);
  583. _hostView.Left = 6;
  584. _hostView.Draw ();
  585. Assert.Equal (_scrollBar.OtherScrollBarView.Position, _hostView.Left);
  586. _hostView.SuperView.Dispose ();
  587. }
  588. [Fact]
  589. [AutoInitShutdown]
  590. public void Horizontal_Default_Draws_Correctly ()
  591. {
  592. var width = 40;
  593. var height = 3;
  594. var super = new Window { Id = "super", Width = Dim.Fill (), Height = Dim.Fill () };
  595. var top = new Toplevel ();
  596. top.Add (super);
  597. var sbv = new ScrollBarView { Id = "sbv", Size = width * 2, ShowScrollIndicator = true };
  598. super.Add (sbv);
  599. Application.Begin (top);
  600. ((FakeDriver)Application.Driver).SetBufferSize (width, height);
  601. var expected = @"
  602. ┌──────────────────────────────────────┐
  603. │◄├────────────────┤░░░░░░░░░░░░░░░░░░►│
  604. └──────────────────────────────────────┘";
  605. _ = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  606. top.Dispose ();
  607. }
  608. [Fact]
  609. [ScrollBarAutoInitShutdown]
  610. public void Hosting_A_Null_SuperView_View_To_A_ScrollBarView_Throws_ArgumentNullException ()
  611. {
  612. Assert.Throws<ArgumentNullException> (
  613. "The host SuperView parameter can't be null.",
  614. () => new ScrollBarView (new View (), true)
  615. );
  616. Assert.Throws<ArgumentNullException> (
  617. "The host SuperView parameter can't be null.",
  618. () => new ScrollBarView (new View (), false)
  619. );
  620. }
  621. [Fact]
  622. [ScrollBarAutoInitShutdown]
  623. public void Hosting_A_Null_View_To_A_ScrollBarView_Throws_ArgumentNullException ()
  624. {
  625. Assert.Throws<ArgumentNullException> (
  626. "The host parameter can't be null.",
  627. () => new ScrollBarView (null, true)
  628. );
  629. Assert.Throws<ArgumentNullException> (
  630. "The host parameter can't be null.",
  631. () => new ScrollBarView (null, false)
  632. );
  633. }
  634. [Fact]
  635. [ScrollBarAutoInitShutdown]
  636. public void Hosting_A_View_To_A_ScrollBarView ()
  637. {
  638. RemoveHandlers ();
  639. _scrollBar = new ScrollBarView (_hostView, true);
  640. Application.Begin (_hostView.SuperView as Toplevel);
  641. Assert.True (_scrollBar.IsVertical);
  642. Assert.False (_scrollBar.OtherScrollBarView.IsVertical);
  643. Assert.Equal (_scrollBar.Position, _hostView.Top);
  644. Assert.NotEqual (_scrollBar.Size, _hostView.Lines);
  645. Assert.Equal (_scrollBar.OtherScrollBarView.Position, _hostView.Left);
  646. Assert.NotEqual (_scrollBar.OtherScrollBarView.Size, _hostView.Cols);
  647. AddHandlers ();
  648. _hostView.SuperView.LayoutSubviews ();
  649. _hostView.Draw ();
  650. Assert.Equal (_scrollBar.Position, _hostView.Top);
  651. Assert.Equal (_scrollBar.Size, _hostView.Lines);
  652. Assert.Equal (_scrollBar.OtherScrollBarView.Position, _hostView.Left);
  653. Assert.Equal (_scrollBar.OtherScrollBarView.Size, _hostView.Cols);
  654. _hostView.SuperView.Dispose ();
  655. }
  656. [Fact]
  657. [AutoInitShutdown]
  658. public void Hosting_ShowBothScrollIndicator_Invisible ()
  659. {
  660. var textView = new TextView
  661. {
  662. Width = Dim.Fill (),
  663. Height = Dim.Fill (),
  664. Text =
  665. "This is the help text for the Second Step.\n\nPress the button to see a message box.\n\nEnter name too."
  666. };
  667. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  668. win.Add (textView);
  669. var scrollBar = new ScrollBarView (textView, true);
  670. scrollBar.ChangedPosition += (s, e) =>
  671. {
  672. textView.TopRow = scrollBar.Position;
  673. if (textView.TopRow != scrollBar.Position)
  674. {
  675. scrollBar.Position = textView.TopRow;
  676. }
  677. textView.SetNeedsDisplay ();
  678. };
  679. scrollBar.OtherScrollBarView.ChangedPosition += (s, e) =>
  680. {
  681. textView.LeftColumn = scrollBar.OtherScrollBarView.Position;
  682. if (textView.LeftColumn != scrollBar.OtherScrollBarView.Position)
  683. {
  684. scrollBar.OtherScrollBarView.Position = textView.LeftColumn;
  685. }
  686. textView.SetNeedsDisplay ();
  687. };
  688. textView.LayoutComplete += (s, e) =>
  689. {
  690. scrollBar.Size = textView.Lines;
  691. scrollBar.Position = textView.TopRow;
  692. if (scrollBar.OtherScrollBarView != null)
  693. {
  694. scrollBar.OtherScrollBarView.Size = textView.Maxlength;
  695. scrollBar.OtherScrollBarView.Position = textView.LeftColumn;
  696. }
  697. scrollBar.LayoutSubviews ();
  698. scrollBar.Refresh ();
  699. };
  700. var top = new Toplevel ();
  701. top.Add (win);
  702. Application.Begin (top);
  703. ((FakeDriver)Application.Driver).SetBufferSize (45, 20);
  704. Assert.True (scrollBar.AutoHideScrollBars);
  705. Assert.False (scrollBar.ShowScrollIndicator);
  706. Assert.False (scrollBar.OtherScrollBarView.ShowScrollIndicator);
  707. Assert.Equal (5, textView.Lines);
  708. Assert.Equal (42, textView.Maxlength);
  709. Assert.Equal (0, textView.LeftColumn);
  710. Assert.Equal (0, scrollBar.Position);
  711. Assert.Equal (0, scrollBar.OtherScrollBarView.Position);
  712. var expected = @"
  713. ┌───────────────────────────────────────────┐
  714. │This is the help text for the Second Step. │
  715. │ │
  716. │Press the button to see a message box. │
  717. │ │
  718. │Enter name too. │
  719. │ │
  720. │ │
  721. │ │
  722. │ │
  723. │ │
  724. │ │
  725. │ │
  726. │ │
  727. │ │
  728. │ │
  729. │ │
  730. │ │
  731. │ │
  732. └───────────────────────────────────────────┘
  733. ";
  734. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  735. Assert.Equal (new Rectangle (0, 0, 45, 20), pos);
  736. textView.WordWrap = true;
  737. ((FakeDriver)Application.Driver).SetBufferSize (26, 20);
  738. Application.Refresh ();
  739. Assert.True (textView.WordWrap);
  740. Assert.True (scrollBar.AutoHideScrollBars);
  741. Assert.Equal (7, textView.Lines);
  742. Assert.Equal (22, textView.Maxlength);
  743. Assert.Equal (0, textView.LeftColumn);
  744. Assert.Equal (0, scrollBar.Position);
  745. Assert.Equal (0, scrollBar.OtherScrollBarView.Position);
  746. expected = @"
  747. ┌────────────────────────┐
  748. │This is the help text │
  749. │for the Second Step. │
  750. │ │
  751. │Press the button to │
  752. │see a message box. │
  753. │ │
  754. │Enter name too. │
  755. │ │
  756. │ │
  757. │ │
  758. │ │
  759. │ │
  760. │ │
  761. │ │
  762. │ │
  763. │ │
  764. │ │
  765. │ │
  766. └────────────────────────┘
  767. ";
  768. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  769. Assert.Equal (new Rectangle (0, 0, 26, 20), pos);
  770. ((FakeDriver)Application.Driver).SetBufferSize (10, 10);
  771. Application.Refresh ();
  772. Assert.True (textView.WordWrap);
  773. Assert.True (scrollBar.AutoHideScrollBars);
  774. Assert.Equal (20, textView.Lines);
  775. Assert.Equal (7, textView.Maxlength);
  776. Assert.Equal (0, textView.LeftColumn);
  777. Assert.Equal (0, scrollBar.Position);
  778. Assert.Equal (0, scrollBar.OtherScrollBarView.Position);
  779. Assert.True (scrollBar.ShowScrollIndicator);
  780. expected = @"
  781. ┌────────┐
  782. │This ▲│
  783. │is the ┬│
  784. │help ││
  785. │text ┴│
  786. │for ░│
  787. │the ░│
  788. │Second ░│
  789. │Step. ▼│
  790. └────────┘
  791. ";
  792. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  793. Assert.Equal (new Rectangle (0, 0, 10, 10), pos);
  794. top.Dispose ();
  795. }
  796. [Fact]
  797. [ScrollBarAutoInitShutdown]
  798. public void Hosting_Two_Horizontal_ScrollBarView_Throws_ArgumentException ()
  799. {
  800. var top = new Toplevel ();
  801. var host = new View ();
  802. top.Add (host);
  803. var v = new ScrollBarView (host, false);
  804. var h = new ScrollBarView (host, false);
  805. Assert.Throws<ArgumentException> (() => v.OtherScrollBarView = h);
  806. Assert.Throws<ArgumentException> (() => h.OtherScrollBarView = v);
  807. top.Dispose ();
  808. }
  809. [Fact]
  810. [ScrollBarAutoInitShutdown]
  811. public void Hosting_Two_Vertical_ScrollBarView_Throws_ArgumentException ()
  812. {
  813. var top = new Toplevel ();
  814. var host = new View ();
  815. top.Add (host);
  816. var v = new ScrollBarView (host, true);
  817. var h = new ScrollBarView (host, true);
  818. Assert.Throws<ArgumentException> (() => v.OtherScrollBarView = h);
  819. Assert.Throws<ArgumentException> (() => h.OtherScrollBarView = v);
  820. top.Dispose ();
  821. }
  822. [Fact]
  823. [AutoInitShutdown]
  824. public void Internal_Tests ()
  825. {
  826. Toplevel top = new ();
  827. Assert.Equal (new Rectangle (0, 0, 80, 25), top.Viewport);
  828. var view = new View { Width = Dim.Fill (), Height = Dim.Fill () };
  829. top.Add (view);
  830. var sbv = new ScrollBarView (view, true);
  831. top.Add (sbv);
  832. Assert.Equal (view, sbv.Host);
  833. sbv.Size = 40;
  834. sbv.Position = 0;
  835. sbv.OtherScrollBarView.Size = 100;
  836. sbv.OtherScrollBarView.Position = 0;
  837. // Host bounds is not empty.
  838. Assert.True (sbv.CanScroll (10, out int max, sbv.IsVertical));
  839. Assert.Equal (10, max);
  840. Assert.True (sbv.OtherScrollBarView.CanScroll (10, out max, sbv.OtherScrollBarView.IsVertical));
  841. Assert.Equal (10, max);
  842. Application.Begin (top);
  843. // They are visible so they are drawn.
  844. Assert.True (sbv.Visible);
  845. Assert.True (sbv.OtherScrollBarView.Visible);
  846. top.LayoutSubviews ();
  847. // Now the host bounds is not empty.
  848. Assert.True (sbv.CanScroll (10, out max, sbv.IsVertical));
  849. Assert.Equal (10, max);
  850. Assert.True (sbv.OtherScrollBarView.CanScroll (10, out max, sbv.OtherScrollBarView.IsVertical));
  851. Assert.Equal (10, max);
  852. Assert.True (sbv.CanScroll (50, out max, sbv.IsVertical));
  853. Assert.Equal (40, sbv.Size);
  854. Assert.Equal (16, max); // 16+25=41
  855. Assert.True (sbv.OtherScrollBarView.CanScroll (150, out max, sbv.OtherScrollBarView.IsVertical));
  856. Assert.Equal (100, sbv.OtherScrollBarView.Size);
  857. Assert.Equal (21, max); // 21+80=101
  858. Assert.True (sbv.Visible);
  859. Assert.True (sbv.OtherScrollBarView.Visible);
  860. sbv.KeepContentAlwaysInViewport = false;
  861. sbv.OtherScrollBarView.KeepContentAlwaysInViewport = false;
  862. Assert.True (sbv.CanScroll (50, out max, sbv.IsVertical));
  863. Assert.Equal (39, max);
  864. Assert.True (sbv.OtherScrollBarView.CanScroll (150, out max, sbv.OtherScrollBarView.IsVertical));
  865. Assert.Equal (99, max);
  866. Assert.True (sbv.Visible);
  867. Assert.True (sbv.OtherScrollBarView.Visible);
  868. top.Dispose ();
  869. }
  870. [Fact]
  871. [ScrollBarAutoInitShutdown]
  872. public void KeepContentAlwaysInViewport_False ()
  873. {
  874. _scrollBar = new ScrollBarView (_hostView, true);
  875. Application.Begin (_hostView.SuperView as Toplevel);
  876. AddHandlers ();
  877. _hostView.SuperView.LayoutSubviews ();
  878. _hostView.Draw ();
  879. _scrollBar.KeepContentAlwaysInViewport = false;
  880. _scrollBar.Position = 50;
  881. Assert.Equal (_scrollBar.Position, _scrollBar.Size - 1);
  882. Assert.Equal (_scrollBar.Position, _hostView.Top);
  883. Assert.Equal (29, _scrollBar.Position);
  884. Assert.Equal (29, _hostView.Top);
  885. _scrollBar.OtherScrollBarView.Position = 150;
  886. Assert.Equal (_scrollBar.OtherScrollBarView.Position, _scrollBar.OtherScrollBarView.Size - 1);
  887. Assert.Equal (_scrollBar.OtherScrollBarView.Position, _hostView.Left);
  888. Assert.Equal (99, _scrollBar.OtherScrollBarView.Position);
  889. Assert.Equal (99, _hostView.Left);
  890. _hostView.SuperView.Dispose ();
  891. }
  892. [Fact]
  893. [ScrollBarAutoInitShutdown]
  894. public void KeepContentAlwaysInViewport_True ()
  895. {
  896. _scrollBar = new ScrollBarView (_hostView, true);
  897. Application.Begin (_hostView.SuperView as Toplevel);
  898. AddHandlers ();
  899. _hostView.SuperView.LayoutSubviews ();
  900. _hostView.Draw ();
  901. Assert.Equal (80, _hostView.Viewport.Width);
  902. Assert.Equal (25, _hostView.Viewport.Height);
  903. Assert.Equal (79, _scrollBar.OtherScrollBarView.Viewport.Width);
  904. Assert.Equal (24, _scrollBar.Viewport.Height);
  905. Assert.Equal (30, _scrollBar.Size);
  906. Assert.Equal (100, _scrollBar.OtherScrollBarView.Size);
  907. Assert.True (_scrollBar.ShowScrollIndicator);
  908. Assert.True (_scrollBar.OtherScrollBarView.ShowScrollIndicator);
  909. Assert.True (_scrollBar.Visible);
  910. Assert.True (_scrollBar.OtherScrollBarView.Visible);
  911. _scrollBar.Position = 50;
  912. Assert.Equal (_scrollBar.Position, _scrollBar.Size - _scrollBar.Viewport.Height);
  913. Assert.Equal (_scrollBar.Position, _hostView.Top);
  914. Assert.Equal (6, _scrollBar.Position);
  915. Assert.Equal (6, _hostView.Top);
  916. Assert.True (_scrollBar.ShowScrollIndicator);
  917. Assert.True (_scrollBar.OtherScrollBarView.ShowScrollIndicator);
  918. Assert.True (_scrollBar.Visible);
  919. Assert.True (_scrollBar.OtherScrollBarView.Visible);
  920. _scrollBar.OtherScrollBarView.Position = 150;
  921. Assert.Equal (
  922. _scrollBar.OtherScrollBarView.Position,
  923. _scrollBar.OtherScrollBarView.Size - _scrollBar.OtherScrollBarView.Viewport.Width
  924. );
  925. Assert.Equal (_scrollBar.OtherScrollBarView.Position, _hostView.Left);
  926. Assert.Equal (21, _scrollBar.OtherScrollBarView.Position);
  927. Assert.Equal (21, _hostView.Left);
  928. Assert.True (_scrollBar.ShowScrollIndicator);
  929. Assert.True (_scrollBar.OtherScrollBarView.ShowScrollIndicator);
  930. Assert.True (_scrollBar.Visible);
  931. Assert.True (_scrollBar.OtherScrollBarView.Visible);
  932. _hostView.SuperView.Dispose ();
  933. }
  934. [Fact]
  935. [ScrollBarAutoInitShutdown]
  936. public void OtherScrollBarView_Not_Null ()
  937. {
  938. _scrollBar = new ScrollBarView (_hostView, true);
  939. Application.Begin (_hostView.SuperView as Toplevel);
  940. AddHandlers ();
  941. Assert.NotNull (_scrollBar.OtherScrollBarView);
  942. Assert.NotEqual (_scrollBar, _scrollBar.OtherScrollBarView);
  943. Assert.Equal (_scrollBar.OtherScrollBarView.OtherScrollBarView, _scrollBar);
  944. _hostView.SuperView.Dispose ();
  945. }
  946. [Fact]
  947. [ScrollBarAutoInitShutdown]
  948. public void Scrolling_With_Default_Constructor_Do_Not_Scroll ()
  949. {
  950. var sbv = new ScrollBarView { Position = 1 };
  951. Assert.Equal (1, sbv.Position);
  952. Assert.NotEqual (0, sbv.Position);
  953. sbv.Dispose ();
  954. }
  955. [Fact]
  956. [ScrollBarAutoInitShutdown]
  957. public void ShowScrollIndicator_Check ()
  958. {
  959. _scrollBar = new ScrollBarView (_hostView, true);
  960. Application.Begin (_hostView.SuperView as Toplevel);
  961. AddHandlers ();
  962. _hostView.SuperView.LayoutSubviews ();
  963. _hostView.Draw ();
  964. Assert.True (_scrollBar.ShowScrollIndicator);
  965. Assert.True (_scrollBar.OtherScrollBarView.ShowScrollIndicator);
  966. _hostView.SuperView.Dispose ();
  967. }
  968. [Fact]
  969. [AutoInitShutdown]
  970. public void ShowScrollIndicator_False_Must_Also_Set_Visible_To_False_To_Not_Respond_To_Events ()
  971. {
  972. var clicked = false;
  973. var text = "This is a test\nThis is a test\nThis is a test\nThis is a test\nThis is a test";
  974. var label = new Label { Width = 14, Height = 5, Text = text };
  975. var btn = new Button { X = 14, Text = "Click Me!" };
  976. btn.Accept += (s, e) => clicked = true;
  977. var top = new Toplevel ();
  978. top.Add (label, btn);
  979. var sbv = new ScrollBarView (label, true, false) { Size = 5 };
  980. Application.Begin (top);
  981. Assert.Equal (5, sbv.Size);
  982. Assert.Null (sbv.OtherScrollBarView);
  983. Assert.False (sbv.ShowScrollIndicator);
  984. Assert.False (sbv.Visible);
  985. TestHelpers.AssertDriverContentsWithFrameAre (
  986. @$"
  987. This is a test{
  988. CM.Glyphs.LeftBracket
  989. } Click Me! {
  990. CM.Glyphs.RightBracket
  991. }
  992. This is a test
  993. This is a test
  994. This is a test
  995. This is a test ",
  996. _output
  997. );
  998. Application.OnMouseEvent (new MouseEvent { Position = new (15, 0), Flags = MouseFlags.Button1Clicked });
  999. Assert.Null (Application.MouseGrabView);
  1000. Assert.True (clicked);
  1001. clicked = false;
  1002. sbv.Visible = true;
  1003. Assert.Equal (5, sbv.Size);
  1004. Assert.False (sbv.ShowScrollIndicator);
  1005. Assert.True (sbv.Visible);
  1006. top.Draw ();
  1007. Assert.False (sbv.Visible);
  1008. TestHelpers.AssertDriverContentsWithFrameAre (
  1009. @$"
  1010. This is a test{
  1011. CM.Glyphs.LeftBracket
  1012. } Click Me! {
  1013. CM.Glyphs.RightBracket
  1014. }
  1015. This is a test
  1016. This is a test
  1017. This is a test
  1018. This is a test ",
  1019. _output
  1020. );
  1021. Application.OnMouseEvent (new MouseEvent { Position = new (15, 0), Flags = MouseFlags.Button1Clicked });
  1022. Assert.Null (Application.MouseGrabView);
  1023. Assert.True (clicked);
  1024. Assert.Equal (5, sbv.Size);
  1025. Assert.False (sbv.ShowScrollIndicator);
  1026. Assert.False (sbv.Visible);
  1027. top.Dispose ();
  1028. }
  1029. [Fact]
  1030. [AutoInitShutdown]
  1031. public void Vertical_Default_Draws_Correctly ()
  1032. {
  1033. var width = 3;
  1034. var height = 40;
  1035. var super = new Window { Id = "super", Width = Dim.Fill (), Height = Dim.Fill () };
  1036. var top = new Toplevel ();
  1037. top.Add (super);
  1038. var sbv = new ScrollBarView
  1039. {
  1040. Id = "sbv",
  1041. Size = height * 2,
  1042. // BUGBUG: ScrollBarView should work if Host is null
  1043. Host = super,
  1044. ShowScrollIndicator = true,
  1045. IsVertical = true
  1046. };
  1047. super.Add (sbv);
  1048. Application.Begin (top);
  1049. ((FakeDriver)Application.Driver).SetBufferSize (width, height);
  1050. var expected = @"
  1051. ┌─┐
  1052. │▲│
  1053. │┬│
  1054. │││
  1055. │││
  1056. │││
  1057. │││
  1058. │││
  1059. │││
  1060. │││
  1061. │││
  1062. │││
  1063. │││
  1064. │││
  1065. │││
  1066. │││
  1067. │││
  1068. │││
  1069. │││
  1070. │┴│
  1071. │░│
  1072. │░│
  1073. │░│
  1074. │░│
  1075. │░│
  1076. │░│
  1077. │░│
  1078. │░│
  1079. │░│
  1080. │░│
  1081. │░│
  1082. │░│
  1083. │░│
  1084. │░│
  1085. │░│
  1086. │░│
  1087. │░│
  1088. │░│
  1089. │▼│
  1090. └─┘";
  1091. _ = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1092. top.Dispose ();
  1093. }
  1094. private void _hostView_DrawContent (object sender, DrawEventArgs e)
  1095. {
  1096. _scrollBar.Size = _hostView.Lines;
  1097. _scrollBar.Position = _hostView.Top;
  1098. _scrollBar.OtherScrollBarView.Size = _hostView.Cols;
  1099. _scrollBar.OtherScrollBarView.Position = _hostView.Left;
  1100. _scrollBar.Refresh ();
  1101. }
  1102. private void _scrollBar_ChangedPosition (object sender, EventArgs e)
  1103. {
  1104. _hostView.Top = _scrollBar.Position;
  1105. if (_hostView.Top != _scrollBar.Position)
  1106. {
  1107. _scrollBar.Position = _hostView.Top;
  1108. }
  1109. _hostView.SetNeedsDisplay ();
  1110. }
  1111. private void _scrollBar_OtherScrollBarView_ChangedPosition (object sender, EventArgs e)
  1112. {
  1113. _hostView.Left = _scrollBar.OtherScrollBarView.Position;
  1114. if (_hostView.Left != _scrollBar.OtherScrollBarView.Position)
  1115. {
  1116. _scrollBar.OtherScrollBarView.Position = _hostView.Left;
  1117. }
  1118. _hostView.SetNeedsDisplay ();
  1119. }
  1120. private void AddHandlers ()
  1121. {
  1122. if (!_added)
  1123. {
  1124. _hostView.DrawContent += _hostView_DrawContent;
  1125. _scrollBar.ChangedPosition += _scrollBar_ChangedPosition;
  1126. _scrollBar.OtherScrollBarView.ChangedPosition += _scrollBar_OtherScrollBarView_ChangedPosition;
  1127. }
  1128. _added = true;
  1129. }
  1130. private void RemoveHandlers ()
  1131. {
  1132. if (_added)
  1133. {
  1134. _hostView.DrawContent -= _hostView_DrawContent;
  1135. _scrollBar.ChangedPosition -= _scrollBar_ChangedPosition;
  1136. _scrollBar.OtherScrollBarView.ChangedPosition -= _scrollBar_OtherScrollBarView_ChangedPosition;
  1137. }
  1138. _added = false;
  1139. }
  1140. public class HostView : View
  1141. {
  1142. public int Cols { get; set; }
  1143. public int Left { get; set; }
  1144. public int Lines { get; set; }
  1145. public int Top { get; set; }
  1146. }
  1147. // This class enables test functions annotated with the [InitShutdown] attribute
  1148. // to have a function called before the test function is called and after.
  1149. //
  1150. // This is necessary because a) Application is a singleton and Init/Shutdown must be called
  1151. // as a pair, and b) all unit test functions should be atomic.
  1152. [AttributeUsage (AttributeTargets.Class | AttributeTargets.Method)]
  1153. public class ScrollBarAutoInitShutdownAttribute : AutoInitShutdownAttribute
  1154. {
  1155. public override void After (MethodInfo methodUnderTest)
  1156. {
  1157. _hostView = null;
  1158. base.After (methodUnderTest);
  1159. }
  1160. public override void Before (MethodInfo methodUnderTest)
  1161. {
  1162. base.Before (methodUnderTest);
  1163. _hostView = new HostView
  1164. {
  1165. Width = Dim.Fill (),
  1166. Height = Dim.Fill (),
  1167. Top = 0,
  1168. Lines = 30,
  1169. Left = 0,
  1170. Cols = 100
  1171. };
  1172. var top = new Toplevel ();
  1173. top.Add (_hostView);
  1174. }
  1175. }
  1176. }