summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Core.UnitTests/TableSectionTests.cs
blob: bcf405b54fd1352020066ff04a885929f25e65ad (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
using System.Collections.Generic;
using NUnit.Framework;

using NContains = NUnit.Framework.Contains;

namespace Xamarin.Forms.Core.UnitTests
{
	[TestFixture]
	public class TableSectionTests : BaseTestFixture
	{
		[SetUp]
		public void Setup()
		{
			Device.PlatformServices = new MockPlatformServices ();
		}

		[TearDown]
		public void TearDown()
		{
			Device.PlatformServices = null;
		}

		[Test]
		public void Constructor ()
		{
			var section = new TableSection ("Title");
			Assert.AreEqual ("Title", section.Title);
			Assert.That (section, Is.Empty);
		}

		[Test]
		public void IsReadOnly ()
		{
			var section = new TableSection () as ICollection<Cell>;
			Assert.False (section.IsReadOnly);
		}

		[Test]
		public void Add ()
		{
			var section = new TableSection ();
			TextCell first, second;
			section.Add (first = new TextCell { Text = "Text" });
			section.Add (second = new TextCell { Text = "Text" });

			Assert.That (section, NContains.Item (first));
			Assert.That (section, NContains.Item (second));
		}

		[Test]
		public void Remove ()
		{
			var section = new TableSection ();
			TextCell first;
			section.Add (first = new TextCell { Text = "Text" });
			section.Add (new TextCell { Text = "Text" });

			var result = section.Remove (first);
			Assert.True (result);
			Assert.That (section, Has.No.Contains (first));
		}

		[Test]
		public void Clear ()
		{
			var section = new TableSection {new TextCell { Text = "Text" }, new TextCell { Text = "Text" }};
			section.Clear ();
			Assert.That (section, Is.Empty);
		}

		[Test]
		public void Contains ()
		{
			var section = new TableSection ();
			TextCell first, second;
			section.Add (first = new TextCell { Text = "Text" });
			section.Add (second = new TextCell { Text = "Text" });

			Assert.True (section.Contains (first));
			Assert.True (section.Contains (second));
		}

		[Test]
		public void IndexOf ()
		{
			var section = new TableSection ();
			TextCell first, second;
			section.Add (first = new TextCell { Text = "Text" });
			section.Add (second = new TextCell { Text = "Text" });

			Assert.AreEqual (0, section.IndexOf (first));
			Assert.AreEqual (1, section.IndexOf (second));
		}

		[Test]
		public void Insert ()
		{
			var section = new TableSection ();
			section.Add (new TextCell { Text = "Text" });
			section.Add (new TextCell { Text = "Text" });

			var third = new TextCell { Text = "Text" };
			section.Insert (1, third);
			Assert.AreEqual (third, section[1]);
		}

		[Test]
		public void RemoveAt ()
		{
			var section = new TableSection ();
			TextCell first, second;
			section.Add (first = new TextCell { Text = "Text" });
			section.Add (second = new TextCell { Text = "Text" });

			section.RemoveAt (0);
			Assert.That (section, Has.No.Contains (first));
		}

		[Test]
		public void Overwrite ()
		{
			var section = new TableSection ();
			TextCell second;
			section.Add (new TextCell { Text = "Text" });
			section.Add (second = new TextCell { Text = "Text" });

			var third = new TextCell { Text = "Text" };
			section[1] = third;

			Assert.AreEqual (third, section[1]);
			Assert.That (section, Has.No.Contains (second));
		}

		[Test]
		public void CopyTo ()
		{
			var section = new TableSection ();
			TextCell first, second;
			section.Add (first = new TextCell { Text = "Text" });
			section.Add (second = new TextCell { Text = "Text" });

			Cell[] cells = new Cell[2];
			section.CopyTo (cells, 0);

			Assert.AreEqual (first, cells[0]);
			Assert.AreEqual (second, cells[1]);
		}

		[Test]
		public void ChainsBindingContextOnSet ()
		{
			var section = new TableSection ();
			TextCell first, second;
			section.Add (first = new TextCell { Text = "Text" });
			section.Add (second = new TextCell { Text = "Text" });

			var bindingContext = "bindingContext";

			section.BindingContext = bindingContext;

			Assert.AreEqual (bindingContext, first.BindingContext);
			Assert.AreEqual (bindingContext, second.BindingContext);
		}

		[Test]
		public void ChainsBindingContextWithExistingContext ()
		{
			var section = new TableSection ();
			TextCell first, second;
			section.Add (first = new TextCell { Text = "Text" });
			section.Add (second = new TextCell { Text = "Text" });

			var bindingContext = "bindingContext";
			section.BindingContext = bindingContext;

			bindingContext = "newContext";
			section.BindingContext = bindingContext;

			Assert.AreEqual (bindingContext, first.BindingContext);
			Assert.AreEqual (bindingContext, second.BindingContext);
		}

		[Test]
		public void ChainsBindingContextToNewlyAdded ()
		{
			var section = new TableSection ();
			var bindingContext = "bindingContext";
			section.BindingContext = bindingContext;

			TextCell first, second;
			section.Add (first = new TextCell { Text = "Text" });
			section.Add (second = new TextCell { Text = "Text" });

			Assert.AreEqual (bindingContext, first.BindingContext);
			Assert.AreEqual (bindingContext, second.BindingContext);
		}

		[Test]
		public void TestBindingTitleSectionChange ()
		{
			var vm = new MockViewModel { Text = "FooBar" };
			var section = new TableSection ();

			section.BindingContext = vm;
			section.SetBinding (TableSectionBase.TitleProperty, "Text");

			Assert.AreEqual ("FooBar", section.Title);

			vm.Text = "Baz";
		
			Assert.AreEqual ("Baz", section.Title);
		}
	
		[Test]
		public void TestBindingTitle ()
		{
			var section = new TableSection ();
			var mock = new MockViewModel ();
			section.BindingContext = mock;
			section.SetBinding (TableSection.TitleProperty, new Binding ("Text"));

			Assert.AreEqual (mock.Text, section.Title);
		}
	}
}