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

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

Issue 2393503002: Don't speculatively start ServiceWorker if it doesn't have FetchEvent handler (Closed)
Patch Set: add test Created 4 years, 2 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 | content/browser/service_worker/service_worker_context_wrapper.cc » ('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/command_line.h"
10 #include "base/files/scoped_temp_dir.h" 10 #include "base/files/scoped_temp_dir.h"
(...skipping 514 matching lines...) Expand 10 before | Expand all | Expand 10 after
525 protected: 525 protected:
526 ServiceWorkerSpeculativeLaunchTest() {} 526 ServiceWorkerSpeculativeLaunchTest() {}
527 ~ServiceWorkerSpeculativeLaunchTest() override {} 527 ~ServiceWorkerSpeculativeLaunchTest() override {}
528 528
529 void SetUpCommandLine(base::CommandLine* command_line) override { 529 void SetUpCommandLine(base::CommandLine* command_line) override {
530 command_line->AppendSwitchASCII( 530 command_line->AppendSwitchASCII(
531 switches::kEnableFeatures, 531 switches::kEnableFeatures,
532 features::kSpeculativeLaunchServiceWorker.name); 532 features::kSpeculativeLaunchServiceWorker.name);
533 } 533 }
534 534
535 void WriteTestHtmlFile() {
536 WriteFile(
537 FILE_PATH_LITERAL("test.html"),
538 "<script>"
539 "navigator.serviceWorker.register('./sw.js', {scope: './scope.html'})"
540 " .then(function(reg) {"
541 " reg.addEventListener('updatefound', function() {"
542 " var worker = reg.installing;"
543 " worker.addEventListener('statechange', function() {"
544 " if (worker.state == 'activated')"
545 " document.title = 'READY';"
546 " });"
547 " });"
548 " });"
549 "</script>"
550 "<body style='margin:0; padding:0;'>"
551 "<a href='./scope.html' "
552 "style='position:fixed; width:1px; height:1px;'></a>"
553 "</body>");
554 }
falken 2016/10/04 04:50:51 nit: add newline
horo 2016/10/04 06:54:07 Done.
555 void RunNavigationHintTest() {
556 embedded_test_server()->ServeFilesFromDirectory(
557 service_worker_dir_.GetPath());
558 ASSERT_TRUE(embedded_test_server()->Start());
559
560 content::ServiceWorkerContext* sw_context =
561 content::BrowserContext::GetDefaultStoragePartition(
562 browser()->profile())
563 ->GetServiceWorkerContext();
564
565 const base::string16 expected_title1 = base::ASCIIToUTF16("READY");
566 content::TitleWatcher title_watcher1(
567 browser()->tab_strip_model()->GetActiveWebContents(), expected_title1);
568 ui_test_utils::NavigateToURL(browser(),
569 embedded_test_server()->GetURL("/test.html"));
570 EXPECT_EQ(expected_title1, title_watcher1.WaitAndGetTitle());
571
572 histogram_tester_.ExpectBucketCount("ServiceWorker.StartNewWorker.Status",
573 0 /* SERVICE_WORKER_OK */, 1);
falken 2016/10/04 04:50:51 nit: how about using SERVICE_WORKER_OK directly?
horo 2016/10/04 06:54:07 Unfortunately SERVICE_WORKER_OK is not available u
574
575 sw_context->StopAllServiceWorkersForOrigin(
576 embedded_test_server()->base_url());
falken 2016/10/04 04:50:51 Since StopWorker is async, it seems like this func
horo 2016/10/04 06:54:07 Even if the worker is still stopping, we record th
577
578 const base::string16 expected_title2 = base::ASCIIToUTF16("Done");
579 content::TitleWatcher title_watcher2(
580 browser()->tab_strip_model()->GetActiveWebContents(), expected_title2);
581
582 histogram_tester_.ExpectTotalCount(
583 "ServiceWorker.StartWorker.StatusByPurpose_NAVIGATION_HINT_LINK_MOUSE_"
584 "DOWN",
falken 2016/10/04 04:50:51 nit: Can you preserve this string on one line for
horo 2016/10/04 06:54:07 Done.
585 0);
586 content::SimulateMouseClickAt(
587 browser()->tab_strip_model()->GetActiveWebContents(), 0,
588 blink::WebMouseEvent::Button::Left, gfx::Point(0, 0));
589 EXPECT_EQ(expected_title2, title_watcher2.WaitAndGetTitle());
590 }
591
535 base::HistogramTester histogram_tester_; 592 base::HistogramTester histogram_tester_;
536 593
537 private: 594 private:
538 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerSpeculativeLaunchTest); 595 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerSpeculativeLaunchTest);
539 }; 596 };
540 597
541 IN_PROC_BROWSER_TEST_F(ServiceWorkerSpeculativeLaunchTest, MouseDown) { 598 IN_PROC_BROWSER_TEST_F(ServiceWorkerSpeculativeLaunchTest, MouseDown) {
542 WriteFile( 599 WriteFile(
543 FILE_PATH_LITERAL("sw.js"), 600 FILE_PATH_LITERAL("sw.js"),
544 "self.onfetch = function(e) {" 601 "self.onfetch = function(e) {"
545 " e.respondWith(new Response('<title>Done</title>'," 602 " e.respondWith(new Response('<title>Done</title>',"
546 " {headers: {'Content-Type': 'text/html'}}));" 603 " {headers: {'Content-Type': 'text/html'}}));"
547 "};"); 604 "};");
548 WriteFile( 605 WriteTestHtmlFile();
549 FILE_PATH_LITERAL("test.html"), 606 RunNavigationHintTest();
550 "<script>"
551 "navigator.serviceWorker.register('./sw.js', {scope: './scope/'})"
552 " .then(function(reg) {"
553 " reg.addEventListener('updatefound', function() {"
554 " var worker = reg.installing;"
555 " worker.addEventListener('statechange', function() {"
556 " if (worker.state == 'activated')"
557 " document.title = 'READY';"
558 " });"
559 " });"
560 " });"
561 "</script>"
562 "<body style='margin:0; padding:0;'>"
563 "<a href='./scope/' style='position:fixed; width:1px; height:1px;'></a>"
564 "</body>");
565
566 embedded_test_server()->ServeFilesFromDirectory(
567 service_worker_dir_.GetPath());
568 ASSERT_TRUE(embedded_test_server()->Start());
569
570 content::ServiceWorkerContext* sw_context =
571 content::BrowserContext::GetDefaultStoragePartition(browser()->profile())
572 ->GetServiceWorkerContext();
573
574 const base::string16 expected_title1 = base::ASCIIToUTF16("READY");
575 content::TitleWatcher title_watcher1(
576 browser()->tab_strip_model()->GetActiveWebContents(), expected_title1);
577 ui_test_utils::NavigateToURL(browser(),
578 embedded_test_server()->GetURL("/test.html"));
579 EXPECT_EQ(expected_title1, title_watcher1.WaitAndGetTitle());
580
581 histogram_tester_.ExpectBucketCount("ServiceWorker.StartNewWorker.Status",
582 0 /* SERVICE_WORKER_OK */, 1);
583
584 sw_context->StopAllServiceWorkersForOrigin(
585 embedded_test_server()->base_url());
586
587 const base::string16 expected_title2 = base::ASCIIToUTF16("Done");
588 content::TitleWatcher title_watcher2(
589 browser()->tab_strip_model()->GetActiveWebContents(), expected_title2);
590
591 histogram_tester_.ExpectTotalCount(
592 "ServiceWorker.StartWorker.StatusByPurpose_NAVIGATION_HINT_LINK_MOUSE_"
593 "DOWN",
594 0);
595 content::SimulateMouseClickAt(
596 browser()->tab_strip_model()->GetActiveWebContents(), 0,
597 blink::WebMouseEvent::Button::Left, gfx::Point(0, 0));
598 EXPECT_EQ(expected_title2, title_watcher2.WaitAndGetTitle());
599
600 // The service worker must be started by a navigation hint. 607 // The service worker must be started by a navigation hint.
601 histogram_tester_.ExpectBucketCount( 608 histogram_tester_.ExpectBucketCount(
602 "ServiceWorker.StartWorker.StatusByPurpose_NAVIGATION_HINT_LINK_MOUSE_" 609 "ServiceWorker.StartWorker.StatusByPurpose_NAVIGATION_HINT_LINK_MOUSE_"
603 "DOWN", 610 "DOWN",
604 0 /* SERVICE_WORKER_OK */, 1); 611 0 /* SERVICE_WORKER_OK */, 1);
falken 2016/10/04 04:50:51 const string and SERVICE_WORKER_OK here too
horo 2016/10/04 06:54:07 Done.
605 } 612 }
606 613
614 IN_PROC_BROWSER_TEST_F(ServiceWorkerSpeculativeLaunchTest,
615 NoFetchEventHandler) {
616 WriteFile(FILE_PATH_LITERAL("sw.js"), "// no fetch event handler.");
falken 2016/10/04 04:50:51 This doesn't need mock headers with javascript con
horo 2016/10/04 06:54:07 Ah, we don't need scope.html.mock-http-headers. e
617 WriteFile(FILE_PATH_LITERAL("scope.html"), "<title>Done</title>");
618 WriteFile(FILE_PATH_LITERAL("scope.html.mock-http-headers"),
619 "HTTP/1.1 200 OK\nContent-Type: text/html; charset=utf-8\n");
620 WriteTestHtmlFile();
621 RunNavigationHintTest();
622 // The service worker must NOT be started by a navigation hint.
623 histogram_tester_.ExpectTotalCount(
624 "ServiceWorker.StartWorker.StatusByPurpose_NAVIGATION_HINT_LINK_MOUSE_"
625 "DOWN",
falken 2016/10/04 04:50:51 const string
horo 2016/10/04 06:54:07 Done.
626 0);
627 }
628
607 } // namespace 629 } // namespace
OLDNEW
« no previous file with comments | « no previous file | content/browser/service_worker/service_worker_context_wrapper.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698