Upstream parts of omnibox.
[chromium-blink-merge.git] / extensions / browser / api_test_utils.cc
blob8d215a5fc29d961dfabbea9790e13310844f1dee
1 // Copyright 2014 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 "extensions/browser/api_test_utils.h"
7 #include "base/json/json_reader.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/values.h"
10 #include "components/crx_file/id_util.h"
11 #include "content/public/browser/browser_context.h"
12 #include "content/public/test/test_utils.h"
13 #include "extensions/browser/extension_function.h"
14 #include "extensions/browser/extension_function_dispatcher.h"
15 #include "extensions/common/extension_builder.h"
16 #include "testing/gtest/include/gtest/gtest.h"
18 using extensions::ExtensionFunctionDispatcher;
20 namespace {
22 class TestFunctionDispatcherDelegate
23 : public ExtensionFunctionDispatcher::Delegate {
24 public:
25 TestFunctionDispatcherDelegate() {}
26 ~TestFunctionDispatcherDelegate() override {}
28 // NULL implementation.
29 private:
30 DISALLOW_COPY_AND_ASSIGN(TestFunctionDispatcherDelegate);
33 base::Value* ParseJSON(const std::string& data) {
34 return base::JSONReader::Read(data);
37 base::ListValue* ParseList(const std::string& data) {
38 base::Value* result = ParseJSON(data);
39 base::ListValue* list = NULL;
40 result->GetAsList(&list);
41 return list;
44 // This helps us be able to wait until an UIThreadExtensionFunction calls
45 // SendResponse.
46 class SendResponseDelegate
47 : public UIThreadExtensionFunction::DelegateForTests {
48 public:
49 SendResponseDelegate() : should_post_quit_(false) {}
51 virtual ~SendResponseDelegate() {}
53 void set_should_post_quit(bool should_quit) {
54 should_post_quit_ = should_quit;
57 bool HasResponse() { return response_.get() != NULL; }
59 bool GetResponse() {
60 EXPECT_TRUE(HasResponse());
61 return *response_.get();
64 void OnSendResponse(UIThreadExtensionFunction* function,
65 bool success,
66 bool bad_message) override {
67 ASSERT_FALSE(bad_message);
68 ASSERT_FALSE(HasResponse());
69 response_.reset(new bool);
70 *response_ = success;
71 if (should_post_quit_) {
72 base::MessageLoopForUI::current()->Quit();
76 private:
77 scoped_ptr<bool> response_;
78 bool should_post_quit_;
81 } // namespace
83 namespace extensions {
85 namespace api_test_utils {
87 base::DictionaryValue* ParseDictionary(const std::string& data) {
88 base::Value* result = ParseJSON(data);
89 base::DictionaryValue* dict = NULL;
90 result->GetAsDictionary(&dict);
91 return dict;
94 bool GetBoolean(const base::DictionaryValue* val, const std::string& key) {
95 bool result = false;
96 if (!val->GetBoolean(key, &result))
97 ADD_FAILURE() << key << " does not exist or is not a boolean.";
98 return result;
101 int GetInteger(const base::DictionaryValue* val, const std::string& key) {
102 int result = 0;
103 if (!val->GetInteger(key, &result))
104 ADD_FAILURE() << key << " does not exist or is not an integer.";
105 return result;
108 std::string GetString(const base::DictionaryValue* val,
109 const std::string& key) {
110 std::string result;
111 if (!val->GetString(key, &result))
112 ADD_FAILURE() << key << " does not exist or is not a string.";
113 return result;
116 scoped_refptr<Extension> CreateExtension(
117 Manifest::Location location,
118 base::DictionaryValue* test_extension_value,
119 const std::string& id_input) {
120 std::string error;
121 const base::FilePath test_extension_path;
122 std::string id;
123 if (!id_input.empty())
124 id = crx_file::id_util::GenerateId(id_input);
125 scoped_refptr<Extension> extension(
126 Extension::Create(test_extension_path, location, *test_extension_value,
127 Extension::NO_FLAGS, id, &error));
128 EXPECT_TRUE(error.empty()) << "Could not parse test extension " << error;
129 return extension;
132 scoped_refptr<Extension> CreateExtension(
133 base::DictionaryValue* test_extension_value) {
134 return CreateExtension(Manifest::INTERNAL, test_extension_value,
135 std::string());
138 base::Value* RunFunctionWithDelegateAndReturnSingleResult(
139 UIThreadExtensionFunction* function,
140 const std::string& args,
141 content::BrowserContext* context,
142 scoped_ptr<extensions::ExtensionFunctionDispatcher> dispatcher) {
143 return RunFunctionWithDelegateAndReturnSingleResult(
144 function, args, context, dispatcher.Pass(), NONE);
147 base::Value* RunFunctionWithDelegateAndReturnSingleResult(
148 UIThreadExtensionFunction* function,
149 const std::string& args,
150 content::BrowserContext* context,
151 scoped_ptr<extensions::ExtensionFunctionDispatcher> dispatcher,
152 RunFunctionFlags flags) {
153 scoped_refptr<ExtensionFunction> function_owner(function);
154 // Without a callback the function will not generate a result.
155 function->set_has_callback(true);
156 RunFunction(function, args, context, dispatcher.Pass(), flags);
157 EXPECT_TRUE(function->GetError().empty())
158 << "Unexpected error: " << function->GetError();
159 const base::Value* single_result = NULL;
160 if (function->GetResultList() != NULL &&
161 function->GetResultList()->Get(0, &single_result)) {
162 return single_result->DeepCopy();
164 return NULL;
167 base::Value* RunFunctionAndReturnSingleResult(
168 UIThreadExtensionFunction* function,
169 const std::string& args,
170 content::BrowserContext* context) {
171 return RunFunctionAndReturnSingleResult(function, args, context, NONE);
174 base::Value* RunFunctionAndReturnSingleResult(
175 UIThreadExtensionFunction* function,
176 const std::string& args,
177 content::BrowserContext* context,
178 RunFunctionFlags flags) {
179 TestFunctionDispatcherDelegate delegate;
180 scoped_ptr<ExtensionFunctionDispatcher> dispatcher(
181 new ExtensionFunctionDispatcher(context, &delegate));
183 return RunFunctionWithDelegateAndReturnSingleResult(
184 function, args, context, dispatcher.Pass(), flags);
187 std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function,
188 const std::string& args,
189 content::BrowserContext* context) {
190 return RunFunctionAndReturnError(function, args, context, NONE);
193 std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function,
194 const std::string& args,
195 content::BrowserContext* context,
196 RunFunctionFlags flags) {
197 TestFunctionDispatcherDelegate delegate;
198 scoped_ptr<ExtensionFunctionDispatcher> dispatcher(
199 new ExtensionFunctionDispatcher(context, &delegate));
200 scoped_refptr<ExtensionFunction> function_owner(function);
201 // Without a callback the function will not generate a result.
202 function->set_has_callback(true);
203 RunFunction(function, args, context, dispatcher.Pass(), flags);
204 EXPECT_FALSE(function->GetResultList()) << "Did not expect a result";
205 return function->GetError();
208 bool RunFunction(UIThreadExtensionFunction* function,
209 const std::string& args,
210 content::BrowserContext* context) {
211 TestFunctionDispatcherDelegate delegate;
212 scoped_ptr<ExtensionFunctionDispatcher> dispatcher(
213 new ExtensionFunctionDispatcher(context, &delegate));
214 return RunFunction(function, args, context, dispatcher.Pass(), NONE);
217 bool RunFunction(UIThreadExtensionFunction* function,
218 const std::string& args,
219 content::BrowserContext* context,
220 scoped_ptr<extensions::ExtensionFunctionDispatcher> dispatcher,
221 RunFunctionFlags flags) {
222 scoped_ptr<base::ListValue> parsed_args(ParseList(args));
223 EXPECT_TRUE(parsed_args.get())
224 << "Could not parse extension function arguments: " << args;
225 return RunFunction(
226 function, parsed_args.Pass(), context, dispatcher.Pass(), flags);
229 bool RunFunction(UIThreadExtensionFunction* function,
230 scoped_ptr<base::ListValue> args,
231 content::BrowserContext* context,
232 scoped_ptr<extensions::ExtensionFunctionDispatcher> dispatcher,
233 RunFunctionFlags flags) {
234 SendResponseDelegate response_delegate;
235 function->set_test_delegate(&response_delegate);
236 function->SetArgs(args.get());
238 CHECK(dispatcher);
239 function->set_dispatcher(dispatcher->AsWeakPtr());
241 function->set_browser_context(context);
242 function->set_include_incognito(flags & INCLUDE_INCOGNITO);
243 function->Run()->Execute();
245 // If the RunAsync of |function| didn't already call SendResponse, run the
246 // message loop until they do.
247 if (!response_delegate.HasResponse()) {
248 response_delegate.set_should_post_quit(true);
249 content::RunMessageLoop();
252 EXPECT_TRUE(response_delegate.HasResponse());
253 return response_delegate.GetResponse();
256 } // namespace api_test_utils
257 } // namespace extensions