summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Core.UnitTests/BindablePropertyUnitTests.cs
diff options
context:
space:
mode:
authorJason Smith <jason.smith@xamarin.com>2016-03-22 13:02:25 -0700
committerJason Smith <jason.smith@xamarin.com>2016-03-22 16:13:41 -0700
commit17fdde66d94155fc62a034fa6658995bef6fd6e5 (patch)
treeb5e5073a2a7b15cdbe826faa5c763e270a505729 /Xamarin.Forms.Core.UnitTests/BindablePropertyUnitTests.cs
downloadxamarin-forms-17fdde66d94155fc62a034fa6658995bef6fd6e5.tar.gz
xamarin-forms-17fdde66d94155fc62a034fa6658995bef6fd6e5.tar.bz2
xamarin-forms-17fdde66d94155fc62a034fa6658995bef6fd6e5.zip
Initial import
Diffstat (limited to 'Xamarin.Forms.Core.UnitTests/BindablePropertyUnitTests.cs')
-rw-r--r--Xamarin.Forms.Core.UnitTests/BindablePropertyUnitTests.cs135
1 files changed, 135 insertions, 0 deletions
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<string> coerce = (bindable, value) => value;
+ BindableProperty.ValidateValueDelegate<string> validate = (b, v) => true;
+ BindableProperty.BindingPropertyChangedDelegate<string> changed = (b, ov, nv) => { };
+ BindableProperty.BindingPropertyChangingDelegate<string> changing = (b, ov, nv) => { };
+
+ var prop = BindableProperty.Create<Button, string> (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<Button, string> (b => b.Text, null, defaultBindingMode: mode);
+ Assert.AreEqual (BindingMode.OneWay, prop.DefaultBindingMode);
+ }
+
+ [Test]
+ public void CreateCasted()
+ {
+ var prop = BindableProperty.Create<Cell, bool> (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<T> : 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<string> changed = (b, ov, nv) => {
+ Assert.True (changingfired);
+ changedfired = true;
+ };
+ BindableProperty.BindingPropertyChangingDelegate<string> changing = (b, ov, nv) => {
+ Assert.False (changedfired);
+ changingfired = true;
+ };
+
+ var prop = BindableProperty.Create<Button, string> (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