blob: 8b33ed1f2d78ada2002abaecdffdd61a789901cf [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:
[email protected]b51f3562012-05-05 22:01:4331 virtual ExtensionWindowController* GetExtensionWindowController()
32 const OVERRIDE {
33 return browser_->extension_window_controller();
[email protected]4e3ce3b2011-10-14 23:25:1734 }
35
[email protected]ea049a02011-12-25 21:37:0936 virtual WebContents* GetAssociatedWebContents() const OVERRIDE {
[email protected]4e3ce3b2011-10-14 23:25:1737 return NULL;
38 }
39
40 Browser* browser_;
41};
42
43} // namespace
44
[email protected]637bf322011-10-01 20:46:3245namespace extension_function_test_utils {
46
47base::Value* ParseJSON(const std::string& data) {
[email protected]cd5785752012-04-11 00:15:4148 return base::JSONReader::Read(data);
[email protected]637bf322011-10-01 20:46:3249}
50
51base::ListValue* ParseList(const std::string& data) {
52 scoped_ptr<base::Value> result(ParseJSON(data));
53 if (result.get() && result->IsType(base::Value::TYPE_LIST))
54 return static_cast<base::ListValue*>(result.release());
55 else
56 return NULL;
57}
58
59base::DictionaryValue* ParseDictionary(
60 const std::string& data) {
61 scoped_ptr<base::Value> result(ParseJSON(data));
62 if (result.get() && result->IsType(base::Value::TYPE_DICTIONARY))
63 return static_cast<base::DictionaryValue*>(result.release());
64 else
65 return NULL;
66}
67
68bool GetBoolean(base::DictionaryValue* val, const std::string& key) {
69 bool result = false;
70 if (!val->GetBoolean(key, &result))
71 ADD_FAILURE() << key << " does not exist or is not a boolean.";
72 return result;
73}
74
75int GetInteger(base::DictionaryValue* val, const std::string& key) {
76 int result = 0;
77 if (!val->GetInteger(key, &result))
78 ADD_FAILURE() << key << " does not exist or is not an integer.";
79 return result;
80}
81
82std::string GetString(base::DictionaryValue* val, const std::string& key) {
83 std::string result;
84 if (!val->GetString(key, &result))
85 ADD_FAILURE() << key << " does not exist or is not a string.";
86 return result;
87}
88
89base::DictionaryValue* ToDictionary(base::Value* val) {
[email protected]8ce80ea62011-10-18 18:00:1890 EXPECT_TRUE(val);
91 EXPECT_EQ(base::Value::TYPE_DICTIONARY, val->GetType());
[email protected]637bf322011-10-01 20:46:3292 return static_cast<base::DictionaryValue*>(val);
93}
94
[email protected]008ff7fb2011-12-19 08:51:1795base::ListValue* ToList(base::Value* val) {
96 EXPECT_TRUE(val);
97 EXPECT_EQ(base::Value::TYPE_LIST, val->GetType());
98 return static_cast<base::ListValue*>(val);
99}
100
[email protected]4e3ce3b2011-10-14 23:25:17101scoped_refptr<Extension> CreateEmptyExtension() {
[email protected]6d2d55b2012-05-05 21:33:43102 return CreateEmptyExtensionWithLocation(Extension::INTERNAL);
103}
104
105scoped_refptr<Extension> CreateEmptyExtensionWithLocation(
106 Extension::Location location) {
[email protected]4e3ce3b2011-10-14 23:25:17107 std::string error;
108 const FilePath test_extension_path;
109 scoped_ptr<base::DictionaryValue> test_extension_value(
110 ParseDictionary("{\"name\": \"Test\", \"version\": \"1.0\"}"));
111 scoped_refptr<Extension> extension(Extension::Create(
112 test_extension_path,
[email protected]6d2d55b2012-05-05 21:33:43113 location,
[email protected]4e3ce3b2011-10-14 23:25:17114 *test_extension_value.get(),
115 Extension::NO_FLAGS,
116 &error));
[email protected]8ce80ea62011-10-18 18:00:18117 EXPECT_TRUE(error.empty()) << "Could not parse test extension " << error;
[email protected]4e3ce3b2011-10-14 23:25:17118 return extension;
119}
120
[email protected]637bf322011-10-01 20:46:32121std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function,
122 const std::string& args,
123 Browser* browser) {
124 return RunFunctionAndReturnError(function, args, browser, NONE);
125}
126std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function,
127 const std::string& args,
128 Browser* browser,
129 RunFunctionFlags flags) {
130 scoped_refptr<ExtensionFunction> function_owner(function);
131 RunFunction(function, args, browser, flags);
[email protected]602542d2012-04-20 02:48:01132 EXPECT_FALSE(function->GetResultValue()) << "Did not expect a result";
[email protected]637bf322011-10-01 20:46:32133 return function->GetError();
134}
135
136base::Value* RunFunctionAndReturnResult(UIThreadExtensionFunction* function,
137 const std::string& args,
138 Browser* browser) {
139 return RunFunctionAndReturnResult(function, args, browser, NONE);
140}
141base::Value* RunFunctionAndReturnResult(UIThreadExtensionFunction* function,
142 const std::string& args,
143 Browser* browser,
144 RunFunctionFlags flags) {
145 scoped_refptr<ExtensionFunction> function_owner(function);
146 RunFunction(function, args, browser, flags);
[email protected]8ce80ea62011-10-18 18:00:18147 EXPECT_TRUE(function->GetError().empty()) << "Unexpected error: "
148 << function->GetError();
[email protected]6b55790a2011-12-15 14:05:05149 return (function->GetResultValue() == NULL) ? NULL :
150 function->GetResultValue()->DeepCopy();
[email protected]637bf322011-10-01 20:46:32151}
152
[email protected]d8c8749b92011-11-16 22:31:32153// This helps us be able to wait until an AsyncExtensionFunction calls
154// SendResponse.
[email protected]bdfc03e2011-11-22 00:20:33155class SendResponseDelegate
156 : public UIThreadExtensionFunction::DelegateForTests {
[email protected]d8c8749b92011-11-16 22:31:32157 public:
158 SendResponseDelegate() : should_post_quit_(false) {}
159
160 virtual ~SendResponseDelegate() {}
161
162 void set_should_post_quit(bool should_quit) {
163 should_post_quit_ = should_quit;
164 }
165
166 bool HasResponse() {
167 return response_.get() != NULL;
168 }
169
170 bool GetResponse() {
171 EXPECT_TRUE(HasResponse());
172 return *response_.get();
173 }
174
[email protected]bdfc03e2011-11-22 00:20:33175 virtual void OnSendResponse(UIThreadExtensionFunction* function,
[email protected]ca6df682012-04-10 23:00:20176 bool success,
177 bool bad_message) {
178 ASSERT_FALSE(bad_message);
[email protected]d8c8749b92011-11-16 22:31:32179 ASSERT_FALSE(HasResponse());
180 response_.reset(new bool);
181 *response_ = success;
182 if (should_post_quit_) {
183 MessageLoopForUI::current()->Quit();
184 }
185 }
186
187 private:
188 scoped_ptr<bool> response_;
189 bool should_post_quit_;
190};
191
[email protected]bdfc03e2011-11-22 00:20:33192bool RunFunction(UIThreadExtensionFunction* function,
193 const std::string& args,
194 Browser* browser,
195 RunFunctionFlags flags) {
[email protected]d8c8749b92011-11-16 22:31:32196 SendResponseDelegate response_delegate;
197 function->set_test_delegate(&response_delegate);
[email protected]bdfc03e2011-11-22 00:20:33198 scoped_ptr<base::ListValue> parsed_args(ParseList(args));
199 EXPECT_TRUE(parsed_args.get()) <<
200 "Could not parse extension function arguments: " << args;
201 function->SetArgs(parsed_args.get());
202
203 TestFunctionDispatcherDelegate dispatcher_delegate(browser);
204 ExtensionFunctionDispatcher dispatcher(
205 browser->profile(), &dispatcher_delegate);
206 function->set_dispatcher(dispatcher.AsWeakPtr());
207
208 function->set_profile(browser->profile());
209 function->set_include_incognito(flags & INCLUDE_INCOGNITO);
210 function->Run();
[email protected]d8c8749b92011-11-16 22:31:32211
212 // If the RunImpl of |function| didn't already call SendResponse, run the
213 // message loop until they do.
214 if (!response_delegate.HasResponse()) {
215 response_delegate.set_should_post_quit(true);
216 ui_test_utils::RunMessageLoop();
217 }
218
219 EXPECT_TRUE(response_delegate.HasResponse());
220 return response_delegate.GetResponse();
221}
222
[email protected]637bf322011-10-01 20:46:32223} // namespace extension_function_test_utils