ScrollBarViewTests.cs 52 KB

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