ItemsPresenterTests_Virtualization.cs 13 KB

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