ScrollBarViewTests.cs 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376
  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 ClearOnVisibleFalse_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, ClearOnVisibleFalse = false };
  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.ClearOnVisibleFalse = true;
  304. sbv.Visible = false;
  305. Assert.False (sbv.Visible);
  306. TestHelpers.AssertDriverContentsWithFrameAre (
  307. @"
  308. This is a tes
  309. This is a tes
  310. This is a tes
  311. This is a tes
  312. This is a tes
  313. This is a tes
  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]
  971. public void ShowScrollIndicator_False_Must_Also_Set_Visible_To_False_To_Not_Respond_To_Events ()
  972. {
  973. var clicked = false;
  974. var text = "This is a test\nThis is a test\nThis is a test\nThis is a test\nThis is a test";
  975. var label = new Label { Width = 14, Height = 5, Text = text };
  976. var btn = new Button { X = 14, Text = "Click Me!" };
  977. btn.Accept += (s, e) => clicked = true;
  978. var top = new Toplevel ();
  979. top.Add (label, btn);
  980. var sbv = new ScrollBarView (label, true, false) { Size = 5 };
  981. Application.Begin (top);
  982. Assert.Equal (5, sbv.Size);
  983. Assert.Null (sbv.OtherScrollBarView);
  984. Assert.False (sbv.ShowScrollIndicator);
  985. Assert.False (sbv.Visible);
  986. TestHelpers.AssertDriverContentsWithFrameAre (
  987. @$"
  988. This is a test{
  989. CM.Glyphs.LeftBracket
  990. } Click Me! {
  991. CM.Glyphs.RightBracket
  992. }
  993. This is a test
  994. This is a test
  995. This is a test
  996. This is a test ",
  997. _output
  998. );
  999. Application.OnMouseEvent (new MouseEvent { Position = new (15, 0), Flags = MouseFlags.Button1Clicked });
  1000. Assert.Null (Application.MouseGrabView);
  1001. Assert.True (clicked);
  1002. clicked = false;
  1003. sbv.Visible = true;
  1004. Assert.Equal (5, sbv.Size);
  1005. Assert.False (sbv.ShowScrollIndicator);
  1006. Assert.True (sbv.Visible);
  1007. top.Draw ();
  1008. Assert.False (sbv.Visible);
  1009. TestHelpers.AssertDriverContentsWithFrameAre (
  1010. @$"
  1011. This is a test{
  1012. CM.Glyphs.LeftBracket
  1013. } Click Me! {
  1014. CM.Glyphs.RightBracket
  1015. }
  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.OnMouseEvent (new MouseEvent { 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. }