event_filter_unittest.cc 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // Copyright 2020 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include <fuchsia/web/cpp/fidl.h>
  5. #include "fuchsia_web/webengine/browser/event_filter.h"
  6. #include "testing/gtest/include/gtest/gtest.h"
  7. #include "ui/events/event.h"
  8. #include "ui/events/test/test_event.h"
  9. using fuchsia::web::InputTypes;
  10. struct EventTypeMappingEntry {
  11. ui::EventType ui_type;
  12. fuchsia::web::InputTypes fuchsia_type;
  13. };
  14. constexpr EventTypeMappingEntry kEventTypeMappings[] = {
  15. {ui::ET_MOUSE_PRESSED, InputTypes::MOUSE_CLICK},
  16. {ui::ET_MOUSE_DRAGGED, InputTypes::MOUSE_CLICK},
  17. {ui::ET_MOUSE_RELEASED, InputTypes::MOUSE_CLICK},
  18. {ui::ET_MOUSE_MOVED, InputTypes::MOUSE_MOVE},
  19. {ui::ET_MOUSE_ENTERED, InputTypes::MOUSE_MOVE},
  20. {ui::ET_MOUSE_EXITED, InputTypes::MOUSE_MOVE},
  21. {ui::ET_MOUSEWHEEL, InputTypes::MOUSE_WHEEL},
  22. {ui::ET_GESTURE_TAP, InputTypes::GESTURE_TAP},
  23. {ui::ET_GESTURE_TAP_DOWN, InputTypes::GESTURE_TAP},
  24. {ui::ET_GESTURE_TAP_CANCEL, InputTypes::GESTURE_TAP},
  25. {ui::ET_GESTURE_TAP_UNCONFIRMED, InputTypes::GESTURE_TAP},
  26. {ui::ET_GESTURE_DOUBLE_TAP, InputTypes::GESTURE_TAP},
  27. {ui::ET_GESTURE_TWO_FINGER_TAP, InputTypes::GESTURE_TAP},
  28. {ui::ET_GESTURE_LONG_PRESS, InputTypes::GESTURE_TAP},
  29. {ui::ET_GESTURE_LONG_TAP, InputTypes::GESTURE_TAP},
  30. {ui::ET_GESTURE_PINCH_BEGIN, InputTypes::GESTURE_PINCH},
  31. {ui::ET_GESTURE_PINCH_END, InputTypes::GESTURE_PINCH},
  32. {ui::ET_GESTURE_PINCH_UPDATE, InputTypes::GESTURE_PINCH},
  33. {ui::ET_GESTURE_SCROLL_BEGIN, InputTypes::GESTURE_DRAG},
  34. {ui::ET_GESTURE_SCROLL_END, InputTypes::GESTURE_DRAG},
  35. {ui::ET_GESTURE_SCROLL_UPDATE, InputTypes::GESTURE_DRAG},
  36. {ui::ET_GESTURE_SWIPE, InputTypes::GESTURE_DRAG},
  37. {ui::ET_SCROLL, InputTypes::GESTURE_DRAG},
  38. {ui::ET_SCROLL_FLING_START, InputTypes::GESTURE_DRAG},
  39. {ui::ET_SCROLL_FLING_CANCEL, InputTypes::GESTURE_DRAG},
  40. {ui::ET_KEY_PRESSED, InputTypes::KEY},
  41. {ui::ET_KEY_RELEASED, InputTypes::KEY},
  42. };
  43. constexpr ui::EventType kAlwaysAllowedEventTypes[] = {
  44. ui::ET_TOUCH_RELEASED, ui::ET_TOUCH_PRESSED,
  45. ui::ET_TOUCH_MOVED, ui::ET_TOUCH_CANCELLED,
  46. ui::ET_DROP_TARGET_EVENT, ui::ET_GESTURE_SHOW_PRESS,
  47. ui::ET_GESTURE_BEGIN, ui::ET_GESTURE_END,
  48. ui::ET_CANCEL_MODE, ui::ET_MOUSE_CAPTURE_CHANGED,
  49. };
  50. constexpr ui::EventType kUserEvent =
  51. static_cast<ui::EventType>(ui::ET_LAST + 1);
  52. class EventFilterTest : public testing::Test {
  53. public:
  54. EventFilterTest() = default;
  55. ~EventFilterTest() override = default;
  56. protected:
  57. void OnEvent(ui::Event* event) { event_filter_.OnEvent(event); }
  58. EventFilter event_filter_;
  59. };
  60. TEST_F(EventFilterTest, AllowedByDefault) {
  61. for (const auto& entry : kEventTypeMappings) {
  62. ui::test::TestEvent event(entry.ui_type);
  63. ASSERT_FALSE(event.stopped_propagation());
  64. OnEvent(&event);
  65. EXPECT_FALSE(event.stopped_propagation());
  66. }
  67. }
  68. TEST_F(EventFilterTest, SelectivelyAllowed) {
  69. event_filter_.ConfigureInputTypes(fuchsia::web::InputTypes::ALL,
  70. fuchsia::web::AllowInputState::DENY);
  71. for (const auto& entry : kEventTypeMappings) {
  72. event_filter_.ConfigureInputTypes(entry.fuchsia_type,
  73. fuchsia::web::AllowInputState::ALLOW);
  74. ui::test::TestEvent event(entry.ui_type);
  75. ASSERT_FALSE(event.stopped_propagation());
  76. OnEvent(&event);
  77. EXPECT_FALSE(event.stopped_propagation());
  78. }
  79. }
  80. TEST_F(EventFilterTest, AllDenied) {
  81. event_filter_.ConfigureInputTypes(fuchsia::web::InputTypes::ALL,
  82. fuchsia::web::AllowInputState::DENY);
  83. for (const auto& entry : kEventTypeMappings) {
  84. ui::test::TestEvent event(entry.ui_type);
  85. ASSERT_FALSE(event.stopped_propagation());
  86. OnEvent(&event);
  87. EXPECT_TRUE(event.stopped_propagation());
  88. }
  89. }
  90. TEST_F(EventFilterTest, SelectivelyDenied) {
  91. for (const auto& entry : kEventTypeMappings) {
  92. event_filter_.ConfigureInputTypes(entry.fuchsia_type,
  93. fuchsia::web::AllowInputState::DENY);
  94. ui::test::TestEvent event(entry.ui_type);
  95. ASSERT_FALSE(event.stopped_propagation());
  96. OnEvent(&event);
  97. EXPECT_TRUE(event.stopped_propagation());
  98. }
  99. }
  100. TEST_F(EventFilterTest, AllowCombination) {
  101. event_filter_.ConfigureInputTypes(fuchsia::web::InputTypes::ALL,
  102. fuchsia::web::AllowInputState::DENY);
  103. event_filter_.ConfigureInputTypes(
  104. InputTypes::MOUSE_CLICK | InputTypes::MOUSE_WHEEL,
  105. fuchsia::web::AllowInputState::ALLOW);
  106. ui::test::TestEvent event1(ui::ET_MOUSE_PRESSED);
  107. ASSERT_FALSE(event1.stopped_propagation());
  108. OnEvent(&event1);
  109. EXPECT_FALSE(event1.stopped_propagation());
  110. ui::test::TestEvent event2(ui::ET_MOUSEWHEEL);
  111. ASSERT_FALSE(event2.stopped_propagation());
  112. OnEvent(&event2);
  113. EXPECT_FALSE(event2.stopped_propagation());
  114. // Events not explicitly re-enabled are still denied.
  115. ui::test::TestEvent dropped_event(ui::ET_KEY_PRESSED);
  116. ASSERT_FALSE(dropped_event.stopped_propagation());
  117. OnEvent(&dropped_event);
  118. EXPECT_TRUE(dropped_event.stopped_propagation());
  119. }
  120. TEST_F(EventFilterTest, AllowUnknown) {
  121. ui::test::TestEvent event(kUserEvent);
  122. ASSERT_FALSE(event.stopped_propagation());
  123. OnEvent(&event);
  124. EXPECT_FALSE(event.stopped_propagation());
  125. ui::test::TestEvent event2(ui::ET_UNKNOWN);
  126. ASSERT_FALSE(event2.stopped_propagation());
  127. OnEvent(&event);
  128. EXPECT_FALSE(event2.stopped_propagation());
  129. }
  130. TEST_F(EventFilterTest, DenyUnknown) {
  131. event_filter_.ConfigureInputTypes(fuchsia::web::InputTypes::ALL,
  132. fuchsia::web::AllowInputState::DENY);
  133. ui::test::TestEvent event(kUserEvent);
  134. ASSERT_FALSE(event.stopped_propagation());
  135. OnEvent(&event);
  136. EXPECT_TRUE(event.stopped_propagation());
  137. ui::test::TestEvent event2(ui::ET_UNKNOWN);
  138. ASSERT_FALSE(event2.stopped_propagation());
  139. OnEvent(&event2);
  140. EXPECT_TRUE(event2.stopped_propagation());
  141. }
  142. TEST_F(EventFilterTest, AllowUnknown_AllowAllAfterDenyAll) {
  143. event_filter_.ConfigureInputTypes(fuchsia::web::InputTypes::ALL,
  144. fuchsia::web::AllowInputState::DENY);
  145. event_filter_.ConfigureInputTypes(fuchsia::web::InputTypes::ALL,
  146. fuchsia::web::AllowInputState::ALLOW);
  147. ui::test::TestEvent event(kUserEvent);
  148. ASSERT_FALSE(event.stopped_propagation());
  149. OnEvent(&event);
  150. EXPECT_FALSE(event.stopped_propagation());
  151. }
  152. TEST_F(EventFilterTest, DenyUnknown_AllowSomeAfterDenyAll) {
  153. event_filter_.ConfigureInputTypes(fuchsia::web::InputTypes::ALL,
  154. fuchsia::web::AllowInputState::DENY);
  155. event_filter_.ConfigureInputTypes(fuchsia::web::InputTypes::MOUSE_CLICK,
  156. fuchsia::web::AllowInputState::ALLOW);
  157. ui::test::TestEvent event(kUserEvent);
  158. ASSERT_FALSE(event.stopped_propagation());
  159. OnEvent(&event);
  160. EXPECT_TRUE(event.stopped_propagation());
  161. }
  162. TEST_F(EventFilterTest, LowLevelAndControlAlwaysAllowed) {
  163. event_filter_.ConfigureInputTypes(fuchsia::web::InputTypes::ALL,
  164. fuchsia::web::AllowInputState::DENY);
  165. for (ui::EventType type : kAlwaysAllowedEventTypes) {
  166. ui::test::TestEvent event(type);
  167. ASSERT_FALSE(event.stopped_propagation());
  168. OnEvent(&event);
  169. EXPECT_FALSE(event.stopped_propagation());
  170. }
  171. }