123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- // Copyright 2020 The Chromium Authors. All rights reserved.
- // Use of this source code is governed by a BSD-style license that can be
- // found in the LICENSE file.
- #include <fuchsia/web/cpp/fidl.h>
- #include "fuchsia_web/webengine/browser/event_filter.h"
- #include "testing/gtest/include/gtest/gtest.h"
- #include "ui/events/event.h"
- #include "ui/events/test/test_event.h"
- using fuchsia::web::InputTypes;
- struct EventTypeMappingEntry {
- ui::EventType ui_type;
- fuchsia::web::InputTypes fuchsia_type;
- };
- constexpr EventTypeMappingEntry kEventTypeMappings[] = {
- {ui::ET_MOUSE_PRESSED, InputTypes::MOUSE_CLICK},
- {ui::ET_MOUSE_DRAGGED, InputTypes::MOUSE_CLICK},
- {ui::ET_MOUSE_RELEASED, InputTypes::MOUSE_CLICK},
- {ui::ET_MOUSE_MOVED, InputTypes::MOUSE_MOVE},
- {ui::ET_MOUSE_ENTERED, InputTypes::MOUSE_MOVE},
- {ui::ET_MOUSE_EXITED, InputTypes::MOUSE_MOVE},
- {ui::ET_MOUSEWHEEL, InputTypes::MOUSE_WHEEL},
- {ui::ET_GESTURE_TAP, InputTypes::GESTURE_TAP},
- {ui::ET_GESTURE_TAP_DOWN, InputTypes::GESTURE_TAP},
- {ui::ET_GESTURE_TAP_CANCEL, InputTypes::GESTURE_TAP},
- {ui::ET_GESTURE_TAP_UNCONFIRMED, InputTypes::GESTURE_TAP},
- {ui::ET_GESTURE_DOUBLE_TAP, InputTypes::GESTURE_TAP},
- {ui::ET_GESTURE_TWO_FINGER_TAP, InputTypes::GESTURE_TAP},
- {ui::ET_GESTURE_LONG_PRESS, InputTypes::GESTURE_TAP},
- {ui::ET_GESTURE_LONG_TAP, InputTypes::GESTURE_TAP},
- {ui::ET_GESTURE_PINCH_BEGIN, InputTypes::GESTURE_PINCH},
- {ui::ET_GESTURE_PINCH_END, InputTypes::GESTURE_PINCH},
- {ui::ET_GESTURE_PINCH_UPDATE, InputTypes::GESTURE_PINCH},
- {ui::ET_GESTURE_SCROLL_BEGIN, InputTypes::GESTURE_DRAG},
- {ui::ET_GESTURE_SCROLL_END, InputTypes::GESTURE_DRAG},
- {ui::ET_GESTURE_SCROLL_UPDATE, InputTypes::GESTURE_DRAG},
- {ui::ET_GESTURE_SWIPE, InputTypes::GESTURE_DRAG},
- {ui::ET_SCROLL, InputTypes::GESTURE_DRAG},
- {ui::ET_SCROLL_FLING_START, InputTypes::GESTURE_DRAG},
- {ui::ET_SCROLL_FLING_CANCEL, InputTypes::GESTURE_DRAG},
- {ui::ET_KEY_PRESSED, InputTypes::KEY},
- {ui::ET_KEY_RELEASED, InputTypes::KEY},
- };
- constexpr ui::EventType kAlwaysAllowedEventTypes[] = {
- ui::ET_TOUCH_RELEASED, ui::ET_TOUCH_PRESSED,
- ui::ET_TOUCH_MOVED, ui::ET_TOUCH_CANCELLED,
- ui::ET_DROP_TARGET_EVENT, ui::ET_GESTURE_SHOW_PRESS,
- ui::ET_GESTURE_BEGIN, ui::ET_GESTURE_END,
- ui::ET_CANCEL_MODE, ui::ET_MOUSE_CAPTURE_CHANGED,
- };
- constexpr ui::EventType kUserEvent =
- static_cast<ui::EventType>(ui::ET_LAST + 1);
- class EventFilterTest : public testing::Test {
- public:
- EventFilterTest() = default;
- ~EventFilterTest() override = default;
- protected:
- void OnEvent(ui::Event* event) { event_filter_.OnEvent(event); }
- EventFilter event_filter_;
- };
- TEST_F(EventFilterTest, AllowedByDefault) {
- for (const auto& entry : kEventTypeMappings) {
- ui::test::TestEvent event(entry.ui_type);
- ASSERT_FALSE(event.stopped_propagation());
- OnEvent(&event);
- EXPECT_FALSE(event.stopped_propagation());
- }
- }
- TEST_F(EventFilterTest, SelectivelyAllowed) {
- event_filter_.ConfigureInputTypes(fuchsia::web::InputTypes::ALL,
- fuchsia::web::AllowInputState::DENY);
- for (const auto& entry : kEventTypeMappings) {
- event_filter_.ConfigureInputTypes(entry.fuchsia_type,
- fuchsia::web::AllowInputState::ALLOW);
- ui::test::TestEvent event(entry.ui_type);
- ASSERT_FALSE(event.stopped_propagation());
- OnEvent(&event);
- EXPECT_FALSE(event.stopped_propagation());
- }
- }
- TEST_F(EventFilterTest, AllDenied) {
- event_filter_.ConfigureInputTypes(fuchsia::web::InputTypes::ALL,
- fuchsia::web::AllowInputState::DENY);
- for (const auto& entry : kEventTypeMappings) {
- ui::test::TestEvent event(entry.ui_type);
- ASSERT_FALSE(event.stopped_propagation());
- OnEvent(&event);
- EXPECT_TRUE(event.stopped_propagation());
- }
- }
- TEST_F(EventFilterTest, SelectivelyDenied) {
- for (const auto& entry : kEventTypeMappings) {
- event_filter_.ConfigureInputTypes(entry.fuchsia_type,
- fuchsia::web::AllowInputState::DENY);
- ui::test::TestEvent event(entry.ui_type);
- ASSERT_FALSE(event.stopped_propagation());
- OnEvent(&event);
- EXPECT_TRUE(event.stopped_propagation());
- }
- }
- TEST_F(EventFilterTest, AllowCombination) {
- event_filter_.ConfigureInputTypes(fuchsia::web::InputTypes::ALL,
- fuchsia::web::AllowInputState::DENY);
- event_filter_.ConfigureInputTypes(
- InputTypes::MOUSE_CLICK | InputTypes::MOUSE_WHEEL,
- fuchsia::web::AllowInputState::ALLOW);
- ui::test::TestEvent event1(ui::ET_MOUSE_PRESSED);
- ASSERT_FALSE(event1.stopped_propagation());
- OnEvent(&event1);
- EXPECT_FALSE(event1.stopped_propagation());
- ui::test::TestEvent event2(ui::ET_MOUSEWHEEL);
- ASSERT_FALSE(event2.stopped_propagation());
- OnEvent(&event2);
- EXPECT_FALSE(event2.stopped_propagation());
- // Events not explicitly re-enabled are still denied.
- ui::test::TestEvent dropped_event(ui::ET_KEY_PRESSED);
- ASSERT_FALSE(dropped_event.stopped_propagation());
- OnEvent(&dropped_event);
- EXPECT_TRUE(dropped_event.stopped_propagation());
- }
- TEST_F(EventFilterTest, AllowUnknown) {
- ui::test::TestEvent event(kUserEvent);
- ASSERT_FALSE(event.stopped_propagation());
- OnEvent(&event);
- EXPECT_FALSE(event.stopped_propagation());
- ui::test::TestEvent event2(ui::ET_UNKNOWN);
- ASSERT_FALSE(event2.stopped_propagation());
- OnEvent(&event);
- EXPECT_FALSE(event2.stopped_propagation());
- }
- TEST_F(EventFilterTest, DenyUnknown) {
- event_filter_.ConfigureInputTypes(fuchsia::web::InputTypes::ALL,
- fuchsia::web::AllowInputState::DENY);
- ui::test::TestEvent event(kUserEvent);
- ASSERT_FALSE(event.stopped_propagation());
- OnEvent(&event);
- EXPECT_TRUE(event.stopped_propagation());
- ui::test::TestEvent event2(ui::ET_UNKNOWN);
- ASSERT_FALSE(event2.stopped_propagation());
- OnEvent(&event2);
- EXPECT_TRUE(event2.stopped_propagation());
- }
- TEST_F(EventFilterTest, AllowUnknown_AllowAllAfterDenyAll) {
- event_filter_.ConfigureInputTypes(fuchsia::web::InputTypes::ALL,
- fuchsia::web::AllowInputState::DENY);
- event_filter_.ConfigureInputTypes(fuchsia::web::InputTypes::ALL,
- fuchsia::web::AllowInputState::ALLOW);
- ui::test::TestEvent event(kUserEvent);
- ASSERT_FALSE(event.stopped_propagation());
- OnEvent(&event);
- EXPECT_FALSE(event.stopped_propagation());
- }
- TEST_F(EventFilterTest, DenyUnknown_AllowSomeAfterDenyAll) {
- event_filter_.ConfigureInputTypes(fuchsia::web::InputTypes::ALL,
- fuchsia::web::AllowInputState::DENY);
- event_filter_.ConfigureInputTypes(fuchsia::web::InputTypes::MOUSE_CLICK,
- fuchsia::web::AllowInputState::ALLOW);
- ui::test::TestEvent event(kUserEvent);
- ASSERT_FALSE(event.stopped_propagation());
- OnEvent(&event);
- EXPECT_TRUE(event.stopped_propagation());
- }
- TEST_F(EventFilterTest, LowLevelAndControlAlwaysAllowed) {
- event_filter_.ConfigureInputTypes(fuchsia::web::InputTypes::ALL,
- fuchsia::web::AllowInputState::DENY);
- for (ui::EventType type : kAlwaysAllowedEventTypes) {
- ui::test::TestEvent event(type);
- ASSERT_FALSE(event.stopped_propagation());
- OnEvent(&event);
- EXPECT_FALSE(event.stopped_propagation());
- }
- }
|