OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/renderer/api_bindings_system.h" | 5 #include "extensions/renderer/api_bindings_system.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/macros.h" | 8 #include "base/macros.h" |
9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 void SetUp() override { | 110 void SetUp() override { |
111 APIBindingTest::SetUp(); | 111 APIBindingTest::SetUp(); |
112 bindings_system_ = base::MakeUnique<APIBindingsSystem>( | 112 bindings_system_ = base::MakeUnique<APIBindingsSystem>( |
113 base::Bind(&RunFunctionOnGlobalAndIgnoreResult), | 113 base::Bind(&RunFunctionOnGlobalAndIgnoreResult), |
114 base::Bind(&RunFunctionOnGlobalAndReturnHandle), | 114 base::Bind(&RunFunctionOnGlobalAndReturnHandle), |
115 base::Bind(&APIBindingsSystemTestBase::GetAPISchema, | 115 base::Bind(&APIBindingsSystemTestBase::GetAPISchema, |
116 base::Unretained(this)), | 116 base::Unretained(this)), |
117 base::Bind(&APIBindingsSystemTestBase::OnAPIRequest, | 117 base::Bind(&APIBindingsSystemTestBase::OnAPIRequest, |
118 base::Unretained(this)), | 118 base::Unretained(this)), |
119 base::Bind(&APIBindingsSystemTestBase::OnEventListenersChanged, | 119 base::Bind(&APIBindingsSystemTestBase::OnEventListenersChanged, |
120 base::Unretained(this))); | 120 base::Unretained(this)), |
| 121 APILastError(APILastError::GetParent())); |
121 } | 122 } |
122 | 123 |
123 void TearDown() override { | 124 void TearDown() override { |
124 bindings_system_.reset(); | 125 bindings_system_.reset(); |
125 APIBindingTest::TearDown(); | 126 APIBindingTest::TearDown(); |
126 } | 127 } |
127 | 128 |
128 // Checks that |last_request_| exists and was provided with the | 129 // Checks that |last_request_| exists and was provided with the |
129 // |expected_name| and |expected_arguments|. | 130 // |expected_name| and |expected_arguments|. |
130 void ValidateLastRequest(const std::string& expected_name, | 131 void ValidateLastRequest(const std::string& expected_name, |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
241 " this.callbackArguments = Array.from(arguments);\n" | 242 " this.callbackArguments = Array.from(arguments);\n" |
242 "});"; | 243 "});"; |
243 CallFunctionOnObject(context, alpha_api, kTestCall); | 244 CallFunctionOnObject(context, alpha_api, kTestCall); |
244 | 245 |
245 ValidateLastRequest("alpha.functionWithCallback", "['foo']"); | 246 ValidateLastRequest("alpha.functionWithCallback", "['foo']"); |
246 | 247 |
247 const char kResponseArgsJson[] = "['response',1,{'key':42}]"; | 248 const char kResponseArgsJson[] = "['response',1,{'key':42}]"; |
248 std::unique_ptr<base::ListValue> expected_args = | 249 std::unique_ptr<base::ListValue> expected_args = |
249 ListValueFromString(kResponseArgsJson); | 250 ListValueFromString(kResponseArgsJson); |
250 bindings_system()->CompleteRequest(last_request()->request_id, | 251 bindings_system()->CompleteRequest(last_request()->request_id, |
251 *expected_args); | 252 *expected_args, std::string()); |
252 | 253 |
253 EXPECT_EQ(ReplaceSingleQuotes(kResponseArgsJson), | 254 EXPECT_EQ(ReplaceSingleQuotes(kResponseArgsJson), |
254 GetStringPropertyFromObject(context->Global(), context, | 255 GetStringPropertyFromObject(context->Global(), context, |
255 "callbackArguments")); | 256 "callbackArguments")); |
256 reset_last_request(); | 257 reset_last_request(); |
257 } | 258 } |
258 | 259 |
259 { | 260 { |
260 // Test a call with references -> response. | 261 // Test a call with references -> response. |
261 const char kTestCall[] = | 262 const char kTestCall[] = |
262 "obj.functionWithRefAndCallback({prop1: 'alpha', prop2: 42},\n" | 263 "obj.functionWithRefAndCallback({prop1: 'alpha', prop2: 42},\n" |
263 " function() {\n" | 264 " function() {\n" |
264 " this.callbackArguments = Array.from(arguments);\n" | 265 " this.callbackArguments = Array.from(arguments);\n" |
265 "});"; | 266 "});"; |
266 | 267 |
267 CallFunctionOnObject(context, alpha_api, kTestCall); | 268 CallFunctionOnObject(context, alpha_api, kTestCall); |
268 | 269 |
269 ValidateLastRequest("alpha.functionWithRefAndCallback", | 270 ValidateLastRequest("alpha.functionWithRefAndCallback", |
270 "[{'prop1':'alpha','prop2':42}]"); | 271 "[{'prop1':'alpha','prop2':42}]"); |
271 | 272 |
272 bindings_system()->CompleteRequest(last_request()->request_id, | 273 bindings_system()->CompleteRequest(last_request()->request_id, |
273 base::ListValue()); | 274 base::ListValue(), std::string()); |
274 | 275 |
275 EXPECT_EQ("[]", GetStringPropertyFromObject(context->Global(), context, | 276 EXPECT_EQ("[]", GetStringPropertyFromObject(context->Global(), context, |
276 "callbackArguments")); | 277 "callbackArguments")); |
277 reset_last_request(); | 278 reset_last_request(); |
278 } | 279 } |
279 | 280 |
280 { | 281 { |
281 // Test an event registration -> event occurrence. | 282 // Test an event registration -> event occurrence. |
282 const char kTestCall[] = | 283 const char kTestCall[] = |
283 "obj.alphaEvent.addListener(function() {\n" | 284 "obj.alphaEvent.addListener(function() {\n" |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
395 const char kTestCall[] = | 396 const char kTestCall[] = |
396 "obj.functionWithCallback('foo', function() {\n" | 397 "obj.functionWithCallback('foo', function() {\n" |
397 " this.callbackArguments = Array.from(arguments);\n" | 398 " this.callbackArguments = Array.from(arguments);\n" |
398 "});"; | 399 "});"; |
399 CallFunctionOnObject(context, alpha_api, kTestCall); | 400 CallFunctionOnObject(context, alpha_api, kTestCall); |
400 | 401 |
401 ValidateLastRequest("alpha.functionWithCallback", "['foo']"); | 402 ValidateLastRequest("alpha.functionWithCallback", "['foo']"); |
402 | 403 |
403 std::unique_ptr<base::ListValue> response = | 404 std::unique_ptr<base::ListValue> response = |
404 ListValueFromString("['alpha','beta']"); | 405 ListValueFromString("['alpha','beta']"); |
405 bindings_system()->CompleteRequest(last_request()->request_id, *response); | 406 bindings_system()->CompleteRequest(last_request()->request_id, *response, |
| 407 std::string()); |
406 | 408 |
407 EXPECT_EQ( | 409 EXPECT_EQ( |
408 "\"alpha.functionWithCallback\"", | 410 "\"alpha.functionWithCallback\"", |
409 GetStringPropertyFromObject(context->Global(), context, "methodName")); | 411 GetStringPropertyFromObject(context->Global(), context, "methodName")); |
410 EXPECT_EQ( | 412 EXPECT_EQ( |
411 "[\"alpha\",\"beta\"]", | 413 "[\"alpha\",\"beta\"]", |
412 GetStringPropertyFromObject(context->Global(), context, "results")); | 414 GetStringPropertyFromObject(context->Global(), context, "results")); |
413 EXPECT_EQ("[\"beta\"]", | 415 EXPECT_EQ("[\"beta\"]", |
414 GetStringPropertyFromObject(context->Global(), context, | 416 GetStringPropertyFromObject(context->Global(), context, |
415 "callbackArguments")); | 417 "callbackArguments")); |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
580 context, "idleState")); | 582 context, "idleState")); |
581 bindings_system()->FireEventInContext("idle.onStateChanged", context, | 583 bindings_system()->FireEventInContext("idle.onStateChanged", context, |
582 *ListValueFromString("['active']")); | 584 *ListValueFromString("['active']")); |
583 | 585 |
584 EXPECT_EQ("\"active\"", GetStringPropertyFromObject(context->Global(), | 586 EXPECT_EQ("\"active\"", GetStringPropertyFromObject(context->Global(), |
585 context, "idleState")); | 587 context, "idleState")); |
586 } | 588 } |
587 } | 589 } |
588 | 590 |
589 } // namespace extensions | 591 } // namespace extensions |
OLD | NEW |