ScrollBarTests.cs 28 KB

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