| 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 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 101 void SetUp() override { | 101 void SetUp() override { |
| 102 APIBindingTest::SetUp(); | 102 APIBindingTest::SetUp(); |
| 103 bindings_system_ = base::MakeUnique<APIBindingsSystem>( | 103 bindings_system_ = base::MakeUnique<APIBindingsSystem>( |
| 104 base::Bind(&RunFunctionOnGlobalAndIgnoreResult), | 104 base::Bind(&RunFunctionOnGlobalAndIgnoreResult), |
| 105 base::Bind(&RunFunctionOnGlobalAndReturnHandle), | 105 base::Bind(&RunFunctionOnGlobalAndReturnHandle), |
| 106 base::Bind(&APIBindingsSystemTestBase::GetAPISchema, | 106 base::Bind(&APIBindingsSystemTestBase::GetAPISchema, |
| 107 base::Unretained(this)), | 107 base::Unretained(this)), |
| 108 base::Bind(&APIBindingsSystemTestBase::OnAPIRequest, | 108 base::Bind(&APIBindingsSystemTestBase::OnAPIRequest, |
| 109 base::Unretained(this)), | 109 base::Unretained(this)), |
| 110 base::Bind(&APIBindingsSystemTestBase::OnEventListenersChanged, | 110 base::Bind(&APIBindingsSystemTestBase::OnEventListenersChanged, |
| 111 base::Unretained(this))); | 111 base::Unretained(this)), |
| 112 APILastError(APILastError::GetParent())); |
| 112 } | 113 } |
| 113 | 114 |
| 114 void TearDown() override { | 115 void TearDown() override { |
| 115 bindings_system_.reset(); | 116 bindings_system_.reset(); |
| 116 APIBindingTest::TearDown(); | 117 APIBindingTest::TearDown(); |
| 117 } | 118 } |
| 118 | 119 |
| 119 // Checks that |last_request_| exists and was provided with the | 120 // Checks that |last_request_| exists and was provided with the |
| 120 // |expected_name| and |expected_arguments|. | 121 // |expected_name| and |expected_arguments|. |
| 121 void ValidateLastRequest(const std::string& expected_name, | 122 void ValidateLastRequest(const std::string& expected_name, |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 230 " this.callbackArguments = Array.from(arguments);\n" | 231 " this.callbackArguments = Array.from(arguments);\n" |
| 231 "});"; | 232 "});"; |
| 232 CallFunctionOnObject(context, alpha_api, kTestCall); | 233 CallFunctionOnObject(context, alpha_api, kTestCall); |
| 233 | 234 |
| 234 ValidateLastRequest("alpha.functionWithCallback", "['foo']"); | 235 ValidateLastRequest("alpha.functionWithCallback", "['foo']"); |
| 235 | 236 |
| 236 const char kResponseArgsJson[] = "['response',1,{'key':42}]"; | 237 const char kResponseArgsJson[] = "['response',1,{'key':42}]"; |
| 237 std::unique_ptr<base::ListValue> expected_args = | 238 std::unique_ptr<base::ListValue> expected_args = |
| 238 ListValueFromString(kResponseArgsJson); | 239 ListValueFromString(kResponseArgsJson); |
| 239 bindings_system()->CompleteRequest(last_request()->request_id, | 240 bindings_system()->CompleteRequest(last_request()->request_id, |
| 240 *expected_args); | 241 *expected_args, std::string()); |
| 241 | 242 |
| 242 EXPECT_EQ(ReplaceSingleQuotes(kResponseArgsJson), | 243 EXPECT_EQ(ReplaceSingleQuotes(kResponseArgsJson), |
| 243 GetStringPropertyFromObject(context->Global(), context, | 244 GetStringPropertyFromObject(context->Global(), context, |
| 244 "callbackArguments")); | 245 "callbackArguments")); |
| 245 reset_last_request(); | 246 reset_last_request(); |
| 246 } | 247 } |
| 247 | 248 |
| 248 { | 249 { |
| 249 // Test a call with references -> response. | 250 // Test a call with references -> response. |
| 250 const char kTestCall[] = | 251 const char kTestCall[] = |
| 251 "obj.functionWithRefAndCallback({prop1: 'alpha', prop2: 42},\n" | 252 "obj.functionWithRefAndCallback({prop1: 'alpha', prop2: 42},\n" |
| 252 " function() {\n" | 253 " function() {\n" |
| 253 " this.callbackArguments = Array.from(arguments);\n" | 254 " this.callbackArguments = Array.from(arguments);\n" |
| 254 "});"; | 255 "});"; |
| 255 | 256 |
| 256 CallFunctionOnObject(context, alpha_api, kTestCall); | 257 CallFunctionOnObject(context, alpha_api, kTestCall); |
| 257 | 258 |
| 258 ValidateLastRequest("alpha.functionWithRefAndCallback", | 259 ValidateLastRequest("alpha.functionWithRefAndCallback", |
| 259 "[{'prop1':'alpha','prop2':42}]"); | 260 "[{'prop1':'alpha','prop2':42}]"); |
| 260 | 261 |
| 261 bindings_system()->CompleteRequest(last_request()->request_id, | 262 bindings_system()->CompleteRequest(last_request()->request_id, |
| 262 base::ListValue()); | 263 base::ListValue(), std::string()); |
| 263 | 264 |
| 264 EXPECT_EQ("[]", GetStringPropertyFromObject(context->Global(), context, | 265 EXPECT_EQ("[]", GetStringPropertyFromObject(context->Global(), context, |
| 265 "callbackArguments")); | 266 "callbackArguments")); |
| 266 reset_last_request(); | 267 reset_last_request(); |
| 267 } | 268 } |
| 268 | 269 |
| 269 { | 270 { |
| 270 // Test an event registration -> event occurrence. | 271 // Test an event registration -> event occurrence. |
| 271 const char kTestCall[] = | 272 const char kTestCall[] = |
| 272 "obj.alphaEvent.addListener(function() {\n" | 273 "obj.alphaEvent.addListener(function() {\n" |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 384 const char kTestCall[] = | 385 const char kTestCall[] = |
| 385 "obj.functionWithCallback('foo', function() {\n" | 386 "obj.functionWithCallback('foo', function() {\n" |
| 386 " this.callbackArguments = Array.from(arguments);\n" | 387 " this.callbackArguments = Array.from(arguments);\n" |
| 387 "});"; | 388 "});"; |
| 388 CallFunctionOnObject(context, alpha_api, kTestCall); | 389 CallFunctionOnObject(context, alpha_api, kTestCall); |
| 389 | 390 |
| 390 ValidateLastRequest("alpha.functionWithCallback", "['foo']"); | 391 ValidateLastRequest("alpha.functionWithCallback", "['foo']"); |
| 391 | 392 |
| 392 std::unique_ptr<base::ListValue> response = | 393 std::unique_ptr<base::ListValue> response = |
| 393 ListValueFromString("['alpha','beta']"); | 394 ListValueFromString("['alpha','beta']"); |
| 394 bindings_system()->CompleteRequest(last_request()->request_id, *response); | 395 bindings_system()->CompleteRequest(last_request()->request_id, *response, |
| 396 std::string()); |
| 395 | 397 |
| 396 EXPECT_EQ( | 398 EXPECT_EQ( |
| 397 "\"alpha.functionWithCallback\"", | 399 "\"alpha.functionWithCallback\"", |
| 398 GetStringPropertyFromObject(context->Global(), context, "methodName")); | 400 GetStringPropertyFromObject(context->Global(), context, "methodName")); |
| 399 EXPECT_EQ( | 401 EXPECT_EQ( |
| 400 "[\"alpha\",\"beta\"]", | 402 "[\"alpha\",\"beta\"]", |
| 401 GetStringPropertyFromObject(context->Global(), context, "results")); | 403 GetStringPropertyFromObject(context->Global(), context, "results")); |
| 402 EXPECT_EQ("[\"beta\"]", | 404 EXPECT_EQ("[\"beta\"]", |
| 403 GetStringPropertyFromObject(context->Global(), context, | 405 GetStringPropertyFromObject(context->Global(), context, |
| 404 "callbackArguments")); | 406 "callbackArguments")); |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 548 context, "idleState")); | 550 context, "idleState")); |
| 549 bindings_system()->FireEventInContext("idle.onStateChanged", context, | 551 bindings_system()->FireEventInContext("idle.onStateChanged", context, |
| 550 *ListValueFromString("['active']")); | 552 *ListValueFromString("['active']")); |
| 551 | 553 |
| 552 EXPECT_EQ("\"active\"", GetStringPropertyFromObject(context->Global(), | 554 EXPECT_EQ("\"active\"", GetStringPropertyFromObject(context->Global(), |
| 553 context, "idleState")); | 555 context, "idleState")); |
| 554 } | 556 } |
| 555 } | 557 } |
| 556 | 558 |
| 557 } // namespace extensions | 559 } // namespace extensions |
| OLD | NEW |