| 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/optional.h" | 10 #include "base/optional.h" |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 69 } // namespace | 69 } // namespace |
| 70 | 70 |
| 71 // Tests adding, removing, and querying event listeners by calling the | 71 // Tests adding, removing, and querying event listeners by calling the |
| 72 // associated methods on the JS object. | 72 // associated methods on the JS object. |
| 73 TEST_F(APIEventHandlerTest, AddingRemovingAndQueryingEventListeners) { | 73 TEST_F(APIEventHandlerTest, AddingRemovingAndQueryingEventListeners) { |
| 74 const char kEventName[] = "alpha"; | 74 const char kEventName[] = "alpha"; |
| 75 v8::HandleScope handle_scope(isolate()); | 75 v8::HandleScope handle_scope(isolate()); |
| 76 v8::Local<v8::Context> context = MainContext(); | 76 v8::Local<v8::Context> context = MainContext(); |
| 77 | 77 |
| 78 v8::Local<v8::Object> event = handler()->CreateEventInstance( | 78 v8::Local<v8::Object> event = handler()->CreateEventInstance( |
| 79 kEventName, false, binding::kNoListenerMax, context); | 79 kEventName, false, binding::kNoListenerMax, true, context); |
| 80 ASSERT_FALSE(event.IsEmpty()); | 80 ASSERT_FALSE(event.IsEmpty()); |
| 81 | 81 |
| 82 EXPECT_EQ(0u, handler()->GetNumEventListenersForTesting(kEventName, context)); | 82 EXPECT_EQ(0u, handler()->GetNumEventListenersForTesting(kEventName, context)); |
| 83 | 83 |
| 84 const char kListenerFunction[] = "(function() {})"; | 84 const char kListenerFunction[] = "(function() {})"; |
| 85 v8::Local<v8::Function> listener_function = | 85 v8::Local<v8::Function> listener_function = |
| 86 FunctionFromString(context, kListenerFunction); | 86 FunctionFromString(context, kListenerFunction); |
| 87 ASSERT_FALSE(listener_function.IsEmpty()); | 87 ASSERT_FALSE(listener_function.IsEmpty()); |
| 88 | 88 |
| 89 const char kAddListenerFunction[] = | 89 const char kAddListenerFunction[] = |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 168 } | 168 } |
| 169 | 169 |
| 170 // Tests listening for and firing different events. | 170 // Tests listening for and firing different events. |
| 171 TEST_F(APIEventHandlerTest, FiringEvents) { | 171 TEST_F(APIEventHandlerTest, FiringEvents) { |
| 172 const char kAlphaName[] = "alpha"; | 172 const char kAlphaName[] = "alpha"; |
| 173 const char kBetaName[] = "beta"; | 173 const char kBetaName[] = "beta"; |
| 174 v8::HandleScope handle_scope(isolate()); | 174 v8::HandleScope handle_scope(isolate()); |
| 175 v8::Local<v8::Context> context = MainContext(); | 175 v8::Local<v8::Context> context = MainContext(); |
| 176 | 176 |
| 177 v8::Local<v8::Object> alpha_event = handler()->CreateEventInstance( | 177 v8::Local<v8::Object> alpha_event = handler()->CreateEventInstance( |
| 178 kAlphaName, false, binding::kNoListenerMax, context); | 178 kAlphaName, false, binding::kNoListenerMax, true, context); |
| 179 v8::Local<v8::Object> beta_event = handler()->CreateEventInstance( | 179 v8::Local<v8::Object> beta_event = handler()->CreateEventInstance( |
| 180 kBetaName, false, binding::kNoListenerMax, context); | 180 kBetaName, false, binding::kNoListenerMax, true, context); |
| 181 ASSERT_FALSE(alpha_event.IsEmpty()); | 181 ASSERT_FALSE(alpha_event.IsEmpty()); |
| 182 ASSERT_FALSE(beta_event.IsEmpty()); | 182 ASSERT_FALSE(beta_event.IsEmpty()); |
| 183 | 183 |
| 184 const char kAlphaListenerFunction1[] = | 184 const char kAlphaListenerFunction1[] = |
| 185 "(function() {\n" | 185 "(function() {\n" |
| 186 " if (!this.alphaCount1) this.alphaCount1 = 0;\n" | 186 " if (!this.alphaCount1) this.alphaCount1 = 0;\n" |
| 187 " ++this.alphaCount1;\n" | 187 " ++this.alphaCount1;\n" |
| 188 "});\n"; | 188 "});\n"; |
| 189 v8::Local<v8::Function> alpha_listener1 = | 189 v8::Local<v8::Function> alpha_listener1 = |
| 190 FunctionFromString(context, kAlphaListenerFunction1); | 190 FunctionFromString(context, kAlphaListenerFunction1); |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 266 EXPECT_EQ(1, get_fired_count("betaCount")); | 266 EXPECT_EQ(1, get_fired_count("betaCount")); |
| 267 } | 267 } |
| 268 | 268 |
| 269 // Tests firing events with arguments. | 269 // Tests firing events with arguments. |
| 270 TEST_F(APIEventHandlerTest, EventArguments) { | 270 TEST_F(APIEventHandlerTest, EventArguments) { |
| 271 v8::HandleScope handle_scope(isolate()); | 271 v8::HandleScope handle_scope(isolate()); |
| 272 v8::Local<v8::Context> context = MainContext(); | 272 v8::Local<v8::Context> context = MainContext(); |
| 273 | 273 |
| 274 const char kEventName[] = "alpha"; | 274 const char kEventName[] = "alpha"; |
| 275 v8::Local<v8::Object> event = handler()->CreateEventInstance( | 275 v8::Local<v8::Object> event = handler()->CreateEventInstance( |
| 276 kEventName, false, binding::kNoListenerMax, context); | 276 kEventName, false, binding::kNoListenerMax, true, context); |
| 277 ASSERT_FALSE(event.IsEmpty()); | 277 ASSERT_FALSE(event.IsEmpty()); |
| 278 | 278 |
| 279 const char kListenerFunction[] = | 279 const char kListenerFunction[] = |
| 280 "(function() { this.eventArgs = Array.from(arguments); })"; | 280 "(function() { this.eventArgs = Array.from(arguments); })"; |
| 281 v8::Local<v8::Function> listener_function = | 281 v8::Local<v8::Function> listener_function = |
| 282 FunctionFromString(context, kListenerFunction); | 282 FunctionFromString(context, kListenerFunction); |
| 283 ASSERT_FALSE(listener_function.IsEmpty()); | 283 ASSERT_FALSE(listener_function.IsEmpty()); |
| 284 | 284 |
| 285 { | 285 { |
| 286 const char kAddListenerFunction[] = | 286 const char kAddListenerFunction[] = |
| (...skipping 27 matching lines...) Expand all Loading... |
| 314 | 314 |
| 315 v8::Local<v8::Function> listener_a = FunctionFromString( | 315 v8::Local<v8::Function> listener_a = FunctionFromString( |
| 316 context_a, "(function(arg) { this.eventArgs = arg + 'alpha'; })"); | 316 context_a, "(function(arg) { this.eventArgs = arg + 'alpha'; })"); |
| 317 ASSERT_FALSE(listener_a.IsEmpty()); | 317 ASSERT_FALSE(listener_a.IsEmpty()); |
| 318 v8::Local<v8::Function> listener_b = FunctionFromString( | 318 v8::Local<v8::Function> listener_b = FunctionFromString( |
| 319 context_b, "(function(arg) { this.eventArgs = arg + 'beta'; })"); | 319 context_b, "(function(arg) { this.eventArgs = arg + 'beta'; })"); |
| 320 ASSERT_FALSE(listener_b.IsEmpty()); | 320 ASSERT_FALSE(listener_b.IsEmpty()); |
| 321 | 321 |
| 322 // Create two instances of the same event in different contexts. | 322 // Create two instances of the same event in different contexts. |
| 323 v8::Local<v8::Object> event_a = handler()->CreateEventInstance( | 323 v8::Local<v8::Object> event_a = handler()->CreateEventInstance( |
| 324 kEventName, false, binding::kNoListenerMax, context_a); | 324 kEventName, false, binding::kNoListenerMax, true, context_a); |
| 325 ASSERT_FALSE(event_a.IsEmpty()); | 325 ASSERT_FALSE(event_a.IsEmpty()); |
| 326 v8::Local<v8::Object> event_b = handler()->CreateEventInstance( | 326 v8::Local<v8::Object> event_b = handler()->CreateEventInstance( |
| 327 kEventName, false, binding::kNoListenerMax, context_b); | 327 kEventName, false, binding::kNoListenerMax, true, context_b); |
| 328 ASSERT_FALSE(event_b.IsEmpty()); | 328 ASSERT_FALSE(event_b.IsEmpty()); |
| 329 | 329 |
| 330 // Add two separate listeners to the event, one in each context. | 330 // Add two separate listeners to the event, one in each context. |
| 331 const char kAddListenerFunction[] = | 331 const char kAddListenerFunction[] = |
| 332 "(function(event, listener) { event.addListener(listener); })"; | 332 "(function(event, listener) { event.addListener(listener); })"; |
| 333 { | 333 { |
| 334 v8::Local<v8::Function> add_listener_a = | 334 v8::Local<v8::Function> add_listener_a = |
| 335 FunctionFromString(context_a, kAddListenerFunction); | 335 FunctionFromString(context_a, kAddListenerFunction); |
| 336 v8::Local<v8::Value> argv[] = {event_a, listener_a}; | 336 v8::Local<v8::Value> argv[] = {event_a, listener_a}; |
| 337 RunFunction(add_listener_a, context_a, arraysize(argv), argv); | 337 RunFunction(add_listener_a, context_a, arraysize(argv), argv); |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 388 "eventArgs")); | 388 "eventArgs")); |
| 389 } | 389 } |
| 390 } | 390 } |
| 391 | 391 |
| 392 TEST_F(APIEventHandlerTest, DifferentCallingMethods) { | 392 TEST_F(APIEventHandlerTest, DifferentCallingMethods) { |
| 393 v8::HandleScope handle_scope(isolate()); | 393 v8::HandleScope handle_scope(isolate()); |
| 394 v8::Local<v8::Context> context = MainContext(); | 394 v8::Local<v8::Context> context = MainContext(); |
| 395 | 395 |
| 396 const char kEventName[] = "alpha"; | 396 const char kEventName[] = "alpha"; |
| 397 v8::Local<v8::Object> event = handler()->CreateEventInstance( | 397 v8::Local<v8::Object> event = handler()->CreateEventInstance( |
| 398 kEventName, false, binding::kNoListenerMax, context); | 398 kEventName, false, binding::kNoListenerMax, true, context); |
| 399 ASSERT_FALSE(event.IsEmpty()); | 399 ASSERT_FALSE(event.IsEmpty()); |
| 400 | 400 |
| 401 const char kAddListenerOnNull[] = | 401 const char kAddListenerOnNull[] = |
| 402 "(function(event) {\n" | 402 "(function(event) {\n" |
| 403 " event.addListener.call(null, function() {});\n" | 403 " event.addListener.call(null, function() {});\n" |
| 404 "})"; | 404 "})"; |
| 405 { | 405 { |
| 406 v8::Local<v8::Value> args[] = {event}; | 406 v8::Local<v8::Value> args[] = {event}; |
| 407 // TODO(devlin): This is the generic type error that gin throws. It's not | 407 // TODO(devlin): This is the generic type error that gin throws. It's not |
| 408 // very descriptive, nor does it match the web (which would just say e.g. | 408 // very descriptive, nor does it match the web (which would just say e.g. |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 440 context, 1, args); | 440 context, 1, args); |
| 441 } | 441 } |
| 442 EXPECT_EQ(2u, handler()->GetNumEventListenersForTesting(kEventName, context)); | 442 EXPECT_EQ(2u, handler()->GetNumEventListenersForTesting(kEventName, context)); |
| 443 } | 443 } |
| 444 | 444 |
| 445 TEST_F(APIEventHandlerTest, TestDispatchFromJs) { | 445 TEST_F(APIEventHandlerTest, TestDispatchFromJs) { |
| 446 v8::HandleScope handle_scope(isolate()); | 446 v8::HandleScope handle_scope(isolate()); |
| 447 v8::Local<v8::Context> context = MainContext(); | 447 v8::Local<v8::Context> context = MainContext(); |
| 448 | 448 |
| 449 v8::Local<v8::Object> event = handler()->CreateEventInstance( | 449 v8::Local<v8::Object> event = handler()->CreateEventInstance( |
| 450 "alpha", false, binding::kNoListenerMax, context); | 450 "alpha", false, binding::kNoListenerMax, true, context); |
| 451 ASSERT_FALSE(event.IsEmpty()); | 451 ASSERT_FALSE(event.IsEmpty()); |
| 452 | 452 |
| 453 const char kListenerFunction[] = | 453 const char kListenerFunction[] = |
| 454 "(function() {\n" | 454 "(function() {\n" |
| 455 " this.eventArgs = Array.from(arguments);\n" | 455 " this.eventArgs = Array.from(arguments);\n" |
| 456 "});"; | 456 "});"; |
| 457 v8::Local<v8::Function> listener = | 457 v8::Local<v8::Function> listener = |
| 458 FunctionFromString(context, kListenerFunction); | 458 FunctionFromString(context, kListenerFunction); |
| 459 | 459 |
| 460 const char kAddListenerFunction[] = | 460 const char kAddListenerFunction[] = |
| (...skipping 20 matching lines...) Expand all Loading... |
| 481 context->Global(), context, "eventArgs")); | 481 context->Global(), context, "eventArgs")); |
| 482 } | 482 } |
| 483 | 483 |
| 484 // Test listeners that remove themselves in their handling of the event. | 484 // Test listeners that remove themselves in their handling of the event. |
| 485 TEST_F(APIEventHandlerTest, RemovingListenersWhileHandlingEvent) { | 485 TEST_F(APIEventHandlerTest, RemovingListenersWhileHandlingEvent) { |
| 486 v8::HandleScope handle_scope(isolate()); | 486 v8::HandleScope handle_scope(isolate()); |
| 487 v8::Local<v8::Context> context = MainContext(); | 487 v8::Local<v8::Context> context = MainContext(); |
| 488 | 488 |
| 489 const char kEventName[] = "alpha"; | 489 const char kEventName[] = "alpha"; |
| 490 v8::Local<v8::Object> event = handler()->CreateEventInstance( | 490 v8::Local<v8::Object> event = handler()->CreateEventInstance( |
| 491 kEventName, false, binding::kNoListenerMax, context); | 491 kEventName, false, binding::kNoListenerMax, true, context); |
| 492 ASSERT_FALSE(event.IsEmpty()); | 492 ASSERT_FALSE(event.IsEmpty()); |
| 493 { | 493 { |
| 494 // Cache the event object on the global in order to allow for easy removal. | 494 // Cache the event object on the global in order to allow for easy removal. |
| 495 v8::Local<v8::Function> set_event_on_global = | 495 v8::Local<v8::Function> set_event_on_global = |
| 496 FunctionFromString( | 496 FunctionFromString( |
| 497 context, | 497 context, |
| 498 "(function(event) { this.testEvent = event; })"); | 498 "(function(event) { this.testEvent = event; })"); |
| 499 v8::Local<v8::Value> args[] = {event}; | 499 v8::Local<v8::Value> args[] = {event}; |
| 500 RunFunctionOnGlobal(set_event_on_global, context, arraysize(args), args); | 500 RunFunctionOnGlobal(set_event_on_global, context, arraysize(args), args); |
| 501 EXPECT_EQ(event, | 501 EXPECT_EQ(event, |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 556 | 556 |
| 557 SetHandler(base::MakeUnique<APIEventHandler>( | 557 SetHandler(base::MakeUnique<APIEventHandler>( |
| 558 base::Bind(run_js_and_expect_error), | 558 base::Bind(run_js_and_expect_error), |
| 559 base::Bind(&DoNothingOnEventListenersChanged))); | 559 base::Bind(&DoNothingOnEventListenersChanged))); |
| 560 | 560 |
| 561 v8::HandleScope handle_scope(isolate()); | 561 v8::HandleScope handle_scope(isolate()); |
| 562 v8::Local<v8::Context> context = MainContext(); | 562 v8::Local<v8::Context> context = MainContext(); |
| 563 | 563 |
| 564 const char kEventName[] = "alpha"; | 564 const char kEventName[] = "alpha"; |
| 565 v8::Local<v8::Object> event = handler()->CreateEventInstance( | 565 v8::Local<v8::Object> event = handler()->CreateEventInstance( |
| 566 kEventName, false, binding::kNoListenerMax, context); | 566 kEventName, false, binding::kNoListenerMax, true, context); |
| 567 ASSERT_FALSE(event.IsEmpty()); | 567 ASSERT_FALSE(event.IsEmpty()); |
| 568 | 568 |
| 569 bool did_throw = false; | 569 bool did_throw = false; |
| 570 auto message_listener = [](v8::Local<v8::Message> message, | 570 auto message_listener = [](v8::Local<v8::Message> message, |
| 571 v8::Local<v8::Value> data) { | 571 v8::Local<v8::Value> data) { |
| 572 ASSERT_FALSE(data.IsEmpty()); | 572 ASSERT_FALSE(data.IsEmpty()); |
| 573 ASSERT_TRUE(data->IsExternal()); | 573 ASSERT_TRUE(data->IsExternal()); |
| 574 bool* did_throw = static_cast<bool*>(data.As<v8::External>()->Value()); | 574 bool* did_throw = static_cast<bool*>(data.As<v8::External>()->Value()); |
| 575 *did_throw = true; | 575 *did_throw = true; |
| 576 EXPECT_EQ("Uncaught Error: Event handler error", | 576 EXPECT_EQ("Uncaught Error: Event handler error", |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 630 base::Bind(&RunFunctionOnGlobalAndIgnoreResult), change_handler.Get())); | 630 base::Bind(&RunFunctionOnGlobalAndIgnoreResult), change_handler.Get())); |
| 631 | 631 |
| 632 v8::HandleScope handle_scope(isolate()); | 632 v8::HandleScope handle_scope(isolate()); |
| 633 | 633 |
| 634 v8::Local<v8::Context> context_a = MainContext(); | 634 v8::Local<v8::Context> context_a = MainContext(); |
| 635 v8::Local<v8::Context> context_b = AddContext(); | 635 v8::Local<v8::Context> context_b = AddContext(); |
| 636 | 636 |
| 637 const char kEventName1[] = "onFoo"; | 637 const char kEventName1[] = "onFoo"; |
| 638 const char kEventName2[] = "onBar"; | 638 const char kEventName2[] = "onBar"; |
| 639 v8::Local<v8::Object> event1_a = handler()->CreateEventInstance( | 639 v8::Local<v8::Object> event1_a = handler()->CreateEventInstance( |
| 640 kEventName1, false, binding::kNoListenerMax, context_a); | 640 kEventName1, false, binding::kNoListenerMax, true, context_a); |
| 641 ASSERT_FALSE(event1_a.IsEmpty()); | 641 ASSERT_FALSE(event1_a.IsEmpty()); |
| 642 v8::Local<v8::Object> event2_a = handler()->CreateEventInstance( | 642 v8::Local<v8::Object> event2_a = handler()->CreateEventInstance( |
| 643 kEventName2, false, binding::kNoListenerMax, context_a); | 643 kEventName2, false, binding::kNoListenerMax, true, context_a); |
| 644 ASSERT_FALSE(event2_a.IsEmpty()); | 644 ASSERT_FALSE(event2_a.IsEmpty()); |
| 645 v8::Local<v8::Object> event1_b = handler()->CreateEventInstance( | 645 v8::Local<v8::Object> event1_b = handler()->CreateEventInstance( |
| 646 kEventName1, false, binding::kNoListenerMax, context_b); | 646 kEventName1, false, binding::kNoListenerMax, true, context_b); |
| 647 ASSERT_FALSE(event1_b.IsEmpty()); | 647 ASSERT_FALSE(event1_b.IsEmpty()); |
| 648 | 648 |
| 649 const char kAddListenerFunction[] = | 649 const char kAddListenerFunction[] = |
| 650 "(function(event, listener) { event.addListener(listener); })"; | 650 "(function(event, listener) { event.addListener(listener); })"; |
| 651 const char kRemoveListenerFunction[] = | 651 const char kRemoveListenerFunction[] = |
| 652 "(function(event, listener) { event.removeListener(listener); })"; | 652 "(function(event, listener) { event.removeListener(listener); })"; |
| 653 | 653 |
| 654 // Add a listener to the first event. The APIEventHandler should notify | 654 // Add a listener to the first event. The APIEventHandler should notify |
| 655 // since it's a change in state (no listeners -> listeners). | 655 // since it's a change in state (no listeners -> listeners). |
| 656 v8::Local<v8::Function> add_listener = | 656 v8::Local<v8::Function> add_listener = |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 758 ::testing::Mock::VerifyAndClearExpectations(&change_handler); | 758 ::testing::Mock::VerifyAndClearExpectations(&change_handler); |
| 759 } | 759 } |
| 760 | 760 |
| 761 // Test registering an argument massager for a given event. | 761 // Test registering an argument massager for a given event. |
| 762 TEST_F(APIEventHandlerTest, TestArgumentMassagers) { | 762 TEST_F(APIEventHandlerTest, TestArgumentMassagers) { |
| 763 v8::HandleScope handle_scope(isolate()); | 763 v8::HandleScope handle_scope(isolate()); |
| 764 v8::Local<v8::Context> context = MainContext(); | 764 v8::Local<v8::Context> context = MainContext(); |
| 765 | 765 |
| 766 const char kEventName[] = "alpha"; | 766 const char kEventName[] = "alpha"; |
| 767 v8::Local<v8::Object> event = handler()->CreateEventInstance( | 767 v8::Local<v8::Object> event = handler()->CreateEventInstance( |
| 768 kEventName, false, binding::kNoListenerMax, context); | 768 kEventName, false, binding::kNoListenerMax, true, context); |
| 769 ASSERT_FALSE(event.IsEmpty()); | 769 ASSERT_FALSE(event.IsEmpty()); |
| 770 | 770 |
| 771 const char kArgumentMassager[] = | 771 const char kArgumentMassager[] = |
| 772 "(function(originalArgs, dispatch) {\n" | 772 "(function(originalArgs, dispatch) {\n" |
| 773 " this.originalArgs = originalArgs;\n" | 773 " this.originalArgs = originalArgs;\n" |
| 774 " dispatch(['primary', 'secondary']);\n" | 774 " dispatch(['primary', 'secondary']);\n" |
| 775 "});"; | 775 "});"; |
| 776 v8::Local<v8::Function> massager = | 776 v8::Local<v8::Function> massager = |
| 777 FunctionFromString(context, kArgumentMassager); | 777 FunctionFromString(context, kArgumentMassager); |
| 778 handler()->RegisterArgumentMassager(context, "alpha", massager); | 778 handler()->RegisterArgumentMassager(context, "alpha", massager); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 807 } | 807 } |
| 808 | 808 |
| 809 // Test registering an argument massager for a given event and dispatching | 809 // Test registering an argument massager for a given event and dispatching |
| 810 // asynchronously. | 810 // asynchronously. |
| 811 TEST_F(APIEventHandlerTest, TestArgumentMassagersAsyncDispatch) { | 811 TEST_F(APIEventHandlerTest, TestArgumentMassagersAsyncDispatch) { |
| 812 v8::HandleScope handle_scope(isolate()); | 812 v8::HandleScope handle_scope(isolate()); |
| 813 v8::Local<v8::Context> context = MainContext(); | 813 v8::Local<v8::Context> context = MainContext(); |
| 814 | 814 |
| 815 const char kEventName[] = "alpha"; | 815 const char kEventName[] = "alpha"; |
| 816 v8::Local<v8::Object> event = handler()->CreateEventInstance( | 816 v8::Local<v8::Object> event = handler()->CreateEventInstance( |
| 817 kEventName, false, binding::kNoListenerMax, context); | 817 kEventName, false, binding::kNoListenerMax, true, context); |
| 818 ASSERT_FALSE(event.IsEmpty()); | 818 ASSERT_FALSE(event.IsEmpty()); |
| 819 | 819 |
| 820 const char kArgumentMassager[] = | 820 const char kArgumentMassager[] = |
| 821 "(function(originalArgs, dispatch) {\n" | 821 "(function(originalArgs, dispatch) {\n" |
| 822 " this.originalArgs = originalArgs;\n" | 822 " this.originalArgs = originalArgs;\n" |
| 823 " this.dispatch = dispatch;\n" | 823 " this.dispatch = dispatch;\n" |
| 824 "});"; | 824 "});"; |
| 825 v8::Local<v8::Function> massager = | 825 v8::Local<v8::Function> massager = |
| 826 FunctionFromString(context, kArgumentMassager); | 826 FunctionFromString(context, kArgumentMassager); |
| 827 handler()->RegisterArgumentMassager(context, "alpha", massager); | 827 handler()->RegisterArgumentMassager(context, "alpha", massager); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 871 GetStringPropertyFromObject(context->Global(), context, "eventArgs")); | 871 GetStringPropertyFromObject(context->Global(), context, "eventArgs")); |
| 872 } | 872 } |
| 873 | 873 |
| 874 // Test registering an argument massager and never dispatching. | 874 // Test registering an argument massager and never dispatching. |
| 875 TEST_F(APIEventHandlerTest, TestArgumentMassagersNeverDispatch) { | 875 TEST_F(APIEventHandlerTest, TestArgumentMassagersNeverDispatch) { |
| 876 v8::HandleScope handle_scope(isolate()); | 876 v8::HandleScope handle_scope(isolate()); |
| 877 v8::Local<v8::Context> context = MainContext(); | 877 v8::Local<v8::Context> context = MainContext(); |
| 878 | 878 |
| 879 const char kEventName[] = "alpha"; | 879 const char kEventName[] = "alpha"; |
| 880 v8::Local<v8::Object> event = handler()->CreateEventInstance( | 880 v8::Local<v8::Object> event = handler()->CreateEventInstance( |
| 881 kEventName, false, binding::kNoListenerMax, context); | 881 kEventName, false, binding::kNoListenerMax, true, context); |
| 882 ASSERT_FALSE(event.IsEmpty()); | 882 ASSERT_FALSE(event.IsEmpty()); |
| 883 | 883 |
| 884 // A massager that never dispatches. | 884 // A massager that never dispatches. |
| 885 const char kArgumentMassager[] = "(function(originalArgs, dispatch) {})"; | 885 const char kArgumentMassager[] = "(function(originalArgs, dispatch) {})"; |
| 886 v8::Local<v8::Function> massager = | 886 v8::Local<v8::Function> massager = |
| 887 FunctionFromString(context, kArgumentMassager); | 887 FunctionFromString(context, kArgumentMassager); |
| 888 handler()->RegisterArgumentMassager(context, "alpha", massager); | 888 handler()->RegisterArgumentMassager(context, "alpha", massager); |
| 889 | 889 |
| 890 const char kListenerFunction[] = "(function() {})"; | 890 const char kListenerFunction[] = "(function() {})"; |
| 891 v8::Local<v8::Function> listener_function = | 891 v8::Local<v8::Function> listener_function = |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 971 "(function(event) {\n" | 971 "(function(event) {\n" |
| 972 " event.addListener(function() {}.bind(null, event));\n" | 972 " event.addListener(function() {}.bind(null, event));\n" |
| 973 "})"; | 973 "})"; |
| 974 v8::Local<v8::Value> add_listener_argv[] = {event}; | 974 v8::Local<v8::Value> add_listener_argv[] = {event}; |
| 975 RunFunction(FunctionFromString(context, kAddListenerFunction), context, | 975 RunFunction(FunctionFromString(context, kAddListenerFunction), context, |
| 976 arraysize(add_listener_argv), add_listener_argv); | 976 arraysize(add_listener_argv), add_listener_argv); |
| 977 | 977 |
| 978 DisposeContext(context); | 978 DisposeContext(context); |
| 979 } | 979 } |
| 980 | 980 |
| 981 TEST_F(APIEventHandlerTest, TestUnmanagedEvents) { |
| 982 v8::HandleScope handle_scope(isolate()); |
| 983 v8::Local<v8::Context> context = MainContext(); |
| 984 |
| 985 auto fail_on_notified = |
| 986 [](const std::string& event_name, binding::EventListenersChanged changed, |
| 987 const base::DictionaryValue* filter, bool was_manual, |
| 988 v8::Local<v8::Context> context) { ADD_FAILURE(); }; |
| 989 |
| 990 APIEventHandler handler(base::Bind(&RunFunctionOnGlobalAndIgnoreResult), |
| 991 base::Bind(fail_on_notified)); |
| 992 |
| 993 const char kEventName[] = "alpha"; |
| 994 v8::Local<v8::Object> event = handler.CreateEventInstance( |
| 995 kEventName, false, binding::kNoListenerMax, false, context); |
| 996 |
| 997 const char kListener[] = |
| 998 "(function() {\n" |
| 999 " this.eventArgs = Array.from(arguments);\n" |
| 1000 "});"; |
| 1001 v8::Local<v8::Function> listener = FunctionFromString(context, kListener); |
| 1002 |
| 1003 { |
| 1004 const char kAddListener[] = |
| 1005 "(function(event, listener) { event.addListener(listener); })"; |
| 1006 v8::Local<v8::Value> args[] = {event, listener}; |
| 1007 RunFunction(FunctionFromString(context, kAddListener), context, |
| 1008 arraysize(args), args); |
| 1009 } |
| 1010 |
| 1011 EXPECT_EQ(1u, handler.GetNumEventListenersForTesting(kEventName, context)); |
| 1012 |
| 1013 handler.FireEventInContext(kEventName, context, |
| 1014 *ListValueFromString("[1, 'foo']"), |
| 1015 EventFilteringInfo()); |
| 1016 |
| 1017 EXPECT_EQ("[1,\"foo\"]", GetStringPropertyFromObject(context->Global(), |
| 1018 context, "eventArgs")); |
| 1019 |
| 1020 { |
| 1021 const char kRemoveListener[] = |
| 1022 "(function(event, listener) { event.removeListener(listener); })"; |
| 1023 v8::Local<v8::Value> args[] = {event, listener}; |
| 1024 RunFunction(FunctionFromString(context, kRemoveListener), context, |
| 1025 arraysize(args), args); |
| 1026 } |
| 1027 |
| 1028 EXPECT_EQ(0u, handler.GetNumEventListenersForTesting(kEventName, context)); |
| 1029 } |
| 1030 |
| 981 } // namespace extensions | 1031 } // namespace extensions |
| OLD | NEW |