blob: 933cb3160372e3e534403b3d19e16f2c6deb6e23 [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
annekao1db36fd2015-07-29 17:09:1646IN_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);
51
52 std::string output;
53 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
54 contents, "window.domAutomationController.send(document.body.innerText);",
55 &output));
56 EXPECT_EQ("No Fetch Event yet.", output);
57
58 // Page must reload in order for the service worker to take control.
59 contents->GetController().Reload(true);
60 content::WaitForLoadStop(contents);
61
62 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
63 contents, "window.domAutomationController.send(document.body.innerText);",
64 &output));
65 EXPECT_EQ("Caught a fetch!", output);
66}
67
annekao38685502015-07-14 17:46:3968} // namespace extensions