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 16 matching lines...) Expand all Loading... |
27 #include "chrome/test/base/ui_test_utils.h" | 27 #include "chrome/test/base/ui_test_utils.h" |
28 #include "components/content_settings/core/browser/host_content_settings_map.h" | 28 #include "components/content_settings/core/browser/host_content_settings_map.h" |
29 #include "content/public/browser/browser_context.h" | 29 #include "content/public/browser/browser_context.h" |
30 #include "content/public/browser/render_frame_host.h" | 30 #include "content/public/browser/render_frame_host.h" |
31 #include "content/public/browser/service_worker_context.h" | 31 #include "content/public/browser/service_worker_context.h" |
32 #include "content/public/browser/storage_partition.h" | 32 #include "content/public/browser/storage_partition.h" |
33 #include "content/public/browser/web_contents.h" | 33 #include "content/public/browser/web_contents.h" |
34 #include "content/public/test/browser_test_utils.h" | 34 #include "content/public/test/browser_test_utils.h" |
35 #include "net/test/embedded_test_server/embedded_test_server.h" | 35 #include "net/test/embedded_test_server/embedded_test_server.h" |
36 #include "ppapi/shared_impl/ppapi_switches.h" | 36 #include "ppapi/shared_impl/ppapi_switches.h" |
| 37 #include "third_party/WebKit/public/platform/modules/serviceworker/WebServiceWor
kerUpdateViaCache.h" |
37 | 38 |
38 namespace { | 39 namespace { |
39 | 40 |
40 const std::string kInstallAndWaitForActivatedPage = | 41 const std::string kInstallAndWaitForActivatedPage = |
41 "<script>" | 42 "<script>" |
42 "navigator.serviceWorker.register('./sw.js', {scope: './scope/'})" | 43 "navigator.serviceWorker.register('./sw.js', {scope: './scope/'})" |
43 " .then(function(reg) {" | 44 " .then(function(reg) {" |
44 " reg.addEventListener('updatefound', function() {" | 45 " reg.addEventListener('updatefound', function() {" |
45 " var worker = reg.installing;" | 46 " var worker = reg.installing;" |
46 " worker.addEventListener('statechange', function() {" | 47 " worker.addEventListener('statechange', function() {" |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 WriteFile(FILE_PATH_LITERAL("service_worker.js"), ""); | 111 WriteFile(FILE_PATH_LITERAL("service_worker.js"), ""); |
111 | 112 |
112 embedded_test_server()->ServeFilesFromDirectory( | 113 embedded_test_server()->ServeFilesFromDirectory( |
113 service_worker_dir_.GetPath()); | 114 service_worker_dir_.GetPath()); |
114 ASSERT_TRUE(embedded_test_server()->Start()); | 115 ASSERT_TRUE(embedded_test_server()->Start()); |
115 | 116 |
116 base::RunLoop run_loop; | 117 base::RunLoop run_loop; |
117 GetServiceWorkerContext()->RegisterServiceWorker( | 118 GetServiceWorkerContext()->RegisterServiceWorker( |
118 embedded_test_server()->GetURL("/"), | 119 embedded_test_server()->GetURL("/"), |
119 embedded_test_server()->GetURL("/service_worker.js"), | 120 embedded_test_server()->GetURL("/service_worker.js"), |
| 121 blink::WebServiceWorkerUpdateViaCache::kImports, |
120 base::Bind(&ExpectResultAndRun<bool>, true, run_loop.QuitClosure())); | 122 base::Bind(&ExpectResultAndRun<bool>, true, run_loop.QuitClosure())); |
121 run_loop.Run(); | 123 run_loop.Run(); |
122 | 124 |
123 // Leave the Service Worker registered, and make sure that the browser can | 125 // Leave the Service Worker registered, and make sure that the browser can |
124 // shut down without DCHECK'ing. It'd be nice to check here that the SW is | 126 // shut down without DCHECK'ing. It'd be nice to check here that the SW is |
125 // actually occupying a process, but we don't yet have the public interface to | 127 // actually occupying a process, but we don't yet have the public interface to |
126 // do that. | 128 // do that. |
127 } | 129 } |
128 | 130 |
129 // http://crbug.com/419290 | 131 // http://crbug.com/419290 |
130 IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerTest, | 132 IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerTest, |
131 CanCloseIncognitoWindowWithServiceWorkerController) { | 133 CanCloseIncognitoWindowWithServiceWorkerController) { |
132 WriteFile(FILE_PATH_LITERAL("service_worker.js"), ""); | 134 WriteFile(FILE_PATH_LITERAL("service_worker.js"), ""); |
133 WriteFile(FILE_PATH_LITERAL("service_worker.js.mock-http-headers"), | 135 WriteFile(FILE_PATH_LITERAL("service_worker.js.mock-http-headers"), |
134 "HTTP/1.1 200 OK\nContent-Type: text/javascript"); | 136 "HTTP/1.1 200 OK\nContent-Type: text/javascript"); |
135 WriteFile(FILE_PATH_LITERAL("test.html"), ""); | 137 WriteFile(FILE_PATH_LITERAL("test.html"), ""); |
136 InitializeServer(); | 138 InitializeServer(); |
137 | 139 |
138 Browser* incognito = CreateIncognitoBrowser(); | 140 Browser* incognito = CreateIncognitoBrowser(); |
139 | 141 |
140 base::RunLoop run_loop; | 142 base::RunLoop run_loop; |
141 GetServiceWorkerContext()->RegisterServiceWorker( | 143 GetServiceWorkerContext()->RegisterServiceWorker( |
142 embedded_test_server()->GetURL("/"), | 144 embedded_test_server()->GetURL("/"), |
143 embedded_test_server()->GetURL("/service_worker.js"), | 145 embedded_test_server()->GetURL("/service_worker.js"), |
| 146 blink::WebServiceWorkerUpdateViaCache::kImports, |
144 base::Bind(&ExpectResultAndRun<bool>, true, run_loop.QuitClosure())); | 147 base::Bind(&ExpectResultAndRun<bool>, true, run_loop.QuitClosure())); |
145 run_loop.Run(); | 148 run_loop.Run(); |
146 | 149 |
147 ui_test_utils::NavigateToURL(incognito, | 150 ui_test_utils::NavigateToURL(incognito, |
148 embedded_test_server()->GetURL("/test.html")); | 151 embedded_test_server()->GetURL("/test.html")); |
149 | 152 |
150 content::WindowedNotificationObserver observer( | 153 content::WindowedNotificationObserver observer( |
151 chrome::NOTIFICATION_BROWSER_CLOSED, content::Source<Browser>(incognito)); | 154 chrome::NOTIFICATION_BROWSER_CLOSED, content::Source<Browser>(incognito)); |
152 incognito->window()->Close(); | 155 incognito->window()->Close(); |
153 observer.Wait(); | 156 observer.Wait(); |
154 | 157 |
155 // Test passes if we don't crash. | 158 // Test passes if we don't crash. |
156 } | 159 } |
157 | 160 |
158 IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerTest, | 161 IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerTest, |
159 FailRegisterServiceWorkerWhenJSDisabled) { | 162 FailRegisterServiceWorkerWhenJSDisabled) { |
160 WriteFile(FILE_PATH_LITERAL("service_worker.js"), ""); | 163 WriteFile(FILE_PATH_LITERAL("service_worker.js"), ""); |
161 InitializeServer(); | 164 InitializeServer(); |
162 | 165 |
163 HostContentSettingsMapFactory::GetForProfile(browser()->profile()) | 166 HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
164 ->SetDefaultContentSetting(CONTENT_SETTINGS_TYPE_JAVASCRIPT, | 167 ->SetDefaultContentSetting(CONTENT_SETTINGS_TYPE_JAVASCRIPT, |
165 CONTENT_SETTING_BLOCK); | 168 CONTENT_SETTING_BLOCK); |
166 | 169 |
167 base::RunLoop run_loop; | 170 base::RunLoop run_loop; |
168 GetServiceWorkerContext()->RegisterServiceWorker( | 171 GetServiceWorkerContext()->RegisterServiceWorker( |
169 embedded_test_server()->GetURL("/"), | 172 embedded_test_server()->GetURL("/"), |
170 embedded_test_server()->GetURL("/service_worker.js"), | 173 embedded_test_server()->GetURL("/service_worker.js"), |
| 174 blink::WebServiceWorkerUpdateViaCache::kImports, |
171 base::Bind(&ExpectResultAndRun<bool>, false, run_loop.QuitClosure())); | 175 base::Bind(&ExpectResultAndRun<bool>, false, run_loop.QuitClosure())); |
172 run_loop.Run(); | 176 run_loop.Run(); |
173 } | 177 } |
174 | 178 |
175 IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerTest, | 179 IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerTest, |
176 FallbackMainResourceRequestWhenJSDisabled) { | 180 FallbackMainResourceRequestWhenJSDisabled) { |
177 WriteFile( | 181 WriteFile( |
178 FILE_PATH_LITERAL("sw.js"), | 182 FILE_PATH_LITERAL("sw.js"), |
179 "self.onfetch = function(e) {" | 183 "self.onfetch = function(e) {" |
180 " e.respondWith(new Response('<title>Fail</title>'," | 184 " e.respondWith(new Response('<title>Fail</title>'," |
(...skipping 492 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
673 WriteFile(FILE_PATH_LITERAL("sw.js"), | 677 WriteFile(FILE_PATH_LITERAL("sw.js"), |
674 "self.oninstall = function(e) {\n" | 678 "self.oninstall = function(e) {\n" |
675 " e.waitUntil(new Promise(r => { /* never resolve */ }));\n" | 679 " e.waitUntil(new Promise(r => { /* never resolve */ }));\n" |
676 " };\n" | 680 " };\n" |
677 "self.onfetch = function(e) {};"); | 681 "self.onfetch = function(e) {};"); |
678 InitializeServer(); | 682 InitializeServer(); |
679 base::RunLoop run_loop; | 683 base::RunLoop run_loop; |
680 GetServiceWorkerContext()->RegisterServiceWorker( | 684 GetServiceWorkerContext()->RegisterServiceWorker( |
681 embedded_test_server()->GetURL("/scope/"), | 685 embedded_test_server()->GetURL("/scope/"), |
682 embedded_test_server()->GetURL("/sw.js"), | 686 embedded_test_server()->GetURL("/sw.js"), |
| 687 blink::WebServiceWorkerUpdateViaCache::kImports, |
683 base::Bind(&ExpectResultAndRun<bool>, true, run_loop.QuitClosure())); | 688 base::Bind(&ExpectResultAndRun<bool>, true, run_loop.QuitClosure())); |
684 run_loop.Run(); | 689 run_loop.Run(); |
685 RunNavigationHintTest("/scope/", | 690 RunNavigationHintTest("/scope/", |
686 content::StartServiceWorkerForNavigationHintResult:: | 691 content::StartServiceWorkerForNavigationHintResult:: |
687 NO_ACTIVE_SERVICE_WORKER_VERSION, | 692 NO_ACTIVE_SERVICE_WORKER_VERSION, |
688 false); | 693 false); |
689 } | 694 } |
690 | 695 |
691 IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerNavigationHintTest, NoFetchHandler) { | 696 IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerNavigationHintTest, NoFetchHandler) { |
692 WriteFile(FILE_PATH_LITERAL("sw.js"), "/* empty */"); | 697 WriteFile(FILE_PATH_LITERAL("sw.js"), "/* empty */"); |
693 WriteFile(FILE_PATH_LITERAL("test.html"), kInstallAndWaitForActivatedPage); | 698 WriteFile(FILE_PATH_LITERAL("test.html"), kInstallAndWaitForActivatedPage); |
694 InitializeServer(); | 699 InitializeServer(); |
695 NavigateToPageAndWaitForReadyTitle("/test.html"); | 700 NavigateToPageAndWaitForReadyTitle("/test.html"); |
696 GetServiceWorkerContext()->StopAllServiceWorkersForOrigin( | 701 GetServiceWorkerContext()->StopAllServiceWorkersForOrigin( |
697 embedded_test_server()->base_url()); | 702 embedded_test_server()->base_url()); |
698 RunNavigationHintTest( | 703 RunNavigationHintTest( |
699 "/scope/", | 704 "/scope/", |
700 content::StartServiceWorkerForNavigationHintResult::NO_FETCH_HANDLER, | 705 content::StartServiceWorkerForNavigationHintResult::NO_FETCH_HANDLER, |
701 false); | 706 false); |
702 } | 707 } |
703 | 708 |
704 } // namespace | 709 } // namespace |
OLD | NEW |