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

Unified Diff: extensions/renderer/api_event_handler_unittest.cc

Issue 2727583004: [Extensions Bindings] Add a registerEventArgumentMassager (Closed)
Patch Set: . Created 3 years, 10 months 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 side-by-side diff with in-line comments
Download patch
Index: extensions/renderer/api_event_handler_unittest.cc
diff --git a/extensions/renderer/api_event_handler_unittest.cc b/extensions/renderer/api_event_handler_unittest.cc
index e4d39d255a8312c413ef9b2df3a6702d6f27c547..71323299662140f9c24b04523b4497f4e1e39a2b 100644
--- a/extensions/renderer/api_event_handler_unittest.cc
+++ b/extensions/renderer/api_event_handler_unittest.cc
@@ -11,6 +11,7 @@
#include "base/values.h"
#include "extensions/renderer/api_binding_test.h"
#include "extensions/renderer/api_binding_test_util.h"
+#include "gin/arguments.h"
#include "gin/converter.h"
#include "gin/public/context_holder.h"
#include "testing/gmock/include/gmock/gmock.h"
@@ -707,4 +708,71 @@ TEST_F(APIEventHandlerTest, CallbackNotifications) {
handler.GetNumEventListenersForTesting(kEventName1, context_b));
}
+// Test registering an argument massager for a given event.
+TEST_F(APIEventHandlerTest, TestArgumentMassagers) {
+ v8::HandleScope handle_scope(isolate());
+ v8::Local<v8::Context> context = ContextLocal();
+
+ const char kEventName[] = "alpha";
+ APIEventHandler handler(base::Bind(&RunFunctionOnGlobalAndIgnoreResult),
+ base::Bind(&DoNothingOnEventListenersChanged));
+ v8::Local<v8::Object> event =
+ handler.CreateEventInstance(kEventName, context);
+ ASSERT_FALSE(event.IsEmpty());
+
+ // Verifies the passed in arguments and dispatches the event with new
+ // arguments, 'primary' and 'secondary'.
+ 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.
+ gin::Arguments args(info);
+ v8::HandleScope handle_scope(args.isolate());
+ v8::Local<v8::Context> context = args.isolate()->GetCurrentContext();
+ std::vector<v8::Local<v8::Value>> event_args;
+ ASSERT_TRUE(args.GetNext(&event_args));
+ ASSERT_EQ(2u, event_args.size());
+ EXPECT_EQ("first", gin::V8ToString(event_args[0]));
+ EXPECT_EQ("second", gin::V8ToString(event_args[1]));
+
+ v8::Local<v8::Function> dispatch;
+ ASSERT_TRUE(args.GetNext(&dispatch));
+ v8::Local<v8::Array> massaged_args = v8::Array::New(args.isolate());
+ massaged_args->Set(context, 0, gin::StringToV8(args.isolate(), "primary"))
+ .ToChecked();
+ massaged_args->Set(context, 1, gin::StringToV8(args.isolate(), "secondary"))
+ .ToChecked();
+ v8::Local<v8::Value> function_args[] = {massaged_args};
+ 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.
+ function_args);
+ };
+ v8::Local<v8::FunctionTemplate> massager_template =
+ v8::FunctionTemplate::New(isolate(), argument_massager);
+ v8::Local<v8::Function> massager =
+ 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. :)
+
+ handler.RegisterArgumentMassager(context, "alpha", massager);
+
+ const char kListenerFunction[] =
+ "(function() { this.eventArgs = Array.from(arguments); })";
+ v8::Local<v8::Function> listener_function =
+ FunctionFromString(context, kListenerFunction);
+ ASSERT_FALSE(listener_function.IsEmpty());
+
+ {
+ const char kAddListenerFunction[] =
+ "(function(event, listener) { event.addListener(listener); })";
+ v8::Local<v8::Function> add_listener_function =
+ FunctionFromString(context, kAddListenerFunction);
+ v8::Local<v8::Value> argv[] = {event, listener_function};
+ RunFunction(add_listener_function, context, arraysize(argv), argv);
+ }
+
+ const char kArguments[] = "['first','second']";
+ std::unique_ptr<base::ListValue> event_args = ListValueFromString(kArguments);
+ ASSERT_TRUE(event_args);
+ handler.FireEventInContext(kEventName, context, *event_args);
+
+ EXPECT_EQ(
+ "[\"primary\",\"secondary\"]",
+ GetStringPropertyFromObject(context->Global(), context, "eventArgs"));
+}
+
} // namespace extensions

Powered by Google App Engine
This is Rietveld 408576698