| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "extensions/browser/api_test_utils.h" | 5 #include "extensions/browser/api_test_utils.h" |
| 6 | 6 |
| 7 #include "base/json/json_reader.h" | 7 #include "base/json/json_reader.h" |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/values.h" | 9 #include "base/values.h" |
| 10 #include "components/crx_file/id_util.h" | 10 #include "components/crx_file/id_util.h" |
| 11 #include "content/public/browser/browser_context.h" | 11 #include "content/public/browser/browser_context.h" |
| 12 #include "content/public/test/test_utils.h" | 12 #include "content/public/test/test_utils.h" |
| 13 #include "extensions/browser/extension_function.h" | 13 #include "extensions/browser/extension_function.h" |
| 14 #include "extensions/browser/extension_function_dispatcher.h" | 14 #include "extensions/browser/extension_function_dispatcher.h" |
| 15 #include "extensions/common/extension_builder.h" | 15 #include "extensions/common/extension_builder.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 | 17 |
| 18 using extensions::ExtensionFunctionDispatcher; | 18 using extensions::ExtensionFunctionDispatcher; |
| 19 | 19 |
| 20 namespace { | 20 namespace { |
| 21 | 21 |
| 22 class TestFunctionDispatcherDelegate | |
| 23 : public ExtensionFunctionDispatcher::Delegate { | |
| 24 public: | |
| 25 TestFunctionDispatcherDelegate() {} | |
| 26 ~TestFunctionDispatcherDelegate() override {} | |
| 27 | |
| 28 // NULL implementation. | |
| 29 private: | |
| 30 DISALLOW_COPY_AND_ASSIGN(TestFunctionDispatcherDelegate); | |
| 31 }; | |
| 32 | |
| 33 scoped_ptr<base::Value> ParseJSON(const std::string& data) { | 22 scoped_ptr<base::Value> ParseJSON(const std::string& data) { |
| 34 return base::JSONReader::Read(data); | 23 return base::JSONReader::Read(data); |
| 35 } | 24 } |
| 36 | 25 |
| 37 scoped_ptr<base::ListValue> ParseList(const std::string& data) { | 26 scoped_ptr<base::ListValue> ParseList(const std::string& data) { |
| 38 scoped_ptr<base::Value> result = ParseJSON(data); | 27 scoped_ptr<base::Value> result = ParseJSON(data); |
| 39 scoped_ptr<base::ListValue> list_result; | 28 scoped_ptr<base::ListValue> list_result; |
| 40 if (result->GetAsList(nullptr)) | 29 if (result->GetAsList(nullptr)) |
| 41 list_result.reset(static_cast<base::ListValue*>(result.release())); | 30 list_result.reset(static_cast<base::ListValue*>(result.release())); |
| 42 return list_result; | 31 return list_result; |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 178 const std::string& args, | 167 const std::string& args, |
| 179 content::BrowserContext* context) { | 168 content::BrowserContext* context) { |
| 180 return RunFunctionAndReturnSingleResult(function, args, context, NONE); | 169 return RunFunctionAndReturnSingleResult(function, args, context, NONE); |
| 181 } | 170 } |
| 182 | 171 |
| 183 base::Value* RunFunctionAndReturnSingleResult( | 172 base::Value* RunFunctionAndReturnSingleResult( |
| 184 UIThreadExtensionFunction* function, | 173 UIThreadExtensionFunction* function, |
| 185 const std::string& args, | 174 const std::string& args, |
| 186 content::BrowserContext* context, | 175 content::BrowserContext* context, |
| 187 RunFunctionFlags flags) { | 176 RunFunctionFlags flags) { |
| 188 TestFunctionDispatcherDelegate delegate; | |
| 189 scoped_ptr<ExtensionFunctionDispatcher> dispatcher( | 177 scoped_ptr<ExtensionFunctionDispatcher> dispatcher( |
| 190 new ExtensionFunctionDispatcher(context, &delegate)); | 178 new ExtensionFunctionDispatcher(context)); |
| 191 | 179 |
| 192 return RunFunctionWithDelegateAndReturnSingleResult( | 180 return RunFunctionWithDelegateAndReturnSingleResult( |
| 193 function, args, context, dispatcher.Pass(), flags); | 181 function, args, context, dispatcher.Pass(), flags); |
| 194 } | 182 } |
| 195 | 183 |
| 196 std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function, | 184 std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function, |
| 197 const std::string& args, | 185 const std::string& args, |
| 198 content::BrowserContext* context) { | 186 content::BrowserContext* context) { |
| 199 return RunFunctionAndReturnError(function, args, context, NONE); | 187 return RunFunctionAndReturnError(function, args, context, NONE); |
| 200 } | 188 } |
| 201 | 189 |
| 202 std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function, | 190 std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function, |
| 203 const std::string& args, | 191 const std::string& args, |
| 204 content::BrowserContext* context, | 192 content::BrowserContext* context, |
| 205 RunFunctionFlags flags) { | 193 RunFunctionFlags flags) { |
| 206 TestFunctionDispatcherDelegate delegate; | |
| 207 scoped_ptr<ExtensionFunctionDispatcher> dispatcher( | 194 scoped_ptr<ExtensionFunctionDispatcher> dispatcher( |
| 208 new ExtensionFunctionDispatcher(context, &delegate)); | 195 new ExtensionFunctionDispatcher(context)); |
| 209 scoped_refptr<ExtensionFunction> function_owner(function); | 196 scoped_refptr<ExtensionFunction> function_owner(function); |
| 210 // Without a callback the function will not generate a result. | 197 // Without a callback the function will not generate a result. |
| 211 function->set_has_callback(true); | 198 function->set_has_callback(true); |
| 212 RunFunction(function, args, context, dispatcher.Pass(), flags); | 199 RunFunction(function, args, context, dispatcher.Pass(), flags); |
| 213 EXPECT_FALSE(function->GetResultList()) << "Did not expect a result"; | 200 EXPECT_FALSE(function->GetResultList()) << "Did not expect a result"; |
| 214 return function->GetError(); | 201 return function->GetError(); |
| 215 } | 202 } |
| 216 | 203 |
| 217 bool RunFunction(UIThreadExtensionFunction* function, | 204 bool RunFunction(UIThreadExtensionFunction* function, |
| 218 const std::string& args, | 205 const std::string& args, |
| 219 content::BrowserContext* context) { | 206 content::BrowserContext* context) { |
| 220 TestFunctionDispatcherDelegate delegate; | |
| 221 scoped_ptr<ExtensionFunctionDispatcher> dispatcher( | 207 scoped_ptr<ExtensionFunctionDispatcher> dispatcher( |
| 222 new ExtensionFunctionDispatcher(context, &delegate)); | 208 new ExtensionFunctionDispatcher(context)); |
| 223 return RunFunction(function, args, context, dispatcher.Pass(), NONE); | 209 return RunFunction(function, args, context, dispatcher.Pass(), NONE); |
| 224 } | 210 } |
| 225 | 211 |
| 226 bool RunFunction(UIThreadExtensionFunction* function, | 212 bool RunFunction(UIThreadExtensionFunction* function, |
| 227 const std::string& args, | 213 const std::string& args, |
| 228 content::BrowserContext* context, | 214 content::BrowserContext* context, |
| 229 scoped_ptr<extensions::ExtensionFunctionDispatcher> dispatcher, | 215 scoped_ptr<extensions::ExtensionFunctionDispatcher> dispatcher, |
| 230 RunFunctionFlags flags) { | 216 RunFunctionFlags flags) { |
| 231 scoped_ptr<base::ListValue> parsed_args = ParseList(args); | 217 scoped_ptr<base::ListValue> parsed_args = ParseList(args); |
| 232 EXPECT_TRUE(parsed_args.get()) | 218 EXPECT_TRUE(parsed_args.get()) |
| (...skipping 24 matching lines...) Expand all Loading... |
| 257 response_delegate.set_should_post_quit(true); | 243 response_delegate.set_should_post_quit(true); |
| 258 content::RunMessageLoop(); | 244 content::RunMessageLoop(); |
| 259 } | 245 } |
| 260 | 246 |
| 261 EXPECT_TRUE(response_delegate.HasResponse()); | 247 EXPECT_TRUE(response_delegate.HasResponse()); |
| 262 return response_delegate.GetResponse(); | 248 return response_delegate.GetResponse(); |
| 263 } | 249 } |
| 264 | 250 |
| 265 } // namespace api_test_utils | 251 } // namespace api_test_utils |
| 266 } // namespace extensions | 252 } // namespace extensions |
| OLD | NEW |