| 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" |
| 11 #include "base/numerics/safe_conversions.h" | 11 #include "base/numerics/safe_conversions.h" |
| 12 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
| 13 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
| 14 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
| 15 #include "base/test/histogram_tester.h" | |
| 16 #include "build/build_config.h" | 15 #include "build/build_config.h" |
| 17 #include "chrome/browser/chrome_notification_types.h" | 16 #include "chrome/browser/chrome_notification_types.h" |
| 18 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" | 17 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
| 19 #include "chrome/browser/content_settings/tab_specific_content_settings.h" | 18 #include "chrome/browser/content_settings/tab_specific_content_settings.h" |
| 20 #include "chrome/browser/profiles/profile.h" | 19 #include "chrome/browser/profiles/profile.h" |
| 21 #include "chrome/browser/ui/browser.h" | 20 #include "chrome/browser/ui/browser.h" |
| 22 #include "chrome/browser/ui/browser_window.h" | 21 #include "chrome/browser/ui/browser_window.h" |
| 23 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 22 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 24 #include "chrome/common/chrome_switches.h" | 23 #include "chrome/common/chrome_switches.h" |
| 25 #include "chrome/test/base/in_process_browser_test.h" | 24 #include "chrome/test/base/in_process_browser_test.h" |
| 26 #include "chrome/test/base/ui_test_utils.h" | 25 #include "chrome/test/base/ui_test_utils.h" |
| 27 #include "components/content_settings/core/browser/host_content_settings_map.h" | 26 #include "components/content_settings/core/browser/host_content_settings_map.h" |
| 28 #include "content/public/browser/browser_context.h" | 27 #include "content/public/browser/browser_context.h" |
| 29 #include "content/public/browser/render_frame_host.h" | 28 #include "content/public/browser/render_frame_host.h" |
| 30 #include "content/public/browser/service_worker_context.h" | 29 #include "content/public/browser/service_worker_context.h" |
| 31 #include "content/public/browser/storage_partition.h" | 30 #include "content/public/browser/storage_partition.h" |
| 32 #include "content/public/browser/web_contents.h" | 31 #include "content/public/browser/web_contents.h" |
| 33 #include "content/public/common/content_features.h" | |
| 34 #include "content/public/common/content_switches.h" | |
| 35 #include "content/public/test/browser_test_utils.h" | 32 #include "content/public/test/browser_test_utils.h" |
| 36 #include "net/test/embedded_test_server/embedded_test_server.h" | 33 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 37 #include "ppapi/shared_impl/ppapi_switches.h" | 34 #include "ppapi/shared_impl/ppapi_switches.h" |
| 38 | 35 |
| 39 namespace { | 36 namespace { |
| 40 | 37 |
| 41 class ChromeServiceWorkerTest : public InProcessBrowserTest { | 38 class ChromeServiceWorkerTest : public InProcessBrowserTest { |
| 42 protected: | 39 protected: |
| 43 ChromeServiceWorkerTest() { | 40 ChromeServiceWorkerTest() { |
| 44 EXPECT_TRUE(service_worker_dir_.CreateUniqueTempDir()); | 41 EXPECT_TRUE(service_worker_dir_.CreateUniqueTempDir()); |
| (...skipping 555 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 600 EXPECT_EQ(GetRequestStringForPNACL(), | 597 EXPECT_EQ(GetRequestStringForPNACL(), |
| 601 ExecutePNACLUrlLoaderTest("OtherCredentials")); | 598 ExecutePNACLUrlLoaderTest("OtherCredentials")); |
| 602 } | 599 } |
| 603 | 600 |
| 604 IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerFetchPPAPIPrivateTest, | 601 IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerFetchPPAPIPrivateTest, |
| 605 OtherOriginCORSCredentials) { | 602 OtherOriginCORSCredentials) { |
| 606 EXPECT_EQ(GetRequestStringForPNACL(), | 603 EXPECT_EQ(GetRequestStringForPNACL(), |
| 607 ExecutePNACLUrlLoaderTest("OtherCORSCredentials")); | 604 ExecutePNACLUrlLoaderTest("OtherCORSCredentials")); |
| 608 } | 605 } |
| 609 | 606 |
| 610 class ServiceWorkerSpeculativeLaunchTest : public ChromeServiceWorkerTest { | |
| 611 protected: | |
| 612 static const std::string kNavigationHintLinkMouseDownMetricName; | |
| 613 | |
| 614 ServiceWorkerSpeculativeLaunchTest() {} | |
| 615 ~ServiceWorkerSpeculativeLaunchTest() override {} | |
| 616 | |
| 617 void SetUpCommandLine(base::CommandLine* command_line) override { | |
| 618 command_line->AppendSwitchASCII( | |
| 619 switches::kEnableFeatures, | |
| 620 features::kSpeculativeLaunchServiceWorker.name); | |
| 621 } | |
| 622 | |
| 623 void WriteTestHtmlFile() { | |
| 624 WriteFile( | |
| 625 FILE_PATH_LITERAL("test.html"), | |
| 626 "<script>" | |
| 627 "navigator.serviceWorker.register('./sw.js', {scope: './scope.html'})" | |
| 628 " .then(function(reg) {" | |
| 629 " reg.addEventListener('updatefound', function() {" | |
| 630 " var worker = reg.installing;" | |
| 631 " worker.addEventListener('statechange', function() {" | |
| 632 " if (worker.state == 'activated')" | |
| 633 " document.title = 'READY';" | |
| 634 " });" | |
| 635 " });" | |
| 636 " });" | |
| 637 "</script>" | |
| 638 "<body style='margin:0; padding:0;'>" | |
| 639 "<a href='./scope.html' " | |
| 640 "style='position:fixed; width:1px; height:1px;'></a>" | |
| 641 "</body>"); | |
| 642 } | |
| 643 | |
| 644 void RunNavigationHintTest() { | |
| 645 embedded_test_server()->ServeFilesFromDirectory( | |
| 646 service_worker_dir_.GetPath()); | |
| 647 ASSERT_TRUE(embedded_test_server()->Start()); | |
| 648 | |
| 649 content::ServiceWorkerContext* sw_context = | |
| 650 content::BrowserContext::GetDefaultStoragePartition( | |
| 651 browser()->profile()) | |
| 652 ->GetServiceWorkerContext(); | |
| 653 | |
| 654 const base::string16 expected_title1 = base::ASCIIToUTF16("READY"); | |
| 655 content::TitleWatcher title_watcher1( | |
| 656 browser()->tab_strip_model()->GetActiveWebContents(), expected_title1); | |
| 657 ui_test_utils::NavigateToURL(browser(), | |
| 658 embedded_test_server()->GetURL("/test.html")); | |
| 659 EXPECT_EQ(expected_title1, title_watcher1.WaitAndGetTitle()); | |
| 660 | |
| 661 histogram_tester_.ExpectBucketCount("ServiceWorker.StartNewWorker.Status", | |
| 662 0 /* SERVICE_WORKER_OK */, 1); | |
| 663 | |
| 664 sw_context->StopAllServiceWorkersForOrigin( | |
| 665 embedded_test_server()->base_url()); | |
| 666 | |
| 667 const base::string16 expected_title2 = base::ASCIIToUTF16("Done"); | |
| 668 content::TitleWatcher title_watcher2( | |
| 669 browser()->tab_strip_model()->GetActiveWebContents(), expected_title2); | |
| 670 | |
| 671 histogram_tester_.ExpectTotalCount(kNavigationHintLinkMouseDownMetricName, | |
| 672 0); | |
| 673 content::SimulateMouseClickAt( | |
| 674 browser()->tab_strip_model()->GetActiveWebContents(), 0, | |
| 675 blink::WebMouseEvent::Button::Left, gfx::Point(0, 0)); | |
| 676 EXPECT_EQ(expected_title2, title_watcher2.WaitAndGetTitle()); | |
| 677 } | |
| 678 | |
| 679 base::HistogramTester histogram_tester_; | |
| 680 | |
| 681 private: | |
| 682 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerSpeculativeLaunchTest); | |
| 683 }; | |
| 684 | |
| 685 // static | |
| 686 const std::string | |
| 687 ServiceWorkerSpeculativeLaunchTest::kNavigationHintLinkMouseDownMetricName = | |
| 688 "ServiceWorker.StartWorker.StatusByPurpose_" | |
| 689 "NAVIGATION_HINT_LINK_MOUSE_DOWN"; | |
| 690 | |
| 691 IN_PROC_BROWSER_TEST_F(ServiceWorkerSpeculativeLaunchTest, MouseDown) { | |
| 692 WriteFile( | |
| 693 FILE_PATH_LITERAL("sw.js"), | |
| 694 "self.onfetch = function(e) {" | |
| 695 " e.respondWith(new Response('<title>Done</title>'," | |
| 696 " {headers: {'Content-Type': 'text/html'}}));" | |
| 697 "};"); | |
| 698 WriteTestHtmlFile(); | |
| 699 RunNavigationHintTest(); | |
| 700 // The service worker must be started by a navigation hint. | |
| 701 histogram_tester_.ExpectBucketCount(kNavigationHintLinkMouseDownMetricName, | |
| 702 0 /* SERVICE_WORKER_OK */, 1); | |
| 703 } | |
| 704 | |
| 705 IN_PROC_BROWSER_TEST_F(ServiceWorkerSpeculativeLaunchTest, | |
| 706 NoFetchEventHandler) { | |
| 707 WriteFile(FILE_PATH_LITERAL("sw.js"), "// no fetch event handler."); | |
| 708 WriteFile(FILE_PATH_LITERAL("scope.html"), "<title>Done</title>"); | |
| 709 WriteTestHtmlFile(); | |
| 710 RunNavigationHintTest(); | |
| 711 // The service worker must NOT be started by a navigation hint. | |
| 712 histogram_tester_.ExpectTotalCount(kNavigationHintLinkMouseDownMetricName, 0); | |
| 713 } | |
| 714 | |
| 715 } // namespace | 607 } // namespace |
| OLD | NEW |