ScrollBarTests.cs 31 KB

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