Chromium Code Reviews| 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_event_handler.h" | 5 #include "extensions/renderer/api_event_handler.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/callback_helpers.h" | 8 #include "base/callback_helpers.h" |
| 9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
| 10 #include "base/test/mock_callback.h" | 10 #include "base/test/mock_callback.h" |
| 11 #include "base/values.h" | 11 #include "base/values.h" |
| 12 #include "extensions/renderer/api_binding_test.h" | 12 #include "extensions/renderer/api_binding_test.h" |
| 13 #include "extensions/renderer/api_binding_test_util.h" | 13 #include "extensions/renderer/api_binding_test_util.h" |
| 14 #include "gin/arguments.h" | |
| 14 #include "gin/converter.h" | 15 #include "gin/converter.h" |
| 15 #include "gin/public/context_holder.h" | 16 #include "gin/public/context_holder.h" |
| 16 #include "testing/gmock/include/gmock/gmock.h" | 17 #include "testing/gmock/include/gmock/gmock.h" |
| 17 | 18 |
| 18 namespace extensions { | 19 namespace extensions { |
| 19 | 20 |
| 20 namespace { | 21 namespace { |
| 21 | 22 |
| 22 using MockEventChangeHandler = ::testing::StrictMock< | 23 using MockEventChangeHandler = ::testing::StrictMock< |
| 23 base::MockCallback<APIEventHandler::EventListenersChangedMethod>>; | 24 base::MockCallback<APIEventHandler::EventListenersChangedMethod>>; |
| (...skipping 676 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 700 v8::Local<v8::Function> listener = | 701 v8::Local<v8::Function> listener = |
| 701 FunctionFromString(context_b, "(function() {})"); | 702 FunctionFromString(context_b, "(function() {})"); |
| 702 v8::Local<v8::Value> argv[] = {event1_b, listener}; | 703 v8::Local<v8::Value> argv[] = {event1_b, listener}; |
| 703 RunFunction(add_listener, context_b, arraysize(argv), argv); | 704 RunFunction(add_listener, context_b, arraysize(argv), argv); |
| 704 ::testing::Mock::VerifyAndClearExpectations(&change_handler); | 705 ::testing::Mock::VerifyAndClearExpectations(&change_handler); |
| 705 } | 706 } |
| 706 EXPECT_EQ(1u, | 707 EXPECT_EQ(1u, |
| 707 handler.GetNumEventListenersForTesting(kEventName1, context_b)); | 708 handler.GetNumEventListenersForTesting(kEventName1, context_b)); |
| 708 } | 709 } |
| 709 | 710 |
| 711 // Test registering an argument massager for a given event. | |
| 712 TEST_F(APIEventHandlerTest, TestArgumentMassagers) { | |
| 713 v8::HandleScope handle_scope(isolate()); | |
| 714 v8::Local<v8::Context> context = ContextLocal(); | |
| 715 | |
| 716 const char kEventName[] = "alpha"; | |
| 717 APIEventHandler handler(base::Bind(&RunFunctionOnGlobalAndIgnoreResult), | |
| 718 base::Bind(&DoNothingOnEventListenersChanged)); | |
| 719 v8::Local<v8::Object> event = | |
| 720 handler.CreateEventInstance(kEventName, context); | |
| 721 ASSERT_FALSE(event.IsEmpty()); | |
| 722 | |
| 723 // Verifies the passed in arguments and dispatches the event with new | |
| 724 // arguments, 'primary' and 'secondary'. | |
| 725 auto argument_massager = [](const v8::FunctionCallbackInfo<v8::Value>& info) { | |
|
jbroman
2017/03/01 22:33:35
This works, but elsewhere you usually use a functi
Devlin
2017/03/02 17:19:26
Translated to JS.
| |
| 726 gin::Arguments args(info); | |
| 727 v8::HandleScope handle_scope(args.isolate()); | |
| 728 v8::Local<v8::Context> context = args.isolate()->GetCurrentContext(); | |
| 729 std::vector<v8::Local<v8::Value>> event_args; | |
| 730 ASSERT_TRUE(args.GetNext(&event_args)); | |
| 731 ASSERT_EQ(2u, event_args.size()); | |
| 732 EXPECT_EQ("first", gin::V8ToString(event_args[0])); | |
| 733 EXPECT_EQ("second", gin::V8ToString(event_args[1])); | |
| 734 | |
| 735 v8::Local<v8::Function> dispatch; | |
| 736 ASSERT_TRUE(args.GetNext(&dispatch)); | |
| 737 v8::Local<v8::Array> massaged_args = v8::Array::New(args.isolate()); | |
| 738 massaged_args->Set(context, 0, gin::StringToV8(args.isolate(), "primary")) | |
| 739 .ToChecked(); | |
| 740 massaged_args->Set(context, 1, gin::StringToV8(args.isolate(), "secondary")) | |
| 741 .ToChecked(); | |
| 742 v8::Local<v8::Value> function_args[] = {massaged_args}; | |
| 743 RunFunctionOnGlobal(dispatch, context, arraysize(function_args), | |
|
jbroman
2017/03/01 22:33:35
Since you mention that the dispatch function can b
Devlin
2017/03/02 17:19:26
Added a second unit test for this.
| |
| 744 function_args); | |
| 745 }; | |
| 746 v8::Local<v8::FunctionTemplate> massager_template = | |
| 747 v8::FunctionTemplate::New(isolate(), argument_massager); | |
| 748 v8::Local<v8::Function> massager = | |
| 749 massager_template->GetFunction(context).ToLocalChecked(); | |
|
jbroman
2017/03/01 22:33:35
nit: Otherwise, might as well use v8::Function::Ne
Devlin
2017/03/02 17:19:26
Ack, but moot. :)
| |
| 750 | |
| 751 handler.RegisterArgumentMassager(context, "alpha", massager); | |
| 752 | |
| 753 const char kListenerFunction[] = | |
| 754 "(function() { this.eventArgs = Array.from(arguments); })"; | |
| 755 v8::Local<v8::Function> listener_function = | |
| 756 FunctionFromString(context, kListenerFunction); | |
| 757 ASSERT_FALSE(listener_function.IsEmpty()); | |
| 758 | |
| 759 { | |
| 760 const char kAddListenerFunction[] = | |
| 761 "(function(event, listener) { event.addListener(listener); })"; | |
| 762 v8::Local<v8::Function> add_listener_function = | |
| 763 FunctionFromString(context, kAddListenerFunction); | |
| 764 v8::Local<v8::Value> argv[] = {event, listener_function}; | |
| 765 RunFunction(add_listener_function, context, arraysize(argv), argv); | |
| 766 } | |
| 767 | |
| 768 const char kArguments[] = "['first','second']"; | |
| 769 std::unique_ptr<base::ListValue> event_args = ListValueFromString(kArguments); | |
| 770 ASSERT_TRUE(event_args); | |
| 771 handler.FireEventInContext(kEventName, context, *event_args); | |
| 772 | |
| 773 EXPECT_EQ( | |
| 774 "[\"primary\",\"secondary\"]", | |
| 775 GetStringPropertyFromObject(context->Global(), context, "eventArgs")); | |
| 776 } | |
| 777 | |
| 710 } // namespace extensions | 778 } // namespace extensions |
| OLD | NEW |