diff --git a/Perspex.UnitTests/Controls/TemplatedControlTests.cs b/Perspex.UnitTests/Controls/TemplatedControlTests.cs new file mode 100644 index 0000000000..1f033f7f60 --- /dev/null +++ b/Perspex.UnitTests/Controls/TemplatedControlTests.cs @@ -0,0 +1,100 @@ +// ----------------------------------------------------------------------- +// +// Copyright 2013 MIT Licence. See licence.md for more information. +// +// ----------------------------------------------------------------------- + +namespace Perspex.UnitTests.Controls +{ + using System; + using System.Collections.Generic; + using System.Linq; + using System.Text; + using System.Threading.Tasks; + using Microsoft.VisualStudio.TestTools.UnitTesting; + using Perspex.Controls; + + [TestClass] + public class TemplatedControlTests + { + [TestMethod] + public void Template_Doesnt_Get_Executed_On_Set() + { + bool executed = false; + + var template = new ControlTemplate(_ => + { + executed = true; + return new Control(); + }); + + var target = new TemplatedControl + { + Template = template, + }; + + Assert.IsFalse(executed); + } + + [TestMethod] + public void Template_Gets_Executed_On_Reading_Visual_Children() + { + bool executed = false; + + var template = new ControlTemplate(_ => + { + executed = true; + return new Control(); + }); + + var target = new TemplatedControl + { + Template = template, + }; + + var children = ((IVisual)target).VisualChildren.ToArray(); + + Assert.IsTrue(executed); + } + + [TestMethod] + public void Template_Result_Becomes_Visual_Child() + { + Control templateResult = new Control(); + + var template = new ControlTemplate(_ => + { + return templateResult; + }); + + var target = new TemplatedControl + { + Template = template, + }; + + var children = ((IVisual)target).VisualChildren.ToArray(); + + CollectionAssert.AreEqual(new[] { templateResult }, children); + } + + [TestMethod] + public void TemplatedParent_Is_Set_On_Generated_Template() + { + Control templateResult = new Control(); + + var template = new ControlTemplate(_ => + { + return templateResult; + }); + + var target = new TemplatedControl + { + Template = template, + }; + + var children = ((IVisual)target).VisualChildren.ToArray(); + + Assert.AreEqual(target, templateResult.TemplatedParent); + } + } +} diff --git a/Perspex.UnitTests/Perspex.UnitTests.csproj b/Perspex.UnitTests/Perspex.UnitTests.csproj index 57b71a448d..d3a7c5292d 100644 --- a/Perspex.UnitTests/Perspex.UnitTests.csproj +++ b/Perspex.UnitTests/Perspex.UnitTests.csproj @@ -69,6 +69,7 @@ + diff --git a/Perspex/Controls/TemplatedControl.cs b/Perspex/Controls/TemplatedControl.cs index e5df95b01d..8bbf73e55f 100644 --- a/Perspex/Controls/TemplatedControl.cs +++ b/Perspex/Controls/TemplatedControl.cs @@ -11,7 +11,7 @@ namespace Perspex.Controls using System.Linq; using Perspex.Media; - public abstract class TemplatedControl : Control, IVisual, ITemplatedControl + public class TemplatedControl : Control, IVisual, ITemplatedControl { public static readonly PerspexProperty TemplateProperty = PerspexProperty.Register("Template");