public void StringFormatTwoWay()
        {
            var property = BindableProperty.Create("Foo", typeof(string), typeof(MockBindable));
            var binding  = CreateBinding(BindingMode.TwoWay, "Foo {0}");

            var vm = new MockViewModel {
                Text = "Bar"
            };
            var bo = new MockBindable {
                BindingContext = vm
            };

            bo.SetBinding(property, binding);

            bo.SetValue(property, "Baz");

            Assert.That(vm.Text, Is.EqualTo("Baz"));
            Assert.That(bo.GetValue(property), Is.EqualTo("Foo Baz"));
        }
Example #2
0
        public void ValueUpdatedWithSimplePathOnTwoWayBinding(
            [Values(true, false)] bool isDefault)
        {
            const string newvalue  = "New Value";
            var          viewmodel = new MockViewModel
            {
                Text = "Foo"
            };

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

            if (isDefault)
            {
                propertyDefault = BindingMode.TwoWay;
                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),
                            "Target property did not update change");
            Assert.AreEqual(newvalue, viewmodel.Text,
                            "Source property changed from what it was set to");

            const string newvalue2 = "New Value in the other direction";

            bindable.SetValue(property, newvalue2);
            Assert.AreEqual(newvalue2, viewmodel.Text,
                            "Source property did not update with Target's change");
            Assert.AreEqual(newvalue2, bindable.GetValue(property),
                            "Target property changed from what it was set to");
            Assert.That(log.Messages.Count, Is.EqualTo(0),
                        "An error was logged: " + log.Messages.FirstOrDefault());
        }
Example #3
0
        public void ValueUpdatedWithSimplePathOnOneWayToSourceBinding(
            [Values(true, false)] 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);

            string       original = (string)bindable.GetValue(property);
            const string value    = "value";

            viewmodel.Text = value;
            Assert.AreEqual(original, bindable.GetValue(property),
                            "Target updated from Source on OneWayToSource");

            bindable.SetValue(property, 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 StructPropertyDefaultValue()
        {
            // Create BindableProperty without explicit default value
            var prop = BindableProperty.Create("foo", typeof(TestStruct), typeof(MockBindable));

            Assert.AreEqual(typeof(TestStruct), prop.ReturnType);

            Assert.AreEqual(((TestStruct)prop.DefaultValue).IntValue, default(int));

            var bindable = new MockBindable();

            Assert.AreEqual(default(int), ((TestStruct)bindable.GetValue(prop)).IntValue);

            var propStruct = new TestStruct {
                IntValue = 1
            };

            bindable.SetValue(prop, propStruct);
            Assert.AreEqual(1, ((TestStruct)bindable.GetValue(prop)).IntValue);
        }
        public void BindablePropertyChanged()
        {
            bool changed = false;

            string oldv = "bar";
            string newv = "foo";

            var property = BindableProperty.Create <MockBindable, string> (w => w.Foo, oldv,
                                                                           propertyChanged: (b, ov, nv) => {
                Assert.AreSame(oldv, ov);
                Assert.AreSame(newv, nv);
                changed = true;
            });

            var mock = new MockBindable();

            mock.SetValue(property, newv);

            Assert.IsTrue(changed, "PropertyChanged was not called");
        }
Example #6
0
        public void ValueUpdatedWithOldContextDoesNotUpdateWithTwoWayBinding(bool isDefault)
        {
            const string newvalue  = "New Value";
            var          viewmodel = new MockViewModel
            {
                Text = "Foo"
            };

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

            if (isDefault)
            {
                propertyDefault = BindingMode.TwoWay;
                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(null, bindable.GetValue(property));

            viewmodel.Text = newvalue;
            Assert.AreEqual(null, bindable.GetValue(property),
                            "Target updated from old Source property change");

            string original = viewmodel.Text;

            bindable.SetValue(property, newvalue);
            Assert.AreEqual(original, viewmodel.Text,
                            "Source updated from old Target property change");
            Assert.That(log.Messages.Count, Is.EqualTo(0),
                        "An error was logged: " + log.Messages.FirstOrDefault());
        }
        public void ClearValueTriggersINPC()
        {
            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");
            changingfired = changedfired = changeddelegatefired = changingdelegatefired = false;

            bindable.ClearValue(property);
            Assert.True(changingfired);
            Assert.True(changedfired);
            Assert.True(changingdelegatefired);
            Assert.True(changeddelegatefired);
        }
        public void SetValueInvalid()
        {
            var mock = new MockBindable();

            Assert.Throws <ArgumentNullException> (() => mock.SetValue((BindableProperty)null, "null"));
        }