Exemple #1
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());
        }
Exemple #2
0
        public void BindingTwoWayToDynamicModel()
        {
            var view  = new MockBindable();
            var model = new DynamicModel
            {
                Properties =
                {
                    { "Title", "Foo" },
                }
            };

            view.SetBinding(MockBindable.TextProperty, "Title");
            view.BindingContext = model;

            Assert.AreEqual("Foo", view.Text);

            view.Text = "Bar";

            Assert.AreEqual("Bar", model.Properties["Title"]);
        }
Exemple #3
0
        public void StringFormatTwoWay()
        {
            var property = BindableProperty.Create <MockBindable, string> (w => w.Foo, null);

            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"));
        }
Exemple #4
0
        public void TestConverterWithStringFormat()
        {
            var property     = BindableProperty.Create("foo", typeof(string), typeof(MockBindable), null);
            var bindable     = new MockBindable();
            var multibinding = new MultiBinding
            {
                Bindings =
                {
                    new Binding("foo"),
                    new Binding("bar", stringFormat: "{0:000}"),
                    new Binding("baz")
                },
                Converter    = new StringConcatenationConverter(),
                StringFormat = "Hello {0}"
            };

            Assert.DoesNotThrow(() => bindable.SetBinding(property, multibinding));
            Assert.DoesNotThrow(() => bindable.BindingContext = new { foo = "FOO", bar = 42, baz = "BAZ" });
            Assert.That(bindable.GetValue(property), Is.EqualTo("Hello FOO 042 BAZ"));
        }
Exemple #5
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());
        }
Exemple #6
0
        public void PropertyChangeBindingsOccurThroughMainThread()
        {
            var vm = new MockViewModel {
                Text = "text"
            };

            var bindable = new MockBindable();
            var binding  = CreateBinding();

            bindable.BindingContext = vm;
            bindable.SetBinding(MockBindable.TextProperty, binding);

            bool invokeOnMainThreadWasCalled = false;

            Device.PlatformServices = new MockPlatformServices(a => invokeOnMainThreadWasCalled = true, isInvokeRequired: true);

            vm.Text = "updated";

            // If we wait five seconds and invokeOnMainThreadWasCalled still hasn't been set, something is very wrong
            Assert.That(invokeOnMainThreadWasCalled, Is.True.After(5000, 10));
        }
Exemple #7
0
        public void ReuseBindingInstance()
        {
            var vm = new MockViewModel();

            var bindable = new MockBindable();

            bindable.BindingContext = vm;

            var property = BindableProperty.Create("Foo", typeof(string), typeof(MockBindable));
            var binding  = new Binding("Text");

            bindable.SetBinding(property, binding);

            var bindable2 = new MockBindable();

            bindable2.BindingContext = new MockViewModel();
            Assert.Throws <InvalidOperationException>(() => bindable2.SetBinding(property, binding),
                                                      "Binding allowed reapplication with a different context");

            GC.KeepAlive(bindable);
        }
        public void PropertyChangeBindingsOccurThroughMainThread()
        {
            var vm = new MockViewModel {
                Text = "text"
            };

            var bindable = new MockBindable();
            var binding  = CreateBinding();

            bindable.BindingContext = vm;
            bindable.SetBinding(MockBindable.TextProperty, binding);

            bool mainThread = false;

            Device.PlatformServices = new MockPlatformServices(invokeOnMainThread: a => mainThread = true);

            vm.Text = "updated";

            Assert.IsTrue(mainThread, "Binding did not occur on main thread");
            Assert.AreNotEqual(vm.Text, bindable.GetValue(MockBindable.TextProperty), "Binding was applied anyway through other means");
        }
        public void StringFormatNonStringType()
        {
            var property = BindableProperty.Create("Foo", typeof(string), typeof(MockBindable));
            var binding  = new Binding("Value", stringFormat: "{0:P2}");

            var vm = new  { Value = 0.95d };
            var bo = new MockBindable {
                BindingContext = vm
            };

            bo.SetBinding(property, binding);

            if (System.Threading.Thread.CurrentThread.CurrentCulture.Name == "tr-TR")
            {
                Assert.That(bo.GetValue(property), Is.EqualTo("%95,00"));
            }
            else
            {
                Assert.That(bo.GetValue(property), Is.EqualTo("95.00 %"));
            }
        }
Exemple #10
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 RemovedBindingDoesNotUpdate()
        {
            const string newvalue  = "New Value";
            var          viewmodel = new MockViewModel {
                Text = "Foo"
            };

            var binding = new Binding("Text");

            var bindable = new MockBindable();

            bindable.BindingContext = viewmodel;
            bindable.SetBinding(MockBindable.TextProperty, binding);

            string original = (string)bindable.GetValue(MockBindable.TextProperty);

            bindable.RemoveBinding(MockBindable.TextProperty);

            viewmodel.Text = newvalue;
            Assert.AreEqual(original, bindable.GetValue(MockBindable.TextProperty),
                            "Property updated from a removed binding");
        }
        // https://bugzilla.xamarin.com/show_bug.cgi?id=24054
        public void BindingsAppliedUnappliedWithNullContext()
        {
            var bindable = new MockBindable();

            var binding = new Binding(".");

            bindable.SetBinding(MockBindable.TextProperty, binding);
            bindable.BindingContext = "foo";

            Assume.That(bindable.Text, Is.EqualTo(bindable.BindingContext));

            bindable.BindingContext = null;

            Assume.That(bindable.Text, Is.EqualTo(bindable.BindingContext));

            bindable.BindingContext = "bar";

            Assume.That(bindable.Text, Is.EqualTo(bindable.BindingContext));

            bindable.RemoveBinding(MockBindable.TextProperty);

            Assert.That(() => binding.Path = "Foo", Throws.Nothing);
        }