blob: 332dd99031c265f7d5d4aac368c2f8e68c72f15d [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>
dcheng1fc00f12015-12-26 22:18:038#include <utility>
[email protected]637bf322011-10-01 20:46:329
[email protected]57999812013-02-24 05:40:5210#include "base/files/file_path.h"
[email protected]637bf322011-10-01 20:46:3211#include "base/json/json_reader.h"
olli.raula4d364162015-09-10 06:39:4012#include "base/macros.h"
[email protected]637bf322011-10-01 20:46:3213#include "base/values.h"
[email protected]37bb5822012-09-10 15:09:5714#include "chrome/browser/extensions/api/tabs/tabs_constants.h"
[email protected]21a40082013-10-28 21:19:2315#include "chrome/browser/profiles/profile.h"
[email protected]637bf322011-10-01 20:46:3216#include "chrome/browser/ui/browser.h"
[email protected]fdd28372014-08-21 02:27:2617#include "components/crx_file/id_util.h"
[email protected]e49e10142014-07-23 06:52:4118#include "extensions/browser/api_test_utils.h"
[email protected]14c3571a2013-11-13 00:18:4419#include "extensions/browser/extension_function.h"
[email protected]0b9de032014-03-15 05:47:0120#include "extensions/browser/extension_function_dispatcher.h"
[email protected]e4452d32013-11-15 23:07:4121#include "extensions/common/extension.h"
[email protected]637bf322011-10-01 20:46:3222#include "testing/gtest/include/gtest/gtest.h"
23
[email protected]ea049a02011-12-25 21:37:0924using content::WebContents;
[email protected]1c321ee2012-05-21 03:02:3425using extensions::Extension;
[email protected]1d5e58b2013-01-31 08:41:4026using extensions::Manifest;
[email protected]37bb5822012-09-10 15:09:5727namespace keys = extensions::tabs_constants;
[email protected]ea049a02011-12-25 21:37:0928
[email protected]4e3ce3b2011-10-14 23:25:1729namespace {
30
31class TestFunctionDispatcherDelegate
[email protected]1a0436892014-04-01 00:38:2532 : public extensions::ExtensionFunctionDispatcher::Delegate {
[email protected]4e3ce3b2011-10-14 23:25:1733 public:
34 explicit TestFunctionDispatcherDelegate(Browser* browser) :
35 browser_(browser) {}
dchengae36a4a2014-10-21 12:36:3636 ~TestFunctionDispatcherDelegate() override {}
[email protected]4e3ce3b2011-10-14 23:25:1737
38 private:
dchengae36a4a2014-10-21 12:36:3639 extensions::WindowController* GetExtensionWindowController() const override {
[email protected]b51f3562012-05-05 22:01:4340 return browser_->extension_window_controller();
[email protected]4e3ce3b2011-10-14 23:25:1741 }
42
dchengae36a4a2014-10-21 12:36:3643 WebContents* GetAssociatedWebContents() const override { return NULL; }
[email protected]4e3ce3b2011-10-14 23:25:1744
45 Browser* browser_;
46};
47
48} // namespace
49
[email protected]637bf322011-10-01 20:46:3250namespace extension_function_test_utils {
51
[email protected]637bf322011-10-01 20:46:3252base::ListValue* ParseList(const std::string& data) {
dchengc963c7142016-04-08 03:55:2253 std::unique_ptr<base::Value> result = base::JSONReader::Read(data);
[email protected]e49e10142014-07-23 06:52:4154 base::ListValue* list = NULL;
55 result->GetAsList(&list);
olli.raula4d364162015-09-10 06:39:4056 ignore_result(result.release());
[email protected]e49e10142014-07-23 06:52:4157 return list;
[email protected]637bf322011-10-01 20:46:3258}
59
[email protected]637bf322011-10-01 20:46:3260base::DictionaryValue* ToDictionary(base::Value* val) {
[email protected]8ce80ea62011-10-18 18:00:1861 EXPECT_TRUE(val);
62 EXPECT_EQ(base::Value::TYPE_DICTIONARY, val->GetType());
[email protected]637bf322011-10-01 20:46:3263 return static_cast<base::DictionaryValue*>(val);
64}
65
[email protected]008ff7fb2011-12-19 08:51:1766base::ListValue* ToList(base::Value* val) {
67 EXPECT_TRUE(val);
68 EXPECT_EQ(base::Value::TYPE_LIST, val->GetType());
69 return static_cast<base::ListValue*>(val);
70}
71
[email protected]37bb5822012-09-10 15:09:5772bool HasPrivacySensitiveFields(base::DictionaryValue* val) {
73 std::string result;
74 if (val->GetString(keys::kUrlKey, &result) ||
75 val->GetString(keys::kTitleKey, &result) ||
76 val->GetString(keys::kFaviconUrlKey, &result))
77 return true;
78 return false;
79}
80
[email protected]637bf322011-10-01 20:46:3281std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function,
82 const std::string& args,
83 Browser* browser) {
84 return RunFunctionAndReturnError(function, args, browser, NONE);
85}
86std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function,
87 const std::string& args,
88 Browser* browser,
89 RunFunctionFlags flags) {
90 scoped_refptr<ExtensionFunction> function_owner(function);
91 RunFunction(function, args, browser, flags);
rdevlin.cronin187edaa92016-09-19 21:34:0292 // When sending a response, the function will set an empty list value if there
93 // is no specified result.
94 const base::ListValue* results = function->GetResultList();
95 CHECK(results);
96 EXPECT_TRUE(results->empty()) << "Did not expect a result";
97 CHECK(function->response_type());
98 EXPECT_EQ(ExtensionFunction::FAILED, *function->response_type());
[email protected]637bf322011-10-01 20:46:3299 return function->GetError();
100}
101
[email protected]07ff5fd2012-07-12 22:39:09102base::Value* RunFunctionAndReturnSingleResult(
103 UIThreadExtensionFunction* function,
104 const std::string& args,
105 Browser* browser) {
106 return RunFunctionAndReturnSingleResult(function, args, browser, NONE);
[email protected]637bf322011-10-01 20:46:32107}
[email protected]07ff5fd2012-07-12 22:39:09108base::Value* RunFunctionAndReturnSingleResult(
109 UIThreadExtensionFunction* function,
110 const std::string& args,
111 Browser* browser,
112 RunFunctionFlags flags) {
[email protected]637bf322011-10-01 20:46:32113 scoped_refptr<ExtensionFunction> function_owner(function);
114 RunFunction(function, args, browser, flags);
[email protected]8ce80ea62011-10-18 18:00:18115 EXPECT_TRUE(function->GetError().empty()) << "Unexpected error: "
116 << function->GetError();
[email protected]5d30f92bf2012-08-03 08:43:37117 const base::Value* single_result = NULL;
[email protected]07ff5fd2012-07-12 22:39:09118 if (function->GetResultList() != NULL &&
119 function->GetResultList()->Get(0, &single_result)) {
120 return single_result->DeepCopy();
121 }
122 return NULL;
[email protected]637bf322011-10-01 20:46:32123}
124
[email protected]bdfc03e2011-11-22 00:20:33125bool RunFunction(UIThreadExtensionFunction* function,
126 const std::string& args,
127 Browser* browser,
128 RunFunctionFlags flags) {
dchengc963c7142016-04-08 03:55:22129 std::unique_ptr<base::ListValue> parsed_args(ParseList(args));
[email protected]fc672e12014-08-16 08:16:15130 EXPECT_TRUE(parsed_args.get())
131 << "Could not parse extension function arguments: " << args;
dcheng1fc00f12015-12-26 22:18:03132 return RunFunction(function, std::move(parsed_args), browser, flags);
[email protected]fc672e12014-08-16 08:16:15133}
134
135bool RunFunction(UIThreadExtensionFunction* function,
dchengc963c7142016-04-08 03:55:22136 std::unique_ptr<base::ListValue> args,
[email protected]fc672e12014-08-16 08:16:15137 Browser* browser,
138 RunFunctionFlags flags) {
[email protected]bdfc03e2011-11-22 00:20:33139 TestFunctionDispatcherDelegate dispatcher_delegate(browser);
dchengc963c7142016-04-08 03:55:22140 std::unique_ptr<extensions::ExtensionFunctionDispatcher> dispatcher(
rdevlin.cronincb2ec659a2015-06-10 23:32:41141 new extensions::ExtensionFunctionDispatcher(browser->profile()));
142 dispatcher->set_delegate(&dispatcher_delegate);
[email protected]e49e10142014-07-23 06:52:41143 // TODO(yoz): The cast is a hack; these flags should be defined in
144 // only one place. See crbug.com/394840.
145 return extensions::api_test_utils::RunFunction(
dcheng1fc00f12015-12-26 22:18:03146 function, std::move(args), browser->profile(), std::move(dispatcher),
[email protected]e49e10142014-07-23 06:52:41147 static_cast<extensions::api_test_utils::RunFunctionFlags>(flags));
[email protected]d8c8749b92011-11-16 22:31:32148}
149
[email protected]637bf322011-10-01 20:46:32150} // namespace extension_function_test_utils