blob: 35989dccbe01a06233816411bcf54d3e9769729d [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]8ce80ea62011-10-18 18:00:18126 EXPECT_FALSE(function->GetResultValue()) << "Unexpected function result " <<
127 function->GetResult();
[email protected]637bf322011-10-01 20:46:32128 return function->GetError();
129}
130
131base::Value* RunFunctionAndReturnResult(UIThreadExtensionFunction* function,
132 const std::string& args,
133 Browser* browser) {
134 return RunFunctionAndReturnResult(function, args, browser, NONE);
135}
136base::Value* RunFunctionAndReturnResult(UIThreadExtensionFunction* function,
137 const std::string& args,
138 Browser* browser,
139 RunFunctionFlags flags) {
140 scoped_refptr<ExtensionFunction> function_owner(function);
141 RunFunction(function, args, browser, flags);
[email protected]8ce80ea62011-10-18 18:00:18142 EXPECT_TRUE(function->GetError().empty()) << "Unexpected error: "
143 << function->GetError();
[email protected]6b55790a2011-12-15 14:05:05144 return (function->GetResultValue() == NULL) ? NULL :
145 function->GetResultValue()->DeepCopy();
[email protected]637bf322011-10-01 20:46:32146}
147
[email protected]d8c8749b92011-11-16 22:31:32148// This helps us be able to wait until an AsyncExtensionFunction calls
149// SendResponse.
[email protected]bdfc03e2011-11-22 00:20:33150class SendResponseDelegate
151 : public UIThreadExtensionFunction::DelegateForTests {
[email protected]d8c8749b92011-11-16 22:31:32152 public:
153 SendResponseDelegate() : should_post_quit_(false) {}
154
155 virtual ~SendResponseDelegate() {}
156
157 void set_should_post_quit(bool should_quit) {
158 should_post_quit_ = should_quit;
159 }
160
161 bool HasResponse() {
162 return response_.get() != NULL;
163 }
164
165 bool GetResponse() {
166 EXPECT_TRUE(HasResponse());
167 return *response_.get();
168 }
169
[email protected]bdfc03e2011-11-22 00:20:33170 virtual void OnSendResponse(UIThreadExtensionFunction* function,
[email protected]ca6df682012-04-10 23:00:20171 bool success,
172 bool bad_message) {
173 ASSERT_FALSE(bad_message);
[email protected]d8c8749b92011-11-16 22:31:32174 ASSERT_FALSE(HasResponse());
175 response_.reset(new bool);
176 *response_ = success;
177 if (should_post_quit_) {
178 MessageLoopForUI::current()->Quit();
179 }
180 }
181
182 private:
183 scoped_ptr<bool> response_;
184 bool should_post_quit_;
185};
186
[email protected]bdfc03e2011-11-22 00:20:33187bool RunFunction(UIThreadExtensionFunction* function,
188 const std::string& args,
189 Browser* browser,
190 RunFunctionFlags flags) {
[email protected]d8c8749b92011-11-16 22:31:32191 SendResponseDelegate response_delegate;
192 function->set_test_delegate(&response_delegate);
[email protected]bdfc03e2011-11-22 00:20:33193 scoped_ptr<base::ListValue> parsed_args(ParseList(args));
194 EXPECT_TRUE(parsed_args.get()) <<
195 "Could not parse extension function arguments: " << args;
196 function->SetArgs(parsed_args.get());
197
198 TestFunctionDispatcherDelegate dispatcher_delegate(browser);
199 ExtensionFunctionDispatcher dispatcher(
200 browser->profile(), &dispatcher_delegate);
201 function->set_dispatcher(dispatcher.AsWeakPtr());
202
203 function->set_profile(browser->profile());
204 function->set_include_incognito(flags & INCLUDE_INCOGNITO);
205 function->Run();
[email protected]d8c8749b92011-11-16 22:31:32206
207 // If the RunImpl of |function| didn't already call SendResponse, run the
208 // message loop until they do.
209 if (!response_delegate.HasResponse()) {
210 response_delegate.set_should_post_quit(true);
211 ui_test_utils::RunMessageLoop();
212 }
213
214 EXPECT_TRUE(response_delegate.HasResponse());
215 return response_delegate.GetResponse();
216}
217
[email protected]637bf322011-10-01 20:46:32218} // namespace extension_function_test_utils