blob: f5561f3312270d7157a0643c83863dc87ec04919 [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]4e3ce3b2011-10-14 23:25:179#include "base/file_path.h"
[email protected]637bf322011-10-01 20:46:3210#include "base/json/json_reader.h"
11#include "base/values.h"
[email protected]4e3ce3b2011-10-14 23:25:1712#include "chrome/browser/extensions/extension_function.h"
13#include "chrome/browser/extensions/extension_function_dispatcher.h"
[email protected]637bf322011-10-01 20:46:3214#include "chrome/browser/ui/browser.h"
[email protected]4e3ce3b2011-10-14 23:25:1715#include "chrome/common/extensions/extension.h"
[email protected]d8c8749b92011-11-16 22:31:3216#include "chrome/test/base/ui_test_utils.h"
[email protected]637bf322011-10-01 20:46:3217#include "testing/gtest/include/gtest/gtest.h"
18
[email protected]ea049a02011-12-25 21:37:0919using content::WebContents;
20
[email protected]4e3ce3b2011-10-14 23:25:1721namespace {
22
23class TestFunctionDispatcherDelegate
24 : public ExtensionFunctionDispatcher::Delegate {
25 public:
26 explicit TestFunctionDispatcherDelegate(Browser* browser) :
27 browser_(browser) {}
28 virtual ~TestFunctionDispatcherDelegate() {}
29
30 private:
31 virtual Browser* GetBrowser() OVERRIDE {
32 return browser_;
33 }
34
[email protected]ea049a02011-12-25 21:37:0935 virtual WebContents* GetAssociatedWebContents() const OVERRIDE {
[email protected]4e3ce3b2011-10-14 23:25:1736 return NULL;
37 }
38
39 Browser* browser_;
40};
41
42} // namespace
43
[email protected]637bf322011-10-01 20:46:3244namespace extension_function_test_utils {
45
46base::Value* ParseJSON(const std::string& data) {
[email protected]cd5785752012-04-11 00:15:4147 return base::JSONReader::Read(data);
[email protected]637bf322011-10-01 20:46:3248}
49
50base::ListValue* ParseList(const std::string& data) {
51 scoped_ptr<base::Value> result(ParseJSON(data));
52 if (result.get() && result->IsType(base::Value::TYPE_LIST))
53 return static_cast<base::ListValue*>(result.release());
54 else
55 return NULL;
56}
57
58base::DictionaryValue* ParseDictionary(
59 const std::string& data) {
60 scoped_ptr<base::Value> result(ParseJSON(data));
61 if (result.get() && result->IsType(base::Value::TYPE_DICTIONARY))
62 return static_cast<base::DictionaryValue*>(result.release());
63 else
64 return NULL;
65}
66
67bool GetBoolean(base::DictionaryValue* val, const std::string& key) {
68 bool result = false;
69 if (!val->GetBoolean(key, &result))
70 ADD_FAILURE() << key << " does not exist or is not a boolean.";
71 return result;
72}
73
74int GetInteger(base::DictionaryValue* val, const std::string& key) {
75 int result = 0;
76 if (!val->GetInteger(key, &result))
77 ADD_FAILURE() << key << " does not exist or is not an integer.";
78 return result;
79}
80
81std::string GetString(base::DictionaryValue* val, const std::string& key) {
82 std::string result;
83 if (!val->GetString(key, &result))
84 ADD_FAILURE() << key << " does not exist or is not a string.";
85 return result;
86}
87
88base::DictionaryValue* ToDictionary(base::Value* val) {
[email protected]8ce80ea62011-10-18 18:00:1889 EXPECT_TRUE(val);
90 EXPECT_EQ(base::Value::TYPE_DICTIONARY, val->GetType());
[email protected]637bf322011-10-01 20:46:3291 return static_cast<base::DictionaryValue*>(val);
92}
93
[email protected]008ff7fb2011-12-19 08:51:1794base::ListValue* ToList(base::Value* val) {
95 EXPECT_TRUE(val);
96 EXPECT_EQ(base::Value::TYPE_LIST, val->GetType());
97 return static_cast<base::ListValue*>(val);
98}
99
[email protected]4e3ce3b2011-10-14 23:25:17100scoped_refptr<Extension> CreateEmptyExtension() {
101 std::string error;
102 const FilePath test_extension_path;
103 scoped_ptr<base::DictionaryValue> test_extension_value(
104 ParseDictionary("{\"name\": \"Test\", \"version\": \"1.0\"}"));
105 scoped_refptr<Extension> extension(Extension::Create(
106 test_extension_path,
107 Extension::INTERNAL,
108 *test_extension_value.get(),
109 Extension::NO_FLAGS,
110 &error));
[email protected]8ce80ea62011-10-18 18:00:18111 EXPECT_TRUE(error.empty()) << "Could not parse test extension " << error;
[email protected]4e3ce3b2011-10-14 23:25:17112 return extension;
113}
114
[email protected]637bf322011-10-01 20:46:32115std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function,
116 const std::string& args,
117 Browser* browser) {
118 return RunFunctionAndReturnError(function, args, browser, NONE);
119}
120std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function,
121 const std::string& args,
122 Browser* browser,
123 RunFunctionFlags flags) {
124 scoped_refptr<ExtensionFunction> function_owner(function);
125 RunFunction(function, args, browser, flags);
[email protected]602542d2012-04-20 02:48:01126 EXPECT_FALSE(function->GetResultValue()) << "Did not expect a result";
[email protected]637bf322011-10-01 20:46:32127 return function->GetError();
128}
129
130base::Value* RunFunctionAndReturnResult(UIThreadExtensionFunction* function,
131 const std::string& args,
132 Browser* browser) {
133 return RunFunctionAndReturnResult(function, args, browser, NONE);
134}
135base::Value* RunFunctionAndReturnResult(UIThreadExtensionFunction* function,
136 const std::string& args,
137 Browser* browser,
138 RunFunctionFlags flags) {
139 scoped_refptr<ExtensionFunction> function_owner(function);
140 RunFunction(function, args, browser, flags);
[email protected]8ce80ea62011-10-18 18:00:18141 EXPECT_TRUE(function->GetError().empty()) << "Unexpected error: "
142 << function->GetError();
[email protected]6b55790a2011-12-15 14:05:05143 return (function->GetResultValue() == NULL) ? NULL :
144 function->GetResultValue()->DeepCopy();
[email protected]637bf322011-10-01 20:46:32145}
146
[email protected]d8c8749b92011-11-16 22:31:32147// This helps us be able to wait until an AsyncExtensionFunction calls
148// SendResponse.
[email protected]bdfc03e2011-11-22 00:20:33149class SendResponseDelegate
150 : public UIThreadExtensionFunction::DelegateForTests {
[email protected]d8c8749b92011-11-16 22:31:32151 public:
152 SendResponseDelegate() : should_post_quit_(false) {}
153
154 virtual ~SendResponseDelegate() {}
155
156 void set_should_post_quit(bool should_quit) {
157 should_post_quit_ = should_quit;
158 }
159
160 bool HasResponse() {
161 return response_.get() != NULL;
162 }
163
164 bool GetResponse() {
165 EXPECT_TRUE(HasResponse());
166 return *response_.get();
167 }
168
[email protected]bdfc03e2011-11-22 00:20:33169 virtual void OnSendResponse(UIThreadExtensionFunction* function,
[email protected]ca6df682012-04-10 23:00:20170 bool success,
171 bool bad_message) {
172 ASSERT_FALSE(bad_message);
[email protected]d8c8749b92011-11-16 22:31:32173 ASSERT_FALSE(HasResponse());
174 response_.reset(new bool);
175 *response_ = success;
176 if (should_post_quit_) {
177 MessageLoopForUI::current()->Quit();
178 }
179 }
180
181 private:
182 scoped_ptr<bool> response_;
183 bool should_post_quit_;
184};
185
[email protected]bdfc03e2011-11-22 00:20:33186bool RunFunction(UIThreadExtensionFunction* function,
187 const std::string& args,
188 Browser* browser,
189 RunFunctionFlags flags) {
[email protected]d8c8749b92011-11-16 22:31:32190 SendResponseDelegate response_delegate;
191 function->set_test_delegate(&response_delegate);
[email protected]bdfc03e2011-11-22 00:20:33192 scoped_ptr<base::ListValue> parsed_args(ParseList(args));
193 EXPECT_TRUE(parsed_args.get()) <<
194 "Could not parse extension function arguments: " << args;
195 function->SetArgs(parsed_args.get());
196
197 TestFunctionDispatcherDelegate dispatcher_delegate(browser);
198 ExtensionFunctionDispatcher dispatcher(
199 browser->profile(), &dispatcher_delegate);
200 function->set_dispatcher(dispatcher.AsWeakPtr());
201
202 function->set_profile(browser->profile());
203 function->set_include_incognito(flags & INCLUDE_INCOGNITO);
204 function->Run();
[email protected]d8c8749b92011-11-16 22:31:32205
206 // If the RunImpl of |function| didn't already call SendResponse, run the
207 // message loop until they do.
208 if (!response_delegate.HasResponse()) {
209 response_delegate.set_should_post_quit(true);
210 ui_test_utils::RunMessageLoop();
211 }
212
213 EXPECT_TRUE(response_delegate.HasResponse());
214 return response_delegate.GetResponse();
215}
216
[email protected]637bf322011-10-01 20:46:32217} // namespace extension_function_test_utils