| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331 |
- // Copyright (c) The Avalonia Project. All rights reserved.
- // Licensed under the MIT license. See licence.md file in the project root for full license information.
- using System;
- using System.Collections.Generic;
- using System.Reactive.Linq;
- using Avalonia.Controls.Presenters;
- using Avalonia.Controls.Primitives;
- using Avalonia.Layout;
- using Xunit;
- namespace Avalonia.Controls.UnitTests.Presenters
- {
- public class ScrollContentPresenterTests
- {
- [Theory]
- [InlineData(HorizontalAlignment.Stretch, VerticalAlignment.Stretch, 10, 10, 80, 80)]
- [InlineData(HorizontalAlignment.Left, VerticalAlignment.Stretch, 10, 10, 16, 80)]
- [InlineData(HorizontalAlignment.Right, VerticalAlignment.Stretch, 74, 10, 16, 80)]
- [InlineData(HorizontalAlignment.Center, VerticalAlignment.Stretch, 42, 10, 16, 80)]
- [InlineData(HorizontalAlignment.Stretch, VerticalAlignment.Top, 10, 10, 80, 16)]
- [InlineData(HorizontalAlignment.Stretch, VerticalAlignment.Bottom, 10, 74, 80, 16)]
- [InlineData(HorizontalAlignment.Stretch, VerticalAlignment.Center, 10, 42, 80, 16)]
- public void Alignment_And_Padding_Are_Applied_To_Child_Bounds(
- HorizontalAlignment h,
- VerticalAlignment v,
- double expectedX,
- double expectedY,
- double expectedWidth,
- double expectedHeight)
- {
- Border content;
- var target = new ScrollContentPresenter
- {
- Padding = new Thickness(10),
- Content = content = new Border
- {
- MinWidth = 16,
- MinHeight = 16,
- HorizontalAlignment = h,
- VerticalAlignment = v,
- },
- };
- target.UpdateChild();
- target.Measure(new Size(100, 100));
- target.Arrange(new Rect(0, 0, 100, 100));
- Assert.Equal(new Rect(expectedX, expectedY, expectedWidth, expectedHeight), content.Bounds);
- }
- [Fact]
- public void DesiredSize_Is_Content_Size_When_Smaller_Than_AvailableSize()
- {
- var target = new ScrollContentPresenter
- {
- Padding = new Thickness(10),
- Content = new Border
- {
- MinWidth = 16,
- MinHeight = 16,
- },
- };
- target.UpdateChild();
- target.Measure(new Size(100, 100));
- target.Arrange(new Rect(0, 0, 100, 100));
- Assert.Equal(new Size(16, 16), target.DesiredSize);
- }
- [Fact]
- public void DesiredSize_Is_AvailableSize_When_Content_Larger_Than_AvailableSize()
- {
- var target = new ScrollContentPresenter
- {
- Padding = new Thickness(10),
- Content = new Border
- {
- MinWidth = 160,
- MinHeight = 160,
- },
- };
- target.UpdateChild();
- target.Measure(new Size(100, 100));
- target.Arrange(new Rect(0, 0, 100, 100));
- Assert.Equal(new Size(100, 100), target.DesiredSize);
- }
- [Fact]
- public void Content_Can_Be_Larger_Than_Viewport()
- {
- TestControl content;
- var target = new ScrollContentPresenter
- {
- CanHorizontallyScroll = true,
- CanVerticallyScroll = true,
- Content = content = new TestControl(),
- };
- target.UpdateChild();
- target.Measure(new Size(100, 100));
- target.Arrange(new Rect(0, 0, 100, 100));
- Assert.Equal(new Rect(0, 0, 150, 150), content.Bounds);
- }
- [Fact]
- public void Content_Can_Be_Offset()
- {
- Border content;
- var target = new ScrollContentPresenter
- {
- CanHorizontallyScroll = true,
- CanVerticallyScroll = true,
- Content = content = new Border
- {
- Width = 150,
- Height = 150,
- },
- Offset = new Vector(25, 25),
- };
- target.UpdateChild();
- target.Measure(new Size(100, 100));
- target.Arrange(new Rect(0, 0, 100, 100));
- Assert.Equal(new Rect(-25, -25, 150, 150), content.Bounds);
- }
- [Fact]
- public void Measure_Should_Pass_Bounded_X_If_CannotScrollHorizontally()
- {
- var child = new TestControl();
- var target = new ScrollContentPresenter
- {
- CanVerticallyScroll = true,
- Content = child,
- };
- target.UpdateChild();
- target.Measure(new Size(100, 100));
- Assert.Equal(new Size(100, double.PositiveInfinity), child.AvailableSize);
- }
- [Fact]
- public void Measure_Should_Pass_Unbounded_X_If_CanScrollHorizontally()
- {
- var child = new TestControl();
- var target = new ScrollContentPresenter
- {
- CanHorizontallyScroll = true,
- CanVerticallyScroll = true,
- Content = child,
- };
- target.UpdateChild();
- target.Measure(new Size(100, 100));
- Assert.Equal(Size.Infinity, child.AvailableSize);
- }
- [Fact]
- public void Arrange_Should_Set_Viewport_And_Extent_In_That_Order()
- {
- var target = new ScrollContentPresenter
- {
- Content = new Border { Width = 40, Height = 50 }
- };
- var set = new List<string>();
- target.UpdateChild();
- target.Measure(new Size(100, 100));
- target.GetObservable(ScrollViewer.ViewportProperty).Skip(1).Subscribe(_ => set.Add("Viewport"));
- target.GetObservable(ScrollViewer.ExtentProperty).Skip(1).Subscribe(_ => set.Add("Extent"));
- target.Arrange(new Rect(0, 0, 100, 100));
- Assert.Equal(new[] { "Viewport", "Extent" }, set);
- }
- [Fact]
- public void Should_Correctly_Arrange_Child_Larger_Than_Viewport()
- {
- var child = new Canvas { MinWidth = 150, MinHeight = 150 };
- var target = new ScrollContentPresenter { Content = child, };
- target.UpdateChild();
- target.Measure(Size.Infinity);
- target.Arrange(new Rect(0, 0, 100, 100));
- Assert.Equal(new Size(150, 150), child.Bounds.Size);
- }
- [Fact]
- public void Arrange_Should_Constrain_Child_Width_When_CanHorizontallyScroll_False()
- {
- var child = new WrapPanel
- {
- Children =
- {
- new Border { Width = 40, Height = 50 },
- new Border { Width = 40, Height = 50 },
- new Border { Width = 40, Height = 50 },
- }
- };
- var target = new ScrollContentPresenter
- {
- Content = child,
- CanHorizontallyScroll = false,
- };
- target.UpdateChild();
- target.Measure(Size.Infinity);
- target.Arrange(new Rect(0, 0, 100, 100));
- Assert.Equal(100, child.Bounds.Width);
- }
- [Fact]
- public void Extent_Width_Should_Be_Arrange_Width_When_CanScrollHorizontally_False()
- {
- var child = new WrapPanel
- {
- Children =
- {
- new Border { Width = 40, Height = 50 },
- new Border { Width = 40, Height = 50 },
- new Border { Width = 40, Height = 50 },
- }
- };
- var target = new ScrollContentPresenter
- {
- Content = child,
- CanHorizontallyScroll = false,
- };
- target.UpdateChild();
- target.Measure(Size.Infinity);
- target.Arrange(new Rect(0, 0, 100, 100));
- Assert.Equal(new Size(100, 100), target.Extent);
- }
- [Fact]
- public void Setting_Offset_Should_Invalidate_Arrange()
- {
- var target = new ScrollContentPresenter
- {
- Content = new Border { Width = 140, Height = 150 }
- };
- target.UpdateChild();
- target.Measure(new Size(100, 100));
- target.Arrange(new Rect(0, 0, 100, 100));
- target.Offset = new Vector(10, 100);
- Assert.True(target.IsMeasureValid);
- Assert.False(target.IsArrangeValid);
- }
- [Fact]
- public void BringDescendantIntoView_Should_Update_Offset()
- {
- var target = new ScrollContentPresenter
- {
- Width = 100,
- Height = 100,
- Content = new Border
- {
- Width = 200,
- Height = 200,
- }
- };
- target.UpdateChild();
- target.Measure(Size.Infinity);
- target.Arrange(new Rect(0, 0, 100, 100));
- target.BringDescendantIntoView(target.Child, new Rect(200, 200, 0, 0));
- Assert.Equal(new Vector(100, 100), target.Offset);
- }
- [Fact]
- public void BringDescendantIntoView_Should_Handle_Child_Margin()
- {
- Border border;
- var target = new ScrollContentPresenter
- {
- CanHorizontallyScroll = true,
- CanVerticallyScroll = true,
- Width = 100,
- Height = 100,
- Content = new Decorator
- {
- Margin = new Thickness(50),
- Child = border = new Border
- {
- Width = 200,
- Height = 200,
- }
- }
- };
- target.UpdateChild();
- target.Measure(Size.Infinity);
- target.Arrange(new Rect(0, 0, 100, 100));
- target.BringDescendantIntoView(border, new Rect(200, 200, 0, 0));
- Assert.Equal(new Vector(150, 150), target.Offset);
- }
- private class TestControl : Control
- {
- public Size AvailableSize { get; private set; }
- protected override Size MeasureOverride(Size availableSize)
- {
- AvailableSize = availableSize;
- return new Size(150, 150);
- }
- }
- }
- }
|