From 7aa634cbe730bc5fdee8541a24f75bfb228311bc Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Mon, 2 Feb 2015 23:43:23 +0100 Subject: [PATCH] Started adding TemplateExtensions tests. --- Perspex.Controls/Properties/AssemblyInfo.cs | 3 ++ .../Perspex.Controls.UnitTests.csproj | 1 + .../Templates/TemplateExtensionsTests.cs | 45 +++++++++++++++++++ .../TestTemplatedControl.cs | 5 +++ 4 files changed, 54 insertions(+) create mode 100644 Tests/Perspex.Controls.UnitTests/Templates/TemplateExtensionsTests.cs diff --git a/Perspex.Controls/Properties/AssemblyInfo.cs b/Perspex.Controls/Properties/AssemblyInfo.cs index f39537fd7a..959688c305 100644 --- a/Perspex.Controls/Properties/AssemblyInfo.cs +++ b/Perspex.Controls/Properties/AssemblyInfo.cs @@ -6,6 +6,7 @@ using System.Reflection; using System.Resources; +using System.Runtime.CompilerServices; // General Information about an assembly is controlled through the following // set of attributes. Change these attribute values to modify the information @@ -32,3 +33,5 @@ using System.Resources; // [assembly: AssemblyVersion("1.0.*")] [assembly: AssemblyVersion("1.0.0.0")] [assembly: AssemblyFileVersion("1.0.0.0")] + +[assembly: InternalsVisibleTo("Perspex.Controls.UnitTests")] \ No newline at end of file diff --git a/Tests/Perspex.Controls.UnitTests/Perspex.Controls.UnitTests.csproj b/Tests/Perspex.Controls.UnitTests/Perspex.Controls.UnitTests.csproj index 68f9cdb109..0ff2d44095 100644 --- a/Tests/Perspex.Controls.UnitTests/Perspex.Controls.UnitTests.csproj +++ b/Tests/Perspex.Controls.UnitTests/Perspex.Controls.UnitTests.csproj @@ -93,6 +93,7 @@ + diff --git a/Tests/Perspex.Controls.UnitTests/Templates/TemplateExtensionsTests.cs b/Tests/Perspex.Controls.UnitTests/Templates/TemplateExtensionsTests.cs new file mode 100644 index 0000000000..160f8807be --- /dev/null +++ b/Tests/Perspex.Controls.UnitTests/Templates/TemplateExtensionsTests.cs @@ -0,0 +1,45 @@ +// ----------------------------------------------------------------------- +// +// Copyright 2014 MIT Licence. See licence.md for more information. +// +// ----------------------------------------------------------------------- + +namespace Perspex.Controls.Templates.UnitTests +{ + using System.Linq; + using Perspex.Controls; + using Perspex.Controls.Templates; + using Perspex.Controls.UnitTests; + using Xunit; + + public class TemplateExtensionsTests + { + /// + /// Control templates can themselves contain templated controls. Make sure that + /// GetTemplateChildren returns only controls that have a TemplatedParent of the + /// control that is being searched. + /// + [Fact] + public void GetTemplateChildren_Should_Not_Return_Nested_Template_Controls() + { + var target = new TestTemplatedControl(); + var border1 = new Border { Id = "border1", TemplatedParent = target }; + var inner = new TestTemplatedControl { Id = "inner", TemplatedParent = target }; + var border2 = new Border { Id = "border2", TemplatedParent = inner }; + var border3 = new Border { Id = "border3", TemplatedParent = inner }; + var border4 = new Border { Id = "border4", TemplatedParent = target }; + var border5 = new Border { Id = "border5", TemplatedParent = null }; + + target.AddVisualChild(border1); + border1.Content = inner; + inner.AddVisualChild(border2); + inner.AddVisualChild(border3); + border3.Content = border4; + border4.Content = border5; + + var result = target.GetTemplateChildren().Select(x => x.Id).ToArray(); + + Assert.Equal(new[] { "border1", "inner", "border4" }, result); + } + } +} \ No newline at end of file diff --git a/Tests/Perspex.Controls.UnitTests/TestTemplatedControl.cs b/Tests/Perspex.Controls.UnitTests/TestTemplatedControl.cs index bfe07c9aca..c3a297a895 100644 --- a/Tests/Perspex.Controls.UnitTests/TestTemplatedControl.cs +++ b/Tests/Perspex.Controls.UnitTests/TestTemplatedControl.cs @@ -12,6 +12,11 @@ namespace Perspex.Controls.UnitTests { public bool OnTemplateAppliedCalled { get; private set; } + public new void AddVisualChild(Visual visual) + { + base.AddVisualChild(visual); + } + protected override void OnTemplateApplied() { this.OnTemplateAppliedCalled = true;