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