ItemsPresenterTests_Virtualization.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. // Copyright (c) The Avalonia Project. All rights reserved.
  2. // Licensed under the MIT license. See licence.md file in the project root for full license information.
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using Avalonia.Controls.Generators;
  7. using Avalonia.Controls.Presenters;
  8. using Avalonia.Controls.Primitives;
  9. using Avalonia.Controls.Templates;
  10. using Avalonia.Layout;
  11. using Avalonia.Platform;
  12. using Avalonia.Rendering;
  13. using Avalonia.UnitTests;
  14. using Avalonia.VisualTree;
  15. using Xunit;
  16. namespace Avalonia.Controls.UnitTests.Presenters
  17. {
  18. public class ItemsPresenterTests_Virtualization
  19. {
  20. [Fact]
  21. public void Should_Not_Create_Items_Before_Added_To_Visual_Tree()
  22. {
  23. var items = Enumerable.Range(0, 10).Select(x => $"Item {x}").ToList();
  24. var target = new TestItemsPresenter(true)
  25. {
  26. Items = items,
  27. ItemsPanel = VirtualizingPanelTemplate(Orientation.Vertical),
  28. ItemTemplate = ItemTemplate(),
  29. VirtualizationMode = ItemVirtualizationMode.Simple,
  30. };
  31. var scroller = new ScrollContentPresenter
  32. {
  33. Content = target,
  34. };
  35. scroller.UpdateChild();
  36. target.ApplyTemplate();
  37. target.Measure(new Size(100, 100));
  38. target.Arrange(new Rect(0, 0, 100, 100));
  39. Assert.Empty(target.Panel.Children);
  40. var root = new TestRoot
  41. {
  42. Child = scroller,
  43. };
  44. target.InvalidateMeasure();
  45. target.Panel.InvalidateMeasure();
  46. target.Measure(new Size(100, 100));
  47. target.Arrange(new Rect(0, 0, 100, 100));
  48. Assert.Equal(10, target.Panel.Children.Count);
  49. }
  50. [Fact]
  51. public void Should_Return_IsLogicalScrollEnabled_False_When_Has_No_Virtualizing_Panel()
  52. {
  53. var target = CreateTarget();
  54. target.ClearValue(ItemsPresenter.ItemsPanelProperty);
  55. target.ApplyTemplate();
  56. Assert.False(((ILogicalScrollable)target).IsLogicalScrollEnabled);
  57. }
  58. [Fact]
  59. public void Should_Return_IsLogicalScrollEnabled_False_When_VirtualizationMode_None()
  60. {
  61. var target = CreateTarget(ItemVirtualizationMode.None);
  62. target.ApplyTemplate();
  63. Assert.False(((ILogicalScrollable)target).IsLogicalScrollEnabled);
  64. }
  65. [Fact]
  66. public void Should_Return_IsLogicalScrollEnabled_False_When_Doesnt_Have_ScrollPresenter_Parent()
  67. {
  68. var target = new ItemsPresenter
  69. {
  70. ItemsPanel = VirtualizingPanelTemplate(),
  71. ItemTemplate = ItemTemplate(),
  72. VirtualizationMode = ItemVirtualizationMode.Simple,
  73. };
  74. target.ApplyTemplate();
  75. Assert.False(((ILogicalScrollable)target).IsLogicalScrollEnabled);
  76. }
  77. [Fact]
  78. public void Should_Return_IsLogicalScrollEnabled_True()
  79. {
  80. var target = CreateTarget();
  81. target.ApplyTemplate();
  82. Assert.True(((ILogicalScrollable)target).IsLogicalScrollEnabled);
  83. }
  84. [Fact]
  85. public void Parent_ScrollContentPresenter_Properties_Should_Be_Set()
  86. {
  87. var target = CreateTarget();
  88. target.ApplyTemplate();
  89. target.Measure(new Size(100, 100));
  90. target.Arrange(new Rect(0, 0, 100, 100));
  91. var scroll = (ScrollContentPresenter)target.Parent;
  92. Assert.Equal(new Size(10, 20), scroll.Extent);
  93. Assert.Equal(new Size(100, 10), scroll.Viewport);
  94. }
  95. [Fact]
  96. public void Should_Fill_Panel_With_Containers()
  97. {
  98. var target = CreateTarget();
  99. target.ApplyTemplate();
  100. target.Measure(new Size(100, 100));
  101. Assert.Equal(10, target.Panel.Children.Count);
  102. target.Arrange(new Rect(0, 0, 100, 100));
  103. Assert.Equal(10, target.Panel.Children.Count);
  104. }
  105. [Fact]
  106. public void Should_Only_Create_Enough_Containers_To_Display_All_Items()
  107. {
  108. var target = CreateTarget(itemCount: 2);
  109. target.ApplyTemplate();
  110. target.Measure(new Size(100, 100));
  111. target.Arrange(new Rect(0, 0, 100, 100));
  112. Assert.Equal(2, target.Panel.Children.Count);
  113. }
  114. [Fact]
  115. public void Should_Expand_To_Fit_Containers_When_Flexible_Size()
  116. {
  117. var target = CreateTarget();
  118. target.ApplyTemplate();
  119. target.Measure(Size.Infinity);
  120. target.Arrange(new Rect(target.DesiredSize));
  121. Assert.Equal(new Size(10, 200), target.DesiredSize);
  122. Assert.Equal(new Size(10, 200), target.Bounds.Size);
  123. Assert.Equal(20, target.Panel.Children.Count);
  124. }
  125. [Fact]
  126. public void Initial_Item_DataContexts_Should_Be_Correct()
  127. {
  128. var target = CreateTarget();
  129. var items = (IList<string>)target.Items;
  130. target.ApplyTemplate();
  131. target.Measure(new Size(100, 100));
  132. target.Arrange(new Rect(0, 0, 100, 100));
  133. for (var i = 0; i < target.Panel.Children.Count; ++i)
  134. {
  135. Assert.Equal(items[i], target.Panel.Children[i].DataContext);
  136. }
  137. }
  138. [Fact]
  139. public void Should_Add_New_Items_When_Control_Is_Enlarged()
  140. {
  141. var target = CreateTarget();
  142. var items = (IList<string>)target.Items;
  143. target.ApplyTemplate();
  144. target.Measure(new Size(100, 100));
  145. target.Arrange(new Rect(0, 0, 100, 100));
  146. Assert.Equal(10, target.Panel.Children.Count);
  147. target.Measure(new Size(120, 120));
  148. target.Arrange(new Rect(0, 0, 100, 120));
  149. Assert.Equal(12, target.Panel.Children.Count);
  150. for (var i = 0; i < target.Panel.Children.Count; ++i)
  151. {
  152. Assert.Equal(items[i], target.Panel.Children[i].DataContext);
  153. }
  154. }
  155. [Fact]
  156. public void Changing_VirtualizationMode_None_To_Simple_Should_Update_Control()
  157. {
  158. var target = CreateTarget(mode: ItemVirtualizationMode.None);
  159. var scroll = (ScrollContentPresenter)target.Parent;
  160. scroll.Measure(new Size(100, 100));
  161. scroll.Arrange(new Rect(0, 0, 100, 100));
  162. Assert.Equal(20, target.Panel.Children.Count);
  163. Assert.Equal(new Size(100, 200), scroll.Extent);
  164. Assert.Equal(new Size(100, 100), scroll.Viewport);
  165. target.VirtualizationMode = ItemVirtualizationMode.Simple;
  166. target.Measure(new Size(100, 100));
  167. target.Arrange(new Rect(0, 0, 100, 100));
  168. Assert.Equal(10, target.Panel.Children.Count);
  169. Assert.Equal(new Size(10, 20), scroll.Extent);
  170. Assert.Equal(new Size(100, 10), scroll.Viewport);
  171. }
  172. [Fact]
  173. public void Changing_VirtualizationMode_None_To_Simple_Should_Add_Correct_Number_Of_Controls()
  174. {
  175. using (UnitTestApplication.Start(new TestServices()))
  176. {
  177. var target = CreateTarget(mode: ItemVirtualizationMode.None);
  178. var scroll = (TestScroller)target.Parent;
  179. scroll.Width = scroll.Height = 100;
  180. scroll.LayoutManager.ExecuteInitialLayoutPass(scroll);
  181. // Ensure than an intermediate measure pass doesn't add more controls than it
  182. // should. This can happen if target gets measured with Size.Infinity which
  183. // is what the available size should be when VirtualizationMode == None but not
  184. // what it should after VirtualizationMode is changed to Simple.
  185. target.Panel.Children.CollectionChanged += (s, e) =>
  186. {
  187. Assert.InRange(target.Panel.Children.Count, 0, 10);
  188. };
  189. target.VirtualizationMode = ItemVirtualizationMode.Simple;
  190. ((ILayoutRoot)scroll.GetVisualRoot()).LayoutManager.ExecuteLayoutPass();
  191. Assert.Equal(10, target.Panel.Children.Count);
  192. }
  193. }
  194. [Fact]
  195. public void Changing_VirtualizationMode_Simple_To_None_Should_Update_Control()
  196. {
  197. var target = CreateTarget();
  198. var scroll = (ScrollContentPresenter)target.Parent;
  199. scroll.Measure(new Size(100, 100));
  200. scroll.Arrange(new Rect(0, 0, 100, 100));
  201. Assert.Equal(10, target.Panel.Children.Count);
  202. Assert.Equal(new Size(10, 20), scroll.Extent);
  203. Assert.Equal(new Size(100, 10), scroll.Viewport);
  204. target.VirtualizationMode = ItemVirtualizationMode.None;
  205. target.Measure(new Size(100, 100));
  206. target.Arrange(new Rect(0, 0, 100, 100));
  207. // Here - unlike changing the other way - we need to do a layout pass on the scroll
  208. // content presenter as non-logical scroll values are only updated on arrange.
  209. scroll.Measure(new Size(100, 100));
  210. scroll.Arrange(new Rect(0, 0, 100, 100));
  211. Assert.Equal(20, target.Panel.Children.Count);
  212. Assert.Equal(new Size(100, 200), scroll.Extent);
  213. Assert.Equal(new Size(100, 100), scroll.Viewport);
  214. }
  215. private static ItemsPresenter CreateTarget(
  216. ItemVirtualizationMode mode = ItemVirtualizationMode.Simple,
  217. Orientation orientation = Orientation.Vertical,
  218. bool useContainers = true,
  219. int itemCount = 20)
  220. {
  221. ItemsPresenter result;
  222. var items = Enumerable.Range(0, itemCount).Select(x => $"Item {x}").ToList();
  223. var scroller = new TestScroller
  224. {
  225. CanHorizontallyScroll = false,
  226. CanVerticallyScroll = true,
  227. Content = result = new TestItemsPresenter(useContainers)
  228. {
  229. Items = items,
  230. ItemsPanel = VirtualizingPanelTemplate(orientation),
  231. ItemTemplate = ItemTemplate(),
  232. VirtualizationMode = mode,
  233. }
  234. };
  235. scroller.UpdateChild();
  236. return result;
  237. }
  238. private static IDataTemplate ItemTemplate()
  239. {
  240. return new FuncDataTemplate<string>(x => new Canvas
  241. {
  242. Width = 10,
  243. Height = 10,
  244. });
  245. }
  246. private static ITemplate<IPanel> VirtualizingPanelTemplate(
  247. Orientation orientation = Orientation.Vertical)
  248. {
  249. return new FuncTemplate<IPanel>(() => new VirtualizingStackPanel
  250. {
  251. Orientation = orientation,
  252. });
  253. }
  254. private class TestScroller : ScrollContentPresenter, IRenderRoot, ILayoutRoot
  255. {
  256. public IRenderer Renderer { get; }
  257. public Size ClientSize { get; }
  258. public double RenderScaling => 1;
  259. public Size MaxClientSize => Size.Infinity;
  260. public double LayoutScaling => 1;
  261. public ILayoutManager LayoutManager { get; } = new LayoutManager();
  262. public IRenderTarget CreateRenderTarget()
  263. {
  264. throw new NotImplementedException();
  265. }
  266. public void Invalidate(Rect rect)
  267. {
  268. throw new NotImplementedException();
  269. }
  270. public Point PointToClient(Point point)
  271. {
  272. throw new NotImplementedException();
  273. }
  274. public Point PointToScreen(Point point)
  275. {
  276. throw new NotImplementedException();
  277. }
  278. }
  279. private class TestItemsPresenter : ItemsPresenter
  280. {
  281. private bool _useContainers;
  282. public TestItemsPresenter(bool useContainers)
  283. {
  284. _useContainers = useContainers;
  285. }
  286. protected override IItemContainerGenerator CreateItemContainerGenerator()
  287. {
  288. return _useContainers ?
  289. new ItemContainerGenerator<TestContainer>(this, TestContainer.ContentProperty, null) :
  290. new ItemContainerGenerator(this);
  291. }
  292. }
  293. private class TestContainer : ContentControl
  294. {
  295. public TestContainer()
  296. {
  297. Width = 10;
  298. Height = 10;
  299. }
  300. }
  301. }
  302. }