VirtualizingStackPanelTests.cs 8.6 KB

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