diff --git a/tests/Perspex.Controls.UnitTests/Perspex.Controls.UnitTests.csproj b/tests/Perspex.Controls.UnitTests/Perspex.Controls.UnitTests.csproj index 5b33890f2a..c2535d887e 100644 --- a/tests/Perspex.Controls.UnitTests/Perspex.Controls.UnitTests.csproj +++ b/tests/Perspex.Controls.UnitTests/Perspex.Controls.UnitTests.csproj @@ -122,7 +122,6 @@ - diff --git a/tests/Perspex.Controls.UnitTests/Primitives/TemplatedControlTests.cs b/tests/Perspex.Controls.UnitTests/Primitives/TemplatedControlTests.cs index 40d8e1c0c0..d4d20d3d9d 100644 --- a/tests/Perspex.Controls.UnitTests/Primitives/TemplatedControlTests.cs +++ b/tests/Perspex.Controls.UnitTests/Primitives/TemplatedControlTests.cs @@ -4,11 +4,12 @@ using System; using System.Collections.Generic; using System.Linq; -using Perspex.Collections; +using Moq; using Perspex.Controls.Presenters; using Perspex.Controls.Primitives; using Perspex.Controls.Templates; using Perspex.LogicalTree; +using Perspex.Styling; using Perspex.VisualTree; using Xunit; @@ -17,7 +18,47 @@ namespace Perspex.Controls.UnitTests.Primitives public class TemplatedControlTests { [Fact] - public void ApplyTemplate_Should_Create_Visual_Child() + public void Template_Doesnt_Get_Executed_On_Set() + { + bool executed = false; + + var template = new FuncControlTemplate(_ => + { + executed = true; + return new Control(); + }); + + var target = new TemplatedControl + { + Template = template, + }; + + Assert.False(executed); + } + + [Fact] + public void Template_Gets_Executed_On_Measure() + { + bool executed = false; + + var template = new FuncControlTemplate(_ => + { + executed = true; + return new Control(); + }); + + var target = new TemplatedControl + { + Template = template, + }; + + target.Measure(new Size(100, 100)); + + Assert.True(executed); + } + + [Fact] + public void ApplyTemplate_Should_Create_Visual_Children() { var target = new TemplatedControl { @@ -119,53 +160,81 @@ namespace Perspex.Controls.UnitTests.Primitives } [Fact] - public void Nested_TemplatedControls_Should_Be_Expanded_And_Have_Correct_TemplatedParent() + public void Nested_Templated_Controls_Have_Correct_TemplatedParent() { - var target = new ItemsControl + var target = new TestTemplatedControl { - Template = new FuncControlTemplate(ItemsControlTemplate), - Items = new[] { "Foo", } + Template = new FuncControlTemplate(_ => + { + return new ContentControl + { + Template = new FuncControlTemplate(parent => + { + return new Border + { + Child = new ContentPresenter + { + [~ContentPresenter.ContentProperty] = parent.GetObservable(ContentControl.ContentProperty), + } + }; + }), + Content = new TextBlock + { + } + }; + }), }; target.ApplyTemplate(); - var scrollViewer = target.GetVisualDescendents() - .OfType() - .Single(); - var types = target.GetVisualDescendents() - .Select(x => x.GetType()) - .ToList(); - var templatedParents = target.GetVisualDescendents() - .OfType() - .Select(x => x.TemplatedParent) - .ToList(); + var contentControl = target.GetTemplateChildren().OfType().Single(); + var border = contentControl.GetTemplateChildren().OfType().Single(); + var presenter = contentControl.GetTemplateChildren().OfType().Single(); + var textBlock = (TextBlock)presenter.Content; - Assert.Equal( - new[] - { - typeof(Border), - typeof(ScrollViewer), - typeof(ScrollContentPresenter), - typeof(ItemsPresenter), - typeof(StackPanel), - typeof(TextBlock), - }, - types); + Assert.Equal(target, contentControl.TemplatedParent); + Assert.Equal(contentControl, border.TemplatedParent); + Assert.Equal(contentControl, presenter.TemplatedParent); + Assert.Equal(target, textBlock.TemplatedParent); + } - Assert.Equal( - new object[] + [Fact] + public void Templated_Children_Should_Be_Styled() + { + using (PerspexLocator.EnterScope()) + { + var styler = new Mock(); + + PerspexLocator.CurrentMutable.Bind().ToConstant(styler.Object); + + TestTemplatedControl target; + + var root = new TestRoot { - target, - target, - scrollViewer, - target, - target, - null - }, - templatedParents); - } + Child = target = new TestTemplatedControl + { + Template = new FuncControlTemplate(_ => + { + return new StackPanel + { + Children = new Controls + { + new TextBlock + { + } + } + }; + }), + } + }; + target.ApplyTemplate(); + styler.Verify(x => x.ApplyStyles(It.IsAny()), Times.Once()); + styler.Verify(x => x.ApplyStyles(It.IsAny()), Times.Once()); + styler.Verify(x => x.ApplyStyles(It.IsAny()), Times.Once()); + } + } [Fact] public void Nested_TemplatedControls_Should_Register_With_Correct_NameScope() diff --git a/tests/Perspex.Controls.UnitTests/TemplatedControlTests.cs b/tests/Perspex.Controls.UnitTests/TemplatedControlTests.cs deleted file mode 100644 index dd6ef2e858..0000000000 --- a/tests/Perspex.Controls.UnitTests/TemplatedControlTests.cs +++ /dev/null @@ -1,272 +0,0 @@ -// Copyright (c) The Perspex Project. All rights reserved. -// Licensed under the MIT license. See licence.md file in the project root for full license information. - -using System.Linq; -using Moq; -using Perspex.Controls; -using Perspex.Controls.Presenters; -using Perspex.Controls.Primitives; -using Perspex.Controls.Templates; -using Perspex.Styling; -using Perspex.VisualTree; -using Xunit; - -namespace Perspex.Controls.UnitTests -{ - public class TemplatedControlTests - { - [Fact] - public void Template_Doesnt_Get_Executed_On_Set() - { - bool executed = false; - - var template = new FuncControlTemplate(_ => - { - executed = true; - return new Control(); - }); - - var target = new TemplatedControl - { - Template = template, - }; - - Assert.False(executed); - } - - [Fact] - public void Template_Gets_Executed_On_Measure() - { - bool executed = false; - - var template = new FuncControlTemplate(_ => - { - executed = true; - return new Control(); - }); - - var target = new TemplatedControl - { - Template = template, - }; - - target.Measure(new Size(100, 100)); - - Assert.True(executed); - } - - [Fact] - public void Template_Result_Becomes_Visual_Child() - { - Control templateResult = new Control(); - - var template = new FuncControlTemplate(_ => - { - return templateResult; - }); - - var target = new TemplatedControl - { - Template = template, - }; - - target.Measure(new Size(100, 100)); - var children = target.GetVisualChildren().ToList(); - - Assert.Equal(new[] { templateResult }, children); - } - - [Fact] - public void TemplatedParent_Is_Set_On_Generated_Template() - { - Control templateResult = new Control(); - - var template = new FuncControlTemplate(_ => - { - return templateResult; - }); - - var target = new TemplatedControl - { - Template = template, - }; - - target.Measure(new Size(100, 100)); - - Assert.Equal(target, templateResult.TemplatedParent); - } - - [Fact] - public void OnTemplateApplied_Is_Called() - { - var target = new TestTemplatedControl - { - Template = new FuncControlTemplate(_ => - { - return new Control(); - }) - }; - - target.Measure(new Size(100, 100)); - - Assert.True(target.OnTemplateAppliedCalled); - } - - [Fact] - public void Template_Should_Be_Instantated() - { - var target = new TestTemplatedControl - { - Template = new FuncControlTemplate(_ => - { - return new StackPanel - { - Children = new Controls - { - new TextBlock - { - } - } - }; - }), - }; - - target.ApplyTemplate(); - - var child = target.GetVisualChildren().Single(); - Assert.IsType(child); - child = child.VisualChildren.Single(); - Assert.IsType(child); - } - - [Fact] - public void Templated_Children_Should_Be_Styled() - { - using (PerspexLocator.EnterScope()) - { - var styler = new Mock(); - - PerspexLocator.CurrentMutable.Bind().ToConstant(styler.Object); - - TestTemplatedControl target; - - var root = new TestRoot - { - Child = target = new TestTemplatedControl - { - Template = new FuncControlTemplate(_ => - { - return new StackPanel - { - Children = new Controls - { - new TextBlock - { - } - } - }; - }), - } - }; - - target.ApplyTemplate(); - - styler.Verify(x => x.ApplyStyles(It.IsAny()), Times.Once()); - styler.Verify(x => x.ApplyStyles(It.IsAny()), Times.Once()); - styler.Verify(x => x.ApplyStyles(It.IsAny()), Times.Once()); - } - } - - [Fact] - public void Templated_Children_Should_Have_TemplatedParent_Set() - { - var target = new TestTemplatedControl - { - Template = new FuncControlTemplate(_ => - { - return new StackPanel - { - Children = new Controls - { - new TextBlock - { - } - } - }; - }), - }; - - target.ApplyTemplate(); - - var panel = target.GetTemplateChildren().OfType().Single(); - var textBlock = target.GetTemplateChildren().OfType().Single(); - - Assert.Equal(target, panel.TemplatedParent); - Assert.Equal(target, textBlock.TemplatedParent); - } - - [Fact] - public void Presenter_Children_Should_Not_Have_TemplatedParent_Set() - { - var target = new TestTemplatedControl - { - Template = new FuncControlTemplate(_ => - { - return new ContentPresenter - { - Content = new TextBlock - { - } - }; - }), - }; - - target.ApplyTemplate(); - - var presenter = target.GetTemplateChildren().OfType().Single(); - var textBlock = (TextBlock)presenter.Child; - - Assert.Equal(target, presenter.TemplatedParent); - Assert.Null(textBlock.TemplatedParent); - } - - [Fact] - public void Nested_Templated_Controls_Have_Correct_TemplatedParent() - { - var target = new TestTemplatedControl - { - Template = new FuncControlTemplate(_ => - { - return new ContentControl - { - Template = new FuncControlTemplate(parent => - { - return new Border - { - Child = new ContentPresenter - { - [~ContentPresenter.ContentProperty] = parent.GetObservable(ContentControl.ContentProperty), - } - }; - }), - Content = new TextBlock - { - } - }; - }), - }; - - target.ApplyTemplate(); - - var contentControl = target.GetTemplateChildren().OfType().Single(); - var border = contentControl.GetTemplateChildren().OfType().Single(); - var presenter = contentControl.GetTemplateChildren().OfType().Single(); - var textBlock = (TextBlock)presenter.Content; - - Assert.Equal(target, contentControl.TemplatedParent); - Assert.Equal(contentControl, border.TemplatedParent); - Assert.Equal(contentControl, presenter.TemplatedParent); - Assert.Equal(target, textBlock.TemplatedParent); - } - } -}