ItemsPresenterTests_Virtualization.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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.Collections.Generic;
  4. using System.Linq;
  5. using Avalonia.Controls.Generators;
  6. using Avalonia.Controls.Presenters;
  7. using Avalonia.Controls.Primitives;
  8. using Avalonia.Controls.Templates;
  9. using Xunit;
  10. namespace Avalonia.Controls.UnitTests.Presenters
  11. {
  12. public class ItemsPresenterTests_Virtualization
  13. {
  14. [Fact]
  15. public void Should_Return_IsLogicalScrollEnabled_False_When_Has_No_Virtualizing_Panel()
  16. {
  17. var target = CreateTarget();
  18. target.ClearValue(ItemsPresenter.ItemsPanelProperty);
  19. target.ApplyTemplate();
  20. Assert.False(((ILogicalScrollable)target).IsLogicalScrollEnabled);
  21. }
  22. [Fact]
  23. public void Should_Return_IsLogicalScrollEnabled_False_When_VirtualizationMode_None()
  24. {
  25. var target = CreateTarget(ItemVirtualizationMode.None);
  26. target.ApplyTemplate();
  27. Assert.False(((ILogicalScrollable)target).IsLogicalScrollEnabled);
  28. }
  29. [Fact]
  30. public void Should_Return_IsLogicalScrollEnabled_False_When_Doesnt_Have_ScrollPresenter_Parent()
  31. {
  32. var target = new ItemsPresenter
  33. {
  34. ItemsPanel = VirtualizingPanelTemplate(),
  35. ItemTemplate = ItemTemplate(),
  36. VirtualizationMode = ItemVirtualizationMode.Simple,
  37. };
  38. target.ApplyTemplate();
  39. Assert.False(((ILogicalScrollable)target).IsLogicalScrollEnabled);
  40. }
  41. [Fact]
  42. public void Should_Return_IsLogicalScrollEnabled_True()
  43. {
  44. var target = CreateTarget();
  45. target.ApplyTemplate();
  46. Assert.True(((ILogicalScrollable)target).IsLogicalScrollEnabled);
  47. }
  48. [Fact]
  49. public void Should_Fill_Panel_With_Containers()
  50. {
  51. var target = CreateTarget();
  52. target.ApplyTemplate();
  53. target.Measure(new Size(100, 100));
  54. target.Arrange(new Rect(0, 0, 100, 100));
  55. Assert.Equal(10, target.Panel.Children.Count);
  56. }
  57. [Fact]
  58. public void Should_Only_Create_Enough_Containers_To_Display_All_Items()
  59. {
  60. var target = CreateTarget(itemCount: 2);
  61. target.ApplyTemplate();
  62. target.Measure(new Size(100, 100));
  63. target.Arrange(new Rect(0, 0, 100, 100));
  64. Assert.Equal(2, target.Panel.Children.Count);
  65. }
  66. [Fact]
  67. public void Initial_Item_DataContexts_Should_Be_Correct()
  68. {
  69. var target = CreateTarget();
  70. var items = (IList<string>)target.Items;
  71. target.ApplyTemplate();
  72. target.Measure(new Size(100, 100));
  73. target.Arrange(new Rect(0, 0, 100, 100));
  74. for (var i = 0; i < target.Panel.Children.Count; ++i)
  75. {
  76. Assert.Equal(items[i], target.Panel.Children[i].DataContext);
  77. }
  78. }
  79. [Fact]
  80. public void Should_Add_New_Items_When_Control_Is_Enlarged()
  81. {
  82. var target = CreateTarget();
  83. var items = (IList<string>)target.Items;
  84. target.ApplyTemplate();
  85. target.Measure(new Size(100, 100));
  86. target.Arrange(new Rect(0, 0, 100, 100));
  87. Assert.Equal(10, target.Panel.Children.Count);
  88. target.Arrange(new Rect(0, 0, 100, 120));
  89. Assert.Equal(12, target.Panel.Children.Count);
  90. for (var i = 0; i < target.Panel.Children.Count; ++i)
  91. {
  92. Assert.Equal(items[i], target.Panel.Children[i].DataContext);
  93. }
  94. }
  95. private static ItemsPresenter CreateTarget(
  96. ItemVirtualizationMode mode = ItemVirtualizationMode.Simple,
  97. Orientation orientation = Orientation.Vertical,
  98. bool useContainers = true,
  99. int itemCount = 20)
  100. {
  101. ItemsPresenter result;
  102. var items = Enumerable.Range(0, itemCount).Select(x => $"Item {x}").ToList();
  103. var scroller = new ScrollContentPresenter
  104. {
  105. Content = result = new TestItemsPresenter(useContainers)
  106. {
  107. Items = items,
  108. ItemsPanel = VirtualizingPanelTemplate(orientation),
  109. ItemTemplate = ItemTemplate(),
  110. VirtualizationMode = mode,
  111. }
  112. };
  113. scroller.UpdateChild();
  114. return result;
  115. }
  116. private static IDataTemplate ItemTemplate()
  117. {
  118. return new FuncDataTemplate<string>(x => new Canvas
  119. {
  120. Width = 10,
  121. Height = 10,
  122. });
  123. }
  124. private static ITemplate<IPanel> VirtualizingPanelTemplate(
  125. Orientation orientation = Orientation.Vertical)
  126. {
  127. return new FuncTemplate<IPanel>(() => new VirtualizingStackPanel
  128. {
  129. Orientation = orientation,
  130. });
  131. }
  132. private class TestItemsPresenter : ItemsPresenter
  133. {
  134. private bool _useContainers;
  135. public TestItemsPresenter(bool useContainers)
  136. {
  137. _useContainers = useContainers;
  138. }
  139. protected override IItemContainerGenerator CreateItemContainerGenerator()
  140. {
  141. return _useContainers ?
  142. new ItemContainerGenerator<TestContainer>(this, TestContainer.ContentProperty, null) :
  143. new ItemContainerGenerator(this);
  144. }
  145. }
  146. private class TestContainer : ContentControl
  147. {
  148. public TestContainer()
  149. {
  150. Width = 10;
  151. Height = 10;
  152. }
  153. }
  154. }
  155. }