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 // Disabled: crbug.com/529516.
47 IN_PROC_BROWSER_TEST_F(ServiceWorkerTest
, DISABLED_ServiceWorkerFetchEvent
) {
48 RunExtensionTest("service_worker/fetch");
49 content::WebContents
* contents
=
50 browser()->tab_strip_model()->GetActiveWebContents();
51 content::WaitForLoadStop(contents
);
54 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
55 contents
, "window.domAutomationController.send(document.body.innerText);",
57 EXPECT_EQ("No Fetch Event yet.", output
);
59 // Page must reload in order for the service worker to take control.
60 contents
->GetController().Reload(true);
61 content::WaitForLoadStop(contents
);
63 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
64 contents
, "window.domAutomationController.send(document.body.innerText);",
66 EXPECT_EQ("Caught a fetch!", output
);
69 // Binding that was created on the v8::Context of the worker for testing
70 // purposes should bind an object to chrome.
71 // Disabled: crbug.com/529516.
72 IN_PROC_BROWSER_TEST_F(ServiceWorkerTest
, DISABLED_ServiceWorkerChromeBinding
) {
73 ASSERT_TRUE(RunExtensionTest("service_worker/bindings")) << message_
;
74 content::WebContents
* contents
=
75 browser()->tab_strip_model()->GetActiveWebContents();
76 content::WaitForLoadStop(contents
);
79 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
80 contents
, "window.domAutomationController.send(document.body.innerText);",
82 EXPECT_EQ("No Fetch Event yet.", output
);
84 // Page must reload in order for the service worker to take control.
85 contents
->GetController().Reload(true);
86 content::WaitForLoadStop(contents
);
88 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
89 contents
, "window.domAutomationController.send(document.body.innerText);",
91 EXPECT_EQ("object", output
);
94 // Disabled: crbug.com/529516.
95 IN_PROC_BROWSER_TEST_F(ServiceWorkerTest
, DISABLED_GetBackgroundClient
) {
96 ASSERT_TRUE(RunExtensionTest("service_worker/background_client")) << message_
;
97 content::WebContents
* contents
=
98 browser()->tab_strip_model()->GetActiveWebContents();
99 content::WaitForLoadStop(contents
);
102 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
103 contents
, "window.domAutomationController.send(document.body.innerText);",
105 EXPECT_EQ("No Fetch Event yet.", output
);
107 // Page must reload in order for the service worker to take control.
108 contents
->GetController().Reload(true);
109 content::WaitForLoadStop(contents
);
111 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
112 contents
, "window.domAutomationController.send(document.body.innerText);",
114 EXPECT_EQ("chrome-extension://" + GetSingleLoadedExtension()->id() +
116 "_generated_background_page.html",
120 IN_PROC_BROWSER_TEST_F(ServiceWorkerTest
, PostMessageToBackgroundClient
) {
121 ASSERT_TRUE(RunExtensionTest("service_worker/post_messaging")) << message_
;
123 EXPECT_EQ("Hello from the SW!",
124 ExecuteScriptInBackgroundPage(
125 GetSingleLoadedExtension()->id(),
126 "window.domAutomationController.send(message);"));
129 } // namespace extensions