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
3 // found in the LICENSE file.
5 #include "chrome/browser/extensions/extension_function_test_utils.h"
9 #include "base/files/file_path.h"
10 #include "base/json/json_reader.h"
11 #include "base/values.h"
12 #include "chrome/browser/extensions/api/tabs/tabs_constants.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/ui/browser.h"
15 #include "components/crx_file/id_util.h"
16 #include "extensions/browser/api_test_utils.h"
17 #include "extensions/browser/extension_function.h"
18 #include "extensions/browser/extension_function_dispatcher.h"
19 #include "extensions/common/extension.h"
20 #include "testing/gtest/include/gtest/gtest.h"
22 using content::WebContents;
23 using extensions::Extension;
24 using extensions::Manifest;
25 namespace keys = extensions::tabs_constants;
29 class TestFunctionDispatcherDelegate
30 : public extensions::ExtensionFunctionDispatcher::Delegate {
32 explicit TestFunctionDispatcherDelegate(Browser* browser) :
34 ~TestFunctionDispatcherDelegate() override {}
37 extensions::WindowController* GetExtensionWindowController() const override {
38 return browser_->extension_window_controller();
41 WebContents* GetAssociatedWebContents() const override { return NULL; }
48 namespace extension_function_test_utils {
50 base::Value* ParseJSON(const std::string& data) {
51 return base::JSONReader::Read(data);
54 base::ListValue* ParseList(const std::string& data) {
55 base::Value* result = ParseJSON(data);
56 base::ListValue* list = NULL;
57 result->GetAsList(&list);
61 base::DictionaryValue* ParseDictionary(
62 const std::string& data) {
63 base::Value* result = ParseJSON(data);
64 base::DictionaryValue* dict = NULL;
65 result->GetAsDictionary(&dict);
69 bool GetBoolean(const base::DictionaryValue* val, const std::string& key) {
71 if (!val->GetBoolean(key, &result))
72 ADD_FAILURE() << key << " does not exist or is not a boolean.";
76 int GetInteger(const base::DictionaryValue* val, const std::string& key) {
78 if (!val->GetInteger(key, &result))
79 ADD_FAILURE() << key << " does not exist or is not an integer.";
83 std::string GetString(const base::DictionaryValue* val,
84 const std::string& key) {
86 if (!val->GetString(key, &result))
87 ADD_FAILURE() << key << " does not exist or is not a string.";
91 base::DictionaryValue* ToDictionary(base::Value* val) {
93 EXPECT_EQ(base::Value::TYPE_DICTIONARY, val->GetType());
94 return static_cast<base::DictionaryValue*>(val);
97 base::ListValue* ToList(base::Value* val) {
99 EXPECT_EQ(base::Value::TYPE_LIST, val->GetType());
100 return static_cast<base::ListValue*>(val);
103 scoped_refptr<Extension> CreateEmptyExtensionWithLocation(
104 Manifest::Location location) {
105 scoped_ptr<base::DictionaryValue> test_extension_value(
106 ParseDictionary("{\"name\": \"Test\", \"version\": \"1.0\"}"));
107 return CreateExtension(location, test_extension_value.get(), std::string());
110 scoped_refptr<Extension> CreateExtension(
111 base::DictionaryValue* test_extension_value) {
112 return CreateExtension(Manifest::INTERNAL, test_extension_value,
116 scoped_refptr<Extension> CreateExtension(
117 Manifest::Location location,
118 base::DictionaryValue* test_extension_value,
119 const std::string& id_input) {
121 const base::FilePath test_extension_path;
123 if (!id_input.empty())
124 id = crx_file::id_util::GenerateId(id_input);
125 scoped_refptr<Extension> extension(Extension::Create(
128 *test_extension_value,
132 EXPECT_TRUE(error.empty()) << "Could not parse test extension " << error;
136 bool HasPrivacySensitiveFields(base::DictionaryValue* val) {
138 if (val->GetString(keys::kUrlKey, &result) ||
139 val->GetString(keys::kTitleKey, &result) ||
140 val->GetString(keys::kFaviconUrlKey, &result))
145 std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function,
146 const std::string& args,
148 return RunFunctionAndReturnError(function, args, browser, NONE);
150 std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function,
151 const std::string& args,
153 RunFunctionFlags flags) {
154 scoped_refptr<ExtensionFunction> function_owner(function);
155 // Without a callback the function will not generate a result.
156 function->set_has_callback(true);
157 RunFunction(function, args, browser, flags);
158 EXPECT_FALSE(function->GetResultList()) << "Did not expect a result";
159 return function->GetError();
162 base::Value* RunFunctionAndReturnSingleResult(
163 UIThreadExtensionFunction* function,
164 const std::string& args,
166 return RunFunctionAndReturnSingleResult(function, args, browser, NONE);
168 base::Value* RunFunctionAndReturnSingleResult(
169 UIThreadExtensionFunction* function,
170 const std::string& args,
172 RunFunctionFlags flags) {
173 scoped_refptr<ExtensionFunction> function_owner(function);
174 // Without a callback the function will not generate a result.
175 function->set_has_callback(true);
176 RunFunction(function, args, browser, flags);
177 EXPECT_TRUE(function->GetError().empty()) << "Unexpected error: "
178 << function->GetError();
179 const base::Value* single_result = NULL;
180 if (function->GetResultList() != NULL &&
181 function->GetResultList()->Get(0, &single_result)) {
182 return single_result->DeepCopy();
187 // This helps us be able to wait until an UIThreadExtensionFunction calls
189 class SendResponseDelegate
190 : public UIThreadExtensionFunction::DelegateForTests {
192 SendResponseDelegate() : should_post_quit_(false) {}
194 virtual ~SendResponseDelegate() {}
196 void set_should_post_quit(bool should_quit) {
197 should_post_quit_ = should_quit;
201 return response_.get() != NULL;
205 EXPECT_TRUE(HasResponse());
206 return *response_.get();
209 void OnSendResponse(UIThreadExtensionFunction* function,
211 bool bad_message) override {
212 ASSERT_FALSE(bad_message);
213 ASSERT_FALSE(HasResponse());
214 response_.reset(new bool);
215 *response_ = success;
216 if (should_post_quit_) {
217 base::MessageLoopForUI::current()->Quit();
222 scoped_ptr<bool> response_;
223 bool should_post_quit_;
226 bool RunFunction(UIThreadExtensionFunction* function,
227 const std::string& args,
229 RunFunctionFlags flags) {
230 scoped_ptr<base::ListValue> parsed_args(ParseList(args));
231 EXPECT_TRUE(parsed_args.get())
232 << "Could not parse extension function arguments: " << args;
233 return RunFunction(function, parsed_args.Pass(), browser, flags);
236 bool RunFunction(UIThreadExtensionFunction* function,
237 scoped_ptr<base::ListValue> args,
239 RunFunctionFlags flags) {
240 TestFunctionDispatcherDelegate dispatcher_delegate(browser);
241 scoped_ptr<extensions::ExtensionFunctionDispatcher> dispatcher(
242 new extensions::ExtensionFunctionDispatcher(browser->profile(),
243 &dispatcher_delegate));
244 // TODO(yoz): The cast is a hack; these flags should be defined in
245 // only one place. See crbug.com/394840.
246 return extensions::api_test_utils::RunFunction(
251 static_cast<extensions::api_test_utils::RunFunctionFlags>(flags));
254 } // namespace extension_function_test_utils