ScrollBarViewTests.cs 51 KB

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