forked from agentic-review-benchmarks/aspnetcore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStatePersistenceTest.cs
More file actions
423 lines (381 loc) · 18.4 KB
/
StatePersistenceTest.cs
File metadata and controls
423 lines (381 loc) · 18.4 KB
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Components.TestServer.RazorComponents;
using Microsoft.AspNetCore.Components.E2ETest.Infrastructure;
using Microsoft.AspNetCore.Components.E2ETest.Infrastructure.ServerFixtures;
using Microsoft.AspNetCore.Components.E2ETests.ServerRenderingTests;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.E2ETesting;
using OpenQA.Selenium;
using TestServer;
using Xunit.Abstractions;
namespace Microsoft.AspNetCore.Components.E2ETests.Tests;
// These tests are for Blazor Web implementation
// For Blazor Server and Webassembly, check SaveStateTest.cs
public class StatePersistenceTest : ServerTestBase<BasicTestAppServerSiteFixture<RazorComponentEndpointsStartup<App>>>
{
static int _nextStreamingIdContext;
public StatePersistenceTest(
BrowserFixture browserFixture,
BasicTestAppServerSiteFixture<RazorComponentEndpointsStartup<App>> serverFixture,
ITestOutputHelper output)
: base(browserFixture, serverFixture, output)
{
}
// Separate contexts to ensure that caches and other state don't interfere across tests.
public override Task InitializeAsync()
=> InitializeAsync(BrowserFixture.StreamingContext + _nextStreamingIdContext++);
// Validates that we can use persisted state across server, webassembly, and auto modes, with and without
// streaming rendering.
// For streaming rendering, we validate that the state is captured and restored after streaming completes.
// For enhanced navigation we validate that the state is captured at the time components are rendered for
// the first time on the page.
// For auto mode, we validate that the state is captured and restored for both server and wasm runtimes.
// In each case, we validate that the state is available until the initial set of components first render reaches quiescence. Similar to how it works for Server and WebAssembly.
// For server we validate that the state is provided every time a circuit is initialized.
[Theory]
[InlineData(true, typeof(InteractiveServerRenderMode), (string)null)]
[InlineData(true, typeof(InteractiveServerRenderMode), "ServerStreaming")]
[InlineData(true, typeof(InteractiveWebAssemblyRenderMode), (string)null)]
[InlineData(true, typeof(InteractiveWebAssemblyRenderMode), "WebAssemblyStreaming")]
[InlineData(true, typeof(InteractiveAutoRenderMode), (string)null)]
[InlineData(true, typeof(InteractiveAutoRenderMode), "AutoStreaming")]
[InlineData(false, typeof(InteractiveServerRenderMode), (string)null)]
[InlineData(false, typeof(InteractiveServerRenderMode), "ServerStreaming")]
[InlineData(false, typeof(InteractiveWebAssemblyRenderMode), (string)null)]
[InlineData(false, typeof(InteractiveWebAssemblyRenderMode), "WebAssemblyStreaming")]
[InlineData(false, typeof(InteractiveAutoRenderMode), (string)null)]
[InlineData(false, typeof(InteractiveAutoRenderMode), "AutoStreaming")]
public void CanRenderComponentWithPersistedState(bool suppressEnhancedNavigation, Type renderMode, string streaming)
{
var mode = renderMode switch
{
var t when t == typeof(InteractiveServerRenderMode) => "server",
var t when t == typeof(InteractiveWebAssemblyRenderMode) => "wasm",
var t when t == typeof(InteractiveAutoRenderMode) => "auto",
_ => throw new ArgumentException($"Unknown render mode: {renderMode.Name}")
};
if (!suppressEnhancedNavigation)
{
// Navigate to a page without components first to make sure that we exercise rendering components
// with enhanced navigation on.
if (streaming == null)
{
Navigate($"subdir/persistent-state/page-no-components?render-mode={mode}&suppress-autostart");
}
else
{
Navigate($"subdir/persistent-state/page-no-components?render-mode={mode}&streaming-id={streaming}&suppress-autostart");
}
if (mode == "auto")
{
BlockWebAssemblyResourceLoad();
}
Browser.Click(By.Id("call-blazor-start"));
Browser.Click(By.Id("page-with-components-link"));
}
else
{
EnhancedNavigationTestUtil.SuppressEnhancedNavigation(this, true);
if (mode == "auto")
{
BlockWebAssemblyResourceLoad();
}
}
if (mode != "auto")
{
RenderComponentsWithPersistentStateAndValidate(suppressEnhancedNavigation, mode, renderMode, streaming);
}
else
{
// For auto mode, validate that the state is persisted for both runtimes and is able
// to be loaded on server and wasm.
RenderComponentsWithPersistentStateAndValidate(suppressEnhancedNavigation, mode, renderMode, streaming, interactiveRuntime: "server");
UnblockWebAssemblyResourceLoad();
Browser.Navigate().Refresh();
RenderComponentsWithPersistentStateAndValidate(suppressEnhancedNavigation, mode, renderMode, streaming, interactiveRuntime: "wasm");
}
}
// Validates that we can use persisted state across server, webassembly, and auto modes, with and without
// streaming rendering.
// For streaming rendering, we validate that the state is captured and restored after streaming completes.
// For enhanced navigation we validate that the state is captured at the time components are rendered for
// the first time on the page.
// For auto mode, we validate that the state is captured and restored for both server and wasm runtimes.
// In each case, we validate that the state is available until the initial set of components first render reaches quiescence. Similar to how it works for Server and WebAssembly.
// For server we validate that the state is provided every time a circuit is initialized.
[Theory]
[InlineData(typeof(InteractiveServerRenderMode), (string)null, "yes")]
[InlineData(typeof(InteractiveServerRenderMode), "ServerStreaming", "yes")]
[InlineData(typeof(InteractiveWebAssemblyRenderMode), (string)null, "yes")]
[InlineData(typeof(InteractiveWebAssemblyRenderMode), "WebAssemblyStreaming", "yes")]
[InlineData(typeof(InteractiveAutoRenderMode), (string)null, "yes")]
[InlineData(typeof(InteractiveAutoRenderMode), "AutoStreaming", "yes")]
[InlineData(typeof(InteractiveServerRenderMode), (string)null, null)]
[InlineData(typeof(InteractiveServerRenderMode), "ServerStreaming", null)]
[InlineData(typeof(InteractiveWebAssemblyRenderMode), (string)null, null)]
[InlineData(typeof(InteractiveWebAssemblyRenderMode), "WebAssemblyStreaming", null)]
[InlineData(typeof(InteractiveAutoRenderMode), (string)null, null)]
[InlineData(typeof(InteractiveAutoRenderMode), "AutoStreaming", null)]
public void CanUpdateComponentsWithPersistedStateAndEnhancedNavUpdates(
Type renderMode,
string streaming,
string key)
{
var mode = renderMode switch
{
var t when t == typeof(InteractiveServerRenderMode) => "server",
var t when t == typeof(InteractiveWebAssemblyRenderMode) => "wasm",
var t when t == typeof(InteractiveAutoRenderMode) => "auto",
_ => throw new ArgumentException($"Unknown render mode: {renderMode.Name}")
};
// Navigate to a page without components first to make sure that we exercise rendering components
// with enhanced navigation on.
NavigateToInitialPage(streaming, mode, key);
if (mode == "auto")
{
BlockWebAssemblyResourceLoad();
}
Browser.Click(By.Id("call-blazor-start"));
Browser.Click(By.Id("page-with-components-link-and-declarative-state"));
if (mode != "auto")
{
RenderComponentsWithDeclarativePersistentStateAndValidate(mode, renderMode, streaming, stateValue: "other");
}
else
{
// For auto mode, validate that the state is persisted for both runtimes and is able
// to be loaded on server and wasm.
RenderComponentsWithDeclarativePersistentStateAndValidate(mode, renderMode, streaming, interactiveRuntime: "server", stateValue: "other");
UnblockWebAssemblyResourceLoad();
Browser.Navigate().Refresh();
NavigateToInitialPage(streaming, mode, key);
Browser.Click(By.Id("call-blazor-start"));
Browser.Click(By.Id("page-with-components-link-and-declarative-state"));
RenderComponentsWithDeclarativePersistentStateAndValidate(mode, renderMode, streaming, interactiveRuntime: "wasm", stateValue: "other");
}
void NavigateToInitialPage(string streaming, string mode, string key)
{
if (key == null)
{
if (streaming == null)
{
Navigate($"subdir/persistent-state/page-no-components?render-mode={mode}&suppress-autostart");
}
else
{
Navigate($"subdir/persistent-state/page-no-components?render-mode={mode}&streaming-id={streaming}&suppress-autostart");
}
}
else
{
if (streaming == null)
{
Navigate($"subdir/persistent-state/page-no-components?render-mode={mode}&key={key}&suppress-autostart");
}
else
{
Navigate($"subdir/persistent-state/page-no-components?render-mode={mode}&key={key}&streaming-id={streaming}&suppress-autostart");
}
}
}
}
private void RenderComponentsWithDeclarativePersistentStateAndValidate(
string mode,
Type renderMode,
string streaming,
string interactiveRuntime = null,
string stateValue = "restored")
{
AssertDeclarativePageState(
mode: mode,
renderMode: renderMode.Name,
interactive: streaming == null,
stateValue: stateValue,
streamingId: streaming,
streamingCompleted: false,
interactiveRuntime: interactiveRuntime);
if (streaming == null)
{
Browser.Click(By.Id("enhanced-nav-update"));
AssertDeclarativePageState(
mode: mode,
renderMode: renderMode.Name,
interactive: streaming == null,
stateValue: "updated",
streamingId: streaming,
streamingCompleted: false,
interactiveRuntime: interactiveRuntime);
return;
}
Browser.Click(By.Id("end-streaming"));
AssertDeclarativePageState(
mode: mode,
renderMode: renderMode.Name,
interactive: true,
stateValue: stateValue,
streamingId: streaming,
streamingCompleted: true,
interactiveRuntime: interactiveRuntime);
Browser.Click(By.Id("enhanced-nav-update"));
Browser.Click(By.Id("end-streaming"));
AssertDeclarativePageState(
mode: mode,
renderMode: renderMode.Name,
interactive: true,
stateValue: "updated",
streamingId: streaming,
streamingCompleted: true,
interactiveRuntime: interactiveRuntime);
return;
}
[Theory]
[InlineData((string)null)]
[InlineData("ServerStreaming")]
public async Task StateIsProvidedEveryTimeACircuitGetsCreated(string streaming)
{
var mode = "server";
if (streaming == null)
{
Navigate($"subdir/persistent-state/page-no-components?render-mode={mode}");
}
else
{
Navigate($"subdir/persistent-state/page-no-components?render-mode={mode}&streaming-id={streaming}");
}
Browser.Click(By.Id("page-with-components-link"));
RenderComponentsWithPersistentStateAndValidate(suppressEnhancedNavigation: false, mode, typeof(InteractiveServerRenderMode), streaming);
Browser.Click(By.Id("page-no-components-link"));
// Ensure that the circuit is gone.
await Task.Delay(1000);
Browser.Click(By.Id("page-with-components-link-and-state"));
RenderComponentsWithPersistentStateAndValidate(suppressEnhancedNavigation: false, mode, typeof(InteractiveServerRenderMode), streaming, stateValue: "other");
}
[Theory]
[InlineData("ServerNonPrerendered")]
[InlineData("WebAssemblyNonPrerendered")]
public void PersistentStateIsSupportedInDynamicJSRoots(string renderMode)
{
Navigate($"subdir/WasmMinimal/dynamic-js-root.html?renderMode={renderMode}");
Browser.Equal("Counter", () => Browser.Exists(By.TagName("h1")).Text);
Browser.Equal("Current count: 0", () => Browser.Exists(By.CssSelector("p[role='status']")).Text);
Browser.Click(By.CssSelector("button.btn-primary"));
Browser.Equal("Current count: 1", () => Browser.Exists(By.CssSelector("p[role='status']")).Text);
}
private void BlockWebAssemblyResourceLoad()
{
// Clear local storage so that the resource hash is not found
((IJavaScriptExecutor)Browser).ExecuteScript("localStorage.clear()");
((IJavaScriptExecutor)Browser).ExecuteScript("sessionStorage.setItem('block-load-boot-resource', 'true')");
// Clear caches so that we can block the resource load
((IJavaScriptExecutor)Browser).ExecuteScript("caches.keys().then(keys => keys.forEach(key => caches.delete(key)))");
}
private void UnblockWebAssemblyResourceLoad()
{
((IJavaScriptExecutor)Browser).ExecuteScript("window.unblockLoadBootResource()");
Browser.Exists(By.Id("unblocked-wasm"));
}
private void RenderComponentsWithPersistentStateAndValidate(
bool suppressEnhancedNavigation,
string mode,
Type renderMode,
string streaming,
string interactiveRuntime = null,
string stateValue = "restored")
{
// No need to navigate if we are using enhanced navigation, the tests will have already navigated to the page via a link.
if (suppressEnhancedNavigation)
{
// In this case we suppress auto start to check some server side state before we boot Blazor.
if (streaming == null)
{
Navigate($"subdir/persistent-state/page-with-components?render-mode={mode}&suppress-autostart");
}
else
{
Navigate($"subdir/persistent-state/page-with-components?render-mode={mode}&streaming-id={streaming}&suppress-autostart");
}
AssertPageState(
mode: mode,
renderMode: renderMode.Name,
interactive: false,
stateFound: true,
stateValue: stateValue,
streamingId: streaming,
streamingCompleted: false,
interactiveRuntime: interactiveRuntime);
Browser.Click(By.Id("call-blazor-start"));
}
AssertPageState(
mode: mode,
renderMode: renderMode.Name,
interactive: streaming == null,
stateFound: true,
stateValue: stateValue,
streamingId: streaming,
streamingCompleted: false,
interactiveRuntime: interactiveRuntime);
if (streaming == null)
{
return;
}
Browser.Click(By.Id("end-streaming"));
AssertPageState(
mode: mode,
renderMode: renderMode.Name,
interactive: true,
stateFound: true,
stateValue: stateValue,
streamingId: streaming,
streamingCompleted: true,
interactiveRuntime: interactiveRuntime);
}
private void AssertPageState(
string mode,
string renderMode,
bool interactive,
bool stateFound,
string stateValue,
string streamingId = null,
bool streamingCompleted = false,
string interactiveRuntime = null)
{
Browser.Equal($"Render mode: {renderMode}", () => Browser.FindElement(By.Id("render-mode")).Text);
Browser.Equal($"Streaming id:{streamingId}", () => Browser.FindElement(By.Id("streaming-id")).Text);
Browser.Equal($"Interactive: {interactive}", () => Browser.FindElement(By.Id("interactive")).Text);
if (streamingId == null || streamingCompleted)
{
interactiveRuntime = !interactive ? "none" : mode == "server" || mode == "wasm" ? mode : (interactiveRuntime ?? throw new InvalidOperationException("Specify interactiveRuntime for auto mode"));
Browser.Equal($"Interactive runtime: {interactiveRuntime}", () => Browser.FindElement(By.Id("interactive-runtime")).Text);
Browser.Equal($"State found:{stateFound}", () => Browser.FindElement(By.Id("state-found")).Text);
Browser.Equal($"State value:{stateValue}", () => Browser.FindElement(By.Id("state-value")).Text);
}
else
{
Browser.Equal("Streaming: True", () => Browser.FindElement(By.Id("streaming")).Text);
}
}
private void AssertDeclarativePageState(
string mode,
string renderMode,
bool interactive,
string stateValue,
string streamingId = null,
bool streamingCompleted = false,
string interactiveRuntime = null)
{
Browser.Equal($"Render mode: {renderMode}", () => Browser.FindElement(By.Id("render-mode")).Text);
Browser.Equal($"Streaming id:{streamingId}", () => Browser.FindElement(By.Id("streaming-id")).Text);
Browser.Equal($"Interactive: {interactive}", () => Browser.FindElement(By.Id("interactive")).Text);
if (streamingId == null || streamingCompleted)
{
interactiveRuntime = !interactive ? "none" : mode == "server" || mode == "wasm" ? mode : (interactiveRuntime ?? throw new InvalidOperationException("Specify interactiveRuntime for auto mode"));
Browser.Equal($"Interactive runtime: {interactiveRuntime}", () => Browser.FindElement(By.Id("interactive-runtime")).Text);
Browser.Equal($"State value:{stateValue}", () => Browser.FindElement(By.Id("state-value")).Text);
}
else
{
Browser.Equal("Streaming: True", () => Browser.FindElement(By.Id("streaming")).Text);
}
}
}