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