-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
CollectionViewTests.cs
266 lines (223 loc) · 8.16 KB
/
CollectionViewTests.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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.Handlers.Items;
using Microsoft.Maui.DeviceTests.Stubs;
using Microsoft.Maui.Graphics;
using Microsoft.Maui.Handlers;
using Microsoft.Maui.Hosting;
using Xunit;
using Xunit.Abstractions;
namespace Microsoft.Maui.DeviceTests
{
[Collection(RunInNewWindowCollection)]
[Category(TestCategory.CollectionView)]
public partial class CollectionViewTests : ControlsHandlerTestBase
{
void SetupBuilder()
{
EnsureHandlerCreated(builder =>
{
builder.ConfigureMauiHandlers(handlers =>
{
handlers.AddHandler(typeof(Toolbar), typeof(ToolbarHandler));
handlers.AddHandler(typeof(NavigationPage), typeof(NavigationViewHandler));
handlers.AddHandler<Page, PageHandler>();
handlers.AddHandler<Window, WindowHandlerStub>();
handlers.AddHandler<CollectionView, CollectionViewHandler>();
handlers.AddHandler<VerticalStackLayout, LayoutHandler>();
handlers.AddHandler<Grid, LayoutHandler>();
handlers.AddHandler<Label, LabelHandler>();
});
});
}
[Fact]
public async Task ItemsSourceDoesNotLeak()
{
SetupBuilder();
IList logicalChildren = null;
WeakReference weakReference = null;
var collectionView = new CollectionView
{
ItemTemplate = new DataTemplate(() => new Label())
};
await CreateHandlerAndAddToWindow<CollectionViewHandler>(collectionView, async handler =>
{
var data = new ObservableCollection<string>()
{
"Item 1",
"Item 2",
"Item 3"
};
weakReference = new WeakReference(data);
collectionView.ItemsSource = data;
await Task.Delay(100);
// Get ItemsView._logicalChildren
var flags = BindingFlags.NonPublic | BindingFlags.Instance;
logicalChildren = typeof(Element).GetField("_internalChildren", flags).GetValue(collectionView) as IList;
Assert.NotNull(logicalChildren);
// Replace with cloned collection
collectionView.ItemsSource = new ObservableCollection<string>(data);
await Task.Delay(100);
});
await AssertionExtensions.WaitForGC(weakReference);
Assert.False(weakReference.IsAlive, "ObservableCollection should not be alive!");
Assert.NotNull(logicalChildren);
Assert.True(logicalChildren.Count <= 3, "_logicalChildren should not grow in size!");
}
[Theory]
[MemberData(nameof(GenerateLayoutOptionsCombos))]
public async Task CollectionViewCanSizeToContent(CollectionViewSizingTestCase testCase)
{
// The goal of this test is to create a CollectionView inside a container with each combination of
// ItemsLayout (vertical or horizontal collection) and LayoutAlignment (Fill, Center, etc).
// And then layout that CollectionView using a fixed-size template and different sizes of collection
// At each collection size, we check the size of the CollectionView to verify that it's laying out
// at its content size, or at the size of the container (if the number of items is sufficiently large)
var itemsLayout = testCase.ItemsLayout;
var layoutOptions = testCase.LayoutOptions;
double templateHeight = 50;
double templateWidth = 50;
double containerHeight = 500;
double containerWidth = 500;
int[] itemCounts = new int[] { 1, 2, 12, 0 };
double tolerance = 1;
SetupBuilder();
var collectionView = new CollectionView
{
ItemsLayout = itemsLayout,
Background = Colors.Red,
ItemTemplate = new DataTemplate(() => new Label() { HeightRequest = templateHeight, WidthRequest = templateWidth }),
};
if (itemsLayout.Orientation == ItemsLayoutOrientation.Horizontal)
{
collectionView.HorizontalOptions = layoutOptions;
}
else
{
collectionView.VerticalOptions = layoutOptions;
}
var layout = new Grid() { IgnoreSafeArea = true, HeightRequest = containerHeight, WidthRequest = containerWidth };
layout.Add(collectionView);
ObservableCollection<string> data = new();
var frame = collectionView.Frame;
await CreateHandlerAndAddToWindow<LayoutHandler>(layout, async handler =>
{
for (int n = 0; n < itemCounts.Length; n++)
{
int itemsCount = itemCounts[n];
GenerateItems(itemsCount, data);
collectionView.ItemsSource = data;
if (n == 0)
{
await AssertionExtensions.Wait(() => collectionView.Frame.Width > 0 && collectionView.Frame.Height > 0);
}
else
{
await WaitForUIUpdate(frame, collectionView);
}
frame = collectionView.Frame;
double expectedWidth = layoutOptions == LayoutOptions.Fill
? containerWidth
: Math.Min(itemsCount * templateWidth, containerWidth);
double expectedHeight = layoutOptions == LayoutOptions.Fill
? containerHeight
: Math.Min(itemsCount * templateHeight, containerHeight);
if (itemsLayout.Orientation == ItemsLayoutOrientation.Horizontal)
{
Assert.Equal(expectedWidth, collectionView.Width, tolerance);
}
else
{
Assert.Equal(expectedHeight, collectionView.Height, tolerance);
}
}
});
}
[Theory]
[InlineData(true, false, false)]
[InlineData(true, false, true)]
[InlineData(true, true, false)]
[InlineData(true, true, true)]
[InlineData(false, false, false)]
[InlineData(false, false, true)]
[InlineData(false, true, false)]
[InlineData(false, true, true)]
public async Task CollectionViewStructuralItems(bool hasHeader, bool hasFooter, bool hasData)
{
SetupBuilder();
double containerHeight = 500;
double containerWidth = 500;
var layout = new Grid() { IgnoreSafeArea = true, HeightRequest = containerHeight, WidthRequest = containerWidth };
Label headerLabel = hasHeader ? new Label { Text = "header" } : null;
Label footerLabel = hasFooter ? new Label { Text = "footer" } : null;
var collectionView = new CollectionView
{
ItemsLayout = LinearItemsLayout.Vertical,
ItemTemplate = new DataTemplate(() => new Label() { HeightRequest = 20, WidthRequest = 20 }),
Header = headerLabel,
Footer = footerLabel,
ItemsSource = hasData ? null : new ObservableCollection<string> { "data" }
};
layout.Add(collectionView);
var frame = collectionView.Frame;
await CreateHandlerAndAddToWindow<LayoutHandler>(layout, async handler =>
{
await WaitForUIUpdate(frame, collectionView);
frame = collectionView.Frame;
#if WINDOWS
// On Windows, the ListView pops in and changes the frame, then actually
// loads in the data, which updates it again. So we need to wait for the second
// update before checking the size
await WaitForUIUpdate(frame, collectionView);
frame = collectionView.Frame;
#endif
if (hasHeader)
{
Assert.True(headerLabel.Height > 0);
Assert.True(headerLabel.Width > 0);
}
if (hasFooter)
{
Assert.True(footerLabel.Height > 0);
Assert.True(footerLabel.Width > 0);
}
});
}
public static IEnumerable<object[]> GenerateLayoutOptionsCombos()
{
var layoutOptions = new LayoutOptions[] { LayoutOptions.Center, LayoutOptions.Start, LayoutOptions.End, LayoutOptions.Fill };
foreach (var option in layoutOptions)
{
yield return new object[] { new CollectionViewSizingTestCase(option, new LinearItemsLayout(ItemsLayoutOrientation.Horizontal)) };
yield return new object[] { new CollectionViewSizingTestCase(option, new LinearItemsLayout(ItemsLayoutOrientation.Vertical)) };
yield return new object[] { new CollectionViewSizingTestCase(option, new LinearItemsLayout(ItemsLayoutOrientation.Horizontal)) };
yield return new object[] { new CollectionViewSizingTestCase(option, new LinearItemsLayout(ItemsLayoutOrientation.Vertical)) };
}
}
static void GenerateItems(int count, ObservableCollection<string> data)
{
if (data.Count > count)
{
data.Clear();
}
for (int n = data.Count; n < count; n++)
{
data.Add($"Item {n}");
}
}
static async Task WaitForUIUpdate(Rect frame, CollectionView collectionView, int timeout = 1000, int interval = 100)
{
// Wait for layout to happen
while (collectionView.Frame == frame && timeout >= 0)
{
await Task.Delay(interval);
timeout -= interval;
}
}
}
}