| 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" | 15 #include "base/test/histogram_tester.h" |
| 16 #include "build/build_config.h" | 16 #include "build/build_config.h" |
| 17 #include "chrome/browser/chrome_notification_types.h" | 17 #include "chrome/browser/chrome_notification_types.h" |
| 18 #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/profiles/profile.h" | 20 #include "chrome/browser/profiles/profile.h" |
| 19 #include "chrome/browser/ui/browser.h" | 21 #include "chrome/browser/ui/browser.h" |
| 20 #include "chrome/browser/ui/browser_window.h" | 22 #include "chrome/browser/ui/browser_window.h" |
| 21 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 23 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 22 #include "chrome/common/chrome_switches.h" | 24 #include "chrome/common/chrome_switches.h" |
| 23 #include "chrome/test/base/in_process_browser_test.h" | 25 #include "chrome/test/base/in_process_browser_test.h" |
| 24 #include "chrome/test/base/ui_test_utils.h" | 26 #include "chrome/test/base/ui_test_utils.h" |
| 27 #include "components/content_settings/core/browser/host_content_settings_map.h" |
| 25 #include "content/public/browser/browser_context.h" | 28 #include "content/public/browser/browser_context.h" |
| 26 #include "content/public/browser/render_frame_host.h" | 29 #include "content/public/browser/render_frame_host.h" |
| 27 #include "content/public/browser/service_worker_context.h" | 30 #include "content/public/browser/service_worker_context.h" |
| 28 #include "content/public/browser/storage_partition.h" | 31 #include "content/public/browser/storage_partition.h" |
| 29 #include "content/public/browser/web_contents.h" | 32 #include "content/public/browser/web_contents.h" |
| 30 #include "content/public/common/content_features.h" | 33 #include "content/public/common/content_features.h" |
| 31 #include "content/public/common/content_switches.h" | 34 #include "content/public/common/content_switches.h" |
| 32 #include "content/public/test/browser_test_utils.h" | 35 #include "content/public/test/browser_test_utils.h" |
| 33 #include "net/test/embedded_test_server/embedded_test_server.h" | 36 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 34 #include "ppapi/shared_impl/ppapi_switches.h" | 37 #include "ppapi/shared_impl/ppapi_switches.h" |
| 35 | 38 |
| 36 namespace { | 39 namespace { |
| 37 | 40 |
| 38 class ChromeServiceWorkerTest : public InProcessBrowserTest { | 41 class ChromeServiceWorkerTest : public InProcessBrowserTest { |
| 39 protected: | 42 protected: |
| 40 ChromeServiceWorkerTest() { | 43 ChromeServiceWorkerTest() { |
| 41 EXPECT_TRUE(service_worker_dir_.CreateUniqueTempDir()); | 44 EXPECT_TRUE(service_worker_dir_.CreateUniqueTempDir()); |
| 45 EXPECT_TRUE(base::CreateDirectoryAndGetError( |
| 46 service_worker_dir_.GetPath().Append( |
| 47 FILE_PATH_LITERAL("scope")), nullptr)); |
| 42 } | 48 } |
| 43 ~ChromeServiceWorkerTest() override {} | 49 ~ChromeServiceWorkerTest() override {} |
| 44 | 50 |
| 45 void WriteFile(const base::FilePath::StringType& filename, | 51 void WriteFile(const base::FilePath::StringType& filename, |
| 46 base::StringPiece contents) { | 52 base::StringPiece contents) { |
| 47 EXPECT_EQ(base::checked_cast<int>(contents.size()), | 53 EXPECT_EQ(base::checked_cast<int>(contents.size()), |
| 48 base::WriteFile(service_worker_dir_.GetPath().Append(filename), | 54 base::WriteFile(service_worker_dir_.GetPath().Append(filename), |
| 49 contents.data(), contents.size())); | 55 contents.data(), contents.size())); |
| 50 } | 56 } |
| 51 | 57 |
| 52 base::ScopedTempDir service_worker_dir_; | 58 base::ScopedTempDir service_worker_dir_; |
| 53 | 59 |
| 54 private: | 60 private: |
| 55 DISALLOW_COPY_AND_ASSIGN(ChromeServiceWorkerTest); | 61 DISALLOW_COPY_AND_ASSIGN(ChromeServiceWorkerTest); |
| 56 }; | 62 }; |
| 57 | 63 |
| 58 static void ExpectResultAndRun(bool expected, | 64 static void ExpectResultAndRun(bool expected, |
| 59 const base::Closure& continuation, | 65 const base::Closure& continuation, |
| 60 bool actual) { | 66 bool actual) { |
| 61 EXPECT_EQ(expected, actual); | 67 EXPECT_EQ(expected, actual); |
| 62 continuation.Run(); | 68 continuation.Run(); |
| 63 } | 69 } |
| 64 | 70 |
| 65 // http://crbug.com/368570 | 71 // http://crbug.com/368570 |
| 66 IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerTest, | 72 IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerTest, |
| 67 CanShutDownWithRegisteredServiceWorker) { | 73 CanShutDownWithRegisteredServiceWorker) { |
| 68 WriteFile(FILE_PATH_LITERAL("service_worker.js"), ""); | 74 WriteFile(FILE_PATH_LITERAL("service_worker.js"), ""); |
| 69 WriteFile(FILE_PATH_LITERAL("service_worker.js.mock-http-headers"), | |
| 70 "HTTP/1.1 200 OK\nContent-Type: text/javascript"); | |
| 71 | 75 |
| 72 embedded_test_server()->ServeFilesFromDirectory( | 76 embedded_test_server()->ServeFilesFromDirectory( |
| 73 service_worker_dir_.GetPath()); | 77 service_worker_dir_.GetPath()); |
| 74 ASSERT_TRUE(embedded_test_server()->Start()); | 78 ASSERT_TRUE(embedded_test_server()->Start()); |
| 75 | 79 |
| 76 content::ServiceWorkerContext* sw_context = | 80 content::ServiceWorkerContext* sw_context = |
| 77 content::BrowserContext::GetDefaultStoragePartition(browser()->profile()) | 81 content::BrowserContext::GetDefaultStoragePartition(browser()->profile()) |
| 78 ->GetServiceWorkerContext(); | 82 ->GetServiceWorkerContext(); |
| 79 | 83 |
| 80 base::RunLoop run_loop; | 84 base::RunLoop run_loop; |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 118 embedded_test_server()->GetURL("/test.html")); | 122 embedded_test_server()->GetURL("/test.html")); |
| 119 | 123 |
| 120 content::WindowedNotificationObserver observer( | 124 content::WindowedNotificationObserver observer( |
| 121 chrome::NOTIFICATION_BROWSER_CLOSED, content::Source<Browser>(incognito)); | 125 chrome::NOTIFICATION_BROWSER_CLOSED, content::Source<Browser>(incognito)); |
| 122 incognito->window()->Close(); | 126 incognito->window()->Close(); |
| 123 observer.Wait(); | 127 observer.Wait(); |
| 124 | 128 |
| 125 // Test passes if we don't crash. | 129 // Test passes if we don't crash. |
| 126 } | 130 } |
| 127 | 131 |
| 132 IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerTest, |
| 133 FailRegisterServiceWorkerWhenJSDisabled) { |
| 134 WriteFile(FILE_PATH_LITERAL("service_worker.js"), ""); |
| 135 |
| 136 embedded_test_server()->ServeFilesFromDirectory( |
| 137 service_worker_dir_.GetPath()); |
| 138 ASSERT_TRUE(embedded_test_server()->Start()); |
| 139 |
| 140 HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 141 ->SetDefaultContentSetting(CONTENT_SETTINGS_TYPE_JAVASCRIPT, |
| 142 CONTENT_SETTING_BLOCK); |
| 143 |
| 144 content::ServiceWorkerContext* sw_context = |
| 145 content::BrowserContext::GetDefaultStoragePartition(browser()->profile()) |
| 146 ->GetServiceWorkerContext(); |
| 147 |
| 148 base::RunLoop run_loop; |
| 149 sw_context->RegisterServiceWorker( |
| 150 embedded_test_server()->GetURL("/"), |
| 151 embedded_test_server()->GetURL("/service_worker.js"), |
| 152 base::Bind(&ExpectResultAndRun, false, run_loop.QuitClosure())); |
| 153 run_loop.Run(); |
| 154 } |
| 155 |
| 156 IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerTest, |
| 157 FallbackMainResourceRequestWhenJSDisabled) { |
| 158 WriteFile( |
| 159 FILE_PATH_LITERAL("sw.js"), |
| 160 "self.onfetch = function(e) {" |
| 161 " e.respondWith(new Response('<title>Fail</title>'," |
| 162 " {headers: {'Content-Type': 'text/html'}}));" |
| 163 "};"); |
| 164 WriteFile(FILE_PATH_LITERAL("scope/done.html"), "<title>Done</title>"); |
| 165 WriteFile( |
| 166 FILE_PATH_LITERAL("test.html"), |
| 167 "<script>" |
| 168 "navigator.serviceWorker.register('./sw.js', {scope: './scope/'})" |
| 169 " .then(function(reg) {" |
| 170 " reg.addEventListener('updatefound', function() {" |
| 171 " var worker = reg.installing;" |
| 172 " worker.addEventListener('statechange', function() {" |
| 173 " if (worker.state == 'activated')" |
| 174 " document.title = 'READY';" |
| 175 " });" |
| 176 " });" |
| 177 " });" |
| 178 "</script>"); |
| 179 embedded_test_server()->ServeFilesFromDirectory( |
| 180 service_worker_dir_.GetPath()); |
| 181 ASSERT_TRUE(embedded_test_server()->Start()); |
| 182 |
| 183 const base::string16 expected_title1 = base::ASCIIToUTF16("READY"); |
| 184 content::TitleWatcher title_watcher1( |
| 185 browser()->tab_strip_model()->GetActiveWebContents(), expected_title1); |
| 186 ui_test_utils::NavigateToURL(browser(), |
| 187 embedded_test_server()->GetURL("/test.html")); |
| 188 EXPECT_EQ(expected_title1, title_watcher1.WaitAndGetTitle()); |
| 189 |
| 190 content::ServiceWorkerContext* sw_context = |
| 191 content::BrowserContext::GetDefaultStoragePartition(browser()->profile()) |
| 192 ->GetServiceWorkerContext(); |
| 193 sw_context->StopAllServiceWorkersForOrigin( |
| 194 embedded_test_server()->base_url()); |
| 195 |
| 196 HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 197 ->SetDefaultContentSetting(CONTENT_SETTINGS_TYPE_JAVASCRIPT, |
| 198 CONTENT_SETTING_BLOCK); |
| 199 |
| 200 const base::string16 expected_title2 = base::ASCIIToUTF16("Done"); |
| 201 content::TitleWatcher title_watcher2( |
| 202 browser()->tab_strip_model()->GetActiveWebContents(), expected_title2); |
| 203 ui_test_utils::NavigateToURL( |
| 204 browser(), |
| 205 embedded_test_server()->GetURL("/scope/done.html")); |
| 206 EXPECT_EQ(expected_title2, title_watcher2.WaitAndGetTitle()); |
| 207 |
| 208 content::WebContents* web_contents = |
| 209 browser()->tab_strip_model()->GetActiveWebContents(); |
| 210 EXPECT_TRUE(TabSpecificContentSettings::FromWebContents(web_contents)-> |
| 211 IsContentBlocked(CONTENT_SETTINGS_TYPE_JAVASCRIPT)); |
| 212 } |
| 213 |
| 128 class ChromeServiceWorkerFetchTest : public ChromeServiceWorkerTest { | 214 class ChromeServiceWorkerFetchTest : public ChromeServiceWorkerTest { |
| 129 protected: | 215 protected: |
| 130 ChromeServiceWorkerFetchTest() {} | 216 ChromeServiceWorkerFetchTest() {} |
| 131 ~ChromeServiceWorkerFetchTest() override {} | 217 ~ChromeServiceWorkerFetchTest() override {} |
| 132 | 218 |
| 133 void SetUpOnMainThread() override { | 219 void SetUpOnMainThread() override { |
| 134 WriteServiceWorkerFetchTestFiles(); | 220 WriteServiceWorkerFetchTestFiles(); |
| 135 embedded_test_server()->ServeFilesFromDirectory( | 221 embedded_test_server()->ServeFilesFromDirectory( |
| 136 service_worker_dir_.GetPath()); | 222 service_worker_dir_.GetPath()); |
| 137 ASSERT_TRUE(embedded_test_server()->Start()); | 223 ASSERT_TRUE(embedded_test_server()->Start()); |
| (...skipping 460 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 598 EXPECT_EQ(expected_title2, title_watcher2.WaitAndGetTitle()); | 684 EXPECT_EQ(expected_title2, title_watcher2.WaitAndGetTitle()); |
| 599 | 685 |
| 600 // The service worker must be started by a navigation hint. | 686 // The service worker must be started by a navigation hint. |
| 601 histogram_tester_.ExpectBucketCount( | 687 histogram_tester_.ExpectBucketCount( |
| 602 "ServiceWorker.StartWorker.StatusByPurpose_NAVIGATION_HINT_LINK_MOUSE_" | 688 "ServiceWorker.StartWorker.StatusByPurpose_NAVIGATION_HINT_LINK_MOUSE_" |
| 603 "DOWN", | 689 "DOWN", |
| 604 0 /* SERVICE_WORKER_OK */, 1); | 690 0 /* SERVICE_WORKER_OK */, 1); |
| 605 } | 691 } |
| 606 | 692 |
| 607 } // namespace | 693 } // namespace |
| OLD | NEW |