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
|
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Linq;
using System.Threading.Tasks;
using BasicTestApp;
using Microsoft.AspNetCore.Components.E2ETest.Infrastructure;
using Microsoft.AspNetCore.Components.E2ETest.Infrastructure.ServerFixtures;
using Microsoft.AspNetCore.E2ETesting;
using Microsoft.AspNetCore.Testing;
using OpenQA.Selenium;
using OpenQA.Selenium.Support.Extensions;
using Xunit;
using Xunit.Abstractions;
namespace Microsoft.AspNetCore.Components.E2ETest.Tests
{
public class VirtualizationTest : ServerTestBase<ToggleExecutionModeServerFixture<Program>>
{
public VirtualizationTest(
BrowserFixture browserFixture,
ToggleExecutionModeServerFixture<Program> serverFixture,
ITestOutputHelper output)
: base(browserFixture, serverFixture, output)
{
}
protected override void InitializeAsyncCore()
{
Navigate(ServerPathBase, noReload: _serverFixture.ExecutionMode == ExecutionMode.Client);
}
[Fact]
public void AlwaysFillsVisibleCapacity_Sync()
{
Browser.MountTestComponent<VirtualizationComponent>();
var topSpacer = Browser.Exists(By.Id("sync-container")).FindElement(By.TagName("div"));
var expectedInitialSpacerStyle = "height: 0px;";
int initialItemCount = 0;
// Wait until items have been rendered.
Browser.True(() => (initialItemCount = GetItemCount()) > 0);
Browser.Equal(expectedInitialSpacerStyle, () => topSpacer.GetAttribute("style"));
// Scroll halfway.
Browser.ExecuteJavaScript("const container = document.getElementById('sync-container');container.scrollTop = container.scrollHeight * 0.5;");
// Validate that we get the same item count after scrolling halfway.
Browser.Equal(initialItemCount, GetItemCount);
Browser.NotEqual(expectedInitialSpacerStyle, () => topSpacer.GetAttribute("style"));
// Scroll to the bottom.
Browser.ExecuteJavaScript("const container = document.getElementById('sync-container');container.scrollTop = container.scrollHeight;");
// Validate that we get the same item count after scrolling to the bottom.
Browser.Equal(initialItemCount, GetItemCount);
Browser.NotEqual(expectedInitialSpacerStyle, () => topSpacer.GetAttribute("style"));
int GetItemCount() => Browser.FindElements(By.Id("sync-item")).Count;
}
[Fact]
public void AlwaysFillsVisibleCapacity_Async()
{
Browser.MountTestComponent<VirtualizationComponent>();
var finishLoadingButton = Browser.Exists(By.Id("finish-loading-button"));
// Check that no items or placeholders are visible.
// No data fetches have happened so we don't know how many items there are.
Browser.Equal(0, GetItemCount);
Browser.Equal(0, GetPlaceholderCount);
// Load the initial set of items.
finishLoadingButton.Click();
var initialItemCount = 0;
// Validate that items appear and placeholders aren't rendered.
Browser.True(() => (initialItemCount = GetItemCount()) > 0);
Browser.Equal(0, GetPlaceholderCount);
// Scroll halfway.
Browser.ExecuteJavaScript("const container = document.getElementById('async-container');container.scrollTop = container.scrollHeight * 0.5;");
// Validate that items are replaced by the same number of placeholders.
Browser.Equal(0, GetItemCount);
Browser.Equal(initialItemCount, GetPlaceholderCount);
// Load the new set of items.
finishLoadingButton.Click();
// Validate that the placeholders are replaced by the same number of items.
Browser.Equal(initialItemCount, GetItemCount);
Browser.Equal(0, GetPlaceholderCount);
// Scroll to the bottom.
Browser.ExecuteJavaScript("const container = document.getElementById('async-container');container.scrollTop = container.scrollHeight;");
// Validate that items are replaced by the same number of placeholders.
Browser.Equal(0, GetItemCount);
Browser.Equal(initialItemCount, GetPlaceholderCount);
// Load the new set of items.
finishLoadingButton.Click();
// Validate that the placeholders are replaced by the same number of items.
Browser.Equal(initialItemCount, GetItemCount);
Browser.Equal(0, GetPlaceholderCount);
int GetItemCount() => Browser.FindElements(By.Id("async-item")).Count;
int GetPlaceholderCount() => Browser.FindElements(By.Id("async-placeholder")).Count;
}
[Fact]
public void RerendersWhenItemSizeShrinks_Sync()
{
Browser.MountTestComponent<VirtualizationComponent>();
int initialItemCount = 0;
// Wait until items have been rendered.
Browser.True(() => (initialItemCount = GetItemCount()) > 0);
var itemSizeInput = Browser.Exists(By.Id("item-size-input"));
// Change the item size.
itemSizeInput.SendKeys("\b\b\b10\n");
// Validate that the list has been re-rendered to show more items.
Browser.True(() => GetItemCount() > initialItemCount);
int GetItemCount() => Browser.FindElements(By.Id("sync-item")).Count;
}
[Fact]
public void RerendersWhenItemSizeShrinks_Async()
{
Browser.MountTestComponent<VirtualizationComponent>();
var finishLoadingButton = Browser.Exists(By.Id("finish-loading-button"));
// Load the initial set of items.
finishLoadingButton.Click();
int initialItemCount = 0;
// Validate that items appear and placeholders aren't rendered.
Browser.True(() => (initialItemCount = GetItemCount()) > 0);
Browser.Equal(0, GetPlaceholderCount);
var itemSizeInput = Browser.Exists(By.Id("item-size-input"));
// Change the item size.
itemSizeInput.SendKeys("\b\b\b10\n");
// Validate that the same number of loaded items is rendered.
Browser.Equal(initialItemCount, GetItemCount);
Browser.True(() => GetPlaceholderCount() > 0);
// Load the new set of items.
finishLoadingButton.Click();
// Validate that the placeholders have been replaced with more loaded items.
Browser.True(() => GetItemCount() > initialItemCount);
Browser.Equal(0, GetPlaceholderCount);
int GetItemCount() => Browser.FindElements(By.Id("async-item")).Count;
int GetPlaceholderCount() => Browser.FindElements(By.Id("async-placeholder")).Count;
}
[Fact]
[QuarantinedTest("https://github.com/dotnet/aspnetcore/issues/27227")]
public void CancelsOutdatedRefreshes_Async()
{
Browser.MountTestComponent<VirtualizationComponent>();
var cancellationCount = Browser.Exists(By.Id("cancellation-count"));
var finishLoadingButton = Browser.Exists(By.Id("finish-loading-button"));
// Load the initial set of items.
finishLoadingButton.Click();
// Validate that there are no initial cancellations.
Browser.Equal("0", () => cancellationCount.Text);
// Validate that there is no initial fetch to cancel.
Browser.ExecuteJavaScript("const container = document.getElementById('async-container');container.scrollTop = 1000;");
Browser.Equal("0", () => cancellationCount.Text);
// Validate that scrolling again cancels the first fetch.
Browser.ExecuteJavaScript("const container = document.getElementById('async-container');container.scrollTop = 2000;");
Browser.Equal("1", () => cancellationCount.Text);
// Validate that scrolling again cancels the second fetch.
Browser.ExecuteJavaScript("const container = document.getElementById('async-container');container.scrollTop = 3000;");
Browser.Equal("2", () => cancellationCount.Text);
}
[Fact]
[QuarantinedTest("https://github.com/dotnet/aspnetcore/issues/24922")]
public void CanUseViewportAsContainer()
{
Browser.MountTestComponent<VirtualizationComponent>();
var expectedInitialSpacerStyle = "height: 0px;";
var topSpacer = Browser.Exists(By.Id("viewport-as-root")).FindElement(By.TagName("div"));
Browser.ExecuteJavaScript("const element = document.getElementById('viewport-as-root'); element.scrollIntoView();");
// Validate that the top spacer has a height of zero.
Browser.Equal(expectedInitialSpacerStyle, () => topSpacer.GetAttribute("style"));
Browser.ExecuteJavaScript("window.scrollTo(0, document.body.scrollHeight);");
// Validate that the top spacer has expanded.
Browser.NotEqual(expectedInitialSpacerStyle, () => topSpacer.GetAttribute("style"));
}
[Fact]
public async Task ToleratesIncorrectItemSize()
{
Browser.MountTestComponent<VirtualizationComponent>();
var topSpacer = Browser.Exists(By.Id("incorrect-size-container")).FindElement(By.TagName("div"));
var expectedInitialSpacerStyle = "height: 0px;";
// Wait until items have been rendered.
Browser.True(() => GetItemCount() > 0);
Browser.Equal(expectedInitialSpacerStyle, () => topSpacer.GetAttribute("style"));
// Scroll slowly, in increments of 50px at a time. At one point this would trigger a bug
// due to the incorrect item size, whereby it would not realise it's necessary to show more
// items because the first time the spacer became visible, the size calculation said that
// we're already showing all the items we need to show.
for (var pos = 0; pos < 1000; pos += 50)
{
Browser.ExecuteJavaScript($"document.getElementById('incorrect-size-container').scrollTop = {pos};");
await Task.Delay(200);
}
// Validate that the top spacer did change
Browser.NotEqual(expectedInitialSpacerStyle, () => topSpacer.GetAttribute("style"));
int GetItemCount() => Browser.FindElements(By.ClassName("incorrect-size-item")).Count;
}
[Fact]
public void CanMutateDataInPlace_Sync()
{
Browser.MountTestComponent<VirtualizationDataChanges>();
// Initial data
var container = Browser.Exists(By.Id("using-items"));
Browser.Collection(() => GetPeopleNames(container),
name => Assert.Equal("Person 1", name),
name => Assert.Equal("Person 2", name),
name => Assert.Equal("Person 3", name));
// Mutate one of them
var itemToMutate = container.FindElements(By.ClassName("person"))[1];
itemToMutate.FindElement(By.TagName("button")).Click();
// See changes
Browser.Collection(() => GetPeopleNames(container),
name => Assert.Equal("Person 1", name),
name => Assert.Equal("Person 2 MUTATED", name),
name => Assert.Equal("Person 3", name));
}
[Fact]
public void CanMutateDataInPlace_Async()
{
Browser.MountTestComponent<VirtualizationDataChanges>();
// Initial data
var container = Browser.Exists(By.Id("using-itemsprovider"));
Browser.Collection(() => GetPeopleNames(container),
name => Assert.Equal("Person 1", name),
name => Assert.Equal("Person 2", name),
name => Assert.Equal("Person 3", name));
// Mutate one of them
var itemToMutate = container.FindElements(By.ClassName("person"))[1];
itemToMutate.FindElement(By.TagName("button")).Click();
// See changes
Browser.Collection(() => GetPeopleNames(container),
name => Assert.Equal("Person 1", name),
name => Assert.Equal("Person 2 MUTATED", name),
name => Assert.Equal("Person 3", name));
}
[Fact]
public void CanChangeDataCount_Sync()
{
Browser.MountTestComponent<VirtualizationDataChanges>();
// Initial data
var container = Browser.Exists(By.Id("using-items"));
Browser.Collection(() => GetPeopleNames(container),
name => Assert.Equal("Person 1", name),
name => Assert.Equal("Person 2", name),
name => Assert.Equal("Person 3", name));
// Add another item
Browser.Exists(By.Id("add-person-to-fixed-list")).Click();
// See changes
Browser.Collection(() => GetPeopleNames(container),
name => Assert.Equal("Person 1", name),
name => Assert.Equal("Person 2", name),
name => Assert.Equal("Person 3", name),
name => Assert.Equal("Person 4", name));
}
[Fact]
public void CanChangeDataCount_Async()
{
Browser.MountTestComponent<VirtualizationDataChanges>();
// Initial data
var container = Browser.Exists(By.Id("using-itemsprovider"));
Browser.Collection(() => GetPeopleNames(container),
name => Assert.Equal("Person 1", name),
name => Assert.Equal("Person 2", name),
name => Assert.Equal("Person 3", name));
// Add another item
Browser.Exists(By.Id("add-person-to-itemsprovider")).Click();
// Initially this has no effect because we don't re-query the provider until told to do so
Browser.Collection(() => GetPeopleNames(container),
name => Assert.Equal("Person 1", name),
name => Assert.Equal("Person 2", name),
name => Assert.Equal("Person 3", name));
// Request refresh
Browser.Exists(By.Id("refresh-itemsprovider")).Click();
// See changes
Browser.Collection(() => GetPeopleNames(container),
name => Assert.Equal("Person 1", name),
name => Assert.Equal("Person 2", name),
name => Assert.Equal("Person 3", name),
name => Assert.Equal("Person 4", name));
}
[Fact]
public void CanRefreshItemsProviderResultsInPlace()
{
Browser.MountTestComponent<VirtualizationDataChanges>();
// Mutate the data
var container = Browser.Exists(By.Id("using-itemsprovider"));
var itemToMutate = container.FindElements(By.ClassName("person"))[1];
itemToMutate.FindElement(By.TagName("button")).Click();
// Verify the mutation was applied
Browser.Collection(() => GetPeopleNames(container),
name => Assert.Equal("Person 1", name),
name => Assert.Equal("Person 2 MUTATED", name),
name => Assert.Equal("Person 3", name));
// Refresh and verify the mutation was reverted
Browser.Exists(By.Id("refresh-itemsprovider")).Click();
Browser.Collection(() => GetPeopleNames(container),
name => Assert.Equal("Person 1", name),
name => Assert.Equal("Person 2", name),
name => Assert.Equal("Person 3", name));
}
private string[] GetPeopleNames(IWebElement container)
{
var peopleElements = container.FindElements(By.CssSelector(".person span"));
return peopleElements.Select(element => element.Text).ToArray();
}
}
}
|