ScrollBarViewTests.cs 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377
  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. RunState rs = 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. Application.RunIteration (ref rs);
  656. Assert.Equal (_scrollBar.Position, _hostView.Top);
  657. Assert.Equal (_scrollBar.Size, _hostView.Lines);
  658. Assert.Equal (_scrollBar.OtherScrollBarView.Position, _hostView.Left);
  659. Assert.Equal (_scrollBar.OtherScrollBarView.Size, _hostView.Cols);
  660. _hostView.SuperView.Dispose ();
  661. }
  662. [Fact]
  663. [AutoInitShutdown]
  664. public void Hosting_ShowBothScrollIndicator_Invisible ()
  665. {
  666. var textView = new TextView
  667. {
  668. Width = Dim.Fill (),
  669. Height = Dim.Fill (),
  670. Text =
  671. "This is the help text for the Second Step.\n\nPress the button to see a message box.\n\nEnter name too."
  672. };
  673. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  674. win.Add (textView);
  675. var scrollBar = new ScrollBarView (textView, true);
  676. scrollBar.ChangedPosition += (s, e) =>
  677. {
  678. textView.TopRow = scrollBar.Position;
  679. if (textView.TopRow != scrollBar.Position)
  680. {
  681. scrollBar.Position = textView.TopRow;
  682. }
  683. textView.SetNeedsDisplay ();
  684. };
  685. scrollBar.OtherScrollBarView.ChangedPosition += (s, e) =>
  686. {
  687. textView.LeftColumn = scrollBar.OtherScrollBarView.Position;
  688. if (textView.LeftColumn != scrollBar.OtherScrollBarView.Position)
  689. {
  690. scrollBar.OtherScrollBarView.Position = textView.LeftColumn;
  691. }
  692. textView.SetNeedsDisplay ();
  693. };
  694. textView.LayoutComplete += (s, e) =>
  695. {
  696. scrollBar.Size = textView.Lines;
  697. scrollBar.Position = textView.TopRow;
  698. if (scrollBar.OtherScrollBarView != null)
  699. {
  700. scrollBar.OtherScrollBarView.Size = textView.Maxlength;
  701. scrollBar.OtherScrollBarView.Position = textView.LeftColumn;
  702. }
  703. };
  704. var top = new Toplevel ();
  705. top.Add (win);
  706. RunState rs = Application.Begin (top);
  707. ((FakeDriver)Application.Driver!).SetBufferSize (45, 20);
  708. Assert.True (scrollBar.AutoHideScrollBars);
  709. Assert.False (scrollBar.ShowScrollIndicator);
  710. Assert.False (scrollBar.OtherScrollBarView.ShowScrollIndicator);
  711. Assert.Equal (5, textView.Lines);
  712. Assert.Equal (42, textView.Maxlength);
  713. Assert.Equal (0, textView.LeftColumn);
  714. Assert.Equal (0, scrollBar.Position);
  715. Assert.Equal (0, scrollBar.OtherScrollBarView.Position);
  716. var expected = @"
  717. ┌───────────────────────────────────────────┐
  718. │This is the help text for the Second Step. │
  719. │ │
  720. │Press the button to see a message box. │
  721. │ │
  722. │Enter name too. │
  723. │ │
  724. │ │
  725. │ │
  726. │ │
  727. │ │
  728. │ │
  729. │ │
  730. │ │
  731. │ │
  732. │ │
  733. │ │
  734. │ │
  735. │ │
  736. └───────────────────────────────────────────┘
  737. ";
  738. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  739. Assert.Equal (new Rectangle (0, 0, 45, 20), pos);
  740. textView.WordWrap = true;
  741. ((FakeDriver)Application.Driver!).SetBufferSize (26, 20);
  742. Application.RunIteration (ref rs);
  743. Assert.True (textView.WordWrap);
  744. Assert.True (scrollBar.AutoHideScrollBars);
  745. Assert.Equal (7, textView.Lines);
  746. Assert.Equal (22, textView.Maxlength);
  747. Assert.Equal (0, textView.LeftColumn);
  748. Assert.Equal (0, scrollBar.Position);
  749. Assert.Equal (0, scrollBar.OtherScrollBarView.Position);
  750. expected = @"
  751. ┌────────────────────────┐
  752. │This is the help text │
  753. │for the Second Step. │
  754. │ │
  755. │Press the button to │
  756. │see a message box. │
  757. │ │
  758. │Enter name too. │
  759. │ │
  760. │ │
  761. │ │
  762. │ │
  763. │ │
  764. │ │
  765. │ │
  766. │ │
  767. │ │
  768. │ │
  769. │ │
  770. └────────────────────────┘
  771. ";
  772. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  773. Assert.Equal (new Rectangle (0, 0, 26, 20), pos);
  774. ((FakeDriver)Application.Driver!).SetBufferSize (10, 10);
  775. Application.RunIteration (ref rs);
  776. Assert.True (textView.WordWrap);
  777. Assert.True (scrollBar.AutoHideScrollBars);
  778. Assert.Equal (20, textView.Lines);
  779. Assert.Equal (7, textView.Maxlength);
  780. Assert.Equal (0, textView.LeftColumn);
  781. Assert.Equal (0, scrollBar.Position);
  782. Assert.Equal (0, scrollBar.OtherScrollBarView.Position);
  783. Assert.True (scrollBar.ShowScrollIndicator);
  784. expected = @"
  785. ┌────────┐
  786. │This ▲│
  787. │is the ┬│
  788. │help ││
  789. │text ┴│
  790. │for ░│
  791. │the ░│
  792. │Second ░│
  793. │Step. ▼│
  794. └────────┘
  795. ";
  796. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  797. Assert.Equal (new Rectangle (0, 0, 10, 10), pos);
  798. top.Dispose ();
  799. }
  800. [Fact]
  801. [ScrollBarAutoInitShutdown]
  802. public void Hosting_Two_Horizontal_ScrollBarView_Throws_ArgumentException ()
  803. {
  804. var top = new Toplevel ();
  805. var host = new View ();
  806. top.Add (host);
  807. var v = new ScrollBarView (host, false);
  808. var h = new ScrollBarView (host, false);
  809. Assert.Throws<ArgumentException> (() => v.OtherScrollBarView = h);
  810. Assert.Throws<ArgumentException> (() => h.OtherScrollBarView = v);
  811. top.Dispose ();
  812. }
  813. [Fact]
  814. [ScrollBarAutoInitShutdown]
  815. public void Hosting_Two_Vertical_ScrollBarView_Throws_ArgumentException ()
  816. {
  817. var top = new Toplevel ();
  818. var host = new View ();
  819. top.Add (host);
  820. var v = new ScrollBarView (host, true);
  821. var h = new ScrollBarView (host, true);
  822. Assert.Throws<ArgumentException> (() => v.OtherScrollBarView = h);
  823. Assert.Throws<ArgumentException> (() => h.OtherScrollBarView = v);
  824. top.Dispose ();
  825. }
  826. [Fact]
  827. [AutoInitShutdown]
  828. public void Internal_Tests ()
  829. {
  830. Toplevel top = new ();
  831. top.Layout ();
  832. Assert.Equal (new Rectangle (0, 0, 80, 25), top.Viewport);
  833. var view = new View { Width = Dim.Fill (), Height = Dim.Fill () };
  834. top.Add (view);
  835. var sbv = new ScrollBarView (view, true);
  836. top.Add (sbv);
  837. Assert.Equal (view, sbv.Host);
  838. sbv.Size = 40;
  839. sbv.Position = 0;
  840. sbv.OtherScrollBarView.Size = 100;
  841. sbv.OtherScrollBarView.Position = 0;
  842. top.Layout ();
  843. // Host bounds is not empty.
  844. Assert.True (sbv.CanScroll (10, out int max, sbv.IsVertical));
  845. Assert.Equal (10, max);
  846. Assert.True (sbv.OtherScrollBarView.CanScroll (10, out max, sbv.OtherScrollBarView.IsVertical));
  847. Assert.Equal (10, max);
  848. Application.Begin (top);
  849. // They are visible so they are drawn.
  850. Assert.True (sbv.Visible);
  851. Assert.True (sbv.OtherScrollBarView.Visible);
  852. top.LayoutSubviews ();
  853. // Now the host bounds is not empty.
  854. Assert.True (sbv.CanScroll (10, out max, sbv.IsVertical));
  855. Assert.Equal (10, max);
  856. Assert.True (sbv.OtherScrollBarView.CanScroll (10, out max, sbv.OtherScrollBarView.IsVertical));
  857. Assert.Equal (10, max);
  858. Assert.True (sbv.CanScroll (50, out max, sbv.IsVertical));
  859. Assert.Equal (40, sbv.Size);
  860. Assert.Equal (16, max); // 16+25=41
  861. Assert.True (sbv.OtherScrollBarView.CanScroll (150, out max, sbv.OtherScrollBarView.IsVertical));
  862. Assert.Equal (100, sbv.OtherScrollBarView.Size);
  863. Assert.Equal (21, max); // 21+80=101
  864. Assert.True (sbv.Visible);
  865. Assert.True (sbv.OtherScrollBarView.Visible);
  866. sbv.KeepContentAlwaysInViewport = false;
  867. sbv.OtherScrollBarView.KeepContentAlwaysInViewport = false;
  868. Assert.True (sbv.CanScroll (50, out max, sbv.IsVertical));
  869. Assert.Equal (39, max);
  870. Assert.True (sbv.OtherScrollBarView.CanScroll (150, out max, sbv.OtherScrollBarView.IsVertical));
  871. Assert.Equal (99, max);
  872. Assert.True (sbv.Visible);
  873. Assert.True (sbv.OtherScrollBarView.Visible);
  874. top.Dispose ();
  875. }
  876. [Fact]
  877. [ScrollBarAutoInitShutdown]
  878. public void KeepContentAlwaysInViewport_False ()
  879. {
  880. _scrollBar = new ScrollBarView (_hostView, true);
  881. RunState rs = Application.Begin (_hostView.SuperView as Toplevel);
  882. AddHandlers ();
  883. Application.RunIteration (ref rs);
  884. _scrollBar.KeepContentAlwaysInViewport = false;
  885. _scrollBar.Position = 50;
  886. Assert.Equal (_scrollBar.Position, _scrollBar.Size - 1);
  887. Assert.Equal (_scrollBar.Position, _hostView.Top);
  888. Assert.Equal (29, _scrollBar.Position);
  889. Assert.Equal (29, _hostView.Top);
  890. _scrollBar.OtherScrollBarView.Position = 150;
  891. Assert.Equal (_scrollBar.OtherScrollBarView.Position, _scrollBar.OtherScrollBarView.Size - 1);
  892. Assert.Equal (_scrollBar.OtherScrollBarView.Position, _hostView.Left);
  893. Assert.Equal (99, _scrollBar.OtherScrollBarView.Position);
  894. Assert.Equal (99, _hostView.Left);
  895. _hostView.SuperView.Dispose ();
  896. }
  897. [Fact]
  898. [ScrollBarAutoInitShutdown]
  899. public void KeepContentAlwaysInViewport_True ()
  900. {
  901. _scrollBar = new ScrollBarView (_hostView, true);
  902. RunState rs = Application.Begin (_hostView.SuperView as Toplevel);
  903. AddHandlers ();
  904. Application.RunIteration (ref rs);
  905. Assert.Equal (80, _hostView.Viewport.Width);
  906. Assert.Equal (25, _hostView.Viewport.Height);
  907. Assert.Equal (79, _scrollBar.OtherScrollBarView.Viewport.Width);
  908. Assert.Equal (24, _scrollBar.Viewport.Height);
  909. Assert.Equal (30, _scrollBar.Size);
  910. Assert.Equal (100, _scrollBar.OtherScrollBarView.Size);
  911. Assert.True (_scrollBar.ShowScrollIndicator);
  912. Assert.True (_scrollBar.OtherScrollBarView.ShowScrollIndicator);
  913. Assert.True (_scrollBar.Visible);
  914. Assert.True (_scrollBar.OtherScrollBarView.Visible);
  915. _scrollBar.Position = 50;
  916. Assert.Equal (_scrollBar.Position, _scrollBar.Size - _scrollBar.Viewport.Height);
  917. Assert.Equal (_scrollBar.Position, _hostView.Top);
  918. Assert.Equal (6, _scrollBar.Position);
  919. Assert.Equal (6, _hostView.Top);
  920. Assert.True (_scrollBar.ShowScrollIndicator);
  921. Assert.True (_scrollBar.OtherScrollBarView.ShowScrollIndicator);
  922. Assert.True (_scrollBar.Visible);
  923. Assert.True (_scrollBar.OtherScrollBarView.Visible);
  924. _scrollBar.OtherScrollBarView.Position = 150;
  925. Assert.Equal (
  926. _scrollBar.OtherScrollBarView.Position,
  927. _scrollBar.OtherScrollBarView.Size - _scrollBar.OtherScrollBarView.Viewport.Width
  928. );
  929. Assert.Equal (_scrollBar.OtherScrollBarView.Position, _hostView.Left);
  930. Assert.Equal (21, _scrollBar.OtherScrollBarView.Position);
  931. Assert.Equal (21, _hostView.Left);
  932. Assert.True (_scrollBar.ShowScrollIndicator);
  933. Assert.True (_scrollBar.OtherScrollBarView.ShowScrollIndicator);
  934. Assert.True (_scrollBar.Visible);
  935. Assert.True (_scrollBar.OtherScrollBarView.Visible);
  936. _hostView.SuperView.Dispose ();
  937. }
  938. [Fact]
  939. [ScrollBarAutoInitShutdown]
  940. public void OtherScrollBarView_Not_Null ()
  941. {
  942. _scrollBar = new ScrollBarView (_hostView, true);
  943. Application.Begin (_hostView.SuperView as Toplevel);
  944. AddHandlers ();
  945. Assert.NotNull (_scrollBar.OtherScrollBarView);
  946. Assert.NotEqual (_scrollBar, _scrollBar.OtherScrollBarView);
  947. Assert.Equal (_scrollBar.OtherScrollBarView.OtherScrollBarView, _scrollBar);
  948. _hostView.SuperView.Dispose ();
  949. }
  950. [Fact]
  951. [ScrollBarAutoInitShutdown]
  952. public void Scrolling_With_Default_Constructor_Do_Not_Scroll ()
  953. {
  954. var sbv = new ScrollBarView { Position = 1 };
  955. Assert.Equal (1, sbv.Position);
  956. Assert.NotEqual (0, sbv.Position);
  957. sbv.Dispose ();
  958. }
  959. [Fact]
  960. [ScrollBarAutoInitShutdown]
  961. public void ShowScrollIndicator_Check ()
  962. {
  963. _scrollBar = new ScrollBarView (_hostView, true);
  964. RunState rs = Application.Begin (_hostView.SuperView as Toplevel);
  965. AddHandlers ();
  966. Application.RunIteration (ref rs);
  967. Assert.True (_scrollBar.ShowScrollIndicator);
  968. Assert.True (_scrollBar.OtherScrollBarView.ShowScrollIndicator);
  969. _hostView.SuperView.Dispose ();
  970. }
  971. [Fact]
  972. [AutoInitShutdown]
  973. public void ShowScrollIndicator_False_Must_Also_Set_Visible_To_False_To_Not_Respond_To_Events ()
  974. {
  975. // Override CM
  976. Window.DefaultBorderStyle = LineStyle.Single;
  977. Dialog.DefaultButtonAlignment = Alignment.Center;
  978. Dialog.DefaultBorderStyle = LineStyle.Single;
  979. Dialog.DefaultShadow = ShadowStyle.None;
  980. Button.DefaultShadow = ShadowStyle.None;
  981. var clicked = false;
  982. var text = "This is a test\nThis is a test\nThis is a test\nThis is a test\nThis is a test";
  983. var label = new Label { Width = 14, Height = 5, Text = text };
  984. var btn = new Button { X = 14, Text = "Click Me!" };
  985. btn.Accepting += (s, e) => clicked = true;
  986. var top = new Toplevel ();
  987. top.Add (label, btn);
  988. var sbv = new ScrollBarView (label, true, false) { Size = 5 };
  989. Application.Begin (top);
  990. Assert.Equal (5, sbv.Size);
  991. Assert.Null (sbv.OtherScrollBarView);
  992. Assert.False (sbv.ShowScrollIndicator);
  993. Assert.False (sbv.Visible);
  994. TestHelpers.AssertDriverContentsWithFrameAre (
  995. @$"
  996. This is a test{CM.Glyphs.LeftBracket} Click Me! {CM.Glyphs.RightBracket}
  997. This is a test
  998. This is a test
  999. This is a test
  1000. This is a test ",
  1001. _output
  1002. );
  1003. Application.RaiseMouseEvent (new MouseEventArgs { Position = new (15, 0), Flags = MouseFlags.Button1Clicked });
  1004. Assert.Null (Application.MouseGrabView);
  1005. Assert.True (clicked);
  1006. clicked = false;
  1007. sbv.Visible = true;
  1008. Assert.Equal (5, sbv.Size);
  1009. Assert.False (sbv.ShowScrollIndicator);
  1010. Assert.True (sbv.Visible);
  1011. top.Draw ();
  1012. Assert.False (sbv.Visible);
  1013. TestHelpers.AssertDriverContentsWithFrameAre (
  1014. @$"
  1015. This is a test{CM.Glyphs.LeftBracket} Click Me! {CM.Glyphs.RightBracket}
  1016. This is a test
  1017. This is a test
  1018. This is a test
  1019. This is a test ",
  1020. _output
  1021. );
  1022. Application.RaiseMouseEvent (new MouseEventArgs { Position = new (15, 0), Flags = MouseFlags.Button1Clicked });
  1023. Assert.Null (Application.MouseGrabView);
  1024. Assert.True (clicked);
  1025. Assert.Equal (5, sbv.Size);
  1026. Assert.False (sbv.ShowScrollIndicator);
  1027. Assert.False (sbv.Visible);
  1028. top.Dispose ();
  1029. }
  1030. [Fact]
  1031. [AutoInitShutdown]
  1032. public void Vertical_Default_Draws_Correctly ()
  1033. {
  1034. var width = 3;
  1035. var height = 40;
  1036. var super = new Window { Id = "super", Width = Dim.Fill (), Height = Dim.Fill () };
  1037. var top = new Toplevel ();
  1038. top.Add (super);
  1039. var sbv = new ScrollBarView
  1040. {
  1041. Id = "sbv",
  1042. Size = height * 2,
  1043. // BUGBUG: ScrollBarView should work if Host is null
  1044. Host = super,
  1045. ShowScrollIndicator = true,
  1046. IsVertical = true
  1047. };
  1048. super.Add (sbv);
  1049. Application.Begin (top);
  1050. ((FakeDriver)Application.Driver!).SetBufferSize (width, height);
  1051. var expected = @"
  1052. ┌─┐
  1053. │▲│
  1054. │┬│
  1055. │││
  1056. │││
  1057. │││
  1058. │││
  1059. │││
  1060. │││
  1061. │││
  1062. │││
  1063. │││
  1064. │││
  1065. │││
  1066. │││
  1067. │││
  1068. │││
  1069. │││
  1070. │││
  1071. │┴│
  1072. │░│
  1073. │░│
  1074. │░│
  1075. │░│
  1076. │░│
  1077. │░│
  1078. │░│
  1079. │░│
  1080. │░│
  1081. │░│
  1082. │░│
  1083. │░│
  1084. │░│
  1085. │░│
  1086. │░│
  1087. │░│
  1088. │░│
  1089. │░│
  1090. │▼│
  1091. └─┘";
  1092. _ = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1093. top.Dispose ();
  1094. }
  1095. private void _hostView_DrawContent (object sender, DrawEventArgs e)
  1096. {
  1097. _scrollBar.Size = _hostView.Lines;
  1098. _scrollBar.Position = _hostView.Top;
  1099. _scrollBar.OtherScrollBarView.Size = _hostView.Cols;
  1100. _scrollBar.OtherScrollBarView.Position = _hostView.Left;
  1101. _scrollBar.Refresh ();
  1102. }
  1103. private void _scrollBar_ChangedPosition (object sender, EventArgs e)
  1104. {
  1105. _hostView.Top = _scrollBar.Position;
  1106. if (_hostView.Top != _scrollBar.Position)
  1107. {
  1108. _scrollBar.Position = _hostView.Top;
  1109. }
  1110. _hostView.SetNeedsDisplay ();
  1111. }
  1112. private void _scrollBar_OtherScrollBarView_ChangedPosition (object sender, EventArgs e)
  1113. {
  1114. _hostView.Left = _scrollBar.OtherScrollBarView.Position;
  1115. if (_hostView.Left != _scrollBar.OtherScrollBarView.Position)
  1116. {
  1117. _scrollBar.OtherScrollBarView.Position = _hostView.Left;
  1118. }
  1119. _hostView.SetNeedsDisplay ();
  1120. }
  1121. private void AddHandlers ()
  1122. {
  1123. if (!_added)
  1124. {
  1125. _hostView.DrawContent += _hostView_DrawContent;
  1126. _scrollBar.ChangedPosition += _scrollBar_ChangedPosition;
  1127. _scrollBar.OtherScrollBarView.ChangedPosition += _scrollBar_OtherScrollBarView_ChangedPosition;
  1128. }
  1129. _added = true;
  1130. }
  1131. private void RemoveHandlers ()
  1132. {
  1133. if (_added)
  1134. {
  1135. _hostView.DrawContent -= _hostView_DrawContent;
  1136. _scrollBar.ChangedPosition -= _scrollBar_ChangedPosition;
  1137. _scrollBar.OtherScrollBarView.ChangedPosition -= _scrollBar_OtherScrollBarView_ChangedPosition;
  1138. }
  1139. _added = false;
  1140. }
  1141. public class HostView : View
  1142. {
  1143. public int Cols { get; set; }
  1144. public int Left { get; set; }
  1145. public int Lines { get; set; }
  1146. public int Top { get; set; }
  1147. }
  1148. // This class enables test functions annotated with the [InitShutdown] attribute
  1149. // to have a function called before the test function is called and after.
  1150. //
  1151. // This is necessary because a) Application is a singleton and Init/Shutdown must be called
  1152. // as a pair, and b) all unit test functions should be atomic.
  1153. [AttributeUsage (AttributeTargets.Class | AttributeTargets.Method)]
  1154. public class ScrollBarAutoInitShutdownAttribute : AutoInitShutdownAttribute
  1155. {
  1156. public override void After (MethodInfo methodUnderTest)
  1157. {
  1158. _hostView = null;
  1159. base.After (methodUnderTest);
  1160. }
  1161. public override void Before (MethodInfo methodUnderTest)
  1162. {
  1163. base.Before (methodUnderTest);
  1164. _hostView = new HostView
  1165. {
  1166. Width = Dim.Fill (),
  1167. Height = Dim.Fill (),
  1168. Top = 0,
  1169. Lines = 30,
  1170. Left = 0,
  1171. Cols = 100
  1172. };
  1173. var top = new Toplevel ();
  1174. top.Add (_hostView);
  1175. }
  1176. }
  1177. }