| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/extensions/extension_browsertest.h" | 5 #include "chrome/browser/extensions/extension_browsertest.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <utility> | 9 #include <utility> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/command_line.h" | 12 #include "base/command_line.h" |
| 13 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
| 14 #include "base/files/file_util.h" | 14 #include "base/files/file_util.h" |
| 15 #include "base/files/scoped_temp_dir.h" | 15 #include "base/files/scoped_temp_dir.h" |
| 16 #include "base/path_service.h" | 16 #include "base/path_service.h" |
| 17 #include "base/strings/string_number_conversions.h" | 17 #include "base/strings/string_number_conversions.h" |
| 18 #include "base/strings/stringprintf.h" | 18 #include "base/strings/stringprintf.h" |
| 19 #include "base/strings/utf_string_conversions.h" | 19 #include "base/strings/utf_string_conversions.h" |
| 20 #include "base/threading/sequenced_worker_pool.h" |
| 20 #include "build/build_config.h" | 21 #include "build/build_config.h" |
| 21 #include "chrome/browser/extensions/browsertest_util.h" | 22 #include "chrome/browser/extensions/browsertest_util.h" |
| 22 #include "chrome/browser/extensions/chrome_test_extension_loader.h" | 23 #include "chrome/browser/extensions/chrome_test_extension_loader.h" |
| 23 #include "chrome/browser/extensions/component_loader.h" | 24 #include "chrome/browser/extensions/component_loader.h" |
| 24 #include "chrome/browser/extensions/crx_installer.h" | 25 #include "chrome/browser/extensions/crx_installer.h" |
| 25 #include "chrome/browser/extensions/extension_creator.h" | 26 #include "chrome/browser/extensions/extension_creator.h" |
| 26 #include "chrome/browser/extensions/extension_error_reporter.h" | 27 #include "chrome/browser/extensions/extension_error_reporter.h" |
| 27 #include "chrome/browser/extensions/extension_install_prompt.h" | 28 #include "chrome/browser/extensions/extension_install_prompt.h" |
| 28 #include "chrome/browser/extensions/extension_install_prompt_show_params.h" | 29 #include "chrome/browser/extensions/extension_install_prompt_show_params.h" |
| 29 #include "chrome/browser/extensions/extension_service.h" | 30 #include "chrome/browser/extensions/extension_service.h" |
| (...skipping 21 matching lines...) Expand all Loading... |
| 51 #include "content/public/test/test_utils.h" | 52 #include "content/public/test/test_utils.h" |
| 52 #include "extensions/browser/extension_dialog_auto_confirm.h" | 53 #include "extensions/browser/extension_dialog_auto_confirm.h" |
| 53 #include "extensions/browser/extension_host.h" | 54 #include "extensions/browser/extension_host.h" |
| 54 #include "extensions/browser/extension_prefs.h" | 55 #include "extensions/browser/extension_prefs.h" |
| 55 #include "extensions/browser/extension_registry.h" | 56 #include "extensions/browser/extension_registry.h" |
| 56 #include "extensions/browser/extension_system.h" | 57 #include "extensions/browser/extension_system.h" |
| 57 #include "extensions/browser/notification_types.h" | 58 #include "extensions/browser/notification_types.h" |
| 58 #include "extensions/browser/uninstall_reason.h" | 59 #include "extensions/browser/uninstall_reason.h" |
| 59 #include "extensions/common/constants.h" | 60 #include "extensions/common/constants.h" |
| 60 #include "extensions/common/extension_set.h" | 61 #include "extensions/common/extension_set.h" |
| 62 #include "net/url_request/url_request_file_job.h" |
| 61 | 63 |
| 62 #if defined(OS_CHROMEOS) | 64 #if defined(OS_CHROMEOS) |
| 63 #include "chromeos/chromeos_switches.h" | 65 #include "chromeos/chromeos_switches.h" |
| 64 #endif | 66 #endif |
| 65 | 67 |
| 66 using extensions::Extension; | 68 using extensions::Extension; |
| 67 using extensions::ExtensionCreator; | 69 using extensions::ExtensionCreator; |
| 68 using extensions::ExtensionRegistry; | 70 using extensions::ExtensionRegistry; |
| 69 using extensions::FeatureSwitch; | 71 using extensions::FeatureSwitch; |
| 70 using extensions::Manifest; | 72 using extensions::Manifest; |
| 71 using extensions::ScopedTestDialogAutoConfirm; | 73 using extensions::ScopedTestDialogAutoConfirm; |
| 72 | 74 |
| 75 namespace { |
| 76 |
| 77 // Maps all chrome-extension://<id>/_test_resources/foo requests to |
| 78 // chrome/test/data/extensions/foo. This is what allows us to share code between |
| 79 // tests without needing to duplicate files in each extension. |
| 80 net::URLRequestJob* ExtensionProtocolTestHandler( |
| 81 const base::FilePath& test_dir_root, |
| 82 net::URLRequest* request, |
| 83 net::NetworkDelegate* network_delegate, |
| 84 const base::FilePath& relative_path) { |
| 85 // Only map paths that begin with _test_resources. |
| 86 if (!base::FilePath(FILE_PATH_LITERAL("_test_resources")) |
| 87 .IsParent(relative_path)) { |
| 88 return nullptr; |
| 89 } |
| 90 |
| 91 // Replace _test_resources/foo with chrome/test/data/foo. |
| 92 std::vector<base::FilePath::StringType> components; |
| 93 relative_path.GetComponents(&components); |
| 94 DCHECK_GT(components.size(), 1u); |
| 95 base::FilePath resource_path = test_dir_root; |
| 96 for (size_t i = 1u; i < components.size(); ++i) |
| 97 resource_path = resource_path.Append(components[i]); |
| 98 |
| 99 return new net::URLRequestFileJob( |
| 100 request, network_delegate, resource_path, |
| 101 content::BrowserThread::GetBlockingPool() |
| 102 ->GetTaskRunnerWithShutdownBehavior( |
| 103 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)); |
| 104 } |
| 105 |
| 106 } // namespace |
| 107 |
| 73 ExtensionBrowserTest::ExtensionBrowserTest() | 108 ExtensionBrowserTest::ExtensionBrowserTest() |
| 74 : loaded_(false), | 109 : loaded_(false), |
| 75 installed_(false), | 110 installed_(false), |
| 76 #if defined(OS_CHROMEOS) | 111 #if defined(OS_CHROMEOS) |
| 77 set_chromeos_user_(true), | 112 set_chromeos_user_(true), |
| 78 #endif | 113 #endif |
| 79 // Default channel is STABLE but override with UNKNOWN so that unlaunched | 114 // Default channel is STABLE but override with UNKNOWN so that unlaunched |
| 80 // or incomplete APIs can write tests. | 115 // or incomplete APIs can write tests. |
| 81 current_channel_(version_info::Channel::UNKNOWN), | 116 current_channel_(version_info::Channel::UNKNOWN), |
| 82 override_prompt_for_external_extensions_( | 117 override_prompt_for_external_extensions_( |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 146 } | 181 } |
| 147 | 182 |
| 148 void ExtensionBrowserTest::SetUpOnMainThread() { | 183 void ExtensionBrowserTest::SetUpOnMainThread() { |
| 149 InProcessBrowserTest::SetUpOnMainThread(); | 184 InProcessBrowserTest::SetUpOnMainThread(); |
| 150 observer_.reset( | 185 observer_.reset( |
| 151 new extensions::ChromeExtensionTestNotificationObserver(browser())); | 186 new extensions::ChromeExtensionTestNotificationObserver(browser())); |
| 152 if (extension_service()->updater()) { | 187 if (extension_service()->updater()) { |
| 153 extension_service()->updater()->SetExtensionCacheForTesting( | 188 extension_service()->updater()->SetExtensionCacheForTesting( |
| 154 test_extension_cache_.get()); | 189 test_extension_cache_.get()); |
| 155 } | 190 } |
| 191 |
| 192 // We don't use test_data_dir_ here because we want this to point to |
| 193 // chrome/test/data/extensions, and subclasses have a nasty habit of altering |
| 194 // the data dir in SetUpCommandLine(). |
| 195 base::FilePath test_root_path; |
| 196 PathService::Get(chrome::DIR_TEST_DATA, &test_root_path); |
| 197 test_root_path = test_root_path.AppendASCII("extensions"); |
| 198 test_protocol_handler_ = |
| 199 base::Bind(&ExtensionProtocolTestHandler, test_root_path); |
| 200 extensions::SetExtensionProtocolTestHandler(&test_protocol_handler_); |
| 156 } | 201 } |
| 157 | 202 |
| 158 void ExtensionBrowserTest::TearDownOnMainThread() { | 203 void ExtensionBrowserTest::TearDownOnMainThread() { |
| 159 ExtensionMessageBubbleFactory::set_override_for_tests( | 204 ExtensionMessageBubbleFactory::set_override_for_tests( |
| 160 ExtensionMessageBubbleFactory::NO_OVERRIDE); | 205 ExtensionMessageBubbleFactory::NO_OVERRIDE); |
| 206 extensions::SetExtensionProtocolTestHandler(nullptr); |
| 161 InProcessBrowserTest::TearDownOnMainThread(); | 207 InProcessBrowserTest::TearDownOnMainThread(); |
| 162 } | 208 } |
| 163 | 209 |
| 164 const Extension* ExtensionBrowserTest::LoadExtension( | 210 const Extension* ExtensionBrowserTest::LoadExtension( |
| 165 const base::FilePath& path) { | 211 const base::FilePath& path) { |
| 166 return LoadExtensionWithFlags(path, kFlagEnableFileAccess); | 212 return LoadExtensionWithFlags(path, kFlagEnableFileAccess); |
| 167 } | 213 } |
| 168 | 214 |
| 169 const Extension* ExtensionBrowserTest::LoadExtensionIncognito( | 215 const Extension* ExtensionBrowserTest::LoadExtensionIncognito( |
| 170 const base::FilePath& path) { | 216 const base::FilePath& path) { |
| (...skipping 380 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 551 return extensions::browsertest_util::ExecuteScriptInBackgroundPage( | 597 return extensions::browsertest_util::ExecuteScriptInBackgroundPage( |
| 552 profile(), extension_id, script); | 598 profile(), extension_id, script); |
| 553 } | 599 } |
| 554 | 600 |
| 555 bool ExtensionBrowserTest::ExecuteScriptInBackgroundPageNoWait( | 601 bool ExtensionBrowserTest::ExecuteScriptInBackgroundPageNoWait( |
| 556 const std::string& extension_id, | 602 const std::string& extension_id, |
| 557 const std::string& script) { | 603 const std::string& script) { |
| 558 return extensions::browsertest_util::ExecuteScriptInBackgroundPageNoWait( | 604 return extensions::browsertest_util::ExecuteScriptInBackgroundPageNoWait( |
| 559 profile(), extension_id, script); | 605 profile(), extension_id, script); |
| 560 } | 606 } |
| OLD | NEW |