ItemsPresenterTests_Virtualization.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Avalonia.Controls.Generators;
  5. using Avalonia.Controls.Presenters;
  6. using Avalonia.Controls.Primitives;
  7. using Avalonia.Controls.Templates;
  8. using Avalonia.Layout;
  9. using Avalonia.Platform;
  10. using Avalonia.Rendering;
  11. using Avalonia.UnitTests;
  12. using Avalonia.VisualTree;
  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(10, 20), scroll.Extent);
  91. Assert.Equal(new Size(100, 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 Should_Not_Create_Virtualizer_Before_Panel()
  155. {
  156. var target = CreateTarget();
  157. Assert.Null(target.Panel);
  158. Assert.Null(target.Virtualizer);
  159. }
  160. [Fact]
  161. public void Changing_VirtualizationMode_None_To_Simple_Should_Update_Control()
  162. {
  163. var target = CreateTarget(mode: ItemVirtualizationMode.None);
  164. var scroll = (ScrollContentPresenter)target.Parent;
  165. scroll.Measure(new Size(100, 100));
  166. scroll.Arrange(new Rect(0, 0, 100, 100));
  167. Assert.Equal(20, target.Panel.Children.Count);
  168. Assert.Equal(new Size(100, 200), scroll.Extent);
  169. Assert.Equal(new Size(100, 100), scroll.Viewport);
  170. target.VirtualizationMode = ItemVirtualizationMode.Simple;
  171. target.Measure(new Size(100, 100));
  172. target.Arrange(new Rect(0, 0, 100, 100));
  173. Assert.Equal(10, target.Panel.Children.Count);
  174. Assert.Equal(new Size(10, 20), scroll.Extent);
  175. Assert.Equal(new Size(100, 10), scroll.Viewport);
  176. }
  177. [Fact]
  178. public void Changing_VirtualizationMode_None_To_Simple_Should_Add_Correct_Number_Of_Controls()
  179. {
  180. using (UnitTestApplication.Start(new TestServices()))
  181. {
  182. var target = CreateTarget(mode: ItemVirtualizationMode.None);
  183. var scroll = (TestScroller)target.Parent;
  184. scroll.Width = scroll.Height = 100;
  185. scroll.LayoutManager.ExecuteInitialLayoutPass();
  186. // Ensure than an intermediate measure pass doesn't add more controls than it
  187. // should. This can happen if target gets measured with Size.Infinity which
  188. // is what the available size should be when VirtualizationMode == None but not
  189. // what it should after VirtualizationMode is changed to Simple.
  190. target.Panel.Children.CollectionChanged += (s, e) =>
  191. {
  192. Assert.InRange(target.Panel.Children.Count, 0, 10);
  193. };
  194. target.VirtualizationMode = ItemVirtualizationMode.Simple;
  195. ((ILayoutRoot)scroll.GetVisualRoot()).LayoutManager.ExecuteLayoutPass();
  196. Assert.Equal(10, target.Panel.Children.Count);
  197. }
  198. }
  199. [Fact]
  200. public void Changing_VirtualizationMode_Simple_To_None_Should_Update_Control()
  201. {
  202. var target = CreateTarget();
  203. var scroll = (ScrollContentPresenter)target.Parent;
  204. scroll.Measure(new Size(100, 100));
  205. scroll.Arrange(new Rect(0, 0, 100, 100));
  206. Assert.Equal(10, target.Panel.Children.Count);
  207. Assert.Equal(new Size(10, 20), scroll.Extent);
  208. Assert.Equal(new Size(100, 10), scroll.Viewport);
  209. target.VirtualizationMode = ItemVirtualizationMode.None;
  210. target.Measure(new Size(100, 100));
  211. target.Arrange(new Rect(0, 0, 100, 100));
  212. // Here - unlike changing the other way - we need to do a layout pass on the scroll
  213. // content presenter as non-logical scroll values are only updated on arrange.
  214. scroll.Measure(new Size(100, 100));
  215. scroll.Arrange(new Rect(0, 0, 100, 100));
  216. Assert.Equal(20, target.Panel.Children.Count);
  217. Assert.Equal(new Size(100, 200), scroll.Extent);
  218. Assert.Equal(new Size(100, 100), scroll.Viewport);
  219. }
  220. private static ItemsPresenter CreateTarget(
  221. ItemVirtualizationMode mode = ItemVirtualizationMode.Simple,
  222. Orientation orientation = Orientation.Vertical,
  223. bool useContainers = true,
  224. int itemCount = 20)
  225. {
  226. ItemsPresenter result;
  227. var items = Enumerable.Range(0, itemCount).Select(x => $"Item {x}").ToList();
  228. var scroller = new TestScroller
  229. {
  230. CanHorizontallyScroll = false,
  231. CanVerticallyScroll = true,
  232. Content = result = new TestItemsPresenter(useContainers)
  233. {
  234. Items = items,
  235. ItemsPanel = VirtualizingPanelTemplate(orientation),
  236. ItemTemplate = ItemTemplate(),
  237. VirtualizationMode = mode,
  238. }
  239. };
  240. scroller.UpdateChild();
  241. return result;
  242. }
  243. private static IDataTemplate ItemTemplate()
  244. {
  245. return new FuncDataTemplate<string>((x, _) => new Canvas
  246. {
  247. Width = 10,
  248. Height = 10,
  249. });
  250. }
  251. private static ITemplate<IPanel> VirtualizingPanelTemplate(
  252. Orientation orientation = Orientation.Vertical)
  253. {
  254. return new FuncTemplate<IPanel>(() => new VirtualizingStackPanel
  255. {
  256. Orientation = orientation,
  257. });
  258. }
  259. private class TestScroller : ScrollContentPresenter, IRenderRoot, ILayoutRoot
  260. {
  261. public TestScroller()
  262. {
  263. LayoutManager = new LayoutManager(this);
  264. }
  265. public IRenderer Renderer { get; }
  266. public Size ClientSize { get; }
  267. public double RenderScaling => 1;
  268. public Size MaxClientSize => Size.Infinity;
  269. public double LayoutScaling => 1;
  270. public ILayoutManager LayoutManager { get; }
  271. public IRenderTarget CreateRenderTarget() => throw new NotImplementedException();
  272. public void Invalidate(Rect rect) => throw new NotImplementedException();
  273. public Point PointToClient(PixelPoint p) => throw new NotImplementedException();
  274. public PixelPoint PointToScreen(Point p) => throw new NotImplementedException();
  275. }
  276. private class TestItemsPresenter : ItemsPresenter
  277. {
  278. private bool _useContainers;
  279. public TestItemsPresenter(bool useContainers)
  280. {
  281. _useContainers = useContainers;
  282. }
  283. protected override IItemContainerGenerator CreateItemContainerGenerator()
  284. {
  285. return _useContainers ?
  286. new ItemContainerGenerator<TestContainer>(this, TestContainer.ContentProperty, null) :
  287. new ItemContainerGenerator(this);
  288. }
  289. }
  290. private class TestContainer : ContentControl
  291. {
  292. public TestContainer()
  293. {
  294. Width = 10;
  295. Height = 10;
  296. }
  297. }
  298. }
  299. }