[email protected] | 29af201 | 2012-01-02 16:28:10 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 25fd1b2e | 2009-08-17 20:57:14 | [diff] [blame] | 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" |
| 6 | |
avi | a2f4804a | 2015-12-24 23:11:13 | [diff] [blame] | 7 | #include <stddef.h> |
dcheng | 1fc00f1 | 2015-12-26 22:18:03 | [diff] [blame] | 8 | #include <utility> |
avi | a2f4804a | 2015-12-24 23:11:13 | [diff] [blame] | 9 | |
shrike | 8fbe9d3 | 2015-06-02 19:53:57 | [diff] [blame] | 10 | #include "base/base_switches.h" |
Sebastien Marchand | f1349f5 | 2019-01-25 03:16:41 | [diff] [blame] | 11 | #include "base/bind.h" |
Devlin Cronin | b15f7f0 | 2018-01-31 19:37:32 | [diff] [blame] | 12 | #include "base/feature_list.h" |
Michael Giuffrida | 352663b | 2017-08-04 01:48:51 | [diff] [blame] | 13 | #include "base/files/file_path.h" |
| 14 | #include "base/path_service.h" |
[email protected] | c1dffe8 | 2013-06-26 20:59:05 | [diff] [blame] | 15 | #include "base/strings/string_split.h" |
[email protected] | 00e7bef | 2013-06-10 20:35:17 | [diff] [blame] | 16 | #include "base/strings/string_util.h" |
| 17 | #include "base/strings/stringprintf.h" |
avi | a2f4804a | 2015-12-24 23:11:13 | [diff] [blame] | 18 | #include "build/build_config.h" |
[email protected] | 06bdd2b | 2012-11-30 18:47:13 | [diff] [blame] | 19 | #include "chrome/browser/extensions/extension_service.h" |
[email protected] | bbcde910 | 2012-03-25 22:40:49 | [diff] [blame] | 20 | #include "chrome/browser/extensions/unpacked_installer.h" |
| 21 | #include "chrome/browser/profiles/profile.h" |
[email protected] | 7b5dc00 | 2010-11-16 23:08:10 | [diff] [blame] | 22 | #include "chrome/browser/ui/browser.h" |
cylee | 49e02a9 | 2014-12-01 19:03:39 | [diff] [blame] | 23 | #include "chrome/browser/ui/extensions/app_launch_params.h" |
[email protected] | bd507a9 | 2012-05-23 08:32:05 | [diff] [blame] | 24 | #include "chrome/browser/ui/extensions/application_launch.h" |
[email protected] | af44e7fb | 2011-07-29 18:32:32 | [diff] [blame] | 25 | #include "chrome/test/base/ui_test_utils.h" |
shrike | 8fbe9d3 | 2015-06-02 19:53:57 | [diff] [blame] | 26 | #include "content/public/common/content_switches.h" |
[email protected] | 09186d83 | 2014-04-04 01:33:33 | [diff] [blame] | 27 | #include "extensions/browser/api/test/test_api.h" |
reillyg | a3acbc1 | 2014-11-11 23:17:12 | [diff] [blame] | 28 | #include "extensions/browser/extension_registry.h" |
[email protected] | 59b0e60 | 2014-01-30 00:41:24 | [diff] [blame] | 29 | #include "extensions/browser/extension_system.h" |
cylee | 49e02a9 | 2014-12-01 19:03:39 | [diff] [blame] | 30 | #include "extensions/common/constants.h" |
[email protected] | e4452d3 | 2013-11-15 23:07:41 | [diff] [blame] | 31 | #include "extensions/common/extension.h" |
Michael Giuffrida | 352663b | 2017-08-04 01:48:51 | [diff] [blame] | 32 | #include "extensions/common/extension_paths.h" |
[email protected] | 289c44b | 2013-12-17 03:26:57 | [diff] [blame] | 33 | #include "extensions/common/extension_set.h" |
Devlin Cronin | b15f7f0 | 2018-01-31 19:37:32 | [diff] [blame] | 34 | #include "extensions/common/feature_switch.h" |
alexmos | 9d656a8 | 2015-12-02 02:03:09 | [diff] [blame] | 35 | #include "extensions/common/switches.h" |
yoz | e8dc2f1 | 2014-09-09 23:16:32 | [diff] [blame] | 36 | #include "extensions/test/result_catcher.h" |
[email protected] | c1dffe8 | 2013-06-26 20:59:05 | [diff] [blame] | 37 | #include "net/base/escape.h" |
[email protected] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 38 | #include "net/base/filename_util.h" |
Karandeep Bhatia | 785b3db8d | 2019-03-05 05:37:57 | [diff] [blame] | 39 | #include "net/test/embedded_test_server/default_handlers.h" |
[email protected] | c1dffe8 | 2013-06-26 20:59:05 | [diff] [blame] | 40 | #include "net/test/embedded_test_server/embedded_test_server.h" |
[email protected] | c1dffe8 | 2013-06-26 20:59:05 | [diff] [blame] | 41 | #include "net/test/embedded_test_server/http_request.h" |
[email protected] | e4452d3 | 2013-11-15 23:07:41 | [diff] [blame] | 42 | #include "net/test/embedded_test_server/http_response.h" |
Matt Falkenhagen | 29950eb | 2019-02-28 23:07:13 | [diff] [blame] | 43 | #include "net/test/embedded_test_server/request_handler_util.h" |
[email protected] | 89b3252 | 2013-05-07 20:04:21 | [diff] [blame] | 44 | #include "net/test/spawned_test_server/spawned_test_server.h" |
[email protected] | 25fd1b2e | 2009-08-17 20:57:14 | [diff] [blame] | 45 | |
Devlin Cronin | ef3e37e | 2018-05-14 23:47:24 | [diff] [blame] | 46 | namespace extensions { |
[email protected] | 761e716 | 2010-10-18 19:26:39 | [diff] [blame] | 47 | |
Devlin Cronin | ef3e37e | 2018-05-14 23:47:24 | [diff] [blame] | 48 | namespace { |
Devlin Cronin | 836f545d | 2018-05-09 00:25:05 | [diff] [blame] | 49 | |
[email protected] | c1d9b2b | 2013-06-13 19:46:59 | [diff] [blame] | 50 | const char kTestCustomArg[] = "customArg"; |
[email protected] | 8f06336 | 2011-11-18 00:32:53 | [diff] [blame] | 51 | const char kTestDataDirectory[] = "testDataDirectory"; |
[email protected] | 8913c61 | 2012-03-20 01:04:15 | [diff] [blame] | 52 | const char kTestWebSocketPort[] = "testWebSocketPort"; |
[email protected] | a0e7579 | 2014-03-13 13:12:17 | [diff] [blame] | 53 | const char kFtpServerPort[] = "ftpServer.port"; |
svaldez | a01f7d9 | 2015-11-18 17:47:56 | [diff] [blame] | 54 | const char kEmbeddedTestServerPort[] = "testServer.port"; |
[email protected] | 761e716 | 2010-10-18 19:26:39 | [diff] [blame] | 55 | |
Nico Weber | 42c88d6 | 2019-02-11 03:09:25 | [diff] [blame] | 56 | } // namespace |
[email protected] | 761e716 | 2010-10-18 19:26:39 | [diff] [blame] | 57 | |
[email protected] | c1dffe8 | 2013-06-26 20:59:05 | [diff] [blame] | 58 | ExtensionApiTest::ExtensionApiTest() { |
Karandeep Bhatia | 785b3db8d | 2019-03-05 05:37:57 | [diff] [blame] | 59 | net::test_server::RegisterDefaultHandlers(embedded_test_server()); |
[email protected] | c1dffe8 | 2013-06-26 20:59:05 | [diff] [blame] | 60 | } |
[email protected] | 5322a7f | 2011-02-11 20:44:42 | [diff] [blame] | 61 | |
Karandeep Bhatia | 785b3db8d | 2019-03-05 05:37:57 | [diff] [blame] | 62 | ExtensionApiTest::~ExtensionApiTest() = default; |
[email protected] | 5322a7f | 2011-02-11 20:44:42 | [diff] [blame] | 63 | |
nasko | d81f57c5 | 2017-02-15 22:46:49 | [diff] [blame] | 64 | void ExtensionApiTest::SetUpOnMainThread() { |
| 65 | ExtensionBrowserTest::SetUpOnMainThread(); |
[email protected] | 761e716 | 2010-10-18 19:26:39 | [diff] [blame] | 66 | DCHECK(!test_config_.get()) << "Previous test did not clear config state."; |
[email protected] | 023b3d1 | 2013-12-23 18:46:49 | [diff] [blame] | 67 | test_config_.reset(new base::DictionaryValue()); |
[email protected] | 8f06336 | 2011-11-18 00:32:53 | [diff] [blame] | 68 | test_config_->SetString(kTestDataDirectory, |
| 69 | net::FilePathToFileURL(test_data_dir_).spec()); |
Karandeep Bhatia | 785b3db8d | 2019-03-05 05:37:57 | [diff] [blame] | 70 | |
Rob Wu | 2649de1 | 2018-01-09 22:54:47 | [diff] [blame] | 71 | if (embedded_test_server()->Started()) { |
| 72 | // InitializeEmbeddedTestServer was called before |test_config_| was set. |
| 73 | // Set the missing port key. |
| 74 | test_config_->SetInteger(kEmbeddedTestServerPort, |
| 75 | embedded_test_server()->port()); |
| 76 | } |
Devlin Cronin | 242d19d2 | 2019-03-12 18:08:48 | [diff] [blame] | 77 | |
Devlin Cronin | ef3e37e | 2018-05-14 23:47:24 | [diff] [blame] | 78 | TestGetConfigFunction::set_test_config_state(test_config_.get()); |
[email protected] | 761e716 | 2010-10-18 19:26:39 | [diff] [blame] | 79 | } |
| 80 | |
nasko | d81f57c5 | 2017-02-15 22:46:49 | [diff] [blame] | 81 | void ExtensionApiTest::TearDownOnMainThread() { |
| 82 | ExtensionBrowserTest::TearDownOnMainThread(); |
Devlin Cronin | ef3e37e | 2018-05-14 23:47:24 | [diff] [blame] | 83 | TestGetConfigFunction::set_test_config_state(NULL); |
[email protected] | 761e716 | 2010-10-18 19:26:39 | [diff] [blame] | 84 | test_config_.reset(NULL); |
| 85 | } |
| 86 | |
[email protected] | 4154716 | 2013-09-24 04:26:50 | [diff] [blame] | 87 | bool ExtensionApiTest::RunExtensionTest(const std::string& extension_name) { |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 88 | return RunExtensionTestImpl( |
[email protected] | c1d9b2b | 2013-06-13 19:46:59 | [diff] [blame] | 89 | extension_name, std::string(), NULL, kFlagEnableFileAccess); |
[email protected] | a320e51 | 2010-09-21 09:58:01 | [diff] [blame] | 90 | } |
| 91 | |
Michael Giuffrida | 352663b | 2017-08-04 01:48:51 | [diff] [blame] | 92 | bool ExtensionApiTest::RunExtensionTestWithFlags( |
| 93 | const std::string& extension_name, |
| 94 | int flags) { |
| 95 | return RunExtensionTestImpl(extension_name, std::string(), nullptr, flags); |
| 96 | } |
| 97 | |
dcheng | 7a9e836 | 2016-04-01 19:09:31 | [diff] [blame] | 98 | bool ExtensionApiTest::RunExtensionTestWithArg( |
| 99 | const std::string& extension_name, |
| 100 | const char* custom_arg) { |
| 101 | return RunExtensionTestImpl(extension_name, std::string(), custom_arg, |
| 102 | kFlagEnableFileAccess); |
| 103 | } |
| 104 | |
Bettina | 0660e087 | 2018-12-10 21:03:02 | [diff] [blame] | 105 | bool ExtensionApiTest::RunExtensionTestWithFlagsAndArg( |
| 106 | const std::string& extension_name, |
| 107 | const char* custom_arg, |
| 108 | int flag) { |
| 109 | return RunExtensionTestImpl(extension_name, std::string(), custom_arg, flag); |
| 110 | } |
| 111 | |
[email protected] | 4154716 | 2013-09-24 04:26:50 | [diff] [blame] | 112 | bool ExtensionApiTest::RunExtensionTestIncognito( |
| 113 | const std::string& extension_name) { |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 114 | return RunExtensionTestImpl(extension_name, |
| 115 | std::string(), |
[email protected] | c1d9b2b | 2013-06-13 19:46:59 | [diff] [blame] | 116 | NULL, |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 117 | kFlagEnableIncognito | kFlagEnableFileAccess); |
[email protected] | 61b55b6 | 2011-03-24 09:03:10 | [diff] [blame] | 118 | } |
| 119 | |
[email protected] | 24e0429 | 2012-08-10 21:08:08 | [diff] [blame] | 120 | bool ExtensionApiTest::RunExtensionTestIgnoreManifestWarnings( |
[email protected] | 4154716 | 2013-09-24 04:26:50 | [diff] [blame] | 121 | const std::string& extension_name) { |
[email protected] | 24e0429 | 2012-08-10 21:08:08 | [diff] [blame] | 122 | return RunExtensionTestImpl( |
[email protected] | c1d9b2b | 2013-06-13 19:46:59 | [diff] [blame] | 123 | extension_name, std::string(), NULL, kFlagIgnoreManifestWarnings); |
[email protected] | b7462f3 | 2012-09-02 15:18:12 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | bool ExtensionApiTest::RunExtensionTestAllowOldManifestVersion( |
[email protected] | 4154716 | 2013-09-24 04:26:50 | [diff] [blame] | 127 | const std::string& extension_name) { |
[email protected] | b7462f3 | 2012-09-02 15:18:12 | [diff] [blame] | 128 | return RunExtensionTestImpl( |
| 129 | extension_name, |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 130 | std::string(), |
[email protected] | c1d9b2b | 2013-06-13 19:46:59 | [diff] [blame] | 131 | NULL, |
[email protected] | b7462f3 | 2012-09-02 15:18:12 | [diff] [blame] | 132 | kFlagEnableFileAccess | kFlagAllowOldManifestVersions); |
[email protected] | 24e0429 | 2012-08-10 21:08:08 | [diff] [blame] | 133 | } |
| 134 | |
[email protected] | 4154716 | 2013-09-24 04:26:50 | [diff] [blame] | 135 | bool ExtensionApiTest::RunComponentExtensionTest( |
| 136 | const std::string& extension_name) { |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 137 | return RunExtensionTestImpl(extension_name, |
| 138 | std::string(), |
[email protected] | c1d9b2b | 2013-06-13 19:46:59 | [diff] [blame] | 139 | NULL, |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 140 | kFlagEnableFileAccess | kFlagLoadAsComponent); |
[email protected] | ff65139 | 2010-07-23 20:21:08 | [diff] [blame] | 141 | } |
[email protected] | 80d6a442 | 2009-10-15 06:59:22 | [diff] [blame] | 142 | |
[email protected] | d84d10b0b | 2018-11-16 22:17:26 | [diff] [blame] | 143 | bool ExtensionApiTest::RunComponentExtensionTestWithArg( |
| 144 | const std::string& extension_name, |
| 145 | const char* custom_arg) { |
| 146 | return RunExtensionTestImpl(extension_name, std::string(), custom_arg, |
| 147 | kFlagEnableFileAccess | kFlagLoadAsComponent); |
| 148 | } |
| 149 | |
[email protected] | c7c401d | 2011-03-16 10:20:01 | [diff] [blame] | 150 | bool ExtensionApiTest::RunExtensionTestNoFileAccess( |
[email protected] | 4154716 | 2013-09-24 04:26:50 | [diff] [blame] | 151 | const std::string& extension_name) { |
[email protected] | c1d9b2b | 2013-06-13 19:46:59 | [diff] [blame] | 152 | return RunExtensionTestImpl(extension_name, std::string(), NULL, kFlagNone); |
[email protected] | c7c401d | 2011-03-16 10:20:01 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | bool ExtensionApiTest::RunExtensionTestIncognitoNoFileAccess( |
[email protected] | 4154716 | 2013-09-24 04:26:50 | [diff] [blame] | 156 | const std::string& extension_name) { |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 157 | return RunExtensionTestImpl( |
[email protected] | c1d9b2b | 2013-06-13 19:46:59 | [diff] [blame] | 158 | extension_name, std::string(), NULL, kFlagEnableIncognito); |
[email protected] | c7c401d | 2011-03-16 10:20:01 | [diff] [blame] | 159 | } |
[email protected] | d10f460 | 2011-06-16 15:44:50 | [diff] [blame] | 160 | |
[email protected] | 4154716 | 2013-09-24 04:26:50 | [diff] [blame] | 161 | bool ExtensionApiTest::RunExtensionSubtest(const std::string& extension_name, |
[email protected] | dd9d627 | 2010-09-09 17:33:18 | [diff] [blame] | 162 | const std::string& page_url) { |
Ken Rockot | 0cf16c3 | 2018-03-01 16:50:49 | [diff] [blame] | 163 | return RunExtensionSubtestWithArgAndFlags(extension_name, page_url, nullptr, |
| 164 | kFlagEnableFileAccess); |
[email protected] | ff65139 | 2010-07-23 20:21:08 | [diff] [blame] | 165 | } |
| 166 | |
[email protected] | 4154716 | 2013-09-24 04:26:50 | [diff] [blame] | 167 | bool ExtensionApiTest::RunExtensionSubtest(const std::string& extension_name, |
[email protected] | 728a660 | 2012-03-23 19:55:29 | [diff] [blame] | 168 | const std::string& page_url, |
| 169 | int flags) { |
Ken Rockot | 0cf16c3 | 2018-03-01 16:50:49 | [diff] [blame] | 170 | return RunExtensionSubtestWithArgAndFlags(extension_name, page_url, nullptr, |
| 171 | flags); |
| 172 | } |
| 173 | |
| 174 | bool ExtensionApiTest::RunExtensionSubtestWithArg( |
| 175 | const std::string& extension_name, |
| 176 | const std::string& page_url, |
| 177 | const char* custom_arg) { |
| 178 | return RunExtensionSubtestWithArgAndFlags(extension_name, page_url, |
| 179 | custom_arg, kFlagEnableFileAccess); |
| 180 | } |
| 181 | |
| 182 | bool ExtensionApiTest::RunExtensionSubtestWithArgAndFlags( |
| 183 | const std::string& extension_name, |
| 184 | const std::string& page_url, |
| 185 | const char* custom_arg, |
| 186 | int flags) { |
[email protected] | 22121e3 | 2012-03-01 05:24:59 | [diff] [blame] | 187 | DCHECK(!page_url.empty()) << "Argument page_url is required."; |
Ken Rockot | 0cf16c3 | 2018-03-01 16:50:49 | [diff] [blame] | 188 | return RunExtensionTestImpl(extension_name, page_url, custom_arg, flags); |
[email protected] | 22121e3 | 2012-03-01 05:24:59 | [diff] [blame] | 189 | } |
| 190 | |
[email protected] | dd9d627 | 2010-09-09 17:33:18 | [diff] [blame] | 191 | bool ExtensionApiTest::RunPageTest(const std::string& page_url) { |
[email protected] | 4154716 | 2013-09-24 04:26:50 | [diff] [blame] | 192 | return RunExtensionSubtest(std::string(), page_url); |
[email protected] | dd9d627 | 2010-09-09 17:33:18 | [diff] [blame] | 193 | } |
[email protected] | ff65139 | 2010-07-23 20:21:08 | [diff] [blame] | 194 | |
[email protected] | 95879f1 | 2012-04-06 23:59:49 | [diff] [blame] | 195 | bool ExtensionApiTest::RunPageTest(const std::string& page_url, |
| 196 | int flags) { |
[email protected] | 4154716 | 2013-09-24 04:26:50 | [diff] [blame] | 197 | return RunExtensionSubtest(std::string(), page_url, flags); |
[email protected] | 95879f1 | 2012-04-06 23:59:49 | [diff] [blame] | 198 | } |
| 199 | |
[email protected] | 4154716 | 2013-09-24 04:26:50 | [diff] [blame] | 200 | bool ExtensionApiTest::RunPlatformAppTest(const std::string& extension_name) { |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 201 | return RunExtensionTestImpl( |
[email protected] | c1d9b2b | 2013-06-13 19:46:59 | [diff] [blame] | 202 | extension_name, std::string(), NULL, kFlagLaunchPlatformApp); |
| 203 | } |
| 204 | |
| 205 | bool ExtensionApiTest::RunPlatformAppTestWithArg( |
[email protected] | 4154716 | 2013-09-24 04:26:50 | [diff] [blame] | 206 | const std::string& extension_name, const char* custom_arg) { |
lazyboy | 7d3240ef | 2016-05-26 03:06:47 | [diff] [blame] | 207 | return RunPlatformAppTestWithFlags(extension_name, custom_arg, kFlagNone); |
[email protected] | 863e647 | 2012-01-24 19:33:58 | [diff] [blame] | 208 | } |
| 209 | |
[email protected] | c89ab53 | 2013-12-06 12:58:32 | [diff] [blame] | 210 | bool ExtensionApiTest::RunPlatformAppTestWithFlags( |
| 211 | const std::string& extension_name, int flags) { |
| 212 | return RunExtensionTestImpl( |
| 213 | extension_name, std::string(), NULL, flags | kFlagLaunchPlatformApp); |
| 214 | } |
| 215 | |
lazyboy | 7d3240ef | 2016-05-26 03:06:47 | [diff] [blame] | 216 | bool ExtensionApiTest::RunPlatformAppTestWithFlags( |
| 217 | const std::string& extension_name, |
| 218 | const char* custom_arg, |
| 219 | int flags) { |
| 220 | return RunExtensionTestImpl(extension_name, std::string(), custom_arg, |
| 221 | flags | kFlagLaunchPlatformApp); |
| 222 | } |
| 223 | |
| 224 | |
[email protected] | dd9d627 | 2010-09-09 17:33:18 | [diff] [blame] | 225 | // Load |extension_name| extension and/or |page_url| and wait for |
| 226 | // PASSED or FAILED notification. |
[email protected] | 4154716 | 2013-09-24 04:26:50 | [diff] [blame] | 227 | bool ExtensionApiTest::RunExtensionTestImpl(const std::string& extension_name, |
[email protected] | a320e51 | 2010-09-21 09:58:01 | [diff] [blame] | 228 | const std::string& page_url, |
[email protected] | c1d9b2b | 2013-06-13 19:46:59 | [diff] [blame] | 229 | const char* custom_arg, |
[email protected] | 863e647 | 2012-01-24 19:33:58 | [diff] [blame] | 230 | int flags) { |
[email protected] | 863e647 | 2012-01-24 19:33:58 | [diff] [blame] | 231 | bool load_as_component = (flags & kFlagLoadAsComponent) != 0; |
[email protected] | dc37b00 | 2012-04-23 23:02:26 | [diff] [blame] | 232 | bool launch_platform_app = (flags & kFlagLaunchPlatformApp) != 0; |
[email protected] | 728a660 | 2012-03-23 19:55:29 | [diff] [blame] | 233 | bool use_incognito = (flags & kFlagUseIncognito) != 0; |
Michael Giuffrida | 352663b | 2017-08-04 01:48:51 | [diff] [blame] | 234 | bool use_root_extensions_dir = (flags & kFlagUseRootExtensionsDir) != 0; |
[email protected] | 863e647 | 2012-01-24 19:33:58 | [diff] [blame] | 235 | |
[email protected] | 4154716 | 2013-09-24 04:26:50 | [diff] [blame] | 236 | if (custom_arg && custom_arg[0]) |
Pavol Marko | cad963db | 2018-02-15 11:02:49 | [diff] [blame] | 237 | SetCustomArg(custom_arg); |
[email protected] | c1d9b2b | 2013-06-13 19:46:59 | [diff] [blame] | 238 | |
Devlin Cronin | ef3e37e | 2018-05-14 23:47:24 | [diff] [blame] | 239 | ResultCatcher catcher; |
[email protected] | 4154716 | 2013-09-24 04:26:50 | [diff] [blame] | 240 | DCHECK(!extension_name.empty() || !page_url.empty()) << |
[email protected] | dd9d627 | 2010-09-09 17:33:18 | [diff] [blame] | 241 | "extension_name and page_url cannot both be empty"; |
[email protected] | dd9d627 | 2010-09-09 17:33:18 | [diff] [blame] | 242 | |
Devlin Cronin | ef3e37e | 2018-05-14 23:47:24 | [diff] [blame] | 243 | const Extension* extension = NULL; |
[email protected] | 4154716 | 2013-09-24 04:26:50 | [diff] [blame] | 244 | if (!extension_name.empty()) { |
Michael Giuffrida | 352663b | 2017-08-04 01:48:51 | [diff] [blame] | 245 | const base::FilePath& root_path = |
| 246 | use_root_extensions_dir ? shared_test_data_dir_ : test_data_dir_; |
| 247 | base::FilePath extension_path = root_path.AppendASCII(extension_name); |
[email protected] | 61b55b6 | 2011-03-24 09:03:10 | [diff] [blame] | 248 | if (load_as_component) { |
[email protected] | 863e647 | 2012-01-24 19:33:58 | [diff] [blame] | 249 | extension = LoadExtensionAsComponent(extension_path); |
[email protected] | c7c401d | 2011-03-16 10:20:01 | [diff] [blame] | 250 | } else { |
[email protected] | 24e0429 | 2012-08-10 21:08:08 | [diff] [blame] | 251 | int browser_test_flags = ExtensionBrowserTest::kFlagNone; |
| 252 | if (flags & kFlagEnableIncognito) |
| 253 | browser_test_flags |= ExtensionBrowserTest::kFlagEnableIncognito; |
| 254 | if (flags & kFlagEnableFileAccess) |
| 255 | browser_test_flags |= ExtensionBrowserTest::kFlagEnableFileAccess; |
| 256 | if (flags & kFlagIgnoreManifestWarnings) |
| 257 | browser_test_flags |= ExtensionBrowserTest::kFlagIgnoreManifestWarnings; |
[email protected] | b7462f3 | 2012-09-02 15:18:12 | [diff] [blame] | 258 | if (flags & kFlagAllowOldManifestVersions) { |
| 259 | browser_test_flags |= |
| 260 | ExtensionBrowserTest::kFlagAllowOldManifestVersions; |
| 261 | } |
Alexander Hendrich | 5f6d6a2c | 2019-05-30 00:45:11 | [diff] [blame] | 262 | if (flags & kFlagLoadForLoginScreen) |
| 263 | browser_test_flags |= ExtensionBrowserTest::kFlagLoadForLoginScreen; |
[email protected] | 24e0429 | 2012-08-10 21:08:08 | [diff] [blame] | 264 | extension = LoadExtensionWithFlags(extension_path, browser_test_flags); |
[email protected] | c7c401d | 2011-03-16 10:20:01 | [diff] [blame] | 265 | } |
[email protected] | 863e647 | 2012-01-24 19:33:58 | [diff] [blame] | 266 | if (!extension) { |
[email protected] | dd9d627 | 2010-09-09 17:33:18 | [diff] [blame] | 267 | message_ = "Failed to load extension."; |
| 268 | return false; |
| 269 | } |
[email protected] | 80d6a442 | 2009-10-15 06:59:22 | [diff] [blame] | 270 | } |
| 271 | |
[email protected] | dd9d627 | 2010-09-09 17:33:18 | [diff] [blame] | 272 | // If there is a page_url to load, navigate it. |
| 273 | if (!page_url.empty()) { |
| 274 | GURL url = GURL(page_url); |
[email protected] | ff65139 | 2010-07-23 20:21:08 | [diff] [blame] | 275 | |
[email protected] | dd9d627 | 2010-09-09 17:33:18 | [diff] [blame] | 276 | // Note: We use is_valid() here in the expectation that the provided url |
| 277 | // may lack a scheme & host and thus be a relative url within the loaded |
| 278 | // extension. |
| 279 | if (!url.is_valid()) { |
[email protected] | 4154716 | 2013-09-24 04:26:50 | [diff] [blame] | 280 | DCHECK(!extension_name.empty()) << |
[email protected] | dd9d627 | 2010-09-09 17:33:18 | [diff] [blame] | 281 | "Relative page_url given with no extension_name"; |
| 282 | |
[email protected] | dd9d627 | 2010-09-09 17:33:18 | [diff] [blame] | 283 | url = extension->GetResourceURL(page_url); |
| 284 | } |
| 285 | |
[email protected] | 728a660 | 2012-03-23 19:55:29 | [diff] [blame] | 286 | if (use_incognito) |
erikchen | ff8b5c7a | 2015-07-13 23:41:20 | [diff] [blame] | 287 | OpenURLOffTheRecord(browser()->profile(), url); |
[email protected] | 728a660 | 2012-03-23 19:55:29 | [diff] [blame] | 288 | else |
| 289 | ui_test_utils::NavigateToURL(browser(), url); |
[email protected] | dc37b00 | 2012-04-23 23:02:26 | [diff] [blame] | 290 | } else if (launch_platform_app) { |
Devlin Cronin | ef3e37e | 2018-05-14 23:47:24 | [diff] [blame] | 291 | AppLaunchParams params(browser()->profile(), extension, |
| 292 | LAUNCH_CONTAINER_NONE, |
| 293 | WindowOpenDisposition::NEW_WINDOW, SOURCE_TEST); |
avi | 3ef9ec9e | 2014-12-22 22:50:17 | [diff] [blame] | 294 | params.command_line = *base::CommandLine::ForCurrentProcess(); |
[email protected] | 74b22110 | 2013-09-27 12:10:27 | [diff] [blame] | 295 | OpenApplication(params); |
[email protected] | ff65139 | 2010-07-23 20:21:08 | [diff] [blame] | 296 | } |
| 297 | |
[email protected] | 80d6a442 | 2009-10-15 06:59:22 | [diff] [blame] | 298 | if (!catcher.GetNextResult()) { |
| 299 | message_ = catcher.message(); |
| 300 | return false; |
[email protected] | 80d6a442 | 2009-10-15 06:59:22 | [diff] [blame] | 301 | } |
[email protected] | 4154716 | 2013-09-24 04:26:50 | [diff] [blame] | 302 | |
| 303 | return true; |
[email protected] | 80d6a442 | 2009-10-15 06:59:22 | [diff] [blame] | 304 | } |
| 305 | |
[email protected] | 84df833 | 2011-12-06 18:22:46 | [diff] [blame] | 306 | // Test that exactly one extension is loaded, and return it. |
Devlin Cronin | ef3e37e | 2018-05-14 23:47:24 | [diff] [blame] | 307 | const Extension* ExtensionApiTest::GetSingleLoadedExtension() { |
| 308 | ExtensionRegistry* registry = ExtensionRegistry::Get(browser()->profile()); |
[email protected] | 1952c7d | 2010-03-04 23:48:34 | [diff] [blame] | 309 | |
Devlin Cronin | ef3e37e | 2018-05-14 23:47:24 | [diff] [blame] | 310 | const Extension* result = NULL; |
| 311 | for (const scoped_refptr<const Extension>& extension : |
reillyg | a3acbc1 | 2014-11-11 23:17:12 | [diff] [blame] | 312 | registry->enabled_extensions()) { |
[email protected] | 1952c7d | 2010-03-04 23:48:34 | [diff] [blame] | 313 | // Ignore any component extensions. They are automatically loaded into all |
| 314 | // profiles and aren't the extension we're looking for here. |
Devlin Cronin | ef3e37e | 2018-05-14 23:47:24 | [diff] [blame] | 315 | if (extension->location() == Manifest::COMPONENT) |
[email protected] | 1952c7d | 2010-03-04 23:48:34 | [diff] [blame] | 316 | continue; |
| 317 | |
reillyg | a3acbc1 | 2014-11-11 23:17:12 | [diff] [blame] | 318 | if (result != NULL) { |
[email protected] | 84df833 | 2011-12-06 18:22:46 | [diff] [blame] | 319 | // TODO(yoz): this is misleading; it counts component extensions. |
[email protected] | 18d4b6c | 2010-09-21 03:21:04 | [diff] [blame] | 320 | message_ = base::StringPrintf( |
[email protected] | 1952c7d | 2010-03-04 23:48:34 | [diff] [blame] | 321 | "Expected only one extension to be present. Found %u.", |
reillyg | a3acbc1 | 2014-11-11 23:17:12 | [diff] [blame] | 322 | static_cast<unsigned>(registry->enabled_extensions().size())); |
[email protected] | 1952c7d | 2010-03-04 23:48:34 | [diff] [blame] | 323 | return NULL; |
| 324 | } |
| 325 | |
reillyg | a3acbc1 | 2014-11-11 23:17:12 | [diff] [blame] | 326 | result = extension.get(); |
[email protected] | 5f9c249 | 2010-02-02 21:37:45 | [diff] [blame] | 327 | } |
[email protected] | 1952c7d | 2010-03-04 23:48:34 | [diff] [blame] | 328 | |
reillyg | a3acbc1 | 2014-11-11 23:17:12 | [diff] [blame] | 329 | if (!result) { |
[email protected] | 5f9c249 | 2010-02-02 21:37:45 | [diff] [blame] | 330 | message_ = "extension pointer is NULL."; |
| 331 | return NULL; |
| 332 | } |
reillyg | a3acbc1 | 2014-11-11 23:17:12 | [diff] [blame] | 333 | return result; |
[email protected] | 5f9c249 | 2010-02-02 21:37:45 | [diff] [blame] | 334 | } |
| 335 | |
[email protected] | c1dffe8 | 2013-06-26 20:59:05 | [diff] [blame] | 336 | bool ExtensionApiTest::StartEmbeddedTestServer() { |
martijn | f9b885b7 | 2016-11-22 22:03:47 | [diff] [blame] | 337 | if (!InitializeEmbeddedTestServer()) |
| 338 | return false; |
| 339 | |
| 340 | EmbeddedTestServerAcceptConnections(); |
| 341 | return true; |
| 342 | } |
| 343 | |
| 344 | bool ExtensionApiTest::InitializeEmbeddedTestServer() { |
| 345 | if (!embedded_test_server()->InitializeAndListen()) |
[email protected] | 761e716 | 2010-10-18 19:26:39 | [diff] [blame] | 346 | return false; |
| 347 | |
| 348 | // Build a dictionary of values that tests can use to build URLs that |
[email protected] | 8f06336 | 2011-11-18 00:32:53 | [diff] [blame] | 349 | // access the test server and local file system. Tests can see these values |
| 350 | // using the extension API function chrome.test.getConfig(). |
Rob Wu | 2649de1 | 2018-01-09 22:54:47 | [diff] [blame] | 351 | if (test_config_) { |
| 352 | test_config_->SetInteger(kEmbeddedTestServerPort, |
| 353 | embedded_test_server()->port()); |
| 354 | } |
| 355 | // else SetUpOnMainThread has not been called yet. Possibly because the |
| 356 | // caller needs a valid port in an overridden SetUpCommandLine method. |
[email protected] | 761e716 | 2010-10-18 19:26:39 | [diff] [blame] | 357 | |
| 358 | return true; |
| 359 | } |
| 360 | |
martijn | f9b885b7 | 2016-11-22 22:03:47 | [diff] [blame] | 361 | void ExtensionApiTest::EmbeddedTestServerAcceptConnections() { |
| 362 | embedded_test_server()->StartAcceptingConnections(); |
| 363 | } |
| 364 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 365 | bool ExtensionApiTest::StartWebSocketServer( |
pkalinnikov | 0f198df | 2017-02-22 11:11:24 | [diff] [blame] | 366 | const base::FilePath& root_directory, |
| 367 | bool enable_basic_auth) { |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 368 | websocket_server_.reset(new net::SpawnedTestServer( |
Sergey Ulanov | 9e8d6f3 | 2017-08-14 22:12:58 | [diff] [blame] | 369 | net::SpawnedTestServer::TYPE_WS, root_directory)); |
pkalinnikov | 0f198df | 2017-02-22 11:11:24 | [diff] [blame] | 370 | websocket_server_->set_websocket_basic_auth(enable_basic_auth); |
[email protected] | 098702478 | 2012-10-13 20:52:19 | [diff] [blame] | 371 | |
| 372 | if (!websocket_server_->Start()) |
[email protected] | 8913c61 | 2012-03-20 01:04:15 | [diff] [blame] | 373 | return false; |
| 374 | |
[email protected] | 098702478 | 2012-10-13 20:52:19 | [diff] [blame] | 375 | test_config_->SetInteger(kTestWebSocketPort, |
| 376 | websocket_server_->host_port_pair().port()); |
| 377 | |
[email protected] | 8913c61 | 2012-03-20 01:04:15 | [diff] [blame] | 378 | return true; |
| 379 | } |
| 380 | |
[email protected] | a0e7579 | 2014-03-13 13:12:17 | [diff] [blame] | 381 | bool ExtensionApiTest::StartFTPServer(const base::FilePath& root_directory) { |
Sergey Ulanov | 9e8d6f3 | 2017-08-14 22:12:58 | [diff] [blame] | 382 | ftp_server_.reset(new net::SpawnedTestServer(net::SpawnedTestServer::TYPE_FTP, |
| 383 | root_directory)); |
[email protected] | a0e7579 | 2014-03-13 13:12:17 | [diff] [blame] | 384 | |
| 385 | if (!ftp_server_->Start()) |
| 386 | return false; |
| 387 | |
| 388 | test_config_->SetInteger(kFtpServerPort, |
| 389 | ftp_server_->host_port_pair().port()); |
| 390 | |
| 391 | return true; |
| 392 | } |
| 393 | |
Pavol Marko | cad963db | 2018-02-15 11:02:49 | [diff] [blame] | 394 | void ExtensionApiTest::SetCustomArg(base::StringPiece custom_arg) { |
| 395 | test_config_->SetKey(kTestCustomArg, base::Value(custom_arg)); |
| 396 | } |
| 397 | |
avi | 3ef9ec9e | 2014-12-22 22:50:17 | [diff] [blame] | 398 | void ExtensionApiTest::SetUpCommandLine(base::CommandLine* command_line) { |
[email protected] | 80d6a442 | 2009-10-15 06:59:22 | [diff] [blame] | 399 | ExtensionBrowserTest::SetUpCommandLine(command_line); |
Michael Giuffrida | 352663b | 2017-08-04 01:48:51 | [diff] [blame] | 400 | |
[email protected] | 80d6a442 | 2009-10-15 06:59:22 | [diff] [blame] | 401 | test_data_dir_ = test_data_dir_.AppendASCII("api_test"); |
Michael Giuffrida | 352663b | 2017-08-04 01:48:51 | [diff] [blame] | 402 | |
Devlin Cronin | ef3e37e | 2018-05-14 23:47:24 | [diff] [blame] | 403 | RegisterPathProvider(); |
| 404 | base::PathService::Get(DIR_TEST_DATA, &shared_test_data_dir_); |
Michael Giuffrida | 352663b | 2017-08-04 01:48:51 | [diff] [blame] | 405 | shared_test_data_dir_ = shared_test_data_dir_.AppendASCII("api_test"); |
| 406 | |
shrike | 8fbe9d3 | 2015-06-02 19:53:57 | [diff] [blame] | 407 | // Backgrounded renderer processes run at a lower priority, causing the |
| 408 | // tests to take more time to complete. Disable backgrounding so that the |
| 409 | // tests don't time out. |
Devlin Cronin | ef3e37e | 2018-05-14 23:47:24 | [diff] [blame] | 410 | command_line->AppendSwitch(::switches::kDisableRendererBackgrounding); |
[email protected] | 80d6a442 | 2009-10-15 06:59:22 | [diff] [blame] | 411 | } |
Devlin Cronin | ef3e37e | 2018-05-14 23:47:24 | [diff] [blame] | 412 | |
| 413 | } // namespace extensions |