ScrollBarTests.cs 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150
  1. using Xunit.Abstractions;
  2. namespace Terminal.Gui.ViewsTests;
  3. public class ScrollBarTests
  4. {
  5. public ScrollBarTests (ITestOutputHelper output) { _output = output; }
  6. private readonly ITestOutputHelper _output;
  7. [Fact]
  8. [AutoInitShutdown]
  9. public void AutoHideScrollBar_CheckScrollBarVisibility ()
  10. {
  11. var scrollBar = new ScrollBar { Width = 2, Height = Dim.Fill (), Size = 30 };
  12. View scrollBarSuperView = ScrollBarSuperView ();
  13. scrollBarSuperView.Add (scrollBar);
  14. Application.Begin ((scrollBarSuperView.SuperView as Toplevel)!);
  15. Assert.Equal (Orientation.Vertical, scrollBar.Orientation);
  16. Assert.True (scrollBar.ShowScrollIndicator);
  17. Assert.True (scrollBar.Visible);
  18. Assert.Equal ("Absolute(2)", scrollBar.Width!.ToString ());
  19. Assert.Equal (2, scrollBar.Viewport.Width);
  20. Assert.Equal ("Fill(Absolute(0))", scrollBar.Height!.ToString ());
  21. Assert.Equal (25, scrollBar.Viewport.Height);
  22. scrollBar.Size = 10;
  23. Assert.False (scrollBar.ShowScrollIndicator);
  24. Assert.False (scrollBar.Visible);
  25. scrollBar.Size = 30;
  26. Assert.True (scrollBar.ShowScrollIndicator);
  27. Assert.True (scrollBar.Visible);
  28. scrollBar.AutoHide = false;
  29. Assert.True (scrollBar.ShowScrollIndicator);
  30. Assert.True (scrollBar.Visible);
  31. scrollBar.Size = 10;
  32. Assert.True (scrollBar.ShowScrollIndicator);
  33. Assert.True (scrollBar.Visible);
  34. scrollBarSuperView.SuperView!.Dispose ();
  35. }
  36. [Theory]
  37. [AutoInitShutdown]
  38. [InlineData (
  39. 20,
  40. @"
  41. ▼",
  42. @"
  43. ▼",
  44. @"
  45. ▼",
  46. @"
  47. ▼",
  48. @"
  49. ◄████░░░░►",
  50. @"
  51. ◄░░████░░►",
  52. @"
  53. ◄░░░░████►",
  54. @"
  55. ◄░░██░░░░►")]
  56. [InlineData (
  57. 40,
  58. @"
  59. ▼",
  60. @"
  61. ▼",
  62. @"
  63. ▼",
  64. @"
  65. ▼",
  66. @"
  67. ◄██░░░░░░►",
  68. @"
  69. ◄░██░░░░░►",
  70. @"
  71. ◄░░██░░░░►",
  72. @"
  73. ◄░█░░░░░░►")]
  74. public void Changing_Position_Size_Orientation_Draws_Correctly_KeepContentInAllViewport_True (
  75. int size,
  76. string firstVertExpected,
  77. string middleVertExpected,
  78. string endVertExpected,
  79. string sizeVertExpected,
  80. string firstHoriExpected,
  81. string middleHoriExpected,
  82. string endHoriExpected,
  83. string sizeHoriExpected
  84. )
  85. {
  86. var scrollBar = new ScrollBar
  87. {
  88. Orientation = Orientation.Vertical,
  89. Size = size,
  90. Height = 10,
  91. KeepContentInAllViewport = true
  92. };
  93. var top = new Toplevel ();
  94. top.Add (scrollBar);
  95. Application.Begin (top);
  96. _ = TestHelpers.AssertDriverContentsWithFrameAre (firstVertExpected, _output);
  97. scrollBar.Position = 4;
  98. Application.Refresh ();
  99. _ = TestHelpers.AssertDriverContentsWithFrameAre (middleVertExpected, _output);
  100. scrollBar.Position = 10;
  101. Application.Refresh ();
  102. _ = TestHelpers.AssertDriverContentsWithFrameAre (endVertExpected, _output);
  103. scrollBar.Size = size * 2;
  104. Application.Refresh ();
  105. _ = TestHelpers.AssertDriverContentsWithFrameAre (sizeVertExpected, _output);
  106. scrollBar.Orientation = Orientation.Horizontal;
  107. scrollBar.Width = 10;
  108. scrollBar.Height = 1;
  109. scrollBar.Position = 0;
  110. scrollBar.Size = size;
  111. Application.Refresh ();
  112. _ = TestHelpers.AssertDriverContentsWithFrameAre (firstHoriExpected, _output);
  113. scrollBar.Position = 4;
  114. Application.Refresh ();
  115. _ = TestHelpers.AssertDriverContentsWithFrameAre (middleHoriExpected, _output);
  116. scrollBar.Position = 10;
  117. Application.Refresh ();
  118. _ = TestHelpers.AssertDriverContentsWithFrameAre (endHoriExpected, _output);
  119. scrollBar.Size = size * 2;
  120. Application.Refresh ();
  121. _ = TestHelpers.AssertDriverContentsWithFrameAre (sizeHoriExpected, _output);
  122. }
  123. [Fact]
  124. public void Constructor_Defaults ()
  125. {
  126. var scrollBar = new ScrollBar ();
  127. Assert.False (scrollBar.CanFocus);
  128. Assert.Equal (Orientation.Vertical, scrollBar.Orientation);
  129. Assert.Equal (0, scrollBar.Size);
  130. Assert.Equal (0, scrollBar.Position);
  131. Assert.Equal ("Auto(Content,Absolute(1),)", scrollBar.Width!.ToString ());
  132. Assert.Equal ("Auto(Content,Absolute(1),)", scrollBar.Height!.ToString ());
  133. Assert.True (scrollBar.ShowScrollIndicator);
  134. Assert.True (scrollBar.AutoHide);
  135. }
  136. [Fact]
  137. [AutoInitShutdown]
  138. public void KeepContentInAllViewport_True_False ()
  139. {
  140. var view = new View { Width = Dim.Fill (), Height = Dim.Fill () };
  141. view.Padding.Thickness = new (0, 0, 2, 0);
  142. view.SetContentSize (new (view.Viewport.Width, 30));
  143. var scrollBar = new ScrollBar { Width = 2, Height = Dim.Fill (), Size = view.GetContentSize ().Height };
  144. scrollBar.PositionChanged += (_, e) => view.Viewport = view.Viewport with { Y = e.CurrentValue };
  145. view.Padding.Add (scrollBar);
  146. var top = new Toplevel ();
  147. top.Add (view);
  148. Application.Begin (top);
  149. Assert.False (scrollBar.KeepContentInAllViewport);
  150. scrollBar.KeepContentInAllViewport = true;
  151. Assert.Equal (80, view.Padding.Viewport.Width);
  152. Assert.Equal (25, view.Padding.Viewport.Height);
  153. Assert.Equal (2, scrollBar.Viewport.Width);
  154. Assert.Equal (25, scrollBar.Viewport.Height);
  155. Assert.Equal (30, scrollBar.Size);
  156. scrollBar.KeepContentInAllViewport = false;
  157. scrollBar.Position = 50;
  158. Assert.Equal (scrollBar.Position, scrollBar.Size - 1);
  159. Assert.Equal (scrollBar.Position, view.Viewport.Y);
  160. Assert.Equal (29, scrollBar.Position);
  161. Assert.Equal (29, view.Viewport.Y);
  162. top.Dispose ();
  163. }
  164. [Theory]
  165. [AutoInitShutdown]
  166. [InlineData (
  167. Orientation.Vertical,
  168. 20,
  169. 10,
  170. 4,
  171. @"
  172. ▼",
  173. 2,
  174. @"
  175. ▼")]
  176. [InlineData (
  177. Orientation.Vertical,
  178. 40,
  179. 10,
  180. 5,
  181. @"
  182. ▼",
  183. 18,
  184. @"
  185. ▼")]
  186. [InlineData (
  187. Orientation.Horizontal,
  188. 20,
  189. 10,
  190. 4,
  191. @"
  192. ◄░░░░████►",
  193. 2,
  194. @"
  195. ◄░████░░░►")]
  196. [InlineData (
  197. Orientation.Horizontal,
  198. 40,
  199. 10,
  200. 5,
  201. @"
  202. ◄░░██░░░░►",
  203. 18,
  204. @"
  205. ◄░░░░██░░►")]
  206. public void Mouse_On_The_Container_KeepContentInAllViewport_True (Orientation orientation, int size, int position, int location, string output, int expectedPos, string expectedOut)
  207. {
  208. var scrollBar = new ScrollBar
  209. {
  210. Width = orientation == Orientation.Vertical ? 1 : 10,
  211. Height = orientation == Orientation.Vertical ? 10 : 1,
  212. Orientation = orientation, Size = size,
  213. Position = position,
  214. KeepContentInAllViewport = true
  215. };
  216. var top = new Toplevel ();
  217. top.Add (scrollBar);
  218. Application.Begin (top);
  219. _ = TestHelpers.AssertDriverContentsWithFrameAre (output, _output);
  220. Application.OnMouseEvent (
  221. new ()
  222. {
  223. Position = orientation == Orientation.Vertical ? new (0, location) : new Point (location, 0),
  224. Flags = MouseFlags.Button1Pressed
  225. });
  226. Assert.Equal (expectedPos, scrollBar.Position);
  227. Application.Refresh ();
  228. _ = TestHelpers.AssertDriverContentsWithFrameAre (expectedOut, _output);
  229. }
  230. [Theory]
  231. [AutoInitShutdown]
  232. [InlineData (
  233. Orientation.Vertical,
  234. 20,
  235. 10,
  236. 5,
  237. 5,
  238. @"
  239. ▼",
  240. MouseFlags.Button1Pressed,
  241. 10,
  242. @"
  243. ▼")]
  244. [InlineData (
  245. Orientation.Vertical,
  246. 40,
  247. 10,
  248. 3,
  249. 3,
  250. @"
  251. ▼",
  252. MouseFlags.Button1Pressed,
  253. 10,
  254. @"
  255. ▼")]
  256. [InlineData (
  257. Orientation.Horizontal,
  258. 20,
  259. 10,
  260. 5,
  261. 5,
  262. @"
  263. ◄░░░░████►",
  264. MouseFlags.Button1Pressed,
  265. 10,
  266. @"
  267. ◄░░░░████►")]
  268. [InlineData (
  269. Orientation.Horizontal,
  270. 40,
  271. 10,
  272. 3,
  273. 3,
  274. @"
  275. ◄░░██░░░░►",
  276. MouseFlags.Button1Pressed,
  277. 10,
  278. @"
  279. ◄░░██░░░░►")]
  280. [InlineData (
  281. Orientation.Vertical,
  282. 20,
  283. 10,
  284. 5,
  285. 7,
  286. @"
  287. ▼",
  288. MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition,
  289. 10,
  290. @"
  291. ▼")]
  292. [InlineData (
  293. Orientation.Horizontal,
  294. 20,
  295. 10,
  296. 5,
  297. 4,
  298. @"
  299. ◄░░░░████►",
  300. MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition,
  301. 8,
  302. @"
  303. ◄░░░████░►")]
  304. [InlineData (
  305. Orientation.Vertical,
  306. 20,
  307. 10,
  308. 5,
  309. 6,
  310. @"
  311. ▼",
  312. MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition,
  313. 10,
  314. @"
  315. ▼")]
  316. [InlineData (
  317. Orientation.Horizontal,
  318. 20,
  319. 10,
  320. 5,
  321. 6,
  322. @"
  323. ◄░░░░████►",
  324. MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition,
  325. 10,
  326. @"
  327. ◄░░░░████►")]
  328. [InlineData (
  329. Orientation.Vertical,
  330. 40,
  331. 10,
  332. 2,
  333. 1,
  334. @"
  335. ▼",
  336. MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition,
  337. 2,
  338. @"
  339. ▼")]
  340. [InlineData (
  341. Orientation.Horizontal,
  342. 40,
  343. 10,
  344. 2,
  345. 1,
  346. @"
  347. ◄░░██░░░░►",
  348. MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition,
  349. 2,
  350. @"
  351. ◄██░░░░░░►")]
  352. [InlineData (
  353. Orientation.Vertical,
  354. 40,
  355. 10,
  356. 3,
  357. 4,
  358. @"
  359. ▼",
  360. MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition,
  361. 15,
  362. @"
  363. ▼")]
  364. [InlineData (
  365. Orientation.Horizontal,
  366. 40,
  367. 10,
  368. 3,
  369. 4,
  370. @"
  371. ◄░░██░░░░►",
  372. MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition,
  373. 15,
  374. @"
  375. ◄░░░██░░░►")]
  376. [InlineData (
  377. Orientation.Vertical,
  378. 40,
  379. 10,
  380. 3,
  381. 3,
  382. @"
  383. ▼",
  384. MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition,
  385. 10,
  386. @"
  387. ▼")]
  388. [InlineData (
  389. Orientation.Horizontal,
  390. 40,
  391. 10,
  392. 3,
  393. 3,
  394. @"
  395. ◄░░██░░░░►",
  396. MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition,
  397. 10,
  398. @"
  399. ◄░░██░░░░►")]
  400. [InlineData (
  401. Orientation.Vertical,
  402. 40,
  403. 10,
  404. 3,
  405. 5,
  406. @"
  407. ▼",
  408. MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition,
  409. 20,
  410. @"
  411. ▼")]
  412. [InlineData (
  413. Orientation.Horizontal,
  414. 40,
  415. 10,
  416. 3,
  417. 5,
  418. @"
  419. ◄░░██░░░░►",
  420. MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition,
  421. 20,
  422. @"
  423. ◄░░░░██░░►")]
  424. public void Mouse_On_The_Slider_KeepContentInAllViewport_True (
  425. Orientation orientation,
  426. int size,
  427. int position,
  428. int startLocation,
  429. int endLocation,
  430. string output,
  431. MouseFlags mouseFlags,
  432. int expectedPos,
  433. string expectedOut
  434. )
  435. {
  436. var scrollBar = new ScrollBar
  437. {
  438. Width = orientation == Orientation.Vertical ? 1 : 10,
  439. Height = orientation == Orientation.Vertical ? 10 : 1,
  440. Orientation = orientation,
  441. Size = size, Position = position,
  442. KeepContentInAllViewport = true
  443. };
  444. var top = new Toplevel ();
  445. top.Add (scrollBar);
  446. Application.Begin (top);
  447. _ = TestHelpers.AssertDriverContentsWithFrameAre (output, _output);
  448. Assert.Null (Application.MouseGrabView);
  449. if (mouseFlags.HasFlag (MouseFlags.ReportMousePosition))
  450. {
  451. MouseFlags mf = mouseFlags & ~MouseFlags.ReportMousePosition;
  452. Application.OnMouseEvent (
  453. new ()
  454. {
  455. Position = orientation == Orientation.Vertical ? new (0, startLocation) : new (startLocation, 0),
  456. Flags = mf
  457. });
  458. Application.OnMouseEvent (
  459. new ()
  460. {
  461. Position = orientation == Orientation.Vertical ? new (0, endLocation) : new (endLocation, 0),
  462. Flags = mouseFlags
  463. });
  464. }
  465. else
  466. {
  467. Assert.Equal (startLocation, endLocation);
  468. Application.OnMouseEvent (
  469. new ()
  470. {
  471. Position = orientation == Orientation.Vertical ? new (0, startLocation) : new (startLocation, 0),
  472. Flags = mouseFlags
  473. });
  474. }
  475. Assert.Equal ("scrollSlider", Application.MouseGrabView?.Id);
  476. Assert.IsType<ScrollSlider> (Application.MouseGrabView);
  477. Assert.Equal (expectedPos, scrollBar.Position);
  478. Application.Refresh ();
  479. _ = TestHelpers.AssertDriverContentsWithFrameAre (expectedOut, _output);
  480. Application.OnMouseEvent (
  481. new ()
  482. {
  483. Position = orientation == Orientation.Vertical ? new (0, startLocation) : new (startLocation, 0),
  484. Flags = MouseFlags.Button1Released
  485. });
  486. Assert.Null (Application.MouseGrabView);
  487. }
  488. [Theory]
  489. [AutoInitShutdown]
  490. [InlineData (Orientation.Vertical)]
  491. [InlineData (Orientation.Horizontal)]
  492. public void Mouse_Pressed_On_ScrollButton_Changes_Position_KeepContentInAllViewport_True (Orientation orientation)
  493. {
  494. var scrollBar = new ScrollBar
  495. {
  496. X = 10, Y = 10, Width = orientation == Orientation.Vertical ? 1 : 10, Height = orientation == Orientation.Vertical ? 10 : 1, Size = 20,
  497. Orientation = orientation, KeepContentInAllViewport = true
  498. };
  499. var top = new Toplevel ();
  500. top.Add (scrollBar);
  501. Application.Begin (top);
  502. var scroll = (Scroll)scrollBar.Subviews.FirstOrDefault (x => x is Scroll);
  503. Rectangle scrollSliderFrame = scroll!.Subviews.FirstOrDefault (x => x is ScrollSlider)!.Frame;
  504. Assert.Equal (scrollSliderFrame, orientation == Orientation.Vertical ? new (0, 0, 1, 4) : new (0, 0, 4, 1));
  505. Assert.Equal (0, scrollBar.Position);
  506. // ScrollButton increase
  507. for (var i = 0; i < 11; i++)
  508. {
  509. Application.OnMouseEvent (
  510. new ()
  511. {
  512. Position = orientation == Orientation.Vertical ? new (10, 19) : new (19, 10), Flags = MouseFlags.Button1Pressed
  513. });
  514. if (i < 10)
  515. {
  516. Assert.Equal (i + 1, scrollBar.Position);
  517. }
  518. else
  519. {
  520. Assert.Equal (i, scrollBar.Position);
  521. Assert.Equal (
  522. orientation == Orientation.Vertical ? new (0, 4) : new (4, 0),
  523. scroll.Subviews.FirstOrDefault (x => x is ScrollSlider)!.Frame.Location);
  524. }
  525. }
  526. for (var i = 10; i > -1; i--)
  527. {
  528. Application.OnMouseEvent (new () { Position = new (10, 10), Flags = MouseFlags.Button1Pressed });
  529. if (i > 0)
  530. {
  531. Assert.Equal (i - 1, scrollBar.Position);
  532. }
  533. else
  534. {
  535. Assert.Equal (0, scrollBar.Position);
  536. Assert.Equal (new (0, 0), scroll.Subviews.FirstOrDefault (x => x is ScrollSlider)!.Frame.Location);
  537. }
  538. }
  539. }
  540. [Theory]
  541. [AutoInitShutdown]
  542. [InlineData (Orientation.Vertical)]
  543. [InlineData (Orientation.Horizontal)]
  544. public void Moving_Mouse_Outside_Host_Ensures_Correct_Location_KeepContentInAllViewport_True (Orientation orientation)
  545. {
  546. var scrollBar = new ScrollBar
  547. {
  548. X = 10, Y = 10, Width = orientation == Orientation.Vertical ? 1 : 10, Height = orientation == Orientation.Vertical ? 10 : 1, Size = 20,
  549. Position = 5, Orientation = orientation, KeepContentInAllViewport = true
  550. };
  551. var top = new Toplevel ();
  552. top.Add (scrollBar);
  553. Application.Begin (top);
  554. var scroll = (Scroll)scrollBar.Subviews.FirstOrDefault (x => x is Scroll);
  555. Rectangle scrollSliderFrame = scroll!.Subviews.FirstOrDefault (x => x is ScrollSlider)!.Frame;
  556. Assert.Equal (scrollSliderFrame, orientation == Orientation.Vertical ? new (0, 2, 1, 4) : new (2, 0, 4, 1));
  557. Application.OnMouseEvent (new () { Position = orientation == Orientation.Vertical ? new (10, 14) : new (14, 10), Flags = MouseFlags.Button1Pressed });
  558. Application.OnMouseEvent (
  559. new ()
  560. {
  561. Position = orientation == Orientation.Vertical ? new (10, 0) : new (0, 10),
  562. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  563. });
  564. Assert.Equal (new (0, 0), scroll.Subviews.FirstOrDefault (x => x is ScrollSlider)!.Frame.Location);
  565. Application.OnMouseEvent (
  566. new ()
  567. {
  568. Position = orientation == Orientation.Vertical ? new (0, 25) : new (80, 0),
  569. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  570. });
  571. Assert.Equal (
  572. orientation == Orientation.Vertical ? new (0, 4) : new (4, 0),
  573. scroll.Subviews.FirstOrDefault (x => x is ScrollSlider)!.Frame.Location);
  574. }
  575. [Theory]
  576. [InlineData (Orientation.Vertical, 20, 10, 10)]
  577. [InlineData (Orientation.Vertical, 40, 30, 30)]
  578. public void Position_Cannot_Be_Negative_Nor_Greater_Than_Size_Minus_Frame_Length_KeepContentInAllViewport_True (Orientation orientation, int size, int expectedPos1, int expectedPos2)
  579. {
  580. var scrollBar = new ScrollBar { Orientation = orientation, Height = 10, Size = size, KeepContentInAllViewport = true };
  581. Assert.Equal (0, scrollBar.Position);
  582. scrollBar.Position = -1;
  583. Assert.Equal (0, scrollBar.Position);
  584. scrollBar.Position = size;
  585. Assert.Equal (expectedPos1, scrollBar.Position);
  586. scrollBar.Position = expectedPos2;
  587. Assert.Equal (expectedPos2, scrollBar.Position);
  588. }
  589. [Fact]
  590. public void PositionChanging_Cancelable_And_PositionChanged_Events ()
  591. {
  592. var changingCount = 0;
  593. var changedCount = 0;
  594. var scrollBar = new ScrollBar { Size = 10 };
  595. scrollBar.PositionChanging += (s, e) =>
  596. {
  597. if (changingCount == 0)
  598. {
  599. e.Cancel = true;
  600. }
  601. changingCount++;
  602. };
  603. scrollBar.PositionChanged += (s, e) => changedCount++;
  604. scrollBar.Position = 1;
  605. Assert.Equal (0, scrollBar.Position);
  606. Assert.Equal (1, changingCount);
  607. Assert.Equal (0, changedCount);
  608. scrollBar.Position = 1;
  609. Assert.Equal (1, scrollBar.Position);
  610. Assert.Equal (2, changingCount);
  611. Assert.Equal (1, changedCount);
  612. }
  613. [Fact]
  614. public void PositionChanging_PositionChanged_Events_Only_Raises_Once_If_Position_Was_Really_Changed_KeepContentInAllViewport_True ()
  615. {
  616. var changing = 0;
  617. var cancel = false;
  618. var changed = 0;
  619. var scrollBar = new ScrollBar { Height = 10, Size = 20, KeepContentInAllViewport = true };
  620. scrollBar.PositionChanging += Scroll_PositionChanging;
  621. scrollBar.PositionChanged += Scroll_PositionChanged;
  622. Assert.Equal (Orientation.Vertical, scrollBar.Orientation);
  623. Assert.Equal (new (0, 0, 1, 10), scrollBar.Viewport);
  624. Assert.Equal (0, scrollBar.Position);
  625. Assert.Equal (0, changing);
  626. Assert.Equal (0, changed);
  627. scrollBar.Position = 0;
  628. Assert.Equal (0, scrollBar.Position);
  629. Assert.Equal (0, changing);
  630. Assert.Equal (0, changed);
  631. scrollBar.Position = 1;
  632. Assert.Equal (1, scrollBar.Position);
  633. Assert.Equal (1, changing);
  634. Assert.Equal (1, changed);
  635. Reset ();
  636. cancel = true;
  637. scrollBar.Position = 2;
  638. Assert.Equal (1, scrollBar.Position);
  639. Assert.Equal (1, changing);
  640. Assert.Equal (0, changed);
  641. Reset ();
  642. scrollBar.Position = 10;
  643. Assert.Equal (10, scrollBar.Position);
  644. Assert.Equal (1, changing);
  645. Assert.Equal (1, changed);
  646. Reset ();
  647. scrollBar.Position = 11;
  648. Assert.Equal (10, scrollBar.Position);
  649. Assert.Equal (0, changing);
  650. Assert.Equal (0, changed);
  651. Reset ();
  652. scrollBar.Position = 12;
  653. Assert.Equal (10, scrollBar.Position);
  654. Assert.Equal (0, changing);
  655. Assert.Equal (0, changed);
  656. Reset ();
  657. scrollBar.Position = 13;
  658. Assert.Equal (10, scrollBar.Position);
  659. Assert.Equal (0, changing);
  660. Assert.Equal (0, changed);
  661. Reset ();
  662. scrollBar.Position = 0;
  663. Assert.Equal (0, scrollBar.Position);
  664. Assert.Equal (1, changing);
  665. Assert.Equal (1, changed);
  666. scrollBar.PositionChanging -= Scroll_PositionChanging;
  667. scrollBar.PositionChanged -= Scroll_PositionChanged;
  668. void Scroll_PositionChanging (object sender, CancelEventArgs<int> e)
  669. {
  670. changing++;
  671. e.Cancel = cancel;
  672. }
  673. void Scroll_PositionChanged (object sender, EventArgs<int> e) { changed++; }
  674. void Reset ()
  675. {
  676. changing = 0;
  677. cancel = false;
  678. changed = 0;
  679. }
  680. }
  681. [Fact]
  682. [AutoInitShutdown]
  683. public void ShowScrollIndicator_CheckScrollBarVisibility ()
  684. {
  685. var scrollBar = new ScrollBar { Width = 2, Height = Dim.Fill (), Size = 30 };
  686. View scrollBarSuperView = ScrollBarSuperView ();
  687. scrollBarSuperView.Add (scrollBar);
  688. Application.Begin ((scrollBarSuperView.SuperView as Toplevel)!);
  689. Assert.True (scrollBar.ShowScrollIndicator);
  690. Assert.True (scrollBar.Visible);
  691. scrollBar.ShowScrollIndicator = false;
  692. Assert.True (scrollBar.AutoHide);
  693. Assert.True (scrollBar.ShowScrollIndicator);
  694. Assert.True (scrollBar.Visible);
  695. scrollBar.AutoHide = false;
  696. Assert.False (scrollBar.ShowScrollIndicator);
  697. Assert.False (scrollBar.Visible);
  698. scrollBarSuperView.SuperView!.Dispose ();
  699. }
  700. [Fact]
  701. public void Size_Cannot_Be_Negative ()
  702. {
  703. var scrollBar = new ScrollBar { Height = 10, Size = -1 };
  704. Assert.Equal (0, scrollBar.Size);
  705. scrollBar.Size = -10;
  706. Assert.Equal (0, scrollBar.Size);
  707. }
  708. [Fact]
  709. public void SizeChanged_Event ()
  710. {
  711. var count = 0;
  712. var scrollBar = new ScrollBar ();
  713. scrollBar.SizeChanged += (s, e) => count++;
  714. scrollBar.Size = 10;
  715. Assert.Equal (10, scrollBar.Size);
  716. Assert.Equal (1, count);
  717. }
  718. [Theory]
  719. [AutoInitShutdown]
  720. [InlineData (
  721. 3,
  722. 10,
  723. 1,
  724. Orientation.Vertical,
  725. @"
  726. ┌─┐
  727. │▲│
  728. │█│
  729. │█│
  730. │░│
  731. │░│
  732. │░│
  733. │░│
  734. │▼│
  735. └─┘")]
  736. [InlineData (
  737. 10,
  738. 3,
  739. 1,
  740. Orientation.Horizontal,
  741. @"
  742. ┌────────┐
  743. │◄██░░░░►│
  744. └────────┘")]
  745. [InlineData (
  746. 3,
  747. 10,
  748. 3,
  749. Orientation.Vertical,
  750. @"
  751. ┌───┐
  752. │ ▲ │
  753. │███│
  754. │███│
  755. │░░░│
  756. │░░░│
  757. │░░░│
  758. │░░░│
  759. │ ▼ │
  760. └───┘")]
  761. [InlineData (
  762. 10,
  763. 3,
  764. 3,
  765. Orientation.Horizontal,
  766. @"
  767. ┌────────┐
  768. │ ██░░░░ │
  769. │◄██░░░░►│
  770. │ ██░░░░ │
  771. └────────┘")]
  772. public void Vertical_Horizontal_Draws_Correctly (int sizeWidth, int sizeHeight, int widthHeight, Orientation orientation, string expected)
  773. {
  774. var super = new Window { Id = "super", Width = Dim.Fill (), Height = Dim.Fill () };
  775. var top = new Toplevel ();
  776. top.Add (super);
  777. var scrollBar = new ScrollBar
  778. {
  779. Orientation = orientation,
  780. Size = orientation == Orientation.Vertical ? sizeHeight * 2 : sizeWidth * 2,
  781. Width = orientation == Orientation.Vertical ? widthHeight : Dim.Fill (),
  782. Height = orientation == Orientation.Vertical ? Dim.Fill () : widthHeight
  783. };
  784. super.Add (scrollBar);
  785. Application.Begin (top);
  786. ((FakeDriver)Application.Driver)!.SetBufferSize (
  787. sizeWidth + (orientation == Orientation.Vertical ? widthHeight - 1 : 0),
  788. sizeHeight + (orientation == Orientation.Vertical ? 0 : widthHeight - 1));
  789. _ = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  790. }
  791. private View ScrollBarSuperView ()
  792. {
  793. var view = new View
  794. {
  795. Width = Dim.Fill (),
  796. Height = Dim.Fill ()
  797. };
  798. var top = new Toplevel ();
  799. top.Add (view);
  800. return view;
  801. }
  802. }