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
;
22 class TestFunctionDispatcherDelegate
23 : public ExtensionFunctionDispatcher::Delegate
{
25 TestFunctionDispatcherDelegate() {}
26 ~TestFunctionDispatcherDelegate() override
{}
28 // NULL implementation.
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
);
44 // This helps us be able to wait until an UIThreadExtensionFunction calls
46 class SendResponseDelegate
47 : public UIThreadExtensionFunction::DelegateForTests
{
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
; }
60 EXPECT_TRUE(HasResponse());
61 return *response_
.get();
64 void OnSendResponse(UIThreadExtensionFunction
* function
,
66 bool bad_message
) override
{
67 ASSERT_FALSE(bad_message
);
68 ASSERT_FALSE(HasResponse());
69 response_
.reset(new bool);
71 if (should_post_quit_
) {
72 base::MessageLoopForUI::current()->Quit();
77 scoped_ptr
<bool> response_
;
78 bool should_post_quit_
;
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
);
94 bool GetBoolean(const base::DictionaryValue
* val
, const std::string
& key
) {
96 if (!val
->GetBoolean(key
, &result
))
97 ADD_FAILURE() << key
<< " does not exist or is not a boolean.";
101 int GetInteger(const base::DictionaryValue
* val
, const std::string
& key
) {
103 if (!val
->GetInteger(key
, &result
))
104 ADD_FAILURE() << key
<< " does not exist or is not an integer.";
108 std::string
GetString(const base::DictionaryValue
* val
,
109 const std::string
& key
) {
111 if (!val
->GetString(key
, &result
))
112 ADD_FAILURE() << key
<< " does not exist or is not a string.";
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(
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
;
132 scoped_refptr
<Extension
> CreateExtension(
133 base::DictionaryValue
* test_extension_value
) {
134 return CreateExtension(Manifest::INTERNAL
, test_extension_value
,
138 scoped_refptr
<Extension
> CreateEmptyExtensionWithLocation(
139 Manifest::Location location
) {
140 scoped_ptr
<base::DictionaryValue
> test_extension_value(
141 ParseDictionary("{\"name\": \"Test\", \"version\": \"1.0\"}"));
142 return CreateExtension(location
, test_extension_value
.get(), std::string());
145 base::Value
* RunFunctionWithDelegateAndReturnSingleResult(
146 UIThreadExtensionFunction
* function
,
147 const std::string
& args
,
148 content::BrowserContext
* context
,
149 scoped_ptr
<extensions::ExtensionFunctionDispatcher
> dispatcher
) {
150 return RunFunctionWithDelegateAndReturnSingleResult(
151 function
, args
, context
, dispatcher
.Pass(), NONE
);
154 base::Value
* RunFunctionWithDelegateAndReturnSingleResult(
155 UIThreadExtensionFunction
* function
,
156 const std::string
& args
,
157 content::BrowserContext
* context
,
158 scoped_ptr
<extensions::ExtensionFunctionDispatcher
> dispatcher
,
159 RunFunctionFlags flags
) {
160 scoped_refptr
<ExtensionFunction
> function_owner(function
);
161 // Without a callback the function will not generate a result.
162 function
->set_has_callback(true);
163 RunFunction(function
, args
, context
, dispatcher
.Pass(), flags
);
164 EXPECT_TRUE(function
->GetError().empty())
165 << "Unexpected error: " << function
->GetError();
166 const base::Value
* single_result
= NULL
;
167 if (function
->GetResultList() != NULL
&&
168 function
->GetResultList()->Get(0, &single_result
)) {
169 return single_result
->DeepCopy();
174 base::Value
* RunFunctionAndReturnSingleResult(
175 UIThreadExtensionFunction
* function
,
176 const std::string
& args
,
177 content::BrowserContext
* context
) {
178 return RunFunctionAndReturnSingleResult(function
, args
, context
, NONE
);
181 base::Value
* RunFunctionAndReturnSingleResult(
182 UIThreadExtensionFunction
* function
,
183 const std::string
& args
,
184 content::BrowserContext
* context
,
185 RunFunctionFlags flags
) {
186 TestFunctionDispatcherDelegate delegate
;
187 scoped_ptr
<ExtensionFunctionDispatcher
> dispatcher(
188 new ExtensionFunctionDispatcher(context
, &delegate
));
190 return RunFunctionWithDelegateAndReturnSingleResult(
191 function
, args
, context
, dispatcher
.Pass(), flags
);
194 std::string
RunFunctionAndReturnError(UIThreadExtensionFunction
* function
,
195 const std::string
& args
,
196 content::BrowserContext
* context
) {
197 return RunFunctionAndReturnError(function
, args
, context
, NONE
);
200 std::string
RunFunctionAndReturnError(UIThreadExtensionFunction
* function
,
201 const std::string
& args
,
202 content::BrowserContext
* context
,
203 RunFunctionFlags flags
) {
204 TestFunctionDispatcherDelegate delegate
;
205 scoped_ptr
<ExtensionFunctionDispatcher
> dispatcher(
206 new ExtensionFunctionDispatcher(context
, &delegate
));
207 scoped_refptr
<ExtensionFunction
> function_owner(function
);
208 // Without a callback the function will not generate a result.
209 function
->set_has_callback(true);
210 RunFunction(function
, args
, context
, dispatcher
.Pass(), flags
);
211 EXPECT_FALSE(function
->GetResultList()) << "Did not expect a result";
212 return function
->GetError();
215 bool RunFunction(UIThreadExtensionFunction
* function
,
216 const std::string
& args
,
217 content::BrowserContext
* context
) {
218 TestFunctionDispatcherDelegate delegate
;
219 scoped_ptr
<ExtensionFunctionDispatcher
> dispatcher(
220 new ExtensionFunctionDispatcher(context
, &delegate
));
221 return RunFunction(function
, args
, context
, dispatcher
.Pass(), NONE
);
224 bool RunFunction(UIThreadExtensionFunction
* function
,
225 const std::string
& args
,
226 content::BrowserContext
* context
,
227 scoped_ptr
<extensions::ExtensionFunctionDispatcher
> dispatcher
,
228 RunFunctionFlags flags
) {
229 scoped_ptr
<base::ListValue
> parsed_args(ParseList(args
));
230 EXPECT_TRUE(parsed_args
.get())
231 << "Could not parse extension function arguments: " << args
;
233 function
, parsed_args
.Pass(), context
, dispatcher
.Pass(), flags
);
236 bool RunFunction(UIThreadExtensionFunction
* function
,
237 scoped_ptr
<base::ListValue
> args
,
238 content::BrowserContext
* context
,
239 scoped_ptr
<extensions::ExtensionFunctionDispatcher
> dispatcher
,
240 RunFunctionFlags flags
) {
241 SendResponseDelegate response_delegate
;
242 function
->set_test_delegate(&response_delegate
);
243 function
->SetArgs(args
.get());
246 function
->set_dispatcher(dispatcher
->AsWeakPtr());
248 function
->set_browser_context(context
);
249 function
->set_include_incognito(flags
& INCLUDE_INCOGNITO
);
250 function
->Run()->Execute();
252 // If the RunAsync of |function| didn't already call SendResponse, run the
253 // message loop until they do.
254 if (!response_delegate
.HasResponse()) {
255 response_delegate
.set_should_post_quit(true);
256 content::RunMessageLoop();
259 EXPECT_TRUE(response_delegate
.HasResponse());
260 return response_delegate
.GetResponse();
263 } // namespace api_test_utils
264 } // namespace extensions