| 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" |
| (...skipping 689 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 700 v8::Local<v8::Function> listener = | 700 v8::Local<v8::Function> listener = |
| 701 FunctionFromString(context_b, "(function() {})"); | 701 FunctionFromString(context_b, "(function() {})"); |
| 702 v8::Local<v8::Value> argv[] = {event1_b, listener}; | 702 v8::Local<v8::Value> argv[] = {event1_b, listener}; |
| 703 RunFunction(add_listener, context_b, arraysize(argv), argv); | 703 RunFunction(add_listener, context_b, arraysize(argv), argv); |
| 704 ::testing::Mock::VerifyAndClearExpectations(&change_handler); | 704 ::testing::Mock::VerifyAndClearExpectations(&change_handler); |
| 705 } | 705 } |
| 706 EXPECT_EQ(1u, | 706 EXPECT_EQ(1u, |
| 707 handler.GetNumEventListenersForTesting(kEventName1, context_b)); | 707 handler.GetNumEventListenersForTesting(kEventName1, context_b)); |
| 708 } | 708 } |
| 709 | 709 |
| 710 // Tests that, when the context is invalidated, the listeners are removed. |
| 711 TEST_F(APIEventHandlerTest, TestContextInvalidation) { |
| 712 v8::HandleScope handle_scope(isolate()); |
| 713 v8::Local<v8::Context> context = ContextLocal(); |
| 714 |
| 715 MockEventChangeHandler change_handler; |
| 716 APIEventHandler handler(base::Bind(&RunFunctionOnGlobalAndIgnoreResult), |
| 717 change_handler.Get()); |
| 718 |
| 719 const char kEventName[] = "onFoo"; |
| 720 v8::Local<v8::Object> event = |
| 721 handler.CreateEventInstance(kEventName, context); |
| 722 const char kAddListenerFunction[] = |
| 723 "(function(event, listener) { event.addListener(listener); })"; |
| 724 |
| 725 // Add a listener to the first event. The APIEventHandler should notify |
| 726 // since it's a change in state (no listeners -> listeners). |
| 727 v8::Local<v8::Function> add_listener = |
| 728 FunctionFromString(context, kAddListenerFunction); |
| 729 v8::Local<v8::Function> listener = |
| 730 FunctionFromString(context, "(function() {})"); |
| 731 |
| 732 EXPECT_CALL( |
| 733 change_handler, |
| 734 Run(kEventName, binding::EventListenersChanged::HAS_LISTENERS, context)) |
| 735 .Times(1); |
| 736 v8::Local<v8::Value> argv[] = {event, listener}; |
| 737 RunFunction(add_listener, context, arraysize(argv), argv); |
| 738 ::testing::Mock::VerifyAndClearExpectations(&change_handler); |
| 739 EXPECT_EQ(1u, handler.GetNumEventListenersForTesting(kEventName, context)); |
| 740 |
| 741 // When the contexts are invalidated, we should receive listener removed |
| 742 // notifications. |
| 743 EXPECT_CALL( |
| 744 change_handler, |
| 745 Run(kEventName, binding::EventListenersChanged::NO_LISTENERS, context)) |
| 746 .Times(1); |
| 747 handler.InvalidateContext(context); |
| 748 ::testing::Mock::VerifyAndClearExpectations(&change_handler); |
| 749 } |
| 750 |
| 710 } // namespace extensions | 751 } // namespace extensions |
| OLD | NEW |