blob: 5a995c9ffc32d8389c97194c496dfb4b9c389620 [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
rdevlin.croninf5863da2015-09-10 19:21:455#include "base/run_loop.h"
annekao38685502015-07-14 17:46:396#include "chrome/browser/extensions/extension_apitest.h"
rdevlin.croninf5863da2015-09-10 19:21:457#include "chrome/browser/extensions/extension_service.h"
annekao1db36fd2015-07-29 17:09:168#include "chrome/browser/ui/tabs/tab_strip_model.h"
rdevlin.croninf5863da2015-09-10 19:21:459#include "chrome/test/base/ui_test_utils.h"
sdefresne9fb67692015-08-03 18:48:2210#include "components/version_info/version_info.h"
rdevlin.croninf5863da2015-09-10 19:21:4511#include "content/public/browser/navigation_entry.h"
annekao1db36fd2015-07-29 17:09:1612#include "content/public/test/browser_test_utils.h"
rdevlin.croninf5863da2015-09-10 19:21:4513#include "content/public/test/test_utils.h"
annekao38685502015-07-14 17:46:3914#include "extensions/test/extension_test_message_listener.h"
15
16namespace extensions {
17
18class ServiceWorkerTest : public ExtensionApiTest {
19 public:
20 // Set the channel to "trunk" since service workers are restricted to trunk.
21 ServiceWorkerTest()
sdefresne6e883e42015-07-30 08:05:5422 : current_channel_(version_info::Channel::UNKNOWN) {}
annekao38685502015-07-14 17:46:3923
24 ~ServiceWorkerTest() override {}
25
26 private:
27 ScopedCurrentChannel current_channel_;
28 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerTest);
29};
30
31IN_PROC_BROWSER_TEST_F(ServiceWorkerTest, RegisterServiceWorkersOnTrunk) {
32 ExtensionTestMessageListener listener(false);
annekao1db36fd2015-07-29 17:09:1633 ASSERT_TRUE(RunExtensionTest("service_worker/register")) << message_;
annekao38685502015-07-14 17:46:3934}
35
36// This feature is restricted to trunk, so on dev it should have existing
37// behavior - which is for it to fail.
38IN_PROC_BROWSER_TEST_F(ServiceWorkerTest, CannotRegisterServiceWorkersOnDev) {
39 ScopedCurrentChannel current_channel_override(
sdefresne6e883e42015-07-30 08:05:5440 version_info::Channel::DEV);
annekao38685502015-07-14 17:46:3941 ExtensionTestMessageListener listener(false);
42 ASSERT_FALSE(RunExtensionTest("service_worker/register")) << message_;
43 ASSERT_TRUE(listener.WaitUntilSatisfied());
annekao1db36fd2015-07-29 17:09:1644 EXPECT_EQ(
annekao38685502015-07-14 17:46:3945 "SecurityError: Failed to register a ServiceWorker: The URL "
46 "protocol of the current origin ('chrome-extension://" +
47 GetSingleLoadedExtension()->id() + "') is not supported.",
48 listener.message());
49}
50
kalman881d73a2015-09-09 08:58:4651// Disabled: crbug.com/529516.
52IN_PROC_BROWSER_TEST_F(ServiceWorkerTest, DISABLED_ServiceWorkerFetchEvent) {
annekao1db36fd2015-07-29 17:09:1653 RunExtensionTest("service_worker/fetch");
54 content::WebContents* contents =
55 browser()->tab_strip_model()->GetActiveWebContents();
56 content::WaitForLoadStop(contents);
57
58 std::string output;
59 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
60 contents, "window.domAutomationController.send(document.body.innerText);",
61 &output));
62 EXPECT_EQ("No Fetch Event yet.", output);
63
64 // Page must reload in order for the service worker to take control.
65 contents->GetController().Reload(true);
66 content::WaitForLoadStop(contents);
67
68 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
69 contents, "window.domAutomationController.send(document.body.innerText);",
70 &output));
71 EXPECT_EQ("Caught a fetch!", output);
72}
73
annekao49241182015-08-18 17:14:0174// Binding that was created on the v8::Context of the worker for testing
annekao533482222015-08-21 23:23:5375// purposes should bind an object to chrome.
kalman881d73a2015-09-09 08:58:4676// Disabled: crbug.com/529516.
77IN_PROC_BROWSER_TEST_F(ServiceWorkerTest, DISABLED_ServiceWorkerChromeBinding) {
annekao49241182015-08-18 17:14:0178 ASSERT_TRUE(RunExtensionTest("service_worker/bindings")) << message_;
79 content::WebContents* contents =
80 browser()->tab_strip_model()->GetActiveWebContents();
81 content::WaitForLoadStop(contents);
82
83 std::string output;
84 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
85 contents, "window.domAutomationController.send(document.body.innerText);",
86 &output));
87 EXPECT_EQ("No Fetch Event yet.", output);
88
89 // Page must reload in order for the service worker to take control.
90 contents->GetController().Reload(true);
91 content::WaitForLoadStop(contents);
92
93 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
94 contents, "window.domAutomationController.send(document.body.innerText);",
95 &output));
96 EXPECT_EQ("object", output);
97}
98
kalman881d73a2015-09-09 08:58:4699// Disabled: crbug.com/529516.
100IN_PROC_BROWSER_TEST_F(ServiceWorkerTest, DISABLED_GetBackgroundClient) {
annekao533482222015-08-21 23:23:53101 ASSERT_TRUE(RunExtensionTest("service_worker/background_client")) << message_;
102 content::WebContents* contents =
103 browser()->tab_strip_model()->GetActiveWebContents();
104 content::WaitForLoadStop(contents);
105
106 std::string output;
107 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
108 contents, "window.domAutomationController.send(document.body.innerText);",
109 &output));
110 EXPECT_EQ("No Fetch Event yet.", output);
111
112 // Page must reload in order for the service worker to take control.
113 contents->GetController().Reload(true);
114 content::WaitForLoadStop(contents);
115
116 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
117 contents, "window.domAutomationController.send(document.body.innerText);",
118 &output));
119 EXPECT_EQ("chrome-extension://" + GetSingleLoadedExtension()->id() +
120 "/"
121 "_generated_background_page.html",
122 output);
123}
124
125IN_PROC_BROWSER_TEST_F(ServiceWorkerTest, PostMessageToBackgroundClient) {
126 ASSERT_TRUE(RunExtensionTest("service_worker/post_messaging")) << message_;
127
128 EXPECT_EQ("Hello from the SW!",
129 ExecuteScriptInBackgroundPage(
130 GetSingleLoadedExtension()->id(),
131 "window.domAutomationController.send(message);"));
132}
133
rdevlin.croninf5863da2015-09-10 19:21:45134IN_PROC_BROWSER_TEST_F(ServiceWorkerTest,
135 ServiceWorkerSuspensionOnExtensionUnload) {
136 const Extension* extension = LoadExtension(
137 test_data_dir_.AppendASCII("service_worker").AppendASCII("suspended"));
138 ASSERT_TRUE(extension);
139 std::string extension_id = extension->id();
140
141 ExtensionTestMessageListener listener("registered", false);
142 GURL url = extension->GetResourceURL("/page.html");
143 ui_test_utils::NavigateToURLWithDisposition(
144 browser(), url, NEW_FOREGROUND_TAB,
145 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
146 listener.WaitUntilSatisfied();
147 content::WebContents* web_contents =
148 browser()->tab_strip_model()->GetActiveWebContents();
149 std::string output;
150 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
151 web_contents,
152 "window.domAutomationController.send(document.body.innerText);",
153 &output));
154 EXPECT_EQ("No Fetch Event yet.", output);
155
156 // Page must reload in order for the service worker to take control.
157 content::RunAllBlockingPoolTasksUntilIdle();
158 base::RunLoop().RunUntilIdle();
159 web_contents->GetController().Reload(true);
160 content::WaitForLoadStop(web_contents);
161
162 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
163 web_contents,
164 "window.domAutomationController.send(document.body.innerText);",
165 &output));
166 EXPECT_EQ("Caught a fetch!", output);
167
168 extension_service()->DisableExtension(extension->id(),
169 Extension::DISABLE_USER_ACTION);
170 base::RunLoop().RunUntilIdle();
171 // When the extension is disabled, chrome closes any tabs open to its pages,
172 // so we have to navigate back by hand.
173 ui_test_utils::NavigateToURLWithDisposition(
174 browser(), url, NEW_FOREGROUND_TAB,
175 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
176 web_contents = browser()->tab_strip_model()->GetActiveWebContents();
177 EXPECT_EQ(content::PAGE_TYPE_ERROR,
178 web_contents->GetController().GetActiveEntry()->GetPageType());
179
180 extension_service()->EnableExtension(extension_id);
181 base::RunLoop().RunUntilIdle();
182
183 web_contents->GetController().Reload(true);
184 content::WaitForLoadStop(web_contents);
185
186 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
187 web_contents,
188 "window.domAutomationController.send(document.body.innerText);",
189 &output));
190 EXPECT_EQ("Caught a fetch!", output);
191}
192
annekao38685502015-07-14 17:46:39193} // namespace extensions