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