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
content / browser / loader / background_resource_fetch_browsertests.cc [blame]
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "content/browser/web_contents/web_contents_impl.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/content_browser_test.h"
#include "content/public/test/content_browser_test_utils.h"
#include "content/shell/browser/shell.h"
#include "content/test/content_browser_test_utils_internal.h"
#include "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/controllable_http_response.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/common/loader/background_resource_fetch_histograms.h"
namespace content {
namespace {
using testing::ElementsAre;
// TODO(crbug.com/40244488): When we will add VirtualTestSuites of web tests for
// BackgroundResourceFetch feature, duplicated basic tests should be deleted
// from BackgroundResourceFetchBrowserTest.
class BackgroundResourceFetchBrowserTest : public ContentBrowserTest {
public:
BackgroundResourceFetchBrowserTest() {
scoped_feature_list_.InitWithFeatures(
/*enabled_features=*/
{blink::features::kBackgroundResourceFetch,
// Needed to trigger cache-aware loading
blink::features::kWebFontsCacheAwareTimeoutAdaption},
/*disabled_features=*/{});
}
BackgroundResourceFetchBrowserTest(
const BackgroundResourceFetchBrowserTest&) = delete;
BackgroundResourceFetchBrowserTest& operator=(
const BackgroundResourceFetchBrowserTest&) = delete;
~BackgroundResourceFetchBrowserTest() override = default;
protected:
void SetUpOnMainThread() override {
host_resolver()->AddRule("*", "127.0.0.1");
}
WebContentsImpl* web_contents() const {
return static_cast<WebContentsImpl*>(shell()->web_contents());
}
RenderFrameHostImpl* current_frame_host() {
return web_contents()->GetPrimaryFrameTree().root()->current_frame_host();
}
void StartServerAndNavigateToTestPage() {
ASSERT_TRUE(embedded_test_server()->Start());
EXPECT_TRUE(
NavigateToURL(shell(), embedded_test_server()->GetURL("/hello.html")));
}
void TestScriptLoad(const std::string& path, bool expect_success) {
const std::string script = JsReplace(R"(
new Promise(resolve => {
window.resolveTest = resolve;
const script = document.createElement('script');
script.src = $1;
script.addEventListener('error', () => {resolve('load error')});
document.body.appendChild(script);
});
)",
path);
EXPECT_EQ(expect_success ? "script executed" : "load error",
EvalJs(shell(), script));
}
void TestFontLoad(const std::string& path, bool expect_success) {
const std::string script = JsReplace(R"(
new Promise(resolve => {
const fontface =
new FontFace('CustomFont', $1);
document.fonts.add(fontface);
fontface.load()
.then(() => {resolve('font loaded');})
.catch(() => {resolve('load error');});
});
)",
path);
EXPECT_EQ(expect_success ? "font loaded" : "load error",
EvalJs(shell(), script));
}
private:
base::test::ScopedFeatureList scoped_feature_list_;
};
IN_PROC_BROWSER_TEST_F(BackgroundResourceFetchBrowserTest, ScriptLoad) {
StartServerAndNavigateToTestPage();
base::HistogramTester histograms;
// Test normal script load behavior.
TestScriptLoad("background_resource_fetch/test.js", /*expect_success=*/true);
FetchHistogramsFromChildProcesses();
EXPECT_THAT(histograms.GetAllSamples(
blink::kBackgroundResourceFetchSupportStatusHistogramName),
ElementsAre(base::Bucket(
blink::BackgroundResourceFetchSupportStatus::kSupported, 1)));
}
IN_PROC_BROWSER_TEST_F(BackgroundResourceFetchBrowserTest, ScriptLoadFailure) {
StartServerAndNavigateToTestPage();
// Test script load failure.
TestScriptLoad("background_resource_fetch/404.js", /*expect_success=*/false);
}
IN_PROC_BROWSER_TEST_F(BackgroundResourceFetchBrowserTest,
ScriptLoadFailureAfterResponse) {
StartServerAndNavigateToTestPage();
// Test script load failure behavior after receiving a response header.
TestScriptLoad("background_resource_fetch/invalid-chunked-encoding.js",
/*expect_success=*/false);
}
IN_PROC_BROWSER_TEST_F(BackgroundResourceFetchBrowserTest, ScriptLoadTwice) {
StartServerAndNavigateToTestPage();
base::HistogramTester histograms;
// Test normal script load behavior.
TestScriptLoad("background_resource_fetch/test.js?1",
/*expect_success=*/true);
// Test normal script load behavior again.
TestScriptLoad("background_resource_fetch/test.js?2",
/*expect_success=*/true);
FetchHistogramsFromChildProcesses();
EXPECT_THAT(histograms.GetAllSamples(
blink::kBackgroundResourceFetchSupportStatusHistogramName),
ElementsAre(base::Bucket(
blink::BackgroundResourceFetchSupportStatus::kSupported, 2)));
}
IN_PROC_BROWSER_TEST_F(BackgroundResourceFetchBrowserTest, ScriptLoadStop) {
StartServerAndNavigateToTestPage();
// window.stop() should cause script load error.
EXPECT_EQ("load error", EvalJs(shell(), R"(
new Promise(resolve => {
window.resolveTest = resolve;
const script = document.createElement('script');
script.src = 'background_resource_fetch/test.js';
script.addEventListener('error', () => {resolve('load error')});
document.body.appendChild(script);
window.stop();
});
)"));
}
IN_PROC_BROWSER_TEST_F(BackgroundResourceFetchBrowserTest,
NavigationWhileLoadingScript) {
StartServerAndNavigateToTestPage();
EXPECT_TRUE(ExecJs(shell(), R"(
(() => {
const script = document.createElement('script');
script.src = 'slow?10';
document.body.appendChild(script);
})();
)"));
GURL new_url(embedded_test_server()->GetURL("/hello.html?2"));
EXPECT_TRUE(ExecJs(shell(), JsReplace("location.replace($1)", new_url)));
EXPECT_TRUE(WaitForLoadStop(shell()->web_contents()));
}
IN_PROC_BROWSER_TEST_F(BackgroundResourceFetchBrowserTest,
UnupportedSyncRequest) {
StartServerAndNavigateToTestPage();
base::HistogramTester histograms;
// Fetch empty.html using sync XMLHttpRequest.
EXPECT_EQ(200, EvalJs(shell(), R"(
(() => {
const request = new XMLHttpRequest();
request.open('GET', '/hello.html', false);
request.send(null);
return request.status;
})();
)"));
FetchHistogramsFromChildProcesses();
// Sync XMLHttpRequest is not supported. So the kUnsupportedSyncRequest UMA
// must have been recorded.
EXPECT_THAT(
histograms.GetAllSamples(
blink::kBackgroundResourceFetchSupportStatusHistogramName),
ElementsAre(base::Bucket(
blink::BackgroundResourceFetchSupportStatus::kUnsupportedSyncRequest,
1)));
}
IN_PROC_BROWSER_TEST_F(BackgroundResourceFetchBrowserTest,
UnupportedNonGetRequest) {
StartServerAndNavigateToTestPage();
base::HistogramTester histograms;
// Fetch empty.html using POST method.
EXPECT_EQ("", EvalJs(shell(), R"(
new Promise(async (resolve) => {
const res = await fetch('empty.html',
{method: 'POST', body: 'body'});
resolve(await res.text());
});
)"));
FetchHistogramsFromChildProcesses();
// POST method is not supported. So the kUnsupportedNonGetRequest UMA must
// have been recorded.
EXPECT_THAT(
histograms.GetAllSamples(
blink::kBackgroundResourceFetchSupportStatusHistogramName),
ElementsAre(base::Bucket(blink::BackgroundResourceFetchSupportStatus::
kUnsupportedNonGetRequest,
1)));
}
IN_PROC_BROWSER_TEST_F(BackgroundResourceFetchBrowserTest,
UnupportedNonHttpUrlRequest) {
StartServerAndNavigateToTestPage();
base::HistogramTester histograms;
// Fetch chrome-untrusted://example.com/.
EXPECT_EQ("failed", EvalJs(shell(), R"(
new Promise(async (resolve) => {
try {
await fetch('chrome-untrusted://example.com/');
} catch (e) {
resolve('failed');
}
});
)"));
FetchHistogramsFromChildProcesses();
// Non HTTP url request not supported. So the UnupportedNonHttpUrlRequest UMA
// must have been recorded.
EXPECT_THAT(
histograms.GetAllSamples(
blink::kBackgroundResourceFetchSupportStatusHistogramName),
ElementsAre(base::Bucket(blink::BackgroundResourceFetchSupportStatus::
kUnsupportedNonHttpUrlRequest,
1)));
}
IN_PROC_BROWSER_TEST_F(BackgroundResourceFetchBrowserTest,
UnupportedKeepAliveRequest) {
StartServerAndNavigateToTestPage();
base::HistogramTester histograms;
// Fetch empty.html with {keepalive: true}.
EXPECT_EQ("", EvalJs(shell(), R"(
new Promise(async (resolve) => {
const res = await fetch('empty.html', {keepalive: true});
resolve(await res.text());
});
)"));
FetchHistogramsFromChildProcesses();
// POST method is not supported. So the UnupportedKeepAliveRequest UMA must
// have been recorded.
EXPECT_THAT(
histograms.GetAllSamples(
blink::kBackgroundResourceFetchSupportStatusHistogramName),
ElementsAre(base::Bucket(blink::BackgroundResourceFetchSupportStatus::
kUnsupportedKeepAliveRequest,
1)));
}
IN_PROC_BROWSER_TEST_F(BackgroundResourceFetchBrowserTest, FontLoad) {
StartServerAndNavigateToTestPage();
// Test font loading behavior that triggers cache-aware loading when
// WebFontsCacheAwareTimeoutAdaption feature is enabled.
TestFontLoad("url(background_resource_fetch/SpaceOnly.otf)",
/*expect_success=*/true);
}
IN_PROC_BROWSER_TEST_F(BackgroundResourceFetchBrowserTest, FontLoadFailure) {
StartServerAndNavigateToTestPage();
// Test font loading failure behavior that triggers cache-aware loading when
// WebFontsCacheAwareTimeoutAdaption feature is enabled.
TestFontLoad("url(background_resource_fetch/not_found.otf)",
/*expect_success=*/false);
}
IN_PROC_BROWSER_TEST_F(BackgroundResourceFetchBrowserTest,
ScriptLoadAfterCrossOriginSameSiteNavigation) {
StartServerAndNavigateToTestPage();
GURL url(embedded_test_server()->GetURL("example.com", "/hello.html"));
EXPECT_TRUE(NavigateToURL(shell(), url));
// Test normal script load behavior.
TestScriptLoad("background_resource_fetch/test.js", /*expect_success=*/true);
// Navigate to a cross origin same site page. This is intended to check that
// `background_resource_fetch_context_` which is attached to the
// RenderFrameImpl is replaced. If `background_resource_fetch_context_` is not
// replaced, the network request after the navigation will cause the initiator
// lock compatibility check failure in the network process.
GURL new_url(
embedded_test_server()->GetURL("sub.example.com", "/hello.html"));
EXPECT_TRUE(ExecJs(shell(), JsReplace("location.replace($1)", new_url)));
EXPECT_TRUE(WaitForLoadStop(shell()->web_contents()));
// Test normal script load behavior after navigation.
TestScriptLoad("background_resource_fetch/test.js", /*expect_success=*/true);
}
IN_PROC_BROWSER_TEST_F(BackgroundResourceFetchBrowserTest, Redirect) {
StartServerAndNavigateToTestPage();
EXPECT_EQ("Redirected", EvalJs(shell(), R"(
new Promise(async (resolve) => {
const res = await fetch('/background_resource_fetch/redirect');
resolve(await res.text());
});
)"));
}
IN_PROC_BROWSER_TEST_F(BackgroundResourceFetchBrowserTest, RedirectRejected) {
StartServerAndNavigateToTestPage();
EXPECT_EQ("fetch failed", EvalJs(shell(), R"(
new Promise(async (resolve) => {
try {
await fetch('/background_resource_fetch/redirect',
{redirect: 'error'});
} catch (e) {
resolve('fetch failed')
}
});
)"));
}
} // namespace
} // namespace content