| 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 "content/public/browser/browser_context.h" | 10 #include "content/public/browser/browser_context.h" |
| 11 #include "content/public/test/test_utils.h" | 11 #include "content/public/test/test_utils.h" |
| 12 #include "extensions/browser/extension_function.h" | 12 #include "extensions/browser/extension_function.h" |
| 13 #include "extensions/browser/extension_function_dispatcher.h" | 13 #include "extensions/browser/extension_function_dispatcher.h" |
| 14 #include "extensions/common/extension_builder.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 16 |
| 17 using extensions::ExtensionFunctionDispatcher; |
| 18 |
| 16 namespace { | 19 namespace { |
| 17 | 20 |
| 21 class TestFunctionDispatcherDelegate |
| 22 : public ExtensionFunctionDispatcher::Delegate { |
| 23 public: |
| 24 TestFunctionDispatcherDelegate() {} |
| 25 virtual ~TestFunctionDispatcherDelegate() {} |
| 26 |
| 27 // NULL implementation. |
| 28 private: |
| 29 DISALLOW_COPY_AND_ASSIGN(TestFunctionDispatcherDelegate); |
| 30 }; |
| 31 |
| 18 base::Value* ParseJSON(const std::string& data) { | 32 base::Value* ParseJSON(const std::string& data) { |
| 19 return base::JSONReader::Read(data); | 33 return base::JSONReader::Read(data); |
| 20 } | 34 } |
| 21 | 35 |
| 22 base::ListValue* ParseList(const std::string& data) { | 36 base::ListValue* ParseList(const std::string& data) { |
| 23 base::Value* result = ParseJSON(data); | 37 base::Value* result = ParseJSON(data); |
| 24 base::ListValue* list = NULL; | 38 base::ListValue* list = NULL; |
| 25 result->GetAsList(&list); | 39 result->GetAsList(&list); |
| 26 return list; | 40 return list; |
| 27 } | 41 } |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 62 scoped_ptr<bool> response_; | 76 scoped_ptr<bool> response_; |
| 63 bool should_post_quit_; | 77 bool should_post_quit_; |
| 64 }; | 78 }; |
| 65 | 79 |
| 66 } // namespace | 80 } // namespace |
| 67 | 81 |
| 68 namespace extensions { | 82 namespace extensions { |
| 69 | 83 |
| 70 namespace api_test_utils { | 84 namespace api_test_utils { |
| 71 | 85 |
| 72 base::Value* RunFunctionAndReturnSingleResult( | 86 base::Value* RunFunctionWithDelegateAndReturnSingleResult( |
| 73 UIThreadExtensionFunction* function, | 87 UIThreadExtensionFunction* function, |
| 74 const std::string& args, | 88 const std::string& args, |
| 75 content::BrowserContext* context, | 89 content::BrowserContext* context, |
| 76 scoped_ptr<extensions::ExtensionFunctionDispatcher> dispatcher) { | 90 scoped_ptr<extensions::ExtensionFunctionDispatcher> dispatcher) { |
| 77 return RunFunctionAndReturnSingleResult( | 91 return RunFunctionWithDelegateAndReturnSingleResult( |
| 78 function, args, context, dispatcher.Pass(), NONE); | 92 function, args, context, dispatcher.Pass(), NONE); |
| 79 } | 93 } |
| 80 | 94 |
| 81 base::Value* RunFunctionAndReturnSingleResult( | 95 base::Value* RunFunctionWithDelegateAndReturnSingleResult( |
| 82 UIThreadExtensionFunction* function, | 96 UIThreadExtensionFunction* function, |
| 83 const std::string& args, | 97 const std::string& args, |
| 84 content::BrowserContext* context, | 98 content::BrowserContext* context, |
| 85 scoped_ptr<extensions::ExtensionFunctionDispatcher> dispatcher, | 99 scoped_ptr<extensions::ExtensionFunctionDispatcher> dispatcher, |
| 86 RunFunctionFlags flags) { | 100 RunFunctionFlags flags) { |
| 87 scoped_refptr<ExtensionFunction> function_owner(function); | 101 scoped_refptr<ExtensionFunction> function_owner(function); |
| 88 // Without a callback the function will not generate a result. | 102 // Without a callback the function will not generate a result. |
| 89 function->set_has_callback(true); | 103 function->set_has_callback(true); |
| 90 RunFunction(function, args, context, dispatcher.Pass(), flags); | 104 RunFunction(function, args, context, dispatcher.Pass(), flags); |
| 91 EXPECT_TRUE(function->GetError().empty()) | 105 EXPECT_TRUE(function->GetError().empty()) |
| 92 << "Unexpected error: " << function->GetError(); | 106 << "Unexpected error: " << function->GetError(); |
| 93 const base::Value* single_result = NULL; | 107 const base::Value* single_result = NULL; |
| 94 if (function->GetResultList() != NULL && | 108 if (function->GetResultList() != NULL && |
| 95 function->GetResultList()->Get(0, &single_result)) { | 109 function->GetResultList()->Get(0, &single_result)) { |
| 96 return single_result->DeepCopy(); | 110 return single_result->DeepCopy(); |
| 97 } | 111 } |
| 98 return NULL; | 112 return NULL; |
| 99 } | 113 } |
| 100 | 114 |
| 115 base::Value* RunFunctionAndReturnSingleResult( |
| 116 UIThreadExtensionFunction* function, |
| 117 const std::string& args, |
| 118 content::BrowserContext* context) { |
| 119 return RunFunctionAndReturnSingleResult(function, args, context, NONE); |
| 120 } |
| 121 |
| 122 base::Value* RunFunctionAndReturnSingleResult( |
| 123 UIThreadExtensionFunction* function, |
| 124 const std::string& args, |
| 125 content::BrowserContext* context, |
| 126 RunFunctionFlags flags) { |
| 127 TestFunctionDispatcherDelegate delegate; |
| 128 scoped_ptr<ExtensionFunctionDispatcher> dispatcher( |
| 129 new ExtensionFunctionDispatcher(context, &delegate)); |
| 130 |
| 131 return RunFunctionWithDelegateAndReturnSingleResult( |
| 132 function, args, context, dispatcher.Pass(), flags); |
| 133 } |
| 134 |
| 101 bool RunFunction(UIThreadExtensionFunction* function, | 135 bool RunFunction(UIThreadExtensionFunction* function, |
| 102 const std::string& args, | 136 const std::string& args, |
| 103 content::BrowserContext* context, | 137 content::BrowserContext* context, |
| 104 scoped_ptr<extensions::ExtensionFunctionDispatcher> dispatcher, | 138 scoped_ptr<extensions::ExtensionFunctionDispatcher> dispatcher, |
| 105 RunFunctionFlags flags) { | 139 RunFunctionFlags flags) { |
| 106 SendResponseDelegate response_delegate; | 140 SendResponseDelegate response_delegate; |
| 107 function->set_test_delegate(&response_delegate); | 141 function->set_test_delegate(&response_delegate); |
| 108 scoped_ptr<base::ListValue> parsed_args(ParseList(args)); | 142 scoped_ptr<base::ListValue> parsed_args(ParseList(args)); |
| 109 EXPECT_TRUE(parsed_args.get()) | 143 EXPECT_TRUE(parsed_args.get()) |
| 110 << "Could not parse extension function arguments: " << args; | 144 << "Could not parse extension function arguments: " << args; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 123 response_delegate.set_should_post_quit(true); | 157 response_delegate.set_should_post_quit(true); |
| 124 content::RunMessageLoop(); | 158 content::RunMessageLoop(); |
| 125 } | 159 } |
| 126 | 160 |
| 127 EXPECT_TRUE(response_delegate.HasResponse()); | 161 EXPECT_TRUE(response_delegate.HasResponse()); |
| 128 return response_delegate.GetResponse(); | 162 return response_delegate.GetResponse(); |
| 129 } | 163 } |
| 130 | 164 |
| 131 } // namespace api_test_utils | 165 } // namespace api_test_utils |
| 132 } // namespace extensions | 166 } // namespace extensions |
| OLD | NEW |