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 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
259 base::ListValue()); | 262 base::ListValue()); |
260 | 263 |
261 std::unique_ptr<base::Value> result = GetBaseValuePropertyFromObject( | 264 std::unique_ptr<base::Value> result = GetBaseValuePropertyFromObject( |
262 context->Global(), context, "callbackArguments"); | 265 context->Global(), context, "callbackArguments"); |
263 ASSERT_TRUE(result); | 266 ASSERT_TRUE(result); |
264 EXPECT_EQ("[]", ValueToString(*result)); | 267 EXPECT_EQ("[]", ValueToString(*result)); |
265 reset_last_request(); | 268 reset_last_request(); |
266 } | 269 } |
267 | 270 |
268 { | 271 { |
| 272 // Test an event registration -> event occurrence. |
| 273 const char kTestCall[] = |
| 274 "obj.alphaEvent.addListener(function() {\n" |
| 275 " this.eventArguments = Array.from(arguments);\n" |
| 276 "});\n"; |
| 277 CallFunctionOnObject(context, alpha_api, kTestCall); |
| 278 |
| 279 const char kResponseArgsJson[] = "['response',1,{'key':42}]"; |
| 280 std::unique_ptr<base::ListValue> expected_args = |
| 281 ListValueFromString(kResponseArgsJson); |
| 282 bindings_system()->FireEventInContext("alpha.alphaEvent", context, |
| 283 *expected_args); |
| 284 |
| 285 std::unique_ptr<base::Value> result = GetBaseValuePropertyFromObject( |
| 286 context->Global(), context, "eventArguments"); |
| 287 ASSERT_TRUE(result); |
| 288 EXPECT_EQ(ReplaceSingleQuotes(kResponseArgsJson), ValueToString(*result)); |
| 289 } |
| 290 |
| 291 { |
269 // Test a call -> response on the second API. | 292 // Test a call -> response on the second API. |
270 const char kTestCall[] = "obj.simpleFunc(2)"; | 293 const char kTestCall[] = "obj.simpleFunc(2)"; |
271 CallFunctionOnObject(context, beta_api, kTestCall); | 294 CallFunctionOnObject(context, beta_api, kTestCall); |
272 ValidateLastRequest("beta.simpleFunc", "[2]"); | 295 ValidateLastRequest("beta.simpleFunc", "[2]"); |
273 EXPECT_TRUE(last_request()->request_id.empty()); | 296 EXPECT_TRUE(last_request()->request_id.empty()); |
274 reset_last_request(); | 297 reset_last_request(); |
275 } | 298 } |
276 } | 299 } |
277 | 300 |
278 // An implementation using real API schemas. | 301 // An implementation using real API schemas. |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
405 reset_last_request(); // Just to not pollute future results. | 428 reset_last_request(); // Just to not pollute future results. |
406 } | 429 } |
407 | 430 |
408 { | 431 { |
409 // The queryState() param has a minimum of 15. | 432 // The queryState() param has a minimum of 15. |
410 const char kTestCall[] = "chrome.idle.queryState(10, function() {});"; | 433 const char kTestCall[] = "chrome.idle.queryState(10, function() {});"; |
411 ExecuteScriptAndExpectError(context, kTestCall, kError); | 434 ExecuteScriptAndExpectError(context, kTestCall, kError); |
412 EXPECT_FALSE(last_request()); | 435 EXPECT_FALSE(last_request()); |
413 reset_last_request(); // Just to not pollute future results. | 436 reset_last_request(); // Just to not pollute future results. |
414 } | 437 } |
| 438 |
| 439 { |
| 440 const char kTestCall[] = |
| 441 "chrome.idle.onStateChanged.addListener(state => {\n" |
| 442 " this.idleState = state;\n" |
| 443 "});\n"; |
| 444 ExecuteScript(context, kTestCall); |
| 445 v8::Local<v8::Value> v8_result = |
| 446 GetPropertyFromObject(context->Global(), context, "idleState"); |
| 447 EXPECT_TRUE(v8_result->IsUndefined()); |
| 448 bindings_system()->FireEventInContext("idle.onStateChanged", context, |
| 449 *ListValueFromString("['active']")); |
| 450 |
| 451 std::unique_ptr<base::Value> result = |
| 452 GetBaseValuePropertyFromObject(context->Global(), context, "idleState"); |
| 453 ASSERT_TRUE(result); |
| 454 EXPECT_EQ("\"active\"", ValueToString(*result)); |
| 455 } |
415 } | 456 } |
416 | 457 |
417 } // namespace extensions | 458 } // namespace extensions |
OLD | NEW |