ScrollViewerTests.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Avalonia.Controls.Presenters;
  5. using Avalonia.Controls.Primitives;
  6. using Avalonia.Controls.Templates;
  7. using Avalonia.Layout;
  8. using Avalonia.Media;
  9. using Avalonia.UnitTests;
  10. using Avalonia.VisualTree;
  11. using Moq;
  12. using Xunit;
  13. namespace Avalonia.Controls.UnitTests
  14. {
  15. public class ScrollViewerTests : ScopedTestBase
  16. {
  17. private readonly MouseTestHelper _mouse = new();
  18. [Fact]
  19. public void Content_Is_Created()
  20. {
  21. var target = new ScrollViewer
  22. {
  23. Template = new FuncControlTemplate<ScrollViewer>(CreateTemplate),
  24. Content = "Foo",
  25. };
  26. InitializeScrollViewer(target);
  27. Assert.IsType<TextBlock>(target.Presenter.Child);
  28. }
  29. [Fact]
  30. public void Offset_Should_Be_Coerced_To_Viewport()
  31. {
  32. var target = new ScrollViewer
  33. {
  34. Extent = new Size(20, 20),
  35. Viewport = new Size(10, 10),
  36. Offset = new Vector(12, 12)
  37. };
  38. Assert.Equal(new Vector(10, 10), target.Offset);
  39. }
  40. [Fact]
  41. public void Test_ScrollToHome()
  42. {
  43. var target = new ScrollViewer
  44. {
  45. Extent = new Size(50, 50),
  46. Viewport = new Size(10, 10),
  47. Offset = new Vector(25, 25)
  48. };
  49. target.ScrollToHome();
  50. Assert.Equal(new Vector(0, 0), target.Offset);
  51. }
  52. [Fact]
  53. public void Test_ScrollToEnd()
  54. {
  55. var target = new ScrollViewer
  56. {
  57. Extent = new Size(50, 50),
  58. Viewport = new Size(10, 10),
  59. Offset = new Vector(25, 25)
  60. };
  61. target.ScrollToEnd();
  62. Assert.Equal(new Vector(0, 40), target.Offset);
  63. }
  64. [Fact]
  65. public void SmallChange_Should_Be_16()
  66. {
  67. var target = new ScrollViewer();
  68. Assert.Equal(new Size(16, 16), target.SmallChange);
  69. }
  70. [Fact]
  71. public void LargeChange_Should_Be_Viewport()
  72. {
  73. var target = new ScrollViewer
  74. {
  75. Viewport = new Size(104, 143)
  76. };
  77. Assert.Equal(new Size(104, 143), target.LargeChange);
  78. }
  79. [Fact]
  80. public void SmallChange_Should_Come_From_ILogicalScrollable_If_Present()
  81. {
  82. var child = new Mock<Control>();
  83. var logicalScroll = child.As<ILogicalScrollable>();
  84. logicalScroll.Setup(x => x.IsLogicalScrollEnabled).Returns(true);
  85. logicalScroll.Setup(x => x.ScrollSize).Returns(new Size(12, 43));
  86. var target = new ScrollViewer
  87. {
  88. Template = new FuncControlTemplate<ScrollViewer>(CreateTemplate),
  89. Content = child.Object,
  90. };
  91. InitializeScrollViewer(target);
  92. Assert.Equal(new Size(12, 43), target.SmallChange);
  93. }
  94. [Fact]
  95. public void LargeChange_Should_Come_From_ILogicalScrollable_If_Present()
  96. {
  97. var child = new Mock<Control>();
  98. var logicalScroll = child.As<ILogicalScrollable>();
  99. logicalScroll.Setup(x => x.IsLogicalScrollEnabled).Returns(true);
  100. logicalScroll.Setup(x => x.PageScrollSize).Returns(new Size(45, 67));
  101. var target = new ScrollViewer
  102. {
  103. Template = new FuncControlTemplate<ScrollViewer>(CreateTemplate),
  104. Content = child.Object,
  105. };
  106. InitializeScrollViewer(target);
  107. Assert.Equal(new Size(45, 67), target.LargeChange);
  108. }
  109. [Fact]
  110. public void Changing_Extent_Should_Raise_ScrollChanged()
  111. {
  112. var target = new ScrollViewer();
  113. var root = new TestRoot(target);
  114. var raised = 0;
  115. target.Extent = new Size(100, 100);
  116. target.Viewport = new Size(50, 50);
  117. target.Offset = new Vector(10, 10);
  118. root.LayoutManager.ExecuteInitialLayoutPass();
  119. target.ScrollChanged += (s, e) =>
  120. {
  121. Assert.Equal(new Vector(11, 12), e.ExtentDelta);
  122. Assert.Equal(default, e.OffsetDelta);
  123. Assert.Equal(default, e.ViewportDelta);
  124. ++raised;
  125. };
  126. target.Extent = new Size(111, 112);
  127. Assert.Equal(0, raised);
  128. root.LayoutManager.ExecuteLayoutPass();
  129. Assert.Equal(1, raised);
  130. }
  131. [Fact]
  132. public void Changing_Offset_Should_Raise_ScrollChanged()
  133. {
  134. var target = new ScrollViewer();
  135. var root = new TestRoot(target);
  136. var raised = 0;
  137. target.Extent = new Size(100, 100);
  138. target.Viewport = new Size(50, 50);
  139. target.Offset = new Vector(10, 10);
  140. root.LayoutManager.ExecuteInitialLayoutPass();
  141. target.ScrollChanged += (s, e) =>
  142. {
  143. Assert.Equal(default, e.ExtentDelta);
  144. Assert.Equal(new Vector(12, 14), e.OffsetDelta);
  145. Assert.Equal(default, e.ViewportDelta);
  146. ++raised;
  147. };
  148. target.Offset = new Vector(22, 24);
  149. Assert.Equal(0, raised);
  150. root.LayoutManager.ExecuteLayoutPass();
  151. Assert.Equal(1, raised);
  152. }
  153. [Fact]
  154. public void Changing_Viewport_Should_Raise_ScrollChanged()
  155. {
  156. var target = new ScrollViewer();
  157. var root = new TestRoot(target);
  158. var raised = 0;
  159. target.Extent = new Size(100, 100);
  160. target.Viewport = new Size(50, 50);
  161. target.Offset = new Vector(10, 10);
  162. root.LayoutManager.ExecuteInitialLayoutPass();
  163. target.ScrollChanged += (s, e) =>
  164. {
  165. Assert.Equal(default, e.ExtentDelta);
  166. Assert.Equal(default, e.OffsetDelta);
  167. Assert.Equal(new Vector(6, 8), e.ViewportDelta);
  168. ++raised;
  169. };
  170. target.Viewport = new Size(56, 58);
  171. Assert.Equal(0, raised);
  172. root.LayoutManager.ExecuteLayoutPass();
  173. Assert.Equal(1, raised);
  174. }
  175. [Fact]
  176. public void Reducing_Extent_Should_Constrain_Offset()
  177. {
  178. var target = new ScrollViewer
  179. {
  180. Template = new FuncControlTemplate<ScrollViewer>(CreateTemplate),
  181. };
  182. var root = new TestRoot(target);
  183. var raised = 0;
  184. target.Extent = new (100, 100);
  185. target.Viewport = new(50, 50);
  186. target.Offset = new Vector(50, 50);
  187. root.LayoutManager.ExecuteInitialLayoutPass();
  188. target.ScrollChanged += (s, e) =>
  189. {
  190. Assert.Equal(new Vector(-30, -30), e.ExtentDelta);
  191. Assert.Equal(new Vector(-30, -30), e.OffsetDelta);
  192. Assert.Equal(default, e.ViewportDelta);
  193. ++raised;
  194. };
  195. target.Extent = new(70, 70);
  196. Assert.Equal(0, raised);
  197. root.LayoutManager.ExecuteLayoutPass();
  198. Assert.Equal(1, raised);
  199. Assert.Equal(new Vector(20, 20), target.Offset);
  200. }
  201. [Fact]
  202. public void Scroll_Does_Not_Jump_When_Viewport_Becomes_Smaller_While_Dragging_ScrollBar_Thumb()
  203. {
  204. var content = new TestContent
  205. {
  206. MeasureSize = new Size(1000, 10000),
  207. };
  208. var target = new ScrollViewer
  209. {
  210. Template = new FuncControlTemplate<ScrollViewer>(CreateTemplate),
  211. Content = content,
  212. };
  213. var root = new TestRoot(target);
  214. root.LayoutManager.ExecuteInitialLayoutPass();
  215. Assert.Equal(new Size(1000, 10000), target.Extent);
  216. Assert.Equal(new Size(1000, 1000), target.Viewport);
  217. // We're working in absolute coordinates (i.e. relative to the root) and clicking on
  218. // the center of the vertical thumb.
  219. var thumb = GetVerticalThumb(target);
  220. var p = GetRootPoint(thumb, thumb.Bounds.Center);
  221. // Press the mouse button in the center of the thumb.
  222. _mouse.Down(thumb, position: p);
  223. root.LayoutManager.ExecuteLayoutPass();
  224. // Drag the thumb down 300 pixels.
  225. _mouse.Move(thumb, p += new Vector(0, 300));
  226. root.LayoutManager.ExecuteLayoutPass();
  227. Assert.Equal(new Vector(0, 3000), target.Offset);
  228. Assert.Equal(300, thumb.Bounds.Top);
  229. // Now the extent changes from 10,000 to 5000.
  230. content.MeasureSize /= 2;
  231. content.InvalidateMeasure();
  232. root.LayoutManager.ExecuteLayoutPass();
  233. // Due to the extent change, the thumb moves down but the value remains the same.
  234. Assert.Equal(600, thumb.Bounds.Top);
  235. Assert.Equal(new Vector(0, 3000), target.Offset);
  236. // Drag the thumb down another 100 pixels.
  237. _mouse.Move(thumb, p += new Vector(0, 100));
  238. root.LayoutManager.ExecuteLayoutPass();
  239. // The drag should not cause the offset/thumb to jump *up* to the current absolute
  240. // mouse position, i.e. it should move down in the direction of the drag even if the
  241. // absolute mouse position is now above the thumb.
  242. Assert.Equal(700, thumb.Bounds.Top);
  243. Assert.Equal(new Vector(0, 3500), target.Offset);
  244. }
  245. [Fact]
  246. public void Thumb_Does_Not_Become_Detached_From_Mouse_Position_When_Scrolling_Past_The_Start()
  247. {
  248. var content = new TestContent();
  249. var target = new ScrollViewer
  250. {
  251. Template = new FuncControlTemplate<ScrollViewer>(CreateTemplate),
  252. Content = content,
  253. };
  254. var root = new TestRoot(target);
  255. root.LayoutManager.ExecuteInitialLayoutPass();
  256. Assert.Equal(new Size(1000, 2000), target.Extent);
  257. Assert.Equal(new Size(1000, 1000), target.Viewport);
  258. // We're working in absolute coordinates (i.e. relative to the root) and clicking on
  259. // the center of the vertical thumb.
  260. var thumb = GetVerticalThumb(target);
  261. var p = GetRootPoint(thumb, thumb.Bounds.Center);
  262. // Press the mouse button in the center of the thumb.
  263. _mouse.Down(thumb, position: p);
  264. root.LayoutManager.ExecuteLayoutPass();
  265. // Drag the thumb down 100 pixels.
  266. _mouse.Move(thumb, p += new Vector(0, 100));
  267. root.LayoutManager.ExecuteLayoutPass();
  268. Assert.Equal(new Vector(0, 200), target.Offset);
  269. Assert.Equal(100, thumb.Bounds.Top);
  270. // Drag the thumb up 200 pixels - 100 pixels past the top of the scrollbar.
  271. _mouse.Move(thumb, p -= new Vector(0, 200));
  272. root.LayoutManager.ExecuteLayoutPass();
  273. Assert.Equal(new Vector(0, 0), target.Offset);
  274. Assert.Equal(0, thumb.Bounds.Top);
  275. // Drag the thumb back down 200 pixels.
  276. _mouse.Move(thumb, p += new Vector(0, 200));
  277. root.LayoutManager.ExecuteLayoutPass();
  278. // We should now be back in the state after we first scrolled down 100 pixels.
  279. Assert.Equal(new Vector(0, 200), target.Offset);
  280. Assert.Equal(100, thumb.Bounds.Top);
  281. }
  282. [Fact]
  283. public void Deferred_Scrolling_Defers_Scrolling_Until_Pointer_Up()
  284. {
  285. var content = new TestContent();
  286. var target = new ScrollViewer
  287. {
  288. Template = new FuncControlTemplate<ScrollViewer>(CreateTemplate),
  289. IsDeferredScrollingEnabled = true,
  290. Content = content,
  291. };
  292. var root = new TestRoot(target);
  293. root.LayoutManager.ExecuteInitialLayoutPass();
  294. // We're working in absolute coordinates (i.e. relative to the root) and clicking on
  295. // the center of the vertical thumb.
  296. var thumb = GetVerticalThumb(target);
  297. var p = GetRootPoint(thumb, thumb.Bounds.Center);
  298. Assert.Equal(Vector.Zero, target.Offset);
  299. Assert.Equal(0, thumb.Bounds.Top);
  300. // Press the mouse button in the center of the thumb.
  301. _mouse.Down(thumb, position: p);
  302. root.LayoutManager.ExecuteLayoutPass();
  303. // Drag the thumb down 100 pixels.
  304. _mouse.Move(thumb, p += new Vector(0, 100));
  305. root.LayoutManager.ExecuteLayoutPass();
  306. Assert.Equal(Vector.Zero, target.Offset); // no change to scroll...
  307. Assert.Equal(100, thumb.Bounds.Top); // ...but the Thumb has moved
  308. // Release the mouse
  309. _mouse.Up(thumb, position: p);
  310. Assert.Equal(new Vector(0, 200), target.Offset);
  311. Assert.Equal(100, thumb.Bounds.Top);
  312. }
  313. [Fact]
  314. public void BringIntoViewOnFocusChange_Scrolls_Child_Control_Into_View_When_Focused()
  315. {
  316. using var app = UnitTestApplication.Start(TestServices.RealFocus);
  317. var content = new StackPanel
  318. {
  319. Children =
  320. {
  321. new Button
  322. {
  323. Width = 100,
  324. Height = 900,
  325. },
  326. new Button
  327. {
  328. Width = 100,
  329. Height = 900,
  330. },
  331. }
  332. };
  333. var target = new ScrollViewer
  334. {
  335. Template = new FuncControlTemplate<ScrollViewer>(CreateTemplate),
  336. Content = content,
  337. };
  338. var root = new TestRoot(target);
  339. root.LayoutManager.ExecuteInitialLayoutPass();
  340. var button = (Button)content.Children[1];
  341. button.Focus();
  342. Assert.Equal(new Vector(0, 800), target.Offset);
  343. }
  344. [Fact]
  345. public void BringIntoViewOnFocusChange_False_Does_Not_Scroll_Child_Control_Into_View_When_Focused()
  346. {
  347. var content = new StackPanel
  348. {
  349. Children =
  350. {
  351. new Button
  352. {
  353. Width = 100,
  354. Height = 900,
  355. },
  356. new Button
  357. {
  358. Width = 100,
  359. Height = 900,
  360. },
  361. }
  362. };
  363. var target = new ScrollViewer
  364. {
  365. Template = new FuncControlTemplate<ScrollViewer>(CreateTemplate),
  366. Content = content,
  367. };
  368. var root = new TestRoot(target);
  369. root.LayoutManager.ExecuteInitialLayoutPass();
  370. var button = (Button)content.Children[1];
  371. button.Focus();
  372. Assert.Equal(new Vector(0, 0), target.Offset);
  373. }
  374. [Fact]
  375. public void MenuScrollBar_Should_Be_Visible_When_Specified_Visible()
  376. {
  377. Converters.MenuScrollingVisibilityConverter converter = Converters.MenuScrollingVisibilityConverter.Instance;
  378. IList<object> args = new List<object> {ScrollBarVisibility.Visible,400d,1800d,500d};
  379. var result = converter.Convert(args, typeof(ScrollBarVisibility), "0", System.Globalization.CultureInfo.CurrentCulture);
  380. Assert.Equal(true, result);
  381. }
  382. [Fact]
  383. public void ScrollBar_Visibility_Should_Invalidate_Measure_And_Arrange()
  384. {
  385. var panel = new TestPanel()
  386. {
  387. DesiredWidth = 100_000
  388. };
  389. var target = new ScrollViewer
  390. {
  391. Content = panel,
  392. Template = new FuncControlTemplate<ScrollViewer>(CreateTemplate),
  393. HorizontalScrollBarVisibility = ScrollBarVisibility.Auto
  394. };
  395. var root = new TestRoot(target);
  396. root.LayoutManager.ExecuteInitialLayoutPass();
  397. panel.Reset();
  398. target.HorizontalScrollBarVisibility = ScrollBarVisibility.Disabled;
  399. root.LayoutManager.ExecuteLayoutPass();
  400. Assert.Equal(1, panel.MeasureOverrideCalls);
  401. Assert.Equal(1, panel.ArrangeOverrideCalls);
  402. }
  403. public class TestPanel : Panel
  404. {
  405. public int DesiredWidth { get; set; }
  406. public int MeasureOverrideCalls { get; private set; }
  407. public int ArrangeOverrideCalls { get; private set; }
  408. protected override Size MeasureOverride(Size availableSize)
  409. {
  410. MeasureOverrideCalls++;
  411. return new Size(DesiredWidth, 1);
  412. }
  413. protected override Size ArrangeOverride(Size finalSize)
  414. {
  415. ArrangeOverrideCalls++;
  416. return base.ArrangeOverride(finalSize);
  417. }
  418. public void Reset()
  419. {
  420. MeasureOverrideCalls = 0;
  421. ArrangeOverrideCalls = 0;
  422. }
  423. }
  424. private Point GetRootPoint(Visual control, Point p)
  425. {
  426. if (control.GetVisualRoot() is Visual root &&
  427. control.TransformToVisual(root) is Matrix m)
  428. {
  429. return p.Transform(m);
  430. }
  431. throw new InvalidOperationException("Could not get the point in root coordinates.");
  432. }
  433. internal static Control CreateTemplate(ScrollViewer control, INameScope scope)
  434. {
  435. return new Grid
  436. {
  437. ColumnDefinitions = new ColumnDefinitions
  438. {
  439. new ColumnDefinition(1, GridUnitType.Star),
  440. new ColumnDefinition(GridLength.Auto),
  441. },
  442. RowDefinitions = new RowDefinitions
  443. {
  444. new RowDefinition(1, GridUnitType.Star),
  445. new RowDefinition(GridLength.Auto),
  446. },
  447. Children =
  448. {
  449. new ScrollContentPresenter
  450. {
  451. Name = "PART_ContentPresenter",
  452. }.RegisterInNameScope(scope),
  453. new ScrollBar
  454. {
  455. Name = "PART_HorizontalScrollBar",
  456. Orientation = Orientation.Horizontal,
  457. Template = new FuncControlTemplate<ScrollBar>(CreateScrollBarTemplate),
  458. [~ScrollBar.VisibilityProperty] = control[~ScrollViewer.HorizontalScrollBarVisibilityProperty],
  459. [Grid.RowProperty] = 1,
  460. }.RegisterInNameScope(scope),
  461. new ScrollBar
  462. {
  463. Name = "PART_VerticalScrollBar",
  464. Orientation = Orientation.Vertical,
  465. Template = new FuncControlTemplate<ScrollBar>(CreateScrollBarTemplate),
  466. [~ScrollBar.VisibilityProperty] = control[~ScrollViewer.VerticalScrollBarVisibilityProperty],
  467. [Grid.ColumnProperty] = 1,
  468. }.RegisterInNameScope(scope),
  469. },
  470. };
  471. }
  472. private static Control CreateScrollBarTemplate(ScrollBar scrollBar, INameScope scope)
  473. {
  474. return new Border
  475. {
  476. Child = new Track
  477. {
  478. Name = "track",
  479. IsDirectionReversed = true,
  480. [!Track.MinimumProperty] = scrollBar[!RangeBase.MinimumProperty],
  481. [!Track.MaximumProperty] = scrollBar[!RangeBase.MaximumProperty],
  482. [!!Track.ValueProperty] = scrollBar[!!RangeBase.ValueProperty],
  483. [!Track.ViewportSizeProperty] = scrollBar[!ScrollBar.ViewportSizeProperty],
  484. [!Track.OrientationProperty] = scrollBar[!ScrollBar.OrientationProperty],
  485. [!Track.DeferThumbDragProperty] = scrollBar.TemplatedParent[!ScrollViewer.IsDeferredScrollingEnabledProperty],
  486. Thumb = new Thumb
  487. {
  488. Template = new FuncControlTemplate<Thumb>(CreateThumbTemplate),
  489. },
  490. }.RegisterInNameScope(scope),
  491. };
  492. }
  493. private static Control CreateThumbTemplate(Thumb control, INameScope scope)
  494. {
  495. return new Border
  496. {
  497. Background = Brushes.Gray,
  498. };
  499. }
  500. private Thumb GetVerticalThumb(ScrollViewer target)
  501. {
  502. var scrollbar = Assert.IsType<ScrollBar>(
  503. target.GetTemplateChildren().FirstOrDefault(x => x.Name == "PART_VerticalScrollBar"));
  504. var track = Assert.IsType<Track>(
  505. scrollbar.GetTemplateChildren().FirstOrDefault(x => x.Name == "track"));
  506. return Assert.IsType<Thumb>(track.Thumb);
  507. }
  508. private static void InitializeScrollViewer(ScrollViewer target)
  509. {
  510. target.ApplyTemplate();
  511. var presenter = (ScrollContentPresenter)target.Presenter;
  512. presenter.AttachToScrollViewer();
  513. presenter.UpdateChild();
  514. }
  515. private class TestContent : Control
  516. {
  517. public Size MeasureSize { get; set; } = new Size(1000, 2000);
  518. protected override Size MeasureOverride(Size availableSize)
  519. {
  520. return MeasureSize;
  521. }
  522. }
  523. }
  524. }