ScrollBarViewTests.cs 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374
  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. var clicked = false;
  980. var text = "This is a test\nThis is a test\nThis is a test\nThis is a test\nThis is a test";
  981. var label = new Label { Width = 14, Height = 5, Text = text };
  982. var btn = new Button { X = 14, Text = "Click Me!" };
  983. btn.Accept += (s, e) => clicked = true;
  984. var top = new Toplevel ();
  985. top.Add (label, btn);
  986. var sbv = new ScrollBarView (label, true, false) { Size = 5 };
  987. Application.Begin (top);
  988. Assert.Equal (5, sbv.Size);
  989. Assert.Null (sbv.OtherScrollBarView);
  990. Assert.False (sbv.ShowScrollIndicator);
  991. Assert.False (sbv.Visible);
  992. TestHelpers.AssertDriverContentsWithFrameAre (
  993. @$"
  994. This is a test{CM.Glyphs.LeftBracket} Click Me! {CM.Glyphs.RightBracket}
  995. This is a test
  996. This is a test
  997. This is a test
  998. This is a test ",
  999. _output
  1000. );
  1001. Application.OnMouseEvent (new MouseEvent { Position = new (15, 0), Flags = MouseFlags.Button1Clicked });
  1002. Assert.Null (Application.MouseGrabView);
  1003. Assert.True (clicked);
  1004. clicked = false;
  1005. sbv.Visible = true;
  1006. Assert.Equal (5, sbv.Size);
  1007. Assert.False (sbv.ShowScrollIndicator);
  1008. Assert.True (sbv.Visible);
  1009. top.Draw ();
  1010. Assert.False (sbv.Visible);
  1011. TestHelpers.AssertDriverContentsWithFrameAre (
  1012. @$"
  1013. This is a test{CM.Glyphs.LeftBracket} Click Me! {CM.Glyphs.RightBracket}
  1014. This is a test
  1015. This is a test
  1016. This is a test
  1017. This is a test ",
  1018. _output
  1019. );
  1020. Application.OnMouseEvent (new MouseEvent { Position = new (15, 0), Flags = MouseFlags.Button1Clicked });
  1021. Assert.Null (Application.MouseGrabView);
  1022. Assert.True (clicked);
  1023. Assert.Equal (5, sbv.Size);
  1024. Assert.False (sbv.ShowScrollIndicator);
  1025. Assert.False (sbv.Visible);
  1026. top.Dispose ();
  1027. }
  1028. [Fact]
  1029. [AutoInitShutdown]
  1030. public void Vertical_Default_Draws_Correctly ()
  1031. {
  1032. var width = 3;
  1033. var height = 40;
  1034. var super = new Window { Id = "super", Width = Dim.Fill (), Height = Dim.Fill () };
  1035. var top = new Toplevel ();
  1036. top.Add (super);
  1037. var sbv = new ScrollBarView
  1038. {
  1039. Id = "sbv",
  1040. Size = height * 2,
  1041. // BUGBUG: ScrollBarView should work if Host is null
  1042. Host = super,
  1043. ShowScrollIndicator = true,
  1044. IsVertical = true
  1045. };
  1046. super.Add (sbv);
  1047. Application.Begin (top);
  1048. ((FakeDriver)Application.Driver!).SetBufferSize (width, height);
  1049. var expected = @"
  1050. ┌─┐
  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. _ = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1091. top.Dispose ();
  1092. }
  1093. private void _hostView_DrawContent (object sender, DrawEventArgs e)
  1094. {
  1095. _scrollBar.Size = _hostView.Lines;
  1096. _scrollBar.Position = _hostView.Top;
  1097. _scrollBar.OtherScrollBarView.Size = _hostView.Cols;
  1098. _scrollBar.OtherScrollBarView.Position = _hostView.Left;
  1099. _scrollBar.Refresh ();
  1100. }
  1101. private void _scrollBar_ChangedPosition (object sender, EventArgs e)
  1102. {
  1103. _hostView.Top = _scrollBar.Position;
  1104. if (_hostView.Top != _scrollBar.Position)
  1105. {
  1106. _scrollBar.Position = _hostView.Top;
  1107. }
  1108. _hostView.SetNeedsDisplay ();
  1109. }
  1110. private void _scrollBar_OtherScrollBarView_ChangedPosition (object sender, EventArgs e)
  1111. {
  1112. _hostView.Left = _scrollBar.OtherScrollBarView.Position;
  1113. if (_hostView.Left != _scrollBar.OtherScrollBarView.Position)
  1114. {
  1115. _scrollBar.OtherScrollBarView.Position = _hostView.Left;
  1116. }
  1117. _hostView.SetNeedsDisplay ();
  1118. }
  1119. private void AddHandlers ()
  1120. {
  1121. if (!_added)
  1122. {
  1123. _hostView.DrawContent += _hostView_DrawContent;
  1124. _scrollBar.ChangedPosition += _scrollBar_ChangedPosition;
  1125. _scrollBar.OtherScrollBarView.ChangedPosition += _scrollBar_OtherScrollBarView_ChangedPosition;
  1126. }
  1127. _added = true;
  1128. }
  1129. private void RemoveHandlers ()
  1130. {
  1131. if (_added)
  1132. {
  1133. _hostView.DrawContent -= _hostView_DrawContent;
  1134. _scrollBar.ChangedPosition -= _scrollBar_ChangedPosition;
  1135. _scrollBar.OtherScrollBarView.ChangedPosition -= _scrollBar_OtherScrollBarView_ChangedPosition;
  1136. }
  1137. _added = false;
  1138. }
  1139. public class HostView : View
  1140. {
  1141. public int Cols { get; set; }
  1142. public int Left { get; set; }
  1143. public int Lines { get; set; }
  1144. public int Top { get; set; }
  1145. }
  1146. // This class enables test functions annotated with the [InitShutdown] attribute
  1147. // to have a function called before the test function is called and after.
  1148. //
  1149. // This is necessary because a) Application is a singleton and Init/Shutdown must be called
  1150. // as a pair, and b) all unit test functions should be atomic.
  1151. [AttributeUsage (AttributeTargets.Class | AttributeTargets.Method)]
  1152. public class ScrollBarAutoInitShutdownAttribute : AutoInitShutdownAttribute
  1153. {
  1154. public override void After (MethodInfo methodUnderTest)
  1155. {
  1156. _hostView = null;
  1157. base.After (methodUnderTest);
  1158. }
  1159. public override void Before (MethodInfo methodUnderTest)
  1160. {
  1161. base.Before (methodUnderTest);
  1162. _hostView = new HostView
  1163. {
  1164. Width = Dim.Fill (),
  1165. Height = Dim.Fill (),
  1166. Top = 0,
  1167. Lines = 30,
  1168. Left = 0,
  1169. Cols = 100
  1170. };
  1171. var top = new Toplevel ();
  1172. top.Add (_hostView);
  1173. }
  1174. }
  1175. }