Exemplo n.º 1
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());
        }
Exemplo n.º 2
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());
        }
Exemplo n.º 3
0
        public Task PropertyChangeBindingsOccurThroughMainThread() => DispatcherTest.Run(async() =>
        {
            var isOnBackgroundThread        = false;
            var invokeOnMainThreadWasCalled = false;

            DispatcherProviderStubOptions.InvokeOnMainThread = action =>
            {
                invokeOnMainThreadWasCalled = true;
                action();
            };
            DispatcherProviderStubOptions.IsInvokeRequired =
                () => isOnBackgroundThread;

            var vm = new MockViewModel {
                Text = "text"
            };

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

            Assert.False(invokeOnMainThreadWasCalled);

            isOnBackgroundThread = true;

            vm.Text = "updated";

            Assert.True(invokeOnMainThreadWasCalled);
        });
Exemplo n.º 4
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());
        }
Exemplo n.º 5
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"));
        }
Exemplo n.º 6
0
		public void StringFormat()
		{
			var property = BindableProperty.Create<MockBindable, string> (w => w.Foo, null);

			var binding = CreateBinding (BindingMode.Default, "Foo {0}");

			var vm = new MockViewModel { Text = "Bar" };
			var bo = new MockBindable { BindingContext = vm };
			bo.SetBinding (property, binding);

			Assert.That (bo.GetValue (property), Is.EqualTo ("Foo Bar"));
		}
Exemplo n.º 7
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"));
        }
Exemplo n.º 8
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);
        }
Exemplo n.º 9
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 ShouldRaisePropertyChangedViaSetter()
            {
                MockBindable bindable = new MockBindable();
                int invokationsCount = 0;
                bindable.PropertyChanged += (s, e) =>
                {
                    Assert.AreEqual("MockProperty", e.PropertyName);
                    invokationsCount++;
                };

                bindable.MockProperty = "";
                Assert.AreEqual(1, invokationsCount);
            }
Exemplo n.º 11
0
		public void StringFormatOneWayToSource()
		{
			var property = BindableProperty.Create<MockBindable, string> (w => w.Foo, null);

			var binding = CreateBinding (BindingMode.OneWayToSource, "Foo {0}");

			var vm = new MockViewModel { Text = "Bar" };
			var bo = new MockBindable { BindingContext = vm };
			bo.SetBinding (property, binding);

			bo.SetValue (property, "Bar");

			Assert.That (vm.Text, Is.EqualTo ("Bar"));
		}
Exemplo n.º 12
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);
        }
Exemplo n.º 13
0
        public void BehaviorsAddedToAttachedCollectionAreAttached()
        {
            var behavior   = new MockBehavior <MockBindable>();
            var collection = new AttachedCollection <Behavior>();
            var bindable   = new MockBindable();

            ((IAttachedObject)collection).AttachTo(bindable);
            Assert.Null(behavior.AssociatedObject);

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

            collection.Remove(behavior);
            Assert.Null(behavior.AssociatedObject);
        }
Exemplo n.º 14
0
        public void StringFormat()
        {
            var property = BindableProperty.Create("Foo", typeof(string), typeof(MockBindable));
            var binding  = CreateBinding(BindingMode.Default, "Foo {0}");

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

            bo.SetBinding(property, binding);

            Assert.That(bo.GetValue(property), Is.EqualTo("Foo Bar"));
        }
Exemplo n.º 15
0
        public void EnumPropertyDefaultValue()
        {
            // Create BindableProperty without explicit default value
            var prop = BindableProperty.Create("foo", typeof(TestEnum), typeof(MockBindable));

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

            Assert.AreEqual(prop.DefaultValue, default(TestEnum));

            var bindable = new MockBindable();

            Assert.AreEqual(default(TestEnum), bindable.GetValue(prop));

            bindable.SetValue(prop, TestEnum.Two);
            Assert.AreEqual(TestEnum.Two, bindable.GetValue(prop));
        }
Exemplo n.º 16
0
        public void StringFormatNonStringType(string culture)
        {
            System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(culture);

            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);

            Assert.That(bo.GetValue(property), Is.EqualTo(string.Format(new System.Globalization.CultureInfo(culture), "{0:P2}", .95d)));             //%95,00 or 95.00%
        }
Exemplo n.º 17
0
        public void ValueTypePropertyDefaultValue()
        {
            // Create BindableProperty without explicit default value
            var prop = BindableProperty.Create("foo", typeof(int), typeof(MockBindable));

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

            Assert.AreEqual(prop.DefaultValue, 0);

            var bindable = new MockBindable();

            Assert.AreEqual(0, bindable.GetValue(prop));

            bindable.SetValue(prop, 1);
            Assert.AreEqual(1, bindable.GetValue(prop));
        }
Exemplo n.º 18
0
        public void ChangeAfterApply()
        {
            var property = BindableProperty.Create("Foo", typeof(string), typeof(MockBindable));
            var binding  = CreateBinding(BindingMode.OneWay);

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

            bo.SetBinding(property, binding);

            Assert.That(() => binding.Mode         = BindingMode.OneWayToSource, Throws.InvalidOperationException);
            Assert.That(() => binding.StringFormat = "{0}", Throws.InvalidOperationException);
        }
