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 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
198 return RunFunction(func, context, 1, argv); | 198 return RunFunction(func, context, 1, argv); |
199 } | 199 } |
200 | 200 |
201 // Tests API object initialization, calling a method on the supplied APIs, and | 201 // Tests API object initialization, calling a method on the supplied APIs, and |
202 // triggering the callback for the request. | 202 // triggering the callback for the request. |
203 TEST_F(APIBindingsSystemTest, TestInitializationAndCallbacks) { | 203 TEST_F(APIBindingsSystemTest, TestInitializationAndCallbacks) { |
204 v8::HandleScope handle_scope(isolate()); | 204 v8::HandleScope handle_scope(isolate()); |
205 v8::Local<v8::Context> context = MainContext(); | 205 v8::Local<v8::Context> context = MainContext(); |
206 | 206 |
207 v8::Local<v8::Object> alpha_api = bindings_system()->CreateAPIInstance( | 207 v8::Local<v8::Object> alpha_api = bindings_system()->CreateAPIInstance( |
208 kAlphaAPIName, context, isolate(), base::Bind(&AllowAllAPIs), nullptr); | 208 kAlphaAPIName, context, base::Bind(&AllowAllAPIs), nullptr); |
209 ASSERT_FALSE(alpha_api.IsEmpty()); | 209 ASSERT_FALSE(alpha_api.IsEmpty()); |
210 v8::Local<v8::Object> beta_api = bindings_system()->CreateAPIInstance( | 210 v8::Local<v8::Object> beta_api = bindings_system()->CreateAPIInstance( |
211 kBetaAPIName, context, isolate(), base::Bind(&AllowAllAPIs), nullptr); | 211 kBetaAPIName, context, base::Bind(&AllowAllAPIs), nullptr); |
212 ASSERT_FALSE(beta_api.IsEmpty()); | 212 ASSERT_FALSE(beta_api.IsEmpty()); |
213 | 213 |
214 { | 214 { |
215 // Test a simple call -> response. | 215 // Test a simple call -> response. |
216 const char kTestCall[] = | 216 const char kTestCall[] = |
217 "obj.functionWithCallback('foo', function() {\n" | 217 "obj.functionWithCallback('foo', function() {\n" |
218 " this.callbackArguments = Array.from(arguments);\n" | 218 " this.callbackArguments = Array.from(arguments);\n" |
219 "});"; | 219 "});"; |
220 CallFunctionOnObject(context, alpha_api, kTestCall); | 220 CallFunctionOnObject(context, alpha_api, kTestCall); |
221 | 221 |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
327 }; | 327 }; |
328 | 328 |
329 auto test_hooks = base::MakeUnique<APIBindingHooksTestDelegate>(); | 329 auto test_hooks = base::MakeUnique<APIBindingHooksTestDelegate>(); |
330 test_hooks->AddHandler("alpha.functionWithCallback", | 330 test_hooks->AddHandler("alpha.functionWithCallback", |
331 base::Bind(hook, &did_call)); | 331 base::Bind(hook, &did_call)); |
332 APIBindingHooks* binding_hooks = | 332 APIBindingHooks* binding_hooks = |
333 bindings_system()->GetHooksForAPI(kAlphaAPIName); | 333 bindings_system()->GetHooksForAPI(kAlphaAPIName); |
334 binding_hooks->SetDelegate(std::move(test_hooks)); | 334 binding_hooks->SetDelegate(std::move(test_hooks)); |
335 | 335 |
336 v8::Local<v8::Object> alpha_api = bindings_system()->CreateAPIInstance( | 336 v8::Local<v8::Object> alpha_api = bindings_system()->CreateAPIInstance( |
337 kAlphaAPIName, context, isolate(), base::Bind(&AllowAllAPIs), nullptr); | 337 kAlphaAPIName, context, base::Bind(&AllowAllAPIs), nullptr); |
338 ASSERT_FALSE(alpha_api.IsEmpty()); | 338 ASSERT_FALSE(alpha_api.IsEmpty()); |
339 | 339 |
340 { | 340 { |
341 // Test a simple call -> response. | 341 // Test a simple call -> response. |
342 const char kTestCall[] = | 342 const char kTestCall[] = |
343 "obj.functionWithCallback('foo', function() {\n" | 343 "obj.functionWithCallback('foo', function() {\n" |
344 " this.callbackArguments = Array.from(arguments);\n" | 344 " this.callbackArguments = Array.from(arguments);\n" |
345 "});"; | 345 "});"; |
346 CallFunctionOnObject(context, alpha_api, kTestCall); | 346 CallFunctionOnObject(context, alpha_api, kTestCall); |
347 EXPECT_TRUE(did_call); | 347 EXPECT_TRUE(did_call); |
(...skipping 17 matching lines...) Expand all Loading... |
365 " this.methodName = name;\n" | 365 " this.methodName = name;\n" |
366 // TODO(devlin): Currently, we don't actually pass anything useful in for | 366 // TODO(devlin): Currently, we don't actually pass anything useful in for |
367 // the |request| object. If/when we do, we should test it. | 367 // the |request| object. If/when we do, we should test it. |
368 " this.results = [firstResult, secondResult];\n" | 368 " this.results = [firstResult, secondResult];\n" |
369 " originalCallback(secondResult);\n" | 369 " originalCallback(secondResult);\n" |
370 " });\n" | 370 " });\n" |
371 "})"; | 371 "})"; |
372 | 372 |
373 APIBindingHooks* hooks = nullptr; | 373 APIBindingHooks* hooks = nullptr; |
374 v8::Local<v8::Object> alpha_api = bindings_system()->CreateAPIInstance( | 374 v8::Local<v8::Object> alpha_api = bindings_system()->CreateAPIInstance( |
375 kAlphaAPIName, context, isolate(), base::Bind(&AllowAllAPIs), &hooks); | 375 kAlphaAPIName, context, base::Bind(&AllowAllAPIs), &hooks); |
376 ASSERT_FALSE(alpha_api.IsEmpty()); | 376 ASSERT_FALSE(alpha_api.IsEmpty()); |
377 ASSERT_TRUE(hooks); | 377 ASSERT_TRUE(hooks); |
378 v8::Local<v8::Object> js_hooks = hooks->GetJSHookInterface(context); | 378 v8::Local<v8::Object> js_hooks = hooks->GetJSHookInterface(context); |
379 v8::Local<v8::Function> function = FunctionFromString(context, kHook); | 379 v8::Local<v8::Function> function = FunctionFromString(context, kHook); |
380 v8::Local<v8::Value> args[] = {js_hooks}; | 380 v8::Local<v8::Value> args[] = {js_hooks}; |
381 RunFunctionOnGlobal(function, context, arraysize(args), args); | 381 RunFunctionOnGlobal(function, context, arraysize(args), args); |
382 | 382 |
383 { | 383 { |
384 const char kTestCall[] = | 384 const char kTestCall[] = |
385 "obj.functionWithCallback('foo', function() {\n" | 385 "obj.functionWithCallback('foo', function() {\n" |
(...skipping 22 matching lines...) Expand all Loading... |
408 | 408 |
409 // Test that references to other API's types works. | 409 // Test that references to other API's types works. |
410 TEST_F(APIBindingsSystemTest, CrossAPIReferences) { | 410 TEST_F(APIBindingsSystemTest, CrossAPIReferences) { |
411 v8::HandleScope handle_scope(isolate()); | 411 v8::HandleScope handle_scope(isolate()); |
412 v8::Local<v8::Context> context = MainContext(); | 412 v8::Local<v8::Context> context = MainContext(); |
413 | 413 |
414 // Instantiate gamma API. Note: It's important that we haven't instantiated | 414 // Instantiate gamma API. Note: It's important that we haven't instantiated |
415 // alpha API yet, since this tests that we can lazily populate the type | 415 // alpha API yet, since this tests that we can lazily populate the type |
416 // information. | 416 // information. |
417 v8::Local<v8::Object> gamma_api = bindings_system()->CreateAPIInstance( | 417 v8::Local<v8::Object> gamma_api = bindings_system()->CreateAPIInstance( |
418 kGammaAPIName, context, isolate(), base::Bind(&AllowAllAPIs), nullptr); | 418 kGammaAPIName, context, base::Bind(&AllowAllAPIs), nullptr); |
419 ASSERT_FALSE(gamma_api.IsEmpty()); | 419 ASSERT_FALSE(gamma_api.IsEmpty()); |
420 | 420 |
421 { | 421 { |
422 // Test a simple call -> response. | 422 // Test a simple call -> response. |
423 const char kTestCall[] = "obj.functionWithExternalRef({prop1: 'foo'});"; | 423 const char kTestCall[] = "obj.functionWithExternalRef({prop1: 'foo'});"; |
424 CallFunctionOnObject(context, gamma_api, kTestCall); | 424 CallFunctionOnObject(context, gamma_api, kTestCall); |
425 ValidateLastRequest("gamma.functionWithExternalRef", "[{'prop1':'foo'}]"); | 425 ValidateLastRequest("gamma.functionWithExternalRef", "[{'prop1':'foo'}]"); |
426 reset_last_request(); | 426 reset_last_request(); |
427 } | 427 } |
428 } | 428 } |
(...skipping 13 matching lines...) Expand all Loading... |
442 return ret.As<v8::Value>(); | 442 return ret.As<v8::Value>(); |
443 }; | 443 }; |
444 | 444 |
445 auto test_hooks = base::MakeUnique<APIBindingHooksTestDelegate>(); | 445 auto test_hooks = base::MakeUnique<APIBindingHooksTestDelegate>(); |
446 test_hooks->SetCustomEvent(base::Bind(create_custom_event)); | 446 test_hooks->SetCustomEvent(base::Bind(create_custom_event)); |
447 APIBindingHooks* binding_hooks = | 447 APIBindingHooks* binding_hooks = |
448 bindings_system()->GetHooksForAPI(kAlphaAPIName); | 448 bindings_system()->GetHooksForAPI(kAlphaAPIName); |
449 binding_hooks->SetDelegate(std::move(test_hooks)); | 449 binding_hooks->SetDelegate(std::move(test_hooks)); |
450 | 450 |
451 v8::Local<v8::Object> api = bindings_system()->CreateAPIInstance( | 451 v8::Local<v8::Object> api = bindings_system()->CreateAPIInstance( |
452 kAlphaAPIName, context, isolate(), base::Bind(&AllowAllAPIs), nullptr); | 452 kAlphaAPIName, context, base::Bind(&AllowAllAPIs), nullptr); |
453 | 453 |
454 v8::Local<v8::Value> event = | 454 v8::Local<v8::Value> event = |
455 GetPropertyFromObject(api, context, "alphaEvent"); | 455 GetPropertyFromObject(api, context, "alphaEvent"); |
456 ASSERT_TRUE(event->IsObject()); | 456 ASSERT_TRUE(event->IsObject()); |
457 EXPECT_EQ( | 457 EXPECT_EQ( |
458 "\"alpha.alphaEvent\"", | 458 "\"alpha.alphaEvent\"", |
459 GetStringPropertyFromObject(event.As<v8::Object>(), context, "name")); | 459 GetStringPropertyFromObject(event.As<v8::Object>(), context, "name")); |
460 v8::Local<v8::Value> event2 = | 460 v8::Local<v8::Value> event2 = |
461 GetPropertyFromObject(api, context, "alphaEvent"); | 461 GetPropertyFromObject(api, context, "alphaEvent"); |
462 EXPECT_EQ(event, event2); | 462 EXPECT_EQ(event, event2); |
463 | 463 |
464 v8::Local<v8::Value> other_event = | 464 v8::Local<v8::Value> other_event = |
465 GetPropertyFromObject(api, context, "alphaOtherEvent"); | 465 GetPropertyFromObject(api, context, "alphaOtherEvent"); |
466 ASSERT_TRUE(other_event->IsObject()); | 466 ASSERT_TRUE(other_event->IsObject()); |
467 EXPECT_EQ("\"alpha.alphaOtherEvent\"", | 467 EXPECT_EQ("\"alpha.alphaOtherEvent\"", |
468 GetStringPropertyFromObject(other_event.As<v8::Object>(), context, | 468 GetStringPropertyFromObject(other_event.As<v8::Object>(), context, |
469 "name")); | 469 "name")); |
470 EXPECT_NE(event, other_event); | 470 EXPECT_NE(event, other_event); |
471 } | 471 } |
472 | 472 |
473 } // namespace extensions | 473 } // namespace extensions |
OLD | NEW |