Extension syncing: Introduce a NeedsSync pref
[chromium-blink-merge.git] / extensions / browser / api_test_utils.cc
blob587f8e298098bc6c8a521a82ad6882d1f7df9ccb
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 scoped_ptr<base::Value> ParseJSON(const std::string& data) {
23 return base::JSONReader::Read(data);
26 scoped_ptr<base::ListValue> ParseList(const std::string& data) {
27 scoped_ptr<base::Value> result = ParseJSON(data);
28 scoped_ptr<base::ListValue> list_result;
29 if (result->GetAsList(nullptr))
30 list_result.reset(static_cast<base::ListValue*>(result.release()));
31 return list_result;
34 // This helps us be able to wait until an UIThreadExtensionFunction calls
35 // SendResponse.
36 class SendResponseDelegate
37 : public UIThreadExtensionFunction::DelegateForTests {
38 public:
39 SendResponseDelegate() : should_post_quit_(false) {}
41 virtual ~SendResponseDelegate() {}
43 void set_should_post_quit(bool should_quit) {
44 should_post_quit_ = should_quit;
47 bool HasResponse() { return response_.get() != NULL; }
49 bool GetResponse() {
50 EXPECT_TRUE(HasResponse());
51 return *response_.get();
54 void OnSendResponse(UIThreadExtensionFunction* function,
55 bool success,
56 bool bad_message) override {
57 ASSERT_FALSE(bad_message);
58 ASSERT_FALSE(HasResponse());
59 response_.reset(new bool);
60 *response_ = success;
61 if (should_post_quit_) {
62 base::MessageLoopForUI::current()->Quit();
66 private:
67 scoped_ptr<bool> response_;
68 bool should_post_quit_;
71 } // namespace
73 namespace extensions {
75 namespace api_test_utils {
77 scoped_ptr<base::DictionaryValue> ParseDictionary(const std::string& data) {
78 scoped_ptr<base::Value> result = ParseJSON(data);
79 scoped_ptr<base::DictionaryValue> dict_result;
80 if (result->GetAsDictionary(nullptr))
81 dict_result.reset(static_cast<base::DictionaryValue*>(result.release()));
82 return dict_result;
85 bool GetBoolean(const base::DictionaryValue* val, const std::string& key) {
86 bool result = false;
87 if (!val->GetBoolean(key, &result))
88 ADD_FAILURE() << key << " does not exist or is not a boolean.";
89 return result;
92 int GetInteger(const base::DictionaryValue* val, const std::string& key) {
93 int result = 0;
94 if (!val->GetInteger(key, &result))
95 ADD_FAILURE() << key << " does not exist or is not an integer.";
96 return result;
99 std::string GetString(const base::DictionaryValue* val,
100 const std::string& key) {
101 std::string result;
102 if (!val->GetString(key, &result))
103 ADD_FAILURE() << key << " does not exist or is not a string.";
104 return result;
107 scoped_refptr<Extension> CreateExtension(
108 Manifest::Location location,
109 base::DictionaryValue* test_extension_value,
110 const std::string& id_input) {
111 std::string error;
112 const base::FilePath test_extension_path;
113 std::string id;
114 if (!id_input.empty())
115 id = crx_file::id_util::GenerateId(id_input);
116 scoped_refptr<Extension> extension(
117 Extension::Create(test_extension_path, location, *test_extension_value,
118 Extension::NO_FLAGS, id, &error));
119 EXPECT_TRUE(error.empty()) << "Could not parse test extension " << error;
120 return extension;
123 scoped_refptr<Extension> CreateExtension(
124 base::DictionaryValue* test_extension_value) {
125 return CreateExtension(Manifest::INTERNAL, test_extension_value,
126 std::string());
129 scoped_refptr<Extension> CreateEmptyExtensionWithLocation(
130 Manifest::Location location) {
131 scoped_ptr<base::DictionaryValue> test_extension_value =
132 ParseDictionary("{\"name\": \"Test\", \"version\": \"1.0\"}");
133 return CreateExtension(location, test_extension_value.get(), std::string());
136 base::Value* RunFunctionWithDelegateAndReturnSingleResult(
137 UIThreadExtensionFunction* function,
138 const std::string& args,
139 content::BrowserContext* context,
140 scoped_ptr<extensions::ExtensionFunctionDispatcher> dispatcher) {
141 return RunFunctionWithDelegateAndReturnSingleResult(
142 function, args, context, dispatcher.Pass(), NONE);
145 base::Value* RunFunctionWithDelegateAndReturnSingleResult(
146 UIThreadExtensionFunction* function,
147 const std::string& args,
148 content::BrowserContext* context,
149 scoped_ptr<extensions::ExtensionFunctionDispatcher> dispatcher,
150 RunFunctionFlags flags) {
151 scoped_refptr<ExtensionFunction> function_owner(function);
152 // Without a callback the function will not generate a result.
153 function->set_has_callback(true);
154 RunFunction(function, args, context, dispatcher.Pass(), flags);
155 EXPECT_TRUE(function->GetError().empty())
156 << "Unexpected error: " << function->GetError();
157 const base::Value* single_result = NULL;
158 if (function->GetResultList() != NULL &&
159 function->GetResultList()->Get(0, &single_result)) {
160 return single_result->DeepCopy();
162 return NULL;
165 base::Value* RunFunctionAndReturnSingleResult(
166 UIThreadExtensionFunction* function,
167 const std::string& args,
168 content::BrowserContext* context) {
169 return RunFunctionAndReturnSingleResult(function, args, context, NONE);
172 base::Value* RunFunctionAndReturnSingleResult(
173 UIThreadExtensionFunction* function,
174 const std::string& args,
175 content::BrowserContext* context,
176 RunFunctionFlags flags) {
177 scoped_ptr<ExtensionFunctionDispatcher> dispatcher(
178 new ExtensionFunctionDispatcher(context));
180 return RunFunctionWithDelegateAndReturnSingleResult(
181 function, args, context, dispatcher.Pass(), flags);
184 std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function,
185 const std::string& args,
186 content::BrowserContext* context) {
187 return RunFunctionAndReturnError(function, args, context, NONE);
190 std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function,
191 const std::string& args,
192 content::BrowserContext* context,
193 RunFunctionFlags flags) {
194 scoped_ptr<ExtensionFunctionDispatcher> dispatcher(
195 new ExtensionFunctionDispatcher(context));
196 scoped_refptr<ExtensionFunction> function_owner(function);
197 // Without a callback the function will not generate a result.
198 function->set_has_callback(true);
199 RunFunction(function, args, context, dispatcher.Pass(), flags);
200 EXPECT_FALSE(function->GetResultList()) << "Did not expect a result";
201 return function->GetError();
204 bool RunFunction(UIThreadExtensionFunction* function,
205 const std::string& args,
206 content::BrowserContext* context) {
207 scoped_ptr<ExtensionFunctionDispatcher> dispatcher(
208 new ExtensionFunctionDispatcher(context));
209 return RunFunction(function, args, context, dispatcher.Pass(), NONE);
212 bool RunFunction(UIThreadExtensionFunction* function,
213 const std::string& args,
214 content::BrowserContext* context,
215 scoped_ptr<extensions::ExtensionFunctionDispatcher> dispatcher,
216 RunFunctionFlags flags) {
217 scoped_ptr<base::ListValue> parsed_args = ParseList(args);
218 EXPECT_TRUE(parsed_args.get())
219 << "Could not parse extension function arguments: " << args;
220 return RunFunction(
221 function, parsed_args.Pass(), context, dispatcher.Pass(), flags);
224 bool RunFunction(UIThreadExtensionFunction* function,
225 scoped_ptr<base::ListValue> args,
226 content::BrowserContext* context,
227 scoped_ptr<extensions::ExtensionFunctionDispatcher> dispatcher,
228 RunFunctionFlags flags) {
229 SendResponseDelegate response_delegate;
230 function->set_test_delegate(&response_delegate);
231 function->SetArgs(args.get());
233 CHECK(dispatcher);
234 function->set_dispatcher(dispatcher->AsWeakPtr());
236 function->set_browser_context(context);
237 function->set_include_incognito(flags & INCLUDE_INCOGNITO);
238 function->Run()->Execute();
240 // If the RunAsync of |function| didn't already call SendResponse, run the
241 // message loop until they do.
242 if (!response_delegate.HasResponse()) {
243 response_delegate.set_should_post_quit(true);
244 content::RunMessageLoop();
247 EXPECT_TRUE(response_delegate.HasResponse());
248 return response_delegate.GetResponse();
251 } // namespace api_test_utils
252 } // namespace extensions