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