Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(785)

Side by Side Diff: extensions/browser/api_test_utils.cc

Issue 2348723002: [Extensions] Remove UIThreadExtensionFunction::DelegateForTests (Closed)
Patch Set: lazyboy's Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « extensions/browser/api_test_utils.h ('k') | extensions/browser/extension_function.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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"
(...skipping 13 matching lines...) Expand all
24 namespace { 24 namespace {
25 25
26 std::unique_ptr<base::Value> ParseJSON(const std::string& data) { 26 std::unique_ptr<base::Value> ParseJSON(const std::string& data) {
27 return base::JSONReader::Read(data); 27 return base::JSONReader::Read(data);
28 } 28 }
29 29
30 std::unique_ptr<base::ListValue> ParseList(const std::string& data) { 30 std::unique_ptr<base::ListValue> ParseList(const std::string& data) {
31 return base::ListValue::From(ParseJSON(data)); 31 return base::ListValue::From(ParseJSON(data));
32 } 32 }
33 33
34 // This helps us be able to wait until an UIThreadExtensionFunction calls
35 // SendResponse.
36 class SendResponseDelegate
37 : public UIThreadExtensionFunction::DelegateForTests {
38 public:
39 SendResponseDelegate() {}
40
41 virtual ~SendResponseDelegate() {}
42
43 bool HasResponse() { return response_.get() != NULL; }
44
45 bool GetResponse() {
46 EXPECT_TRUE(HasResponse());
47 return *response_;
48 }
49
50 void OnSendResponse(UIThreadExtensionFunction* function,
51 bool success,
52 bool bad_message) override {
53 ASSERT_FALSE(bad_message);
54 ASSERT_FALSE(HasResponse());
55 response_.reset(new bool);
56 *response_ = success;
57 run_loop_.Quit();
58 }
59
60 void WaitForResponse() {
61 // If the RunAsync of UIThreadExtensionFunction already called SendResponse,
62 // this will finish immediately.
63 run_loop_.Run();
64 }
65
66 private:
67 base::RunLoop run_loop_;
68 std::unique_ptr<bool> response_;
69 DISALLOW_COPY_AND_ASSIGN(SendResponseDelegate);
70 };
71
72 } // namespace 34 } // namespace
73 35
74 namespace extensions { 36 namespace extensions {
75 37
76 namespace api_test_utils { 38 namespace api_test_utils {
77 39
40 SendResponseHelper::SendResponseHelper(UIThreadExtensionFunction* function) {
41 function->set_has_callback(true);
42 function->set_response_callback(
43 base::Bind(&SendResponseHelper::OnResponse, base::Unretained(this)));
44 }
45
46 SendResponseHelper::~SendResponseHelper() {}
47
48 bool SendResponseHelper::GetResponse() {
49 EXPECT_TRUE(has_response());
50 return *response_;
51 }
52
53 void SendResponseHelper::OnResponse(ExtensionFunction::ResponseType response,
54 const base::ListValue& results,
55 const std::string& error,
56 functions::HistogramValue histogram_value) {
57 ASSERT_NE(ExtensionFunction::BAD_MESSAGE, response);
58 response_.reset(new bool(response == ExtensionFunction::SUCCEEDED));
59 run_loop_.Quit();
60 }
61
62 void SendResponseHelper::WaitForResponse() {
63 run_loop_.Run();
64 }
65
78 std::unique_ptr<base::DictionaryValue> ParseDictionary( 66 std::unique_ptr<base::DictionaryValue> ParseDictionary(
79 const std::string& data) { 67 const std::string& data) {
80 return base::DictionaryValue::From(ParseJSON(data)); 68 return base::DictionaryValue::From(ParseJSON(data));
81 } 69 }
82 70
83 bool GetBoolean(const base::DictionaryValue* val, const std::string& key) { 71 bool GetBoolean(const base::DictionaryValue* val, const std::string& key) {
84 bool result = false; 72 bool result = false;
85 if (!val->GetBoolean(key, &result)) 73 if (!val->GetBoolean(key, &result))
86 ADD_FAILURE() << key << " does not exist or is not a boolean."; 74 ADD_FAILURE() << key << " does not exist or is not a boolean.";
87 return result; 75 return result;
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
153 return RunFunctionWithDelegateAndReturnSingleResult( 141 return RunFunctionWithDelegateAndReturnSingleResult(
154 function, std::move(parsed_args), context, std::move(dispatcher), flags); 142 function, std::move(parsed_args), context, std::move(dispatcher), flags);
155 } 143 }
156 144
157 std::unique_ptr<base::Value> RunFunctionWithDelegateAndReturnSingleResult( 145 std::unique_ptr<base::Value> RunFunctionWithDelegateAndReturnSingleResult(
158 scoped_refptr<UIThreadExtensionFunction> function, 146 scoped_refptr<UIThreadExtensionFunction> function,
159 std::unique_ptr<base::ListValue> args, 147 std::unique_ptr<base::ListValue> args,
160 content::BrowserContext* context, 148 content::BrowserContext* context,
161 std::unique_ptr<extensions::ExtensionFunctionDispatcher> dispatcher, 149 std::unique_ptr<extensions::ExtensionFunctionDispatcher> dispatcher,
162 RunFunctionFlags flags) { 150 RunFunctionFlags flags) {
163 // Without a callback the function will not generate a result.
164 function->set_has_callback(true);
165 RunFunction(function.get(), std::move(args), context, std::move(dispatcher), 151 RunFunction(function.get(), std::move(args), context, std::move(dispatcher),
166 flags); 152 flags);
167 EXPECT_TRUE(function->GetError().empty()) << "Unexpected error: " 153 EXPECT_TRUE(function->GetError().empty()) << "Unexpected error: "
168 << function->GetError(); 154 << function->GetError();
169 const base::Value* single_result = NULL; 155 const base::Value* single_result = NULL;
170 if (function->GetResultList() != NULL && 156 if (function->GetResultList() != NULL &&
171 function->GetResultList()->Get(0, &single_result)) { 157 function->GetResultList()->Get(0, &single_result)) {
172 return single_result->CreateDeepCopy(); 158 return single_result->CreateDeepCopy();
173 } 159 }
174 return NULL; 160 return NULL;
(...skipping 25 matching lines...) Expand all
200 } 186 }
201 187
202 std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function, 188 std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function,
203 const std::string& args, 189 const std::string& args,
204 content::BrowserContext* context, 190 content::BrowserContext* context,
205 RunFunctionFlags flags) { 191 RunFunctionFlags flags) {
206 std::unique_ptr<ExtensionFunctionDispatcher> dispatcher( 192 std::unique_ptr<ExtensionFunctionDispatcher> dispatcher(
207 new ExtensionFunctionDispatcher(context)); 193 new ExtensionFunctionDispatcher(context));
208 scoped_refptr<ExtensionFunction> function_owner(function); 194 scoped_refptr<ExtensionFunction> function_owner(function);
209 // Without a callback the function will not generate a result. 195 // Without a callback the function will not generate a result.
210 function->set_has_callback(true);
211 RunFunction(function, args, context, std::move(dispatcher), flags); 196 RunFunction(function, args, context, std::move(dispatcher), flags);
212 EXPECT_FALSE(function->GetResultList()) << "Did not expect a result"; 197 // When sending a response, the function will set an empty list value if there
198 // is no specified result.
199 const base::ListValue* results = function->GetResultList();
200 CHECK(results);
201 EXPECT_TRUE(results->empty()) << "Did not expect a result";
202 CHECK(function->response_type());
203 EXPECT_EQ(ExtensionFunction::FAILED, *function->response_type());
213 return function->GetError(); 204 return function->GetError();
214 } 205 }
215 206
216 bool RunFunction(UIThreadExtensionFunction* function, 207 bool RunFunction(UIThreadExtensionFunction* function,
217 const std::string& args, 208 const std::string& args,
218 content::BrowserContext* context) { 209 content::BrowserContext* context) {
219 std::unique_ptr<ExtensionFunctionDispatcher> dispatcher( 210 std::unique_ptr<ExtensionFunctionDispatcher> dispatcher(
220 new ExtensionFunctionDispatcher(context)); 211 new ExtensionFunctionDispatcher(context));
221 return RunFunction(function, args, context, std::move(dispatcher), NONE); 212 return RunFunction(function, args, context, std::move(dispatcher), NONE);
222 } 213 }
(...skipping 10 matching lines...) Expand all
233 return RunFunction(function, std::move(parsed_args), context, 224 return RunFunction(function, std::move(parsed_args), context,
234 std::move(dispatcher), flags); 225 std::move(dispatcher), flags);
235 } 226 }
236 227
237 bool RunFunction( 228 bool RunFunction(
238 UIThreadExtensionFunction* function, 229 UIThreadExtensionFunction* function,
239 std::unique_ptr<base::ListValue> args, 230 std::unique_ptr<base::ListValue> args,
240 content::BrowserContext* context, 231 content::BrowserContext* context,
241 std::unique_ptr<extensions::ExtensionFunctionDispatcher> dispatcher, 232 std::unique_ptr<extensions::ExtensionFunctionDispatcher> dispatcher,
242 RunFunctionFlags flags) { 233 RunFunctionFlags flags) {
243 SendResponseDelegate response_delegate; 234 SendResponseHelper response_helper(function);
244 function->set_test_delegate(&response_delegate);
245 function->SetArgs(args.get()); 235 function->SetArgs(args.get());
246 236
247 CHECK(dispatcher); 237 CHECK(dispatcher);
248 function->set_dispatcher(dispatcher->AsWeakPtr()); 238 function->set_dispatcher(dispatcher->AsWeakPtr());
249 239
250 function->set_browser_context(context); 240 function->set_browser_context(context);
251 function->set_include_incognito(flags & INCLUDE_INCOGNITO); 241 function->set_include_incognito(flags & INCLUDE_INCOGNITO);
252 function->RunWithValidation()->Execute(); 242 function->RunWithValidation()->Execute();
253 response_delegate.WaitForResponse(); 243 response_helper.WaitForResponse();
254 244
255 EXPECT_TRUE(response_delegate.HasResponse()); 245 EXPECT_TRUE(response_helper.has_response());
256 return response_delegate.GetResponse(); 246 return response_helper.GetResponse();
257 } 247 }
258 248
259 } // namespace api_test_utils 249 } // namespace api_test_utils
260 } // namespace extensions 250 } // namespace extensions
OLDNEW
« no previous file with comments | « extensions/browser/api_test_utils.h ('k') | extensions/browser/extension_function.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698