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