summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Core.UnitTests/DataTemplateTests.cs
blob: 24bf6a06b8bc6f4fc9f164b2774388be7270a24c (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
using System;
using System.Collections.Generic;
using NUnit.Framework;

namespace Xamarin.Forms.Core.UnitTests
{
	[TestFixture]
	public class DataTemplateTests : BaseTestFixture
	{
		[Test]
		public void CtorInvalid()
		{
			Assert.Throws<ArgumentNullException> (() => new DataTemplate ((Func<object>)null),
				"Allowed null creator delegate");

			Assert.Throws<ArgumentNullException> (() => new DataTemplate ((Type)null),
				"Allowed null type");
		}

		[Test]
		public void CreateContent()
		{
			var template = new DataTemplate (() => new MockBindable());
			object obj = template.CreateContent();

			Assert.IsNotNull (obj);
			Assert.That (obj, Is.InstanceOf<MockBindable>());
		}

		[Test]
		public void CreateContentType()
		{
			var template = new DataTemplate (typeof (MockBindable));
			object obj = template.CreateContent();

			Assert.IsNotNull (obj);
			Assert.That (obj, Is.InstanceOf<MockBindable>());
		}

		[Test]
		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"));
		}

		[Test]
		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"));
		}

		[Test]
		public void SetBindingInvalid()
		{
			var template = new DataTemplate (typeof (MockBindable));
			Assert.That (() => template.SetBinding (null, new Binding (".")), Throws.InstanceOf<ArgumentNullException>());
			Assert.That (() => template.SetBinding (MockBindable.TextProperty, null), Throws.InstanceOf<ArgumentNullException>());
		}

		[Test]
		public void SetBindingOverridesValue()
		{
			var template = new DataTemplate (typeof (MockBindable));
			template.SetValue (MockBindable.TextProperty, "value");
			template.SetBinding (MockBindable.TextProperty, new Binding ("."));

			MockBindable bindable = (MockBindable) template.CreateContent();
			Assume.That (bindable.GetValue (MockBindable.TextProperty), Is.EqualTo (bindable.BindingContext));

			bindable.BindingContext = "binding";
			Assert.That (bindable.GetValue (MockBindable.TextProperty), Is.EqualTo ("binding"));
		}

		[Test]
		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"));
		}

		[Test]
		public void SetValueInvalid()
		{
			var template = new DataTemplate (typeof (MockBindable));
			Assert.That (() => template.SetValue (null, "string"), Throws.InstanceOf<ArgumentNullException>());
		}

		[Test]
		public void SetValueAndBinding ()
		{
			var template = new DataTemplate (typeof (TextCell)) {
				Bindings = {
					{TextCell.TextProperty, new Binding ("Text")}
				},
				Values = {
					{TextCell.TextProperty, "Text"}
				}
			};
			Assert.That (() => template.CreateContent (), Throws.InstanceOf<InvalidOperationException> ());			
		}
	}
}