[email protected] | 8809f144 | 2012-01-20 21:21:47 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 637bf32 | 2011-10-01 20:46:32 | [diff] [blame] | 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> |
dcheng | 1fc00f1 | 2015-12-26 22:18:03 | [diff] [blame] | 8 | #include <utility> |
[email protected] | 637bf32 | 2011-10-01 20:46:32 | [diff] [blame] | 9 | |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 10 | #include "base/files/file_path.h" |
[email protected] | 637bf32 | 2011-10-01 20:46:32 | [diff] [blame] | 11 | #include "base/json/json_reader.h" |
olli.raula | 4d36416 | 2015-09-10 06:39:40 | [diff] [blame] | 12 | #include "base/macros.h" |
[email protected] | 637bf32 | 2011-10-01 20:46:32 | [diff] [blame] | 13 | #include "base/values.h" |
[email protected] | 37bb582 | 2012-09-10 15:09:57 | [diff] [blame] | 14 | #include "chrome/browser/extensions/api/tabs/tabs_constants.h" |
Devlin Cronin | ad230bb | 2018-05-30 18:41:46 | [diff] [blame] | 15 | #include "chrome/browser/extensions/browser_extension_window_controller.h" |
[email protected] | 21a4008 | 2013-10-28 21:19:23 | [diff] [blame] | 16 | #include "chrome/browser/profiles/profile.h" |
[email protected] | 637bf32 | 2011-10-01 20:46:32 | [diff] [blame] | 17 | #include "chrome/browser/ui/browser.h" |
[email protected] | fdd2837 | 2014-08-21 02:27:26 | [diff] [blame] | 18 | #include "components/crx_file/id_util.h" |
[email protected] | e49e1014 | 2014-07-23 06:52:41 | [diff] [blame] | 19 | #include "extensions/browser/api_test_utils.h" |
[email protected] | 14c3571a | 2013-11-13 00:18:44 | [diff] [blame] | 20 | #include "extensions/browser/extension_function.h" |
[email protected] | 0b9de03 | 2014-03-15 05:47:01 | [diff] [blame] | 21 | #include "extensions/browser/extension_function_dispatcher.h" |
[email protected] | e4452d3 | 2013-11-15 23:07:41 | [diff] [blame] | 22 | #include "extensions/common/extension.h" |
[email protected] | 637bf32 | 2011-10-01 20:46:32 | [diff] [blame] | 23 | #include "testing/gtest/include/gtest/gtest.h" |
| 24 | |
[email protected] | ea049a0 | 2011-12-25 21:37:09 | [diff] [blame] | 25 | using content::WebContents; |
[email protected] | 1c321ee | 2012-05-21 03:02:34 | [diff] [blame] | 26 | using extensions::Extension; |
[email protected] | 1d5e58b | 2013-01-31 08:41:40 | [diff] [blame] | 27 | using extensions::Manifest; |
[email protected] | 37bb582 | 2012-09-10 15:09:57 | [diff] [blame] | 28 | namespace keys = extensions::tabs_constants; |
[email protected] | ea049a0 | 2011-12-25 21:37:09 | [diff] [blame] | 29 | |
[email protected] | 4e3ce3b | 2011-10-14 23:25:17 | [diff] [blame] | 30 | namespace { |
| 31 | |
| 32 | class TestFunctionDispatcherDelegate |
[email protected] | 1a043689 | 2014-04-01 00:38:25 | [diff] [blame] | 33 | : public extensions::ExtensionFunctionDispatcher::Delegate { |
[email protected] | 4e3ce3b | 2011-10-14 23:25:17 | [diff] [blame] | 34 | public: |
| 35 | explicit TestFunctionDispatcherDelegate(Browser* browser) : |
| 36 | browser_(browser) {} |
dcheng | ae36a4a | 2014-10-21 12:36:36 | [diff] [blame] | 37 | ~TestFunctionDispatcherDelegate() override {} |
[email protected] | 4e3ce3b | 2011-10-14 23:25:17 | [diff] [blame] | 38 | |
| 39 | private: |
dcheng | ae36a4a | 2014-10-21 12:36:36 | [diff] [blame] | 40 | extensions::WindowController* GetExtensionWindowController() const override { |
[email protected] | b51f356 | 2012-05-05 22:01:43 | [diff] [blame] | 41 | return browser_->extension_window_controller(); |
[email protected] | 4e3ce3b | 2011-10-14 23:25:17 | [diff] [blame] | 42 | } |
| 43 | |
dcheng | ae36a4a | 2014-10-21 12:36:36 | [diff] [blame] | 44 | WebContents* GetAssociatedWebContents() const override { return NULL; } |
[email protected] | 4e3ce3b | 2011-10-14 23:25:17 | [diff] [blame] | 45 | |
| 46 | Browser* browser_; |
| 47 | }; |
| 48 | |
| 49 | } // namespace |
| 50 | |
[email protected] | 637bf32 | 2011-10-01 20:46:32 | [diff] [blame] | 51 | namespace extension_function_test_utils { |
| 52 | |
[email protected] | 637bf32 | 2011-10-01 20:46:32 | [diff] [blame] | 53 | base::ListValue* ParseList(const std::string& data) { |
Lei Zhang | 582ecd1 | 2019-02-13 20:28:54 | [diff] [blame] | 54 | std::unique_ptr<base::Value> result = base::JSONReader::ReadDeprecated(data); |
Christian Dullweber | adb9784c | 2019-02-07 10:18:14 | [diff] [blame] | 55 | if (!result) { |
| 56 | ADD_FAILURE() << "Failed to parse: " << data; |
| 57 | return nullptr; |
| 58 | } |
[email protected] | e49e1014 | 2014-07-23 06:52:41 | [diff] [blame] | 59 | base::ListValue* list = NULL; |
| 60 | result->GetAsList(&list); |
olli.raula | 4d36416 | 2015-09-10 06:39:40 | [diff] [blame] | 61 | ignore_result(result.release()); |
[email protected] | e49e1014 | 2014-07-23 06:52:41 | [diff] [blame] | 62 | return list; |
[email protected] | 637bf32 | 2011-10-01 20:46:32 | [diff] [blame] | 63 | } |
| 64 | |
[email protected] | 637bf32 | 2011-10-01 20:46:32 | [diff] [blame] | 65 | base::DictionaryValue* ToDictionary(base::Value* val) { |
[email protected] | 8ce80ea6 | 2011-10-18 18:00:18 | [diff] [blame] | 66 | EXPECT_TRUE(val); |
jdoerrie | 76cee9c | 2017-10-06 22:42:42 | [diff] [blame] | 67 | EXPECT_EQ(base::Value::Type::DICTIONARY, val->type()); |
[email protected] | 637bf32 | 2011-10-01 20:46:32 | [diff] [blame] | 68 | return static_cast<base::DictionaryValue*>(val); |
| 69 | } |
| 70 | |
[email protected] | 008ff7fb | 2011-12-19 08:51:17 | [diff] [blame] | 71 | base::ListValue* ToList(base::Value* val) { |
| 72 | EXPECT_TRUE(val); |
jdoerrie | 76cee9c | 2017-10-06 22:42:42 | [diff] [blame] | 73 | EXPECT_EQ(base::Value::Type::LIST, val->type()); |
[email protected] | 008ff7fb | 2011-12-19 08:51:17 | [diff] [blame] | 74 | return static_cast<base::ListValue*>(val); |
| 75 | } |
| 76 | |
Tim Judkins | 92389f75 | 2019-09-20 21:04:14 | [diff] [blame] | 77 | bool HasAnyPrivacySensitiveFields(base::DictionaryValue* val) { |
[email protected] | 37bb582 | 2012-09-10 15:09:57 | [diff] [blame] | 78 | std::string result; |
| 79 | if (val->GetString(keys::kUrlKey, &result) || |
| 80 | val->GetString(keys::kTitleKey, &result) || |
Tim Judkins | 92389f75 | 2019-09-20 21:04:14 | [diff] [blame] | 81 | val->GetString(keys::kFaviconUrlKey, &result) || |
| 82 | val->GetString(keys::kPendingUrlKey, &result)) |
[email protected] | 37bb582 | 2012-09-10 15:09:57 | [diff] [blame] | 83 | return true; |
| 84 | return false; |
| 85 | } |
| 86 | |
Clark DuVall | fd4db3d | 2019-07-30 19:10:43 | [diff] [blame] | 87 | std::string RunFunctionAndReturnError(ExtensionFunction* function, |
[email protected] | 637bf32 | 2011-10-01 20:46:32 | [diff] [blame] | 88 | const std::string& args, |
| 89 | Browser* browser) { |
Devlin Cronin | 35252c2b | 2018-02-14 00:03:53 | [diff] [blame] | 90 | return RunFunctionAndReturnError(function, args, browser, |
| 91 | extensions::api_test_utils::NONE); |
[email protected] | 637bf32 | 2011-10-01 20:46:32 | [diff] [blame] | 92 | } |
Devlin Cronin | 35252c2b | 2018-02-14 00:03:53 | [diff] [blame] | 93 | std::string RunFunctionAndReturnError( |
Clark DuVall | fd4db3d | 2019-07-30 19:10:43 | [diff] [blame] | 94 | ExtensionFunction* function, |
Devlin Cronin | 35252c2b | 2018-02-14 00:03:53 | [diff] [blame] | 95 | const std::string& args, |
| 96 | Browser* browser, |
| 97 | extensions::api_test_utils::RunFunctionFlags flags) { |
[email protected] | 637bf32 | 2011-10-01 20:46:32 | [diff] [blame] | 98 | scoped_refptr<ExtensionFunction> function_owner(function); |
| 99 | RunFunction(function, args, browser, flags); |
rdevlin.cronin | 187edaa9 | 2016-09-19 21:34:02 | [diff] [blame] | 100 | // 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] | 637bf32 | 2011-10-01 20:46:32 | [diff] [blame] | 107 | return function->GetError(); |
| 108 | } |
| 109 | |
Clark DuVall | fd4db3d | 2019-07-30 19:10:43 | [diff] [blame] | 110 | base::Value* RunFunctionAndReturnSingleResult(ExtensionFunction* function, |
| 111 | const std::string& args, |
| 112 | Browser* browser) { |
Devlin Cronin | 35252c2b | 2018-02-14 00:03:53 | [diff] [blame] | 113 | return RunFunctionAndReturnSingleResult(function, args, browser, |
| 114 | extensions::api_test_utils::NONE); |
[email protected] | 637bf32 | 2011-10-01 20:46:32 | [diff] [blame] | 115 | } |
[email protected] | 07ff5fd | 2012-07-12 22:39:09 | [diff] [blame] | 116 | base::Value* RunFunctionAndReturnSingleResult( |
Clark DuVall | fd4db3d | 2019-07-30 19:10:43 | [diff] [blame] | 117 | ExtensionFunction* function, |
[email protected] | 07ff5fd | 2012-07-12 22:39:09 | [diff] [blame] | 118 | const std::string& args, |
| 119 | Browser* browser, |
Devlin Cronin | 35252c2b | 2018-02-14 00:03:53 | [diff] [blame] | 120 | extensions::api_test_utils::RunFunctionFlags flags) { |
[email protected] | 637bf32 | 2011-10-01 20:46:32 | [diff] [blame] | 121 | scoped_refptr<ExtensionFunction> function_owner(function); |
| 122 | RunFunction(function, args, browser, flags); |
[email protected] | 8ce80ea6 | 2011-10-18 18:00:18 | [diff] [blame] | 123 | EXPECT_TRUE(function->GetError().empty()) << "Unexpected error: " |
| 124 | << function->GetError(); |
[email protected] | 5d30f92bf | 2012-08-03 08:43:37 | [diff] [blame] | 125 | const base::Value* single_result = NULL; |
[email protected] | 07ff5fd | 2012-07-12 22:39:09 | [diff] [blame] | 126 | if (function->GetResultList() != NULL && |
| 127 | function->GetResultList()->Get(0, &single_result)) { |
| 128 | return single_result->DeepCopy(); |
| 129 | } |
| 130 | return NULL; |
[email protected] | 637bf32 | 2011-10-01 20:46:32 | [diff] [blame] | 131 | } |
| 132 | |
Clark DuVall | fd4db3d | 2019-07-30 19:10:43 | [diff] [blame] | 133 | bool RunFunction(ExtensionFunction* function, |
[email protected] | bdfc03e | 2011-11-22 00:20:33 | [diff] [blame] | 134 | const std::string& args, |
| 135 | Browser* browser, |
Devlin Cronin | 35252c2b | 2018-02-14 00:03:53 | [diff] [blame] | 136 | extensions::api_test_utils::RunFunctionFlags flags) { |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 137 | std::unique_ptr<base::ListValue> parsed_args(ParseList(args)); |
[email protected] | fc672e1 | 2014-08-16 08:16:15 | [diff] [blame] | 138 | EXPECT_TRUE(parsed_args.get()) |
| 139 | << "Could not parse extension function arguments: " << args; |
dcheng | 1fc00f1 | 2015-12-26 22:18:03 | [diff] [blame] | 140 | return RunFunction(function, std::move(parsed_args), browser, flags); |
[email protected] | fc672e1 | 2014-08-16 08:16:15 | [diff] [blame] | 141 | } |
| 142 | |
Clark DuVall | fd4db3d | 2019-07-30 19:10:43 | [diff] [blame] | 143 | bool RunFunction(ExtensionFunction* function, |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 144 | std::unique_ptr<base::ListValue> args, |
[email protected] | fc672e1 | 2014-08-16 08:16:15 | [diff] [blame] | 145 | Browser* browser, |
Devlin Cronin | 35252c2b | 2018-02-14 00:03:53 | [diff] [blame] | 146 | extensions::api_test_utils::RunFunctionFlags flags) { |
[email protected] | bdfc03e | 2011-11-22 00:20:33 | [diff] [blame] | 147 | TestFunctionDispatcherDelegate dispatcher_delegate(browser); |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 148 | std::unique_ptr<extensions::ExtensionFunctionDispatcher> dispatcher( |
rdevlin.cronin | cb2ec659a | 2015-06-10 23:32:41 | [diff] [blame] | 149 | new extensions::ExtensionFunctionDispatcher(browser->profile())); |
| 150 | dispatcher->set_delegate(&dispatcher_delegate); |
Devlin Cronin | 35252c2b | 2018-02-14 00:03:53 | [diff] [blame] | 151 | return extensions::api_test_utils::RunFunction(function, std::move(args), |
| 152 | browser->profile(), |
| 153 | std::move(dispatcher), flags); |
[email protected] | d8c8749b9 | 2011-11-16 22:31:32 | [diff] [blame] | 154 | } |
| 155 | |
[email protected] | 637bf32 | 2011-10-01 20:46:32 | [diff] [blame] | 156 | } // namespace extension_function_test_utils |