summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Core.UnitTests/BindablePropertyUnitTests.cs
blob: 1d006a6d68721376c9380a85af62cf8a067d7ded (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
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));
		}
	}
}