MacViews: Get c/b/ui/views/tabs to build on Mac
[chromium-blink-merge.git] / chrome / browser / extensions / extension_function_test_utils.cc
blobb05098e7d281ac5976696790f0c11637b901478b
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"
7 #include <string>
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 "chrome/test/base/ui_test_utils.h"
16 #include "components/crx_file/id_util.h"
17 #include "extensions/browser/api_test_utils.h"
18 #include "extensions/browser/extension_function.h"
19 #include "extensions/browser/extension_function_dispatcher.h"
20 #include "extensions/common/extension.h"
21 #include "testing/gtest/include/gtest/gtest.h"
23 using content::WebContents;
24 using extensions::Extension;
25 using extensions::Manifest;
26 namespace keys = extensions::tabs_constants;
28 namespace {
30 class TestFunctionDispatcherDelegate
31 : public extensions::ExtensionFunctionDispatcher::Delegate {
32 public:
33 explicit TestFunctionDispatcherDelegate(Browser* browser) :
34 browser_(browser) {}
35 ~TestFunctionDispatcherDelegate() override {}
37 private:
38 extensions::WindowController* GetExtensionWindowController() const override {
39 return browser_->extension_window_controller();
42 WebContents* GetAssociatedWebContents() const override { return NULL; }
44 Browser* browser_;
47 } // namespace
49 namespace extension_function_test_utils {
51 base::Value* ParseJSON(const std::string& data) {
52 return base::JSONReader::Read(data);
55 base::ListValue* ParseList(const std::string& data) {
56 base::Value* result = ParseJSON(data);
57 base::ListValue* list = NULL;
58 result->GetAsList(&list);
59 return list;
62 base::DictionaryValue* ParseDictionary(
63 const std::string& data) {
64 base::Value* result = ParseJSON(data);
65 base::DictionaryValue* dict = NULL;
66 result->GetAsDictionary(&dict);
67 return dict;
70 bool GetBoolean(const base::DictionaryValue* val, const std::string& key) {
71 bool result = false;
72 if (!val->GetBoolean(key, &result))
73 ADD_FAILURE() << key << " does not exist or is not a boolean.";
74 return result;
77 int GetInteger(const base::DictionaryValue* val, const std::string& key) {
78 int result = 0;
79 if (!val->GetInteger(key, &result))
80 ADD_FAILURE() << key << " does not exist or is not an integer.";
81 return result;
84 std::string GetString(const base::DictionaryValue* val,
85 const std::string& key) {
86 std::string result;
87 if (!val->GetString(key, &result))
88 ADD_FAILURE() << key << " does not exist or is not a string.";
89 return result;
92 base::DictionaryValue* ToDictionary(base::Value* val) {
93 EXPECT_TRUE(val);
94 EXPECT_EQ(base::Value::TYPE_DICTIONARY, val->GetType());
95 return static_cast<base::DictionaryValue*>(val);
98 base::ListValue* ToList(base::Value* val) {
99 EXPECT_TRUE(val);
100 EXPECT_EQ(base::Value::TYPE_LIST, val->GetType());
101 return static_cast<base::ListValue*>(val);
104 scoped_refptr<Extension> CreateEmptyExtensionWithLocation(
105 Manifest::Location location) {
106 scoped_ptr<base::DictionaryValue> test_extension_value(
107 ParseDictionary("{\"name\": \"Test\", \"version\": \"1.0\"}"));
108 return CreateExtension(location, test_extension_value.get(), std::string());
111 scoped_refptr<Extension> CreateExtension(
112 base::DictionaryValue* test_extension_value) {
113 return CreateExtension(Manifest::INTERNAL, test_extension_value,
114 std::string());
117 scoped_refptr<Extension> CreateExtension(
118 Manifest::Location location,
119 base::DictionaryValue* test_extension_value,
120 const std::string& id_input) {
121 std::string error;
122 const base::FilePath test_extension_path;
123 std::string id;
124 if (!id_input.empty())
125 id = crx_file::id_util::GenerateId(id_input);
126 scoped_refptr<Extension> extension(Extension::Create(
127 test_extension_path,
128 location,
129 *test_extension_value,
130 Extension::NO_FLAGS,
132 &error));
133 EXPECT_TRUE(error.empty()) << "Could not parse test extension " << error;
134 return extension;
137 bool HasPrivacySensitiveFields(base::DictionaryValue* val) {
138 std::string result;
139 if (val->GetString(keys::kUrlKey, &result) ||
140 val->GetString(keys::kTitleKey, &result) ||
141 val->GetString(keys::kFaviconUrlKey, &result))
142 return true;
143 return false;
146 std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function,
147 const std::string& args,
148 Browser* browser) {
149 return RunFunctionAndReturnError(function, args, browser, NONE);
151 std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function,
152 const std::string& args,
153 Browser* browser,
154 RunFunctionFlags flags) {
155 scoped_refptr<ExtensionFunction> function_owner(function);
156 // Without a callback the function will not generate a result.
157 function->set_has_callback(true);
158 RunFunction(function, args, browser, flags);
159 EXPECT_FALSE(function->GetResultList()) << "Did not expect a result";
160 return function->GetError();
163 base::Value* RunFunctionAndReturnSingleResult(
164 UIThreadExtensionFunction* function,
165 const std::string& args,
166 Browser* browser) {
167 return RunFunctionAndReturnSingleResult(function, args, browser, NONE);
169 base::Value* RunFunctionAndReturnSingleResult(
170 UIThreadExtensionFunction* function,
171 const std::string& args,
172 Browser* browser,
173 RunFunctionFlags flags) {
174 scoped_refptr<ExtensionFunction> function_owner(function);
175 // Without a callback the function will not generate a result.
176 function->set_has_callback(true);
177 RunFunction(function, args, browser, flags);
178 EXPECT_TRUE(function->GetError().empty()) << "Unexpected error: "
179 << function->GetError();
180 const base::Value* single_result = NULL;
181 if (function->GetResultList() != NULL &&
182 function->GetResultList()->Get(0, &single_result)) {
183 return single_result->DeepCopy();
185 return NULL;
188 // This helps us be able to wait until an UIThreadExtensionFunction calls
189 // SendResponse.
190 class SendResponseDelegate
191 : public UIThreadExtensionFunction::DelegateForTests {
192 public:
193 SendResponseDelegate() : should_post_quit_(false) {}
195 virtual ~SendResponseDelegate() {}
197 void set_should_post_quit(bool should_quit) {
198 should_post_quit_ = should_quit;
201 bool HasResponse() {
202 return response_.get() != NULL;
205 bool GetResponse() {
206 EXPECT_TRUE(HasResponse());
207 return *response_.get();
210 void OnSendResponse(UIThreadExtensionFunction* function,
211 bool success,
212 bool bad_message) override {
213 ASSERT_FALSE(bad_message);
214 ASSERT_FALSE(HasResponse());
215 response_.reset(new bool);
216 *response_ = success;
217 if (should_post_quit_) {
218 base::MessageLoopForUI::current()->Quit();
222 private:
223 scoped_ptr<bool> response_;
224 bool should_post_quit_;
227 bool RunFunction(UIThreadExtensionFunction* function,
228 const std::string& args,
229 Browser* browser,
230 RunFunctionFlags flags) {
231 scoped_ptr<base::ListValue> parsed_args(ParseList(args));
232 EXPECT_TRUE(parsed_args.get())
233 << "Could not parse extension function arguments: " << args;
234 return RunFunction(function, parsed_args.Pass(), browser, flags);
237 bool RunFunction(UIThreadExtensionFunction* function,
238 scoped_ptr<base::ListValue> args,
239 Browser* browser,
240 RunFunctionFlags flags) {
241 TestFunctionDispatcherDelegate dispatcher_delegate(browser);
242 scoped_ptr<extensions::ExtensionFunctionDispatcher> dispatcher(
243 new extensions::ExtensionFunctionDispatcher(browser->profile(),
244 &dispatcher_delegate));
245 // TODO(yoz): The cast is a hack; these flags should be defined in
246 // only one place. See crbug.com/394840.
247 return extensions::api_test_utils::RunFunction(
248 function,
249 args.Pass(),
250 browser->profile(),
251 dispatcher.Pass(),
252 static_cast<extensions::api_test_utils::RunFunctionFlags>(flags));
255 } // namespace extension_function_test_utils