| OLD | NEW |
| 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 505 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 516 } | 516 } |
| 517 | 517 |
| 518 IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerFetchPPAPIPrivateTest, | 518 IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerFetchPPAPIPrivateTest, |
| 519 OtherOriginCORSCredentials) { | 519 OtherOriginCORSCredentials) { |
| 520 EXPECT_EQ(GetRequestStringForPNACL(), | 520 EXPECT_EQ(GetRequestStringForPNACL(), |
| 521 ExecutePNACLUrlLoaderTest("OtherCORSCredentials")); | 521 ExecutePNACLUrlLoaderTest("OtherCORSCredentials")); |
| 522 } | 522 } |
| 523 | 523 |
| 524 class ServiceWorkerSpeculativeLaunchTest : public ChromeServiceWorkerTest { | 524 class ServiceWorkerSpeculativeLaunchTest : public ChromeServiceWorkerTest { |
| 525 protected: | 525 protected: |
| 526 static const std::string kNavigationHintLinkMouseDownMetricName; |
| 527 |
| 526 ServiceWorkerSpeculativeLaunchTest() {} | 528 ServiceWorkerSpeculativeLaunchTest() {} |
| 527 ~ServiceWorkerSpeculativeLaunchTest() override {} | 529 ~ServiceWorkerSpeculativeLaunchTest() override {} |
| 528 | 530 |
| 529 void SetUpCommandLine(base::CommandLine* command_line) override { | 531 void SetUpCommandLine(base::CommandLine* command_line) override { |
| 530 command_line->AppendSwitchASCII( | 532 command_line->AppendSwitchASCII( |
| 531 switches::kEnableFeatures, | 533 switches::kEnableFeatures, |
| 532 features::kSpeculativeLaunchServiceWorker.name); | 534 features::kSpeculativeLaunchServiceWorker.name); |
| 533 } | 535 } |
| 534 | 536 |
| 537 void WriteTestHtmlFile() { |
| 538 WriteFile( |
| 539 FILE_PATH_LITERAL("test.html"), |
| 540 "<script>" |
| 541 "navigator.serviceWorker.register('./sw.js', {scope: './scope.html'})" |
| 542 " .then(function(reg) {" |
| 543 " reg.addEventListener('updatefound', function() {" |
| 544 " var worker = reg.installing;" |
| 545 " worker.addEventListener('statechange', function() {" |
| 546 " if (worker.state == 'activated')" |
| 547 " document.title = 'READY';" |
| 548 " });" |
| 549 " });" |
| 550 " });" |
| 551 "</script>" |
| 552 "<body style='margin:0; padding:0;'>" |
| 553 "<a href='./scope.html' " |
| 554 "style='position:fixed; width:1px; height:1px;'></a>" |
| 555 "</body>"); |
| 556 } |
| 557 |
| 558 void RunNavigationHintTest() { |
| 559 embedded_test_server()->ServeFilesFromDirectory( |
| 560 service_worker_dir_.GetPath()); |
| 561 ASSERT_TRUE(embedded_test_server()->Start()); |
| 562 |
| 563 content::ServiceWorkerContext* sw_context = |
| 564 content::BrowserContext::GetDefaultStoragePartition( |
| 565 browser()->profile()) |
| 566 ->GetServiceWorkerContext(); |
| 567 |
| 568 const base::string16 expected_title1 = base::ASCIIToUTF16("READY"); |
| 569 content::TitleWatcher title_watcher1( |
| 570 browser()->tab_strip_model()->GetActiveWebContents(), expected_title1); |
| 571 ui_test_utils::NavigateToURL(browser(), |
| 572 embedded_test_server()->GetURL("/test.html")); |
| 573 EXPECT_EQ(expected_title1, title_watcher1.WaitAndGetTitle()); |
| 574 |
| 575 histogram_tester_.ExpectBucketCount("ServiceWorker.StartNewWorker.Status", |
| 576 0 /* SERVICE_WORKER_OK */, 1); |
| 577 |
| 578 sw_context->StopAllServiceWorkersForOrigin( |
| 579 embedded_test_server()->base_url()); |
| 580 |
| 581 const base::string16 expected_title2 = base::ASCIIToUTF16("Done"); |
| 582 content::TitleWatcher title_watcher2( |
| 583 browser()->tab_strip_model()->GetActiveWebContents(), expected_title2); |
| 584 |
| 585 histogram_tester_.ExpectTotalCount(kNavigationHintLinkMouseDownMetricName, |
| 586 0); |
| 587 content::SimulateMouseClickAt( |
| 588 browser()->tab_strip_model()->GetActiveWebContents(), 0, |
| 589 blink::WebMouseEvent::Button::Left, gfx::Point(0, 0)); |
| 590 EXPECT_EQ(expected_title2, title_watcher2.WaitAndGetTitle()); |
| 591 } |
| 592 |
| 535 base::HistogramTester histogram_tester_; | 593 base::HistogramTester histogram_tester_; |
| 536 | 594 |
| 537 private: | 595 private: |
| 538 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerSpeculativeLaunchTest); | 596 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerSpeculativeLaunchTest); |
| 539 }; | 597 }; |
| 540 | 598 |
| 599 // static |
| 600 const std::string |
| 601 ServiceWorkerSpeculativeLaunchTest::kNavigationHintLinkMouseDownMetricName = |
| 602 "ServiceWorker.StartWorker.StatusByPurpose_" |
| 603 "NAVIGATION_HINT_LINK_MOUSE_DOWN"; |
| 604 |
| 541 IN_PROC_BROWSER_TEST_F(ServiceWorkerSpeculativeLaunchTest, MouseDown) { | 605 IN_PROC_BROWSER_TEST_F(ServiceWorkerSpeculativeLaunchTest, MouseDown) { |
| 542 WriteFile( | 606 WriteFile( |
| 543 FILE_PATH_LITERAL("sw.js"), | 607 FILE_PATH_LITERAL("sw.js"), |
| 544 "self.onfetch = function(e) {" | 608 "self.onfetch = function(e) {" |
| 545 " e.respondWith(new Response('<title>Done</title>'," | 609 " e.respondWith(new Response('<title>Done</title>'," |
| 546 " {headers: {'Content-Type': 'text/html'}}));" | 610 " {headers: {'Content-Type': 'text/html'}}));" |
| 547 "};"); | 611 "};"); |
| 548 WriteFile( | 612 WriteTestHtmlFile(); |
| 549 FILE_PATH_LITERAL("test.html"), | 613 RunNavigationHintTest(); |
| 550 "<script>" | 614 // The service worker must be started by a navigation hint. |
| 551 "navigator.serviceWorker.register('./sw.js', {scope: './scope/'})" | 615 histogram_tester_.ExpectBucketCount(kNavigationHintLinkMouseDownMetricName, |
| 552 " .then(function(reg) {" | 616 0 /* SERVICE_WORKER_OK */, 1); |
| 553 " reg.addEventListener('updatefound', function() {" | 617 } |
| 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 | 618 |
| 566 embedded_test_server()->ServeFilesFromDirectory( | 619 IN_PROC_BROWSER_TEST_F(ServiceWorkerSpeculativeLaunchTest, |
| 567 service_worker_dir_.GetPath()); | 620 NoFetchEventHandler) { |
| 568 ASSERT_TRUE(embedded_test_server()->Start()); | 621 WriteFile(FILE_PATH_LITERAL("sw.js"), "// no fetch event handler."); |
| 569 | 622 WriteFile(FILE_PATH_LITERAL("scope.html"), "<title>Done</title>"); |
| 570 content::ServiceWorkerContext* sw_context = | 623 WriteTestHtmlFile(); |
| 571 content::BrowserContext::GetDefaultStoragePartition(browser()->profile()) | 624 RunNavigationHintTest(); |
| 572 ->GetServiceWorkerContext(); | 625 // The service worker must NOT be started by a navigation hint. |
| 573 | 626 histogram_tester_.ExpectTotalCount(kNavigationHintLinkMouseDownMetricName, 0); |
| 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. | |
| 601 histogram_tester_.ExpectBucketCount( | |
| 602 "ServiceWorker.StartWorker.StatusByPurpose_NAVIGATION_HINT_LINK_MOUSE_" | |
| 603 "DOWN", | |
| 604 0 /* SERVICE_WORKER_OK */, 1); | |
| 605 } | 627 } |
| 606 | 628 |
| 607 } // namespace | 629 } // namespace |
| OLD | NEW |