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/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
9 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
10 #include "base/values.h" | 10 #include "base/values.h" |
(...skipping 429 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
440 { | 440 { |
441 v8::Local<v8::Value> argv[] = {event}; | 441 v8::Local<v8::Value> argv[] = {event}; |
442 RunFunctionOnGlobal(fire_event_function, context, arraysize(argv), argv); | 442 RunFunctionOnGlobal(fire_event_function, context, arraysize(argv), argv); |
443 } | 443 } |
444 | 444 |
445 EXPECT_EQ("[42,\"foo\",{\"bar\":\"baz\"}]", | 445 EXPECT_EQ("[42,\"foo\",{\"bar\":\"baz\"}]", |
446 GetStringPropertyFromObject( | 446 GetStringPropertyFromObject( |
447 context->Global(), context, "eventArgs")); | 447 context->Global(), context, "eventArgs")); |
448 } | 448 } |
449 | 449 |
| 450 // Test listeners that remove themselves in their handling of the event. |
| 451 TEST_F(APIEventHandlerTest, RemovingListenersWhileHandlingEvent) { |
| 452 v8::HandleScope handle_scope(isolate()); |
| 453 v8::Local<v8::Context> context = ContextLocal(); |
| 454 |
| 455 APIEventHandler handler(base::Bind(&RunFunctionOnGlobalAndIgnoreResult)); |
| 456 const char kEventName[] = "alpha"; |
| 457 v8::Local<v8::Object> event = |
| 458 handler.CreateEventInstance(kEventName, context); |
| 459 ASSERT_FALSE(event.IsEmpty()); |
| 460 |
| 461 { |
| 462 // Cache the event object on the global in order to allow for easy removal. |
| 463 v8::Local<v8::Function> set_event_on_global = |
| 464 FunctionFromString( |
| 465 context, |
| 466 "(function(event) { this.testEvent = event; })"); |
| 467 v8::Local<v8::Value> args[] = {event}; |
| 468 RunFunctionOnGlobal(set_event_on_global, context, arraysize(args), args); |
| 469 EXPECT_EQ(event, |
| 470 GetPropertyFromObject(context->Global(), context, "testEvent")); |
| 471 } |
| 472 |
| 473 // A listener function that removes itself as a listener. |
| 474 const char kListenerFunction[] = |
| 475 "(function() {\n" |
| 476 " return function listener() {\n" |
| 477 " this.testEvent.removeListener(listener);\n" |
| 478 " };\n" |
| 479 "})();"; |
| 480 |
| 481 // Create and add a bunch of listeners. |
| 482 std::vector<v8::Local<v8::Function>> listeners; |
| 483 const size_t kNumListeners = 20u; |
| 484 listeners.reserve(kNumListeners); |
| 485 for (size_t i = 0; i < kNumListeners; ++i) |
| 486 listeners.push_back(FunctionFromString(context, kListenerFunction)); |
| 487 |
| 488 const char kAddListenerFunction[] = |
| 489 "(function(event, listener) { event.addListener(listener); })"; |
| 490 v8::Local<v8::Function> add_listener_function = |
| 491 FunctionFromString(context, kAddListenerFunction); |
| 492 |
| 493 for (const auto& listener : listeners) { |
| 494 v8::Local<v8::Value> argv[] = {event, listener}; |
| 495 RunFunctionOnGlobal(add_listener_function, context, arraysize(argv), argv); |
| 496 } |
| 497 |
| 498 // Fire the event. All listeners should be removed (and we shouldn't crash). |
| 499 EXPECT_EQ(kNumListeners, |
| 500 handler.GetNumEventListenersForTesting(kEventName, context)); |
| 501 handler.FireEventInContext(kEventName, context, base::ListValue()); |
| 502 EXPECT_EQ(0u, handler.GetNumEventListenersForTesting(kEventName, context)); |
| 503 |
| 504 // TODO(devlin): Another possible test: register listener a and listener b, |
| 505 // where a removes b and b removes a. Theoretically, only one should be |
| 506 // notified. Investigate what we currently do in JS-style bindings. |
| 507 } |
| 508 |
450 } // namespace extensions | 509 } // namespace extensions |
OLD | NEW |