forked from xamarin/Xamarin.Forms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBindableLayoutTests.cs
476 lines (383 loc) · 12.2 KB
/
BindableLayoutTests.cs
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using NUnit.Framework;
namespace Xamarin.Forms.Core.UnitTests
{
[TestFixture]
public class BindableLayoutTests : BaseTestFixture
{
[Test]
public void TracksEmpty()
{
var layout = new StackLayout
{
IsPlatformEnabled = true,
};
var itemsSource = new ObservableCollection<int>();
BindableLayout.SetItemsSource(layout, itemsSource);
Assert.IsTrue(IsLayoutWithItemsSource(itemsSource, layout));
}
[Test]
public void TracksAdd()
{
var layout = new StackLayout
{
IsPlatformEnabled = true,
};
var itemsSource = new ObservableCollection<int>();
BindableLayout.SetItemsSource(layout, itemsSource);
itemsSource.Add(1);
Assert.IsTrue(IsLayoutWithItemsSource(itemsSource, layout));
}
[Test]
public void TracksInsert()
{
var layout = new StackLayout
{
IsPlatformEnabled = true,
};
var itemsSource = new ObservableCollection<int>() { 0, 1, 2, 3, 4 };
BindableLayout.SetItemsSource(layout, itemsSource);
itemsSource.Insert(2, 5);
Assert.IsTrue(IsLayoutWithItemsSource(itemsSource, layout));
}
[Test]
public void TracksRemove()
{
var layout = new StackLayout
{
IsPlatformEnabled = true,
};
var itemsSource = new ObservableCollection<int>() { 0, 1 };
BindableLayout.SetItemsSource(layout, itemsSource);
itemsSource.RemoveAt(0);
Assert.IsTrue(IsLayoutWithItemsSource(itemsSource, layout));
itemsSource.Remove(1);
Assert.IsTrue(IsLayoutWithItemsSource(itemsSource, layout));
}
[Test]
public void TracksRemoveAll()
{
var layout = new StackLayout
{
IsPlatformEnabled = true,
};
var itemsSource = new ObservableRangeCollection<int>(Enumerable.Range(0, 10));
BindableLayout.SetItemsSource(layout, itemsSource);
itemsSource.RemoveAll();
Assert.IsTrue(IsLayoutWithItemsSource(itemsSource, layout));
}
[Test]
public void TracksReplace()
{
var layout = new StackLayout
{
IsPlatformEnabled = true,
};
var itemsSource = new ObservableCollection<int>() { 0, 1, 2 };
BindableLayout.SetItemsSource(layout, itemsSource);
itemsSource[0] = 3;
itemsSource[1] = 4;
itemsSource[2] = 5;
Assert.IsTrue(IsLayoutWithItemsSource(itemsSource, layout));
}
[Test]
public void TracksMove()
{
var layout = new StackLayout
{
IsPlatformEnabled = true,
};
var itemsSource = new ObservableCollection<int>() { 0, 1 };
BindableLayout.SetItemsSource(layout, itemsSource);
itemsSource.Move(0, 1);
Assert.IsTrue(IsLayoutWithItemsSource(itemsSource, layout));
itemsSource.Move(1, 0);
Assert.IsTrue(IsLayoutWithItemsSource(itemsSource, layout));
}
[Test]
public void TracksClear()
{
var layout = new StackLayout
{
IsPlatformEnabled = true,
};
var itemsSource = new ObservableCollection<int>() { 0, 1 };
BindableLayout.SetItemsSource(layout, itemsSource);
itemsSource.Clear();
Assert.IsTrue(IsLayoutWithItemsSource(itemsSource, layout));
}
[Test]
public void TracksNull()
{
var layout = new StackLayout
{
IsPlatformEnabled = true,
};
var itemsSource = new ObservableCollection<int>(Enumerable.Range(0, 10));
BindableLayout.SetItemsSource(layout, itemsSource);
Assert.IsTrue(IsLayoutWithItemsSource(itemsSource, layout));
itemsSource = null;
BindableLayout.SetItemsSource(layout, itemsSource);
Assert.IsTrue(IsLayoutWithItemsSource(itemsSource, layout));
}
[Test]
public void ItemTemplateIsSet()
{
var layout = new StackLayout
{
IsPlatformEnabled = true,
};
var itemsSource = new ObservableCollection<int>(Enumerable.Range(0, 10));
BindableLayout.SetItemsSource(layout, itemsSource);
BindableLayout.SetItemTemplate(layout, new DataTemplateBoxView());
Assert.IsTrue(IsLayoutWithItemsSource(itemsSource, layout));
Assert.AreEqual(itemsSource.Count, layout.Children.Cast<BoxView>().Count());
}
[Test]
public void ItemTemplateSelectorIsSet()
{
var layout = new StackLayout
{
IsPlatformEnabled = true,
};
var itemsSource = new ObservableCollection<int>(Enumerable.Range(0, 10));
BindableLayout.SetItemsSource(layout, itemsSource);
BindableLayout.SetItemTemplateSelector(layout, new DataTemplateSelectorFrame());
Assert.IsTrue(IsLayoutWithItemsSource(itemsSource, layout));
Assert.AreEqual(itemsSource.Count, layout.Children.Cast<Frame>().Count());
}
[Test]
public void ContainerIsPassedInSelectTemplate()
{
var layout = new StackLayout
{
IsPlatformEnabled = true,
};
var itemsSource = new ObservableCollection<int>(Enumerable.Range(0, 10));
BindableLayout.SetItemsSource(layout, itemsSource);
int containerPassedCount = 0;
BindableLayout.SetItemTemplateSelector(layout, new MyDataTemplateSelectorTest((item, container) =>
{
if (container == layout)
++containerPassedCount;
return null;
}));
Assert.AreEqual(containerPassedCount, itemsSource.Count);
}
[Test]
public void ItemTemplateTakesPrecendenceOverItemTemplateSelector()
{
var layout = new StackLayout
{
IsPlatformEnabled = true,
};
var itemsSource = new ObservableCollection<int>(Enumerable.Range(0, 10));
BindableLayout.SetItemsSource(layout, itemsSource);
BindableLayout.SetItemTemplate(layout, new DataTemplateBoxView());
BindableLayout.SetItemTemplateSelector(layout, new DataTemplateSelectorFrame());
Assert.IsTrue(IsLayoutWithItemsSource(itemsSource, layout));
Assert.AreEqual(itemsSource.Count, layout.Children.Cast<BoxView>().Count());
}
[Test]
public void ItemsSourceTakePrecendenceOverLayoutChildren()
{
var layout = new StackLayout
{
IsPlatformEnabled = true,
};
layout.Children.Add(new Label());
layout.Children.Add(new Label());
layout.Children.Add(new Label());
var itemsSource = new ObservableCollection<int>(Enumerable.Range(0, 10));
BindableLayout.SetItemsSource(layout, itemsSource);
Assert.IsTrue(IsLayoutWithItemsSource(itemsSource, layout));
}
[Test]
public void LayoutIsGarbageCollectedAfterItsRemoved()
{
var layout = new StackLayout
{
IsPlatformEnabled = true,
};
var itemsSource = new ObservableCollection<int>(Enumerable.Range(0, 10));
BindableLayout.SetItemsSource(layout, itemsSource);
var pageRoot = new Grid();
pageRoot.Children.Add(layout);
var page = new ContentPage() { Content = pageRoot };
var weakReference = new WeakReference(layout);
pageRoot.Children.Remove(layout);
layout = null;
GC.Collect();
GC.WaitForPendingFinalizers();
Assert.IsFalse(weakReference.IsAlive);
}
[Test]
public void ThrowsExceptionOnUsingDataTemplateSelectorForItemTemplate()
{
var layout = new StackLayout
{
IsPlatformEnabled = true,
};
var itemsSource = new ObservableCollection<int>(Enumerable.Range(0, 10));
BindableLayout.SetItemsSource(layout, itemsSource);
Assert.Throws(typeof(NotSupportedException), () => BindableLayout.SetItemTemplate(layout, new DataTemplateSelectorFrame()));
}
[Test]
public void DontTrackAfterItemsSourceChanged()
{
var layout = new StackLayout
{
IsPlatformEnabled = true,
};
var itemsSource = new ObservableCollection<int>(Enumerable.Range(0, 10));
BindableLayout.SetItemsSource(layout, itemsSource);
BindableLayout.SetItemsSource(layout, new ObservableCollection<int>(Enumerable.Range(0, 10)));
bool wasCalled = false;
layout.ChildAdded += (_, __) => wasCalled = true;
itemsSource.Add(11);
Assert.IsFalse(wasCalled);
}
[Test]
public void WorksWithNullItems()
{
var layout = new StackLayout
{
IsPlatformEnabled = true,
};
var itemsSource = new ObservableCollection<int?>(Enumerable.Range(0, 10).Cast<int?>());
itemsSource.Add(null);
BindableLayout.SetItemsSource(layout, itemsSource);
Assert.IsTrue(IsLayoutWithItemsSource(itemsSource, layout));
}
[Test]
public void WorksWithDuplicateItems()
{
var layout = new StackLayout
{
IsPlatformEnabled = true,
};
var itemsSource = new ObservableCollection<int>(Enumerable.Range(0, 10));
foreach (int item in itemsSource.ToList())
{
itemsSource.Add(item);
}
BindableLayout.SetItemsSource(layout, itemsSource);
Assert.IsTrue(IsLayoutWithItemsSource(itemsSource, layout));
itemsSource.Remove(0);
Assert.IsTrue(IsLayoutWithItemsSource(itemsSource, layout));
}
[Test]
public void ValidateBindableProperties()
{
var layout = new StackLayout
{
IsPlatformEnabled = true,
};
// EmptyView
object emptyView = new object();
BindableLayout.SetEmptyView(layout, emptyView);
Assert.AreEqual(emptyView, BindableLayout.GetEmptyView(layout));
Assert.AreEqual(emptyView, layout.GetValue(BindableLayout.EmptyViewProperty));
// EmptyViewTemplateProperty
DataTemplate emptyViewTemplate = new DataTemplate(typeof(Label));
BindableLayout.SetEmptyViewTemplate(layout, emptyViewTemplate);
Assert.AreEqual(emptyViewTemplate, BindableLayout.GetEmptyViewTemplate(layout));
Assert.AreEqual(emptyViewTemplate, layout.GetValue(BindableLayout.EmptyViewTemplateProperty));
// ItemsSourceProperty
IEnumerable itemsSource = new object[0];
BindableLayout.SetItemsSource(layout, itemsSource);
Assert.AreEqual(itemsSource, BindableLayout.GetItemsSource(layout));
Assert.AreEqual(itemsSource, layout.GetValue(BindableLayout.ItemsSourceProperty));
// ItemTemplateProperty
DataTemplate itemTemplate = new DataTemplate(typeof(Label));
BindableLayout.SetItemTemplate(layout, itemTemplate);
Assert.AreEqual(itemTemplate, BindableLayout.GetItemTemplate(layout));
Assert.AreEqual(itemTemplate, layout.GetValue(BindableLayout.ItemTemplateProperty));
// ItemTemplateSelectorProperty
var itemTemplateSelector = new DataTemplateSelectorFrame();
BindableLayout.SetItemTemplateSelector(layout, itemTemplateSelector);
Assert.AreEqual(itemTemplateSelector, BindableLayout.GetItemTemplateSelector(layout));
Assert.AreEqual(itemTemplateSelector, layout.GetValue(BindableLayout.ItemTemplateSelectorProperty));
}
// Checks if for every item in the items source there's a corresponding view
static bool IsLayoutWithItemsSource(IEnumerable itemsSource, Layout layout)
{
if (itemsSource == null)
{
return layout.Children.Count() == 0;
}
int i = 0;
foreach (object item in itemsSource)
{
if (BindableLayout.GetItemTemplate(layout) is DataTemplate dataTemplate ||
BindableLayout.GetItemTemplateSelector(layout) is DataTemplateSelector dataTemplateSelector)
{
if (!Equals(item, layout.Children[i].BindingContext))
{
return false;
}
}
else
{
if (!Equals(item?.ToString(), ((Label)layout.Children[i]).Text))
{
return false;
}
}
++i;
}
return layout.Children.Count == i;
}
class DataTemplateBoxView : DataTemplate
{
public DataTemplateBoxView() : base(() => new BoxView())
{
}
}
class DataTemplateFrame : DataTemplate
{
public DataTemplateFrame() : base(() => new Frame())
{
}
}
class DataTemplateSelectorFrame : DataTemplateSelector
{
DataTemplateFrame dt = new DataTemplateFrame();
protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
{
return dt;
}
}
class ObservableRangeCollection<T> : ObservableCollection<T>
{
public ObservableRangeCollection(IEnumerable<T> collection)
: base(collection)
{
}
public void RemoveAll()
{
CheckReentrancy();
var changedItems = new List<T>(Items);
foreach (var i in changedItems)
Items.Remove(i);
OnPropertyChanged(new PropertyChangedEventArgs("Count"));
OnPropertyChanged(new PropertyChangedEventArgs("Item[]"));
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, changedItems, 0));
}
}
class MyDataTemplateSelectorTest : DataTemplateSelector
{
readonly Func<object, BindableObject, DataTemplate> _func;
public MyDataTemplateSelectorTest(Func<object, BindableObject, DataTemplate> func)
=> _func = func;
protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
=> _func(item, container);
}
}
}