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/strings/string_split.h" | 7 #include "base/strings/string_split.h" |
8 #include "base/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
9 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
10 #include "chrome/browser/extensions/extension_service.h" | 10 #include "chrome/browser/extensions/extension_service.h" |
11 #include "chrome/browser/extensions/unpacked_installer.h" | 11 #include "chrome/browser/extensions/unpacked_installer.h" |
12 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
13 #include "chrome/browser/ui/browser.h" | 13 #include "chrome/browser/ui/browser.h" |
14 #include "chrome/browser/ui/extensions/application_launch.h" | 14 #include "chrome/browser/ui/extensions/application_launch.h" |
15 #include "chrome/test/base/ui_test_utils.h" | 15 #include "chrome/test/base/ui_test_utils.h" |
16 #include "content/public/browser/notification_registrar.h" | |
17 #include "content/public/browser/notification_service.h" | |
18 #include "extensions/browser/api/test/test_api.h" | 16 #include "extensions/browser/api/test/test_api.h" |
19 #include "extensions/browser/extension_system.h" | 17 #include "extensions/browser/extension_system.h" |
20 #include "extensions/browser/notification_types.h" | |
21 #include "extensions/common/extension.h" | 18 #include "extensions/common/extension.h" |
22 #include "extensions/common/extension_set.h" | 19 #include "extensions/common/extension_set.h" |
| 20 #include "extensions/test/result_catcher.h" |
23 #include "net/base/escape.h" | 21 #include "net/base/escape.h" |
24 #include "net/base/filename_util.h" | 22 #include "net/base/filename_util.h" |
25 #include "net/test/embedded_test_server/embedded_test_server.h" | 23 #include "net/test/embedded_test_server/embedded_test_server.h" |
26 #include "net/test/embedded_test_server/http_request.h" | 24 #include "net/test/embedded_test_server/http_request.h" |
27 #include "net/test/embedded_test_server/http_response.h" | 25 #include "net/test/embedded_test_server/http_response.h" |
28 #include "net/test/spawned_test_server/spawned_test_server.h" | 26 #include "net/test/spawned_test_server/spawned_test_server.h" |
29 | 27 |
30 namespace { | 28 namespace { |
31 | 29 |
32 const char kTestCustomArg[] = "customArg"; | 30 const char kTestCustomArg[] = "customArg"; |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 embedded_test_server()->RegisterRequestHandler( | 132 embedded_test_server()->RegisterRequestHandler( |
135 base::Bind(&HandleEchoHeaderRequest)); | 133 base::Bind(&HandleEchoHeaderRequest)); |
136 embedded_test_server()->RegisterRequestHandler( | 134 embedded_test_server()->RegisterRequestHandler( |
137 base::Bind(&HandleSetCookieRequest)); | 135 base::Bind(&HandleSetCookieRequest)); |
138 embedded_test_server()->RegisterRequestHandler( | 136 embedded_test_server()->RegisterRequestHandler( |
139 base::Bind(&HandleSetHeaderRequest)); | 137 base::Bind(&HandleSetHeaderRequest)); |
140 } | 138 } |
141 | 139 |
142 ExtensionApiTest::~ExtensionApiTest() {} | 140 ExtensionApiTest::~ExtensionApiTest() {} |
143 | 141 |
144 ExtensionApiTest::ResultCatcher::ResultCatcher() | |
145 : profile_restriction_(NULL), | |
146 waiting_(false) { | |
147 registrar_.Add(this, | |
148 extensions::NOTIFICATION_EXTENSION_TEST_PASSED, | |
149 content::NotificationService::AllSources()); | |
150 registrar_.Add(this, | |
151 extensions::NOTIFICATION_EXTENSION_TEST_FAILED, | |
152 content::NotificationService::AllSources()); | |
153 } | |
154 | |
155 ExtensionApiTest::ResultCatcher::~ResultCatcher() { | |
156 } | |
157 | |
158 bool ExtensionApiTest::ResultCatcher::GetNextResult() { | |
159 // Depending on the tests, multiple results can come in from a single call | |
160 // to RunMessageLoop(), so we maintain a queue of results and just pull them | |
161 // off as the test calls this, going to the run loop only when the queue is | |
162 // empty. | |
163 if (results_.empty()) { | |
164 waiting_ = true; | |
165 content::RunMessageLoop(); | |
166 waiting_ = false; | |
167 } | |
168 | |
169 if (!results_.empty()) { | |
170 bool ret = results_.front(); | |
171 results_.pop_front(); | |
172 message_ = messages_.front(); | |
173 messages_.pop_front(); | |
174 return ret; | |
175 } | |
176 | |
177 NOTREACHED(); | |
178 return false; | |
179 } | |
180 | |
181 void ExtensionApiTest::ResultCatcher::Observe( | |
182 int type, const content::NotificationSource& source, | |
183 const content::NotificationDetails& details) { | |
184 if (profile_restriction_ && | |
185 content::Source<Profile>(source).ptr() != profile_restriction_) { | |
186 return; | |
187 } | |
188 | |
189 switch (type) { | |
190 case extensions::NOTIFICATION_EXTENSION_TEST_PASSED: | |
191 VLOG(1) << "Got EXTENSION_TEST_PASSED notification."; | |
192 results_.push_back(true); | |
193 messages_.push_back(std::string()); | |
194 if (waiting_) | |
195 base::MessageLoopForUI::current()->Quit(); | |
196 break; | |
197 | |
198 case extensions::NOTIFICATION_EXTENSION_TEST_FAILED: | |
199 VLOG(1) << "Got EXTENSION_TEST_FAILED notification."; | |
200 results_.push_back(false); | |
201 messages_.push_back(*(content::Details<std::string>(details).ptr())); | |
202 if (waiting_) | |
203 base::MessageLoopForUI::current()->Quit(); | |
204 break; | |
205 | |
206 default: | |
207 NOTREACHED(); | |
208 } | |
209 } | |
210 | |
211 void ExtensionApiTest::SetUpInProcessBrowserTestFixture() { | 142 void ExtensionApiTest::SetUpInProcessBrowserTestFixture() { |
212 DCHECK(!test_config_.get()) << "Previous test did not clear config state."; | 143 DCHECK(!test_config_.get()) << "Previous test did not clear config state."; |
213 test_config_.reset(new base::DictionaryValue()); | 144 test_config_.reset(new base::DictionaryValue()); |
214 test_config_->SetString(kTestDataDirectory, | 145 test_config_->SetString(kTestDataDirectory, |
215 net::FilePathToFileURL(test_data_dir_).spec()); | 146 net::FilePathToFileURL(test_data_dir_).spec()); |
216 test_config_->SetInteger(kTestWebSocketPort, 0); | 147 test_config_->SetInteger(kTestWebSocketPort, 0); |
217 extensions::TestGetConfigFunction::set_test_config_state( | 148 extensions::TestGetConfigFunction::set_test_config_state( |
218 test_config_.get()); | 149 test_config_.get()); |
219 } | 150 } |
220 | 151 |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
321 const std::string& page_url, | 252 const std::string& page_url, |
322 const char* custom_arg, | 253 const char* custom_arg, |
323 int flags) { | 254 int flags) { |
324 bool load_as_component = (flags & kFlagLoadAsComponent) != 0; | 255 bool load_as_component = (flags & kFlagLoadAsComponent) != 0; |
325 bool launch_platform_app = (flags & kFlagLaunchPlatformApp) != 0; | 256 bool launch_platform_app = (flags & kFlagLaunchPlatformApp) != 0; |
326 bool use_incognito = (flags & kFlagUseIncognito) != 0; | 257 bool use_incognito = (flags & kFlagUseIncognito) != 0; |
327 | 258 |
328 if (custom_arg && custom_arg[0]) | 259 if (custom_arg && custom_arg[0]) |
329 test_config_->SetString(kTestCustomArg, custom_arg); | 260 test_config_->SetString(kTestCustomArg, custom_arg); |
330 | 261 |
331 ResultCatcher catcher; | 262 extensions::ResultCatcher catcher; |
332 DCHECK(!extension_name.empty() || !page_url.empty()) << | 263 DCHECK(!extension_name.empty() || !page_url.empty()) << |
333 "extension_name and page_url cannot both be empty"; | 264 "extension_name and page_url cannot both be empty"; |
334 | 265 |
335 const extensions::Extension* extension = NULL; | 266 const extensions::Extension* extension = NULL; |
336 if (!extension_name.empty()) { | 267 if (!extension_name.empty()) { |
337 base::FilePath extension_path = test_data_dir_.AppendASCII(extension_name); | 268 base::FilePath extension_path = test_data_dir_.AppendASCII(extension_name); |
338 if (load_as_component) { | 269 if (load_as_component) { |
339 extension = LoadExtensionAsComponent(extension_path); | 270 extension = LoadExtensionAsComponent(extension_path); |
340 } else { | 271 } else { |
341 int browser_test_flags = ExtensionBrowserTest::kFlagNone; | 272 int browser_test_flags = ExtensionBrowserTest::kFlagNone; |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
478 test_config_->SetInteger(kSpawnedTestServerPort, | 409 test_config_->SetInteger(kSpawnedTestServerPort, |
479 test_server()->host_port_pair().port()); | 410 test_server()->host_port_pair().port()); |
480 | 411 |
481 return true; | 412 return true; |
482 } | 413 } |
483 | 414 |
484 void ExtensionApiTest::SetUpCommandLine(CommandLine* command_line) { | 415 void ExtensionApiTest::SetUpCommandLine(CommandLine* command_line) { |
485 ExtensionBrowserTest::SetUpCommandLine(command_line); | 416 ExtensionBrowserTest::SetUpCommandLine(command_line); |
486 test_data_dir_ = test_data_dir_.AppendASCII("api_test"); | 417 test_data_dir_ = test_data_dir_.AppendASCII("api_test"); |
487 } | 418 } |
OLD | NEW |