annekao | 3868550 | 2015-07-14 17:46:39 | [diff] [blame] | 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. |
| 4 | |
rdevlin.cronin | f5863da | 2015-09-10 19:21:45 | [diff] [blame^] | 5 | #include "base/run_loop.h" |
annekao | 3868550 | 2015-07-14 17:46:39 | [diff] [blame] | 6 | #include "chrome/browser/extensions/extension_apitest.h" |
rdevlin.cronin | f5863da | 2015-09-10 19:21:45 | [diff] [blame^] | 7 | #include "chrome/browser/extensions/extension_service.h" |
annekao | 1db36fd | 2015-07-29 17:09:16 | [diff] [blame] | 8 | #include "chrome/browser/ui/tabs/tab_strip_model.h" |
rdevlin.cronin | f5863da | 2015-09-10 19:21:45 | [diff] [blame^] | 9 | #include "chrome/test/base/ui_test_utils.h" |
sdefresne | 9fb6769 | 2015-08-03 18:48:22 | [diff] [blame] | 10 | #include "components/version_info/version_info.h" |
rdevlin.cronin | f5863da | 2015-09-10 19:21:45 | [diff] [blame^] | 11 | #include "content/public/browser/navigation_entry.h" |
annekao | 1db36fd | 2015-07-29 17:09:16 | [diff] [blame] | 12 | #include "content/public/test/browser_test_utils.h" |
rdevlin.cronin | f5863da | 2015-09-10 19:21:45 | [diff] [blame^] | 13 | #include "content/public/test/test_utils.h" |
annekao | 3868550 | 2015-07-14 17:46:39 | [diff] [blame] | 14 | #include "extensions/test/extension_test_message_listener.h" |
| 15 | |
| 16 | namespace extensions { |
| 17 | |
| 18 | class ServiceWorkerTest : public ExtensionApiTest { |
| 19 | public: |
| 20 | // Set the channel to "trunk" since service workers are restricted to trunk. |
| 21 | ServiceWorkerTest() |
sdefresne | 6e883e4 | 2015-07-30 08:05:54 | [diff] [blame] | 22 | : current_channel_(version_info::Channel::UNKNOWN) {} |
annekao | 3868550 | 2015-07-14 17:46:39 | [diff] [blame] | 23 | |
| 24 | ~ServiceWorkerTest() override {} |
| 25 | |
| 26 | private: |
| 27 | ScopedCurrentChannel current_channel_; |
| 28 | DISALLOW_COPY_AND_ASSIGN(ServiceWorkerTest); |
| 29 | }; |
| 30 | |
| 31 | IN_PROC_BROWSER_TEST_F(ServiceWorkerTest, RegisterServiceWorkersOnTrunk) { |
| 32 | ExtensionTestMessageListener listener(false); |
annekao | 1db36fd | 2015-07-29 17:09:16 | [diff] [blame] | 33 | ASSERT_TRUE(RunExtensionTest("service_worker/register")) << message_; |
annekao | 3868550 | 2015-07-14 17:46:39 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | // This feature is restricted to trunk, so on dev it should have existing |
| 37 | // behavior - which is for it to fail. |
| 38 | IN_PROC_BROWSER_TEST_F(ServiceWorkerTest, CannotRegisterServiceWorkersOnDev) { |
| 39 | ScopedCurrentChannel current_channel_override( |
sdefresne | 6e883e4 | 2015-07-30 08:05:54 | [diff] [blame] | 40 | version_info::Channel::DEV); |
annekao | 3868550 | 2015-07-14 17:46:39 | [diff] [blame] | 41 | ExtensionTestMessageListener listener(false); |
| 42 | ASSERT_FALSE(RunExtensionTest("service_worker/register")) << message_; |
| 43 | ASSERT_TRUE(listener.WaitUntilSatisfied()); |
annekao | 1db36fd | 2015-07-29 17:09:16 | [diff] [blame] | 44 | EXPECT_EQ( |
annekao | 3868550 | 2015-07-14 17:46:39 | [diff] [blame] | 45 | "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 | |
kalman | 881d73a | 2015-09-09 08:58:46 | [diff] [blame] | 51 | // Disabled: crbug.com/529516. |
| 52 | IN_PROC_BROWSER_TEST_F(ServiceWorkerTest, DISABLED_ServiceWorkerFetchEvent) { |
annekao | 1db36fd | 2015-07-29 17:09:16 | [diff] [blame] | 53 | 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 | |
annekao | 4924118 | 2015-08-18 17:14:01 | [diff] [blame] | 74 | // Binding that was created on the v8::Context of the worker for testing |
annekao | 53348222 | 2015-08-21 23:23:53 | [diff] [blame] | 75 | // purposes should bind an object to chrome. |
kalman | 881d73a | 2015-09-09 08:58:46 | [diff] [blame] | 76 | // Disabled: crbug.com/529516. |
| 77 | IN_PROC_BROWSER_TEST_F(ServiceWorkerTest, DISABLED_ServiceWorkerChromeBinding) { |
annekao | 4924118 | 2015-08-18 17:14:01 | [diff] [blame] | 78 | 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 | |
kalman | 881d73a | 2015-09-09 08:58:46 | [diff] [blame] | 99 | // Disabled: crbug.com/529516. |
| 100 | IN_PROC_BROWSER_TEST_F(ServiceWorkerTest, DISABLED_GetBackgroundClient) { |
annekao | 53348222 | 2015-08-21 23:23:53 | [diff] [blame] | 101 | 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 | |
| 125 | IN_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.cronin | f5863da | 2015-09-10 19:21:45 | [diff] [blame^] | 134 | IN_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 | |
annekao | 3868550 | 2015-07-14 17:46:39 | [diff] [blame] | 193 | } // namespace extensions |