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 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
46 " }]" | 46 " }]" |
47 " }, {" | 47 " }, {" |
48 " 'name': 'functionWithRefAndCallback'," | 48 " 'name': 'functionWithRefAndCallback'," |
49 " 'parameters': [{" | 49 " 'parameters': [{" |
50 " 'name': 'ref'," | 50 " 'name': 'ref'," |
51 " '$ref': 'objRef'" | 51 " '$ref': 'objRef'" |
52 " }, {" | 52 " }, {" |
53 " 'name': 'callback'," | 53 " 'name': 'callback'," |
54 " 'type': 'function'" | 54 " 'type': 'function'" |
55 " }]" | 55 " }]" |
| 56 " }]," |
| 57 " 'events': [{" |
| 58 " 'name': 'alphaEvent'" |
56 " }]" | 59 " }]" |
57 "}"; | 60 "}"; |
58 | 61 |
59 // Another fake API for testing. | 62 // Another fake API for testing. |
60 const char kBetaAPIName[] = "beta"; | 63 const char kBetaAPIName[] = "beta"; |
61 const char kBetaAPISpec[] = | 64 const char kBetaAPISpec[] = |
62 "{" | 65 "{" |
63 " 'functions': [{" | 66 " 'functions': [{" |
64 " 'name': 'simpleFunc'," | 67 " 'name': 'simpleFunc'," |
65 " 'parameters': [{'name': 'int', 'type': 'integer'}]" | 68 " 'parameters': [{'name': 'int', 'type': 'integer'}]" |
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
263 base::ListValue()); | 266 base::ListValue()); |
264 | 267 |
265 std::unique_ptr<base::Value> result = GetBaseValuePropertyFromObject( | 268 std::unique_ptr<base::Value> result = GetBaseValuePropertyFromObject( |
266 context->Global(), context, "callbackArguments"); | 269 context->Global(), context, "callbackArguments"); |
267 ASSERT_TRUE(result); | 270 ASSERT_TRUE(result); |
268 EXPECT_EQ("[]", ValueToString(*result)); | 271 EXPECT_EQ("[]", ValueToString(*result)); |
269 reset_last_request(); | 272 reset_last_request(); |
270 } | 273 } |
271 | 274 |
272 { | 275 { |
| 276 // Test an event registration -> event occurrence. |
| 277 const char kTestCall[] = |
| 278 "obj.alphaEvent.addListener(function() {\n" |
| 279 " this.eventArguments = Array.from(arguments);\n" |
| 280 "});\n"; |
| 281 CallFunctionOnObject(context, alpha_api, kTestCall); |
| 282 |
| 283 const char kResponseArgsJson[] = "['response',1,{'key':42}]"; |
| 284 std::unique_ptr<base::ListValue> expected_args = |
| 285 ListValueFromString(kResponseArgsJson); |
| 286 bindings_system()->FireEventInContext("alpha.alphaEvent", context, |
| 287 *expected_args); |
| 288 |
| 289 std::unique_ptr<base::Value> result = GetBaseValuePropertyFromObject( |
| 290 context->Global(), context, "eventArguments"); |
| 291 ASSERT_TRUE(result); |
| 292 EXPECT_EQ(ReplaceSingleQuotes(kResponseArgsJson), ValueToString(*result)); |
| 293 } |
| 294 |
| 295 { |
273 // Test a call -> response on the second API. | 296 // Test a call -> response on the second API. |
274 const char kTestCall[] = "obj.simpleFunc(2)"; | 297 const char kTestCall[] = "obj.simpleFunc(2)"; |
275 CallFunctionOnObject(context, beta_api, kTestCall); | 298 CallFunctionOnObject(context, beta_api, kTestCall); |
276 ValidateLastRequest("beta.simpleFunc", "[2]"); | 299 ValidateLastRequest("beta.simpleFunc", "[2]"); |
277 EXPECT_TRUE(last_request()->request_id.empty()); | 300 EXPECT_TRUE(last_request()->request_id.empty()); |
278 reset_last_request(); | 301 reset_last_request(); |
279 } | 302 } |
280 } | 303 } |
281 | 304 |
282 // An implementation using real API schemas. | 305 // An implementation using real API schemas. |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
409 reset_last_request(); // Just to not pollute future results. | 432 reset_last_request(); // Just to not pollute future results. |
410 } | 433 } |
411 | 434 |
412 { | 435 { |
413 // The queryState() param has a minimum of 15. | 436 // The queryState() param has a minimum of 15. |
414 const char kTestCall[] = "chrome.idle.queryState(10, function() {});"; | 437 const char kTestCall[] = "chrome.idle.queryState(10, function() {});"; |
415 ExecuteScriptAndExpectError(context, kTestCall, kError); | 438 ExecuteScriptAndExpectError(context, kTestCall, kError); |
416 EXPECT_FALSE(last_request()); | 439 EXPECT_FALSE(last_request()); |
417 reset_last_request(); // Just to not pollute future results. | 440 reset_last_request(); // Just to not pollute future results. |
418 } | 441 } |
| 442 |
| 443 { |
| 444 const char kTestCall[] = |
| 445 "chrome.idle.onStateChanged.addListener(state => {\n" |
| 446 " this.idleState = state;\n" |
| 447 "});\n"; |
| 448 ExecuteScript(context, kTestCall); |
| 449 v8::Local<v8::Value> v8_result = |
| 450 GetPropertyFromObject(context->Global(), context, "idleState"); |
| 451 EXPECT_TRUE(v8_result->IsUndefined()); |
| 452 bindings_system()->FireEventInContext("idle.onStateChanged", context, |
| 453 *ListValueFromString("['active']")); |
| 454 |
| 455 std::unique_ptr<base::Value> result = |
| 456 GetBaseValuePropertyFromObject(context->Global(), context, "idleState"); |
| 457 ASSERT_TRUE(result); |
| 458 EXPECT_EQ("\"active\"", ValueToString(*result)); |
| 459 } |
419 } | 460 } |
420 | 461 |
421 } // namespace extensions | 462 } // namespace extensions |
OLD | NEW |