From 17fdde66d94155fc62a034fa6658995bef6fd6e5 Mon Sep 17 00:00:00 2001 From: Jason Smith Date: Tue, 22 Mar 2016 13:02:25 -0700 Subject: Initial import --- .../BindablePropertyUnitTests.cs | 135 +++++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 Xamarin.Forms.Core.UnitTests/BindablePropertyUnitTests.cs (limited to 'Xamarin.Forms.Core.UnitTests/BindablePropertyUnitTests.cs') diff --git a/Xamarin.Forms.Core.UnitTests/BindablePropertyUnitTests.cs b/Xamarin.Forms.Core.UnitTests/BindablePropertyUnitTests.cs new file mode 100644 index 00000000..1d006a6d --- /dev/null +++ b/Xamarin.Forms.Core.UnitTests/BindablePropertyUnitTests.cs @@ -0,0 +1,135 @@ +using System; +using System.Linq; +using NUnit.Framework; + +namespace Xamarin.Forms.Core.UnitTests +{ + [TestFixture] + public class BindablePropertyUnitTests : BaseTestFixture + { + [Test] + public void Create() + { + const BindingMode mode = BindingMode.OneWayToSource; + const string dvalue = "default"; + BindableProperty.CoerceValueDelegate coerce = (bindable, value) => value; + BindableProperty.ValidateValueDelegate validate = (b, v) => true; + BindableProperty.BindingPropertyChangedDelegate changed = (b, ov, nv) => { }; + BindableProperty.BindingPropertyChangingDelegate changing = (b, ov, nv) => { }; + + var prop = BindableProperty.Create (b => b.Text, dvalue, mode, validate, changed, changing, coerce); + Assert.AreEqual ("Text", prop.PropertyName); + Assert.AreEqual (typeof (Button), prop.DeclaringType); + Assert.AreEqual (typeof (string), prop.ReturnType); + Assert.AreEqual (dvalue, prop.DefaultValue); + Assert.AreEqual (mode, prop.DefaultBindingMode); + } + + [Test] + public void CreateWithDefaultMode () + { + const BindingMode mode = BindingMode.Default; + var prop = BindableProperty.Create (b => b.Text, null, defaultBindingMode: mode); + Assert.AreEqual (BindingMode.OneWay, prop.DefaultBindingMode); + } + + [Test] + public void CreateCasted() + { + var prop = BindableProperty.Create (c => c.IsEnabled, true); + + Assert.AreEqual ("IsEnabled", prop.PropertyName); + Assert.AreEqual (typeof (Cell), prop.DeclaringType); + Assert.AreEqual (typeof (bool), prop.ReturnType); + } + + [Test] + public void CreateNonGeneric() + { + const BindingMode mode = BindingMode.OneWayToSource; + const string dvalue = "default"; + BindableProperty.CoerceValueDelegate coerce = (bindable, value) => value; + BindableProperty.ValidateValueDelegate validate = (b, v) => true; + BindableProperty.BindingPropertyChangedDelegate changed = (b, ov, nv) => { }; + BindableProperty.BindingPropertyChangingDelegate changing = (b, ov, nv) => { }; + + var prop = BindableProperty.Create ("Text", typeof(string), typeof(Button), dvalue, mode, validate, changed, changing, coerce); + Assert.AreEqual ("Text", prop.PropertyName); + Assert.AreEqual (typeof (Button), prop.DeclaringType); + Assert.AreEqual (typeof (string), prop.ReturnType); + Assert.AreEqual (dvalue, prop.DefaultValue); + Assert.AreEqual (mode, prop.DefaultBindingMode); + } + + class GenericView : View + { + public string Text + { + get; + set; + } + } + + [Test] + public void CreateForGeneric() + { + const BindingMode mode = BindingMode.OneWayToSource; + const string dvalue = "default"; + BindableProperty.CoerceValueDelegate coerce = (bindable, value) => value; + BindableProperty.ValidateValueDelegate validate = (b, v) => true; + BindableProperty.BindingPropertyChangedDelegate changed = (b, ov, nv) => { }; + BindableProperty.BindingPropertyChangingDelegate changing = (b, ov, nv) => { }; + + var prop = BindableProperty.Create ("Text", typeof(string), typeof(GenericView<>), dvalue, mode, validate, changed, changing, coerce); + Assert.AreEqual ("Text", prop.PropertyName); + Assert.AreEqual (typeof (GenericView<>), prop.DeclaringType); + Assert.AreEqual (typeof (string), prop.ReturnType); + Assert.AreEqual (dvalue, prop.DefaultValue); + Assert.AreEqual (mode, prop.DefaultBindingMode); + } + + [Test] + public void ChangingBeforeChanged () + { + bool changingfired = false; + bool changedfired = false; + BindableProperty.BindingPropertyChangedDelegate changed = (b, ov, nv) => { + Assert.True (changingfired); + changedfired = true; + }; + BindableProperty.BindingPropertyChangingDelegate changing = (b, ov, nv) => { + Assert.False (changedfired); + changingfired = true; + }; + + var prop = BindableProperty.Create (b => b.Text, "Foo", + propertyChanging: changing, + propertyChanged: changed); + + Assert.False (changingfired); + Assert.False (changedfired); + + (new View ()).SetValue (prop, "Bar"); + + Assert.True (changingfired); + Assert.True (changedfired); + } + + [Test] + 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)); + } + } +} \ No newline at end of file -- cgit v1.2.3