public void ClearValueDoesNotTriggersINPCOnSameValues()
        {
            var  bindable              = new MockBindable();
            bool changingfired         = false;
            bool changedfired          = false;
            bool changingdelegatefired = false;
            bool changeddelegatefired  = false;
            var  property              = BindableProperty.Create("Foo", typeof(string), typeof(MockBindable), "foo",
                                                                 propertyChanged: (b, o, n) => changeddelegatefired   = true,
                                                                 propertyChanging: (b, o, n) => changingdelegatefired = true
                                                                 );

            bindable.PropertyChanged  += (sender, e) => { changedfired |= e.PropertyName == "Foo"; };
            bindable.PropertyChanging += (sender, e) => { changingfired |= e.PropertyName == "Foo"; };

            bindable.SetValue(property, "foobar");
            bindable.SetValue(property, "foo");
            changingfired = changedfired = changeddelegatefired = changingdelegatefired = false;

            bindable.ClearValue(property);
            Assert.False(changingfired);
            Assert.False(changedfired);
            Assert.False(changingdelegatefired);
            Assert.False(changeddelegatefired);
        }
        public void DefaultValueCreatorCalledForChangeDelegates()
        {
            int changedOld = -1;
            int changedNew = -1;

            int changingOld = -1;
            int changingNew = -1;
            var prop        = BindableProperty.Create("Foo", typeof(int), typeof(MockBindable), 0, defaultValueCreator: b => 10,
                                                      propertyChanged: (b, value, newValue) => {
                changedOld = (int)value;
                changedNew = (int)newValue;
            },
                                                      propertyChanging: (b, value, newValue) => {
                changingOld = (int)value;
                changingNew = (int)newValue;
            });

            var bindable = new MockBindable();


            var defaultValue = (int)bindable.GetValue(prop);

            Assert.AreEqual(10, defaultValue);

            bindable.SetValue(prop, 5);

            bindable.ClearValue(prop);

            Assert.AreEqual(5, changedOld);
            Assert.AreEqual(5, changingOld);
            Assert.AreEqual(10, changedNew);
            Assert.AreEqual(10, changingNew);
        }
        public void TestReadOnly()
        {
            var bindablePropertyKey = BindableProperty.CreateReadOnly <MockBindable, string> (w => w.Foo, "DefaultValue");
            var bindableProperty    = bindablePropertyKey.BindableProperty;

            var bindable = new MockBindable();

            Assert.AreEqual("DefaultValue", bindable.GetValue(bindableProperty));

            bindable.SetValue(bindablePropertyKey, "Bar");
            Assert.AreEqual("Bar", bindable.GetValue(bindableProperty));

            Assert.Throws <InvalidOperationException> (() => bindable.SetValue(bindableProperty, "Baz"));
            Assert.AreEqual("Bar", bindable.GetValue(bindableProperty));

            Assert.Throws <InvalidOperationException> (() => bindable.ClearValue(bindableProperty));

            bindable.ClearValue(bindablePropertyKey);
            Assert.AreEqual("DefaultValue", bindable.GetValue(bindableProperty));
        }
        public void ClearValue()
        {
            const string value = "foo";
            var          mock  = new MockBindable();

            mock.SetValue(MockBindable.TextProperty, value);
            Assert.AreEqual(value, mock.GetValue(MockBindable.TextProperty));

            mock.ClearValue(MockBindable.TextProperty);
            TestGetValueDefault(MockBindable.TextProperty);
        }
        public void DefaultValueCreatorInvokedAfterClearValue()
        {
            int invoked          = 0;
            var bindableProperty = BindableProperty.Create("Foo", typeof(object), typeof(MockBindable), null, defaultValueCreator: o => {
                invoked++;
                return(new object());
            });
            var bindable = new MockBindable();

            Assert.AreEqual(0, invoked);

            var value0 = bindable.GetValue(bindableProperty);

            Assert.NotNull(value0);
            Assert.AreEqual(1, invoked);
            bindable.ClearValue(bindableProperty);

            var value1 = bindable.GetValue(bindableProperty);

            Assert.NotNull(value1);
            Assert.AreEqual(2, invoked);
            Assert.AreNotSame(value0, value1);
        }
        public void ClearValueInvalid()
        {
            var mock = new MockBindable();

            Assert.Throws <ArgumentNullException> (() => mock.ClearValue((BindableProperty)null));
        }