1 // 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.
5 #include "chrome/browser/extensions/extension_apitest.h"
6 #include "chrome/browser/ui/tabs/tab_strip_model.h"
7 #include "components/version_info/version_info.h"
8 #include "content/public/test/browser_test_utils.h"
9 #include "extensions/test/extension_test_message_listener.h"
11 namespace extensions
{
13 class ServiceWorkerTest
: public ExtensionApiTest
{
15 // Set the channel to "trunk" since service workers are restricted to trunk.
17 : current_channel_(version_info::Channel::UNKNOWN
) {}
19 ~ServiceWorkerTest() override
{}
22 ScopedCurrentChannel current_channel_
;
23 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerTest
);
26 IN_PROC_BROWSER_TEST_F(ServiceWorkerTest
, RegisterServiceWorkersOnTrunk
) {
27 ExtensionTestMessageListener
listener(false);
28 ASSERT_TRUE(RunExtensionTest("service_worker/register")) << message_
;
31 // This feature is restricted to trunk, so on dev it should have existing
32 // behavior - which is for it to fail.
33 IN_PROC_BROWSER_TEST_F(ServiceWorkerTest
, CannotRegisterServiceWorkersOnDev
) {
34 ScopedCurrentChannel
current_channel_override(
35 version_info::Channel::DEV
);
36 ExtensionTestMessageListener
listener(false);
37 ASSERT_FALSE(RunExtensionTest("service_worker/register")) << message_
;
38 ASSERT_TRUE(listener
.WaitUntilSatisfied());
40 "SecurityError: Failed to register a ServiceWorker: The URL "
41 "protocol of the current origin ('chrome-extension://" +
42 GetSingleLoadedExtension()->id() + "') is not supported.",
46 IN_PROC_BROWSER_TEST_F(ServiceWorkerTest
, ServiceWorkerFetchEvent
) {
47 RunExtensionTest("service_worker/fetch");
48 content::WebContents
* contents
=
49 browser()->tab_strip_model()->GetActiveWebContents();
50 content::WaitForLoadStop(contents
);
53 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
54 contents
, "window.domAutomationController.send(document.body.innerText);",
56 EXPECT_EQ("No Fetch Event yet.", output
);
58 // Page must reload in order for the service worker to take control.
59 contents
->GetController().Reload(true);
60 content::WaitForLoadStop(contents
);
62 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
63 contents
, "window.domAutomationController.send(document.body.innerText);",
65 EXPECT_EQ("Caught a fetch!", output
);
68 // Binding that was created on the v8::Context of the worker for testing
69 // purposes should bind an object to chrome.
70 IN_PROC_BROWSER_TEST_F(ServiceWorkerTest
, ServiceWorkerChromeBinding
) {
71 ASSERT_TRUE(RunExtensionTest("service_worker/bindings")) << message_
;
72 content::WebContents
* contents
=
73 browser()->tab_strip_model()->GetActiveWebContents();
74 content::WaitForLoadStop(contents
);
77 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
78 contents
, "window.domAutomationController.send(document.body.innerText);",
80 EXPECT_EQ("No Fetch Event yet.", output
);
82 // Page must reload in order for the service worker to take control.
83 contents
->GetController().Reload(true);
84 content::WaitForLoadStop(contents
);
86 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
87 contents
, "window.domAutomationController.send(document.body.innerText);",
89 EXPECT_EQ("object", output
);
92 IN_PROC_BROWSER_TEST_F(ServiceWorkerTest
, GetBackgroundClient
) {
93 ASSERT_TRUE(RunExtensionTest("service_worker/background_client")) << message_
;
94 content::WebContents
* contents
=
95 browser()->tab_strip_model()->GetActiveWebContents();
96 content::WaitForLoadStop(contents
);
99 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
100 contents
, "window.domAutomationController.send(document.body.innerText);",
102 EXPECT_EQ("No Fetch Event yet.", output
);
104 // Page must reload in order for the service worker to take control.
105 contents
->GetController().Reload(true);
106 content::WaitForLoadStop(contents
);
108 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
109 contents
, "window.domAutomationController.send(document.body.innerText);",
111 EXPECT_EQ("chrome-extension://" + GetSingleLoadedExtension()->id() +
113 "_generated_background_page.html",
117 IN_PROC_BROWSER_TEST_F(ServiceWorkerTest
, PostMessageToBackgroundClient
) {
118 ASSERT_TRUE(RunExtensionTest("service_worker/post_messaging")) << message_
;
120 EXPECT_EQ("Hello from the SW!",
121 ExecuteScriptInBackgroundPage(
122 GetSingleLoadedExtension()->id(),
123 "window.domAutomationController.send(message);"));
126 } // namespace extensions