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 SetBindingInvalid()
        {
            var mock = new MockBindable();

            Assert.Throws <ArgumentNullException> (() => mock.SetBinding(null, new Binding(".")));
            Assert.Throws <ArgumentNullException> (() => mock.SetBinding(MockBindable.TextProperty, null));
        }
        void TestGetValueDefault(BindableProperty property)
        {
            var    mock  = new MockBindable();
            object value = mock.GetValue(property);

            Assert.AreEqual(property.DefaultValue, value);
        }
        public void RaiseOnEqual()
        {
            string foo  = "foo";
            var    mock = new MockBindable();

            mock.SetValue(MockBindable.TextProperty, foo);

            bool changing = false;

            mock.PropertyChanging += (o, e) => {
                Assert.That(e.PropertyName, Is.EqualTo(MockBindable.TextProperty.PropertyName));
                changing = true;
            };

            bool changed = true;

            mock.PropertyChanged += (o, e) => {
                Assert.That(e.PropertyName, Is.EqualTo(MockBindable.TextProperty.PropertyName));
                changed = true;
            };

            mock.SetValueCore(MockBindable.TextProperty, foo,
                              BindableObject.SetValueFlags.ClearOneWayBindings | BindableObject.SetValueFlags.ClearDynamicResource | BindableObject.SetValueFlags.RaiseOnEqual);

            Assert.That(changing, Is.True, "PropertyChanging event did not fire");
            Assert.That(changed, Is.True, "PropertyChanged event did not fire");
        }
        public void DefaultValueCreatorDoesNotTriggerINPC()
        {
            int invoked          = 0;
            int propertychanged  = 0;
            int changedfired     = 0;
            var bindableProperty = BindableProperty.Create("Foo", typeof(object), typeof(MockBindable), null,
                                                           propertyChanged: (bindable, oldvalue, newvalue) => {
                propertychanged++;
            },
                                                           defaultValueCreator: o => {
                invoked++;
                return(new object());
            });

            var bp = new MockBindable();

            bp.PropertyChanged += (sender, e) => {
                if (e.PropertyName == "Foo")
                {
                    changedfired++;
                }
            };
            var value0 = bp.GetValue(bindableProperty);

            Assert.NotNull(value0);
            Assert.AreEqual(1, invoked);
            Assert.AreEqual(0, propertychanged);
            Assert.AreEqual(0, changedfired);
        }
示例#6
0
        public void ValueUpdatedWithOldContextDoesNotUpdateWithOneWayToSourceBinding(bool isDefault)
        {
            const string newvalue  = "New Value";
            var          viewmodel = new MockViewModel
            {
                Text = "Foo"
            };

            BindingMode propertyDefault = BindingMode.OneWay;
            BindingMode bindingMode     = BindingMode.OneWayToSource;

            if (isDefault)
            {
                propertyDefault = BindingMode.OneWayToSource;
                bindingMode     = BindingMode.Default;
            }

            var property = BindableProperty.Create("Text", typeof(string), typeof(MockBindable), "default value", propertyDefault);
            var binding  = CreateBinding(bindingMode);

            var bindable = new MockBindable();

            bindable.BindingContext = viewmodel;
            bindable.SetBinding(property, binding);

            bindable.BindingContext = new MockViewModel();
            Assert.AreEqual(property.DefaultValue, bindable.GetValue(property));

            viewmodel.Text = newvalue;
            Assert.AreEqual(property.DefaultValue, bindable.GetValue(property),
                            "Target updated from old Source property change");
            Assert.That(log.Messages.Count, Is.EqualTo(0),
                        "An error was logged: " + log.Messages.FirstOrDefault());
        }
示例#7
0
        public void ValueUpdatedWithSimplePathOnOneWayBinding(
            [Values(true, false)] bool isDefault)
        {
            const string newvalue  = "New Value";
            var          viewmodel = new MockViewModel
            {
                Text = "Foo"
            };

            BindingMode propertyDefault = BindingMode.OneWay;
            BindingMode bindingMode     = BindingMode.OneWay;

            if (isDefault)
            {
                propertyDefault = BindingMode.OneWay;
                bindingMode     = BindingMode.Default;
            }

            var property = BindableProperty.Create("Text", typeof(string), typeof(MockBindable), "default value", propertyDefault);
            var binding  = CreateBinding(bindingMode);

            var bindable = new MockBindable();

            bindable.BindingContext = viewmodel;
            bindable.SetBinding(property, binding);

            viewmodel.Text = newvalue;
            Assert.AreEqual(newvalue, bindable.GetValue(property),
                            "Bindable did not update on binding context property change");
            Assert.AreEqual(newvalue, viewmodel.Text,
                            "Source property changed when it shouldn't");
            Assert.That(log.Messages.Count, Is.EqualTo(0),
                        "An error was logged: " + log.Messages.FirstOrDefault());
        }
        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);
        }
