blob: f7ef1c350cd5b54dbf2b364201d5014cb6be7b99 [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);
jdoerrie76cee9c2017-10-06 22:42:4262 EXPECT_EQ(base::Value::Type::DICTIONARY, val->type());
[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);
jdoerrie76cee9c2017-10-06 22:42:4268 EXPECT_EQ(base::Value::Type::LIST, val->type());
[email protected]008ff7fb2011-12-19 08:51:1769 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) {
Devlin Cronin35252c2b2018-02-14 00:03:5384 return RunFunctionAndReturnError(function, args, browser,
85 extensions::api_test_utils::NONE);
[email protected]637bf322011-10-01 20:46:3286}
Devlin Cronin35252c2b2018-02-14 00:03:5387std::string RunFunctionAndReturnError(
88 UIThreadExtensionFunction* function,
89 const std::string& args,
90 Browser* browser,
91 extensions::api_test_utils::RunFunctionFlags flags) {
[email protected]637bf322011-10-01 20:46:3292 scoped_refptr<ExtensionFunction> function_owner(function);
93 RunFunction(function, args, browser, flags);
rdevlin.cronin187edaa92016-09-19 21:34:0294 // When sending a response, the function will set an empty list value if there
95 // is no specified result.
96 const base::ListValue* results = function->GetResultList();
97 CHECK(results);
98 EXPECT_TRUE(results->empty()) << "Did not expect a result";
99 CHECK(function->response_type());
100 EXPECT_EQ(ExtensionFunction::FAILED, *function->response_type());
[email protected]637bf322011-10-01 20:46:32101 return function->GetError();
102}
103
[email protected]07ff5fd2012-07-12 22:39:09104base::Value* RunFunctionAndReturnSingleResult(
105 UIThreadExtensionFunction* function,
106 const std::string& args,
107 Browser* browser) {
Devlin Cronin35252c2b2018-02-14 00:03:53108 return RunFunctionAndReturnSingleResult(function, args, browser,
109 extensions::api_test_utils::NONE);
[email protected]637bf322011-10-01 20:46:32110}
[email protected]07ff5fd2012-07-12 22:39:09111base::Value* RunFunctionAndReturnSingleResult(
112 UIThreadExtensionFunction* function,
113 const std::string& args,
114 Browser* browser,
Devlin Cronin35252c2b2018-02-14 00:03:53115 extensions::api_test_utils::RunFunctionFlags flags) {
[email protected]637bf322011-10-01 20:46:32116 scoped_refptr<ExtensionFunction> function_owner(function);
117 RunFunction(function, args, browser, flags);
[email protected]8ce80ea62011-10-18 18:00:18118 EXPECT_TRUE(function->GetError().empty()) << "Unexpected error: "
119 << function->GetError();
[email protected]5d30f92bf2012-08-03 08:43:37120 const base::Value* single_result = NULL;
[email protected]07ff5fd2012-07-12 22:39:09121 if (function->GetResultList() != NULL &&
122 function->GetResultList()->Get(0, &single_result)) {
123 return single_result->DeepCopy();
124 }
125 return NULL;
[email protected]637bf322011-10-01 20:46:32126}
127
[email protected]bdfc03e2011-11-22 00:20:33128bool RunFunction(UIThreadExtensionFunction* function,
129 const std::string& args,
130 Browser* browser,
Devlin Cronin35252c2b2018-02-14 00:03:53131 extensions::api_test_utils::RunFunctionFlags flags) {
dchengc963c7142016-04-08 03:55:22132 std::unique_ptr<base::ListValue> parsed_args(ParseList(args));
[email protected]fc672e12014-08-16 08:16:15133 EXPECT_TRUE(parsed_args.get())
134 << "Could not parse extension function arguments: " << args;
dcheng1fc00f12015-12-26 22:18:03135 return RunFunction(function, std::move(parsed_args), browser, flags);
[email protected]fc672e12014-08-16 08:16:15136}
137
138bool RunFunction(UIThreadExtensionFunction* function,
dchengc963c7142016-04-08 03:55:22139 std::unique_ptr<base::ListValue> args,
[email protected]fc672e12014-08-16 08:16:15140 Browser* browser,
Devlin Cronin35252c2b2018-02-14 00:03:53141 extensions::api_test_utils::RunFunctionFlags flags) {
[email protected]bdfc03e2011-11-22 00:20:33142 TestFunctionDispatcherDelegate dispatcher_delegate(browser);
dchengc963c7142016-04-08 03:55:22143 std::unique_ptr<extensions::ExtensionFunctionDispatcher> dispatcher(
rdevlin.cronincb2ec659a2015-06-10 23:32:41144 new extensions::ExtensionFunctionDispatcher(browser->profile()));
145 dispatcher->set_delegate(&dispatcher_delegate);
Devlin Cronin35252c2b2018-02-14 00:03:53146 return extensions::api_test_utils::RunFunction(function, std::move(args),
147 browser->profile(),
148 std::move(dispatcher), flags);
[email protected]d8c8749b92011-11-16 22:31:32149}
150
[email protected]637bf322011-10-01 20:46:32151} // namespace extension_function_test_utils