ScrollViewerTests.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Copyright (c) The Perspex 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.Linq;
  4. using Perspex.Controls;
  5. using Perspex.Controls.Presenters;
  6. using Perspex.Controls.Primitives;
  7. using Perspex.Controls.Templates;
  8. using Perspex.VisualTree;
  9. using Xunit;
  10. namespace Perspex.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. Assert.IsType<TextBlock>(target.Presenter.Child);
  24. }
  25. [Fact]
  26. public void ScrollViewer_In_Template_Sets_Child_TemplatedParent()
  27. {
  28. var target = new ContentControl
  29. {
  30. Template = new FuncControlTemplate<ContentControl>(CreateNestedTemplate),
  31. Content = "Foo",
  32. };
  33. target.ApplyTemplate();
  34. var presenter = target.GetVisualDescendents()
  35. .OfType<ContentPresenter>()
  36. .Single(x => x.Name == "this");
  37. Assert.Equal(target, presenter.TemplatedParent);
  38. }
  39. [Fact]
  40. public void Offset_Should_Be_Coerced_To_Viewport()
  41. {
  42. var target = new ScrollViewer();
  43. target.SetValue(ScrollViewer.ExtentProperty, new Size(20, 20));
  44. target.SetValue(ScrollViewer.ViewportProperty, new Size(10, 10));
  45. target.Offset = new Vector(12, 12);
  46. Assert.Equal(new Vector(10, 10), target.Offset);
  47. }
  48. private Control CreateTemplate(ScrollViewer control)
  49. {
  50. return new Grid
  51. {
  52. ColumnDefinitions = new ColumnDefinitions
  53. {
  54. new ColumnDefinition(1, GridUnitType.Star),
  55. new ColumnDefinition(GridLength.Auto),
  56. },
  57. RowDefinitions = new RowDefinitions
  58. {
  59. new RowDefinition(1, GridUnitType.Star),
  60. new RowDefinition(GridLength.Auto),
  61. },
  62. Children = new Controls
  63. {
  64. new ScrollContentPresenter
  65. {
  66. Name = "PART_ContentPresenter",
  67. [~ContentPresenter.ContentProperty] = control[~ContentControl.ContentProperty],
  68. [~~ScrollContentPresenter.ExtentProperty] = control[~~ScrollViewer.ExtentProperty],
  69. [~~ScrollContentPresenter.OffsetProperty] = control[~~ScrollViewer.OffsetProperty],
  70. [~~ScrollContentPresenter.ViewportProperty] = control[~~ScrollViewer.ViewportProperty],
  71. [~ScrollContentPresenter.CanScrollHorizontallyProperty] = control[~ScrollViewer.CanScrollHorizontallyProperty],
  72. },
  73. new ScrollBar
  74. {
  75. Name = "horizontalScrollBar",
  76. Orientation = Orientation.Horizontal,
  77. [~RangeBase.MaximumProperty] = control[~ScrollViewer.HorizontalScrollBarMaximumProperty],
  78. [~~RangeBase.ValueProperty] = control[~~ScrollViewer.HorizontalScrollBarValueProperty],
  79. [~ScrollBar.ViewportSizeProperty] = control[~ScrollViewer.HorizontalScrollBarViewportSizeProperty],
  80. [~ScrollBar.VisibilityProperty] = control[~ScrollViewer.HorizontalScrollBarVisibilityProperty],
  81. [Grid.RowProperty] = 1,
  82. },
  83. new ScrollBar
  84. {
  85. Name = "verticalScrollBar",
  86. Orientation = Orientation.Vertical,
  87. [~RangeBase.MaximumProperty] = control[~ScrollViewer.VerticalScrollBarMaximumProperty],
  88. [~~RangeBase.ValueProperty] = control[~~ScrollViewer.VerticalScrollBarValueProperty],
  89. [~ScrollBar.ViewportSizeProperty] = control[~ScrollViewer.VerticalScrollBarViewportSizeProperty],
  90. [~ScrollBar.VisibilityProperty] = control[~ScrollViewer.VerticalScrollBarVisibilityProperty],
  91. [Grid.ColumnProperty] = 1,
  92. },
  93. },
  94. };
  95. }
  96. private Control CreateNestedTemplate(ContentControl control)
  97. {
  98. return new ScrollViewer
  99. {
  100. Template = new FuncControlTemplate<ScrollViewer>(CreateTemplate),
  101. Content = new ContentPresenter
  102. {
  103. Name = "this"
  104. }
  105. };
  106. }
  107. }
  108. }