123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- // -----------------------------------------------------------------------
- // <copyright file="PerspexObjectTests_SetValue.cs" company="Steven Kirk">
- // Copyright 2015 MIT Licence. See licence.md for more information.
- // </copyright>
- // -----------------------------------------------------------------------
- namespace Perspex.Base.UnitTests
- {
- using System;
- using System.Linq;
- using System.Reactive.Linq;
- using System.Reactive.Subjects;
- using Xunit;
- public class PerspexObjectTests_SetValue
- {
- [Fact]
- public void ClearValue_Clears_Value()
- {
- Class1 target = new Class1();
- target.SetValue(Class1.FooProperty, "newvalue");
- target.ClearValue(Class1.FooProperty);
- Assert.Equal("foodefault", target.GetValue(Class1.FooProperty));
- }
- [Fact]
- public void SetValue_Sets_Value()
- {
- Class1 target = new Class1();
- target.SetValue(Class1.FooProperty, "newvalue");
- Assert.Equal("newvalue", target.GetValue(Class1.FooProperty));
- }
- [Fact]
- public void SetValue_Raises_PropertyChanged()
- {
- Class1 target = new Class1();
- bool raised = false;
- target.PropertyChanged += (s, e) =>
- {
- raised = s == target &&
- e.Property == Class1.FooProperty &&
- (string)e.OldValue == "foodefault" &&
- (string)e.NewValue == "newvalue";
- };
- target.SetValue(Class1.FooProperty, "newvalue");
- Assert.True(raised);
- }
- [Fact]
- public void SetValue_Doesnt_Raise_PropertyChanged_If_Value_Not_Changed()
- {
- Class1 target = new Class1();
- bool raised = false;
- target.SetValue(Class1.FooProperty, "bar");
- target.PropertyChanged += (s, e) =>
- {
- raised = true;
- };
- target.SetValue(Class1.FooProperty, "bar");
- Assert.False(raised);
- }
- [Fact]
- public void SetValue_Doesnt_Raise_PropertyChanged_If_Value_Not_Changed_From_Default()
- {
- Class1 target = new Class1();
- bool raised = false;
- target.PropertyChanged += (s, e) =>
- {
- raised = true;
- };
- target.SetValue(Class1.FooProperty, "foodefault");
- Assert.False(raised);
- }
- [Fact]
- public void SetValue_Throws_Exception_For_Unregistered_Property()
- {
- Class1 target = new Class1();
- Assert.Throws<InvalidOperationException>(() =>
- {
- target.SetValue(Class2.BarProperty, "invalid");
- });
- }
- [Fact]
- public void SetValue_Throws_Exception_For_Invalid_Value_Type()
- {
- Class1 target = new Class1();
- Assert.Throws<InvalidOperationException>(() =>
- {
- target.SetValue(Class1.FooProperty, 123);
- });
- }
- [Fact]
- public void SetValue_Of_Integer_On_Double_Property_Works()
- {
- Class2 target = new Class2();
- target.SetValue((PerspexProperty)Class2.FlobProperty, 4);
- var value = target.GetValue(Class2.FlobProperty);
- Assert.IsType<double>(value);
- Assert.Equal(4, value);
- }
- [Fact]
- public void SetValue_Respects_Implicit_Conversions()
- {
- Class2 target = new Class2();
- target.SetValue((PerspexProperty)Class2.FlobProperty, new ImplictDouble(4));
- var value = target.GetValue(Class2.FlobProperty);
- Assert.IsType<double>(value);
- Assert.Equal(4, value);
- }
- [Fact]
- public void SetValue_Can_Convert_To_Nullable()
- {
- Class2 target = new Class2();
- target.SetValue((PerspexProperty)Class2.FredProperty, 4.0);
- var value = target.GetValue(Class2.FredProperty);
- Assert.IsType<double>(value);
- Assert.Equal(4, value);
- }
- [Fact]
- public void SetValue_Respects_Priority()
- {
- Class1 target = new Class1();
- target.SetValue(Class1.FooProperty, "one", BindingPriority.TemplatedParent);
- Assert.Equal("one", target.GetValue(Class1.FooProperty));
- target.SetValue(Class1.FooProperty, "two", BindingPriority.Style);
- Assert.Equal("one", target.GetValue(Class1.FooProperty));
- target.SetValue(Class1.FooProperty, "three", BindingPriority.StyleTrigger);
- Assert.Equal("three", target.GetValue(Class1.FooProperty));
- }
- [Fact]
- public void Setting_UnsetValue_Reverts_To_Default_Value()
- {
- Class1 target = new Class1();
- target.SetValue(Class1.FooProperty, "newvalue");
- target.SetValue(Class1.FooProperty, PerspexProperty.UnsetValue);
- Assert.Equal("foodefault", target.GetValue(Class1.FooProperty));
- }
- private class Class1 : PerspexObject
- {
- public static readonly PerspexProperty<string> FooProperty =
- PerspexProperty.Register<Class1, string>("Foo", "foodefault");
- }
- private class Class2 : Class1
- {
- public static readonly PerspexProperty<string> BarProperty =
- PerspexProperty.Register<Class2, string>("Bar", "bardefault");
- public static readonly PerspexProperty<double> FlobProperty =
- PerspexProperty.Register<Class2, double>("Flob");
- public static readonly PerspexProperty<double?> FredProperty =
- PerspexProperty.Register<Class2, double?>("Fred");
- public Class1 Parent
- {
- get { return (Class1)this.InheritanceParent; }
- set { this.InheritanceParent = value; }
- }
- }
- private class ImplictDouble
- {
- public ImplictDouble(double value)
- {
- this.Value = value;
- }
- public double Value { get; }
- public static implicit operator double(ImplictDouble v)
- {
- return v.Value;
- }
- }
- }
- }
|