ScrollViewerTests.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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;
  4. using System.Collections.Generic;
  5. using Avalonia.Controls.Presenters;
  6. using Avalonia.Controls.Primitives;
  7. using Avalonia.Controls.Templates;
  8. using Avalonia.Layout;
  9. using Xunit;
  10. namespace Avalonia.Controls.UnitTests
  11. {
  12. public class ScrollViewerTests
  13. {
  14. [Fact]
  15. public void Content_Is_Created()
  16. {
  17. var target = new ScrollViewer
  18. {
  19. Template = new FuncControlTemplate<ScrollViewer>(CreateTemplate),
  20. Content = "Foo",
  21. };
  22. target.ApplyTemplate();
  23. ((ContentPresenter)target.Presenter).UpdateChild();
  24. Assert.IsType<TextBlock>(target.Presenter.Child);
  25. }
  26. [Fact]
  27. public void CanHorizontallyScroll_Should_Track_HorizontalScrollBarVisibility()
  28. {
  29. var target = new ScrollViewer();
  30. var values = new List<bool>();
  31. target.GetObservable(ScrollViewer.CanHorizontallyScrollProperty).Subscribe(x => values.Add(x));
  32. target.HorizontalScrollBarVisibility = ScrollBarVisibility.Disabled;
  33. target.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto;
  34. Assert.Equal(new[] { true, false, true }, values);
  35. }
  36. [Fact]
  37. public void CanVerticallyScroll_Should_Track_VerticalScrollBarVisibility()
  38. {
  39. var target = new ScrollViewer();
  40. var values = new List<bool>();
  41. target.GetObservable(ScrollViewer.CanVerticallyScrollProperty).Subscribe(x => values.Add(x));
  42. target.VerticalScrollBarVisibility = ScrollBarVisibility.Disabled;
  43. target.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
  44. Assert.Equal(new[] { true, false, true }, values);
  45. }
  46. [Fact]
  47. public void Offset_Should_Be_Coerced_To_Viewport()
  48. {
  49. var target = new ScrollViewer();
  50. target.SetValue(ScrollViewer.ExtentProperty, new Size(20, 20));
  51. target.SetValue(ScrollViewer.ViewportProperty, new Size(10, 10));
  52. target.Offset = new Vector(12, 12);
  53. Assert.Equal(new Vector(10, 10), target.Offset);
  54. }
  55. [Fact]
  56. public void Test_ScrollToHome()
  57. {
  58. var target = new ScrollViewer();
  59. target.SetValue(ScrollViewer.ExtentProperty, new Size(50, 50));
  60. target.SetValue(ScrollViewer.ViewportProperty, new Size(10, 10));
  61. target.Offset = new Vector(25, 25);
  62. target.ScrollToHome();
  63. Assert.Equal(new Vector(0, 0), target.Offset);
  64. }
  65. [Fact]
  66. public void Test_ScrollToEnd()
  67. {
  68. var target = new ScrollViewer();
  69. target.SetValue(ScrollViewer.ExtentProperty, new Size(50, 50));
  70. target.SetValue(ScrollViewer.ViewportProperty, new Size(10, 10));
  71. target.Offset = new Vector(25, 25);
  72. target.ScrollToEnd();
  73. Assert.Equal(new Vector(0, 40), target.Offset);
  74. }
  75. private Control CreateTemplate(ScrollViewer control, INameScope scope)
  76. {
  77. return new Grid
  78. {
  79. ColumnDefinitions = new ColumnDefinitions
  80. {
  81. new ColumnDefinition(1, GridUnitType.Star),
  82. new ColumnDefinition(GridLength.Auto),
  83. },
  84. RowDefinitions = new RowDefinitions
  85. {
  86. new RowDefinition(1, GridUnitType.Star),
  87. new RowDefinition(GridLength.Auto),
  88. },
  89. Children =
  90. {
  91. new ScrollContentPresenter
  92. {
  93. Name = "PART_ContentPresenter",
  94. [~ContentPresenter.ContentProperty] = control[~ContentControl.ContentProperty],
  95. [~~ScrollContentPresenter.ExtentProperty] = control[~~ScrollViewer.ExtentProperty],
  96. [~~ScrollContentPresenter.OffsetProperty] = control[~~ScrollViewer.OffsetProperty],
  97. [~~ScrollContentPresenter.ViewportProperty] = control[~~ScrollViewer.ViewportProperty],
  98. [~ScrollContentPresenter.CanHorizontallyScrollProperty] = control[~ScrollViewer.CanHorizontallyScrollProperty],
  99. }.RegisterInNameScope(scope),
  100. new ScrollBar
  101. {
  102. Name = "horizontalScrollBar",
  103. Orientation = Orientation.Horizontal,
  104. [~RangeBase.MaximumProperty] = control[~ScrollViewer.HorizontalScrollBarMaximumProperty],
  105. [~~RangeBase.ValueProperty] = control[~~ScrollViewer.HorizontalScrollBarValueProperty],
  106. [~ScrollBar.ViewportSizeProperty] = control[~ScrollViewer.HorizontalScrollBarViewportSizeProperty],
  107. [~ScrollBar.VisibilityProperty] = control[~ScrollViewer.HorizontalScrollBarVisibilityProperty],
  108. [Grid.RowProperty] = 1,
  109. }.RegisterInNameScope(scope),
  110. new ScrollBar
  111. {
  112. Name = "verticalScrollBar",
  113. Orientation = Orientation.Vertical,
  114. [~RangeBase.MaximumProperty] = control[~ScrollViewer.VerticalScrollBarMaximumProperty],
  115. [~~RangeBase.ValueProperty] = control[~~ScrollViewer.VerticalScrollBarValueProperty],
  116. [~ScrollBar.ViewportSizeProperty] = control[~ScrollViewer.VerticalScrollBarViewportSizeProperty],
  117. [~ScrollBar.VisibilityProperty] = control[~ScrollViewer.VerticalScrollBarVisibilityProperty],
  118. [Grid.ColumnProperty] = 1,
  119. }.RegisterInNameScope(scope),
  120. },
  121. };
  122. }
  123. }
  124. }