| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/bind_helpers.h" | 5 #include "base/bind_helpers.h" |
| 6 #include "base/strings/stringprintf.h" | 6 #include "base/strings/stringprintf.h" |
| 7 #include "chrome/browser/extensions/extension_apitest.h" | 7 #include "chrome/browser/extensions/extension_apitest.h" |
| 8 #include "chrome/browser/extensions/extension_service.h" | 8 #include "chrome/browser/extensions/extension_service.h" |
| 9 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 9 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 10 #include "chrome/test/base/ui_test_utils.h" | 10 #include "chrome/test/base/ui_test_utils.h" |
| 11 #include "components/version_info/version_info.h" | 11 #include "components/version_info/version_info.h" |
| 12 #include "content/public/browser/navigation_controller.h" | 12 #include "content/public/browser/navigation_controller.h" |
| 13 #include "content/public/browser/navigation_entry.h" | 13 #include "content/public/browser/navigation_entry.h" |
| 14 #include "content/public/browser/web_contents.h" | 14 #include "content/public/browser/web_contents.h" |
| 15 #include "content/public/common/page_type.h" | 15 #include "content/public/common/page_type.h" |
| 16 #include "content/public/test/browser_test_utils.h" | 16 #include "content/public/test/browser_test_utils.h" |
| 17 #include "extensions/browser/extension_host.h" | 17 #include "extensions/browser/extension_host.h" |
| 18 #include "extensions/browser/extension_registry.h" |
| 18 #include "extensions/browser/process_manager.h" | 19 #include "extensions/browser/process_manager.h" |
| 19 #include "extensions/test/background_page_watcher.h" | 20 #include "extensions/test/background_page_watcher.h" |
| 20 #include "extensions/test/extension_test_message_listener.h" | 21 #include "extensions/test/extension_test_message_listener.h" |
| 21 | 22 |
| 22 namespace extensions { | 23 namespace extensions { |
| 23 | 24 |
| 24 namespace { | 25 namespace { |
| 25 | 26 |
| 26 // Pass into ServiceWorkerTest::StartTestFromBackgroundPage to indicate that | 27 // Pass into ServiceWorkerTest::StartTestFromBackgroundPage to indicate that |
| 27 // registration is expected to succeed. | 28 // registration is expected to succeed. |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 std::string error; | 126 std::string error; |
| 126 const Extension* extension = | 127 const Extension* extension = |
| 127 StartTestFromBackgroundPage("register.js", &error); | 128 StartTestFromBackgroundPage("register.js", &error); |
| 128 EXPECT_EQ( | 129 EXPECT_EQ( |
| 129 "Failed to register a ServiceWorker: The URL protocol of the current " | 130 "Failed to register a ServiceWorker: The URL protocol of the current " |
| 130 "origin ('chrome-extension://" + | 131 "origin ('chrome-extension://" + |
| 131 extension->id() + "') is not supported.", | 132 extension->id() + "') is not supported.", |
| 132 error); | 133 error); |
| 133 } | 134 } |
| 134 | 135 |
| 136 IN_PROC_BROWSER_TEST_F(ServiceWorkerTest, UpdateRefreshesServiceWorker) { |
| 137 base::ScopedTempDir scoped_temp_dir; |
| 138 ASSERT_TRUE(scoped_temp_dir.CreateUniqueTempDir()); |
| 139 base::FilePath pem_path = test_data_dir_.AppendASCII("service_worker") |
| 140 .AppendASCII("update") |
| 141 .AppendASCII("service_worker.pem"); |
| 142 base::FilePath path_v1 = PackExtensionWithOptions( |
| 143 test_data_dir_.AppendASCII("service_worker") |
| 144 .AppendASCII("update") |
| 145 .AppendASCII("v1"), |
| 146 scoped_temp_dir.path().AppendASCII("v1.crx"), pem_path, base::FilePath()); |
| 147 base::FilePath path_v2 = PackExtensionWithOptions( |
| 148 test_data_dir_.AppendASCII("service_worker") |
| 149 .AppendASCII("update") |
| 150 .AppendASCII("v2"), |
| 151 scoped_temp_dir.path().AppendASCII("v2.crx"), pem_path, base::FilePath()); |
| 152 |
| 153 const char* kId = "hfaanndiiilofhfokeanhddpkfffchdi"; |
| 154 |
| 155 // Install version 1.0 of the extension. |
| 156 ASSERT_TRUE(InstallExtension(path_v1, 1)); |
| 157 EXPECT_TRUE(extensions::ExtensionRegistry::Get(profile()) |
| 158 ->enabled_extensions() |
| 159 .GetByID(kId) != NULL); |
| 160 |
| 161 const char* kScript = "window.testResolver();"; |
| 162 EXPECT_EQ("Pong from version 1", ExecuteScriptInBackgroundPage(kId, kScript)); |
| 163 |
| 164 // Update to version 2.0. |
| 165 EXPECT_TRUE(UpdateExtension(kId, path_v2, 0)); |
| 166 EXPECT_TRUE(extensions::ExtensionRegistry::Get(profile()) |
| 167 ->enabled_extensions() |
| 168 .GetByID(kId) != NULL); |
| 169 |
| 170 EXPECT_EQ("Pong from version 2", ExecuteScriptInBackgroundPage(kId, kScript)); |
| 171 } |
| 172 |
| 135 IN_PROC_BROWSER_TEST_F(ServiceWorkerTest, FetchArbitraryPaths) { | 173 IN_PROC_BROWSER_TEST_F(ServiceWorkerTest, FetchArbitraryPaths) { |
| 136 const Extension* extension = | 174 const Extension* extension = |
| 137 StartTestFromBackgroundPage("fetch.js", kExpectSuccess); | 175 StartTestFromBackgroundPage("fetch.js", kExpectSuccess); |
| 138 | 176 |
| 139 // Open some arbirary paths. Their contents should be what the service worker | 177 // Open some arbirary paths. Their contents should be what the service worker |
| 140 // responds with, which in this case is the path of the fetch. | 178 // responds with, which in this case is the path of the fetch. |
| 141 EXPECT_EQ( | 179 EXPECT_EQ( |
| 142 "Caught a fetch for /index.html", | 180 "Caught a fetch for /index.html", |
| 143 NavigateAndExtractInnerText(extension->GetResourceURL("index.html"))); | 181 NavigateAndExtractInnerText(extension->GetResourceURL("index.html"))); |
| 144 EXPECT_EQ("Caught a fetch for /path/to/other.html", | 182 EXPECT_EQ("Caught a fetch for /path/to/other.html", |
| (...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 331 IN_PROC_BROWSER_TEST_F(ServiceWorkerTest, | 369 IN_PROC_BROWSER_TEST_F(ServiceWorkerTest, |
| 332 GetBackgroundClientFailsWithNoBackgroundPage) { | 370 GetBackgroundClientFailsWithNoBackgroundPage) { |
| 333 // This extension doesn't have a background page, only a tab at page.html. | 371 // This extension doesn't have a background page, only a tab at page.html. |
| 334 // The service worker it registers tries to call getBackgroundClient() and | 372 // The service worker it registers tries to call getBackgroundClient() and |
| 335 // should fail. | 373 // should fail. |
| 336 // Note that this also tests that service workers can be registered from tabs. | 374 // Note that this also tests that service workers can be registered from tabs. |
| 337 EXPECT_TRUE(RunExtensionSubtest("service_worker/no_background", "page.html")); | 375 EXPECT_TRUE(RunExtensionSubtest("service_worker/no_background", "page.html")); |
| 338 } | 376 } |
| 339 | 377 |
| 340 } // namespace extensions | 378 } // namespace extensions |
| OLD | NEW |