From a702b9d37267e466ab92b87632c22f0d1712ffb0 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Mon, 2 Feb 2015 22:59:55 +0100 Subject: [PATCH] Started adding ScrollViewer tests. --- .../Perspex.Controls.UnitTests.csproj | 1 + .../ScrollViewerTests.cs | 114 ++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 Tests/Perspex.Controls.UnitTests/ScrollViewerTests.cs diff --git a/Tests/Perspex.Controls.UnitTests/Perspex.Controls.UnitTests.csproj b/Tests/Perspex.Controls.UnitTests/Perspex.Controls.UnitTests.csproj index 7016fd417f..2f6064d26d 100644 --- a/Tests/Perspex.Controls.UnitTests/Perspex.Controls.UnitTests.csproj +++ b/Tests/Perspex.Controls.UnitTests/Perspex.Controls.UnitTests.csproj @@ -65,6 +65,7 @@ + diff --git a/Tests/Perspex.Controls.UnitTests/ScrollViewerTests.cs b/Tests/Perspex.Controls.UnitTests/ScrollViewerTests.cs new file mode 100644 index 0000000000..6a494577c8 --- /dev/null +++ b/Tests/Perspex.Controls.UnitTests/ScrollViewerTests.cs @@ -0,0 +1,114 @@ +// ----------------------------------------------------------------------- +// +// Copyright 2014 MIT Licence. See licence.md for more information. +// +// ----------------------------------------------------------------------- + +namespace Perspex.Controls.UnitTests +{ + using System.Linq; + using Perspex.Controls; + using Perspex.Controls.Presenters; + using Perspex.Controls.Primitives; + using Perspex.Controls.Templates; + using Perspex.VisualTree; + using Xunit; + + public class ScrollViewerTests + { + [Fact] + public void Content_Is_Created() + { + var target = new ScrollViewer + { + Template = ControlTemplate.Create(this.CreateTemplate), + Content = "Foo", + }; + + target.ApplyTemplate(); + + var presenter = target.GetTemplateChild("contentPresenter"); + + Assert.IsType(presenter.Child); + } + + [Fact] + public void ScrollViewer_In_Template_Sets_Child_TemplatedParent() + { + var target = new ContentControl + { + Template = ControlTemplate.Create(this.CreateNestedTemplate), + Content = "Foo", + }; + + target.ApplyTemplate(); + + var presenter = target.GetVisualDescendents() + .OfType() + .Single(x => x.Id == "this"); + + Assert.Equal(target, presenter.TemplatedParent); + } + + private Control CreateTemplate(ScrollViewer control) + { + return new Grid + { + ColumnDefinitions = new ColumnDefinitions + { + new ColumnDefinition(1, GridUnitType.Star), + new ColumnDefinition(GridLength.Auto), + }, + RowDefinitions = new RowDefinitions + { + new RowDefinition(1, GridUnitType.Star), + new RowDefinition(GridLength.Auto), + }, + Children = new Controls + { + new ScrollContentPresenter + { + Id = "contentPresenter", + [~ScrollContentPresenter.ContentProperty] = control[~ScrollViewer.ContentProperty], + [~~ScrollContentPresenter.ExtentProperty] = control[~~ScrollViewer.ExtentProperty], + [~~ScrollContentPresenter.OffsetProperty] = control[~~ScrollViewer.OffsetProperty], + [~~ScrollContentPresenter.ViewportProperty] = control[~~ScrollViewer.ViewportProperty], + [~ScrollContentPresenter.CanScrollHorizontallyProperty] = control[~ScrollViewer.CanScrollHorizontallyProperty], + }, + new ScrollBar + { + Id = "horizontalScrollBar", + Orientation = Orientation.Horizontal, + [~ScrollBar.MaximumProperty] = control[~ScrollViewer.HorizontalScrollBarMaximumProperty], + [~~ScrollBar.ValueProperty] = control[~~ScrollViewer.HorizontalScrollBarValueProperty], + [~ScrollBar.ViewportSizeProperty] = control[~ScrollViewer.HorizontalScrollBarViewportSizeProperty], + [~ScrollBar.VisibilityProperty] = control[~ScrollViewer.HorizontalScrollBarVisibilityProperty], + [Grid.RowProperty] = 1, + }, + new ScrollBar + { + Id = "verticalScrollBar", + Orientation = Orientation.Vertical, + [~ScrollBar.MaximumProperty] = control[~ScrollViewer.VerticalScrollBarMaximumProperty], + [~~ScrollBar.ValueProperty] = control[~~ScrollViewer.VerticalScrollBarValueProperty], + [~ScrollBar.ViewportSizeProperty] = control[~ScrollViewer.VerticalScrollBarViewportSizeProperty], + [~ScrollBar.VisibilityProperty] = control[~ScrollViewer.VerticalScrollBarVisibilityProperty], + [Grid.ColumnProperty] = 1, + }, + }, + }; + } + + private Control CreateNestedTemplate(ContentControl control) + { + return new ScrollViewer + { + Template = ControlTemplate.Create(this.CreateTemplate), + Content = new ContentPresenter + { + Id = "this" + } + }; + } + } +} \ No newline at end of file