| 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..c391d242180bcf4dfee0f81fce6031a44aa19be3 100644
|
| --- a/extensions/renderer/api_event_handler_unittest.cc
|
| +++ b/extensions/renderer/api_event_handler_unittest.cc
|
| @@ -707,4 +707,45 @@ TEST_F(APIEventHandlerTest, CallbackNotifications) {
|
| handler.GetNumEventListenersForTesting(kEventName1, context_b));
|
| }
|
|
|
| +// Tests that, when the context is invalidated, the listeners are removed.
|
| +TEST_F(APIEventHandlerTest, TestContextInvalidation) {
|
| + v8::HandleScope handle_scope(isolate());
|
| + v8::Local<v8::Context> context = ContextLocal();
|
| +
|
| + MockEventChangeHandler change_handler;
|
| + APIEventHandler handler(base::Bind(&RunFunctionOnGlobalAndIgnoreResult),
|
| + change_handler.Get());
|
| +
|
| + const char kEventName[] = "onFoo";
|
| + v8::Local<v8::Object> event =
|
| + handler.CreateEventInstance(kEventName, context);
|
| + const char kAddListenerFunction[] =
|
| + "(function(event, listener) { event.addListener(listener); })";
|
| +
|
| + // Add a listener to the first event. The APIEventHandler should notify
|
| + // since it's a change in state (no listeners -> listeners).
|
| + v8::Local<v8::Function> add_listener =
|
| + FunctionFromString(context, kAddListenerFunction);
|
| + v8::Local<v8::Function> listener =
|
| + FunctionFromString(context, "(function() {})");
|
| +
|
| + EXPECT_CALL(
|
| + change_handler,
|
| + Run(kEventName, binding::EventListenersChanged::HAS_LISTENERS, context))
|
| + .Times(1);
|
| + v8::Local<v8::Value> argv[] = {event, listener};
|
| + RunFunction(add_listener, context, arraysize(argv), argv);
|
| + ::testing::Mock::VerifyAndClearExpectations(&change_handler);
|
| + EXPECT_EQ(1u, handler.GetNumEventListenersForTesting(kEventName, context));
|
| +
|
| + // When the contexts are invalidated, we should receive listener removed
|
| + // notifications.
|
| + EXPECT_CALL(
|
| + change_handler,
|
| + Run(kEventName, binding::EventListenersChanged::NO_LISTENERS, context))
|
| + .Times(1);
|
| + handler.InvalidateContext(context);
|
| + ::testing::Mock::VerifyAndClearExpectations(&change_handler);
|
| +}
|
| +
|
| } // namespace extensions
|
|
|