[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> |
| 8 | |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 9 | #include "base/files/file_path.h" |
[email protected] | 637bf32 | 2011-10-01 20:46:32 | [diff] [blame] | 10 | #include "base/json/json_reader.h" |
| 11 | #include "base/values.h" |
[email protected] | 37bb582 | 2012-09-10 15:09:57 | [diff] [blame] | 12 | #include "chrome/browser/extensions/api/tabs/tabs_constants.h" |
[email protected] | 21a4008 | 2013-10-28 21:19:23 | [diff] [blame] | 13 | #include "chrome/browser/profiles/profile.h" |
[email protected] | 637bf32 | 2011-10-01 20:46:32 | [diff] [blame] | 14 | #include "chrome/browser/ui/browser.h" |
[email protected] | d8c8749b9 | 2011-11-16 22:31:32 | [diff] [blame] | 15 | #include "chrome/test/base/ui_test_utils.h" |
[email protected] | e49e1014 | 2014-07-23 06:52:41 | [diff] [blame] | 16 | #include "extensions/browser/api_test_utils.h" |
[email protected] | 14c3571a | 2013-11-13 00:18:44 | [diff] [blame] | 17 | #include "extensions/browser/extension_function.h" |
[email protected] | 0b9de03 | 2014-03-15 05:47:01 | [diff] [blame] | 18 | #include "extensions/browser/extension_function_dispatcher.h" |
[email protected] | e4452d3 | 2013-11-15 23:07:41 | [diff] [blame] | 19 | #include "extensions/common/extension.h" |
[email protected] | 993da5e | 2013-03-23 21:25:16 | [diff] [blame] | 20 | #include "extensions/common/id_util.h" |
[email protected] | 637bf32 | 2011-10-01 20:46:32 | [diff] [blame] | 21 | #include "testing/gtest/include/gtest/gtest.h" |
| 22 | |
[email protected] | ea049a0 | 2011-12-25 21:37:09 | [diff] [blame] | 23 | using content::WebContents; |
[email protected] | 1c321ee | 2012-05-21 03:02:34 | [diff] [blame] | 24 | using extensions::Extension; |
[email protected] | 1d5e58b | 2013-01-31 08:41:40 | [diff] [blame] | 25 | using extensions::Manifest; |
[email protected] | 37bb582 | 2012-09-10 15:09:57 | [diff] [blame] | 26 | namespace keys = extensions::tabs_constants; |
[email protected] | ea049a0 | 2011-12-25 21:37:09 | [diff] [blame] | 27 | |
[email protected] | 4e3ce3b | 2011-10-14 23:25:17 | [diff] [blame] | 28 | namespace { |
| 29 | |
| 30 | class TestFunctionDispatcherDelegate |
[email protected] | 1a043689 | 2014-04-01 00:38:25 | [diff] [blame] | 31 | : public extensions::ExtensionFunctionDispatcher::Delegate { |
[email protected] | 4e3ce3b | 2011-10-14 23:25:17 | [diff] [blame] | 32 | public: |
| 33 | explicit TestFunctionDispatcherDelegate(Browser* browser) : |
| 34 | browser_(browser) {} |
| 35 | virtual ~TestFunctionDispatcherDelegate() {} |
| 36 | |
| 37 | private: |
[email protected] | 44f4b13 | 2012-07-17 20:36:57 | [diff] [blame] | 38 | virtual extensions::WindowController* GetExtensionWindowController() |
[email protected] | b51f356 | 2012-05-05 22:01:43 | [diff] [blame] | 39 | const OVERRIDE { |
| 40 | return browser_->extension_window_controller(); |
[email protected] | 4e3ce3b | 2011-10-14 23:25:17 | [diff] [blame] | 41 | } |
| 42 | |
[email protected] | ea049a0 | 2011-12-25 21:37:09 | [diff] [blame] | 43 | virtual WebContents* GetAssociatedWebContents() const OVERRIDE { |
[email protected] | 4e3ce3b | 2011-10-14 23:25:17 | [diff] [blame] | 44 | return NULL; |
| 45 | } |
| 46 | |
| 47 | Browser* browser_; |
| 48 | }; |
| 49 | |
| 50 | } // namespace |
| 51 | |
[email protected] | 637bf32 | 2011-10-01 20:46:32 | [diff] [blame] | 52 | namespace extension_function_test_utils { |
| 53 | |
| 54 | base::Value* ParseJSON(const std::string& data) { |
[email protected] | cd578575 | 2012-04-11 00:15:41 | [diff] [blame] | 55 | return base::JSONReader::Read(data); |
[email protected] | 637bf32 | 2011-10-01 20:46:32 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | base::ListValue* ParseList(const std::string& data) { |
[email protected] | e49e1014 | 2014-07-23 06:52:41 | [diff] [blame] | 59 | base::Value* result = ParseJSON(data); |
| 60 | base::ListValue* list = NULL; |
| 61 | result->GetAsList(&list); |
| 62 | return list; |
[email protected] | 637bf32 | 2011-10-01 20:46:32 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | base::DictionaryValue* ParseDictionary( |
| 66 | const std::string& data) { |
[email protected] | e49e1014 | 2014-07-23 06:52:41 | [diff] [blame] | 67 | base::Value* result = ParseJSON(data); |
| 68 | base::DictionaryValue* dict = NULL; |
| 69 | result->GetAsDictionary(&dict); |
| 70 | return dict; |
[email protected] | 637bf32 | 2011-10-01 20:46:32 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | bool GetBoolean(base::DictionaryValue* val, const std::string& key) { |
| 74 | bool result = false; |
| 75 | if (!val->GetBoolean(key, &result)) |
| 76 | ADD_FAILURE() << key << " does not exist or is not a boolean."; |
| 77 | return result; |
| 78 | } |
| 79 | |
| 80 | int GetInteger(base::DictionaryValue* val, const std::string& key) { |
| 81 | int result = 0; |
| 82 | if (!val->GetInteger(key, &result)) |
| 83 | ADD_FAILURE() << key << " does not exist or is not an integer."; |
| 84 | return result; |
| 85 | } |
| 86 | |
| 87 | std::string GetString(base::DictionaryValue* val, const std::string& key) { |
| 88 | std::string result; |
| 89 | if (!val->GetString(key, &result)) |
| 90 | ADD_FAILURE() << key << " does not exist or is not a string."; |
| 91 | return result; |
| 92 | } |
| 93 | |
| 94 | base::DictionaryValue* ToDictionary(base::Value* val) { |
[email protected] | 8ce80ea6 | 2011-10-18 18:00:18 | [diff] [blame] | 95 | EXPECT_TRUE(val); |
| 96 | EXPECT_EQ(base::Value::TYPE_DICTIONARY, val->GetType()); |
[email protected] | 637bf32 | 2011-10-01 20:46:32 | [diff] [blame] | 97 | return static_cast<base::DictionaryValue*>(val); |
| 98 | } |
| 99 | |
[email protected] | 008ff7fb | 2011-12-19 08:51:17 | [diff] [blame] | 100 | base::ListValue* ToList(base::Value* val) { |
| 101 | EXPECT_TRUE(val); |
| 102 | EXPECT_EQ(base::Value::TYPE_LIST, val->GetType()); |
| 103 | return static_cast<base::ListValue*>(val); |
| 104 | } |
| 105 | |
[email protected] | 4e3ce3b | 2011-10-14 23:25:17 | [diff] [blame] | 106 | scoped_refptr<Extension> CreateEmptyExtension() { |
[email protected] | 1d5e58b | 2013-01-31 08:41:40 | [diff] [blame] | 107 | return CreateEmptyExtensionWithLocation(Manifest::INTERNAL); |
[email protected] | 6d2d55b | 2012-05-05 21:33:43 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | scoped_refptr<Extension> CreateEmptyExtensionWithLocation( |
[email protected] | 1d5e58b | 2013-01-31 08:41:40 | [diff] [blame] | 111 | Manifest::Location location) { |
[email protected] | 4e3ce3b | 2011-10-14 23:25:17 | [diff] [blame] | 112 | scoped_ptr<base::DictionaryValue> test_extension_value( |
| 113 | ParseDictionary("{\"name\": \"Test\", \"version\": \"1.0\"}")); |
[email protected] | 0942685 | 2012-09-11 22:39:07 | [diff] [blame] | 114 | return CreateExtension(location, test_extension_value.get(), std::string()); |
| 115 | } |
| 116 | |
| 117 | scoped_refptr<Extension> CreateEmptyExtension( |
| 118 | const std::string& id_input) { |
| 119 | scoped_ptr<base::DictionaryValue> test_extension_value( |
| 120 | ParseDictionary("{\"name\": \"Test\", \"version\": \"1.0\"}")); |
[email protected] | 1d5e58b | 2013-01-31 08:41:40 | [diff] [blame] | 121 | return CreateExtension(Manifest::INTERNAL, test_extension_value.get(), |
[email protected] | 0942685 | 2012-09-11 22:39:07 | [diff] [blame] | 122 | id_input); |
[email protected] | 37bb582 | 2012-09-10 15:09:57 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | scoped_refptr<Extension> CreateExtension( |
| 126 | base::DictionaryValue* test_extension_value) { |
[email protected] | 1d5e58b | 2013-01-31 08:41:40 | [diff] [blame] | 127 | return CreateExtension(Manifest::INTERNAL, test_extension_value, |
[email protected] | 0942685 | 2012-09-11 22:39:07 | [diff] [blame] | 128 | std::string()); |
[email protected] | 37bb582 | 2012-09-10 15:09:57 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | scoped_refptr<Extension> CreateExtension( |
[email protected] | 1d5e58b | 2013-01-31 08:41:40 | [diff] [blame] | 132 | Manifest::Location location, |
[email protected] | 0942685 | 2012-09-11 22:39:07 | [diff] [blame] | 133 | base::DictionaryValue* test_extension_value, |
| 134 | const std::string& id_input) { |
[email protected] | 37bb582 | 2012-09-10 15:09:57 | [diff] [blame] | 135 | std::string error; |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 136 | const base::FilePath test_extension_path; |
[email protected] | 0942685 | 2012-09-11 22:39:07 | [diff] [blame] | 137 | std::string id; |
| 138 | if (!id_input.empty()) |
[email protected] | 993da5e | 2013-03-23 21:25:16 | [diff] [blame] | 139 | id = extensions::id_util::GenerateId(id_input); |
[email protected] | 4e3ce3b | 2011-10-14 23:25:17 | [diff] [blame] | 140 | scoped_refptr<Extension> extension(Extension::Create( |
| 141 | test_extension_path, |
[email protected] | 6d2d55b | 2012-05-05 21:33:43 | [diff] [blame] | 142 | location, |
[email protected] | 37bb582 | 2012-09-10 15:09:57 | [diff] [blame] | 143 | *test_extension_value, |
[email protected] | 4e3ce3b | 2011-10-14 23:25:17 | [diff] [blame] | 144 | Extension::NO_FLAGS, |
[email protected] | 0942685 | 2012-09-11 22:39:07 | [diff] [blame] | 145 | id, |
[email protected] | 4e3ce3b | 2011-10-14 23:25:17 | [diff] [blame] | 146 | &error)); |
[email protected] | 8ce80ea6 | 2011-10-18 18:00:18 | [diff] [blame] | 147 | EXPECT_TRUE(error.empty()) << "Could not parse test extension " << error; |
[email protected] | 4e3ce3b | 2011-10-14 23:25:17 | [diff] [blame] | 148 | return extension; |
| 149 | } |
| 150 | |
[email protected] | 37bb582 | 2012-09-10 15:09:57 | [diff] [blame] | 151 | bool HasPrivacySensitiveFields(base::DictionaryValue* val) { |
| 152 | std::string result; |
| 153 | if (val->GetString(keys::kUrlKey, &result) || |
| 154 | val->GetString(keys::kTitleKey, &result) || |
| 155 | val->GetString(keys::kFaviconUrlKey, &result)) |
| 156 | return true; |
| 157 | return false; |
| 158 | } |
| 159 | |
[email protected] | 637bf32 | 2011-10-01 20:46:32 | [diff] [blame] | 160 | std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function, |
| 161 | const std::string& args, |
| 162 | Browser* browser) { |
| 163 | return RunFunctionAndReturnError(function, args, browser, NONE); |
| 164 | } |
| 165 | std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function, |
| 166 | const std::string& args, |
| 167 | Browser* browser, |
| 168 | RunFunctionFlags flags) { |
| 169 | scoped_refptr<ExtensionFunction> function_owner(function); |
[email protected] | 61165fd | 2012-07-24 01:12:23 | [diff] [blame] | 170 | // Without a callback the function will not generate a result. |
| 171 | function->set_has_callback(true); |
[email protected] | 637bf32 | 2011-10-01 20:46:32 | [diff] [blame] | 172 | RunFunction(function, args, browser, flags); |
[email protected] | 07ff5fd | 2012-07-12 22:39:09 | [diff] [blame] | 173 | EXPECT_FALSE(function->GetResultList()) << "Did not expect a result"; |
[email protected] | 637bf32 | 2011-10-01 20:46:32 | [diff] [blame] | 174 | return function->GetError(); |
| 175 | } |
| 176 | |
[email protected] | 07ff5fd | 2012-07-12 22:39:09 | [diff] [blame] | 177 | base::Value* RunFunctionAndReturnSingleResult( |
| 178 | UIThreadExtensionFunction* function, |
| 179 | const std::string& args, |
| 180 | Browser* browser) { |
| 181 | return RunFunctionAndReturnSingleResult(function, args, browser, NONE); |
[email protected] | 637bf32 | 2011-10-01 20:46:32 | [diff] [blame] | 182 | } |
[email protected] | 07ff5fd | 2012-07-12 22:39:09 | [diff] [blame] | 183 | base::Value* RunFunctionAndReturnSingleResult( |
| 184 | UIThreadExtensionFunction* function, |
| 185 | const std::string& args, |
| 186 | Browser* browser, |
| 187 | RunFunctionFlags flags) { |
[email protected] | 637bf32 | 2011-10-01 20:46:32 | [diff] [blame] | 188 | scoped_refptr<ExtensionFunction> function_owner(function); |
[email protected] | 61165fd | 2012-07-24 01:12:23 | [diff] [blame] | 189 | // Without a callback the function will not generate a result. |
| 190 | function->set_has_callback(true); |
[email protected] | 637bf32 | 2011-10-01 20:46:32 | [diff] [blame] | 191 | RunFunction(function, args, browser, flags); |
[email protected] | 8ce80ea6 | 2011-10-18 18:00:18 | [diff] [blame] | 192 | EXPECT_TRUE(function->GetError().empty()) << "Unexpected error: " |
| 193 | << function->GetError(); |
[email protected] | 5d30f92bf | 2012-08-03 08:43:37 | [diff] [blame] | 194 | const base::Value* single_result = NULL; |
[email protected] | 07ff5fd | 2012-07-12 22:39:09 | [diff] [blame] | 195 | if (function->GetResultList() != NULL && |
| 196 | function->GetResultList()->Get(0, &single_result)) { |
| 197 | return single_result->DeepCopy(); |
| 198 | } |
| 199 | return NULL; |
[email protected] | 637bf32 | 2011-10-01 20:46:32 | [diff] [blame] | 200 | } |
| 201 | |
[email protected] | 4bf2b05 | 2013-09-30 21:05:41 | [diff] [blame] | 202 | // This helps us be able to wait until an UIThreadExtensionFunction calls |
[email protected] | d8c8749b9 | 2011-11-16 22:31:32 | [diff] [blame] | 203 | // SendResponse. |
[email protected] | bdfc03e | 2011-11-22 00:20:33 | [diff] [blame] | 204 | class SendResponseDelegate |
| 205 | : public UIThreadExtensionFunction::DelegateForTests { |
[email protected] | d8c8749b9 | 2011-11-16 22:31:32 | [diff] [blame] | 206 | public: |
| 207 | SendResponseDelegate() : should_post_quit_(false) {} |
| 208 | |
| 209 | virtual ~SendResponseDelegate() {} |
| 210 | |
| 211 | void set_should_post_quit(bool should_quit) { |
| 212 | should_post_quit_ = should_quit; |
| 213 | } |
| 214 | |
| 215 | bool HasResponse() { |
| 216 | return response_.get() != NULL; |
| 217 | } |
| 218 | |
| 219 | bool GetResponse() { |
| 220 | EXPECT_TRUE(HasResponse()); |
| 221 | return *response_.get(); |
| 222 | } |
| 223 | |
[email protected] | bdfc03e | 2011-11-22 00:20:33 | [diff] [blame] | 224 | virtual void OnSendResponse(UIThreadExtensionFunction* function, |
[email protected] | ca6df68 | 2012-04-10 23:00:20 | [diff] [blame] | 225 | bool success, |
[email protected] | 49aeab6 | 2013-02-07 02:53:11 | [diff] [blame] | 226 | bool bad_message) OVERRIDE { |
[email protected] | ca6df68 | 2012-04-10 23:00:20 | [diff] [blame] | 227 | ASSERT_FALSE(bad_message); |
[email protected] | d8c8749b9 | 2011-11-16 22:31:32 | [diff] [blame] | 228 | ASSERT_FALSE(HasResponse()); |
| 229 | response_.reset(new bool); |
| 230 | *response_ = success; |
| 231 | if (should_post_quit_) { |
[email protected] | b3a2509 | 2013-05-28 22:08:16 | [diff] [blame] | 232 | base::MessageLoopForUI::current()->Quit(); |
[email protected] | d8c8749b9 | 2011-11-16 22:31:32 | [diff] [blame] | 233 | } |
| 234 | } |
| 235 | |
| 236 | private: |
| 237 | scoped_ptr<bool> response_; |
| 238 | bool should_post_quit_; |
| 239 | }; |
| 240 | |
[email protected] | bdfc03e | 2011-11-22 00:20:33 | [diff] [blame] | 241 | bool RunFunction(UIThreadExtensionFunction* function, |
| 242 | const std::string& args, |
| 243 | Browser* browser, |
| 244 | RunFunctionFlags flags) { |
[email protected] | bdfc03e | 2011-11-22 00:20:33 | [diff] [blame] | 245 | TestFunctionDispatcherDelegate dispatcher_delegate(browser); |
[email protected] | e49e1014 | 2014-07-23 06:52:41 | [diff] [blame] | 246 | scoped_ptr<extensions::ExtensionFunctionDispatcher> dispatcher( |
| 247 | new extensions::ExtensionFunctionDispatcher(browser->profile(), |
| 248 | &dispatcher_delegate)); |
| 249 | // TODO(yoz): The cast is a hack; these flags should be defined in |
| 250 | // only one place. See crbug.com/394840. |
| 251 | return extensions::api_test_utils::RunFunction( |
| 252 | function, |
| 253 | args, |
| 254 | browser->profile(), |
| 255 | dispatcher.Pass(), |
| 256 | static_cast<extensions::api_test_utils::RunFunctionFlags>(flags)); |
[email protected] | d8c8749b9 | 2011-11-16 22:31:32 | [diff] [blame] | 257 | } |
| 258 | |
[email protected] | 637bf32 | 2011-10-01 20:46:32 | [diff] [blame] | 259 | } // namespace extension_function_test_utils |