Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / extension_function_test_utils.cc
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // 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
9 #include "base/files/file_path.h"
10 #include "base/json/json_reader.h"
11 #include "base/values.h"
12 #include "chrome/browser/extensions/api/tabs/tabs_constants.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/ui/browser.h"
15 #include "components/crx_file/id_util.h"
16 #include "extensions/browser/api_test_utils.h"
17 #include "extensions/browser/extension_function.h"
18 #include "extensions/browser/extension_function_dispatcher.h"
19 #include "extensions/common/extension.h"
20 #include "testing/gtest/include/gtest/gtest.h"
21
22 using content::WebContents;
23 using extensions::Extension;
24 using extensions::Manifest;
25 namespace keys = extensions::tabs_constants;
26
27 namespace {
28
29 class TestFunctionDispatcherDelegate
30     : public extensions::ExtensionFunctionDispatcher::Delegate {
31  public:
32   explicit TestFunctionDispatcherDelegate(Browser* browser) :
33       browser_(browser) {}
34   ~TestFunctionDispatcherDelegate() override {}
35
36  private:
37   extensions::WindowController* GetExtensionWindowController() const override {
38     return browser_->extension_window_controller();
39   }
40
41   WebContents* GetAssociatedWebContents() const override { return NULL; }
42
43   Browser* browser_;
44 };
45
46 }  // namespace
47
48 namespace extension_function_test_utils {
49
50 base::Value* ParseJSON(const std::string& data) {
51   return base::JSONReader::Read(data);
52 }
53
54 base::ListValue* ParseList(const std::string& data) {
55   base::Value* result = ParseJSON(data);
56   base::ListValue* list = NULL;
57   result->GetAsList(&list);
58   return list;
59 }
60
61 base::DictionaryValue* ParseDictionary(
62     const std::string& data) {
63   base::Value* result = ParseJSON(data);
64   base::DictionaryValue* dict = NULL;
65   result->GetAsDictionary(&dict);
66   return dict;
67 }
68
69 bool GetBoolean(const base::DictionaryValue* val, const std::string& key) {
70   bool result = false;
71   if (!val->GetBoolean(key, &result))
72       ADD_FAILURE() << key << " does not exist or is not a boolean.";
73   return result;
74 }
75
76 int GetInteger(const base::DictionaryValue* val, const std::string& key) {
77   int result = 0;
78   if (!val->GetInteger(key, &result))
79     ADD_FAILURE() << key << " does not exist or is not an integer.";
80   return result;
81 }
82
83 std::string GetString(const base::DictionaryValue* val,
84                       const std::string& key) {
85   std::string result;
86   if (!val->GetString(key, &result))
87     ADD_FAILURE() << key << " does not exist or is not a string.";
88   return result;
89 }
90
91 base::DictionaryValue* ToDictionary(base::Value* val) {
92   EXPECT_TRUE(val);
93   EXPECT_EQ(base::Value::TYPE_DICTIONARY, val->GetType());
94   return static_cast<base::DictionaryValue*>(val);
95 }
96
97 base::ListValue* ToList(base::Value* val) {
98   EXPECT_TRUE(val);
99   EXPECT_EQ(base::Value::TYPE_LIST, val->GetType());
100   return static_cast<base::ListValue*>(val);
101 }
102
103 scoped_refptr<Extension> CreateEmptyExtensionWithLocation(
104     Manifest::Location location) {
105   scoped_ptr<base::DictionaryValue> test_extension_value(
106       ParseDictionary("{\"name\": \"Test\", \"version\": \"1.0\"}"));
107   return CreateExtension(location, test_extension_value.get(), std::string());
108 }
109
110 scoped_refptr<Extension> CreateExtension(
111     base::DictionaryValue* test_extension_value) {
112   return CreateExtension(Manifest::INTERNAL, test_extension_value,
113                          std::string());
114 }
115
116 scoped_refptr<Extension> CreateExtension(
117     Manifest::Location location,
118     base::DictionaryValue* test_extension_value,
119     const std::string& id_input) {
120   std::string error;
121   const base::FilePath test_extension_path;
122   std::string id;
123   if (!id_input.empty())
124     id = crx_file::id_util::GenerateId(id_input);
125   scoped_refptr<Extension> extension(Extension::Create(
126       test_extension_path,
127       location,
128       *test_extension_value,
129       Extension::NO_FLAGS,
130       id,
131       &error));
132   EXPECT_TRUE(error.empty()) << "Could not parse test extension " << error;
133   return extension;
134 }
135
136 bool HasPrivacySensitiveFields(base::DictionaryValue* val) {
137   std::string result;
138   if (val->GetString(keys::kUrlKey, &result) ||
139       val->GetString(keys::kTitleKey, &result) ||
140       val->GetString(keys::kFaviconUrlKey, &result))
141     return true;
142   return false;
143 }
144
145 std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function,
146                                       const std::string& args,
147                                       Browser* browser) {
148   return RunFunctionAndReturnError(function, args, browser, NONE);
149 }
150 std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function,
151                                       const std::string& args,
152                                       Browser* browser,
153                                       RunFunctionFlags flags) {
154   scoped_refptr<ExtensionFunction> function_owner(function);
155   // Without a callback the function will not generate a result.
156   function->set_has_callback(true);
157   RunFunction(function, args, browser, flags);
158   EXPECT_FALSE(function->GetResultList()) << "Did not expect a result";
159   return function->GetError();
160 }
161
162 base::Value* RunFunctionAndReturnSingleResult(
163     UIThreadExtensionFunction* function,
164     const std::string& args,
165     Browser* browser) {
166   return RunFunctionAndReturnSingleResult(function, args, browser, NONE);
167 }
168 base::Value* RunFunctionAndReturnSingleResult(
169     UIThreadExtensionFunction* function,
170     const std::string& args,
171     Browser* browser,
172     RunFunctionFlags flags) {
173   scoped_refptr<ExtensionFunction> function_owner(function);
174   // Without a callback the function will not generate a result.
175   function->set_has_callback(true);
176   RunFunction(function, args, browser, flags);
177   EXPECT_TRUE(function->GetError().empty()) << "Unexpected error: "
178       << function->GetError();
179   const base::Value* single_result = NULL;
180   if (function->GetResultList() != NULL &&
181       function->GetResultList()->Get(0, &single_result)) {
182     return single_result->DeepCopy();
183   }
184   return NULL;
185 }
186
187 // This helps us be able to wait until an UIThreadExtensionFunction calls
188 // SendResponse.
189 class SendResponseDelegate
190     : public UIThreadExtensionFunction::DelegateForTests {
191  public:
192   SendResponseDelegate() : should_post_quit_(false) {}
193
194   virtual ~SendResponseDelegate() {}
195
196   void set_should_post_quit(bool should_quit) {
197     should_post_quit_ = should_quit;
198   }
199
200   bool HasResponse() {
201     return response_.get() != NULL;
202   }
203
204   bool GetResponse() {
205     EXPECT_TRUE(HasResponse());
206     return *response_.get();
207   }
208
209   void OnSendResponse(UIThreadExtensionFunction* function,
210                       bool success,
211                       bool bad_message) override {
212     ASSERT_FALSE(bad_message);
213     ASSERT_FALSE(HasResponse());
214     response_.reset(new bool);
215     *response_ = success;
216     if (should_post_quit_) {
217       base::MessageLoopForUI::current()->Quit();
218     }
219   }
220
221  private:
222   scoped_ptr<bool> response_;
223   bool should_post_quit_;
224 };
225
226 bool RunFunction(UIThreadExtensionFunction* function,
227                  const std::string& args,
228                  Browser* browser,
229                  RunFunctionFlags flags) {
230   scoped_ptr<base::ListValue> parsed_args(ParseList(args));
231   EXPECT_TRUE(parsed_args.get())
232       << "Could not parse extension function arguments: " << args;
233   return RunFunction(function, parsed_args.Pass(), browser, flags);
234 }
235
236 bool RunFunction(UIThreadExtensionFunction* function,
237                  scoped_ptr<base::ListValue> args,
238                  Browser* browser,
239                  RunFunctionFlags flags) {
240   TestFunctionDispatcherDelegate dispatcher_delegate(browser);
241   scoped_ptr<extensions::ExtensionFunctionDispatcher> dispatcher(
242       new extensions::ExtensionFunctionDispatcher(browser->profile(),
243                                                   &dispatcher_delegate));
244   // TODO(yoz): The cast is a hack; these flags should be defined in
245   // only one place.  See crbug.com/394840.
246   return extensions::api_test_utils::RunFunction(
247       function,
248       args.Pass(),
249       browser->profile(),
250       dispatcher.Pass(),
251       static_cast<extensions::api_test_utils::RunFunctionFlags>(flags));
252 }
253
254 } // namespace extension_function_test_utils