diff --git a/Perspex.UnitTests/Styling/SelectorTests.cs b/Perspex.UnitTests/Styling/SelectorTests.cs index 2ae75688c1..a392c624cb 100644 --- a/Perspex.UnitTests/Styling/SelectorTests.cs +++ b/Perspex.UnitTests/Styling/SelectorTests.cs @@ -7,12 +7,13 @@ namespace Perspex.UnitTests.Styling { using System; - using System.Linq; - using System.Reactive; - using System.Reactive.Linq; - using Microsoft.VisualStudio.TestTools.UnitTesting; - using Perspex.Styling; - using Match = Perspex.Styling.Match; +using System.Linq; +using System.Reactive; +using System.Reactive.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Perspex.Controls; +using Perspex.Styling; +using Match = Perspex.Styling.Match; [TestClass] public class SelectorTests @@ -90,6 +91,24 @@ namespace Perspex.UnitTests.Styling CollectionAssert.AreEqual(new[] { false }, target.GetActivator().Take(1).ToEnumerable().ToArray()); } + [TestMethod] + public void Id_Doesnt_Match_Control_With_TemplatedParent() + { + var control = new Control1 { Id = "foo", TemplatedParent = new TemplatedControl1() }; + var target = control.Select().Id("foo"); + + CollectionAssert.AreEqual(new[] { false }, target.GetActivator().Take(1).ToEnumerable().ToArray()); + } + + [TestMethod] + public void Id_Matches_Control_With_TemplatedParent_After_InTemplateOf() + { + var control = new Control1 { Id = "foo", TemplatedParent = new TemplatedControl1() }; + var target = control.Select().InTemplateOf().Id("foo"); + + CollectionAssert.AreEqual(new[] { true }, target.GetActivator().Take(1).ToEnumerable().ToArray()); + } + [TestMethod] public void When_Id_Matches_Control_Other_Selectors_Are_Subscribed() { @@ -112,6 +131,46 @@ namespace Perspex.UnitTests.Styling Assert.AreEqual(0, control.SubscribeCheckObservable.SubscribedCount); } + [TestMethod] + public void InTemplateOf_Matches_Control_Of_Correct_Type() + { + var control = new Control1 { TemplatedParent = new TemplatedControl1() }; + var target = control.Select().InTemplateOf(); + + CollectionAssert.AreEqual(new[] { true }, target.GetActivator().Take(1).ToEnumerable().ToArray()); + } + + [TestMethod] + public void InTemplateOf_Doesnt_Match_Control_Of_Wrong_Type() + { + var control = new Control1 { TemplatedParent = new TemplatedControl1() }; + var target = control.Select().InTemplateOf(); + + CollectionAssert.AreEqual(new[] { false }, target.GetActivator().Take(1).ToEnumerable().ToArray()); + } + + [TestMethod] + public void When_InTemplateOf_Matches_Control_Other_Selectors_Are_Subscribed() + { + var control = new Control1 { TemplatedParent = new TemplatedControl1() }; + var target = control.Select().InTemplateOf().SubscribeCheck(); + + var result = target.GetActivator().ToEnumerable().Take(1).ToArray(); + + Assert.AreEqual(1, control.SubscribeCheckObservable.SubscribedCount); + } + + [TestMethod] + public void When_InTemplateOf_Doesnt_Match_Control_Other_Selectors_Are_Not_Subscribed() + { + var control = new Control1 { TemplatedParent = new TemplatedControl1() }; + var target = control.Select().InTemplateOf().SubscribeCheck(); + + var result = target.GetActivator().ToEnumerable().Take(1).ToArray(); + + Assert.AreEqual(0, control.SubscribeCheckObservable.SubscribedCount); + } + [TestMethod] public void OfType_Matches_Control_Of_Correct_Type() { @@ -159,5 +218,21 @@ namespace Perspex.UnitTests.Styling public class Control2 : SubscribeCheck { } + + public class TemplatedControl1 : TemplatedControl + { + protected override Size MeasureContent(Size availableSize) + { + throw new NotImplementedException(); + } + } + + public class TemplatedControl2 : TemplatedControl + { + protected override Size MeasureContent(Size availableSize) + { + throw new NotImplementedException(); + } + } } } diff --git a/Perspex/Styling/Selectors.cs b/Perspex/Styling/Selectors.cs index 51712705ca..10041af8f1 100644 --- a/Perspex/Styling/Selectors.cs +++ b/Perspex/Styling/Selectors.cs @@ -59,7 +59,7 @@ namespace Perspex.Styling return match; } - public static Match InTemplateOf(this Match match) + public static Match InTemplateOf(this Match match) where T : TemplatedControl { Contract.Requires(match != null); @@ -69,7 +69,7 @@ namespace Perspex.Styling return match; } - public static Match OfType(this Match match) + public static Match OfType(this Match match) where T : IStyleable { Contract.Requires(match != null);