| 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_apitest.h" | 5 #include "chrome/browser/extensions/extension_apitest.h" |
| 6 | 6 |
| 7 #include "base/string_util.h" | 7 #include "base/string_util.h" |
| 8 #include "base/stringprintf.h" | 8 #include "base/stringprintf.h" |
| 9 #include "chrome/browser/extensions/extension_service.h" | 9 #include "chrome/browser/extensions/extension_service.h" |
| 10 #include "chrome/browser/extensions/extension_test_api.h" | 10 #include "chrome/browser/extensions/extension_test_api.h" |
| 11 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
| 12 #include "chrome/browser/ui/browser.h" | 12 #include "chrome/browser/ui/browser.h" |
| 13 #include "chrome/browser/web_applications/web_app.h" |
| 13 #include "chrome/common/chrome_notification_types.h" | 14 #include "chrome/common/chrome_notification_types.h" |
| 14 #include "chrome/test/base/ui_test_utils.h" | 15 #include "chrome/test/base/ui_test_utils.h" |
| 15 #include "content/public/browser/notification_registrar.h" | 16 #include "content/public/browser/notification_registrar.h" |
| 16 #include "content/public/browser/notification_service.h" | 17 #include "content/public/browser/notification_service.h" |
| 17 | 18 |
| 18 namespace { | 19 namespace { |
| 19 | 20 |
| 20 const char kTestServerPort[] = "testServer.port"; | 21 const char kTestServerPort[] = "testServer.port"; |
| 21 const char kTestDataDirectory[] = "testDataDirectory"; | 22 const char kTestDataDirectory[] = "testDataDirectory"; |
| 22 | 23 |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 98 net::FilePathToFileURL(test_data_dir_).spec()); | 99 net::FilePathToFileURL(test_data_dir_).spec()); |
| 99 ExtensionTestGetConfigFunction::set_test_config_state(test_config_.get()); | 100 ExtensionTestGetConfigFunction::set_test_config_state(test_config_.get()); |
| 100 } | 101 } |
| 101 | 102 |
| 102 void ExtensionApiTest::TearDownInProcessBrowserTestFixture() { | 103 void ExtensionApiTest::TearDownInProcessBrowserTestFixture() { |
| 103 ExtensionTestGetConfigFunction::set_test_config_state(NULL); | 104 ExtensionTestGetConfigFunction::set_test_config_state(NULL); |
| 104 test_config_.reset(NULL); | 105 test_config_.reset(NULL); |
| 105 } | 106 } |
| 106 | 107 |
| 107 bool ExtensionApiTest::RunExtensionTest(const char* extension_name) { | 108 bool ExtensionApiTest::RunExtensionTest(const char* extension_name) { |
| 108 return RunExtensionTestImpl(extension_name, "", false, true, false); | 109 return RunExtensionTestImpl(extension_name, "", kFlagEnableFileAccess); |
| 109 } | 110 } |
| 110 | 111 |
| 111 bool ExtensionApiTest::RunExtensionTestIncognito(const char* extension_name) { | 112 bool ExtensionApiTest::RunExtensionTestIncognito(const char* extension_name) { |
| 112 return RunExtensionTestImpl(extension_name, "", true, true, false); | 113 return RunExtensionTestImpl( |
| 114 extension_name, "", kFlagEnableIncognito | kFlagEnableFileAccess); |
| 113 } | 115 } |
| 114 | 116 |
| 115 bool ExtensionApiTest::RunComponentExtensionTest(const char* extension_name) { | 117 bool ExtensionApiTest::RunComponentExtensionTest(const char* extension_name) { |
| 116 return RunExtensionTestImpl(extension_name, "", false, true, true); | 118 return RunExtensionTestImpl( |
| 119 extension_name, "", kFlagEnableFileAccess | kFlagLoadAsComponent); |
| 117 } | 120 } |
| 118 | 121 |
| 119 bool ExtensionApiTest::RunExtensionTestNoFileAccess( | 122 bool ExtensionApiTest::RunExtensionTestNoFileAccess( |
| 120 const char* extension_name) { | 123 const char* extension_name) { |
| 121 return RunExtensionTestImpl(extension_name, "", false, false, false); | 124 return RunExtensionTestImpl(extension_name, "", kFlagNone); |
| 122 } | 125 } |
| 123 | 126 |
| 124 bool ExtensionApiTest::RunExtensionTestIncognitoNoFileAccess( | 127 bool ExtensionApiTest::RunExtensionTestIncognitoNoFileAccess( |
| 125 const char* extension_name) { | 128 const char* extension_name) { |
| 126 return RunExtensionTestImpl(extension_name, "", true, false, false); | 129 return RunExtensionTestImpl(extension_name, "", kFlagEnableIncognito); |
| 127 } | 130 } |
| 128 | 131 |
| 129 bool ExtensionApiTest::RunExtensionSubtest(const char* extension_name, | 132 bool ExtensionApiTest::RunExtensionSubtest(const char* extension_name, |
| 130 const std::string& page_url) { | 133 const std::string& page_url) { |
| 131 DCHECK(!page_url.empty()) << "Argument page_url is required."; | 134 DCHECK(!page_url.empty()) << "Argument page_url is required."; |
| 132 return RunExtensionTestImpl(extension_name, page_url, false, true, false); | 135 return RunExtensionTestImpl(extension_name, page_url, kFlagEnableFileAccess); |
| 133 } | 136 } |
| 134 | 137 |
| 135 bool ExtensionApiTest::RunExtensionSubtestNoFileAccess( | 138 bool ExtensionApiTest::RunExtensionSubtestNoFileAccess( |
| 136 const char* extension_name, | 139 const char* extension_name, |
| 137 const std::string& page_url) { | 140 const std::string& page_url) { |
| 138 DCHECK(!page_url.empty()) << "Argument page_url is required."; | 141 DCHECK(!page_url.empty()) << "Argument page_url is required."; |
| 139 return RunExtensionTestImpl(extension_name, page_url, false, false, false); | 142 return RunExtensionTestImpl(extension_name, page_url, kFlagNone); |
| 140 } | 143 } |
| 141 | 144 |
| 142 bool ExtensionApiTest::RunExtensionSubtestIncognito( | 145 bool ExtensionApiTest::RunExtensionSubtestIncognito( |
| 143 const char* extension_name, | 146 const char* extension_name, |
| 144 const std::string& page_url) { | 147 const std::string& page_url) { |
| 145 DCHECK(!page_url.empty()) << "Argument page_url is required."; | 148 DCHECK(!page_url.empty()) << "Argument page_url is required."; |
| 146 return RunExtensionTestImpl(extension_name, page_url, true, true, false); | 149 return RunExtensionTestImpl(extension_name, page_url, |
| 150 kFlagEnableIncognito | kFlagEnableFileAccess); |
| 147 } | 151 } |
| 148 | 152 |
| 149 bool ExtensionApiTest::RunExtensionSubtestIncognitoNoFileAccess( | 153 bool ExtensionApiTest::RunExtensionSubtestIncognitoNoFileAccess( |
| 150 const char* extension_name, | 154 const char* extension_name, |
| 151 const std::string& page_url) { | 155 const std::string& page_url) { |
| 152 DCHECK(!page_url.empty()) << "Argument page_url is required."; | 156 DCHECK(!page_url.empty()) << "Argument page_url is required."; |
| 153 return RunExtensionTestImpl(extension_name, page_url, true, false, false); | 157 return RunExtensionTestImpl(extension_name, page_url, kFlagEnableIncognito); |
| 154 } | 158 } |
| 155 | 159 |
| 156 bool ExtensionApiTest::RunPageTest(const std::string& page_url) { | 160 bool ExtensionApiTest::RunPageTest(const std::string& page_url) { |
| 157 return RunExtensionSubtest("", page_url); | 161 return RunExtensionSubtest("", page_url); |
| 158 } | 162 } |
| 159 | 163 |
| 164 bool ExtensionApiTest::RunPlatformAppTest(const char* extension_name) { |
| 165 return RunExtensionTestImpl(extension_name, "", kFlagLaunchAppShell); |
| 166 } |
| 167 |
| 160 // Load |extension_name| extension and/or |page_url| and wait for | 168 // Load |extension_name| extension and/or |page_url| and wait for |
| 161 // PASSED or FAILED notification. | 169 // PASSED or FAILED notification. |
| 162 bool ExtensionApiTest::RunExtensionTestImpl(const char* extension_name, | 170 bool ExtensionApiTest::RunExtensionTestImpl(const char* extension_name, |
| 163 const std::string& page_url, | 171 const std::string& page_url, |
| 164 bool enable_incognito, | 172 int flags) { |
| 165 bool enable_fileaccess, | 173 bool enable_incognito = (flags & kFlagEnableIncognito) != 0; |
| 166 bool load_as_component) { | 174 bool enable_fileaccess = (flags & kFlagEnableFileAccess) != 0; |
| 175 bool load_as_component = (flags & kFlagLoadAsComponent) != 0; |
| 176 bool launch_shell = (flags & kFlagLaunchAppShell) != 0; |
| 177 |
| 167 ResultCatcher catcher; | 178 ResultCatcher catcher; |
| 168 DCHECK(!std::string(extension_name).empty() || !page_url.empty()) << | 179 DCHECK(!std::string(extension_name).empty() || !page_url.empty()) << |
| 169 "extension_name and page_url cannot both be empty"; | 180 "extension_name and page_url cannot both be empty"; |
| 170 | 181 |
| 182 const Extension* extension = NULL; |
| 171 if (!std::string(extension_name).empty()) { | 183 if (!std::string(extension_name).empty()) { |
| 172 bool loaded = false; | |
| 173 FilePath extension_path = test_data_dir_.AppendASCII(extension_name); | 184 FilePath extension_path = test_data_dir_.AppendASCII(extension_name); |
| 174 if (load_as_component) { | 185 if (load_as_component) { |
| 175 loaded = LoadExtensionAsComponent(extension_path); | 186 extension = LoadExtensionAsComponent(extension_path); |
| 176 } else { | 187 } else { |
| 177 loaded = LoadExtensionWithOptions(extension_path, | 188 extension = LoadExtensionWithOptions(extension_path, |
| 178 enable_incognito, enable_fileaccess); | 189 enable_incognito, enable_fileaccess); |
| 179 } | 190 } |
| 180 if (!loaded) { | 191 if (!extension) { |
| 181 message_ = "Failed to load extension."; | 192 message_ = "Failed to load extension."; |
| 182 return false; | 193 return false; |
| 183 } | 194 } |
| 184 } | 195 } |
| 185 | 196 |
| 186 // If there is a page_url to load, navigate it. | 197 // If there is a page_url to load, navigate it. |
| 187 if (!page_url.empty()) { | 198 if (!page_url.empty()) { |
| 188 GURL url = GURL(page_url); | 199 GURL url = GURL(page_url); |
| 189 | 200 |
| 190 // Note: We use is_valid() here in the expectation that the provided url | 201 // Note: We use is_valid() here in the expectation that the provided url |
| 191 // may lack a scheme & host and thus be a relative url within the loaded | 202 // may lack a scheme & host and thus be a relative url within the loaded |
| 192 // extension. | 203 // extension. |
| 193 if (!url.is_valid()) { | 204 if (!url.is_valid()) { |
| 194 DCHECK(!std::string(extension_name).empty()) << | 205 DCHECK(!std::string(extension_name).empty()) << |
| 195 "Relative page_url given with no extension_name"; | 206 "Relative page_url given with no extension_name"; |
| 196 | 207 |
| 197 ExtensionService* service = browser()->profile()->GetExtensionService(); | |
| 198 const Extension* extension = | |
| 199 service->GetExtensionById(last_loaded_extension_id_, false); | |
| 200 if (!extension) { | |
| 201 message_ = "Failed to find extension in ExtensionService."; | |
| 202 return false; | |
| 203 } | |
| 204 | |
| 205 url = extension->GetResourceURL(page_url); | 208 url = extension->GetResourceURL(page_url); |
| 206 } | 209 } |
| 207 | 210 |
| 208 ui_test_utils::NavigateToURL(browser(), url); | 211 ui_test_utils::NavigateToURL(browser(), url); |
| 212 |
| 213 } else if (launch_shell) { |
| 214 web_app::SetDisableShortcutCreationForTests(true); |
| 215 Browser::OpenApplication( |
| 216 browser()->profile(), |
| 217 extension, |
| 218 extension_misc::LAUNCH_SHELL, |
| 219 GURL(), |
| 220 NEW_WINDOW); |
| 209 } | 221 } |
| 210 | 222 |
| 211 if (!catcher.GetNextResult()) { | 223 if (!catcher.GetNextResult()) { |
| 212 message_ = catcher.message(); | 224 message_ = catcher.message(); |
| 213 return false; | 225 return false; |
| 214 } else { | 226 } else { |
| 215 return true; | 227 return true; |
| 216 } | 228 } |
| 217 } | 229 } |
| 218 | 230 |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 256 test_config_->SetInteger(kTestServerPort, | 268 test_config_->SetInteger(kTestServerPort, |
| 257 test_server()->host_port_pair().port()); | 269 test_server()->host_port_pair().port()); |
| 258 | 270 |
| 259 return true; | 271 return true; |
| 260 } | 272 } |
| 261 | 273 |
| 262 void ExtensionApiTest::SetUpCommandLine(CommandLine* command_line) { | 274 void ExtensionApiTest::SetUpCommandLine(CommandLine* command_line) { |
| 263 ExtensionBrowserTest::SetUpCommandLine(command_line); | 275 ExtensionBrowserTest::SetUpCommandLine(command_line); |
| 264 test_data_dir_ = test_data_dir_.AppendASCII("api_test"); | 276 test_data_dir_ = test_data_dir_.AppendASCII("api_test"); |
| 265 } | 277 } |
| OLD | NEW |