// Copyright (c) The Perspex Project. All rights reserved. // Licensed under the MIT license. See licence.md file in the project root for full license information. using System; using Xunit; namespace Perspex.Base.UnitTests { public class PerspexPropertyTests { [Fact] public void Constructor_Sets_Properties() { PerspexProperty target = new PerspexProperty( "test", typeof(Class1), "Foo", false, BindingMode.OneWay, null); Assert.Equal("test", target.Name); Assert.Equal(typeof(string), target.PropertyType); Assert.Equal(typeof(Class1), target.OwnerType); Assert.Equal(false, target.Inherits); } [Fact] public void Name_Cannot_Contain_Periods() { Assert.Throws(() => new PerspexProperty( "Foo.Bar", typeof(Class1), "Foo", false, BindingMode.OneWay, null)); } [Fact] public void GetDefaultValue_Returns_Registered_Value() { PerspexProperty target = new PerspexProperty( "test", typeof(Class1), "Foo", false, BindingMode.OneWay, null); Assert.Equal("Foo", target.GetDefaultValue()); } [Fact] public void GetDefaultValue_Returns_Registered_Value_For_Not_Overridden_Class() { PerspexProperty target = new PerspexProperty( "test", typeof(Class1), "Foo", false, BindingMode.OneWay, null); Assert.Equal("Foo", target.GetDefaultValue()); } [Fact] public void GetDefaultValue_Returns_Registered_Value_For_Unrelated_Class() { PerspexProperty target = new PerspexProperty( "test", typeof(Class3), "Foo", false, BindingMode.OneWay, null); Assert.Equal("Foo", target.GetDefaultValue()); } [Fact] public void GetDefaultValue_Returns_Overridden_Value() { PerspexProperty target = new PerspexProperty( "test", typeof(Class1), "Foo", false, BindingMode.OneWay, null); target.OverrideDefaultValue(typeof(Class2), "Bar"); Assert.Equal("Bar", target.GetDefaultValue()); } [Fact] public void Initialized_Observable_Fired() { bool invoked = false; Class1.FooProperty.Initialized.Subscribe(x => { Assert.Equal(PerspexProperty.UnsetValue, x.OldValue); Assert.Equal("default", x.NewValue); Assert.Equal(BindingPriority.Unset, x.Priority); invoked = true; }); var target = new Class1(); Assert.True(invoked); } [Fact] public void Changed_Observable_Fired() { var target = new Class1(); string value = null; Class1.FooProperty.Changed.Subscribe(x => value = (string)x.NewValue); target.SetValue(Class1.FooProperty, "newvalue"); Assert.Equal("newvalue", value); } private class Class1 : PerspexObject { public static readonly PerspexProperty FooProperty = PerspexProperty.Register("Foo", "default"); } private class Class2 : Class1 { } private class Class3 { } } }