blob: b642c1fd0145a3b88f59e85023c5e6b42f0973e0 [file] [log] [blame]
[email protected]8809f1442012-01-20 21:21:471// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]637bf322011-10-01 20:46:322// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "chrome/browser/extensions/extension_function_test_utils.h"
6
7#include <string>
8
[email protected]57999812013-02-24 05:40:529#include "base/files/file_path.h"
[email protected]637bf322011-10-01 20:46:3210#include "base/json/json_reader.h"
11#include "base/values.h"
[email protected]37bb5822012-09-10 15:09:5712#include "chrome/browser/extensions/api/tabs/tabs_constants.h"
[email protected]4e3ce3b2011-10-14 23:25:1713#include "chrome/browser/extensions/extension_function.h"
14#include "chrome/browser/extensions/extension_function_dispatcher.h"
[email protected]637bf322011-10-01 20:46:3215#include "chrome/browser/ui/browser.h"
[email protected]4e3ce3b2011-10-14 23:25:1716#include "chrome/common/extensions/extension.h"
[email protected]d8c8749b92011-11-16 22:31:3217#include "chrome/test/base/ui_test_utils.h"
[email protected]637bf322011-10-01 20:46:3218#include "testing/gtest/include/gtest/gtest.h"
19
[email protected]ea049a02011-12-25 21:37:0920using content::WebContents;
[email protected]1c321ee2012-05-21 03:02:3421using extensions::Extension;
[email protected]1d5e58b2013-01-31 08:41:4022using extensions::Manifest;
[email protected]37bb5822012-09-10 15:09:5723namespace keys = extensions::tabs_constants;
[email protected]ea049a02011-12-25 21:37:0924
[email protected]4e3ce3b2011-10-14 23:25:1725namespace {
26
27class TestFunctionDispatcherDelegate
28 : public ExtensionFunctionDispatcher::Delegate {
29 public:
30 explicit TestFunctionDispatcherDelegate(Browser* browser) :
31 browser_(browser) {}
32 virtual ~TestFunctionDispatcherDelegate() {}
33
34 private:
[email protected]44f4b132012-07-17 20:36:5735 virtual extensions::WindowController* GetExtensionWindowController()
[email protected]b51f3562012-05-05 22:01:4336 const OVERRIDE {
37 return browser_->extension_window_controller();
[email protected]4e3ce3b2011-10-14 23:25:1738 }
39
[email protected]ea049a02011-12-25 21:37:0940 virtual WebContents* GetAssociatedWebContents() const OVERRIDE {
[email protected]4e3ce3b2011-10-14 23:25:1741 return NULL;
42 }
43
44 Browser* browser_;
45};
46
47} // namespace
48
[email protected]637bf322011-10-01 20:46:3249namespace extension_function_test_utils {
50
51base::Value* ParseJSON(const std::string& data) {
[email protected]cd5785752012-04-11 00:15:4152 return base::JSONReader::Read(data);
[email protected]637bf322011-10-01 20:46:3253}
54
55base::ListValue* ParseList(const std::string& data) {
56 scoped_ptr<base::Value> result(ParseJSON(data));
57 if (result.get() && result->IsType(base::Value::TYPE_LIST))
58 return static_cast<base::ListValue*>(result.release());
59 else
60 return NULL;
61}
62
63base::DictionaryValue* ParseDictionary(
64 const std::string& data) {
65 scoped_ptr<base::Value> result(ParseJSON(data));
66 if (result.get() && result->IsType(base::Value::TYPE_DICTIONARY))
67 return static_cast<base::DictionaryValue*>(result.release());
68 else
69 return NULL;
70}
71
72bool GetBoolean(base::DictionaryValue* val, const std::string& key) {
73 bool result = false;
74 if (!val->GetBoolean(key, &result))
75 ADD_FAILURE() << key << " does not exist or is not a boolean.";
76 return result;
77}
78
79int GetInteger(base::DictionaryValue* val, const std::string& key) {
80 int result = 0;
81 if (!val->GetInteger(key, &result))
82 ADD_FAILURE() << key << " does not exist or is not an integer.";
83 return result;
84}
85
86std::string GetString(base::DictionaryValue* val, const std::string& key) {
87 std::string result;
88 if (!val->GetString(key, &result))
89 ADD_FAILURE() << key << " does not exist or is not a string.";
90 return result;
91}
92
93base::DictionaryValue* ToDictionary(base::Value* val) {
[email protected]8ce80ea62011-10-18 18:00:1894 EXPECT_TRUE(val);
95 EXPECT_EQ(base::Value::TYPE_DICTIONARY, val->GetType());
[email protected]637bf322011-10-01 20:46:3296 return static_cast<base::DictionaryValue*>(val);
97}
98
[email protected]008ff7fb2011-12-19 08:51:1799base::ListValue* ToList(base::Value* val) {
100 EXPECT_TRUE(val);
101 EXPECT_EQ(base::Value::TYPE_LIST, val->GetType());
102 return static_cast<base::ListValue*>(val);
103}
104
[email protected]4e3ce3b2011-10-14 23:25:17105scoped_refptr<Extension> CreateEmptyExtension() {
[email protected]1d5e58b2013-01-31 08:41:40106 return CreateEmptyExtensionWithLocation(Manifest::INTERNAL);
[email protected]6d2d55b2012-05-05 21:33:43107}
108
109scoped_refptr<Extension> CreateEmptyExtensionWithLocation(
[email protected]1d5e58b2013-01-31 08:41:40110 Manifest::Location location) {
[email protected]4e3ce3b2011-10-14 23:25:17111 scoped_ptr<base::DictionaryValue> test_extension_value(
112 ParseDictionary("{\"name\": \"Test\", \"version\": \"1.0\"}"));
[email protected]09426852012-09-11 22:39:07113 return CreateExtension(location, test_extension_value.get(), std::string());
114}
115
116scoped_refptr<Extension> CreateEmptyExtension(
117 const std::string& id_input) {
118 scoped_ptr<base::DictionaryValue> test_extension_value(
119 ParseDictionary("{\"name\": \"Test\", \"version\": \"1.0\"}"));
[email protected]1d5e58b2013-01-31 08:41:40120 return CreateExtension(Manifest::INTERNAL, test_extension_value.get(),
[email protected]09426852012-09-11 22:39:07121 id_input);
[email protected]37bb5822012-09-10 15:09:57122}
123
124scoped_refptr<Extension> CreateExtension(
125 base::DictionaryValue* test_extension_value) {
[email protected]1d5e58b2013-01-31 08:41:40126 return CreateExtension(Manifest::INTERNAL, test_extension_value,
[email protected]09426852012-09-11 22:39:07127 std::string());
[email protected]37bb5822012-09-10 15:09:57128}
129
130scoped_refptr<Extension> CreateExtension(
[email protected]1d5e58b2013-01-31 08:41:40131 Manifest::Location location,
[email protected]09426852012-09-11 22:39:07132 base::DictionaryValue* test_extension_value,
133 const std::string& id_input) {
[email protected]37bb5822012-09-10 15:09:57134 std::string error;
[email protected]650b2d52013-02-10 03:41:45135 const base::FilePath test_extension_path;
[email protected]09426852012-09-11 22:39:07136 std::string id;
137 if (!id_input.empty())
138 CHECK(Extension::GenerateId(id_input, &id));
[email protected]4e3ce3b2011-10-14 23:25:17139 scoped_refptr<Extension> extension(Extension::Create(
140 test_extension_path,
[email protected]6d2d55b2012-05-05 21:33:43141 location,
[email protected]37bb5822012-09-10 15:09:57142 *test_extension_value,
[email protected]4e3ce3b2011-10-14 23:25:17143 Extension::NO_FLAGS,
[email protected]09426852012-09-11 22:39:07144 id,
[email protected]4e3ce3b2011-10-14 23:25:17145 &error));
[email protected]8ce80ea62011-10-18 18:00:18146 EXPECT_TRUE(error.empty()) << "Could not parse test extension " << error;
[email protected]4e3ce3b2011-10-14 23:25:17147 return extension;
148}
149
[email protected]37bb5822012-09-10 15:09:57150bool HasPrivacySensitiveFields(base::DictionaryValue* val) {
151 std::string result;
152 if (val->GetString(keys::kUrlKey, &result) ||
153 val->GetString(keys::kTitleKey, &result) ||
154 val->GetString(keys::kFaviconUrlKey, &result))
155 return true;
156 return false;
157}
158
[email protected]637bf322011-10-01 20:46:32159std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function,
160 const std::string& args,
161 Browser* browser) {
162 return RunFunctionAndReturnError(function, args, browser, NONE);
163}
164std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function,
165 const std::string& args,
166 Browser* browser,
167 RunFunctionFlags flags) {
168 scoped_refptr<ExtensionFunction> function_owner(function);
[email protected]61165fd2012-07-24 01:12:23169 // Without a callback the function will not generate a result.
170 function->set_has_callback(true);
[email protected]637bf322011-10-01 20:46:32171 RunFunction(function, args, browser, flags);
[email protected]07ff5fd2012-07-12 22:39:09172 EXPECT_FALSE(function->GetResultList()) << "Did not expect a result";
[email protected]637bf322011-10-01 20:46:32173 return function->GetError();
174}
175
[email protected]07ff5fd2012-07-12 22:39:09176base::Value* RunFunctionAndReturnSingleResult(
177 UIThreadExtensionFunction* function,
178 const std::string& args,
179 Browser* browser) {
180 return RunFunctionAndReturnSingleResult(function, args, browser, NONE);
[email protected]637bf322011-10-01 20:46:32181}
[email protected]07ff5fd2012-07-12 22:39:09182base::Value* RunFunctionAndReturnSingleResult(
183 UIThreadExtensionFunction* function,
184 const std::string& args,
185 Browser* browser,
186 RunFunctionFlags flags) {
[email protected]637bf322011-10-01 20:46:32187 scoped_refptr<ExtensionFunction> function_owner(function);
[email protected]61165fd2012-07-24 01:12:23188 // Without a callback the function will not generate a result.
189 function->set_has_callback(true);
[email protected]637bf322011-10-01 20:46:32190 RunFunction(function, args, browser, flags);
[email protected]8ce80ea62011-10-18 18:00:18191 EXPECT_TRUE(function->GetError().empty()) << "Unexpected error: "
192 << function->GetError();
[email protected]5d30f92bf2012-08-03 08:43:37193 const base::Value* single_result = NULL;
[email protected]07ff5fd2012-07-12 22:39:09194 if (function->GetResultList() != NULL &&
195 function->GetResultList()->Get(0, &single_result)) {
196 return single_result->DeepCopy();
197 }
198 return NULL;
[email protected]637bf322011-10-01 20:46:32199}
200
[email protected]d8c8749b92011-11-16 22:31:32201// This helps us be able to wait until an AsyncExtensionFunction calls
202// SendResponse.
[email protected]bdfc03e2011-11-22 00:20:33203class SendResponseDelegate
204 : public UIThreadExtensionFunction::DelegateForTests {
[email protected]d8c8749b92011-11-16 22:31:32205 public:
206 SendResponseDelegate() : should_post_quit_(false) {}
207
208 virtual ~SendResponseDelegate() {}
209
210 void set_should_post_quit(bool should_quit) {
211 should_post_quit_ = should_quit;
212 }
213
214 bool HasResponse() {
215 return response_.get() != NULL;
216 }
217
218 bool GetResponse() {
219 EXPECT_TRUE(HasResponse());
220 return *response_.get();
221 }
222
[email protected]bdfc03e2011-11-22 00:20:33223 virtual void OnSendResponse(UIThreadExtensionFunction* function,
[email protected]ca6df682012-04-10 23:00:20224 bool success,
[email protected]49aeab62013-02-07 02:53:11225 bool bad_message) OVERRIDE {
[email protected]ca6df682012-04-10 23:00:20226 ASSERT_FALSE(bad_message);
[email protected]d8c8749b92011-11-16 22:31:32227 ASSERT_FALSE(HasResponse());
228 response_.reset(new bool);
229 *response_ = success;
230 if (should_post_quit_) {
231 MessageLoopForUI::current()->Quit();
232 }
233 }
234
235 private:
236 scoped_ptr<bool> response_;
237 bool should_post_quit_;
238};
239
[email protected]bdfc03e2011-11-22 00:20:33240bool RunFunction(UIThreadExtensionFunction* function,
241 const std::string& args,
242 Browser* browser,
243 RunFunctionFlags flags) {
[email protected]d8c8749b92011-11-16 22:31:32244 SendResponseDelegate response_delegate;
245 function->set_test_delegate(&response_delegate);
[email protected]bdfc03e2011-11-22 00:20:33246 scoped_ptr<base::ListValue> parsed_args(ParseList(args));
247 EXPECT_TRUE(parsed_args.get()) <<
248 "Could not parse extension function arguments: " << args;
249 function->SetArgs(parsed_args.get());
250
251 TestFunctionDispatcherDelegate dispatcher_delegate(browser);
252 ExtensionFunctionDispatcher dispatcher(
253 browser->profile(), &dispatcher_delegate);
254 function->set_dispatcher(dispatcher.AsWeakPtr());
255
256 function->set_profile(browser->profile());
257 function->set_include_incognito(flags & INCLUDE_INCOGNITO);
258 function->Run();
[email protected]d8c8749b92011-11-16 22:31:32259
260 // If the RunImpl of |function| didn't already call SendResponse, run the
261 // message loop until they do.
262 if (!response_delegate.HasResponse()) {
263 response_delegate.set_should_post_quit(true);
[email protected]729eb632012-07-26 04:45:26264 content::RunMessageLoop();
[email protected]d8c8749b92011-11-16 22:31:32265 }
266
267 EXPECT_TRUE(response_delegate.HasResponse());
268 return response_delegate.GetResponse();
269}
270
[email protected]637bf322011-10-01 20:46:32271} // namespace extension_function_test_utils