diff --git a/Perspex.UnitTests/StyleTests.cs b/Perspex.UnitTests/StyleTests.cs index f842c2563a..b612b7abb4 100644 --- a/Perspex.UnitTests/StyleTests.cs +++ b/Perspex.UnitTests/StyleTests.cs @@ -18,80 +18,111 @@ namespace Perspex.UnitTests [TestMethod] public void Style_With_Only_Type_Selector_Should_Update_Value() { - Style style = new Style(x => x.Select().OfType()) + Style style = new Style(x => x.Select().OfType()) { Setters = new[] { - new Setter(TextBlock.TextProperty, "Foo"), + new Setter(Class1.FooProperty, "Foo"), }, }; - TextBlock textBlock = new TextBlock - { - Text = "Original", - }; + var target = new Class1(); - style.Attach(textBlock); - Assert.AreEqual("Foo", textBlock.Text); + style.Attach(target); + + Assert.AreEqual("Foo", target.Foo); } [TestMethod] public void Style_With_Class_Selector_Should_Update_And_Restore_Value() { - Style style = new Style(x => x.Select().OfType().Class("foo")) + Style style = new Style(x => x.Select().OfType().Class("foo")) { Setters = new[] { - new Setter(TextBlock.TextProperty, "Foo"), + new Setter(Class1.FooProperty, "Foo"), }, }; - TextBlock textBlock = new TextBlock - { - Text = "Original", - }; + var target = new Class1(); - style.Attach(textBlock); - Assert.AreEqual("Original", textBlock.Text); - textBlock.Classes.Add("foo"); - Assert.AreEqual("Foo", textBlock.Text); - textBlock.Classes.Remove("foo"); - Assert.AreEqual("Original", textBlock.Text); + style.Attach(target); + Assert.AreEqual("foodefault", target.Foo); + target.Classes.Add("foo"); + Assert.AreEqual("Foo", target.Foo); + target.Classes.Remove("foo"); + Assert.AreEqual("foodefault", target.Foo); } [TestMethod] - public void Later_Styles_Should_Override_Earlier() + public void LocalValue_Should_Override_Style() { - Style style1 = new Style(x => x.Select().OfType().Class("foo")) + Style style = new Style(x => x.Select().OfType()) { Setters = new[] { - new Setter(TextBlock.TextProperty, "Foo"), + new Setter(Class1.FooProperty, "Foo"), }, }; - Style style2 = new Style(x => x.Select().OfType().Class("foo")) + var target = new Class1 { - Setters = new[] - { - new Setter(TextBlock.TextProperty, "Bar"), - }, + Foo = "Original", }; - TextBlock textBlock = new TextBlock + style.Attach(target); + Assert.AreEqual("Original", target.Foo); + } + + [TestMethod] + public void Later_Styles_Should_Override_Earlier() + { + Styles styles = new Styles { - Text = "Original", + new Style(x => x.Select().OfType().Class("foo")) + { + Setters = new[] + { + new Setter(Class1.FooProperty, "Foo"), + }, + }, + + new Style(x => x.Select().OfType().Class("foo")) + { + Setters = new[] + { + new Setter(Class1.FooProperty, "Bar"), + }, + } }; + var target = new Class1(); + List values = new List(); - textBlock.GetObservable(TextBlock.TextProperty).Subscribe(x => values.Add(x)); + target.GetObservable(Class1.FooProperty).Subscribe(x => values.Add(x)); - style1.Attach(textBlock); - style2.Attach(textBlock); - textBlock.Classes.Add("foo"); - textBlock.Classes.Remove("foo"); + styles.Attach(target); + target.Classes.Add("foo"); + target.Classes.Remove("foo"); - CollectionAssert.AreEqual(new[] { "Original", "Bar", "Original" }, values); + CollectionAssert.AreEqual(new[] { "foodefault", "Bar", "foodefault" }, values); + } + + private class Class1 : Control + { + public static readonly PerspexProperty FooProperty = + PerspexProperty.Register("Foo", "foodefault"); + + public string Foo + { + get { return this.GetValue(FooProperty); } + set { this.SetValue(FooProperty, value); } + } + + protected override Size MeasureContent(Size availableSize) + { + throw new NotImplementedException(); + } } } }