ScrollViewerTests_ILogicalScrollable.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. using System;
  2. using Avalonia.Controls.Presenters;
  3. using Avalonia.Controls.Primitives;
  4. using Avalonia.Controls.Templates;
  5. using Avalonia.Input;
  6. using Avalonia.Styling;
  7. using Avalonia.UnitTests;
  8. using Xunit;
  9. #nullable enable
  10. namespace Avalonia.Controls.UnitTests;
  11. public class ScrollViewerTests_ILogicalScrollable : ScopedTestBase
  12. {
  13. [Fact]
  14. public void Extent_Offset_And_Viewport_Should_Be_Read_From_ILogicalScrollable()
  15. {
  16. var scrollable = new TestScrollable
  17. {
  18. Extent = new Size(100, 100),
  19. Offset = new Vector(50, 50),
  20. Viewport = new Size(25, 25),
  21. };
  22. var target = CreateTarget(scrollable);
  23. Assert.Equal(scrollable.Extent, target.Extent);
  24. Assert.Equal(scrollable.Offset, target.Offset);
  25. Assert.Equal(scrollable.Viewport, target.Viewport);
  26. scrollable.Extent = new Size(200, 200);
  27. scrollable.Offset = new Vector(100, 100);
  28. scrollable.Viewport = new Size(50, 50);
  29. Assert.Equal(scrollable.Extent, target.Extent);
  30. Assert.Equal(scrollable.Offset, target.Offset);
  31. Assert.Equal(scrollable.Viewport, target.Viewport);
  32. }
  33. private static ScrollViewer CreateTarget(object? content)
  34. {
  35. var result = new ScrollViewer
  36. {
  37. Content = content,
  38. };
  39. var root = new TestRoot
  40. {
  41. Resources =
  42. {
  43. { typeof(ScrollViewer), CreateScrollViewerTheme() },
  44. },
  45. Child = result,
  46. };
  47. root.LayoutManager.ExecuteInitialLayoutPass();
  48. return result;
  49. }
  50. private static ControlTheme CreateScrollViewerTheme()
  51. {
  52. return new ControlTheme(typeof(ScrollViewer))
  53. {
  54. Setters =
  55. {
  56. new Setter(TreeView.TemplateProperty, CreateScrollViewerTemplate()),
  57. },
  58. };
  59. }
  60. private static FuncControlTemplate CreateScrollViewerTemplate()
  61. {
  62. return new FuncControlTemplate<ScrollViewer>((parent, scope) =>
  63. new Panel
  64. {
  65. Children =
  66. {
  67. new ScrollContentPresenter
  68. {
  69. Name = "PART_ContentPresenter",
  70. }.RegisterInNameScope(scope),
  71. }
  72. });
  73. }
  74. private class TestScrollable : Control, ILogicalScrollable
  75. {
  76. private Size _extent;
  77. private Vector _offset;
  78. private Size _viewport;
  79. private EventHandler? _scrollInvalidated;
  80. public bool CanHorizontallyScroll { get; set; }
  81. public bool CanVerticallyScroll { get; set; }
  82. public bool IsLogicalScrollEnabled { get; set; } = true;
  83. public Size AvailableSize { get; private set; }
  84. public bool HasScrollInvalidatedSubscriber => _scrollInvalidated != null;
  85. public event EventHandler? ScrollInvalidated
  86. {
  87. add => _scrollInvalidated += value;
  88. remove => _scrollInvalidated -= value;
  89. }
  90. public Size Extent
  91. {
  92. get => _extent;
  93. set
  94. {
  95. _extent = value;
  96. _scrollInvalidated?.Invoke(this, EventArgs.Empty);
  97. }
  98. }
  99. public Vector Offset
  100. {
  101. get => _offset;
  102. set
  103. {
  104. _offset = value;
  105. _scrollInvalidated?.Invoke(this, EventArgs.Empty);
  106. }
  107. }
  108. public Size Viewport
  109. {
  110. get => _viewport;
  111. set
  112. {
  113. _viewport = value;
  114. _scrollInvalidated?.Invoke(this, EventArgs.Empty);
  115. }
  116. }
  117. public Size ScrollSize => new(double.PositiveInfinity, 1);
  118. public Size PageScrollSize => new(double.PositiveInfinity, Viewport.Height);
  119. public bool BringIntoView(Control target, Rect targetRect)
  120. {
  121. throw new NotImplementedException();
  122. }
  123. public void RaiseScrollInvalidated(EventArgs e)
  124. {
  125. _scrollInvalidated?.Invoke(this, e);
  126. }
  127. protected override Size MeasureOverride(Size availableSize)
  128. {
  129. AvailableSize = availableSize;
  130. return new Size(150, 150);
  131. }
  132. public Control? GetControlInDirection(NavigationDirection direction, Control? from)
  133. {
  134. throw new NotImplementedException();
  135. }
  136. }
  137. }