ItemsPresenterTests_Virtualization.cs 12 KB

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