blob: c38e09958c68a0214f82f4ff3203208404cabdb7 [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
5#include "chrome/browser/extensions/extension_apitest.h"
annekao1db36fd2015-07-29 17:09:166#include "chrome/browser/ui/tabs/tab_strip_model.h"
sdefresne9fb67692015-08-03 18:48:227#include "components/version_info/version_info.h"
annekao1db36fd2015-07-29 17:09:168#include "content/public/test/browser_test_utils.h"
annekao38685502015-07-14 17:46:399#include "extensions/test/extension_test_message_listener.h"
10
11namespace extensions {
12
13class ServiceWorkerTest : public ExtensionApiTest {
14 public:
15 // Set the channel to "trunk" since service workers are restricted to trunk.
16 ServiceWorkerTest()
sdefresne6e883e42015-07-30 08:05:5417 : current_channel_(version_info::Channel::UNKNOWN) {}
annekao38685502015-07-14 17:46:3918
19 ~ServiceWorkerTest() override {}
20
21 private:
22 ScopedCurrentChannel current_channel_;
23 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerTest);
24};
25
26IN_PROC_BROWSER_TEST_F(ServiceWorkerTest, RegisterServiceWorkersOnTrunk) {
27 ExtensionTestMessageListener listener(false);
annekao1db36fd2015-07-29 17:09:1628 ASSERT_TRUE(RunExtensionTest("service_worker/register")) << message_;
annekao38685502015-07-14 17:46:3929}
30
31// This feature is restricted to trunk, so on dev it should have existing
32// behavior - which is for it to fail.
33IN_PROC_BROWSER_TEST_F(ServiceWorkerTest, CannotRegisterServiceWorkersOnDev) {
34 ScopedCurrentChannel current_channel_override(
sdefresne6e883e42015-07-30 08:05:5435 version_info::Channel::DEV);
annekao38685502015-07-14 17:46:3936 ExtensionTestMessageListener listener(false);
37 ASSERT_FALSE(RunExtensionTest("service_worker/register")) << message_;
38 ASSERT_TRUE(listener.WaitUntilSatisfied());
annekao1db36fd2015-07-29 17:09:1639 EXPECT_EQ(
annekao38685502015-07-14 17:46:3940 "SecurityError: Failed to register a ServiceWorker: The URL "
41 "protocol of the current origin ('chrome-extension://" +
42 GetSingleLoadedExtension()->id() + "') is not supported.",
43 listener.message());
44}
45
kalman881d73a2015-09-09 08:58:4646// Disabled: crbug.com/529516.
47IN_PROC_BROWSER_TEST_F(ServiceWorkerTest, DISABLED_ServiceWorkerFetchEvent) {
annekao1db36fd2015-07-29 17:09:1648 RunExtensionTest("service_worker/fetch");
49 content::WebContents* contents =
50 browser()->tab_strip_model()->GetActiveWebContents();
51 content::WaitForLoadStop(contents);
52
53 std::string output;
54 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
55 contents, "window.domAutomationController.send(document.body.innerText);",
56 &output));
57 EXPECT_EQ("No Fetch Event yet.", output);
58
59 // Page must reload in order for the service worker to take control.
60 contents->GetController().Reload(true);
61 content::WaitForLoadStop(contents);
62
63 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
64 contents, "window.domAutomationController.send(document.body.innerText);",
65 &output));
66 EXPECT_EQ("Caught a fetch!", output);
67}
68
annekao49241182015-08-18 17:14:0169// Binding that was created on the v8::Context of the worker for testing
annekao533482222015-08-21 23:23:5370// purposes should bind an object to chrome.
kalman881d73a2015-09-09 08:58:4671// Disabled: crbug.com/529516.
72IN_PROC_BROWSER_TEST_F(ServiceWorkerTest, DISABLED_ServiceWorkerChromeBinding) {
annekao49241182015-08-18 17:14:0173 ASSERT_TRUE(RunExtensionTest("service_worker/bindings")) << message_;
74 content::WebContents* contents =
75 browser()->tab_strip_model()->GetActiveWebContents();
76 content::WaitForLoadStop(contents);
77
78 std::string output;
79 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
80 contents, "window.domAutomationController.send(document.body.innerText);",
81 &output));
82 EXPECT_EQ("No Fetch Event yet.", output);
83
84 // Page must reload in order for the service worker to take control.
85 contents->GetController().Reload(true);
86 content::WaitForLoadStop(contents);
87
88 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
89 contents, "window.domAutomationController.send(document.body.innerText);",
90 &output));
91 EXPECT_EQ("object", output);
92}
93
kalman881d73a2015-09-09 08:58:4694// Disabled: crbug.com/529516.
95IN_PROC_BROWSER_TEST_F(ServiceWorkerTest, DISABLED_GetBackgroundClient) {
annekao533482222015-08-21 23:23:5396 ASSERT_TRUE(RunExtensionTest("service_worker/background_client")) << message_;
97 content::WebContents* contents =
98 browser()->tab_strip_model()->GetActiveWebContents();
99 content::WaitForLoadStop(contents);
100
101 std::string output;
102 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
103 contents, "window.domAutomationController.send(document.body.innerText);",
104 &output));
105 EXPECT_EQ("No Fetch Event yet.", output);
106
107 // Page must reload in order for the service worker to take control.
108 contents->GetController().Reload(true);
109 content::WaitForLoadStop(contents);
110
111 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
112 contents, "window.domAutomationController.send(document.body.innerText);",
113 &output));
114 EXPECT_EQ("chrome-extension://" + GetSingleLoadedExtension()->id() +
115 "/"
116 "_generated_background_page.html",
117 output);
118}
119
120IN_PROC_BROWSER_TEST_F(ServiceWorkerTest, PostMessageToBackgroundClient) {
121 ASSERT_TRUE(RunExtensionTest("service_worker/post_messaging")) << message_;
122
123 EXPECT_EQ("Hello from the SW!",
124 ExecuteScriptInBackgroundPage(
125 GetSingleLoadedExtension()->id(),
126 "window.domAutomationController.send(message);"));
127}
128
annekao38685502015-07-14 17:46:39129} // namespace extensions