Browse Source

Match inside/outside template.

Only match outside template before Template(), only match inside
afterwards.
pull/4/head
Steven Kirk 12 years ago
parent
commit
e0f826edea
  1. 1
      Perspex.UnitTests/Perspex.UnitTests.csproj
  2. 16
      Perspex.UnitTests/Styling/SelectorTests_Class.cs
  3. 11
      Perspex.UnitTests/Styling/SelectorTests_Id.cs
  4. 11
      Perspex.UnitTests/Styling/SelectorTests_OfType.cs
  5. 66
      Perspex.UnitTests/Styling/SelectorTests_Template.cs
  6. 21
      Perspex/Styling/Match.cs
  7. 11
      Perspex/Styling/Selectors.cs

1
Perspex.UnitTests/Perspex.UnitTests.csproj

@ -75,6 +75,7 @@
<Compile Include="Styling\SelectorTests_Class.cs" />
<Compile Include="Styling\SelectorTests_Id.cs" />
<Compile Include="Styling\SelectorTests_OfType.cs" />
<Compile Include="Styling\SelectorTests_Template.cs" />
<Compile Include="Styling\TestControlBase.cs" />
</ItemGroup>
<ItemGroup>

16
Perspex.UnitTests/Styling/SelectorTests_Class.cs

@ -9,6 +9,8 @@ namespace Perspex.UnitTests.Styling
using System.Linq;
using System.Reactive.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Perspex.Controls;
using Perspex.Styling;
[TestClass]
@ -40,6 +42,20 @@ namespace Perspex.UnitTests.Styling
CollectionAssert.AreEqual(new[] { false }, target.GetActivator().Take(1).ToEnumerable().ToArray());
}
[TestMethod]
public void Class_Doesnt_Match_Control_With_TemplatedParent()
{
var control = new Control1
{
Classes = new Classes { "foo" },
TemplatedParent = new Mock<ITemplatedControl>().Object,
};
var target = control.Select().Class("foo");
CollectionAssert.AreEqual(new[] { false }, target.GetActivator().Take(1).ToEnumerable().ToArray());
}
[TestMethod]
public void Class_Tracks_Additions()
{

11
Perspex.UnitTests/Styling/SelectorTests_Id.cs

@ -11,6 +11,7 @@ namespace Perspex.UnitTests.Styling
using System.Linq;
using System.Reactive.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Perspex.Controls;
using Perspex.Styling;
@ -38,7 +39,7 @@ namespace Perspex.UnitTests.Styling
[TestMethod]
public void Id_Doesnt_Match_Control_With_TemplatedParent()
{
var control = new Control1 { Id = "foo", TemplatedParent = new TemplatedControl1() };
var control = new Control1 { TemplatedParent = new Mock<ITemplatedControl>().Object };
var target = control.Select().Id("foo");
CollectionAssert.AreEqual(new[] { false }, target.GetActivator().Take(1).ToEnumerable().ToArray());
@ -69,13 +70,5 @@ namespace Perspex.UnitTests.Styling
public class Control1 : TestControlBase
{
}
public class TemplatedControl1 : ITemplatedControl
{
public IEnumerable<IVisual> VisualChildren
{
get { throw new NotImplementedException(); }
}
}
}
}

11
Perspex.UnitTests/Styling/SelectorTests_OfType.cs

@ -9,6 +9,8 @@ namespace Perspex.UnitTests.Styling
using System.Linq;
using System.Reactive.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Perspex.Controls;
using Perspex.Styling;
[TestClass]
@ -32,6 +34,15 @@ namespace Perspex.UnitTests.Styling
CollectionAssert.AreEqual(new[] { false }, target.GetActivator().Take(1).ToEnumerable().ToArray());
}
[TestMethod]
public void OfType_Doesnt_Match_Control_With_TemplatedParent()
{
var control = new Control1 { TemplatedParent = new Mock<ITemplatedControl>().Object };
var target = control.Select().OfType<Control1>();
CollectionAssert.AreEqual(new[] { false }, target.GetActivator().Take(1).ToEnumerable().ToArray());
}
[TestMethod]
public void When_OfType_Matches_Control_Other_Selectors_Are_Subscribed()
{

66
Perspex.UnitTests/Styling/SelectorTests_Template.cs

@ -0,0 +1,66 @@
// -----------------------------------------------------------------------
// <copyright file="SelectorTests.cs" company="Steven Kirk">
// Copyright 2014 MIT Licence. See licence.md for more information.
// </copyright>
// -----------------------------------------------------------------------
namespace Perspex.UnitTests.Styling
{
using System.Linq;
using System.Reactive.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Perspex.Controls;
using Perspex.Styling;
using Match = Perspex.Styling.Match;
[TestClass]
public class SelectorTests_Template
{
[TestMethod]
public void Control_In_Template_Is_Not_Matched_Without_Template_Selector()
{
var templatedControl = new Mock<ITemplatedControl>();
var styleable = templatedControl.As<IStyleable>();
this.BuildVisualTree(templatedControl);
var border = (Border)templatedControl.Object.VisualChildren.Single();
var selector = border.Select().OfType<Border>();
Assert.AreEqual(false, ActivatorValue(selector));
}
[TestMethod]
public void Control_In_Template_Is_Matched_With_Template_Selector()
{
var templatedControl = new Mock<ITemplatedControl>();
var styleable = templatedControl.As<IStyleable>();
this.BuildVisualTree(templatedControl);
var border = (Border)templatedControl.Object.VisualChildren.Single();
var selector = border.Select().Template().OfType<Border>();
Assert.AreEqual(true, ActivatorValue(selector));
}
private static bool ActivatorValue(Match selector)
{
return selector.GetActivator().Take(1).ToEnumerable().Single();
}
private void BuildVisualTree(Mock<ITemplatedControl> templatedControl)
{
templatedControl.Setup(x => x.VisualChildren).Returns(new[]
{
new Border
{
TemplatedParent = templatedControl.Object,
Content = new TextBlock
{
TemplatedParent = templatedControl.Object,
},
},
});
}
}
}

21
Perspex/Styling/Match.cs

@ -15,6 +15,8 @@ namespace Perspex.Styling
public class Match
{
private IObservable<bool> observable;
public Match(IStyleable control)
{
this.Control = control;
@ -41,8 +43,23 @@ namespace Perspex.Styling
public IObservable<bool> Observable
{
get;
set;
get
{
return this.observable;
}
set
{
if ((!InTemplate && Control.TemplatedParent == null) ||
(InTemplate && Control.TemplatedParent != null))
{
this.observable = value;
}
else
{
this.observable = System.Reactive.Linq.Observable.Return(false);
}
}
}
public Match Previous

11
Perspex/Styling/Selectors.cs

@ -54,5 +54,16 @@ namespace Perspex.Styling
SelectorString = typeof(T).Name,
};
}
public static Match Template(this Match match)
{
Contract.Requires<ArgumentNullException>(match != null);
return new Match(match)
{
InTemplate = true,
SelectorString = " $ ",
};
}
}
}

Loading…
Cancel
Save