示例#9
0
        public void BindingStaysOnUpdateValueFromBinding()
        {
            const string newvalue  = "New Value";
            var          viewmodel = new MockViewModel
            {
                Text = "Foo"
            };

            var property = BindableProperty.Create("Text", typeof(string), typeof(MockBindable), null);
            var binding  = CreateBinding(BindingMode.Default);

            var bindable = new MockBindable();

            bindable.BindingContext = viewmodel;
            bindable.SetBinding(property, binding);

            viewmodel.Text = newvalue;
            Assert.AreEqual(newvalue, bindable.GetValue(property));

            const string newValue2 = "new value 2";

            viewmodel.Text = newValue2;
            Assert.AreEqual(newValue2, bindable.GetValue(property));

            Assert.That(log.Messages.Count, Is.EqualTo(0),
                        "An error was logged: " + log.Messages.FirstOrDefault());
        }
        public void GetSetValue()
        {
            const string value = "foo";
            var          mock  = new MockBindable();

            mock.SetValue(MockBindable.TextProperty, value);

            Assert.AreEqual(value, mock.GetValue(MockBindable.TextProperty));
        }
        public void ValidateValue()
        {
            var property = BindableProperty.Create <MockBindable, string> (w => w.Foo, null,
                                                                           validateValue: (b, v) => false);

            var mock = new MockBindable();

            Assert.Throws <ArgumentException> (() => mock.SetValue(property, null));
        }
        //https://bugzilla.xamarin.com/show_bug.cgi?id=27299
        public void BindingOnBindingContextDoesntReapplyBindingContextBinding()
        {
            var bindable = new MockBindable();
            var locator  = new VMLocator();

            Assert.AreEqual(0, locator.Count);
            locator.Invoked += (sender, e) => Assert.IsTrue(locator.Count <= 1);
            bindable.SetBinding(BindableObject.BindingContextProperty, new Binding("VM", source: locator));
            Assert.IsTrue(locator.Count == 1);
        }
        //https://bugzilla.xamarin.com/show_bug.cgi?id=24485
        public void BindingContextBoundThroughConverter()
        {
            var bindable = new MockBindable();

            bindable.BindingContext = "test";
            bindable.SetBinding(BindableObject.BindingContextProperty, new Binding(".", converter: new BindingContextConverter()));
            bindable.SetBinding(MockBindable.TextProperty, "Text");

            Assert.That(() => bindable.Text, Is.EqualTo("testConverted"));
        }
        public void SetValueCoreInvokesOpImplicitOnValue()
        {
            var prop     = BindableProperty.Create("Foo", typeof(string), typeof(MockBindable), null);
            var bindable = new MockBindable();

            Assert.Null(bindable.GetValue(prop));
            bindable.SetValue(prop, new CastToString("foo"));

            Assert.AreEqual("foo", bindable.GetValue(prop));
        }
        public void SetValueCoreInvokesOpImplicitOnPropertyType()
        {
            var prop     = BindableProperty.Create("Foo", typeof(CastFromString), typeof(MockBindable), null);
            var bindable = new MockBindable();

            Assert.Null(bindable.GetValue(prop));
            bindable.SetValue(prop, "foo");

            Assert.AreEqual("foo", ((CastFromString)bindable.GetValue(prop)).Result);
        }
        public void BindingContextChangedEvent()
        {
            var mock = new MockBindable();

            mock.BindingContextChanged += (sender, args) => Assert.Pass();

            mock.BindingContext = new object();

            Assert.Fail("The BindingContextChanged event was not fired.");
        }
        public void CoerceValue()
        {
            var property = BindableProperty.Create <MockBindable, string> (w => w.Foo, null,
                                                                           coerceValue: (bo, o) => o.ToUpper());

            const string value = "value";
            var          mock  = new MockBindable();

            mock.SetValue(property, value);
            Assert.AreEqual(value.ToUpper(), mock.GetValue(property));
        }
        public void PropertyChangingDefaultValue()
        {
            var prop = BindableProperty.Create <MockBindable, string> (w => w.Foo, "DefaultValue");

            var mock = new MockBindable();

            mock.PropertyChanging += (s, e) => Assert.Fail();
            mock.SetValue(prop, prop.DefaultValue);

            Assert.Pass();
        }
        public void BindingContext()
        {
            var mock = new MockBindable();

            Assert.IsNull(mock.BindingContext);

            object obj = new object();

            mock.BindingContext = obj;
            Assert.AreSame(obj, mock.BindingContext);
        }
示例#20
0
        public void CreateContentValues()
        {
            var template = new DataTemplate(typeof(MockBindable))
            {
                Values = { { MockBindable.TextProperty, "value" } }
            };

            MockBindable bindable = (MockBindable)template.CreateContent();

            Assert.That(bindable.GetValue(MockBindable.TextProperty), Is.EqualTo("value"));
        }
        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 SetValueCoreImplicitelyCastBasicType()
        {
            var prop     = BindableProperty.Create("Foo", typeof(int), typeof(MockBindable), 0);
            var bindable = new MockBindable();

            Assert.DoesNotThrow(() => bindable.SetValue(prop, (object)(short)42));
            Assert.AreEqual(42, bindable.GetValue(prop));

            bindable.SetValue(prop, (object)(long)-42);
            Assert.AreNotEqual(-42, bindable.GetValue(prop));
        }
