blob: 1dbd2f3494e1c6ff462c7a3571a821440fd28a49 [file] [log] [blame]
annekao38685502015-07-14 17:46:391// Copyright 2015 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
avia2f4804a2015-12-24 23:11:135#include <stdint.h>
6
kalman6f984ae2015-09-18 17:21:587#include "base/bind_helpers.h"
avia2f4804a2015-12-24 23:11:138#include "base/macros.h"
kalman6f984ae2015-09-18 17:21:589#include "base/strings/stringprintf.h"
horo1eeddde2015-11-19 05:59:2510#include "base/strings/utf_string_conversions.h"
annekao38685502015-07-14 17:46:3911#include "chrome/browser/extensions/extension_apitest.h"
rdevlin.croninf5863da2015-09-10 19:21:4512#include "chrome/browser/extensions/extension_service.h"
lazyboy561b7de2015-11-19 19:27:3013#include "chrome/browser/notifications/desktop_notification_profile_util.h"
14#include "chrome/browser/push_messaging/push_messaging_app_identifier.h"
15#include "chrome/browser/push_messaging/push_messaging_service_factory.h"
16#include "chrome/browser/push_messaging/push_messaging_service_impl.h"
17#include "chrome/browser/services/gcm/fake_gcm_profile_service.h"
18#include "chrome/browser/services/gcm/gcm_profile_service_factory.h"
annekao1db36fd2015-07-29 17:09:1619#include "chrome/browser/ui/tabs/tab_strip_model.h"
rdevlin.croninf5863da2015-09-10 19:21:4520#include "chrome/test/base/ui_test_utils.h"
sdefresne9fb67692015-08-03 18:48:2221#include "components/version_info/version_info.h"
kalman6f984ae2015-09-18 17:21:5822#include "content/public/browser/navigation_controller.h"
rdevlin.croninf5863da2015-09-10 19:21:4523#include "content/public/browser/navigation_entry.h"
kalman6f984ae2015-09-18 17:21:5824#include "content/public/browser/web_contents.h"
lazyboybd325ae2015-11-18 21:35:2625#include "content/public/common/content_switches.h"
kalman6f984ae2015-09-18 17:21:5826#include "content/public/common/page_type.h"
lazyboybd325ae2015-11-18 21:35:2627#include "content/public/test/background_sync_test_util.h"
annekao1db36fd2015-07-29 17:09:1628#include "content/public/test/browser_test_utils.h"
kalman6f984ae2015-09-18 17:21:5829#include "extensions/browser/extension_host.h"
lazyboyc3e763a2015-12-09 23:09:5830#include "extensions/browser/extension_registry.h"
kalman6f984ae2015-09-18 17:21:5831#include "extensions/browser/process_manager.h"
32#include "extensions/test/background_page_watcher.h"
annekao38685502015-07-14 17:46:3933#include "extensions/test/extension_test_message_listener.h"
horo1eeddde2015-11-19 05:59:2534#include "net/test/embedded_test_server/embedded_test_server.h"
annekao38685502015-07-14 17:46:3935
36namespace extensions {
37
kalman6f984ae2015-09-18 17:21:5838namespace {
39
40// Pass into ServiceWorkerTest::StartTestFromBackgroundPage to indicate that
41// registration is expected to succeed.
42std::string* const kExpectSuccess = nullptr;
43
44void DoNothingWithBool(bool b) {}
45
lazyboy22eddc712015-12-10 21:16:2646// Returns the newly added WebContents.
47content::WebContents* AddTab(Browser* browser, const GURL& url) {
48 int starting_tab_count = browser->tab_strip_model()->count();
49 ui_test_utils::NavigateToURLWithDisposition(
50 browser, url, NEW_FOREGROUND_TAB,
51 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
52 int tab_count = browser->tab_strip_model()->count();
53 EXPECT_EQ(starting_tab_count + 1, tab_count);
54 return browser->tab_strip_model()->GetActiveWebContents();
55}
56
57class WebContentsLoadStopObserver : content::WebContentsObserver {
58 public:
59 explicit WebContentsLoadStopObserver(content::WebContents* web_contents)
60 : content::WebContentsObserver(web_contents),
61 load_stop_observed_(false) {}
62
63 void WaitForLoadStop() {
64 if (load_stop_observed_)
65 return;
66 message_loop_runner_ = new content::MessageLoopRunner;
67 message_loop_runner_->Run();
68 }
69
70 private:
71 void DidStopLoading() override {
72 load_stop_observed_ = true;
73 if (message_loop_runner_)
74 message_loop_runner_->Quit();
75 }
76
77 bool load_stop_observed_;
78 scoped_refptr<content::MessageLoopRunner> message_loop_runner_;
79
80 DISALLOW_COPY_AND_ASSIGN(WebContentsLoadStopObserver);
81};
82
kalman6f984ae2015-09-18 17:21:5883} // namespace
84
annekao38685502015-07-14 17:46:3985class ServiceWorkerTest : public ExtensionApiTest {
86 public:
kalman6f984ae2015-09-18 17:21:5887 ServiceWorkerTest() : current_channel_(version_info::Channel::UNKNOWN) {}
annekao38685502015-07-14 17:46:3988
89 ~ServiceWorkerTest() override {}
90
kalman6f984ae2015-09-18 17:21:5891 protected:
92 // Returns the ProcessManager for the test's profile.
93 ProcessManager* process_manager() { return ProcessManager::Get(profile()); }
94
95 // Starts running a test from the background page test extension.
96 //
97 // This registers a service worker with |script_name|, and fetches the
98 // registration result.
99 //
100 // If |error_or_null| is null (kExpectSuccess), success is expected and this
101 // will fail if there is an error.
102 // If |error_or_null| is not null, nothing is assumed, and the error (which
103 // may be empty) is written to it.
104 const Extension* StartTestFromBackgroundPage(const char* script_name,
105 std::string* error_or_null) {
106 const Extension* extension =
107 LoadExtension(test_data_dir_.AppendASCII("service_worker/background"));
108 CHECK(extension);
109 ExtensionHost* background_host =
110 process_manager()->GetBackgroundHostForExtension(extension->id());
111 CHECK(background_host);
112 std::string error;
113 CHECK(content::ExecuteScriptAndExtractString(
114 background_host->host_contents(),
115 base::StringPrintf("test.registerServiceWorker('%s')", script_name),
116 &error));
117 if (error_or_null)
118 *error_or_null = error;
119 else if (!error.empty())
120 ADD_FAILURE() << "Got unexpected error " << error;
121 return extension;
122 }
123
124 // Navigates the browser to a new tab at |url|, waits for it to load, then
125 // returns it.
126 content::WebContents* Navigate(const GURL& url) {
127 ui_test_utils::NavigateToURLWithDisposition(
128 browser(), url, NEW_FOREGROUND_TAB,
129 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
130 content::WebContents* web_contents =
131 browser()->tab_strip_model()->GetActiveWebContents();
132 content::WaitForLoadStop(web_contents);
133 return web_contents;
134 }
135
136 // Navigates the browser to |url| and returns the new tab's page type.
137 content::PageType NavigateAndGetPageType(const GURL& url) {
138 return Navigate(url)->GetController().GetActiveEntry()->GetPageType();
139 }
140
141 // Extracts the innerText from |contents|.
142 std::string ExtractInnerText(content::WebContents* contents) {
143 std::string inner_text;
144 if (!content::ExecuteScriptAndExtractString(
145 contents,
146 "window.domAutomationController.send(document.body.innerText)",
147 &inner_text)) {
148 ADD_FAILURE() << "Failed to get inner text for "
149 << contents->GetVisibleURL();
150 }
151 return inner_text;
152 }
153
154 // Navigates the browser to |url|, then returns the innerText of the new
155 // tab's WebContents' main frame.
156 std::string NavigateAndExtractInnerText(const GURL& url) {
157 return ExtractInnerText(Navigate(url));
158 }
159
annekao38685502015-07-14 17:46:39160 private:
kalman6f984ae2015-09-18 17:21:58161 // Sets the channel to "trunk" since service workers are restricted to trunk.
annekao38685502015-07-14 17:46:39162 ScopedCurrentChannel current_channel_;
kalman6f984ae2015-09-18 17:21:58163
annekao38685502015-07-14 17:46:39164 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerTest);
165};
166
lazyboybd325ae2015-11-18 21:35:26167class ServiceWorkerBackgroundSyncTest : public ServiceWorkerTest {
168 public:
169 ServiceWorkerBackgroundSyncTest() {}
170 ~ServiceWorkerBackgroundSyncTest() override {}
171
172 void SetUpCommandLine(base::CommandLine* command_line) override {
173 // ServiceWorkerRegistration.sync requires experimental flag.
174 command_line->AppendSwitch(
175 switches::kEnableExperimentalWebPlatformFeatures);
176 ServiceWorkerTest::SetUpCommandLine(command_line);
177 }
178
179 void SetUp() override {
180 content::background_sync_test_util::SetIgnoreNetworkChangeNotifier(true);
181 ServiceWorkerTest::SetUp();
182 }
183
184 private:
185 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerBackgroundSyncTest);
186};
187
lazyboy561b7de2015-11-19 19:27:30188class ServiceWorkerPushMessagingTest : public ServiceWorkerTest {
189 public:
190 ServiceWorkerPushMessagingTest()
191 : gcm_service_(nullptr), push_service_(nullptr) {}
192 ~ServiceWorkerPushMessagingTest() override {}
193
194 void GrantNotificationPermissionForTest(const GURL& url) {
195 GURL origin = url.GetOrigin();
196 DesktopNotificationProfileUtil::GrantPermission(profile(), origin);
197 ASSERT_EQ(
198 CONTENT_SETTING_ALLOW,
199 DesktopNotificationProfileUtil::GetContentSetting(profile(), origin));
200 }
201
202 PushMessagingAppIdentifier GetAppIdentifierForServiceWorkerRegistration(
avia2f4804a2015-12-24 23:11:13203 int64_t service_worker_registration_id,
lazyboy561b7de2015-11-19 19:27:30204 const GURL& origin) {
205 PushMessagingAppIdentifier app_identifier =
206 PushMessagingAppIdentifier::FindByServiceWorker(
207 profile(), origin, service_worker_registration_id);
208
209 EXPECT_FALSE(app_identifier.is_null());
210 return app_identifier;
211 }
212
213 // ExtensionApiTest overrides.
214 void SetUpCommandLine(base::CommandLine* command_line) override {
peter9de96272015-12-04 15:23:27215 command_line->AppendSwitch(
216 switches::kEnableExperimentalWebPlatformFeatures);
lazyboy561b7de2015-11-19 19:27:30217 ServiceWorkerTest::SetUpCommandLine(command_line);
218 }
219 void SetUpOnMainThread() override {
220 gcm_service_ = static_cast<gcm::FakeGCMProfileService*>(
221 gcm::GCMProfileServiceFactory::GetInstance()->SetTestingFactoryAndUse(
222 profile(), &gcm::FakeGCMProfileService::Build));
223 gcm_service_->set_collect(true);
224 push_service_ = PushMessagingServiceFactory::GetForProfile(profile());
225
226 ServiceWorkerTest::SetUpOnMainThread();
227 }
228
229 gcm::FakeGCMProfileService* gcm_service() const { return gcm_service_; }
230 PushMessagingServiceImpl* push_service() const { return push_service_; }
231
232 private:
233 gcm::FakeGCMProfileService* gcm_service_;
234 PushMessagingServiceImpl* push_service_;
235
236 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerPushMessagingTest);
237};
238
kalman6f984ae2015-09-18 17:21:58239IN_PROC_BROWSER_TEST_F(ServiceWorkerTest, RegisterSucceedsOnTrunk) {
240 StartTestFromBackgroundPage("register.js", kExpectSuccess);
annekao38685502015-07-14 17:46:39241}
242
243// This feature is restricted to trunk, so on dev it should have existing
244// behavior - which is for it to fail.
kalman6f984ae2015-09-18 17:21:58245IN_PROC_BROWSER_TEST_F(ServiceWorkerTest, RegisterFailsOnDev) {
annekao38685502015-07-14 17:46:39246 ScopedCurrentChannel current_channel_override(
sdefresne6e883e42015-07-30 08:05:54247 version_info::Channel::DEV);
kalman6f984ae2015-09-18 17:21:58248 std::string error;
249 const Extension* extension =
250 StartTestFromBackgroundPage("register.js", &error);
annekao1db36fd2015-07-29 17:09:16251 EXPECT_EQ(
kalman6f984ae2015-09-18 17:21:58252 "Failed to register a ServiceWorker: The URL protocol of the current "
253 "origin ('chrome-extension://" +
254 extension->id() + "') is not supported.",
255 error);
annekao38685502015-07-14 17:46:39256}
257
lazyboyc3e763a2015-12-09 23:09:58258IN_PROC_BROWSER_TEST_F(ServiceWorkerTest, UpdateRefreshesServiceWorker) {
259 base::ScopedTempDir scoped_temp_dir;
260 ASSERT_TRUE(scoped_temp_dir.CreateUniqueTempDir());
261 base::FilePath pem_path = test_data_dir_.AppendASCII("service_worker")
262 .AppendASCII("update")
263 .AppendASCII("service_worker.pem");
264 base::FilePath path_v1 = PackExtensionWithOptions(
265 test_data_dir_.AppendASCII("service_worker")
266 .AppendASCII("update")
267 .AppendASCII("v1"),
268 scoped_temp_dir.path().AppendASCII("v1.crx"), pem_path, base::FilePath());
269 base::FilePath path_v2 = PackExtensionWithOptions(
270 test_data_dir_.AppendASCII("service_worker")
271 .AppendASCII("update")
272 .AppendASCII("v2"),
273 scoped_temp_dir.path().AppendASCII("v2.crx"), pem_path, base::FilePath());
274 const char* kId = "hfaanndiiilofhfokeanhddpkfffchdi";
275
276 ExtensionTestMessageListener listener_v1("Pong from version 1", false);
277 listener_v1.set_failure_message("FAILURE_V1");
278 // Install version 1.0 of the extension.
279 ASSERT_TRUE(InstallExtension(path_v1, 1));
280 EXPECT_TRUE(extensions::ExtensionRegistry::Get(profile())
281 ->enabled_extensions()
282 .GetByID(kId));
283 EXPECT_TRUE(listener_v1.WaitUntilSatisfied());
284
285 ExtensionTestMessageListener listener_v2("Pong from version 2", false);
286 listener_v2.set_failure_message("FAILURE_V2");
287
288 // Update to version 2.0.
289 EXPECT_TRUE(UpdateExtension(kId, path_v2, 0));
290 EXPECT_TRUE(extensions::ExtensionRegistry::Get(profile())
291 ->enabled_extensions()
292 .GetByID(kId));
293 EXPECT_TRUE(listener_v2.WaitUntilSatisfied());
294}
295
lazyboy22eddc712015-12-10 21:16:26296IN_PROC_BROWSER_TEST_F(ServiceWorkerTest, UpdateWithoutSkipWaiting) {
297 base::ScopedTempDir scoped_temp_dir;
298 ASSERT_TRUE(scoped_temp_dir.CreateUniqueTempDir());
299 base::FilePath pem_path = test_data_dir_.AppendASCII("service_worker")
300 .AppendASCII("update_without_skip_waiting")
301 .AppendASCII("update_without_skip_waiting.pem");
302 base::FilePath path_v1 = PackExtensionWithOptions(
303 test_data_dir_.AppendASCII("service_worker")
304 .AppendASCII("update_without_skip_waiting")
305 .AppendASCII("v1"),
306 scoped_temp_dir.path().AppendASCII("v1.crx"), pem_path, base::FilePath());
307 base::FilePath path_v2 = PackExtensionWithOptions(
308 test_data_dir_.AppendASCII("service_worker")
309 .AppendASCII("update_without_skip_waiting")
310 .AppendASCII("v2"),
311 scoped_temp_dir.path().AppendASCII("v2.crx"), pem_path, base::FilePath());
312 const char* kId = "mhnnnflgagdakldgjpfcofkiocpdmogl";
313
314 // Install version 1.0 of the extension.
315 ASSERT_TRUE(InstallExtension(path_v1, 1));
316 EXPECT_TRUE(extensions::ExtensionRegistry::Get(profile())
317 ->enabled_extensions()
318 .GetByID(kId));
319 const Extension* extension = extensions::ExtensionRegistry::Get(profile())
320 ->enabled_extensions()
321 .GetByID(kId);
322
323 ExtensionTestMessageListener listener1("Pong from version 1", false);
324 listener1.set_failure_message("FAILURE");
325 content::WebContents* web_contents =
326 AddTab(browser(), extension->GetResourceURL("page.html"));
327 EXPECT_TRUE(listener1.WaitUntilSatisfied());
328
329 // Update to version 2.0.
330 EXPECT_TRUE(UpdateExtension(kId, path_v2, 0));
331 EXPECT_TRUE(extensions::ExtensionRegistry::Get(profile())
332 ->enabled_extensions()
333 .GetByID(kId));
334 const Extension* extension_after_update =
335 extensions::ExtensionRegistry::Get(profile())
336 ->enabled_extensions()
337 .GetByID(kId);
338
339 // Service worker version 2 would be installed but it won't be controlling
340 // the extension page yet.
341 ExtensionTestMessageListener listener2("Pong from version 1", false);
342 listener2.set_failure_message("FAILURE");
343 web_contents =
344 AddTab(browser(), extension_after_update->GetResourceURL("page.html"));
345 EXPECT_TRUE(listener2.WaitUntilSatisfied());
346
347 // Navigate the tab away from the extension page so that no clients are
348 // using the service worker.
349 // Note that just closing the tab with WebContentsDestroyedWatcher doesn't
350 // seem to be enough because it returns too early.
351 WebContentsLoadStopObserver navigate_away_observer(web_contents);
352 web_contents->GetController().LoadURL(
353 GURL(url::kAboutBlankURL), content::Referrer(), ui::PAGE_TRANSITION_TYPED,
354 std::string());
355 navigate_away_observer.WaitForLoadStop();
356
357 // Now expect service worker version 2 to control the extension page.
358 ExtensionTestMessageListener listener3("Pong from version 2", false);
359 listener3.set_failure_message("FAILURE");
360 web_contents =
361 AddTab(browser(), extension_after_update->GetResourceURL("page.html"));
362 EXPECT_TRUE(listener3.WaitUntilSatisfied());
363}
364
kalman6f984ae2015-09-18 17:21:58365IN_PROC_BROWSER_TEST_F(ServiceWorkerTest, FetchArbitraryPaths) {
366 const Extension* extension =
367 StartTestFromBackgroundPage("fetch.js", kExpectSuccess);
annekao1db36fd2015-07-29 17:09:16368
kalman6f984ae2015-09-18 17:21:58369 // Open some arbirary paths. Their contents should be what the service worker
370 // responds with, which in this case is the path of the fetch.
371 EXPECT_EQ(
372 "Caught a fetch for /index.html",
373 NavigateAndExtractInnerText(extension->GetResourceURL("index.html")));
374 EXPECT_EQ("Caught a fetch for /path/to/other.html",
375 NavigateAndExtractInnerText(
376 extension->GetResourceURL("path/to/other.html")));
377 EXPECT_EQ("Caught a fetch for /some/text/file.txt",
378 NavigateAndExtractInnerText(
379 extension->GetResourceURL("some/text/file.txt")));
380 EXPECT_EQ("Caught a fetch for /no/file/extension",
381 NavigateAndExtractInnerText(
382 extension->GetResourceURL("no/file/extension")));
383 EXPECT_EQ("Caught a fetch for /",
384 NavigateAndExtractInnerText(extension->GetResourceURL("")));
annekao1db36fd2015-07-29 17:09:16385}
386
kalman6f984ae2015-09-18 17:21:58387IN_PROC_BROWSER_TEST_F(ServiceWorkerTest,
388 LoadingBackgroundPageBypassesServiceWorker) {
389 const Extension* extension =
390 StartTestFromBackgroundPage("fetch.js", kExpectSuccess);
annekao49241182015-08-18 17:14:01391
kalman6f984ae2015-09-18 17:21:58392 std::string kExpectedInnerText = "background.html contents for testing.";
annekao49241182015-08-18 17:14:01393
kalman6f984ae2015-09-18 17:21:58394 // Sanity check that the background page has the expected content.
395 ExtensionHost* background_page =
396 process_manager()->GetBackgroundHostForExtension(extension->id());
397 ASSERT_TRUE(background_page);
398 EXPECT_EQ(kExpectedInnerText,
399 ExtractInnerText(background_page->host_contents()));
annekao49241182015-08-18 17:14:01400
kalman6f984ae2015-09-18 17:21:58401 // Close the background page.
402 background_page->Close();
403 BackgroundPageWatcher(process_manager(), extension).WaitForClose();
404 background_page = nullptr;
405
406 // Start it again.
407 process_manager()->WakeEventPage(extension->id(),
408 base::Bind(&DoNothingWithBool));
409 BackgroundPageWatcher(process_manager(), extension).WaitForOpen();
410
411 // Content should not have been affected by the fetch, which would otherwise
412 // be "Caught fetch for...".
413 background_page =
414 process_manager()->GetBackgroundHostForExtension(extension->id());
415 ASSERT_TRUE(background_page);
416 content::WaitForLoadStop(background_page->host_contents());
417
418 // TODO(kalman): Everything you've read has been a LIE! It should be:
419 //
420 // EXPECT_EQ(kExpectedInnerText,
421 // ExtractInnerText(background_page->host_contents()));
422 //
423 // but there is a bug, and we're actually *not* bypassing the service worker
424 // for background page loads! For now, let it pass (assert wrong behavior)
425 // because it's not a regression, but this must be fixed eventually.
426 //
427 // Tracked in crbug.com/532720.
428 EXPECT_EQ("Caught a fetch for /background.html",
429 ExtractInnerText(background_page->host_contents()));
annekao49241182015-08-18 17:14:01430}
431
kalman6f984ae2015-09-18 17:21:58432IN_PROC_BROWSER_TEST_F(ServiceWorkerTest,
433 ServiceWorkerPostsMessageToBackgroundClient) {
434 const Extension* extension = StartTestFromBackgroundPage(
435 "post_message_to_background_client.js", kExpectSuccess);
annekao533482222015-08-21 23:23:53436
kalman6f984ae2015-09-18 17:21:58437 // The service worker in this test simply posts a message to the background
438 // client it receives from getBackgroundClient().
439 const char* kScript =
440 "var messagePromise = null;\n"
441 "if (test.lastMessageFromServiceWorker) {\n"
442 " messagePromise = Promise.resolve(test.lastMessageFromServiceWorker);\n"
443 "} else {\n"
444 " messagePromise = test.waitForMessage(navigator.serviceWorker);\n"
445 "}\n"
446 "messagePromise.then(function(message) {\n"
447 " window.domAutomationController.send(String(message == 'success'));\n"
448 "})\n";
449 EXPECT_EQ("true", ExecuteScriptInBackgroundPage(extension->id(), kScript));
annekao533482222015-08-21 23:23:53450}
451
kalman6f984ae2015-09-18 17:21:58452IN_PROC_BROWSER_TEST_F(ServiceWorkerTest,
453 BackgroundPagePostsMessageToServiceWorker) {
454 const Extension* extension =
455 StartTestFromBackgroundPage("post_message_to_sw.js", kExpectSuccess);
annekao533482222015-08-21 23:23:53456
kalman6f984ae2015-09-18 17:21:58457 // The service worker in this test waits for a message, then echoes it back
458 // by posting a message to the background page via getBackgroundClient().
459 const char* kScript =
460 "var mc = new MessageChannel();\n"
461 "test.waitForMessage(mc.port1).then(function(message) {\n"
462 " window.domAutomationController.send(String(message == 'hello'));\n"
463 "});\n"
464 "test.registeredServiceWorker.postMessage(\n"
465 " {message: 'hello', port: mc.port2}, [mc.port2])\n";
466 EXPECT_EQ("true", ExecuteScriptInBackgroundPage(extension->id(), kScript));
annekao533482222015-08-21 23:23:53467}
468
rdevlin.croninf5863da2015-09-10 19:21:45469IN_PROC_BROWSER_TEST_F(ServiceWorkerTest,
470 ServiceWorkerSuspensionOnExtensionUnload) {
kalman6f984ae2015-09-18 17:21:58471 // For this test, only hold onto the extension's ID and URL + a function to
472 // get a resource URL, because we're going to be disabling and uninstalling
473 // it, which will invalidate the pointer.
474 std::string extension_id;
475 GURL extension_url;
476 {
477 const Extension* extension =
478 StartTestFromBackgroundPage("fetch.js", kExpectSuccess);
479 extension_id = extension->id();
480 extension_url = extension->url();
481 }
482 auto get_resource_url = [&extension_url](const std::string& path) {
483 return Extension::GetResourceURL(extension_url, path);
484 };
rdevlin.croninf5863da2015-09-10 19:21:45485
kalman6f984ae2015-09-18 17:21:58486 // Fetch should route to the service worker.
487 EXPECT_EQ("Caught a fetch for /index.html",
488 NavigateAndExtractInnerText(get_resource_url("index.html")));
rdevlin.croninf5863da2015-09-10 19:21:45489
kalman6f984ae2015-09-18 17:21:58490 // Disable the extension. Opening the page should fail.
491 extension_service()->DisableExtension(extension_id,
rdevlin.croninf5863da2015-09-10 19:21:45492 Extension::DISABLE_USER_ACTION);
493 base::RunLoop().RunUntilIdle();
rdevlin.croninf5863da2015-09-10 19:21:45494
kalman6f984ae2015-09-18 17:21:58495 EXPECT_EQ(content::PAGE_TYPE_ERROR,
496 NavigateAndGetPageType(get_resource_url("index.html")));
497 EXPECT_EQ(content::PAGE_TYPE_ERROR,
498 NavigateAndGetPageType(get_resource_url("other.html")));
499
500 // Re-enable the extension. Opening pages should immediately start to succeed
501 // again.
rdevlin.croninf5863da2015-09-10 19:21:45502 extension_service()->EnableExtension(extension_id);
503 base::RunLoop().RunUntilIdle();
504
kalman6f984ae2015-09-18 17:21:58505 EXPECT_EQ("Caught a fetch for /index.html",
506 NavigateAndExtractInnerText(get_resource_url("index.html")));
507 EXPECT_EQ("Caught a fetch for /other.html",
508 NavigateAndExtractInnerText(get_resource_url("other.html")));
509 EXPECT_EQ("Caught a fetch for /another.html",
510 NavigateAndExtractInnerText(get_resource_url("another.html")));
rdevlin.croninf5863da2015-09-10 19:21:45511
kalman6f984ae2015-09-18 17:21:58512 // Uninstall the extension. Opening pages should fail again.
513 base::string16 error;
514 extension_service()->UninstallExtension(
515 extension_id, UninstallReason::UNINSTALL_REASON_FOR_TESTING,
516 base::Bind(&base::DoNothing), &error);
517 base::RunLoop().RunUntilIdle();
518
519 EXPECT_EQ(content::PAGE_TYPE_ERROR,
520 NavigateAndGetPageType(get_resource_url("index.html")));
521 EXPECT_EQ(content::PAGE_TYPE_ERROR,
522 NavigateAndGetPageType(get_resource_url("other.html")));
523 EXPECT_EQ(content::PAGE_TYPE_ERROR,
524 NavigateAndGetPageType(get_resource_url("anotherother.html")));
525 EXPECT_EQ(content::PAGE_TYPE_ERROR,
526 NavigateAndGetPageType(get_resource_url("final.html")));
527}
528
529IN_PROC_BROWSER_TEST_F(ServiceWorkerTest, BackgroundPageIsWokenIfAsleep) {
530 const Extension* extension =
531 StartTestFromBackgroundPage("wake_on_fetch.js", kExpectSuccess);
532
533 // Navigate to special URLs that this test's service worker recognises, each
534 // making a check then populating the response with either "true" or "false".
535 EXPECT_EQ("true", NavigateAndExtractInnerText(extension->GetResourceURL(
536 "background-client-is-awake")));
537 EXPECT_EQ("true", NavigateAndExtractInnerText(
538 extension->GetResourceURL("ping-background-client")));
539 // Ping more than once for good measure.
540 EXPECT_EQ("true", NavigateAndExtractInnerText(
541 extension->GetResourceURL("ping-background-client")));
542
543 // Shut down the event page. The SW should detect that it's closed, but still
544 // be able to ping it.
545 ExtensionHost* background_page =
546 process_manager()->GetBackgroundHostForExtension(extension->id());
547 ASSERT_TRUE(background_page);
548 background_page->Close();
549 BackgroundPageWatcher(process_manager(), extension).WaitForClose();
550
551 EXPECT_EQ("false", NavigateAndExtractInnerText(extension->GetResourceURL(
552 "background-client-is-awake")));
553 EXPECT_EQ("true", NavigateAndExtractInnerText(
554 extension->GetResourceURL("ping-background-client")));
555 EXPECT_EQ("true", NavigateAndExtractInnerText(
556 extension->GetResourceURL("ping-background-client")));
557 EXPECT_EQ("true", NavigateAndExtractInnerText(extension->GetResourceURL(
558 "background-client-is-awake")));
559}
560
561IN_PROC_BROWSER_TEST_F(ServiceWorkerTest,
562 GetBackgroundClientFailsWithNoBackgroundPage) {
563 // This extension doesn't have a background page, only a tab at page.html.
564 // The service worker it registers tries to call getBackgroundClient() and
565 // should fail.
566 // Note that this also tests that service workers can be registered from tabs.
567 EXPECT_TRUE(RunExtensionSubtest("service_worker/no_background", "page.html"));
rdevlin.croninf5863da2015-09-10 19:21:45568}
569
lazyboy6ddb7d62015-11-10 23:15:27570IN_PROC_BROWSER_TEST_F(ServiceWorkerTest, NotificationAPI) {
571 EXPECT_TRUE(RunExtensionSubtest("service_worker/notifications/has_permission",
572 "page.html"));
573}
574
lazyboybd325ae2015-11-18 21:35:26575IN_PROC_BROWSER_TEST_F(ServiceWorkerBackgroundSyncTest, Sync) {
576 const Extension* extension = LoadExtensionWithFlags(
577 test_data_dir_.AppendASCII("service_worker/sync"), kFlagNone);
578 ASSERT_TRUE(extension);
579 ui_test_utils::NavigateToURL(browser(),
580 extension->GetResourceURL("page.html"));
581 content::WebContents* web_contents =
582 browser()->tab_strip_model()->GetActiveWebContents();
583
584 // Prevent firing by going offline.
585 content::background_sync_test_util::SetOnline(web_contents, false);
586
587 ExtensionTestMessageListener sync_listener("SYNC: send-chats", false);
588 sync_listener.set_failure_message("FAIL");
589
590 std::string result;
591 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
592 web_contents, "window.runServiceWorker()", &result));
593 ASSERT_EQ("SERVICE_WORKER_READY", result);
594
595 EXPECT_FALSE(sync_listener.was_satisfied());
596 // Resume firing by going online.
597 content::background_sync_test_util::SetOnline(web_contents, true);
598 EXPECT_TRUE(sync_listener.WaitUntilSatisfied());
599}
600
horo1eeddde2015-11-19 05:59:25601IN_PROC_BROWSER_TEST_F(ServiceWorkerTest,
602 FetchFromContentScriptShouldNotGoToServiceWorkerOfPage) {
603 ASSERT_TRUE(StartEmbeddedTestServer());
604 GURL page_url = embedded_test_server()->GetURL(
605 "/extensions/api_test/service_worker/content_script_fetch/"
606 "controlled_page/index.html");
607 content::WebContents* tab =
608 browser()->tab_strip_model()->GetActiveWebContents();
609 ui_test_utils::NavigateToURL(browser(), page_url);
610 content::WaitForLoadStop(tab);
611
612 std::string value;
613 ASSERT_TRUE(
614 content::ExecuteScriptAndExtractString(tab, "register();", &value));
615 EXPECT_EQ("SW controlled", value);
616
617 ASSERT_TRUE(RunExtensionTest("service_worker/content_script_fetch"))
618 << message_;
619}
620
lazyboy561b7de2015-11-19 19:27:30621IN_PROC_BROWSER_TEST_F(ServiceWorkerPushMessagingTest, OnPush) {
622 const Extension* extension = LoadExtensionWithFlags(
623 test_data_dir_.AppendASCII("service_worker/push_messaging"), kFlagNone);
624 ASSERT_TRUE(extension);
625 GURL extension_url = extension->url();
626
627 ASSERT_NO_FATAL_FAILURE(GrantNotificationPermissionForTest(extension_url));
628
629 GURL url = extension->GetResourceURL("page.html");
630 ui_test_utils::NavigateToURL(browser(), url);
631
632 content::WebContents* web_contents =
633 browser()->tab_strip_model()->GetActiveWebContents();
634
635 // Start the ServiceWorker.
636 ExtensionTestMessageListener ready_listener("SERVICE_WORKER_READY", false);
637 ready_listener.set_failure_message("SERVICE_WORKER_FAILURE");
638 const char* kScript = "window.runServiceWorker()";
639 EXPECT_TRUE(content::ExecuteScript(web_contents->GetMainFrame(), kScript));
640 EXPECT_TRUE(ready_listener.WaitUntilSatisfied());
641
642 PushMessagingAppIdentifier app_identifier =
643 GetAppIdentifierForServiceWorkerRegistration(0LL, extension_url);
644 ASSERT_EQ(app_identifier.app_id(), gcm_service()->last_registered_app_id());
645 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]);
646
647 // Send a push message via gcm and expect the ServiceWorker to receive it.
648 ExtensionTestMessageListener push_message_listener("OK", false);
649 push_message_listener.set_failure_message("FAIL");
650 gcm::IncomingMessage message;
651 message.sender_id = "1234567890";
652 message.raw_data = "testdata";
653 message.decrypted = true;
654 push_service()->OnMessage(app_identifier.app_id(), message);
655 EXPECT_TRUE(push_message_listener.WaitUntilSatisfied());
656}
657
annekao38685502015-07-14 17:46:39658} // namespace extensions