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()); }
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 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 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)); }
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 BindingContextChangedBindingsApplied() { var mock = new MockBindable(); mock.SetBinding(MockBindable.TextProperty, new Binding(".")); mock.BindingContext = "Test"; Assert.AreEqual("Test", mock.GetValue(MockBindable.TextProperty)); mock.BindingContext = "Testing"; Assert.AreEqual("Testing", mock.GetValue(MockBindable.TextProperty), "Bindings were not reapplied to the new binding context"); }
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); }
void TestGetValueDefault(BindableProperty property) { var mock = new MockBindable(); object value = mock.GetValue(property); Assert.AreEqual(property.DefaultValue, value); }
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); }
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 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)); }
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 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)); }
public void DefaultValueCreatorIsInvokedOnlyAtFirstTime() { int invoked = 0; var bindableProperty = BindableProperty.Create("Foo", typeof(object), typeof(MockBindable), null, defaultValueCreator: o => { invoked++; return(new object()); }); var bindable = new MockBindable(); var value0 = bindable.GetValue(bindableProperty); var value1 = bindable.GetValue(bindableProperty); Assert.NotNull(value0); Assert.NotNull(value1); Assert.AreSame(value0, value1); Assert.AreEqual(1, invoked); }
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)); }
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 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()); }
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 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 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 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 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 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 %")); } }
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")); }
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"); }
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."); }
public void DefaultValueCreatorNotSharedAccrossInstances() { int invoked = 0; var bindableProperty = BindableProperty.Create("Foo", typeof(object), typeof(MockBindable), null, defaultValueCreator: o => { invoked++; return(new object()); }); var bindable0 = new MockBindable(); var bindable1 = new MockBindable(); var value0 = bindable0.GetValue(bindableProperty); var value1 = bindable1.GetValue(bindableProperty); Assert.AreNotSame(value0, value1); Assert.AreEqual(2, invoked); }
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% }