ScrollBarTests.cs 30 KB

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