Exemplo n.º 19
0
        public void SourceAndTargetAreWeakWeakSimplePath(BindingMode mode)
        {
            var property = BindableProperty.Create("Text", typeof(string), typeof(MockBindable), "default value", BindingMode.OneWay);
            var binding  = CreateBinding(mode);

            WeakReference weakViewModel = null, weakBindable = null;

            int    i = 0;
            Action create = null;

            create = () =>
            {
                if (i++ < 1024)
                {
                    create();
                    return;
                }

                MockBindable bindable = new MockBindable();
                weakBindable = new WeakReference(bindable);

                MockViewModel viewmodel = new MockViewModel();
                weakViewModel = new WeakReference(viewmodel);

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

                Assume.That(() => bindable.BindingContext = null, Throws.Nothing);
            };

            create();

            GC.Collect();
            GC.WaitForPendingFinalizers();

            if (mode == BindingMode.TwoWay || mode == BindingMode.OneWay)
            {
                Assert.IsFalse(weakViewModel.IsAlive, "ViewModel wasn't collected");
            }

            if (mode == BindingMode.TwoWay || mode == BindingMode.OneWayToSource)
            {
                Assert.IsFalse(weakBindable.IsAlive, "Bindable wasn't collected");
            }
        }
Exemplo n.º 20
0
        public void NullableProperty()
        {
            var prop = BindableProperty.Create("foo", typeof(DateTime?), typeof(MockBindable), null);

            Assert.AreEqual(typeof(DateTime?), prop.ReturnType);

            var bindable = new MockBindable();

            Assert.AreEqual(null, bindable.GetValue(prop));

            var now = DateTime.Now;

            bindable.SetValue(prop, now);
            Assert.AreEqual(now, bindable.GetValue(prop));

            bindable.SetValue(prop, null);
            Assert.AreEqual(null, bindable.GetValue(prop));
        }
Exemplo n.º 21
0
        public void StringFormatOneWayToSource()
        {
            var property = BindableProperty.Create("Foo", typeof(string), typeof(MockBindable));
            var binding  = CreateBinding(BindingMode.OneWayToSource, "Foo {0}");

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

            bo.SetBinding(property, binding);

            bo.SetValue(property, "Bar");

            Assert.That(vm.Text, Is.EqualTo("Bar"));
        }
Exemplo n.º 22
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");
            var messages = MockApplication.MockLogger.Messages;

            Assert.That(messages.Count, Is.EqualTo(0),
                        "An error was logged: " + messages.FirstOrDefault());
        }
Exemplo n.º 23
0
        public void ValueSetOnTwoWay(
            [Values(true, false)] bool setContextFirst,
            [Values(true, false)] bool isDefault)
        {
            const string value     = "Foo";
            var          viewmodel = new MockViewModel
            {
                Text = value
            };

            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), defaultValue: "default value", defaultBindingMode: propertyDefault);
            var binding  = CreateBinding(bindingMode);

            var bindable = new MockBindable();

            if (setContextFirst)
            {
                bindable.BindingContext = viewmodel;
                bindable.SetBinding(property, binding);
            }
            else
            {
                bindable.SetBinding(property, binding);
                bindable.BindingContext = viewmodel;
            }

            Assert.AreEqual(value, viewmodel.Text,
                            "BindingContext property changed");
            Assert.AreEqual(value, bindable.GetValue(property),
                            "Target property did not change");
            var messages = MockApplication.MockLogger.Messages;

            Assert.That(messages.Count, Is.EqualTo(0),
                        "An error was logged: " + messages.FirstOrDefault());
        }
Exemplo n.º 24
0
        public void TestStringFormat()
        {
            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"),
                    new Binding("baz"),
                },
                StringFormat = "{0} - {1} - {2}"
            };

            Assert.DoesNotThrow(() => bindable.SetBinding(property, multibinding));
            Assert.DoesNotThrow(() => bindable.BindingContext = new { foo = "FOO", bar = 42, baz = "BAZ" });
            Assert.That(bindable.GetValue(property), Is.EqualTo("FOO - 42 - BAZ"));
        }
Exemplo n.º 25
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");
            var messages = MockApplication.MockLogger.Messages;

            Assert.That(messages.Count, Is.EqualTo(0),
                        "An error was logged: " + messages.FirstOrDefault());
        }
Exemplo n.º 26
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"));
        }
Exemplo n.º 27
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"]);
        }
		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"]);
		}
Exemplo n.º 29
0
        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);
        }
Exemplo n.º 30
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));
        }
Exemplo n.º 31
0
        public void AttachAndDetach()
        {
            var behavior = new MockBehavior <MockBindable>();
            var bindable = new MockBindable();

            Assert.False(behavior.attached);
            Assert.False(behavior.detached);
            Assert.Null(behavior.AssociatedObject);

            ((IAttachedObject)behavior).AttachTo(bindable);

            Assert.True(behavior.attached);
            Assert.False(behavior.detached);
            Assert.AreSame(bindable, behavior.AssociatedObject);

            ((IAttachedObject)behavior).DetachFrom(bindable);

            Assert.True(behavior.attached);
            Assert.True(behavior.detached);
            Assert.Null(behavior.AssociatedObject);
        }
Exemplo n.º 32
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);
        }
Exemplo n.º 33
0
		public void ChangeAfterApply()
		{
			var property = BindableProperty.Create<MockBindable, string> (w => w.Foo, null);

			var binding = CreateBinding (BindingMode.OneWay);

			var vm = new MockViewModel { Text = "Bar" };
			var bo = new MockBindable { BindingContext = vm };
			bo.SetBinding (property, binding);

			Assert.That (() => binding.Mode = BindingMode.OneWayToSource, Throws.InvalidOperationException);
			Assert.That (() => binding.StringFormat = "{0}", Throws.InvalidOperationException);
		}