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("scope"), nullptr)); | |
42 } | 47 } |
43 ~ChromeServiceWorkerTest() override {} | 48 ~ChromeServiceWorkerTest() override {} |
44 | 49 |
45 void WriteFile(const base::FilePath::StringType& filename, | 50 void WriteFile(const base::FilePath::StringType& filename, |
46 base::StringPiece contents) { | 51 base::StringPiece contents) { |
47 EXPECT_EQ(base::checked_cast<int>(contents.size()), | 52 EXPECT_EQ(base::checked_cast<int>(contents.size()), |
48 base::WriteFile(service_worker_dir_.GetPath().Append(filename), | 53 base::WriteFile(service_worker_dir_.GetPath().Append(filename), |
49 contents.data(), contents.size())); | 54 contents.data(), contents.size())); |
50 } | 55 } |
51 | 56 |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
118 embedded_test_server()->GetURL("/test.html")); | 123 embedded_test_server()->GetURL("/test.html")); |
119 | 124 |
120 content::WindowedNotificationObserver observer( | 125 content::WindowedNotificationObserver observer( |
121 chrome::NOTIFICATION_BROWSER_CLOSED, content::Source<Browser>(incognito)); | 126 chrome::NOTIFICATION_BROWSER_CLOSED, content::Source<Browser>(incognito)); |
122 incognito->window()->Close(); | 127 incognito->window()->Close(); |
123 observer.Wait(); | 128 observer.Wait(); |
124 | 129 |
125 // Test passes if we don't crash. | 130 // Test passes if we don't crash. |
126 } | 131 } |
127 | 132 |
133 IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerTest, | |
134 FailRegisterServiceWorkerWhenJSDisabled) { | |
135 WriteFile(FILE_PATH_LITERAL("service_worker.js"), ""); | |
136 WriteFile(FILE_PATH_LITERAL("service_worker.js.mock-http-headers"), | |
137 "HTTP/1.1 200 OK\nContent-Type: text/javascript"); | |
138 | |
139 embedded_test_server()->ServeFilesFromDirectory( | |
140 service_worker_dir_.GetPath()); | |
141 ASSERT_TRUE(embedded_test_server()->Start()); | |
142 | |
143 HostContentSettingsMapFactory::GetForProfile(browser()->profile()) | |
144 ->SetDefaultContentSetting(CONTENT_SETTINGS_TYPE_JAVASCRIPT, | |
145 CONTENT_SETTING_BLOCK); | |
146 | |
147 content::ServiceWorkerContext* sw_context = | |
148 content::BrowserContext::GetDefaultStoragePartition(browser()->profile()) | |
149 ->GetServiceWorkerContext(); | |
150 | |
151 base::RunLoop run_loop; | |
152 sw_context->RegisterServiceWorker( | |
153 embedded_test_server()->GetURL("/"), | |
154 embedded_test_server()->GetURL("/service_worker.js"), | |
155 base::Bind(&ExpectResultAndRun, false, run_loop.QuitClosure())); | |
156 run_loop.Run(); | |
157 } | |
158 | |
159 IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerTest, | |
160 FallbackMainResourceRequestWhenJSDisabled) { | |
161 WriteFile( | |
162 FILE_PATH_LITERAL("sw.js"), | |
163 "self.onfetch = function(e) {" | |
164 " e.respondWith(new Response('<title>Fail</title>'," | |
165 " {headers: {'Content-Type': 'text/html'}}));" | |
166 "};"); | |
167 WriteFile(FILE_PATH_LITERAL("scope/done.html"), "<title>Done</title>"); | |
168 WriteFile( | |
169 FILE_PATH_LITERAL("test.html"), | |
170 "<script>" | |
171 "navigator.serviceWorker.register('./sw.js', {scope: './scope/'})" | |
172 " .then(function(reg) {" | |
173 " reg.addEventListener('updatefound', function() {" | |
174 " var worker = reg.installing;" | |
175 " worker.addEventListener('statechange', function() {" | |
176 " if (worker.state == 'activated')" | |
177 " document.title = 'READY';" | |
178 " });" | |
179 " });" | |
180 " });" | |
181 "</script>"); | |
182 embedded_test_server()->ServeFilesFromDirectory( | |
183 service_worker_dir_.GetPath()); | |
184 ASSERT_TRUE(embedded_test_server()->Start()); | |
185 | |
186 const base::string16 expected_title1 = base::ASCIIToUTF16("READY"); | |
187 content::TitleWatcher title_watcher1( | |
188 browser()->tab_strip_model()->GetActiveWebContents(), expected_title1); | |
189 ui_test_utils::NavigateToURL(browser(), | |
190 embedded_test_server()->GetURL("/test.html")); | |
191 EXPECT_EQ(expected_title1, title_watcher1.WaitAndGetTitle()); | |
192 | |
193 content::ServiceWorkerContext* sw_context = | |
194 content::BrowserContext::GetDefaultStoragePartition(browser()->profile()) | |
195 ->GetServiceWorkerContext(); | |
196 sw_context->StopAllServiceWorkersForOrigin( | |
197 embedded_test_server()->base_url()); | |
198 | |
199 HostContentSettingsMapFactory::GetForProfile(browser()->profile()) | |
200 ->SetDefaultContentSetting(CONTENT_SETTINGS_TYPE_JAVASCRIPT, | |
201 CONTENT_SETTING_BLOCK); | |
202 | |
203 const base::string16 expected_title2 = base::ASCIIToUTF16("Done"); | |
204 content::TitleWatcher title_watcher2( | |
205 browser()->tab_strip_model()->GetActiveWebContents(), expected_title2); | |
206 ui_test_utils::NavigateToURL( | |
207 browser(), | |
208 embedded_test_server()->GetURL("/scope/done.html")); | |
209 EXPECT_EQ(expected_title2, title_watcher2.WaitAndGetTitle()); | |
jochen (gone - plz use gerrit)
2016/09/29 12:15:49
you could check that the tab's tab_specific_conten
shimazu
2016/10/04 09:21:48
Done.
| |
210 } | |
211 | |
128 class ChromeServiceWorkerFetchTest : public ChromeServiceWorkerTest { | 212 class ChromeServiceWorkerFetchTest : public ChromeServiceWorkerTest { |
129 protected: | 213 protected: |
130 ChromeServiceWorkerFetchTest() {} | 214 ChromeServiceWorkerFetchTest() {} |
131 ~ChromeServiceWorkerFetchTest() override {} | 215 ~ChromeServiceWorkerFetchTest() override {} |
132 | 216 |
133 void SetUpOnMainThread() override { | 217 void SetUpOnMainThread() override { |
134 WriteServiceWorkerFetchTestFiles(); | 218 WriteServiceWorkerFetchTestFiles(); |
135 embedded_test_server()->ServeFilesFromDirectory( | 219 embedded_test_server()->ServeFilesFromDirectory( |
136 service_worker_dir_.GetPath()); | 220 service_worker_dir_.GetPath()); |
137 ASSERT_TRUE(embedded_test_server()->Start()); | 221 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()); | 682 EXPECT_EQ(expected_title2, title_watcher2.WaitAndGetTitle()); |
599 | 683 |
600 // The service worker must be started by a navigation hint. | 684 // The service worker must be started by a navigation hint. |
601 histogram_tester_.ExpectBucketCount( | 685 histogram_tester_.ExpectBucketCount( |
602 "ServiceWorker.StartWorker.StatusByPurpose_NAVIGATION_HINT_LINK_MOUSE_" | 686 "ServiceWorker.StartWorker.StatusByPurpose_NAVIGATION_HINT_LINK_MOUSE_" |
603 "DOWN", | 687 "DOWN", |
604 0 /* SERVICE_WORKER_OK */, 1); | 688 0 /* SERVICE_WORKER_OK */, 1); |
605 } | 689 } |
606 | 690 |
607 } // namespace | 691 } // namespace |
OLD | NEW |