Chromium Code Reviews| 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_function_test_utils.h" | 5 #include "chrome/browser/extensions/extension_function_test_utils.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 10 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
| 11 #include "base/values.h" | 11 #include "base/values.h" |
| 12 #include "chrome/browser/extensions/api/tabs/tabs_constants.h" | 12 #include "chrome/browser/extensions/api/tabs/tabs_constants.h" |
| 13 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
| 14 #include "chrome/browser/ui/browser.h" | 14 #include "chrome/browser/ui/browser.h" |
| 15 #include "chrome/test/base/ui_test_utils.h" | 15 #include "chrome/test/base/ui_test_utils.h" |
| 16 #include "extensions/browser/api_test_utils.h" | |
| 16 #include "extensions/browser/extension_function.h" | 17 #include "extensions/browser/extension_function.h" |
| 17 #include "extensions/browser/extension_function_dispatcher.h" | 18 #include "extensions/browser/extension_function_dispatcher.h" |
| 18 #include "extensions/common/extension.h" | 19 #include "extensions/common/extension.h" |
| 19 #include "extensions/common/id_util.h" | 20 #include "extensions/common/id_util.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
| 21 | 22 |
| 22 using content::WebContents; | 23 using content::WebContents; |
| 23 using extensions::Extension; | 24 using extensions::Extension; |
| 24 using extensions::Manifest; | 25 using extensions::Manifest; |
| 25 namespace keys = extensions::tabs_constants; | 26 namespace keys = extensions::tabs_constants; |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 48 | 49 |
| 49 } // namespace | 50 } // namespace |
| 50 | 51 |
| 51 namespace extension_function_test_utils { | 52 namespace extension_function_test_utils { |
| 52 | 53 |
| 53 base::Value* ParseJSON(const std::string& data) { | 54 base::Value* ParseJSON(const std::string& data) { |
| 54 return base::JSONReader::Read(data); | 55 return base::JSONReader::Read(data); |
| 55 } | 56 } |
| 56 | 57 |
| 57 base::ListValue* ParseList(const std::string& data) { | 58 base::ListValue* ParseList(const std::string& data) { |
| 58 scoped_ptr<base::Value> result(ParseJSON(data)); | 59 base::Value* result(ParseJSON(data)); |
|
James Cook
2014/07/18 20:32:48
nit: I find "result = ParseJSON(data)" slightly ea
Yoyo Zhou
2014/07/22 00:10:31
Me too.
| |
| 59 if (result.get() && result->IsType(base::Value::TYPE_LIST)) | 60 base::ListValue* list = NULL; |
| 60 return static_cast<base::ListValue*>(result.release()); | 61 result->GetAsList(&list); |
| 61 else | 62 return list; |
| 62 return NULL; | |
| 63 } | 63 } |
| 64 | 64 |
| 65 base::DictionaryValue* ParseDictionary( | 65 base::DictionaryValue* ParseDictionary( |
| 66 const std::string& data) { | 66 const std::string& data) { |
| 67 scoped_ptr<base::Value> result(ParseJSON(data)); | 67 scoped_ptr<base::Value> result(ParseJSON(data)); |
| 68 if (result.get() && result->IsType(base::Value::TYPE_DICTIONARY)) | 68 if (result.get() && result->IsType(base::Value::TYPE_DICTIONARY)) |
| 69 return static_cast<base::DictionaryValue*>(result.release()); | 69 return static_cast<base::DictionaryValue*>(result.release()); |
| 70 else | 70 else |
| 71 return NULL; | 71 return NULL; |
| 72 } | 72 } |
| (...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 236 | 236 |
| 237 private: | 237 private: |
| 238 scoped_ptr<bool> response_; | 238 scoped_ptr<bool> response_; |
| 239 bool should_post_quit_; | 239 bool should_post_quit_; |
| 240 }; | 240 }; |
| 241 | 241 |
| 242 bool RunFunction(UIThreadExtensionFunction* function, | 242 bool RunFunction(UIThreadExtensionFunction* function, |
| 243 const std::string& args, | 243 const std::string& args, |
| 244 Browser* browser, | 244 Browser* browser, |
| 245 RunFunctionFlags flags) { | 245 RunFunctionFlags flags) { |
| 246 SendResponseDelegate response_delegate; | |
| 247 function->set_test_delegate(&response_delegate); | |
| 248 scoped_ptr<base::ListValue> parsed_args(ParseList(args)); | |
| 249 EXPECT_TRUE(parsed_args.get()) << | |
| 250 "Could not parse extension function arguments: " << args; | |
| 251 function->SetArgs(parsed_args.get()); | |
| 252 | |
| 253 TestFunctionDispatcherDelegate dispatcher_delegate(browser); | 246 TestFunctionDispatcherDelegate dispatcher_delegate(browser); |
| 254 extensions::ExtensionFunctionDispatcher dispatcher(browser->profile(), | 247 scoped_ptr<extensions::ExtensionFunctionDispatcher> dispatcher( |
| 255 &dispatcher_delegate); | 248 new extensions::ExtensionFunctionDispatcher(browser->profile(), |
| 256 function->set_dispatcher(dispatcher.AsWeakPtr()); | 249 &dispatcher_delegate)); |
| 257 | 250 // The cast is a hack; these flags should be defined in only one place. |
|
James Cook
2014/07/18 20:32:48
TODO(yoz) here
Yoyo Zhou
2014/07/22 00:10:31
added
| |
| 258 function->set_browser_context(browser->profile()); | 251 // See crbug.com/394840. |
| 259 function->set_include_incognito(flags & INCLUDE_INCOGNITO); | 252 return extensions::api_test_utils::RunFunction( |
| 260 function->Run()->Execute(); | 253 function, |
| 261 | 254 args, |
| 262 // If the RunAsync of |function| didn't already call SendResponse, run the | 255 browser->profile(), |
| 263 // message loop until they do. | 256 dispatcher.Pass(), |
| 264 if (!response_delegate.HasResponse()) { | 257 static_cast<extensions::api_test_utils::RunFunctionFlags>(flags)); |
| 265 response_delegate.set_should_post_quit(true); | |
| 266 content::RunMessageLoop(); | |
| 267 } | |
| 268 | |
| 269 EXPECT_TRUE(response_delegate.HasResponse()); | |
| 270 return response_delegate.GetResponse(); | |
| 271 } | 258 } |
| 272 | 259 |
| 273 } // namespace extension_function_test_utils | 260 } // namespace extension_function_test_utils |
| OLD | NEW |