Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(616)

Side by Side Diff: chrome/browser/chrome_service_worker_browsertest.cc

Issue 1775143003: Reland of Add browser tests for FetchEvent.request.mode/credentials in the service worker (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | chrome/chrome_tests.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // This file tests that Service Workers (a Content feature) work in the Chromium 5 // This file tests that Service Workers (a Content feature) work in the Chromium
6 // embedder. 6 // embedder.
7 7
8 #include "base/bind.h" 8 #include "base/bind.h"
9 #include "base/command_line.h"
9 #include "base/files/scoped_temp_dir.h" 10 #include "base/files/scoped_temp_dir.h"
10 #include "base/numerics/safe_conversions.h" 11 #include "base/numerics/safe_conversions.h"
11 #include "base/run_loop.h" 12 #include "base/run_loop.h"
13 #include "base/strings/stringprintf.h"
14 #include "base/strings/utf_string_conversions.h"
12 #include "chrome/browser/chrome_notification_types.h" 15 #include "chrome/browser/chrome_notification_types.h"
13 #include "chrome/browser/profiles/profile.h" 16 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/ui/browser.h" 17 #include "chrome/browser/ui/browser.h"
15 #include "chrome/browser/ui/browser_window.h" 18 #include "chrome/browser/ui/browser_window.h"
16 #include "chrome/browser/ui/tabs/tab_strip_model.h" 19 #include "chrome/browser/ui/tabs/tab_strip_model.h"
20 #include "chrome/common/chrome_switches.h"
17 #include "chrome/test/base/in_process_browser_test.h" 21 #include "chrome/test/base/in_process_browser_test.h"
18 #include "chrome/test/base/ui_test_utils.h" 22 #include "chrome/test/base/ui_test_utils.h"
19 #include "content/public/browser/browser_context.h" 23 #include "content/public/browser/browser_context.h"
24 #include "content/public/browser/render_frame_host.h"
20 #include "content/public/browser/service_worker_context.h" 25 #include "content/public/browser/service_worker_context.h"
21 #include "content/public/browser/storage_partition.h" 26 #include "content/public/browser/storage_partition.h"
22 #include "content/public/browser/web_contents.h" 27 #include "content/public/browser/web_contents.h"
28 #include "content/public/test/browser_test_utils.h"
23 #include "net/test/embedded_test_server/embedded_test_server.h" 29 #include "net/test/embedded_test_server/embedded_test_server.h"
30 #include "ppapi/shared_impl/ppapi_switches.h"
24 31
25 namespace { 32 namespace {
26 33
27 class ChromeServiceWorkerTest : public InProcessBrowserTest { 34 class ChromeServiceWorkerTest : public InProcessBrowserTest {
28 protected: 35 protected:
29 ChromeServiceWorkerTest() { 36 ChromeServiceWorkerTest() {
30 EXPECT_TRUE(service_worker_dir_.CreateUniqueTempDir()); 37 EXPECT_TRUE(service_worker_dir_.CreateUniqueTempDir());
31 } 38 }
39 ~ChromeServiceWorkerTest() override {}
32 40
33 void WriteFile(const base::FilePath::StringType& filename, 41 void WriteFile(const base::FilePath::StringType& filename,
34 base::StringPiece contents) { 42 base::StringPiece contents) {
35 EXPECT_EQ(base::checked_cast<int>(contents.size()), 43 EXPECT_EQ(base::checked_cast<int>(contents.size()),
36 base::WriteFile(service_worker_dir_.path().Append(filename), 44 base::WriteFile(service_worker_dir_.path().Append(filename),
37 contents.data(), 45 contents.data(),
38 contents.size())); 46 contents.size()));
39 } 47 }
40 48
41 base::ScopedTempDir service_worker_dir_; 49 base::ScopedTempDir service_worker_dir_;
50
51 private:
52 DISALLOW_COPY_AND_ASSIGN(ChromeServiceWorkerTest);
42 }; 53 };
43 54
44 static void ExpectResultAndRun(bool expected, 55 static void ExpectResultAndRun(bool expected,
45 const base::Closure& continuation, 56 const base::Closure& continuation,
46 bool actual) { 57 bool actual) {
47 EXPECT_EQ(expected, actual); 58 EXPECT_EQ(expected, actual);
48 continuation.Run(); 59 continuation.Run();
49 } 60 }
50 61
51 // http://crbug.com/368570 62 // http://crbug.com/368570
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
102 embedded_test_server()->GetURL("/test.html")); 113 embedded_test_server()->GetURL("/test.html"));
103 114
104 content::WindowedNotificationObserver observer( 115 content::WindowedNotificationObserver observer(
105 chrome::NOTIFICATION_BROWSER_CLOSED, content::Source<Browser>(incognito)); 116 chrome::NOTIFICATION_BROWSER_CLOSED, content::Source<Browser>(incognito));
106 incognito->window()->Close(); 117 incognito->window()->Close();
107 observer.Wait(); 118 observer.Wait();
108 119
109 // Test passes if we don't crash. 120 // Test passes if we don't crash.
110 } 121 }
111 122
123 class ChromeServiceWorkerFetchTest : public ChromeServiceWorkerTest {
124 protected:
125 ChromeServiceWorkerFetchTest() {}
126 ~ChromeServiceWorkerFetchTest() override {}
127
128 void SetUpOnMainThread() override {
129 WriteServiceWorkerFetchTestFiles();
130 embedded_test_server()->ServeFilesFromDirectory(service_worker_dir_.path());
131 ASSERT_TRUE(embedded_test_server()->Start());
132 InitializeServiceWorkerFetchTestPage();
133 }
134
135 std::string ExecuteScriptAndExtractString(const std::string& js) {
136 std::string result;
137 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
138 browser()->tab_strip_model()->GetActiveWebContents(), js, &result));
139 return result;
140 }
141
142 std::string RequestString(const std::string& url,
143 const std::string& mode,
144 const std::string& credentials) const {
145 return base::StringPrintf("url:%s, mode:%s, credentials:%s\n", url.c_str(),
146 mode.c_str(), credentials.c_str());
147 }
148
149 std::string GetURL(const std::string& relative_url) const {
150 return embedded_test_server()->GetURL(relative_url).spec();
151 }
152
153 private:
154 void WriteServiceWorkerFetchTestFiles() {
155 WriteFile(FILE_PATH_LITERAL("sw.js"),
156 "this.onactivate = function(event) {"
157 " event.waitUntil(self.clients.claim());"
158 "};"
159 "this.onfetch = function(event) {"
160 " event.respondWith("
161 " self.clients.matchAll().then(function(clients) {"
162 " clients.forEach(function(client) {"
163 " client.postMessage("
164 " 'url:' + event.request.url + ', ' +"
165 " 'mode:' + event.request.mode + ', ' +"
166 " 'credentials:' + event.request.credentials"
167 " );"
168 " });"
169 " return fetch(event.request);"
170 " }));"
171 "};");
172 WriteFile(FILE_PATH_LITERAL("test.html"),
173 "<script>"
174 "navigator.serviceWorker.register('./sw.js', {scope: './'})"
175 " .then(function(reg) {"
176 " reg.addEventListener('updatefound', function() {"
177 " var worker = reg.installing;"
178 " worker.addEventListener('statechange', function() {"
179 " if (worker.state == 'activated')"
180 " document.title = 'READY';"
181 " });"
182 " });"
183 " });"
184 "var reportOnFetch = true;"
185 "var issuedRequests = [];"
186 "function reportRequests() {"
187 " var str = '';"
188 " issuedRequests.forEach(function(data) {"
189 " str += data + '\\n';"
190 " });"
191 " window.domAutomationController.setAutomationId(0);"
192 " window.domAutomationController.send(str);"
193 "}"
194 "navigator.serviceWorker.addEventListener("
195 " 'message',"
196 " function(event) {"
197 " issuedRequests.push(event.data);"
198 " if (reportOnFetch) {"
199 " reportRequests();"
200 " }"
201 " }, false);"
202 "</script>");
203 }
204
205 void InitializeServiceWorkerFetchTestPage() {
206 // The message "READY" will be sent when the service worker is activated.
207 const base::string16 expected_title = base::ASCIIToUTF16("READY");
208 content::TitleWatcher title_watcher(
209 browser()->tab_strip_model()->GetActiveWebContents(), expected_title);
210 ui_test_utils::NavigateToURL(browser(),
211 embedded_test_server()->GetURL("/test.html"));
212 EXPECT_EQ(expected_title, title_watcher.WaitAndGetTitle());
213 }
214
215 DISALLOW_COPY_AND_ASSIGN(ChromeServiceWorkerFetchTest);
216 };
217
218 IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerFetchTest, EmbedPdfSameOrigin) {
219 // <embed src="test.pdf">
220 const std::string result(ExecuteScriptAndExtractString(
221 "var embed = document.createElement('embed');"
222 "embed.src = 'test.pdf';"
223 "document.body.appendChild(embed);"));
224 EXPECT_EQ(RequestString(GetURL("/test.pdf"), "no-cors", "include"), result);
225 }
226
227 IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerFetchTest, EmbedPdfOtherOrigin) {
228 // <embed src="https://www.example.com/test.pdf">
229 const std::string result(ExecuteScriptAndExtractString(
230 "var embed = document.createElement('embed');"
231 "embed.src = 'https://www.example.com/test.pdf';"
232 "document.body.appendChild(embed);"));
233 EXPECT_EQ(
234 RequestString("https://www.example.com/test.pdf", "no-cors", "include"),
235 result);
236 }
237
238 class ChromeServiceWorkerManifestFetchTest
239 : public ChromeServiceWorkerFetchTest {
240 protected:
241 ChromeServiceWorkerManifestFetchTest() {}
242 ~ChromeServiceWorkerManifestFetchTest() override {}
243
244 void SetUpCommandLine(base::CommandLine* command_line) override {
245 ChromeServiceWorkerFetchTest::SetUpCommandLine(command_line);
246 command_line->AppendSwitch(switches::kEnableAddToShelf);
247 }
248
249 std::string ExecuteManifestFetchTest(const std::string& url,
250 const std::string& cross_origin) {
251 std::string js(
252 base::StringPrintf("reportOnFetch = false;"
253 "var link = document.createElement('link');"
254 "link.rel = 'manifest';"
255 "link.href = '%s';",
256 url.c_str()));
257 if (!cross_origin.empty()) {
258 js +=
259 base::StringPrintf("link.crossOrigin = '%s';", cross_origin.c_str());
260 }
261 js += "document.head.appendChild(link);";
262 ExecuteJavaScriptForTests(js);
263 return RequestAppBannerAndGetIssuedRequests();
264 }
265
266 private:
267 void ExecuteJavaScriptForTests(const std::string& js) {
268 browser()
269 ->tab_strip_model()
270 ->GetActiveWebContents()
271 ->GetMainFrame()
272 ->ExecuteJavaScriptForTests(base::ASCIIToUTF16(js));
273 }
274
275 std::string RequestAppBannerAndGetIssuedRequests() {
276 EXPECT_TRUE(browser()->RequestAppBanner(
277 browser()->tab_strip_model()->GetActiveWebContents()));
278 return ExecuteScriptAndExtractString(
279 "if (issuedRequests.length != 0) reportRequests();"
280 "else reportOnFetch = true;");
281 }
282 DISALLOW_COPY_AND_ASSIGN(ChromeServiceWorkerManifestFetchTest);
283 };
284
285 IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerManifestFetchTest, SameOrigin) {
286 // <link rel="manifest" href="manifest.json">
287 EXPECT_EQ(RequestString(GetURL("/manifest.json"), "cors", "same-origin"),
288 ExecuteManifestFetchTest("manifest.json", ""));
289 }
290
291 IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerManifestFetchTest,
292 SameOriginUseCredentials) {
293 // <link rel="manifest" href="manifest.json" crossorigin="use-credentials">
294 EXPECT_EQ(RequestString(GetURL("/manifest.json"), "cors", "include"),
295 ExecuteManifestFetchTest("manifest.json", "use-credentials"));
296 }
297
298 IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerManifestFetchTest, OtherOrigin) {
299 // <link rel="manifest" href="https://www.example.com/manifest.json">
300 EXPECT_EQ(
301 RequestString("https://www.example.com/manifest.json", "cors",
302 "same-origin"),
303 ExecuteManifestFetchTest("https://www.example.com/manifest.json", ""));
304 }
305
306 IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerManifestFetchTest,
307 OtherOriginUseCredentials) {
308 // <link rel="manifest" href="https://www.example.com/manifest.json"
309 // crossorigin="use-credentials">
310 EXPECT_EQ(
311 RequestString("https://www.example.com/manifest.json", "cors", "include"),
312 ExecuteManifestFetchTest("https://www.example.com/manifest.json",
313 "use-credentials"));
314 }
315
316 class ChromeServiceWorkerFetchPPAPITest : public ChromeServiceWorkerFetchTest {
317 protected:
318 ChromeServiceWorkerFetchPPAPITest() {}
319 ~ChromeServiceWorkerFetchPPAPITest() override {}
320
321 void SetUpOnMainThread() override {
322 base::FilePath document_root;
323 ASSERT_TRUE(ui_test_utils::GetRelativeBuildDirectory(&document_root));
324 embedded_test_server()->AddDefaultHandlers(
325 document_root.Append(FILE_PATH_LITERAL("nacl_test_data"))
326 .Append(FILE_PATH_LITERAL("pnacl")));
327 ChromeServiceWorkerFetchTest::SetUpOnMainThread();
328 test_page_url_ = GetURL("/pnacl_url_loader.html");
329 }
330
331 std::string GetRequestStringForPNACL() const {
332 return RequestString(test_page_url_, "navigate", "include") +
333 RequestString(GetURL("/pnacl_url_loader.nmf"), "same-origin",
334 "include") +
335 RequestString(GetURL("/pnacl_url_loader_newlib_pnacl.pexe"),
336 "same-origin", "include");
337 }
338
339 std::string ExecutePNACLUrlLoaderTest(const std::string& mode) {
340 std::string result(ExecuteScriptAndExtractString(
341 base::StringPrintf("reportOnFetch = false;"
342 "var iframe = document.createElement('iframe');"
343 "iframe.src='%s#%s';"
344 "document.body.appendChild(iframe);",
345 test_page_url_.c_str(), mode.c_str())));
346 EXPECT_EQ(base::StringPrintf("OnOpen%s", mode.c_str()), result);
347 return ExecuteScriptAndExtractString("reportRequests();");
348 }
349
350 private:
351 std::string test_page_url_;
352
353 DISALLOW_COPY_AND_ASSIGN(ChromeServiceWorkerFetchPPAPITest);
354 };
355
356 IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerFetchPPAPITest, SameOrigin) {
357 // In pnacl_url_loader.cc:
358 // request.SetMethod("GET");
359 // request.SetURL("/echo");
360 EXPECT_EQ(GetRequestStringForPNACL() +
361 RequestString(GetURL("/echo"), "same-origin", "include"),
362 ExecutePNACLUrlLoaderTest("Same"));
363 }
364
365 IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerFetchPPAPITest, SameOriginCORS) {
366 // In pnacl_url_loader.cc:
367 // request.SetMethod("GET");
368 // request.SetURL("/echo");
369 // request.SetAllowCrossOriginRequests(true);
370 EXPECT_EQ(GetRequestStringForPNACL() +
371 RequestString(GetURL("/echo"), "cors", "same-origin"),
372 ExecutePNACLUrlLoaderTest("SameCORS"));
373 }
374
375 IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerFetchPPAPITest,
376 SameOriginCredentials) {
377 // In pnacl_url_loader.cc:
378 // request.SetMethod("GET");
379 // request.SetURL("/echo");
380 // request.SetAllowCredentials(true);
381 EXPECT_EQ(GetRequestStringForPNACL() +
382 RequestString(GetURL("/echo"), "same-origin", "include"),
383 ExecutePNACLUrlLoaderTest("SameCredentials"));
384 }
385
386 IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerFetchPPAPITest,
387 SameOriginCORSCredentials) {
388 // In pnacl_url_loader.cc:
389 // request.SetMethod("GET");
390 // request.SetURL("/echo");
391 // request.SetAllowCrossOriginRequests(true);
392 // request.SetAllowCredentials(true);
393 EXPECT_EQ(GetRequestStringForPNACL() +
394 RequestString(GetURL("/echo"), "cors", "include"),
395 ExecutePNACLUrlLoaderTest("SameCORSCredentials"));
396 }
397
398 IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerFetchPPAPITest, OtherOrigin) {
399 // In pnacl_url_loader.cc:
400 // request.SetMethod("GET");
401 // request.SetURL("https://www.example.com/echo");
402 // This request fails because AllowCrossOriginRequests is not set.
403 EXPECT_EQ(GetRequestStringForPNACL(), ExecutePNACLUrlLoaderTest("Other"));
404 }
405
406 IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerFetchPPAPITest, OtherOriginCORS) {
407 // In pnacl_url_loader.cc:
408 // request.SetMethod("GET");
409 // request.SetURL("https://www.example.com/echo");
410 // request.SetAllowCrossOriginRequests(true);
411 EXPECT_EQ(
412 GetRequestStringForPNACL() +
413 RequestString("https://www.example.com/echo", "cors", "same-origin"),
414 ExecutePNACLUrlLoaderTest("OtherCORS"));
415 }
416
417 IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerFetchPPAPITest,
418 OtherOriginCredentials) {
419 // In pnacl_url_loader.cc:
420 // request.SetMethod("GET");
421 // request.SetURL("https://www.example.com/echo");
422 // request.SetAllowCredentials(true);
423 // This request fails because AllowCrossOriginRequests is not set.
424 EXPECT_EQ(GetRequestStringForPNACL(),
425 ExecutePNACLUrlLoaderTest("OtherCredentials"));
426 }
427
428 IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerFetchPPAPITest,
429 OtherOriginCORSCredentials) {
430 // In pnacl_url_loader.cc:
431 // request.SetMethod("GET");
432 // request.SetURL("https://www.example.com/echo");
433 // request.SetAllowCrossOriginRequests(true);
434 // request.SetAllowCredentials(true);
435 EXPECT_EQ(
436 GetRequestStringForPNACL() +
437 RequestString("https://www.example.com/echo", "cors", "include"),
438 ExecutePNACLUrlLoaderTest("OtherCORSCredentials"));
439 }
440
441 class ChromeServiceWorkerFetchPPAPIPrivateTest
442 : public ChromeServiceWorkerFetchPPAPITest {
443 protected:
444 ChromeServiceWorkerFetchPPAPIPrivateTest() {}
445 ~ChromeServiceWorkerFetchPPAPIPrivateTest() override {}
446
447 void SetUpCommandLine(base::CommandLine* command_line) override {
448 ChromeServiceWorkerFetchPPAPITest::SetUpCommandLine(command_line);
449 // Sets this flag to test that the fetch request from the plugins with
450 // private permission (PERMISSION_PRIVATE) should not go to the service
451 // worker.
452 command_line->AppendSwitch(switches::kEnablePepperTesting);
453 }
454
455 private:
456 DISALLOW_COPY_AND_ASSIGN(ChromeServiceWorkerFetchPPAPIPrivateTest);
457 };
458
459 IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerFetchPPAPIPrivateTest, SameOrigin) {
460 EXPECT_EQ(GetRequestStringForPNACL(), ExecutePNACLUrlLoaderTest("Same"));
461 }
462
463 IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerFetchPPAPIPrivateTest,
464 SameOriginCORS) {
465 EXPECT_EQ(GetRequestStringForPNACL(), ExecutePNACLUrlLoaderTest("SameCORS"));
466 }
467
468 IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerFetchPPAPIPrivateTest,
469 SameOriginCredentials) {
470 EXPECT_EQ(GetRequestStringForPNACL(),
471 ExecutePNACLUrlLoaderTest("SameCredentials"));
472 }
473
474 IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerFetchPPAPIPrivateTest,
475 SameOriginCORSCredentials) {
476 EXPECT_EQ(GetRequestStringForPNACL(),
477 ExecutePNACLUrlLoaderTest("SameCORSCredentials"));
478 }
479
480 IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerFetchPPAPIPrivateTest, OtherOrigin) {
481 EXPECT_EQ(GetRequestStringForPNACL(), ExecutePNACLUrlLoaderTest("Other"));
482 }
483
484 IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerFetchPPAPIPrivateTest,
485 OtherOriginCORS) {
486 EXPECT_EQ(GetRequestStringForPNACL(), ExecutePNACLUrlLoaderTest("OtherCORS"));
487 }
488
489 IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerFetchPPAPIPrivateTest,
490 OtherOriginCredentials) {
491 EXPECT_EQ(GetRequestStringForPNACL(),
492 ExecutePNACLUrlLoaderTest("OtherCredentials"));
493 }
494
495 IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerFetchPPAPIPrivateTest,
496 OtherOriginCORSCredentials) {
497 EXPECT_EQ(GetRequestStringForPNACL(),
498 ExecutePNACLUrlLoaderTest("OtherCORSCredentials"));
499 }
500
112 } // namespace 501 } // namespace
OLDNEW
« no previous file with comments | « no previous file | chrome/chrome_tests.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698