| 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 3a376b4ab57758ed445f3747061aeb554ef897b1..3230ebb060e10e4661bfaa25d5d216a4a1ed4745 100644
|
| --- a/extensions/renderer/api_event_handler_unittest.cc
|
| +++ b/extensions/renderer/api_event_handler_unittest.cc
|
| @@ -10,6 +10,7 @@
|
| #include "base/optional.h"
|
| #include "base/test/mock_callback.h"
|
| #include "base/values.h"
|
| +#include "extensions/common/event_filtering_info.h"
|
| #include "extensions/renderer/api_binding_test.h"
|
| #include "extensions/renderer/api_binding_test_util.h"
|
| #include "gin/arguments.h"
|
| @@ -26,6 +27,7 @@ using MockEventChangeHandler = ::testing::StrictMock<
|
|
|
| void DoNothingOnEventListenersChanged(const std::string& event_name,
|
| binding::EventListenersChanged change,
|
| + const base::DictionaryValue* value,
|
| v8::Local<v8::Context> context) {}
|
|
|
| class APIEventHandlerTest : public APIBindingTest {
|
| @@ -73,7 +75,7 @@ TEST_F(APIEventHandlerTest, AddingRemovingAndQueryingEventListeners) {
|
| v8::Local<v8::Context> context = MainContext();
|
|
|
| v8::Local<v8::Object> event =
|
| - handler()->CreateEventInstance(kEventName, context);
|
| + handler()->CreateEventInstance(kEventName, false, context);
|
| ASSERT_FALSE(event.IsEmpty());
|
|
|
| EXPECT_EQ(0u, handler()->GetNumEventListenersForTesting(kEventName, context));
|
| @@ -172,9 +174,9 @@ TEST_F(APIEventHandlerTest, FiringEvents) {
|
| v8::Local<v8::Context> context = MainContext();
|
|
|
| v8::Local<v8::Object> alpha_event =
|
| - handler()->CreateEventInstance(kAlphaName, context);
|
| + handler()->CreateEventInstance(kAlphaName, false, context);
|
| v8::Local<v8::Object> beta_event =
|
| - handler()->CreateEventInstance(kBetaName, context);
|
| + handler()->CreateEventInstance(kBetaName, false, context);
|
| ASSERT_FALSE(alpha_event.IsEmpty());
|
| ASSERT_FALSE(beta_event.IsEmpty());
|
|
|
| @@ -241,7 +243,8 @@ TEST_F(APIEventHandlerTest, FiringEvents) {
|
| EXPECT_EQ(0, get_fired_count("alphaCount2"));
|
| EXPECT_EQ(0, get_fired_count("betaCount"));
|
|
|
| - handler()->FireEventInContext(kAlphaName, context, base::ListValue());
|
| + handler()->FireEventInContext(kAlphaName, context, base::ListValue(),
|
| + EventFilteringInfo());
|
| EXPECT_EQ(2u, handler()->GetNumEventListenersForTesting(kAlphaName, context));
|
| EXPECT_EQ(1u, handler()->GetNumEventListenersForTesting(kBetaName, context));
|
|
|
| @@ -249,12 +252,14 @@ TEST_F(APIEventHandlerTest, FiringEvents) {
|
| EXPECT_EQ(1, get_fired_count("alphaCount2"));
|
| EXPECT_EQ(0, get_fired_count("betaCount"));
|
|
|
| - handler()->FireEventInContext(kAlphaName, context, base::ListValue());
|
| + handler()->FireEventInContext(kAlphaName, context, base::ListValue(),
|
| + EventFilteringInfo());
|
| EXPECT_EQ(2, get_fired_count("alphaCount1"));
|
| EXPECT_EQ(2, get_fired_count("alphaCount2"));
|
| EXPECT_EQ(0, get_fired_count("betaCount"));
|
|
|
| - handler()->FireEventInContext(kBetaName, context, base::ListValue());
|
| + handler()->FireEventInContext(kBetaName, context, base::ListValue(),
|
| + EventFilteringInfo());
|
| EXPECT_EQ(2, get_fired_count("alphaCount1"));
|
| EXPECT_EQ(2, get_fired_count("alphaCount2"));
|
| EXPECT_EQ(1, get_fired_count("betaCount"));
|
| @@ -267,7 +272,7 @@ TEST_F(APIEventHandlerTest, EventArguments) {
|
|
|
| const char kEventName[] = "alpha";
|
| v8::Local<v8::Object> event =
|
| - handler()->CreateEventInstance(kEventName, context);
|
| + handler()->CreateEventInstance(kEventName, false, context);
|
| ASSERT_FALSE(event.IsEmpty());
|
|
|
| const char kListenerFunction[] =
|
| @@ -288,7 +293,8 @@ TEST_F(APIEventHandlerTest, EventArguments) {
|
| const char kArguments[] = "['foo',1,{'prop1':'bar'}]";
|
| std::unique_ptr<base::ListValue> event_args = ListValueFromString(kArguments);
|
| ASSERT_TRUE(event_args);
|
| - handler()->FireEventInContext(kEventName, context, *event_args);
|
| + handler()->FireEventInContext(kEventName, context, *event_args,
|
| + EventFilteringInfo());
|
|
|
| EXPECT_EQ(
|
| ReplaceSingleQuotes(kArguments),
|
| @@ -314,10 +320,10 @@ TEST_F(APIEventHandlerTest, MultipleContexts) {
|
|
|
| // Create two instances of the same event in different contexts.
|
| v8::Local<v8::Object> event_a =
|
| - handler()->CreateEventInstance(kEventName, context_a);
|
| + handler()->CreateEventInstance(kEventName, false, context_a);
|
| ASSERT_FALSE(event_a.IsEmpty());
|
| v8::Local<v8::Object> event_b =
|
| - handler()->CreateEventInstance(kEventName, context_b);
|
| + handler()->CreateEventInstance(kEventName, false, context_b);
|
| ASSERT_FALSE(event_b.IsEmpty());
|
|
|
| // Add two separate listeners to the event, one in each context.
|
| @@ -351,7 +357,8 @@ TEST_F(APIEventHandlerTest, MultipleContexts) {
|
| ListValueFromString("['result_a:']");
|
| ASSERT_TRUE(arguments_a);
|
|
|
| - handler()->FireEventInContext(kEventName, context_a, *arguments_a);
|
| + handler()->FireEventInContext(kEventName, context_a, *arguments_a,
|
| + EventFilteringInfo());
|
| {
|
| EXPECT_EQ("\"result_a:alpha\"",
|
| GetStringPropertyFromObject(context_a->Global(), context_a,
|
| @@ -367,7 +374,8 @@ TEST_F(APIEventHandlerTest, MultipleContexts) {
|
| std::unique_ptr<base::ListValue> arguments_b =
|
| ListValueFromString("['result_b:']");
|
| ASSERT_TRUE(arguments_b);
|
| - handler()->FireEventInContext(kEventName, context_b, *arguments_b);
|
| + handler()->FireEventInContext(kEventName, context_b, *arguments_b,
|
| + EventFilteringInfo());
|
| {
|
| EXPECT_EQ("\"result_a:alpha\"",
|
| GetStringPropertyFromObject(context_a->Global(), context_a,
|
| @@ -386,7 +394,7 @@ TEST_F(APIEventHandlerTest, DifferentCallingMethods) {
|
|
|
| const char kEventName[] = "alpha";
|
| v8::Local<v8::Object> event =
|
| - handler()->CreateEventInstance(kEventName, context);
|
| + handler()->CreateEventInstance(kEventName, false, context);
|
| ASSERT_FALSE(event.IsEmpty());
|
|
|
| const char kAddListenerOnNull[] =
|
| @@ -438,7 +446,7 @@ TEST_F(APIEventHandlerTest, TestDispatchFromJs) {
|
| v8::Local<v8::Context> context = MainContext();
|
|
|
| v8::Local<v8::Object> event =
|
| - handler()->CreateEventInstance("alpha", context);
|
| + handler()->CreateEventInstance("alpha", false, context);
|
| ASSERT_FALSE(event.IsEmpty());
|
|
|
| const char kListenerFunction[] =
|
| @@ -479,7 +487,7 @@ TEST_F(APIEventHandlerTest, RemovingListenersWhileHandlingEvent) {
|
|
|
| const char kEventName[] = "alpha";
|
| v8::Local<v8::Object> event =
|
| - handler()->CreateEventInstance(kEventName, context);
|
| + handler()->CreateEventInstance(kEventName, false, context);
|
| ASSERT_FALSE(event.IsEmpty());
|
| {
|
| // Cache the event object on the global in order to allow for easy removal.
|
| @@ -521,7 +529,8 @@ TEST_F(APIEventHandlerTest, RemovingListenersWhileHandlingEvent) {
|
| // Fire the event. All listeners should be removed (and we shouldn't crash).
|
| EXPECT_EQ(kNumListeners,
|
| handler()->GetNumEventListenersForTesting(kEventName, context));
|
| - handler()->FireEventInContext(kEventName, context, base::ListValue());
|
| + handler()->FireEventInContext(kEventName, context, base::ListValue(),
|
| + EventFilteringInfo());
|
| EXPECT_EQ(0u, handler()->GetNumEventListenersForTesting(kEventName, context));
|
|
|
| // TODO(devlin): Another possible test: register listener a and listener b,
|
| @@ -553,7 +562,7 @@ TEST_F(APIEventHandlerTest, TestEventListenersThrowingExceptions) {
|
|
|
| const char kEventName[] = "alpha";
|
| v8::Local<v8::Object> event =
|
| - handler()->CreateEventInstance(kEventName, context);
|
| + handler()->CreateEventInstance(kEventName, false, context);
|
| ASSERT_FALSE(event.IsEmpty());
|
|
|
| bool did_throw = false;
|
| @@ -601,7 +610,8 @@ TEST_F(APIEventHandlerTest, TestEventListenersThrowingExceptions) {
|
|
|
| std::unique_ptr<base::ListValue> event_args = ListValueFromString("[42]");
|
| ASSERT_TRUE(event_args);
|
| - handler()->FireEventInContext(kEventName, context, *event_args);
|
| + handler()->FireEventInContext(kEventName, context, *event_args,
|
| + EventFilteringInfo());
|
|
|
| // An exception should have been thrown by the first listener and the second
|
| // listener should have recorded the event arguments.
|
| @@ -626,13 +636,13 @@ TEST_F(APIEventHandlerTest, CallbackNotifications) {
|
| const char kEventName1[] = "onFoo";
|
| const char kEventName2[] = "onBar";
|
| v8::Local<v8::Object> event1_a =
|
| - handler()->CreateEventInstance(kEventName1, context_a);
|
| + handler()->CreateEventInstance(kEventName1, false, context_a);
|
| ASSERT_FALSE(event1_a.IsEmpty());
|
| v8::Local<v8::Object> event2_a =
|
| - handler()->CreateEventInstance(kEventName2, context_a);
|
| + handler()->CreateEventInstance(kEventName2, false, context_a);
|
| ASSERT_FALSE(event2_a.IsEmpty());
|
| v8::Local<v8::Object> event1_b =
|
| - handler()->CreateEventInstance(kEventName1, context_b);
|
| + handler()->CreateEventInstance(kEventName1, false, context_b);
|
| ASSERT_FALSE(event1_b.IsEmpty());
|
|
|
| const char kAddListenerFunction[] =
|
| @@ -649,7 +659,7 @@ TEST_F(APIEventHandlerTest, CallbackNotifications) {
|
| {
|
| EXPECT_CALL(change_handler,
|
| Run(kEventName1, binding::EventListenersChanged::HAS_LISTENERS,
|
| - context_a))
|
| + nullptr, context_a))
|
| .Times(1);
|
| v8::Local<v8::Value> argv[] = {event1_a, listener1};
|
| RunFunction(add_listener, context_a, arraysize(argv), argv);
|
| @@ -686,7 +696,7 @@ TEST_F(APIEventHandlerTest, CallbackNotifications) {
|
| {
|
| EXPECT_CALL(change_handler,
|
| Run(kEventName1, binding::EventListenersChanged::NO_LISTENERS,
|
| - context_a))
|
| + nullptr, context_a))
|
| .Times(1);
|
| v8::Local<v8::Value> argv[] = {event1_a, listener2};
|
| RunFunction(remove_listener, context_a, arraysize(argv), argv);
|
| @@ -702,7 +712,7 @@ TEST_F(APIEventHandlerTest, CallbackNotifications) {
|
| {
|
| EXPECT_CALL(change_handler,
|
| Run(kEventName2, binding::EventListenersChanged::HAS_LISTENERS,
|
| - context_a))
|
| + nullptr, context_a))
|
| .Times(1);
|
| v8::Local<v8::Value> argv[] = {event2_a, listener3};
|
| RunFunction(add_listener, context_a, arraysize(argv), argv);
|
| @@ -714,7 +724,7 @@ TEST_F(APIEventHandlerTest, CallbackNotifications) {
|
| {
|
| EXPECT_CALL(change_handler,
|
| Run(kEventName1, binding::EventListenersChanged::HAS_LISTENERS,
|
| - context_b))
|
| + nullptr, context_b))
|
| .Times(1);
|
| // And add a listener to an event in a different context to make sure the
|
| // associated context is correct.
|
| @@ -731,20 +741,16 @@ TEST_F(APIEventHandlerTest, CallbackNotifications) {
|
|
|
| // When the contexts are invalidated, we should receive listener removed
|
| // notifications.
|
| - EXPECT_CALL(
|
| - change_handler,
|
| - Run(kEventName1, binding::EventListenersChanged::NO_LISTENERS, context_a))
|
| - .Times(1);
|
| - EXPECT_CALL(
|
| - change_handler,
|
| - Run(kEventName2, binding::EventListenersChanged::NO_LISTENERS, context_a))
|
| + EXPECT_CALL(change_handler,
|
| + Run(kEventName2, binding::EventListenersChanged::NO_LISTENERS,
|
| + nullptr, context_a))
|
| .Times(1);
|
| DisposeContext(context_a);
|
| ::testing::Mock::VerifyAndClearExpectations(&change_handler);
|
|
|
| - EXPECT_CALL(
|
| - change_handler,
|
| - Run(kEventName1, binding::EventListenersChanged::NO_LISTENERS, context_b))
|
| + EXPECT_CALL(change_handler,
|
| + Run(kEventName1, binding::EventListenersChanged::NO_LISTENERS,
|
| + nullptr, context_b))
|
| .Times(1);
|
| DisposeContext(context_b);
|
| ::testing::Mock::VerifyAndClearExpectations(&change_handler);
|
| @@ -757,7 +763,7 @@ TEST_F(APIEventHandlerTest, TestArgumentMassagers) {
|
|
|
| const char kEventName[] = "alpha";
|
| v8::Local<v8::Object> event =
|
| - handler()->CreateEventInstance(kEventName, context);
|
| + handler()->CreateEventInstance(kEventName, false, context);
|
| ASSERT_FALSE(event.IsEmpty());
|
|
|
| const char kArgumentMassager[] =
|
| @@ -787,7 +793,8 @@ TEST_F(APIEventHandlerTest, TestArgumentMassagers) {
|
| const char kArguments[] = "['first','second']";
|
| std::unique_ptr<base::ListValue> event_args = ListValueFromString(kArguments);
|
| ASSERT_TRUE(event_args);
|
| - handler()->FireEventInContext(kEventName, context, *event_args);
|
| + handler()->FireEventInContext(kEventName, context, *event_args,
|
| + EventFilteringInfo());
|
|
|
| EXPECT_EQ(
|
| "[\"first\",\"second\"]",
|
| @@ -805,7 +812,7 @@ TEST_F(APIEventHandlerTest, TestArgumentMassagersAsyncDispatch) {
|
|
|
| const char kEventName[] = "alpha";
|
| v8::Local<v8::Object> event =
|
| - handler()->CreateEventInstance(kEventName, context);
|
| + handler()->CreateEventInstance(kEventName, false, context);
|
| ASSERT_FALSE(event.IsEmpty());
|
|
|
| const char kArgumentMassager[] =
|
| @@ -835,7 +842,8 @@ TEST_F(APIEventHandlerTest, TestArgumentMassagersAsyncDispatch) {
|
| const char kArguments[] = "['first','second']";
|
| std::unique_ptr<base::ListValue> event_args = ListValueFromString(kArguments);
|
| ASSERT_TRUE(event_args);
|
| - handler()->FireEventInContext(kEventName, context, *event_args);
|
| + handler()->FireEventInContext(kEventName, context, *event_args,
|
| + EventFilteringInfo());
|
|
|
| // The massager should have been triggered, but since it doesn't call
|
| // dispatch(), the listener shouldn't have been notified.
|
| @@ -868,7 +876,7 @@ TEST_F(APIEventHandlerTest, TestArgumentMassagersNeverDispatch) {
|
|
|
| const char kEventName[] = "alpha";
|
| v8::Local<v8::Object> event =
|
| - handler()->CreateEventInstance(kEventName, context);
|
| + handler()->CreateEventInstance(kEventName, false, context);
|
| ASSERT_FALSE(event.IsEmpty());
|
|
|
| // A massager that never dispatches.
|
| @@ -889,7 +897,8 @@ TEST_F(APIEventHandlerTest, TestArgumentMassagersNeverDispatch) {
|
| v8::Local<v8::Value> argv[] = {event, listener_function};
|
| RunFunction(add_listener_function, context, arraysize(argv), argv);
|
|
|
| - handler()->FireEventInContext(kEventName, context, base::ListValue());
|
| + handler()->FireEventInContext(kEventName, context, base::ListValue(),
|
| + EventFilteringInfo());
|
|
|
| // Nothing should blow up. (We tested in the previous test that the event
|
| // isn't notified without calling dispatch, so all there is to test here is
|
|
|