blob: cf16bd1a60830bb3ed2a3ebe5a40f1578130be79 [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_dispatcher.h"
[email protected]21a40082013-10-28 21:19:2314#include "chrome/browser/profiles/profile.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]14c3571a2013-11-13 00:18:4418#include "extensions/browser/extension_function.h"
[email protected]993da5e2013-03-23 21:25:1619#include "extensions/common/id_util.h"
[email protected]637bf322011-10-01 20:46:3220#include "testing/gtest/include/gtest/gtest.h"
21
[email protected]ea049a02011-12-25 21:37:0922using content::WebContents;
[email protected]1c321ee2012-05-21 03:02:3423using extensions::Extension;
[email protected]1d5e58b2013-01-31 08:41:4024using extensions::Manifest;
[email protected]37bb5822012-09-10 15:09:5725namespace keys = extensions::tabs_constants;
[email protected]ea049a02011-12-25 21:37:0926
[email protected]4e3ce3b2011-10-14 23:25:1727namespace {
28
29class TestFunctionDispatcherDelegate
30 : public ExtensionFunctionDispatcher::Delegate {
31 public:
32 explicit TestFunctionDispatcherDelegate(Browser* browser) :
33 browser_(browser) {}
34 virtual ~TestFunctionDispatcherDelegate() {}
35
36 private:
[email protected]44f4b132012-07-17 20:36:5737 virtual extensions::WindowController* GetExtensionWindowController()
[email protected]b51f3562012-05-05 22:01:4338 const OVERRIDE {
39 return browser_->extension_window_controller();
[email protected]4e3ce3b2011-10-14 23:25:1740 }
41
[email protected]ea049a02011-12-25 21:37:0942 virtual WebContents* GetAssociatedWebContents() const OVERRIDE {
[email protected]4e3ce3b2011-10-14 23:25:1743 return NULL;
44 }
45
46 Browser* browser_;
47};
48
49} // namespace
50
[email protected]637bf322011-10-01 20:46:3251namespace extension_function_test_utils {
52
53base::Value* ParseJSON(const std::string& data) {
[email protected]cd5785752012-04-11 00:15:4154 return base::JSONReader::Read(data);
[email protected]637bf322011-10-01 20:46:3255}
56
57base::ListValue* ParseList(const std::string& data) {
58 scoped_ptr<base::Value> result(ParseJSON(data));
59 if (result.get() && result->IsType(base::Value::TYPE_LIST))
60 return static_cast<base::ListValue*>(result.release());
61 else
62 return NULL;
63}
64
65base::DictionaryValue* ParseDictionary(
66 const std::string& data) {
67 scoped_ptr<base::Value> result(ParseJSON(data));
68 if (result.get() && result->IsType(base::Value::TYPE_DICTIONARY))
69 return static_cast<base::DictionaryValue*>(result.release());
70 else
71 return NULL;
72}
73
74bool GetBoolean(base::DictionaryValue* val, const std::string& key) {
75 bool result = false;
76 if (!val->GetBoolean(key, &result))
77 ADD_FAILURE() << key << " does not exist or is not a boolean.";
78 return result;
79}
80
81int GetInteger(base::DictionaryValue* val, const std::string& key) {
82 int result = 0;
83 if (!val->GetInteger(key, &result))
84 ADD_FAILURE() << key << " does not exist or is not an integer.";
85 return result;
86}
87
88std::string GetString(base::DictionaryValue* val, const std::string& key) {
89 std::string result;
90 if (!val->GetString(key, &result))
91 ADD_FAILURE() << key << " does not exist or is not a string.";
92 return result;
93}
94
95base::DictionaryValue* ToDictionary(base::Value* val) {
[email protected]8ce80ea62011-10-18 18:00:1896 EXPECT_TRUE(val);
97 EXPECT_EQ(base::Value::TYPE_DICTIONARY, val->GetType());
[email protected]637bf322011-10-01 20:46:3298 return static_cast<base::DictionaryValue*>(val);
99}
100
[email protected]008ff7fb2011-12-19 08:51:17101base::ListValue* ToList(base::Value* val) {
102 EXPECT_TRUE(val);
103 EXPECT_EQ(base::Value::TYPE_LIST, val->GetType());
104 return static_cast<base::ListValue*>(val);
105}
106
[email protected]4e3ce3b2011-10-14 23:25:17107scoped_refptr<Extension> CreateEmptyExtension() {
[email protected]1d5e58b2013-01-31 08:41:40108 return CreateEmptyExtensionWithLocation(Manifest::INTERNAL);
[email protected]6d2d55b2012-05-05 21:33:43109}
110
111scoped_refptr<Extension> CreateEmptyExtensionWithLocation(
[email protected]1d5e58b2013-01-31 08:41:40112 Manifest::Location location) {
[email protected]4e3ce3b2011-10-14 23:25:17113 scoped_ptr<base::DictionaryValue> test_extension_value(
114 ParseDictionary("{\"name\": \"Test\", \"version\": \"1.0\"}"));
[email protected]09426852012-09-11 22:39:07115 return CreateExtension(location, test_extension_value.get(), std::string());
116}
117
118scoped_refptr<Extension> CreateEmptyExtension(
119 const std::string& id_input) {
120 scoped_ptr<base::DictionaryValue> test_extension_value(
121 ParseDictionary("{\"name\": \"Test\", \"version\": \"1.0\"}"));
[email protected]1d5e58b2013-01-31 08:41:40122 return CreateExtension(Manifest::INTERNAL, test_extension_value.get(),
[email protected]09426852012-09-11 22:39:07123 id_input);
[email protected]37bb5822012-09-10 15:09:57124}
125
126scoped_refptr<Extension> CreateExtension(
127 base::DictionaryValue* test_extension_value) {
[email protected]1d5e58b2013-01-31 08:41:40128 return CreateExtension(Manifest::INTERNAL, test_extension_value,
[email protected]09426852012-09-11 22:39:07129 std::string());
[email protected]37bb5822012-09-10 15:09:57130}
131
132scoped_refptr<Extension> CreateExtension(
[email protected]1d5e58b2013-01-31 08:41:40133 Manifest::Location location,
[email protected]09426852012-09-11 22:39:07134 base::DictionaryValue* test_extension_value,
135 const std::string& id_input) {
[email protected]37bb5822012-09-10 15:09:57136 std::string error;
[email protected]650b2d52013-02-10 03:41:45137 const base::FilePath test_extension_path;
[email protected]09426852012-09-11 22:39:07138 std::string id;
139 if (!id_input.empty())
[email protected]993da5e2013-03-23 21:25:16140 id = extensions::id_util::GenerateId(id_input);
[email protected]4e3ce3b2011-10-14 23:25:17141 scoped_refptr<Extension> extension(Extension::Create(
142 test_extension_path,
[email protected]6d2d55b2012-05-05 21:33:43143 location,
[email protected]37bb5822012-09-10 15:09:57144 *test_extension_value,
[email protected]4e3ce3b2011-10-14 23:25:17145 Extension::NO_FLAGS,
[email protected]09426852012-09-11 22:39:07146 id,
[email protected]4e3ce3b2011-10-14 23:25:17147 &error));
[email protected]8ce80ea62011-10-18 18:00:18148 EXPECT_TRUE(error.empty()) << "Could not parse test extension " << error;
[email protected]4e3ce3b2011-10-14 23:25:17149 return extension;
150}
151
[email protected]37bb5822012-09-10 15:09:57152bool HasPrivacySensitiveFields(base::DictionaryValue* val) {
153 std::string result;
154 if (val->GetString(keys::kUrlKey, &result) ||
155 val->GetString(keys::kTitleKey, &result) ||
156 val->GetString(keys::kFaviconUrlKey, &result))
157 return true;
158 return false;
159}
160
[email protected]637bf322011-10-01 20:46:32161std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function,
162 const std::string& args,
163 Browser* browser) {
164 return RunFunctionAndReturnError(function, args, browser, NONE);
165}
166std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function,
167 const std::string& args,
168 Browser* browser,
169 RunFunctionFlags flags) {
170 scoped_refptr<ExtensionFunction> function_owner(function);
[email protected]61165fd2012-07-24 01:12:23171 // Without a callback the function will not generate a result.
172 function->set_has_callback(true);
[email protected]637bf322011-10-01 20:46:32173 RunFunction(function, args, browser, flags);
[email protected]07ff5fd2012-07-12 22:39:09174 EXPECT_FALSE(function->GetResultList()) << "Did not expect a result";
[email protected]637bf322011-10-01 20:46:32175 return function->GetError();
176}
177
[email protected]07ff5fd2012-07-12 22:39:09178base::Value* RunFunctionAndReturnSingleResult(
179 UIThreadExtensionFunction* function,
180 const std::string& args,
181 Browser* browser) {
182 return RunFunctionAndReturnSingleResult(function, args, browser, NONE);
[email protected]637bf322011-10-01 20:46:32183}
[email protected]07ff5fd2012-07-12 22:39:09184base::Value* RunFunctionAndReturnSingleResult(
185 UIThreadExtensionFunction* function,
186 const std::string& args,
187 Browser* browser,
188 RunFunctionFlags flags) {
[email protected]637bf322011-10-01 20:46:32189 scoped_refptr<ExtensionFunction> function_owner(function);
[email protected]61165fd2012-07-24 01:12:23190 // Without a callback the function will not generate a result.
191 function->set_has_callback(true);
[email protected]637bf322011-10-01 20:46:32192 RunFunction(function, args, browser, flags);
[email protected]8ce80ea62011-10-18 18:00:18193 EXPECT_TRUE(function->GetError().empty()) << "Unexpected error: "
194 << function->GetError();
[email protected]5d30f92bf2012-08-03 08:43:37195 const base::Value* single_result = NULL;
[email protected]07ff5fd2012-07-12 22:39:09196 if (function->GetResultList() != NULL &&
197 function->GetResultList()->Get(0, &single_result)) {
198 return single_result->DeepCopy();
199 }
200 return NULL;
[email protected]637bf322011-10-01 20:46:32201}
202
[email protected]4bf2b052013-09-30 21:05:41203// This helps us be able to wait until an UIThreadExtensionFunction calls
[email protected]d8c8749b92011-11-16 22:31:32204// SendResponse.
[email protected]bdfc03e2011-11-22 00:20:33205class SendResponseDelegate
206 : public UIThreadExtensionFunction::DelegateForTests {
[email protected]d8c8749b92011-11-16 22:31:32207 public:
208 SendResponseDelegate() : should_post_quit_(false) {}
209
210 virtual ~SendResponseDelegate() {}
211
212 void set_should_post_quit(bool should_quit) {
213 should_post_quit_ = should_quit;
214 }
215
216 bool HasResponse() {
217 return response_.get() != NULL;
218 }
219
220 bool GetResponse() {
221 EXPECT_TRUE(HasResponse());
222 return *response_.get();
223 }
224
[email protected]bdfc03e2011-11-22 00:20:33225 virtual void OnSendResponse(UIThreadExtensionFunction* function,
[email protected]ca6df682012-04-10 23:00:20226 bool success,
[email protected]49aeab62013-02-07 02:53:11227 bool bad_message) OVERRIDE {
[email protected]ca6df682012-04-10 23:00:20228 ASSERT_FALSE(bad_message);
[email protected]d8c8749b92011-11-16 22:31:32229 ASSERT_FALSE(HasResponse());
230 response_.reset(new bool);
231 *response_ = success;
232 if (should_post_quit_) {
[email protected]b3a25092013-05-28 22:08:16233 base::MessageLoopForUI::current()->Quit();
[email protected]d8c8749b92011-11-16 22:31:32234 }
235 }
236
237 private:
238 scoped_ptr<bool> response_;
239 bool should_post_quit_;
240};
241
[email protected]bdfc03e2011-11-22 00:20:33242bool RunFunction(UIThreadExtensionFunction* function,
243 const std::string& args,
244 Browser* browser,
245 RunFunctionFlags flags) {
[email protected]d8c8749b92011-11-16 22:31:32246 SendResponseDelegate response_delegate;
247 function->set_test_delegate(&response_delegate);
[email protected]bdfc03e2011-11-22 00:20:33248 scoped_ptr<base::ListValue> parsed_args(ParseList(args));
249 EXPECT_TRUE(parsed_args.get()) <<
250 "Could not parse extension function arguments: " << args;
251 function->SetArgs(parsed_args.get());
252
253 TestFunctionDispatcherDelegate dispatcher_delegate(browser);
254 ExtensionFunctionDispatcher dispatcher(
255 browser->profile(), &dispatcher_delegate);
256 function->set_dispatcher(dispatcher.AsWeakPtr());
257
[email protected]21a40082013-10-28 21:19:23258 function->set_context(browser->profile());
[email protected]bdfc03e2011-11-22 00:20:33259 function->set_include_incognito(flags & INCLUDE_INCOGNITO);
260 function->Run();
[email protected]d8c8749b92011-11-16 22:31:32261
262 // If the RunImpl of |function| didn't already call SendResponse, run the
263 // message loop until they do.
264 if (!response_delegate.HasResponse()) {
265 response_delegate.set_should_post_quit(true);
[email protected]729eb632012-07-26 04:45:26266 content::RunMessageLoop();
[email protected]d8c8749b92011-11-16 22:31:32267 }
268
269 EXPECT_TRUE(response_delegate.HasResponse());
270 return response_delegate.GetResponse();
271}
272
[email protected]637bf322011-10-01 20:46:32273} // namespace extension_function_test_utils