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