Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(38)

Side by Side Diff: chrome/browser/chrome_service_worker_browsertest.cc

Issue 2377603002: Disable ServiceWorker when JS is disabled (Closed)
Patch Set: wrap by FILE_PATH_LITERAL Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
118 embedded_test_server()->GetURL("/test.html")); 124 embedded_test_server()->GetURL("/test.html"));
119 125
120 content::WindowedNotificationObserver observer( 126 content::WindowedNotificationObserver observer(
121 chrome::NOTIFICATION_BROWSER_CLOSED, content::Source<Browser>(incognito)); 127 chrome::NOTIFICATION_BROWSER_CLOSED, content::Source<Browser>(incognito));
122 incognito->window()->Close(); 128 incognito->window()->Close();
123 observer.Wait(); 129 observer.Wait();
124 130
125 // Test passes if we don't crash. 131 // Test passes if we don't crash.
126 } 132 }
127 133
134 IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerTest,
135 FailRegisterServiceWorkerWhenJSDisabled) {
136 WriteFile(FILE_PATH_LITERAL("service_worker.js"), "");
137 WriteFile(FILE_PATH_LITERAL("service_worker.js.mock-http-headers"),
138 "HTTP/1.1 200 OK\nContent-Type: text/javascript");
falken 2016/10/05 01:16:50 Apparently we don't need these mock headers: https
shimazu 2016/10/05 02:10:57 Done.
139
140 embedded_test_server()->ServeFilesFromDirectory(
141 service_worker_dir_.GetPath());
142 ASSERT_TRUE(embedded_test_server()->Start());
143
144 HostContentSettingsMapFactory::GetForProfile(browser()->profile())
145 ->SetDefaultContentSetting(CONTENT_SETTINGS_TYPE_JAVASCRIPT,
146 CONTENT_SETTING_BLOCK);
147
148 content::ServiceWorkerContext* sw_context =
149 content::BrowserContext::GetDefaultStoragePartition(browser()->profile())
150 ->GetServiceWorkerContext();
151
152 base::RunLoop run_loop;
153 sw_context->RegisterServiceWorker(
154 embedded_test_server()->GetURL("/"),
155 embedded_test_server()->GetURL("/service_worker.js"),
156 base::Bind(&ExpectResultAndRun, false, run_loop.QuitClosure()));
157 run_loop.Run();
158 }
159
160 IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerTest,
161 FallbackMainResourceRequestWhenJSDisabled) {
162 WriteFile(
163 FILE_PATH_LITERAL("sw.js"),
164 "self.onfetch = function(e) {"
165 " e.respondWith(new Response('<title>Fail</title>',"
166 " {headers: {'Content-Type': 'text/html'}}));"
167 "};");
168 WriteFile(FILE_PATH_LITERAL("scope/done.html"), "<title>Done</title>");
169 WriteFile(
170 FILE_PATH_LITERAL("test.html"),
171 "<script>"
172 "navigator.serviceWorker.register('./sw.js', {scope: './scope/'})"
173 " .then(function(reg) {"
174 " reg.addEventListener('updatefound', function() {"
175 " var worker = reg.installing;"
176 " worker.addEventListener('statechange', function() {"
177 " if (worker.state == 'activated')"
178 " document.title = 'READY';"
179 " });"
180 " });"
181 " });"
182 "</script>");
183 embedded_test_server()->ServeFilesFromDirectory(
184 service_worker_dir_.GetPath());
185 ASSERT_TRUE(embedded_test_server()->Start());
186
187 const base::string16 expected_title1 = base::ASCIIToUTF16("READY");
188 content::TitleWatcher title_watcher1(
189 browser()->tab_strip_model()->GetActiveWebContents(), expected_title1);
190 ui_test_utils::NavigateToURL(browser(),
191 embedded_test_server()->GetURL("/test.html"));
192 EXPECT_EQ(expected_title1, title_watcher1.WaitAndGetTitle());
193
194 content::ServiceWorkerContext* sw_context =
195 content::BrowserContext::GetDefaultStoragePartition(browser()->profile())
196 ->GetServiceWorkerContext();
197 sw_context->StopAllServiceWorkersForOrigin(
198 embedded_test_server()->base_url());
199
200 HostContentSettingsMapFactory::GetForProfile(browser()->profile())
201 ->SetDefaultContentSetting(CONTENT_SETTINGS_TYPE_JAVASCRIPT,
202 CONTENT_SETTING_BLOCK);
203
204 const base::string16 expected_title2 = base::ASCIIToUTF16("Done");
205 content::TitleWatcher title_watcher2(
206 browser()->tab_strip_model()->GetActiveWebContents(), expected_title2);
207 ui_test_utils::NavigateToURL(
208 browser(),
209 embedded_test_server()->GetURL("/scope/done.html"));
210 EXPECT_EQ(expected_title2, title_watcher2.WaitAndGetTitle());
211
212 content::WebContents* web_contents =
213 browser()->tab_strip_model()->GetActiveWebContents();
214 EXPECT_TRUE(TabSpecificContentSettings::FromWebContents(web_contents)->
215 IsContentBlocked(CONTENT_SETTINGS_TYPE_JAVASCRIPT));
216 }
217
128 class ChromeServiceWorkerFetchTest : public ChromeServiceWorkerTest { 218 class ChromeServiceWorkerFetchTest : public ChromeServiceWorkerTest {
129 protected: 219 protected:
130 ChromeServiceWorkerFetchTest() {} 220 ChromeServiceWorkerFetchTest() {}
131 ~ChromeServiceWorkerFetchTest() override {} 221 ~ChromeServiceWorkerFetchTest() override {}
132 222
133 void SetUpOnMainThread() override { 223 void SetUpOnMainThread() override {
134 WriteServiceWorkerFetchTestFiles(); 224 WriteServiceWorkerFetchTestFiles();
135 embedded_test_server()->ServeFilesFromDirectory( 225 embedded_test_server()->ServeFilesFromDirectory(
136 service_worker_dir_.GetPath()); 226 service_worker_dir_.GetPath());
137 ASSERT_TRUE(embedded_test_server()->Start()); 227 ASSERT_TRUE(embedded_test_server()->Start());
(...skipping 460 matching lines...) Expand 10 before | Expand all | Expand 10 after
598 EXPECT_EQ(expected_title2, title_watcher2.WaitAndGetTitle()); 688 EXPECT_EQ(expected_title2, title_watcher2.WaitAndGetTitle());
599 689
600 // The service worker must be started by a navigation hint. 690 // The service worker must be started by a navigation hint.
601 histogram_tester_.ExpectBucketCount( 691 histogram_tester_.ExpectBucketCount(
602 "ServiceWorker.StartWorker.StatusByPurpose_NAVIGATION_HINT_LINK_MOUSE_" 692 "ServiceWorker.StartWorker.StatusByPurpose_NAVIGATION_HINT_LINK_MOUSE_"
603 "DOWN", 693 "DOWN",
604 0 /* SERVICE_WORKER_OK */, 1); 694 0 /* SERVICE_WORKER_OK */, 1);
605 } 695 }
606 696
607 } // namespace 697 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698