Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(370)

Side by Side Diff: extensions/renderer/api_binding_unittest.cc

Issue 2563093002: [Extension Bindings] Add JS custom hook support (Closed)
Patch Set: jbroman's Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "base/bind.h" 5 #include "base/bind.h"
6 #include "base/memory/ptr_util.h" 6 #include "base/memory/ptr_util.h"
7 #include "base/stl_util.h" 7 #include "base/stl_util.h"
8 #include "base/strings/stringprintf.h" 8 #include "base/strings/stringprintf.h"
9 #include "base/values.h" 9 #include "base/values.h"
10 #include "extensions/renderer/api_binding.h" 10 #include "extensions/renderer/api_binding.h"
(...skipping 432 matching lines...) Expand 10 before | Expand all | Expand 10 after
443 // builds. 443 // builds.
444 } 444 }
445 445
446 // Tests adding custom hooks for an API method. 446 // Tests adding custom hooks for an API method.
447 TEST_F(APIBindingUnittest, TestCustomHooks) { 447 TEST_F(APIBindingUnittest, TestCustomHooks) {
448 std::unique_ptr<base::ListValue> functions = ListValueFromString(kFunctions); 448 std::unique_ptr<base::ListValue> functions = ListValueFromString(kFunctions);
449 ASSERT_TRUE(functions); 449 ASSERT_TRUE(functions);
450 ArgumentSpec::RefMap refs; 450 ArgumentSpec::RefMap refs;
451 451
452 // Register a hook for the test.oneString method. 452 // Register a hook for the test.oneString method.
453 auto hooks = base::MakeUnique<APIBindingHooks>(); 453 auto hooks = base::MakeUnique<APIBindingHooks>(
454 base::Bind(&RunFunctionOnGlobalAndIgnoreResult));
454 bool did_call = false; 455 bool did_call = false;
455 auto hook = [](bool* did_call, const binding::APISignature* signature, 456 auto hook = [](bool* did_call, const binding::APISignature* signature,
456 gin::Arguments* arguments) { 457 gin::Arguments* arguments) {
457 *did_call = true; 458 *did_call = true;
458 EXPECT_EQ(1, arguments->Length()); 459 EXPECT_EQ(1, arguments->Length());
459 std::string argument; 460 std::string argument;
460 EXPECT_TRUE(arguments->GetNext(&argument)); 461 EXPECT_TRUE(arguments->GetNext(&argument));
461 EXPECT_EQ("foo", argument); 462 EXPECT_EQ("foo", argument);
462 }; 463 };
463 hooks->RegisterHandleRequest("test.oneString", base::Bind(hook, &did_call)); 464 hooks->RegisterHandleRequest("test.oneString", base::Bind(hook, &did_call));
(...skipping 17 matching lines...) Expand all
481 v8::Local<v8::Function> func = 482 v8::Local<v8::Function> func =
482 FunctionFromString(context, "(function(obj) { obj.oneString('foo'); })"); 483 FunctionFromString(context, "(function(obj) { obj.oneString('foo'); })");
483 v8::Local<v8::Value> args[] = {binding_object}; 484 v8::Local<v8::Value> args[] = {binding_object};
484 RunFunction(func, context, 1, args); 485 RunFunction(func, context, 1, args);
485 EXPECT_TRUE(did_call); 486 EXPECT_TRUE(did_call);
486 487
487 // Other methods, like stringAndInt(), should behave normally. 488 // Other methods, like stringAndInt(), should behave normally.
488 ExpectPass(binding_object, "obj.stringAndInt('foo', 42);", "['foo',42]"); 489 ExpectPass(binding_object, "obj.stringAndInt('foo', 42);", "['foo',42]");
489 } 490 }
490 491
492 TEST_F(APIBindingUnittest, TestJSCustomHook) {
493 // Register a hook for the test.oneString method.
494 auto hooks = base::MakeUnique<APIBindingHooks>(
495 base::Bind(&RunFunctionOnGlobalAndIgnoreResult));
496
497 v8::HandleScope handle_scope(isolate());
498 v8::Local<v8::Context> context = ContextLocal();
499 const char kRegisterHook[] =
500 "(function(hooks) {\n"
501 " hooks.setHandleRequest('oneString', function() {\n"
502 " this.requestArguments = Array.from(arguments);\n"
503 " });\n"
504 "})";
505 v8::Local<v8::String> source_string =
506 gin::StringToV8(isolate(), kRegisterHook);
507 v8::Local<v8::String> source_name =
508 gin::StringToV8(isolate(), "custom_hook");
509 hooks->RegisterJsSource(
510 v8::Global<v8::String>(isolate(), source_string),
511 v8::Global<v8::String>(isolate(), source_name));
512
513 std::unique_ptr<base::ListValue> functions = ListValueFromString(kFunctions);
514 ASSERT_TRUE(functions);
515 ArgumentSpec::RefMap refs;
516
517 APIBinding binding(
518 "test", *functions, nullptr, nullptr,
519 base::Bind(&APIBindingUnittest::OnFunctionCall, base::Unretained(this)),
520 std::move(hooks), &refs);
521 EXPECT_TRUE(refs.empty());
522
523 APIEventHandler event_handler(
524 base::Bind(&RunFunctionOnGlobalAndIgnoreResult));
525 v8::Local<v8::Object> binding_object = binding.CreateInstance(
526 context, isolate(), &event_handler, base::Bind(&AllowAllAPIs));
527
528 // First try calling the oneString() method, which has a custom hook
529 // installed.
530 v8::Local<v8::Function> func =
531 FunctionFromString(context, "(function(obj) { obj.oneString('foo'); })");
532 v8::Local<v8::Value> args[] = {binding_object};
533 RunFunction(func, context, 1, args);
534
535 std::unique_ptr<base::Value> response_args =
536 GetBaseValuePropertyFromObject(context->Global(), context,
537 "requestArguments");
538 ASSERT_TRUE(response_args);
539 EXPECT_EQ("[\"foo\"]", ValueToString(*response_args));
540
541 // Other methods, like stringAndInt(), should behave normally.
542 ExpectPass(binding_object, "obj.stringAndInt('foo', 42);", "['foo',42]");
543 }
544
491 } // namespace extensions 545 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698