ScrollBarTests.cs 25 KB

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