ScrollViewerTests.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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
  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. private Point GetRootPoint(Visual control, Point p)
  246. {
  247. if (control.GetVisualRoot() is Visual root &&
  248. control.TransformToVisual(root) is Matrix m)
  249. {
  250. return p.Transform(m);
  251. }
  252. throw new InvalidOperationException("Could not get the point in root coordinates.");
  253. }
  254. private Control CreateTemplate(ScrollViewer control, INameScope scope)
  255. {
  256. return new Grid
  257. {
  258. ColumnDefinitions = new ColumnDefinitions
  259. {
  260. new ColumnDefinition(1, GridUnitType.Star),
  261. new ColumnDefinition(GridLength.Auto),
  262. },
  263. RowDefinitions = new RowDefinitions
  264. {
  265. new RowDefinition(1, GridUnitType.Star),
  266. new RowDefinition(GridLength.Auto),
  267. },
  268. Children =
  269. {
  270. new ScrollContentPresenter
  271. {
  272. Name = "PART_ContentPresenter",
  273. }.RegisterInNameScope(scope),
  274. new ScrollBar
  275. {
  276. Name = "PART_HorizontalScrollBar",
  277. Orientation = Orientation.Horizontal,
  278. Template = new FuncControlTemplate<ScrollBar>(CreateScrollBarTemplate),
  279. [~ScrollBar.VisibilityProperty] = control[~ScrollViewer.HorizontalScrollBarVisibilityProperty],
  280. [Grid.RowProperty] = 1,
  281. }.RegisterInNameScope(scope),
  282. new ScrollBar
  283. {
  284. Name = "PART_VerticalScrollBar",
  285. Orientation = Orientation.Vertical,
  286. Template = new FuncControlTemplate<ScrollBar>(CreateScrollBarTemplate),
  287. [~ScrollBar.VisibilityProperty] = control[~ScrollViewer.VerticalScrollBarVisibilityProperty],
  288. [Grid.ColumnProperty] = 1,
  289. }.RegisterInNameScope(scope),
  290. },
  291. };
  292. }
  293. private Control CreateScrollBarTemplate(ScrollBar scrollBar, INameScope scope)
  294. {
  295. return new Border
  296. {
  297. Child = new Track
  298. {
  299. Name = "track",
  300. IsDirectionReversed = true,
  301. [!Track.MinimumProperty] = scrollBar[!RangeBase.MinimumProperty],
  302. [!Track.MaximumProperty] = scrollBar[!RangeBase.MaximumProperty],
  303. [!!Track.ValueProperty] = scrollBar[!!RangeBase.ValueProperty],
  304. [!Track.ViewportSizeProperty] = scrollBar[!ScrollBar.ViewportSizeProperty],
  305. [!Track.OrientationProperty] = scrollBar[!ScrollBar.OrientationProperty],
  306. Thumb = new Thumb
  307. {
  308. Template = new FuncControlTemplate<Thumb>(CreateThumbTemplate),
  309. },
  310. }.RegisterInNameScope(scope),
  311. };
  312. }
  313. private static Control CreateThumbTemplate(Thumb control, INameScope scope)
  314. {
  315. return new Border
  316. {
  317. Background = Brushes.Gray,
  318. };
  319. }
  320. private Thumb GetVerticalThumb(ScrollViewer target)
  321. {
  322. var scrollbar = Assert.IsType<ScrollBar>(
  323. target.GetTemplateChildren().FirstOrDefault(x => x.Name == "PART_VerticalScrollBar"));
  324. var track = Assert.IsType<Track>(
  325. scrollbar.GetTemplateChildren().FirstOrDefault(x => x.Name == "track"));
  326. return Assert.IsType<Thumb>(track.Thumb);
  327. }
  328. private static void InitializeScrollViewer(ScrollViewer target)
  329. {
  330. target.ApplyTemplate();
  331. var presenter = (ScrollContentPresenter)target.Presenter;
  332. presenter.AttachToScrollViewer();
  333. presenter.UpdateChild();
  334. }
  335. private class TestContent : Control
  336. {
  337. public Size MeasureSize { get; set; } = new Size(1000, 2000);
  338. protected override Size MeasureOverride(Size availableSize)
  339. {
  340. return MeasureSize;
  341. }
  342. }
  343. }
  344. }