| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "extensions/renderer/event_emitter.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/memory/ptr_util.h" |
| 9 #include "base/values.h" |
| 10 #include "extensions/renderer/api_binding_test.h" |
| 11 #include "extensions/renderer/api_binding_test_util.h" |
| 12 #include "extensions/renderer/api_event_listeners.h" |
| 13 #include "gin/handle.h" |
| 14 |
| 15 namespace extensions { |
| 16 namespace { |
| 17 |
| 18 void DoNothingOnListenerChange(binding::EventListenersChanged changed, |
| 19 const base::DictionaryValue* filter, |
| 20 bool was_manual, |
| 21 v8::Local<v8::Context> context) {} |
| 22 |
| 23 } // namespace |
| 24 |
| 25 using EventEmitterUnittest = APIBindingTest; |
| 26 |
| 27 TEST_F(EventEmitterUnittest, TestDispatchMethod) { |
| 28 v8::HandleScope handle_scope(isolate()); |
| 29 v8::Local<v8::Context> context = MainContext(); |
| 30 |
| 31 auto listeners = base::MakeUnique<UnfilteredEventListeners>( |
| 32 base::Bind(&DoNothingOnListenerChange), binding::kNoListenerMax); |
| 33 |
| 34 // The test util methods enforce that functions always throw or always don't |
| 35 // throw, but we test listeners that do both. Provide implementations for |
| 36 // running functions that don't enforce throw behavior. |
| 37 auto run_js_sync = [](v8::Local<v8::Function> function, |
| 38 v8::Local<v8::Context> context, int argc, |
| 39 v8::Local<v8::Value> argv[]) { |
| 40 v8::Global<v8::Value> global_result; |
| 41 v8::Local<v8::Value> result; |
| 42 if (function->Call(context, context->Global(), argc, argv).ToLocal(&result)) |
| 43 global_result.Reset(context->GetIsolate(), result); |
| 44 return global_result; |
| 45 }; |
| 46 |
| 47 auto run_js = [](v8::Local<v8::Function> function, |
| 48 v8::Local<v8::Context> context, int argc, |
| 49 v8::Local<v8::Value> argv[]) { |
| 50 ignore_result(function->Call(context, context->Global(), argc, argv)); |
| 51 }; |
| 52 |
| 53 gin::Handle<EventEmitter> event = gin::CreateHandle( |
| 54 isolate(), new EventEmitter(false, std::move(listeners), |
| 55 base::Bind(run_js), base::Bind(run_js_sync))); |
| 56 |
| 57 v8::Local<v8::Value> v8_event = event.ToV8(); |
| 58 |
| 59 const char kAddListener[] = |
| 60 "(function(event, listener) { event.addListener(listener); })"; |
| 61 v8::Local<v8::Function> add_listener_function = |
| 62 FunctionFromString(context, kAddListener); |
| 63 |
| 64 auto add_listener = [context, v8_event, |
| 65 add_listener_function](base::StringPiece listener) { |
| 66 v8::Local<v8::Function> listener_function = |
| 67 FunctionFromString(context, listener); |
| 68 v8::Local<v8::Value> args[] = {v8_event, listener_function}; |
| 69 RunFunction(add_listener_function, context, arraysize(args), args); |
| 70 }; |
| 71 |
| 72 const char kListener1[] = |
| 73 "(function() {\n" |
| 74 " this.eventArgs1 = Array.from(arguments);\n" |
| 75 " return 'listener1';\n" |
| 76 "})"; |
| 77 add_listener(kListener1); |
| 78 const char kListener2[] = |
| 79 "(function() {\n" |
| 80 " this.eventArgs2 = Array.from(arguments);\n" |
| 81 " return {listener: 'listener2'};\n" |
| 82 "})"; |
| 83 add_listener(kListener2); |
| 84 // Listener3 throws, but shouldn't stop the event from reaching other |
| 85 // listeners. |
| 86 const char kListener3[] = |
| 87 "(function() {\n" |
| 88 " this.eventArgs3 = Array.from(arguments);\n" |
| 89 " throw new Error('hahaha');\n" |
| 90 "})"; |
| 91 add_listener(kListener3); |
| 92 // Returning undefined should not be added to the array of results from |
| 93 // dispatch. |
| 94 const char kListener4[] = |
| 95 "(function() {\n" |
| 96 " this.eventArgs4 = Array.from(arguments);\n" |
| 97 "})"; |
| 98 add_listener(kListener4); |
| 99 |
| 100 const char kDispatch[] = |
| 101 "(function(event) {\n" |
| 102 " return event.dispatch('arg1', 2);\n" |
| 103 "})"; |
| 104 v8::Local<v8::Value> dispatch_args[] = {v8_event}; |
| 105 v8::Local<v8::Value> dispatch_result = |
| 106 RunFunctionOnGlobal(FunctionFromString(context, kDispatch), context, |
| 107 arraysize(dispatch_args), dispatch_args); |
| 108 |
| 109 const char kExpectedEventArgs[] = "[\"arg1\",2]"; |
| 110 for (const char* property : |
| 111 {"eventArgs1", "eventArgs2", "eventArgs3", "eventArgs4"}) { |
| 112 EXPECT_EQ(kExpectedEventArgs, GetStringPropertyFromObject( |
| 113 context->Global(), context, property)); |
| 114 } |
| 115 EXPECT_EQ("{\"results\":[\"listener1\",{\"listener\":\"listener2\"}]}", |
| 116 V8ToString(dispatch_result, context)); |
| 117 } |
| 118 |
| 119 } // namespace extensions |
| OLD | NEW |