ScrollTests.cs 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313
  1. using Microsoft.VisualStudio.TestPlatform.Utilities;
  2. using Xunit.Abstractions;
  3. namespace Terminal.Gui.ViewsTests;
  4. public class ScrollTests
  5. {
  6. public ScrollTests (ITestOutputHelper output) { _output = output; }
  7. private readonly ITestOutputHelper _output;
  8. [Fact]
  9. public void OnOrientationChanged_Keeps_Size ()
  10. {
  11. var scroll = new Scroll ();
  12. scroll.Layout ();
  13. scroll.Size = 1;
  14. scroll.Orientation = Orientation.Horizontal;
  15. Assert.Equal (1, scroll.Size);
  16. }
  17. [Fact]
  18. public void OnOrientationChanged_Sets_Position_To_0 ()
  19. {
  20. View super = new View ()
  21. {
  22. Id = "super",
  23. Width = 10,
  24. Height = 10
  25. };
  26. var scroll = new Scroll ()
  27. {
  28. };
  29. super.Add (scroll);
  30. scroll.Layout ();
  31. scroll.SliderPosition = 1;
  32. scroll.Orientation = Orientation.Horizontal;
  33. Assert.Equal (0, scroll.SliderPosition);
  34. }
  35. // [Theory]
  36. // [AutoInitShutdown]
  37. // [InlineData (
  38. // 20,
  39. // @"
  40. //█
  41. //█
  42. //█
  43. //█
  44. //█
  45. //░
  46. //░
  47. //░
  48. //░
  49. //░",
  50. // @"
  51. //░
  52. //░
  53. //█
  54. //█
  55. //█
  56. //█
  57. //█
  58. //░
  59. //░
  60. //░",
  61. // @"
  62. //░
  63. //░
  64. //░
  65. //░
  66. //░
  67. //█
  68. //█
  69. //█
  70. //█
  71. //█",
  72. // @"
  73. //░
  74. //░
  75. //█
  76. //█
  77. //█
  78. //░
  79. //░
  80. //░
  81. //░
  82. //░",
  83. // @"
  84. //█████░░░░░",
  85. // @"
  86. //░░█████░░░",
  87. // @"
  88. //░░░░░█████",
  89. // @"
  90. //░░███░░░░░")]
  91. // [InlineData (
  92. // 40,
  93. // @"
  94. //█
  95. //█
  96. //█
  97. //░
  98. //░
  99. //░
  100. //░
  101. //░
  102. //░
  103. //░",
  104. // @"
  105. //░
  106. //█
  107. //█
  108. //█
  109. //░
  110. //░
  111. //░
  112. //░
  113. //░
  114. //░",
  115. // @"
  116. //░
  117. //░
  118. //█
  119. //█
  120. //█
  121. //░
  122. //░
  123. //░
  124. //░
  125. //░",
  126. // @"
  127. //░
  128. //█
  129. //█
  130. //░
  131. //░
  132. //░
  133. //░
  134. //░
  135. //░
  136. //░",
  137. // @"
  138. //███░░░░░░░",
  139. // @"
  140. //░███░░░░░░",
  141. // @"
  142. //░░███░░░░░",
  143. // @"
  144. //░██░░░░░░░")]
  145. // public void Changing_Position_Size_Orientation_Draws_Correctly_KeepContentInAllViewport_True (
  146. // int size,
  147. // string firstVertExpected,
  148. // string middleVertExpected,
  149. // string endVertExpected,
  150. // string sizeVertExpected,
  151. // string firstHoriExpected,
  152. // string middleHoriExpected,
  153. // string endHoriExpected,
  154. // string sizeHoriExpected
  155. // )
  156. // {
  157. // var scroll = new Scroll
  158. // {
  159. // Orientation = Orientation.Vertical,
  160. // Size = size,
  161. // Height = 10,
  162. // KeepContentInAllViewport = true
  163. // };
  164. // var top = new Toplevel ();
  165. // top.Add (scroll);
  166. // RunState rs = Application.Begin (top);
  167. // Application.RunIteration (ref rs);
  168. // _ = TestHelpers.AssertDriverContentsWithFrameAre (firstVertExpected, _output);
  169. // scroll.Position = 4;
  170. // Application.RunIteration (ref rs);
  171. // _ = TestHelpers.AssertDriverContentsWithFrameAre (middleVertExpected, _output);
  172. // scroll.Position = 10;
  173. // Application.RunIteration (ref rs);
  174. // _ = TestHelpers.AssertDriverContentsWithFrameAre (endVertExpected, _output);
  175. // scroll.Size = size * 2;
  176. // Application.RunIteration (ref rs);
  177. // _ = TestHelpers.AssertDriverContentsWithFrameAre (sizeVertExpected, _output);
  178. // scroll.Orientation = Orientation.Horizontal;
  179. // scroll.Width = 10;
  180. // scroll.Height = 1;
  181. // scroll.Position = 0;
  182. // scroll.Size = size;
  183. // Application.RunIteration (ref rs);
  184. // _ = TestHelpers.AssertDriverContentsWithFrameAre (firstHoriExpected, _output);
  185. // scroll.Position = 4;
  186. // Application.RunIteration (ref rs);
  187. // _ = TestHelpers.AssertDriverContentsWithFrameAre (middleHoriExpected, _output);
  188. // scroll.Position = 10;
  189. // Application.RunIteration (ref rs);
  190. // _ = TestHelpers.AssertDriverContentsWithFrameAre (endHoriExpected, _output);
  191. // scroll.Size = size * 2;
  192. // Application.RunIteration (ref rs);
  193. // _ = TestHelpers.AssertDriverContentsWithFrameAre (sizeHoriExpected, _output);
  194. // }
  195. [Fact]
  196. public void Constructor_Defaults ()
  197. {
  198. var scroll = new Scroll ();
  199. Assert.False (scroll.CanFocus);
  200. Assert.Equal (Orientation.Vertical, scroll.Orientation);
  201. Assert.Equal (0, scroll.Size);
  202. Assert.Equal (0, scroll.SliderPosition);
  203. }
  204. //[Fact]
  205. //[AutoInitShutdown]
  206. //public void KeepContentInAllViewport_True_False_KeepContentInAllViewport_True ()
  207. //{
  208. // var view = new View { Width = Dim.Fill (), Height = Dim.Fill () };
  209. // view.Padding.Thickness = new (0, 0, 2, 0);
  210. // view.SetContentSize (new (view.Viewport.Width, 30));
  211. // var scroll = new Scroll { Width = 2, Height = Dim.Fill (), Size = view.GetContentSize ().Height, KeepContentInAllViewport = true };
  212. // scroll.PositionChanged += (_, e) => view.Viewport = view.Viewport with { Y = e.CurrentValue };
  213. // view.Padding.Add (scroll);
  214. // var top = new Toplevel ();
  215. // top.Add (view);
  216. // Application.Begin (top);
  217. // Assert.True (scroll.KeepContentInAllViewport);
  218. // Assert.Equal (80, view.Padding.Viewport.Width);
  219. // Assert.Equal (25, view.Padding.Viewport.Height);
  220. // Assert.Equal (2, scroll.Viewport.Width);
  221. // Assert.Equal (25, scroll.Viewport.Height);
  222. // Assert.Equal (30, scroll.Size);
  223. // scroll.KeepContentInAllViewport = false;
  224. // scroll.Position = 50;
  225. // Assert.Equal (scroll.Position, scroll.Size - 1);
  226. // Assert.Equal (scroll.Position, view.Viewport.Y);
  227. // Assert.Equal (29, scroll.Position);
  228. // Assert.Equal (29, view.Viewport.Y);
  229. // top.Dispose ();
  230. //}
  231. [Theory]
  232. [AutoInitShutdown]
  233. [InlineData (
  234. Orientation.Vertical,
  235. 20,
  236. 10,
  237. 4,
  238. @"
  239. █",
  240. 0,
  241. @"
  242. ░")]
  243. [InlineData (
  244. Orientation.Vertical,
  245. 40,
  246. 10,
  247. 5,
  248. @"
  249. ░",
  250. 20,
  251. @"
  252. ░")]
  253. [InlineData (
  254. Orientation.Horizontal,
  255. 20,
  256. 10,
  257. 4,
  258. @"
  259. ░░░░░█████",
  260. 0,
  261. @"
  262. █████░░░░░")]
  263. [InlineData (
  264. Orientation.Horizontal,
  265. 40,
  266. 10,
  267. 5,
  268. @"
  269. ░░███░░░░░",
  270. 20,
  271. @"
  272. ░░░░░███░░")]
  273. public void Mouse_On_The_Container (Orientation orientation, int size, int position, int location, string output, int expectedPos, string expectedOut)
  274. {
  275. var scroll = new Scroll
  276. {
  277. Width = orientation == Orientation.Vertical ? 1 : 10,
  278. Height = orientation == Orientation.Vertical ? 10 : 1,
  279. Orientation = orientation, Size = size,
  280. SliderPosition = position,
  281. };
  282. var top = new Toplevel ();
  283. top.Add (scroll);
  284. RunState rs = Application.Begin (top);
  285. Application.RunIteration (ref rs);
  286. _ = TestHelpers.AssertDriverContentsWithFrameAre (output, _output);
  287. Application.RaiseMouseEvent (
  288. new ()
  289. {
  290. ScreenPosition = orientation == Orientation.Vertical ? new (0, location) : new Point (location, 0),
  291. Flags = MouseFlags.Button1Pressed
  292. });
  293. Assert.Equal (expectedPos, scroll.SliderPosition);
  294. Application.RunIteration (ref rs);
  295. _ = TestHelpers.AssertDriverContentsWithFrameAre (expectedOut, _output);
  296. }
  297. [Theory]
  298. [AutoInitShutdown]
  299. [InlineData (
  300. Orientation.Vertical,
  301. 20,
  302. 10,
  303. 5,
  304. 5,
  305. @"
  306. █",
  307. MouseFlags.Button1Pressed,
  308. 10,
  309. @"
  310. █")]
  311. [InlineData (
  312. Orientation.Vertical,
  313. 40,
  314. 10,
  315. 3,
  316. 3,
  317. @"
  318. ░",
  319. MouseFlags.Button1Pressed,
  320. 10,
  321. @"
  322. ░")]
  323. [InlineData (
  324. Orientation.Horizontal,
  325. 20,
  326. 10,
  327. 5,
  328. 5,
  329. @"
  330. ░░░░░█████",
  331. MouseFlags.Button1Pressed,
  332. 10,
  333. @"
  334. ░░░░░█████")]
  335. [InlineData (
  336. Orientation.Horizontal,
  337. 40,
  338. 10,
  339. 3,
  340. 3,
  341. @"
  342. ░░███░░░░░",
  343. MouseFlags.Button1Pressed,
  344. 10,
  345. @"
  346. ░░███░░░░░")]
  347. [InlineData (
  348. Orientation.Vertical,
  349. 20,
  350. 10,
  351. 5,
  352. 4,
  353. @"
  354. █",
  355. MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition,
  356. 8,
  357. @"
  358. ░")]
  359. [InlineData (
  360. Orientation.Horizontal,
  361. 20,
  362. 10,
  363. 5,
  364. 4,
  365. @"
  366. ░░░░░█████",
  367. MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition,
  368. 8,
  369. @"
  370. ░░░░█████░")]
  371. [InlineData (
  372. Orientation.Vertical,
  373. 20,
  374. 10,
  375. 5,
  376. 6,
  377. @"
  378. █",
  379. MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition,
  380. 10,
  381. @"
  382. █")]
  383. [InlineData (
  384. Orientation.Horizontal,
  385. 20,
  386. 10,
  387. 5,
  388. 6,
  389. @"
  390. ░░░░░█████",
  391. MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition,
  392. 10,
  393. @"
  394. ░░░░░█████")]
  395. [InlineData (
  396. Orientation.Vertical,
  397. 40,
  398. 10,
  399. 2,
  400. 1,
  401. @"
  402. ░",
  403. MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition,
  404. 4,
  405. @"
  406. ░")]
  407. [InlineData (
  408. Orientation.Horizontal,
  409. 40,
  410. 10,
  411. 2,
  412. 1,
  413. @"
  414. ░░███░░░░░",
  415. MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition,
  416. 4,
  417. @"
  418. ░███░░░░░░")]
  419. [InlineData (
  420. Orientation.Vertical,
  421. 40,
  422. 10,
  423. 3,
  424. 4,
  425. @"
  426. ░",
  427. MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition,
  428. 12,
  429. @"
  430. ░")]
  431. [InlineData (
  432. Orientation.Horizontal,
  433. 40,
  434. 10,
  435. 3,
  436. 4,
  437. @"
  438. ░░███░░░░░",
  439. MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition,
  440. 12,
  441. @"
  442. ░░░███░░░░")]
  443. [InlineData (
  444. Orientation.Vertical,
  445. 40,
  446. 10,
  447. 2,
  448. 3,
  449. @"
  450. ░",
  451. MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition,
  452. 12,
  453. @"
  454. ░")]
  455. [InlineData (
  456. Orientation.Horizontal,
  457. 40,
  458. 10,
  459. 2,
  460. 3,
  461. @"
  462. ░░███░░░░░",
  463. MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition,
  464. 12,
  465. @"
  466. ░░░███░░░░")]
  467. [InlineData (
  468. Orientation.Vertical,
  469. 40,
  470. 10,
  471. 2,
  472. 4,
  473. @"
  474. ░",
  475. MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition,
  476. 16,
  477. @"
  478. ░")]
  479. [InlineData (
  480. Orientation.Horizontal,
  481. 40,
  482. 10,
  483. 2,
  484. 4,
  485. @"
  486. ░░███░░░░░",
  487. MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition,
  488. 16,
  489. @"
  490. ░░░░███░░░")]
  491. public void Mouse_On_The_Slider (
  492. Orientation orientation,
  493. int size,
  494. int position,
  495. int startLocation,
  496. int endLocation,
  497. string output,
  498. MouseFlags mouseFlags,
  499. int expectedPos,
  500. string expectedOut
  501. )
  502. {
  503. var scroll = new Scroll
  504. {
  505. Width = orientation == Orientation.Vertical ? 1 : 10,
  506. Height = orientation == Orientation.Vertical ? 10 : 1,
  507. Orientation = orientation,
  508. Size = size, SliderPosition = position,
  509. };
  510. var top = new Toplevel ();
  511. top.Add (scroll);
  512. RunState rs = Application.Begin (top);
  513. Application.RunIteration (ref rs);
  514. _ = TestHelpers.AssertDriverContentsWithFrameAre (output, _output);
  515. Assert.Null (Application.MouseGrabView);
  516. if (mouseFlags.HasFlag (MouseFlags.ReportMousePosition))
  517. {
  518. MouseFlags mf = mouseFlags & ~MouseFlags.ReportMousePosition;
  519. Application.RaiseMouseEvent (
  520. new ()
  521. {
  522. ScreenPosition = orientation == Orientation.Vertical ? new (0, startLocation) : new (startLocation, 0),
  523. Flags = mf
  524. });
  525. Application.RunIteration (ref rs);
  526. Application.RaiseMouseEvent (
  527. new ()
  528. {
  529. ScreenPosition = orientation == Orientation.Vertical ? new (0, endLocation) : new (endLocation, 0),
  530. Flags = mouseFlags
  531. });
  532. Application.RunIteration (ref rs);
  533. }
  534. else
  535. {
  536. Assert.Equal (startLocation, endLocation);
  537. Application.RaiseMouseEvent (
  538. new ()
  539. {
  540. ScreenPosition = orientation == Orientation.Vertical ? new (0, startLocation) : new (startLocation, 0),
  541. Flags = mouseFlags
  542. });
  543. Application.RunIteration (ref rs);
  544. }
  545. Assert.Equal ("scrollSlider", Application.MouseGrabView?.Id);
  546. Assert.Equal (expectedPos, scroll.SliderPosition);
  547. Application.RunIteration (ref rs);
  548. _ = TestHelpers.AssertDriverContentsWithFrameAre (expectedOut, _output);
  549. Application.RaiseMouseEvent (
  550. new ()
  551. {
  552. ScreenPosition = orientation == Orientation.Vertical ? new (0, startLocation) : new (startLocation, 0),
  553. Flags = MouseFlags.Button1Released
  554. });
  555. Assert.Null (Application.MouseGrabView);
  556. }
  557. [Theory]
  558. [AutoInitShutdown]
  559. [InlineData (Orientation.Vertical)]
  560. [InlineData (Orientation.Horizontal)]
  561. public void Moving_Mouse_Outside_Host_Ensures_Correct_Location_KeepContentInAllViewport_True (Orientation orientation)
  562. {
  563. var scroll = new Scroll
  564. {
  565. X = 10, Y = 10, Width = orientation == Orientation.Vertical ? 1 : 10, Height = orientation == Orientation.Vertical ? 10 : 1, Size = 20,
  566. SliderPosition = 5, Orientation = orientation
  567. };
  568. var top = new Toplevel ();
  569. top.Add (scroll);
  570. RunState rs = Application.Begin (top);
  571. Rectangle scrollSliderFrame = scroll.Subviews.FirstOrDefault (x => x.Id == "scrollSlider")!.Frame;
  572. Assert.Equal (scrollSliderFrame, orientation == Orientation.Vertical ? new (0, 2, 1, 5) : new (2, 0, 5, 1));
  573. Application.RaiseMouseEvent (new () { ScreenPosition = orientation == Orientation.Vertical ? new (10, 12) : new (12, 10), Flags = MouseFlags.Button1Pressed });
  574. Application.RunIteration (ref rs);
  575. Application.RaiseMouseEvent (
  576. new ()
  577. {
  578. ScreenPosition = orientation == Orientation.Vertical ? new (10, 0) : new (0, 10),
  579. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  580. });
  581. Application.RunIteration (ref rs);
  582. Assert.Equal (new (0, 0), scroll.Subviews.FirstOrDefault (x => x.Id == "scrollSlider")!.Frame.Location);
  583. Application.RaiseMouseEvent (
  584. new ()
  585. {
  586. ScreenPosition = orientation == Orientation.Vertical ? new (0, 25) : new (80, 0),
  587. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  588. });
  589. Application.RunIteration (ref rs);
  590. Assert.Equal (
  591. orientation == Orientation.Vertical ? new (0, 5) : new (5, 0),
  592. scroll.Subviews.FirstOrDefault (x => x.Id == "scrollSlider")!.Frame.Location);
  593. }
  594. [Theory]
  595. [CombinatorialData]
  596. public void Position_Clamps_To_Size (
  597. [CombinatorialRange (1, 6, 1)] int scrollSize,
  598. [CombinatorialRange (-1, 6, 1)] int scrollPosition,
  599. Orientation orientation
  600. )
  601. {
  602. var super = new View
  603. {
  604. Id = "super",
  605. Width = 5,
  606. Height = 5
  607. };
  608. var scroll = new Scroll
  609. {
  610. Orientation = orientation,
  611. Width = Dim.Fill(),
  612. Height = Dim.Fill ()
  613. };
  614. super.Add (scroll);
  615. scroll.Size = scrollSize;
  616. super.Layout ();
  617. scroll.SliderPosition = scrollPosition;
  618. super.Layout ();
  619. Assert.True (scroll.SliderPosition <= scrollSize);
  620. }
  621. [Fact]
  622. public void PositionChanging_Cancelable_And_PositionChanged_Events ()
  623. {
  624. var changingCount = 0;
  625. var changedCount = 0;
  626. var scroll = new Scroll { Size = 10 };
  627. scroll.Layout ();
  628. scroll.SliderPositionChanging += (s, e) =>
  629. {
  630. if (changingCount == 0)
  631. {
  632. e.Cancel = true;
  633. }
  634. changingCount++;
  635. };
  636. scroll.SliderPositionChanged += (s, e) => changedCount++;
  637. scroll.SliderPosition = 1;
  638. Assert.Equal (0, scroll.SliderPosition);
  639. Assert.Equal (1, changingCount);
  640. Assert.Equal (0, changedCount);
  641. scroll.SliderPosition = 1;
  642. Assert.Equal (1, scroll.SliderPosition);
  643. Assert.Equal (2, changingCount);
  644. Assert.Equal (1, changedCount);
  645. }
  646. [Fact]
  647. public void PositionChanging_PositionChanged_Events_Only_Raises_Once_If_Position_Was_Really_Changed ()
  648. {
  649. var changing = 0;
  650. var cancel = false;
  651. var changed = 0;
  652. var scroll = new Scroll { Height = 10, Size = 20 };
  653. scroll.SliderPositionChanging += Scroll_PositionChanging;
  654. scroll.SliderPositionChanged += Scroll_PositionChanged;
  655. Assert.Equal (Orientation.Vertical, scroll.Orientation);
  656. scroll.Layout ();
  657. Assert.Equal (new (0, 0, 1, 10), scroll.Viewport);
  658. Assert.Equal (0, scroll.SliderPosition);
  659. Assert.Equal (0, changing);
  660. Assert.Equal (0, changed);
  661. scroll.SliderPosition = 0;
  662. Assert.Equal (0, scroll.SliderPosition);
  663. Assert.Equal (0, changing);
  664. Assert.Equal (0, changed);
  665. scroll.SliderPosition = 1;
  666. Assert.Equal (1, scroll.SliderPosition);
  667. Assert.Equal (1, changing);
  668. Assert.Equal (1, changed);
  669. Reset ();
  670. cancel = true;
  671. scroll.SliderPosition = 2;
  672. Assert.Equal (1, scroll.SliderPosition);
  673. Assert.Equal (1, changing);
  674. Assert.Equal (0, changed);
  675. Reset ();
  676. scroll.SliderPosition = 10;
  677. Assert.Equal (10, scroll.SliderPosition);
  678. Assert.Equal (1, changing);
  679. Assert.Equal (1, changed);
  680. Reset ();
  681. scroll.SliderPosition = 11;
  682. Assert.Equal (10, scroll.SliderPosition);
  683. Assert.Equal (0, changing);
  684. Assert.Equal (0, changed);
  685. Reset ();
  686. scroll.SliderPosition = 0;
  687. Assert.Equal (0, scroll.SliderPosition);
  688. Assert.Equal (1, changing);
  689. Assert.Equal (1, changed);
  690. scroll.SliderPositionChanging -= Scroll_PositionChanging;
  691. scroll.SliderPositionChanged -= Scroll_PositionChanged;
  692. void Scroll_PositionChanging (object sender, CancelEventArgs<int> e)
  693. {
  694. changing++;
  695. e.Cancel = cancel;
  696. }
  697. void Scroll_PositionChanged (object sender, EventArgs<int> e) { changed++; }
  698. void Reset ()
  699. {
  700. changing = 0;
  701. cancel = false;
  702. changed = 0;
  703. }
  704. }
  705. [Fact]
  706. public void Size_Cannot_Be_Negative ()
  707. {
  708. var scroll = new Scroll { Height = 10, Size = -1 };
  709. Assert.Equal (0, scroll.Size);
  710. scroll.Size = -10;
  711. Assert.Equal (0, scroll.Size);
  712. }
  713. [Fact]
  714. public void SizeChanged_Event ()
  715. {
  716. var count = 0;
  717. var scroll = new Scroll ();
  718. scroll.Layout ();
  719. scroll.SizeChanged += (s, e) => count++;
  720. scroll.Size = 10;
  721. Assert.Equal (10, scroll.Size);
  722. Assert.Equal (1, count);
  723. }
  724. [Theory]
  725. [SetupFakeDriver]
  726. [InlineData (
  727. 3,
  728. 10,
  729. 1,
  730. 0,
  731. Orientation.Vertical,
  732. @"
  733. ┌───┐
  734. │███│
  735. │ │
  736. │ │
  737. │ │
  738. │ │
  739. │ │
  740. │ │
  741. │ │
  742. │ │
  743. │ │
  744. └───┘")]
  745. [InlineData (
  746. 10,
  747. 1,
  748. 3,
  749. 0,
  750. Orientation.Horizontal,
  751. @"
  752. ┌──────────┐
  753. │███ │
  754. └──────────┘")]
  755. [InlineData (
  756. 3,
  757. 10,
  758. 3,
  759. 0,
  760. Orientation.Vertical,
  761. @"
  762. ┌───┐
  763. │███│
  764. │███│
  765. │███│
  766. │ │
  767. │ │
  768. │ │
  769. │ │
  770. │ │
  771. │ │
  772. │ │
  773. └───┘")]
  774. [InlineData (
  775. 3,
  776. 10,
  777. 5,
  778. 0,
  779. Orientation.Vertical,
  780. @"
  781. ┌───┐
  782. │███│
  783. │███│
  784. │███│
  785. │███│
  786. │███│
  787. │ │
  788. │ │
  789. │ │
  790. │ │
  791. │ │
  792. └───┘")]
  793. [InlineData (
  794. 3,
  795. 10,
  796. 5,
  797. 1,
  798. Orientation.Vertical,
  799. @"
  800. ┌───┐
  801. │ │
  802. │███│
  803. │███│
  804. │███│
  805. │███│
  806. │███│
  807. │ │
  808. │ │
  809. │ │
  810. │ │
  811. └───┘")]
  812. [InlineData (
  813. 3,
  814. 10,
  815. 5,
  816. 4,
  817. Orientation.Vertical,
  818. @"
  819. ┌───┐
  820. │ │
  821. │ │
  822. │ │
  823. │ │
  824. │███│
  825. │███│
  826. │███│
  827. │███│
  828. │███│
  829. │ │
  830. └───┘")]
  831. [InlineData (
  832. 3,
  833. 10,
  834. 5,
  835. 5,
  836. Orientation.Vertical,
  837. @"
  838. ┌───┐
  839. │ │
  840. │ │
  841. │ │
  842. │ │
  843. │ │
  844. │███│
  845. │███│
  846. │███│
  847. │███│
  848. │███│
  849. └───┘")]
  850. [InlineData (
  851. 3,
  852. 10,
  853. 5,
  854. 6,
  855. Orientation.Vertical,
  856. @"
  857. ┌───┐
  858. │ │
  859. │ │
  860. │ │
  861. │ │
  862. │ │
  863. │███│
  864. │███│
  865. │███│
  866. │███│
  867. │███│
  868. └───┘")]
  869. [InlineData (
  870. 3,
  871. 10,
  872. 10,
  873. 0,
  874. Orientation.Vertical,
  875. @"
  876. ┌───┐
  877. │███│
  878. │███│
  879. │███│
  880. │███│
  881. │███│
  882. │███│
  883. │███│
  884. │███│
  885. │███│
  886. │███│
  887. └───┘")]
  888. [InlineData (
  889. 3,
  890. 10,
  891. 10,
  892. 5,
  893. Orientation.Vertical,
  894. @"
  895. ┌───┐
  896. │███│
  897. │███│
  898. │███│
  899. │███│
  900. │███│
  901. │███│
  902. │███│
  903. │███│
  904. │███│
  905. │███│
  906. └───┘")]
  907. [InlineData (
  908. 3,
  909. 10,
  910. 11,
  911. 0,
  912. Orientation.Vertical,
  913. @"
  914. ┌───┐
  915. │███│
  916. │███│
  917. │███│
  918. │███│
  919. │███│
  920. │███│
  921. │███│
  922. │███│
  923. │███│
  924. │███│
  925. └───┘")]
  926. [InlineData (
  927. 10,
  928. 3,
  929. 5,
  930. 0,
  931. Orientation.Horizontal,
  932. @"
  933. ┌──────────┐
  934. │█████ │
  935. │█████ │
  936. │█████ │
  937. └──────────┘")]
  938. [InlineData (
  939. 10,
  940. 3,
  941. 5,
  942. 1,
  943. Orientation.Horizontal,
  944. @"
  945. ┌──────────┐
  946. │ █████ │
  947. │ █████ │
  948. │ █████ │
  949. └──────────┘")]
  950. [InlineData (
  951. 10,
  952. 3,
  953. 5,
  954. 4,
  955. Orientation.Horizontal,
  956. @"
  957. ┌──────────┐
  958. │ █████ │
  959. │ █████ │
  960. │ █████ │
  961. └──────────┘")]
  962. [InlineData (
  963. 10,
  964. 3,
  965. 5,
  966. 5,
  967. Orientation.Horizontal,
  968. @"
  969. ┌──────────┐
  970. │ █████│
  971. │ █████│
  972. │ █████│
  973. └──────────┘")]
  974. [InlineData (
  975. 10,
  976. 3,
  977. 5,
  978. 6,
  979. Orientation.Horizontal,
  980. @"
  981. ┌──────────┐
  982. │ █████│
  983. │ █████│
  984. │ █████│
  985. └──────────┘")]
  986. [InlineData (
  987. 10,
  988. 3,
  989. 10,
  990. 0,
  991. Orientation.Horizontal,
  992. @"
  993. ┌──────────┐
  994. │██████████│
  995. │██████████│
  996. │██████████│
  997. └──────────┘")]
  998. [InlineData (
  999. 10,
  1000. 3,
  1001. 10,
  1002. 5,
  1003. Orientation.Horizontal,
  1004. @"
  1005. ┌──────────┐
  1006. │██████████│
  1007. │██████████│
  1008. │██████████│
  1009. └──────────┘")]
  1010. [InlineData (
  1011. 10,
  1012. 3,
  1013. 11,
  1014. 0,
  1015. Orientation.Horizontal,
  1016. @"
  1017. ┌──────────┐
  1018. │██████████│
  1019. │██████████│
  1020. │██████████│
  1021. └──────────┘")]
  1022. public void Draws_Correctly (int superViewportWidth, int superViewportHeight, int sliderSize, int sliderPosition, Orientation orientation, string expected)
  1023. {
  1024. var super = new Window
  1025. {
  1026. Id = "super",
  1027. Width = superViewportWidth + 2,
  1028. Height = superViewportHeight + 2
  1029. };
  1030. var scroll = new Scroll
  1031. {
  1032. Orientation = orientation,
  1033. };
  1034. super.Add (scroll);
  1035. scroll.Size = sliderSize;
  1036. scroll.Layout ();
  1037. scroll.SliderPosition = sliderPosition;
  1038. super.BeginInit ();
  1039. super.EndInit ();
  1040. super.Layout ();
  1041. super.Draw ();
  1042. _ = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1043. }
  1044. }