summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Core.UnitTests/ResourceDictionaryTests.cs
blob: 3621d95593def63e84a403c8444b0fda00a4a2b7 (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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
´╗┐using System;
using System.Linq;
using NUnit.Framework;
using System.Collections.Generic;
using Xamarin.Forms.Internals;

namespace Xamarin.Forms.Core.UnitTests
{
	[TestFixture]
	public class ResourceDictionaryTests : BaseTestFixture
	{
		[Test]
		public void Add ()
		{
			var rd = new ResourceDictionary ();
			rd.Add ("foo", "bar");
			Assert.AreEqual ("bar", rd ["foo"]);
		}

		[Test]
		public void AddKVP ()
		{
			var rd = new ResourceDictionary ();
			((ICollection<KeyValuePair<string,object>>)rd).Add (new KeyValuePair<string,object> ("foo", "bar"));
			Assert.AreEqual ("bar", rd ["foo"]);
		}

		[Test]
		public void ResourceDictionaryTriggersValueChangedOnAdd ()
		{
			var rd = new ResourceDictionary ();
			((IResourceDictionary)rd).ValuesChanged += (sender, e) => {
				Assert.AreEqual (1, e.Values.Count());
				var kvp = e.Values.First();
				Assert.AreEqual ("foo", kvp.Key);
				Assert.AreEqual ("FOO", kvp.Value);
				Assert.Pass ();
			};
			rd.Add ("foo", "FOO");
			Assert.Fail ();
		}

		[Test]
		public void ResourceDictionaryTriggersValueChangedOnChange ()
		{
			var rd = new ResourceDictionary ();
			rd.Add ("foo", "FOO");
			((IResourceDictionary)rd).ValuesChanged += (sender, e) => {
				Assert.AreEqual (1, e.Values.Count());
				var kvp = e.Values.First();
				Assert.AreEqual ("foo", kvp.Key);
				Assert.AreEqual ("BAR", kvp.Value);
				Assert.Pass ();
			};
			rd ["foo"] = "BAR";
			Assert.Fail ();
		}

		[Test]
		public void ResourceDictionaryCtor ()
		{
			var rd = new ResourceDictionary ();
			Assert.AreEqual (0, rd.Count());
		}

		[Test]
		public void ElementMergesParentRDWithCurrent ()
		{
			var elt = new VisualElement {
				Resources = new ResourceDictionary { 
					{ "bar","BAR" },
				}
			};

			var parent = new VisualElement {
				Resources = new ResourceDictionary { 
					{ "foo", "FOO" },
				}
			};

			elt.Parent = parent;

			object value;
			Assert.True (elt.TryGetResource ("foo", out value));
			Assert.AreEqual ("FOO", value);
			Assert.True (elt.TryGetResource ("bar", out value));
			Assert.AreEqual ("BAR", value);
		}

		[Test]
		public void CurrentOverridesParentValues ()
		{
			var elt = new VisualElement {
				Resources = new ResourceDictionary { 
					{ "bar","BAZ" },
				}
			};

			var parent = new VisualElement {
				Resources = new ResourceDictionary { 
					{ "foo", "FOO" },
					{ "bar","BAR" },
				}
			};

			elt.Parent = parent;

			object value;
			Assert.True (elt.TryGetResource ("foo", out value));
			Assert.AreEqual ("FOO", value);
			Assert.True (elt.TryGetResource ("bar", out value));
			Assert.AreEqual ("BAZ", value);
		}

		[Test]
		public void AddingToParentTriggersValuesChanged ()
		{
			var elt = new VisualElement {
				Resources = new ResourceDictionary { 
					{ "bar","BAR" },
				}
			};

			var parent = new VisualElement {
				Resources = new ResourceDictionary { 
					{ "foo", "FOO" },
				}
			};

			elt.Parent = parent;

			((IElement)elt).AddResourcesChangedListener ((sender, e) => {
				Assert.AreEqual (1, e.Values.Count ());
				var kvp = e.Values.First ();
				Assert.AreEqual ("baz", kvp.Key);
				Assert.AreEqual ("BAZ", kvp.Value);
				Assert.Pass ();
			});

			parent.Resources ["baz"] = "BAZ";
			Assert.Fail ();
		}

		[Test]
		public void ResourcesChangedNotRaisedIfKeyExistsInCurrent ()
		{
			var elt = new VisualElement {
				Resources = new ResourceDictionary { 
					{ "bar","BAR" },
				}
			};

			var parent = new VisualElement {
				Resources = new ResourceDictionary { 
					{ "foo", "FOO" },
				}
			};

			elt.Parent = parent;

			((IElement)elt).AddResourcesChangedListener ((sender, e) => Assert.Fail ());
			parent.Resources ["bar"] = "BAZ";
			Assert.Pass ();
		}

		[Test]
		public void SettingParentTriggersValuesChanged ()
		{
			var elt = new VisualElement {
				Resources = new ResourceDictionary { 
					{ "bar","BAR" },
				}
			};

			var parent = new VisualElement {
				Resources = new ResourceDictionary { 
					{"foo", "FOO"},
					{"baz", "BAZ"},
					{"bar", "NEWBAR"}
				}
			};

			((IElement)elt).AddResourcesChangedListener ((sender, e) => {
				Assert.AreEqual (2, e.Values.Count ());
				Assert.AreEqual ("FOO", e.Values.First (kvp => kvp.Key == "foo").Value);
				Assert.AreEqual ("BAZ", e.Values.First (kvp => kvp.Key == "baz").Value);
				Assert.Pass ();
			});
			elt.Parent = parent;
			Assert.Fail ();
		}

		[Test]
		public void SettingResourcesTriggersResourcesChanged ()
		{
			var elt = new VisualElement ();

			var parent = new VisualElement {
				Resources = new ResourceDictionary { 
					{"bar", "BAR"}
				}
			};

			elt.Parent = parent;

			((IElement)elt).AddResourcesChangedListener ((sender, e) => {
				Assert.AreEqual (3, e.Values.Count ());
				Assert.Pass ();
			});
			elt.Resources = new ResourceDictionary { 
				{"foo", "FOO"},
				{"baz", "BAZ"},
				{"bar", "NEWBAR"}
			};
			Assert.Fail();
		}

		[Test]
		public void DontThrowOnReparenting ()
		{
			var elt = new View { Resources = new ResourceDictionary () };
			var parent = new StackLayout ();

			parent.Children.Add (elt);
			Assert.DoesNotThrow (() => parent.Children.Remove (elt));
		}

		[Test]
		public void MultiLevelMerge ()
		{
			var elt = new VisualElement {
				Resources = new ResourceDictionary { 
					{ "bar","BAR" },
				}
			};

			var parent = new VisualElement {
				Resources = new ResourceDictionary { 
					{ "foo", "FOO" },
				},
				Parent = new VisualElement {
					Resources = new ResourceDictionary { 
						{"baz", "BAZ"}
					}
				}
			};

			((IElement)elt).AddResourcesChangedListener ((sender, e) => {
				Assert.AreEqual (2, e.Values.Count ());
				Assert.Pass ();
			});

			elt.Parent = parent;
			Assert.Fail ();
		}

		[Test]
		public void ShowKeyInExceptionIfNotFound()
		{
			var rd = new ResourceDictionary();
			rd.Add("foo", "bar");
			var ex = Assert.Throws<KeyNotFoundException>(() => { var foo = rd ["test_invalid_key"]; });
			Assert.That(ex.Message, Is.StringContaining("test_invalid_key"));
		}

		class MyRD : ResourceDictionary
		{
			public MyRD()
			{
				CreationCount = CreationCount + 1;
				Add("foo", "Foo");
				Add("bar", "Bar");
			}

			public static int CreationCount { get; set; }
		}

		[Test]
		public void MergedWithFailsToMergeAnythingButRDs()
		{
			var rd = new ResourceDictionary();
			Assert.DoesNotThrow(() => rd.MergedWith = typeof(MyRD));
			Assert.Throws<ArgumentException>(() => rd.MergedWith = typeof(ContentPage));
		}

		[Test]
		public void MergedResourcesAreFound()
		{
			var rd0 = new ResourceDictionary();
			rd0.MergedWith = typeof(MyRD);

			object _;
			Assert.True(rd0.TryGetMergedValue("foo", out _));
			Assert.AreEqual("Foo", _);
		}

		[Test]
		public void ThrowOnDuplicateKey()
		{
			var rd0 = new ResourceDictionary();
			rd0.Add("foo", "Foo");
			try {
				rd0.Add("foo", "Bar");
			} catch (ArgumentException ae) {
				Assert.AreEqual("A resource with the key 'foo' is already present in the ResourceDictionary.", ae.Message);
				Assert.Pass();
			}
			Assert.Fail();
		}
	}
}