| 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 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 225 CallFunctionOnObject(context, alpha_api, kTestCall); | 225 CallFunctionOnObject(context, alpha_api, kTestCall); |
| 226 | 226 |
| 227 ValidateLastRequest("alpha.functionWithCallback", "['foo']"); | 227 ValidateLastRequest("alpha.functionWithCallback", "['foo']"); |
| 228 | 228 |
| 229 const char kResponseArgsJson[] = "['response',1,{'key':42}]"; | 229 const char kResponseArgsJson[] = "['response',1,{'key':42}]"; |
| 230 std::unique_ptr<base::ListValue> expected_args = | 230 std::unique_ptr<base::ListValue> expected_args = |
| 231 ListValueFromString(kResponseArgsJson); | 231 ListValueFromString(kResponseArgsJson); |
| 232 bindings_system()->CompleteRequest(last_request()->request_id, | 232 bindings_system()->CompleteRequest(last_request()->request_id, |
| 233 *expected_args); | 233 *expected_args); |
| 234 | 234 |
| 235 std::unique_ptr<base::Value> result = GetBaseValuePropertyFromObject( | 235 EXPECT_EQ(ReplaceSingleQuotes(kResponseArgsJson), |
| 236 context->Global(), context, "callbackArguments"); | 236 GetStringPropertyFromObject(context->Global(), context, |
| 237 ASSERT_TRUE(result); | 237 "callbackArguments")); |
| 238 EXPECT_EQ(ReplaceSingleQuotes(kResponseArgsJson), ValueToString(*result)); | |
| 239 reset_last_request(); | 238 reset_last_request(); |
| 240 } | 239 } |
| 241 | 240 |
| 242 { | 241 { |
| 243 // Test a call with references -> response. | 242 // Test a call with references -> response. |
| 244 const char kTestCall[] = | 243 const char kTestCall[] = |
| 245 "obj.functionWithRefAndCallback({prop1: 'alpha', prop2: 42},\n" | 244 "obj.functionWithRefAndCallback({prop1: 'alpha', prop2: 42},\n" |
| 246 " function() {\n" | 245 " function() {\n" |
| 247 " this.callbackArguments = Array.from(arguments);\n" | 246 " this.callbackArguments = Array.from(arguments);\n" |
| 248 "});"; | 247 "});"; |
| 249 | 248 |
| 250 CallFunctionOnObject(context, alpha_api, kTestCall); | 249 CallFunctionOnObject(context, alpha_api, kTestCall); |
| 251 | 250 |
| 252 ValidateLastRequest("alpha.functionWithRefAndCallback", | 251 ValidateLastRequest("alpha.functionWithRefAndCallback", |
| 253 "[{'prop1':'alpha','prop2':42}]"); | 252 "[{'prop1':'alpha','prop2':42}]"); |
| 254 | 253 |
| 255 bindings_system()->CompleteRequest(last_request()->request_id, | 254 bindings_system()->CompleteRequest(last_request()->request_id, |
| 256 base::ListValue()); | 255 base::ListValue()); |
| 257 | 256 |
| 258 std::unique_ptr<base::Value> result = GetBaseValuePropertyFromObject( | 257 EXPECT_EQ("[]", GetStringPropertyFromObject(context->Global(), context, |
| 259 context->Global(), context, "callbackArguments"); | 258 "callbackArguments")); |
| 260 ASSERT_TRUE(result); | |
| 261 EXPECT_EQ("[]", ValueToString(*result)); | |
| 262 reset_last_request(); | 259 reset_last_request(); |
| 263 } | 260 } |
| 264 | 261 |
| 265 { | 262 { |
| 266 // Test an event registration -> event occurrence. | 263 // Test an event registration -> event occurrence. |
| 267 const char kTestCall[] = | 264 const char kTestCall[] = |
| 268 "obj.alphaEvent.addListener(function() {\n" | 265 "obj.alphaEvent.addListener(function() {\n" |
| 269 " this.eventArguments = Array.from(arguments);\n" | 266 " this.eventArguments = Array.from(arguments);\n" |
| 270 "});\n"; | 267 "});\n"; |
| 271 CallFunctionOnObject(context, alpha_api, kTestCall); | 268 CallFunctionOnObject(context, alpha_api, kTestCall); |
| 272 | 269 |
| 273 const char kResponseArgsJson[] = "['response',1,{'key':42}]"; | 270 const char kResponseArgsJson[] = "['response',1,{'key':42}]"; |
| 274 std::unique_ptr<base::ListValue> expected_args = | 271 std::unique_ptr<base::ListValue> expected_args = |
| 275 ListValueFromString(kResponseArgsJson); | 272 ListValueFromString(kResponseArgsJson); |
| 276 bindings_system()->FireEventInContext("alpha.alphaEvent", context, | 273 bindings_system()->FireEventInContext("alpha.alphaEvent", context, |
| 277 *expected_args); | 274 *expected_args); |
| 278 | 275 |
| 279 std::unique_ptr<base::Value> result = GetBaseValuePropertyFromObject( | 276 EXPECT_EQ(ReplaceSingleQuotes(kResponseArgsJson), |
| 280 context->Global(), context, "eventArguments"); | 277 GetStringPropertyFromObject(context->Global(), context, |
| 281 ASSERT_TRUE(result); | 278 "eventArguments")); |
| 282 EXPECT_EQ(ReplaceSingleQuotes(kResponseArgsJson), ValueToString(*result)); | |
| 283 } | 279 } |
| 284 | 280 |
| 285 { | 281 { |
| 286 // Test a call -> response on the second API. | 282 // Test a call -> response on the second API. |
| 287 const char kTestCall[] = "obj.simpleFunc(2)"; | 283 const char kTestCall[] = "obj.simpleFunc(2)"; |
| 288 CallFunctionOnObject(context, beta_api, kTestCall); | 284 CallFunctionOnObject(context, beta_api, kTestCall); |
| 289 ValidateLastRequest("beta.simpleFunc", "[2]"); | 285 ValidateLastRequest("beta.simpleFunc", "[2]"); |
| 290 EXPECT_EQ(-1, last_request()->request_id); | 286 EXPECT_EQ(-1, last_request()->request_id); |
| 291 reset_last_request(); | 287 reset_last_request(); |
| 292 } | 288 } |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 332 | 328 |
| 333 { | 329 { |
| 334 // Test a simple call -> response. | 330 // Test a simple call -> response. |
| 335 const char kTestCall[] = | 331 const char kTestCall[] = |
| 336 "obj.functionWithCallback('foo', function() {\n" | 332 "obj.functionWithCallback('foo', function() {\n" |
| 337 " this.callbackArguments = Array.from(arguments);\n" | 333 " this.callbackArguments = Array.from(arguments);\n" |
| 338 "});"; | 334 "});"; |
| 339 CallFunctionOnObject(context, alpha_api, kTestCall); | 335 CallFunctionOnObject(context, alpha_api, kTestCall); |
| 340 EXPECT_TRUE(did_call); | 336 EXPECT_TRUE(did_call); |
| 341 | 337 |
| 342 std::unique_ptr<base::Value> result = GetBaseValuePropertyFromObject( | 338 EXPECT_EQ("[\"bar\"]", |
| 343 context->Global(), context, "callbackArguments"); | 339 GetStringPropertyFromObject(context->Global(), context, |
| 344 ASSERT_TRUE(result); | 340 "callbackArguments")); |
| 345 EXPECT_EQ("[\"bar\"]", ValueToString(*result)); | |
| 346 } | 341 } |
| 347 } | 342 } |
| 348 | 343 |
| 349 // An implementation using real API schemas. | 344 // An implementation using real API schemas. |
| 350 class APIBindingsSystemTestWithRealAPI : public APIBindingsSystemTestBase { | 345 class APIBindingsSystemTestWithRealAPI : public APIBindingsSystemTestBase { |
| 351 protected: | 346 protected: |
| 352 APIBindingsSystemTestWithRealAPI() {} | 347 APIBindingsSystemTestWithRealAPI() {} |
| 353 | 348 |
| 354 // Executes the given |script_source| in |context|, expecting no exceptions. | 349 // Executes the given |script_source| in |context|, expecting no exceptions. |
| 355 void ExecuteScript(v8::Local<v8::Context> context, | 350 void ExecuteScript(v8::Local<v8::Context> context, |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 478 EXPECT_FALSE(last_request()); | 473 EXPECT_FALSE(last_request()); |
| 479 reset_last_request(); // Just to not pollute future results. | 474 reset_last_request(); // Just to not pollute future results. |
| 480 } | 475 } |
| 481 | 476 |
| 482 { | 477 { |
| 483 const char kTestCall[] = | 478 const char kTestCall[] = |
| 484 "chrome.idle.onStateChanged.addListener(state => {\n" | 479 "chrome.idle.onStateChanged.addListener(state => {\n" |
| 485 " this.idleState = state;\n" | 480 " this.idleState = state;\n" |
| 486 "});\n"; | 481 "});\n"; |
| 487 ExecuteScript(context, kTestCall); | 482 ExecuteScript(context, kTestCall); |
| 488 v8::Local<v8::Value> v8_result = | 483 EXPECT_EQ("undefined", GetStringPropertyFromObject(context->Global(), |
| 489 GetPropertyFromObject(context->Global(), context, "idleState"); | 484 context, "idleState")); |
| 490 EXPECT_TRUE(v8_result->IsUndefined()); | |
| 491 bindings_system()->FireEventInContext("idle.onStateChanged", context, | 485 bindings_system()->FireEventInContext("idle.onStateChanged", context, |
| 492 *ListValueFromString("['active']")); | 486 *ListValueFromString("['active']")); |
| 493 | 487 |
| 494 std::unique_ptr<base::Value> result = | 488 EXPECT_EQ("\"active\"", GetStringPropertyFromObject(context->Global(), |
| 495 GetBaseValuePropertyFromObject(context->Global(), context, "idleState"); | 489 context, "idleState")); |
| 496 ASSERT_TRUE(result); | |
| 497 EXPECT_EQ("\"active\"", ValueToString(*result)); | |
| 498 } | 490 } |
| 499 } | 491 } |
| 500 | 492 |
| 501 } // namespace extensions | 493 } // namespace extensions |
| OLD | NEW |