blob: a87d2e5ec8c4d8f37ab3e21476f1f16d040f7450 [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"
Devlin Croninad230bb2018-05-30 18:41:4615#include "chrome/browser/extensions/browser_extension_window_controller.h"
[email protected]21a40082013-10-28 21:19:2316#include "chrome/browser/profiles/profile.h"
[email protected]637bf322011-10-01 20:46:3217#include "chrome/browser/ui/browser.h"
[email protected]fdd28372014-08-21 02:27:2618#include "components/crx_file/id_util.h"
[email protected]e49e10142014-07-23 06:52:4119#include "extensions/browser/api_test_utils.h"
[email protected]14c3571a2013-11-13 00:18:4420#include "extensions/browser/extension_function.h"
[email protected]0b9de032014-03-15 05:47:0121#include "extensions/browser/extension_function_dispatcher.h"
[email protected]e4452d32013-11-15 23:07:4122#include "extensions/common/extension.h"
[email protected]637bf322011-10-01 20:46:3223#include "testing/gtest/include/gtest/gtest.h"
24
[email protected]ea049a02011-12-25 21:37:0925using content::WebContents;
[email protected]1c321ee2012-05-21 03:02:3426using extensions::Extension;
[email protected]1d5e58b2013-01-31 08:41:4027using extensions::Manifest;
[email protected]37bb5822012-09-10 15:09:5728namespace keys = extensions::tabs_constants;
[email protected]ea049a02011-12-25 21:37:0929
[email protected]4e3ce3b2011-10-14 23:25:1730namespace {
31
32class TestFunctionDispatcherDelegate
[email protected]1a0436892014-04-01 00:38:2533 : public extensions::ExtensionFunctionDispatcher::Delegate {
[email protected]4e3ce3b2011-10-14 23:25:1734 public:
35 explicit TestFunctionDispatcherDelegate(Browser* browser) :
36 browser_(browser) {}
dchengae36a4a2014-10-21 12:36:3637 ~TestFunctionDispatcherDelegate() override {}
[email protected]4e3ce3b2011-10-14 23:25:1738
39 private:
dchengae36a4a2014-10-21 12:36:3640 extensions::WindowController* GetExtensionWindowController() const override {
[email protected]b51f3562012-05-05 22:01:4341 return browser_->extension_window_controller();
[email protected]4e3ce3b2011-10-14 23:25:1742 }
43
dchengae36a4a2014-10-21 12:36:3644 WebContents* GetAssociatedWebContents() const override { return NULL; }
[email protected]4e3ce3b2011-10-14 23:25:1745
46 Browser* browser_;
47};
48
49} // namespace
50
[email protected]637bf322011-10-01 20:46:3251namespace extension_function_test_utils {
52
[email protected]637bf322011-10-01 20:46:3253base::ListValue* ParseList(const std::string& data) {
Lei Zhang582ecd12019-02-13 20:28:5454 std::unique_ptr<base::Value> result = base::JSONReader::ReadDeprecated(data);
Christian Dullweberadb9784c2019-02-07 10:18:1455 if (!result) {
56 ADD_FAILURE() << "Failed to parse: " << data;
57 return nullptr;
58 }
[email protected]e49e10142014-07-23 06:52:4159 base::ListValue* list = NULL;
60 result->GetAsList(&list);
olli.raula4d364162015-09-10 06:39:4061 ignore_result(result.release());
[email protected]e49e10142014-07-23 06:52:4162 return list;
[email protected]637bf322011-10-01 20:46:3263}
64
[email protected]637bf322011-10-01 20:46:3265base::DictionaryValue* ToDictionary(base::Value* val) {
[email protected]8ce80ea62011-10-18 18:00:1866 EXPECT_TRUE(val);
jdoerrie76cee9c2017-10-06 22:42:4267 EXPECT_EQ(base::Value::Type::DICTIONARY, val->type());
[email protected]637bf322011-10-01 20:46:3268 return static_cast<base::DictionaryValue*>(val);
69}
70
[email protected]008ff7fb2011-12-19 08:51:1771base::ListValue* ToList(base::Value* val) {
72 EXPECT_TRUE(val);
jdoerrie76cee9c2017-10-06 22:42:4273 EXPECT_EQ(base::Value::Type::LIST, val->type());
[email protected]008ff7fb2011-12-19 08:51:1774 return static_cast<base::ListValue*>(val);
75}
76
Tim Judkins92389f752019-09-20 21:04:1477bool HasAnyPrivacySensitiveFields(base::DictionaryValue* val) {
[email protected]37bb5822012-09-10 15:09:5778 std::string result;
79 if (val->GetString(keys::kUrlKey, &result) ||
80 val->GetString(keys::kTitleKey, &result) ||
Tim Judkins92389f752019-09-20 21:04:1481 val->GetString(keys::kFaviconUrlKey, &result) ||
82 val->GetString(keys::kPendingUrlKey, &result))
[email protected]37bb5822012-09-10 15:09:5783 return true;
84 return false;
85}
86
Clark DuVallfd4db3d2019-07-30 19:10:4387std::string RunFunctionAndReturnError(ExtensionFunction* function,
[email protected]637bf322011-10-01 20:46:3288 const std::string& args,
89 Browser* browser) {
Devlin Cronin35252c2b2018-02-14 00:03:5390 return RunFunctionAndReturnError(function, args, browser,
91 extensions::api_test_utils::NONE);
[email protected]637bf322011-10-01 20:46:3292}
Devlin Cronin35252c2b2018-02-14 00:03:5393std::string RunFunctionAndReturnError(
Clark DuVallfd4db3d2019-07-30 19:10:4394 ExtensionFunction* function,
Devlin Cronin35252c2b2018-02-14 00:03:5395 const std::string& args,
96 Browser* browser,
97 extensions::api_test_utils::RunFunctionFlags flags) {
[email protected]637bf322011-10-01 20:46:3298 scoped_refptr<ExtensionFunction> function_owner(function);
99 RunFunction(function, args, browser, flags);
rdevlin.cronin187edaa92016-09-19 21:34:02100 // When sending a response, the function will set an empty list value if there
101 // is no specified result.
102 const base::ListValue* results = function->GetResultList();
103 CHECK(results);
104 EXPECT_TRUE(results->empty()) << "Did not expect a result";
105 CHECK(function->response_type());
106 EXPECT_EQ(ExtensionFunction::FAILED, *function->response_type());
[email protected]637bf322011-10-01 20:46:32107 return function->GetError();
108}
109
Clark DuVallfd4db3d2019-07-30 19:10:43110base::Value* RunFunctionAndReturnSingleResult(ExtensionFunction* function,
111 const std::string& args,
112 Browser* browser) {
Devlin Cronin35252c2b2018-02-14 00:03:53113 return RunFunctionAndReturnSingleResult(function, args, browser,
114 extensions::api_test_utils::NONE);
[email protected]637bf322011-10-01 20:46:32115}
[email protected]07ff5fd2012-07-12 22:39:09116base::Value* RunFunctionAndReturnSingleResult(
Clark DuVallfd4db3d2019-07-30 19:10:43117 ExtensionFunction* function,
[email protected]07ff5fd2012-07-12 22:39:09118 const std::string& args,
119 Browser* browser,
Devlin Cronin35252c2b2018-02-14 00:03:53120 extensions::api_test_utils::RunFunctionFlags flags) {
[email protected]637bf322011-10-01 20:46:32121 scoped_refptr<ExtensionFunction> function_owner(function);
122 RunFunction(function, args, browser, flags);
[email protected]8ce80ea62011-10-18 18:00:18123 EXPECT_TRUE(function->GetError().empty()) << "Unexpected error: "
124 << function->GetError();
[email protected]5d30f92bf2012-08-03 08:43:37125 const base::Value* single_result = NULL;
[email protected]07ff5fd2012-07-12 22:39:09126 if (function->GetResultList() != NULL &&
127 function->GetResultList()->Get(0, &single_result)) {
128 return single_result->DeepCopy();
129 }
130 return NULL;
[email protected]637bf322011-10-01 20:46:32131}
132
Clark DuVallfd4db3d2019-07-30 19:10:43133bool RunFunction(ExtensionFunction* function,
[email protected]bdfc03e2011-11-22 00:20:33134 const std::string& args,
135 Browser* browser,
Devlin Cronin35252c2b2018-02-14 00:03:53136 extensions::api_test_utils::RunFunctionFlags flags) {
dchengc963c7142016-04-08 03:55:22137 std::unique_ptr<base::ListValue> parsed_args(ParseList(args));
[email protected]fc672e12014-08-16 08:16:15138 EXPECT_TRUE(parsed_args.get())
139 << "Could not parse extension function arguments: " << args;
dcheng1fc00f12015-12-26 22:18:03140 return RunFunction(function, std::move(parsed_args), browser, flags);
[email protected]fc672e12014-08-16 08:16:15141}
142
Clark DuVallfd4db3d2019-07-30 19:10:43143bool RunFunction(ExtensionFunction* function,
dchengc963c7142016-04-08 03:55:22144 std::unique_ptr<base::ListValue> args,
[email protected]fc672e12014-08-16 08:16:15145 Browser* browser,
Devlin Cronin35252c2b2018-02-14 00:03:53146 extensions::api_test_utils::RunFunctionFlags flags) {
[email protected]bdfc03e2011-11-22 00:20:33147 TestFunctionDispatcherDelegate dispatcher_delegate(browser);
dchengc963c7142016-04-08 03:55:22148 std::unique_ptr<extensions::ExtensionFunctionDispatcher> dispatcher(
rdevlin.cronincb2ec659a2015-06-10 23:32:41149 new extensions::ExtensionFunctionDispatcher(browser->profile()));
150 dispatcher->set_delegate(&dispatcher_delegate);
Devlin Cronin35252c2b2018-02-14 00:03:53151 return extensions::api_test_utils::RunFunction(function, std::move(args),
152 browser->profile(),
153 std::move(dispatcher), flags);
[email protected]d8c8749b92011-11-16 22:31:32154}
155
[email protected]637bf322011-10-01 20:46:32156} // namespace extension_function_test_utils