blob: 889f6beb7909b24eaf51f6e1b5fb8fb691dee42a [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) {
47 const bool kAllowTrailingComma = false;
48 return base::JSONReader::Read(data, kAllowTrailingComma);
49}
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() {
102 std::string error;
103 const FilePath test_extension_path;
104 scoped_ptr<base::DictionaryValue> test_extension_value(
105 ParseDictionary("{\"name\": \"Test\", \"version\": \"1.0\"}"));
106 scoped_refptr<Extension> extension(Extension::Create(
107 test_extension_path,
108 Extension::INTERNAL,
109 *test_extension_value.get(),
110 Extension::NO_FLAGS,
111 &error));
[email protected]8ce80ea62011-10-18 18:00:18112 EXPECT_TRUE(error.empty()) << "Could not parse test extension " << error;
[email protected]4e3ce3b2011-10-14 23:25:17113 return extension;
114}
115
[email protected]637bf322011-10-01 20:46:32116std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function,
117 const std::string& args,
118 Browser* browser) {
119 return RunFunctionAndReturnError(function, args, browser, NONE);
120}
121std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function,
122 const std::string& args,
123 Browser* browser,
124 RunFunctionFlags flags) {
125 scoped_refptr<ExtensionFunction> function_owner(function);
126 RunFunction(function, args, browser, flags);
[email protected]8ce80ea62011-10-18 18:00:18127 EXPECT_FALSE(function->GetResultValue()) << "Unexpected function result " <<
128 function->GetResult();
[email protected]637bf322011-10-01 20:46:32129 return function->GetError();
130}
131
132base::Value* RunFunctionAndReturnResult(UIThreadExtensionFunction* function,
133 const std::string& args,
134 Browser* browser) {
135 return RunFunctionAndReturnResult(function, args, browser, NONE);
136}
137base::Value* RunFunctionAndReturnResult(UIThreadExtensionFunction* function,
138 const std::string& args,
139 Browser* browser,
140 RunFunctionFlags flags) {
141 scoped_refptr<ExtensionFunction> function_owner(function);
142 RunFunction(function, args, browser, flags);
[email protected]8ce80ea62011-10-18 18:00:18143 EXPECT_TRUE(function->GetError().empty()) << "Unexpected error: "
144 << function->GetError();
[email protected]6b55790a2011-12-15 14:05:05145 return (function->GetResultValue() == NULL) ? NULL :
146 function->GetResultValue()->DeepCopy();
[email protected]637bf322011-10-01 20:46:32147}
148
[email protected]d8c8749b92011-11-16 22:31:32149// This helps us be able to wait until an AsyncExtensionFunction calls
150// SendResponse.
[email protected]bdfc03e2011-11-22 00:20:33151class SendResponseDelegate
152 : public UIThreadExtensionFunction::DelegateForTests {
[email protected]d8c8749b92011-11-16 22:31:32153 public:
154 SendResponseDelegate() : should_post_quit_(false) {}
155
156 virtual ~SendResponseDelegate() {}
157
158 void set_should_post_quit(bool should_quit) {
159 should_post_quit_ = should_quit;
160 }
161
162 bool HasResponse() {
163 return response_.get() != NULL;
164 }
165
166 bool GetResponse() {
167 EXPECT_TRUE(HasResponse());
168 return *response_.get();
169 }
170
[email protected]bdfc03e2011-11-22 00:20:33171 virtual void OnSendResponse(UIThreadExtensionFunction* function,
[email protected]ca6df682012-04-10 23:00:20172 bool success,
173 bool bad_message) {
174 ASSERT_FALSE(bad_message);
[email protected]d8c8749b92011-11-16 22:31:32175 ASSERT_FALSE(HasResponse());
176 response_.reset(new bool);
177 *response_ = success;
178 if (should_post_quit_) {
179 MessageLoopForUI::current()->Quit();
180 }
181 }
182
183 private:
184 scoped_ptr<bool> response_;
185 bool should_post_quit_;
186};
187
[email protected]bdfc03e2011-11-22 00:20:33188bool RunFunction(UIThreadExtensionFunction* function,
189 const std::string& args,
190 Browser* browser,
191 RunFunctionFlags flags) {
[email protected]d8c8749b92011-11-16 22:31:32192 SendResponseDelegate response_delegate;
193 function->set_test_delegate(&response_delegate);
[email protected]bdfc03e2011-11-22 00:20:33194 scoped_ptr<base::ListValue> parsed_args(ParseList(args));
195 EXPECT_TRUE(parsed_args.get()) <<
196 "Could not parse extension function arguments: " << args;
197 function->SetArgs(parsed_args.get());
198
199 TestFunctionDispatcherDelegate dispatcher_delegate(browser);
200 ExtensionFunctionDispatcher dispatcher(
201 browser->profile(), &dispatcher_delegate);
202 function->set_dispatcher(dispatcher.AsWeakPtr());
203
204 function->set_profile(browser->profile());
205 function->set_include_incognito(flags & INCLUDE_INCOGNITO);
206 function->Run();
[email protected]d8c8749b92011-11-16 22:31:32207
208 // If the RunImpl of |function| didn't already call SendResponse, run the
209 // message loop until they do.
210 if (!response_delegate.HasResponse()) {
211 response_delegate.set_should_post_quit(true);
212 ui_test_utils::RunMessageLoop();
213 }
214
215 EXPECT_TRUE(response_delegate.HasResponse());
216 return response_delegate.GetResponse();
217}
218
[email protected]637bf322011-10-01 20:46:32219} // namespace extension_function_test_utils