示例#23
0
        public void CreateContentBindings()
        {
            var template = new DataTemplate(() => new MockBindable())
            {
                Bindings = { { MockBindable.TextProperty, new Binding(".") } }
            };

            MockBindable bindable = (MockBindable)template.CreateContent();

            bindable.BindingContext = "text";
            Assert.That(bindable.GetValue(MockBindable.TextProperty), Is.EqualTo("text"));
        }
示例#24
0
        public void OneWayToSourceContextSetToNull()
        {
            var binding = new Binding("Text", BindingMode.OneWayToSource);

            MockBindable bindable = new MockBindable {
                BindingContext = new MockViewModel()
            };

            bindable.SetBinding(MockBindable.TextProperty, binding);

            Assert.That(() => bindable.BindingContext = null, Throws.Nothing);
        }
示例#25
0
        public void SetValueOverridesBinding()
        {
            var template = new DataTemplate(typeof(MockBindable));

            template.SetBinding(MockBindable.TextProperty, new Binding("."));
            template.SetValue(MockBindable.TextProperty, "value");

            MockBindable bindable = (MockBindable)template.CreateContent();

            Assert.That(bindable.GetValue(MockBindable.TextProperty), Is.EqualTo("value"));
            bindable.BindingContext = "binding";
            Assert.That(bindable.GetValue(MockBindable.TextProperty), Is.EqualTo("value"));
        }
        public void PropertyChangedSameValue()
        {
            const string value = "foo";

            var mock = new MockBindable();

            mock.SetValue(MockBindable.TextProperty, value);
            mock.PropertyChanged += (s, e) => Assert.Fail();

            mock.SetValue(MockBindable.TextProperty, value);

            Assert.Pass();
        }
        public void TestBindingTwoWayOnReadOnly()
        {
            var bindablePropertyKey = BindableProperty.CreateReadOnly <MockBindable, string> (w => w.Foo, "DefaultValue", BindingMode.OneWayToSource);
            var bindableProperty    = bindablePropertyKey.BindableProperty;

            var bindable = new MockBindable();
            var vm       = new MockViewModel();

            bindable.SetBinding(bindableProperty, new Binding("Text", BindingMode.TwoWay));
            Assert.DoesNotThrow(() => bindable.BindingContext = vm);

            Assert.AreEqual("DefaultValue", bindable.GetValue(bindableProperty));
        }
        public void PropertyChanging()
        {
            var mock = new MockBindable();

            mock.PropertyChanging += (sender, args) => {
                Assert.AreEqual(MockBindable.TextProperty.PropertyName, args.PropertyName);
                Assert.AreEqual(MockBindable.TextProperty.DefaultValue, mock.GetValue(MockBindable.TextProperty));
                Assert.Pass();
            };

            mock.SetValue(MockBindable.TextProperty, "foo");

            Assert.Fail("The PropertyChanging event was not fired.");
        }
示例#29
0
        public void TestBehaviorsAttachedDP()
        {
            var behavior   = new MockBehavior <MockBindable> ();
            var bindable   = new MockBindable();
            var collection = bindable.Behaviors;

            Assert.Null(behavior.AssociatedObject);

            collection.Add(behavior);
            Assert.AreSame(bindable, behavior.AssociatedObject);

            collection.Remove(behavior);
            Assert.Null(behavior.AssociatedObject);
        }
        public void RecursiveChange()
        {
            bool changedA1 = false, changedA2 = false, changedB1 = false, changedB2 = false;

            var mock = new MockBindable();

            mock.PropertyChanged += (sender, args) => {
                if (!changedA1)
                {
                    Assert.AreEqual("1", mock.GetValue(MockBindable.TextProperty));
                    Assert.IsFalse(changedA2);
                    Assert.IsFalse(changedB1);
                    Assert.IsFalse(changedB2);
                    mock.SetValue(MockBindable.TextProperty, "2");
                    changedA1 = true;
                }
                else
                {
                    Assert.AreEqual("2", mock.GetValue(MockBindable.TextProperty));
                    Assert.IsFalse(changedA2);
                    Assert.IsTrue(changedB1);
                    Assert.IsFalse(changedB2);
                    changedA2 = true;
                }
            };
            mock.PropertyChanged += (sender, args) => {
                if (!changedB1)
                {
                    Assert.AreEqual("1", mock.GetValue(MockBindable.TextProperty));
                    Assert.IsTrue(changedA1);
                    Assert.IsFalse(changedA2);
                    Assert.IsFalse(changedB2);
                    changedB1 = true;
                }
                else
                {
                    Assert.AreEqual("2", mock.GetValue(MockBindable.TextProperty));
                    Assert.IsTrue(changedA1);
                    Assert.IsTrue(changedA2);
                    Assert.IsFalse(changedB2);
                    changedB2 = true;
                }
            };
            mock.SetValue(MockBindable.TextProperty, "1");
            Assert.AreEqual("2", mock.GetValue(MockBindable.TextProperty));
            Assert.IsTrue(changedA1);
            Assert.IsTrue(changedA2);
            Assert.IsTrue(changedB1);
            Assert.IsTrue(changedB2);
        }