ScrollBarViewTests.cs 52 KB

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