123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409 |
- using System;
- using System.Reactive.Linq;
- using Avalonia.Controls.Presenters;
- using Avalonia.Controls.Primitives;
- using Avalonia.Input;
- using Avalonia.UnitTests;
- using Xunit;
- namespace Avalonia.Controls.UnitTests
- {
- public class ScrollContentPresenterTests_ILogicalScrollable : ScopedTestBase
- {
- [Fact]
- public void Measure_Should_Pass_Unchanged_Bounds_To_ILogicalScrollable()
- {
- var scrollable = new TestScrollable();
- var target = new ScrollContentPresenter
- {
- Content = scrollable,
- };
- target.UpdateChild();
- target.Measure(new Size(100, 100));
- Assert.Equal(new Size(100, 100), scrollable.AvailableSize);
- }
- [Fact]
- public void Arrange_Should_Not_Offset_ILogicalScrollable_Bounds()
- {
- var scrollable = new TestScrollable
- {
- Extent = new Size(100, 100),
- Offset = new Vector(50, 50),
- Viewport = new Size(25, 25),
- };
- var target = new ScrollContentPresenter
- {
- Content = scrollable,
- };
- target.UpdateChild();
- target.Measure(new Size(100, 100));
- target.Arrange(new Rect(0, 0, 100, 100));
- Assert.Equal(new Rect(0, 0, 100, 100), scrollable.Bounds);
- }
- [Fact]
- public void Arrange_Should_Offset_ILogicalScrollable_Bounds_When_Logical_Scroll_Disabled()
- {
- var scrollable = new TestScrollable
- {
- IsLogicalScrollEnabled = false,
- };
- var target = new ScrollContentPresenter
- {
- CanHorizontallyScroll = true,
- CanVerticallyScroll = true,
- Content = scrollable,
- };
- target.UpdateChild();
- target.Measure(new Size(100, 100));
- target.Arrange(new Rect(0, 0, 100, 100));
- target.Offset = new Vector(25, 25);
-
- target.Measure(new Size(100, 100));
- target.Arrange(new Rect(0, 0, 100, 100));
- Assert.Equal(new Rect(-25, -25, 150, 150), scrollable.Bounds);
- }
- [Fact]
- public void Arrange_Should_Not_Set_Viewport_And_Extent_With_ILogicalScrollable()
- {
- var target = new ScrollContentPresenter
- {
- Content = new TestScrollable()
- };
- var changed = false;
- target.UpdateChild();
- target.Measure(new Size(100, 100));
- target.GetObservable(ScrollViewer.ViewportProperty).Skip(1).Subscribe(_ => changed = true);
- target.GetObservable(ScrollViewer.ExtentProperty).Skip(1).Subscribe(_ => changed = true);
- target.Arrange(new Rect(0, 0, 100, 100));
- Assert.False(changed);
- }
- [Fact]
- public void InvalidateScroll_Should_Be_Set_When_Set_As_Content()
- {
- var scrollable = new TestScrollable();
- var target = new ScrollContentPresenter
- {
- Content = scrollable
- };
- target.UpdateChild();
- Assert.True(scrollable.HasScrollInvalidatedSubscriber);
- }
- [Fact]
- public void InvalidateScroll_Should_Be_Cleared_When_Removed_From_Content()
- {
- var scrollable = new TestScrollable();
- var target = new ScrollContentPresenter
- {
- Content = scrollable
- };
- target.UpdateChild();
- target.Content = null;
- target.UpdateChild();
- Assert.False(scrollable.HasScrollInvalidatedSubscriber);
- }
- [Fact]
- public void Extent_Offset_And_Viewport_Should_Be_Read_From_ILogicalScrollable()
- {
- var scrollable = new TestScrollable
- {
- Extent = new Size(100, 100),
- Offset = new Vector(50, 50),
- Viewport = new Size(25, 25),
- };
- var target = new ScrollContentPresenter
- {
- Content = scrollable
- };
- target.UpdateChild();
- Assert.Equal(scrollable.Extent, target.Extent);
- Assert.Equal(scrollable.Offset, target.Offset);
- Assert.Equal(scrollable.Viewport, target.Viewport);
- scrollable.Extent = new Size(200, 200);
- scrollable.Offset = new Vector(100, 100);
- scrollable.Viewport = new Size(50, 50);
- Assert.Equal(scrollable.Extent, target.Extent);
- Assert.Equal(scrollable.Offset, target.Offset);
- Assert.Equal(scrollable.Viewport, target.Viewport);
- }
- [Fact]
- public void Offset_Should_Be_Written_To_ILogicalScrollable()
- {
- var scrollable = new TestScrollable
- {
- Extent = new Size(100, 100),
- Offset = new Vector(50, 50),
- };
- var target = new ScrollContentPresenter
- {
- Content = scrollable
- };
- target.UpdateChild();
- target.Offset = new Vector(25, 25);
- Assert.Equal(target.Offset, scrollable.Offset);
- }
- [Fact]
- public void Offset_Should_Not_Be_Written_To_ILogicalScrollable_After_Removal()
- {
- var scrollable = new TestScrollable
- {
- Extent = new Size(100, 100),
- Offset = new Vector(50, 50),
- };
- var target = new ScrollContentPresenter
- {
- Content = scrollable
- };
- target.Content = null;
- target.Offset = new Vector(25, 25);
- Assert.Equal(new Vector(50, 50), scrollable.Offset);
- }
- [Fact]
- public void Toggling_IsLogicalScrollEnabled_Should_Update_State()
- {
- var scrollable = new TestScrollable
- {
- Extent = new Size(100, 100),
- Offset = new Vector(50, 50),
- Viewport = new Size(25, 25),
- };
- var target = new ScrollContentPresenter
- {
- CanHorizontallyScroll = true,
- CanVerticallyScroll = true,
- Content = scrollable,
- };
- target.UpdateChild();
- target.Measure(new Size(100, 100));
- target.Arrange(new Rect(0, 0, 100, 100));
- Assert.Equal(scrollable.Extent, target.Extent);
- Assert.Equal(scrollable.Offset, target.Offset);
- Assert.Equal(scrollable.Viewport, target.Viewport);
- Assert.Equal(new Rect(0, 0, 100, 100), scrollable.Bounds);
- scrollable.IsLogicalScrollEnabled = false;
- scrollable.RaiseScrollInvalidated(EventArgs.Empty);
- target.Measure(new Size(100, 100));
- target.Arrange(new Rect(0, 0, 100, 100));
- Assert.Equal(new Size(150, 150), target.Extent);
- Assert.Equal(new Vector(0, 0), target.Offset);
- Assert.Equal(new Size(100, 100), target.Viewport);
- Assert.Equal(new Rect(0, 0, 150, 150), scrollable.Bounds);
- scrollable.IsLogicalScrollEnabled = true;
- scrollable.RaiseScrollInvalidated(EventArgs.Empty);
- target.Measure(new Size(100, 100));
- target.Arrange(new Rect(0, 0, 100, 100));
- Assert.Equal(scrollable.Extent, target.Extent);
- Assert.Equal(scrollable.Offset, target.Offset);
- Assert.Equal(scrollable.Viewport, target.Viewport);
- Assert.Equal(new Rect(0, 0, 100, 100), scrollable.Bounds);
- }
- [Fact]
- public void Changing_Content_Should_Update_State()
- {
- var logicalScrollable = new TestScrollable
- {
- Extent = new Size(100, 100),
- Offset = new Vector(50, 50),
- Viewport = new Size(25, 25),
- };
- var nonLogicalScrollable = new TestScrollable
- {
- IsLogicalScrollEnabled = false,
- };
- var target = new ScrollContentPresenter
- {
- CanHorizontallyScroll = true,
- CanVerticallyScroll = true,
- Content = logicalScrollable,
- };
- target.UpdateChild();
- target.Measure(new Size(100, 100));
- target.Arrange(new Rect(0, 0, 100, 100));
- Assert.Equal(logicalScrollable.Extent, target.Extent);
- Assert.Equal(logicalScrollable.Offset, target.Offset);
- Assert.Equal(logicalScrollable.Viewport, target.Viewport);
- Assert.Equal(new Rect(0, 0, 100, 100), logicalScrollable.Bounds);
- target.Content = nonLogicalScrollable;
- target.UpdateChild();
- target.Measure(new Size(100, 100));
- target.Arrange(new Rect(0, 0, 100, 100));
- Assert.Equal(new Size(150, 150), target.Extent);
- Assert.Equal(new Vector(0, 0), target.Offset);
- Assert.Equal(new Size(100, 100), target.Viewport);
- Assert.Equal(new Rect(0, 0, 150, 150), nonLogicalScrollable.Bounds);
- target.Content = logicalScrollable;
- target.UpdateChild();
- target.Measure(new Size(100, 100));
- target.Arrange(new Rect(0, 0, 100, 100));
- Assert.Equal(logicalScrollable.Extent, target.Extent);
- Assert.Equal(logicalScrollable.Offset, target.Offset);
- Assert.Equal(logicalScrollable.Viewport, target.Viewport);
- Assert.Equal(new Rect(0, 0, 100, 100), logicalScrollable.Bounds);
- }
- [Fact]
- public void Should_Set_ILogicalScrolable_CanHorizontallyScroll()
- {
- var logicalScrollable = new TestScrollable();
- var target = new ScrollContentPresenter { Content = logicalScrollable };
- target.UpdateChild();
- Assert.False(logicalScrollable.CanHorizontallyScroll);
- target.CanHorizontallyScroll = true;
- Assert.True(logicalScrollable.CanHorizontallyScroll);
- }
- [Fact]
- public void Should_Set_ILogicalScrolable_CanVerticallyScroll()
- {
- var logicalScrollable = new TestScrollable();
- var target = new ScrollContentPresenter { Content = logicalScrollable };
- target.UpdateChild();
- Assert.False(logicalScrollable.CanVerticallyScroll);
- target.CanVerticallyScroll = true;
- Assert.True(logicalScrollable.CanVerticallyScroll);
- }
- private class TestScrollable : Control, ILogicalScrollable
- {
- private Size _extent;
- private Vector _offset;
- private Size _viewport;
- private EventHandler _scrollInvalidated;
- public bool CanHorizontallyScroll { get; set; }
- public bool CanVerticallyScroll { get; set; }
- public bool IsLogicalScrollEnabled { get; set; } = true;
- public Size AvailableSize { get; private set; }
- public bool HasScrollInvalidatedSubscriber => _scrollInvalidated != null;
-
- public event EventHandler ScrollInvalidated
- {
- add => _scrollInvalidated += value;
- remove => _scrollInvalidated -= value;
- }
- public Size Extent
- {
- get { return _extent; }
- set
- {
- _extent = value;
- _scrollInvalidated?.Invoke(this, EventArgs.Empty);
- }
- }
- public Vector Offset
- {
- get { return _offset; }
- set
- {
- _offset = value;
- _scrollInvalidated?.Invoke(this, EventArgs.Empty);
- }
- }
- public Size Viewport
- {
- get { return _viewport; }
- set
- {
- _viewport = value;
- _scrollInvalidated?.Invoke(this, EventArgs.Empty);
- }
- }
- public Size ScrollSize
- {
- get
- {
- return new Size(double.PositiveInfinity, 1);
- }
- }
- public Size PageScrollSize
- {
- get
- {
- return new Size(double.PositiveInfinity, Viewport.Height);
- }
- }
- public bool BringIntoView(Control target, Rect targetRect)
- {
- throw new NotImplementedException();
- }
- public void RaiseScrollInvalidated(EventArgs e)
- {
- _scrollInvalidated?.Invoke(this, e);
- }
- protected override Size MeasureOverride(Size availableSize)
- {
- AvailableSize = availableSize;
- return new Size(150, 150);
- }
- public Control GetControlInDirection(NavigationDirection direction, Control from)
- {
- throw new NotImplementedException();
- }
- }
- }
- }
|