VirtualizingStackPanelTests.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 Avalonia.Controls.Primitives;
  4. using Avalonia.Input;
  5. using Avalonia.LogicalTree;
  6. using Moq;
  7. using Xunit;
  8. namespace Avalonia.Controls.UnitTests
  9. {
  10. public class VirtualizingStackPanelTests
  11. {
  12. public class Vertical
  13. {
  14. [Fact]
  15. public void Measure_Invokes_Controller_UpdateControls()
  16. {
  17. var target = (IVirtualizingPanel)new VirtualizingStackPanel();
  18. var controller = new Mock<IVirtualizingController>();
  19. target.Controller = controller.Object;
  20. target.Measure(new Size(100, 100));
  21. controller.Verify(x => x.UpdateControls(), Times.Once());
  22. }
  23. [Fact]
  24. public void Measure_Invokes_Controller_UpdateControls_If_AvailableSize_Changes()
  25. {
  26. var target = (IVirtualizingPanel)new VirtualizingStackPanel();
  27. var controller = new Mock<IVirtualizingController>();
  28. target.Controller = controller.Object;
  29. target.Measure(new Size(100, 100));
  30. target.InvalidateMeasure();
  31. target.Measure(new Size(100, 100));
  32. target.InvalidateMeasure();
  33. target.Measure(new Size(100, 101));
  34. controller.Verify(x => x.UpdateControls(), Times.Exactly(2));
  35. }
  36. [Fact]
  37. public void Measure_Does_Not_Invoke_Controller_UpdateControls_If_AvailableSize_Is_The_Same()
  38. {
  39. var target = (IVirtualizingPanel)new VirtualizingStackPanel();
  40. var controller = new Mock<IVirtualizingController>();
  41. target.Controller = controller.Object;
  42. target.Measure(new Size(100, 100));
  43. target.InvalidateMeasure();
  44. target.Measure(new Size(100, 100));
  45. controller.Verify(x => x.UpdateControls(), Times.Once());
  46. }
  47. [Fact]
  48. public void Measure_Invokes_Controller_UpdateControls_If_AvailableSize_Is_The_Same_After_ForceInvalidateMeasure()
  49. {
  50. var target = (IVirtualizingPanel)new VirtualizingStackPanel();
  51. var controller = new Mock<IVirtualizingController>();
  52. target.Controller = controller.Object;
  53. target.Measure(new Size(100, 100));
  54. target.ForceInvalidateMeasure();
  55. target.Measure(new Size(100, 100));
  56. controller.Verify(x => x.UpdateControls(), Times.Exactly(2));
  57. }
  58. [Fact]
  59. public void Arrange_Invokes_Controller_UpdateControls()
  60. {
  61. var target = (IVirtualizingPanel)new VirtualizingStackPanel();
  62. var controller = new Mock<IVirtualizingController>();
  63. target.Controller = controller.Object;
  64. target.Measure(new Size(100, 100));
  65. target.Arrange(new Rect(0, 0, 110, 110));
  66. controller.Verify(x => x.UpdateControls(), Times.Exactly(2));
  67. }
  68. [Fact]
  69. public void Reports_IsFull_False_Until_Measure_Height_Is_Reached()
  70. {
  71. var target = (IVirtualizingPanel)new VirtualizingStackPanel();
  72. target.Measure(new Size(100, 100));
  73. Assert.Equal(new Size(0, 0), target.DesiredSize);
  74. Assert.Equal(new Size(0, 0), target.Bounds.Size);
  75. Assert.False(target.IsFull);
  76. Assert.Equal(0, target.OverflowCount);
  77. target.Children.Add(new Canvas { Width = 50, Height = 50 });
  78. Assert.False(target.IsFull);
  79. Assert.Equal(0, target.OverflowCount);
  80. target.Children.Add(new Canvas { Width = 50, Height = 50 });
  81. Assert.True(target.IsFull);
  82. Assert.Equal(0, target.OverflowCount);
  83. }
  84. [Fact]
  85. public void Reports_Overflow_After_Arrange()
  86. {
  87. var target = (IVirtualizingPanel)new VirtualizingStackPanel();
  88. target.Measure(new Size(100, 100));
  89. target.Arrange(new Rect(target.DesiredSize));
  90. Assert.Equal(new Size(0, 0), target.Bounds.Size);
  91. target.Children.Add(new Canvas { Width = 50, Height = 50 });
  92. target.Children.Add(new Canvas { Width = 50, Height = 50 });
  93. target.Children.Add(new Canvas { Width = 50, Height = 50 });
  94. target.Children.Add(new Canvas { Width = 50, Height = 50 });
  95. Assert.Equal(0, target.OverflowCount);
  96. target.Measure(new Size(100, 100));
  97. target.Arrange(new Rect(target.DesiredSize));
  98. Assert.Equal(2, target.OverflowCount);
  99. }
  100. [Fact]
  101. public void Reports_Correct_Overflow_During_Arrange()
  102. {
  103. var target = (IVirtualizingPanel)new VirtualizingStackPanel();
  104. var controller = new Mock<IVirtualizingController>();
  105. var called = false;
  106. target.Children.Add(new Canvas { Width = 50, Height = 50 });
  107. target.Children.Add(new Canvas { Width = 50, Height = 52 });
  108. target.Measure(new Size(100, 100));
  109. controller.Setup(x => x.UpdateControls()).Callback(() =>
  110. {
  111. Assert.Equal(2, target.PixelOverflow);
  112. Assert.Equal(0, target.OverflowCount);
  113. called = true;
  114. });
  115. target.Controller = controller.Object;
  116. target.Arrange(new Rect(target.DesiredSize));
  117. Assert.True(called);
  118. }
  119. [Fact]
  120. public void Reports_PixelOverflow_After_Arrange()
  121. {
  122. var target = (IVirtualizingPanel)new VirtualizingStackPanel();
  123. target.Children.Add(new Canvas { Width = 50, Height = 50 });
  124. target.Children.Add(new Canvas { Width = 50, Height = 52 });
  125. target.Measure(new Size(100, 100));
  126. target.Arrange(new Rect(target.DesiredSize));
  127. Assert.Equal(2, target.PixelOverflow);
  128. }
  129. [Fact]
  130. public void Reports_PixelOverflow_After_Arrange_Smaller_Than_Measure()
  131. {
  132. var target = (IVirtualizingPanel)new VirtualizingStackPanel();
  133. target.Children.Add(new Canvas { Width = 50, Height = 50 });
  134. target.Children.Add(new Canvas { Width = 50, Height = 52 });
  135. target.Measure(new Size(100, 100));
  136. target.Arrange(new Rect(0, 0, 50, 50));
  137. Assert.Equal(52, target.PixelOverflow);
  138. }
  139. [Fact]
  140. public void Reports_PixelOverflow_With_PixelOffset()
  141. {
  142. var target = (IVirtualizingPanel)new VirtualizingStackPanel();
  143. target.Children.Add(new Canvas { Width = 50, Height = 50 });
  144. target.Children.Add(new Canvas { Width = 50, Height = 52 });
  145. target.PixelOffset = 2;
  146. target.Measure(new Size(100, 100));
  147. target.Arrange(new Rect(target.DesiredSize));
  148. Assert.Equal(2, target.PixelOverflow);
  149. }
  150. [Fact]
  151. public void PixelOffset_Can_Be_More_Than_Child_Without_Affecting_IsFull()
  152. {
  153. var target = (IVirtualizingPanel)new VirtualizingStackPanel();
  154. target.Children.Add(new Canvas { Width = 50, Height = 50 });
  155. target.Children.Add(new Canvas { Width = 50, Height = 52 });
  156. target.PixelOffset = 55;
  157. target.Measure(new Size(100, 100));
  158. target.Arrange(new Rect(target.DesiredSize));
  159. Assert.Equal(55, target.PixelOffset);
  160. Assert.Equal(2, target.PixelOverflow);
  161. Assert.True(target.IsFull);
  162. }
  163. [Fact]
  164. public void Passes_Navigation_Request_To_ILogicalScrollable_Parent()
  165. {
  166. var presenter = new Mock<ILogical>().As<IControl>();
  167. var scrollable = presenter.As<ILogicalScrollable>();
  168. var target = (IVirtualizingPanel)new VirtualizingStackPanel();
  169. var from = new Canvas();
  170. scrollable.Setup(x => x.IsLogicalScrollEnabled).Returns(true);
  171. ((ISetLogicalParent)target).SetParent(presenter.Object);
  172. ((INavigableContainer)target).GetControl(NavigationDirection.Next, from, false);
  173. scrollable.Verify(x => x.GetControlInDirection(NavigationDirection.Next, from));
  174. }
  175. }
  176. }
  177. }