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); |
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 base::Value* result = ParseJSON(data); |
68 if (result.get() && result->IsType(base::Value::TYPE_DICTIONARY)) | 68 base::DictionaryValue* dict = NULL; |
69 return static_cast<base::DictionaryValue*>(result.release()); | 69 result->GetAsDictionary(&dict); |
70 else | 70 return dict; |
71 return NULL; | |
72 } | 71 } |
73 | 72 |
74 bool GetBoolean(base::DictionaryValue* val, const std::string& key) { | 73 bool GetBoolean(base::DictionaryValue* val, const std::string& key) { |
75 bool result = false; | 74 bool result = false; |
76 if (!val->GetBoolean(key, &result)) | 75 if (!val->GetBoolean(key, &result)) |
77 ADD_FAILURE() << key << " does not exist or is not a boolean."; | 76 ADD_FAILURE() << key << " does not exist or is not a boolean."; |
78 return result; | 77 return result; |
79 } | 78 } |
80 | 79 |
81 int GetInteger(base::DictionaryValue* val, const std::string& key) { | 80 int GetInteger(base::DictionaryValue* val, const std::string& key) { |
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
236 | 235 |
237 private: | 236 private: |
238 scoped_ptr<bool> response_; | 237 scoped_ptr<bool> response_; |
239 bool should_post_quit_; | 238 bool should_post_quit_; |
240 }; | 239 }; |
241 | 240 |
242 bool RunFunction(UIThreadExtensionFunction* function, | 241 bool RunFunction(UIThreadExtensionFunction* function, |
243 const std::string& args, | 242 const std::string& args, |
244 Browser* browser, | 243 Browser* browser, |
245 RunFunctionFlags flags) { | 244 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); | 245 TestFunctionDispatcherDelegate dispatcher_delegate(browser); |
254 extensions::ExtensionFunctionDispatcher dispatcher(browser->profile(), | 246 scoped_ptr<extensions::ExtensionFunctionDispatcher> dispatcher( |
255 &dispatcher_delegate); | 247 new extensions::ExtensionFunctionDispatcher(browser->profile(), |
256 function->set_dispatcher(dispatcher.AsWeakPtr()); | 248 &dispatcher_delegate)); |
257 | 249 // TODO(yoz): The cast is a hack; these flags should be defined in |
258 function->set_browser_context(browser->profile()); | 250 // only one place. See crbug.com/394840. |
259 function->set_include_incognito(flags & INCLUDE_INCOGNITO); | 251 return extensions::api_test_utils::RunFunction( |
260 function->Run()->Execute(); | 252 function, |
261 | 253 args, |
262 // If the RunAsync of |function| didn't already call SendResponse, run the | 254 browser->profile(), |
263 // message loop until they do. | 255 dispatcher.Pass(), |
264 if (!response_delegate.HasResponse()) { | 256 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 } | 257 } |
272 | 258 |
273 } // namespace extension_function_test_utils | 259 } // namespace extension_function_test_utils |
OLD | NEW |