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 <memory> | 7 #include <memory> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/callback_helpers.h" | 10 #include "base/callback_helpers.h" |
11 #include "base/json/json_reader.h" | 11 #include "base/json/json_reader.h" |
| 12 #include "base/memory/ptr_util.h" |
12 #include "base/values.h" | 13 #include "base/values.h" |
13 #include "components/crx_file/id_util.h" | 14 #include "components/crx_file/id_util.h" |
14 #include "content/public/browser/browser_context.h" | 15 #include "content/public/browser/browser_context.h" |
15 #include "content/public/test/test_utils.h" | 16 #include "content/public/test/test_utils.h" |
16 #include "extensions/browser/extension_function.h" | 17 #include "extensions/browser/extension_function.h" |
17 #include "extensions/browser/extension_function_dispatcher.h" | 18 #include "extensions/browser/extension_function_dispatcher.h" |
18 #include "extensions/common/extension_builder.h" | 19 #include "extensions/common/extension_builder.h" |
19 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
20 | 21 |
21 using extensions::ExtensionFunctionDispatcher; | 22 using extensions::ExtensionFunctionDispatcher; |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
123 std::string()); | 124 std::string()); |
124 } | 125 } |
125 | 126 |
126 scoped_refptr<Extension> CreateEmptyExtensionWithLocation( | 127 scoped_refptr<Extension> CreateEmptyExtensionWithLocation( |
127 Manifest::Location location) { | 128 Manifest::Location location) { |
128 std::unique_ptr<base::DictionaryValue> test_extension_value = | 129 std::unique_ptr<base::DictionaryValue> test_extension_value = |
129 ParseDictionary("{\"name\": \"Test\", \"version\": \"1.0\"}"); | 130 ParseDictionary("{\"name\": \"Test\", \"version\": \"1.0\"}"); |
130 return CreateExtension(location, test_extension_value.get(), std::string()); | 131 return CreateExtension(location, test_extension_value.get(), std::string()); |
131 } | 132 } |
132 | 133 |
133 base::Value* RunFunctionWithDelegateAndReturnSingleResult( | 134 std::unique_ptr<base::Value> RunFunctionWithDelegateAndReturnSingleResult( |
134 UIThreadExtensionFunction* function, | 135 scoped_refptr<UIThreadExtensionFunction> function, |
135 const std::string& args, | 136 const std::string& args, |
136 content::BrowserContext* context, | 137 content::BrowserContext* context, |
137 std::unique_ptr<extensions::ExtensionFunctionDispatcher> dispatcher) { | 138 std::unique_ptr<extensions::ExtensionFunctionDispatcher> dispatcher) { |
138 return RunFunctionWithDelegateAndReturnSingleResult( | 139 return RunFunctionWithDelegateAndReturnSingleResult( |
139 function, args, context, std::move(dispatcher), NONE); | 140 function, args, context, std::move(dispatcher), NONE); |
140 } | 141 } |
141 | 142 |
142 base::Value* RunFunctionWithDelegateAndReturnSingleResult( | 143 std::unique_ptr<base::Value> RunFunctionWithDelegateAndReturnSingleResult( |
143 UIThreadExtensionFunction* function, | 144 scoped_refptr<UIThreadExtensionFunction> function, |
144 const std::string& args, | 145 const std::string& args, |
145 content::BrowserContext* context, | 146 content::BrowserContext* context, |
146 std::unique_ptr<extensions::ExtensionFunctionDispatcher> dispatcher, | 147 std::unique_ptr<extensions::ExtensionFunctionDispatcher> dispatcher, |
147 RunFunctionFlags flags) { | 148 RunFunctionFlags flags) { |
148 scoped_refptr<ExtensionFunction> function_owner(function); | 149 std::unique_ptr<base::ListValue> parsed_args = ParseList(args); |
| 150 EXPECT_TRUE(parsed_args.get()) |
| 151 << "Could not parse extension function arguments: " << args; |
| 152 |
| 153 return RunFunctionWithDelegateAndReturnSingleResult( |
| 154 function, std::move(parsed_args), context, std::move(dispatcher), flags); |
| 155 } |
| 156 |
| 157 std::unique_ptr<base::Value> RunFunctionWithDelegateAndReturnSingleResult( |
| 158 scoped_refptr<UIThreadExtensionFunction> function, |
| 159 std::unique_ptr<base::ListValue> args, |
| 160 content::BrowserContext* context, |
| 161 std::unique_ptr<extensions::ExtensionFunctionDispatcher> dispatcher, |
| 162 RunFunctionFlags flags) { |
149 // Without a callback the function will not generate a result. | 163 // Without a callback the function will not generate a result. |
150 function->set_has_callback(true); | 164 function->set_has_callback(true); |
151 RunFunction(function, args, context, std::move(dispatcher), flags); | 165 RunFunction(function.get(), std::move(args), context, std::move(dispatcher), |
152 EXPECT_TRUE(function->GetError().empty()) | 166 flags); |
153 << "Unexpected error: " << function->GetError(); | 167 EXPECT_TRUE(function->GetError().empty()) << "Unexpected error: " |
| 168 << function->GetError(); |
154 const base::Value* single_result = NULL; | 169 const base::Value* single_result = NULL; |
155 if (function->GetResultList() != NULL && | 170 if (function->GetResultList() != NULL && |
156 function->GetResultList()->Get(0, &single_result)) { | 171 function->GetResultList()->Get(0, &single_result)) { |
157 return single_result->DeepCopy(); | 172 return single_result->CreateDeepCopy(); |
158 } | 173 } |
159 return NULL; | 174 return NULL; |
160 } | 175 } |
161 | 176 |
162 base::Value* RunFunctionAndReturnSingleResult( | 177 std::unique_ptr<base::Value> RunFunctionAndReturnSingleResult( |
163 UIThreadExtensionFunction* function, | 178 UIThreadExtensionFunction* function, |
164 const std::string& args, | 179 const std::string& args, |
165 content::BrowserContext* context) { | 180 content::BrowserContext* context) { |
166 return RunFunctionAndReturnSingleResult(function, args, context, NONE); | 181 return RunFunctionAndReturnSingleResult(function, args, context, NONE); |
167 } | 182 } |
168 | 183 |
169 base::Value* RunFunctionAndReturnSingleResult( | 184 std::unique_ptr<base::Value> RunFunctionAndReturnSingleResult( |
170 UIThreadExtensionFunction* function, | 185 UIThreadExtensionFunction* function, |
171 const std::string& args, | 186 const std::string& args, |
172 content::BrowserContext* context, | 187 content::BrowserContext* context, |
173 RunFunctionFlags flags) { | 188 RunFunctionFlags flags) { |
174 std::unique_ptr<ExtensionFunctionDispatcher> dispatcher( | 189 std::unique_ptr<ExtensionFunctionDispatcher> dispatcher( |
175 new ExtensionFunctionDispatcher(context)); | 190 new ExtensionFunctionDispatcher(context)); |
176 | 191 |
177 return RunFunctionWithDelegateAndReturnSingleResult( | 192 return RunFunctionWithDelegateAndReturnSingleResult( |
178 function, args, context, std::move(dispatcher), flags); | 193 function, args, context, std::move(dispatcher), flags); |
179 } | 194 } |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
236 function->set_include_incognito(flags & INCLUDE_INCOGNITO); | 251 function->set_include_incognito(flags & INCLUDE_INCOGNITO); |
237 function->RunWithValidation()->Execute(); | 252 function->RunWithValidation()->Execute(); |
238 response_delegate.WaitForResponse(); | 253 response_delegate.WaitForResponse(); |
239 | 254 |
240 EXPECT_TRUE(response_delegate.HasResponse()); | 255 EXPECT_TRUE(response_delegate.HasResponse()); |
241 return response_delegate.GetResponse(); | 256 return response_delegate.GetResponse(); |
242 } | 257 } |
243 | 258 |
244 } // namespace api_test_utils | 259 } // namespace api_test_utils |
245 } // namespace extensions | 260 } // namespace extensions |
OLD | NEW |