event_processor_unittest.cc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. // Copyright (c) 2013 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 <tuple>
  5. #include <utility>
  6. #include <vector>
  7. #include "base/memory/ptr_util.h"
  8. #include "base/memory/raw_ptr.h"
  9. #include "testing/gtest/include/gtest/gtest.h"
  10. #include "ui/events/event.h"
  11. #include "ui/events/event_target_iterator.h"
  12. #include "ui/events/event_targeter.h"
  13. #include "ui/events/event_utils.h"
  14. #include "ui/events/test/events_test_utils.h"
  15. #include "ui/events/test/test_event_handler.h"
  16. #include "ui/events/test/test_event_processor.h"
  17. #include "ui/events/test/test_event_target.h"
  18. #include "ui/events/test/test_event_targeter.h"
  19. typedef std::vector<std::string> HandlerSequenceRecorder;
  20. namespace ui {
  21. namespace test {
  22. class EventProcessorTest : public testing::Test {
  23. public:
  24. EventProcessorTest() {}
  25. EventProcessorTest(const EventProcessorTest&) = delete;
  26. EventProcessorTest& operator=(const EventProcessorTest&) = delete;
  27. ~EventProcessorTest() override {}
  28. protected:
  29. // testing::Test:
  30. void SetUp() override {
  31. processor_.SetRoot(std::make_unique<TestEventTarget>());
  32. processor_.Reset();
  33. root()->SetEventTargeter(
  34. std::make_unique<TestEventTargeter>(root(), false));
  35. }
  36. TestEventTarget* root() {
  37. return static_cast<TestEventTarget*>(processor_.GetRoot());
  38. }
  39. TestEventProcessor* processor() {
  40. return &processor_;
  41. }
  42. void DispatchEvent(Event* event) {
  43. processor_.OnEventFromSource(event);
  44. }
  45. void SetTarget(TestEventTarget* target) {
  46. static_cast<TestEventTargeter*>(root()->GetEventTargeter())
  47. ->set_target(target);
  48. }
  49. private:
  50. TestEventProcessor processor_;
  51. };
  52. TEST_F(EventProcessorTest, Basic) {
  53. std::unique_ptr<TestEventTarget> child(new TestEventTarget());
  54. child->SetEventTargeter(
  55. std::make_unique<TestEventTargeter>(child.get(), false));
  56. SetTarget(child.get());
  57. root()->AddChild(std::move(child));
  58. MouseEvent mouse(ET_MOUSE_MOVED, gfx::Point(10, 10), gfx::Point(10, 10),
  59. EventTimeForNow(), EF_NONE, EF_NONE);
  60. DispatchEvent(&mouse);
  61. EXPECT_TRUE(root()->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED));
  62. EXPECT_FALSE(root()->DidReceiveEvent(ET_MOUSE_MOVED));
  63. SetTarget(root());
  64. root()->RemoveChild(root()->child_at(0));
  65. DispatchEvent(&mouse);
  66. EXPECT_TRUE(root()->DidReceiveEvent(ET_MOUSE_MOVED));
  67. }
  68. // ReDispatchEventHandler is used to receive mouse events and forward them
  69. // to a specified EventProcessor. Verifies that the event has the correct
  70. // target and phase both before and after the nested event processing. Also
  71. // verifies that the location of the event remains the same after it has
  72. // been processed by the second EventProcessor.
  73. class ReDispatchEventHandler : public TestEventHandler {
  74. public:
  75. ReDispatchEventHandler(EventProcessor* processor, EventTarget* target)
  76. : processor_(processor), expected_target_(target) {}
  77. ReDispatchEventHandler(const ReDispatchEventHandler&) = delete;
  78. ReDispatchEventHandler& operator=(const ReDispatchEventHandler&) = delete;
  79. ~ReDispatchEventHandler() override {}
  80. // TestEventHandler:
  81. void OnMouseEvent(MouseEvent* event) override {
  82. TestEventHandler::OnMouseEvent(event);
  83. EXPECT_EQ(expected_target_, event->target());
  84. EXPECT_EQ(EP_TARGET, event->phase());
  85. gfx::Point location(event->location());
  86. EventDispatchDetails details = processor_->OnEventFromSource(event);
  87. EXPECT_FALSE(details.dispatcher_destroyed);
  88. EXPECT_FALSE(details.target_destroyed);
  89. // The nested event-processing should not have mutated the target,
  90. // phase, or location of |event|.
  91. EXPECT_EQ(expected_target_, event->target());
  92. EXPECT_EQ(EP_TARGET, event->phase());
  93. EXPECT_EQ(location, event->location());
  94. }
  95. private:
  96. raw_ptr<EventProcessor> processor_;
  97. raw_ptr<EventTarget> expected_target_;
  98. };
  99. // Verifies that the phase and target information of an event is not mutated
  100. // as a result of sending the event to an event processor while it is still
  101. // being processed by another event processor.
  102. TEST_F(EventProcessorTest, NestedEventProcessing) {
  103. // Add one child to the default event processor used in this test suite.
  104. std::unique_ptr<TestEventTarget> child(new TestEventTarget());
  105. SetTarget(child.get());
  106. root()->AddChild(std::move(child));
  107. // Define a second root target and child.
  108. std::unique_ptr<EventTarget> second_root_scoped(new TestEventTarget());
  109. TestEventTarget* second_root =
  110. static_cast<TestEventTarget*>(second_root_scoped.get());
  111. std::unique_ptr<TestEventTarget> second_child(new TestEventTarget());
  112. second_root->SetEventTargeter(
  113. base::WrapUnique(new TestEventTargeter(second_child.get(), false)));
  114. second_root->AddChild(std::move(second_child));
  115. // Define a second event processor which owns the second root.
  116. std::unique_ptr<TestEventProcessor> second_processor(
  117. new TestEventProcessor());
  118. second_processor->SetRoot(std::move(second_root_scoped));
  119. // Indicate that an event which is dispatched to the child target owned by the
  120. // first event processor should be handled by |target_handler| instead.
  121. std::unique_ptr<TestEventHandler> target_handler(
  122. new ReDispatchEventHandler(second_processor.get(), root()->child_at(0)));
  123. std::ignore = root()->child_at(0)->SetTargetHandler(target_handler.get());
  124. // Dispatch a mouse event to the tree of event targets owned by the first
  125. // event processor, checking in ReDispatchEventHandler that the phase and
  126. // target information of the event is correct.
  127. MouseEvent mouse(ET_MOUSE_MOVED, gfx::Point(10, 10), gfx::Point(10, 10),
  128. EventTimeForNow(), EF_NONE, EF_NONE);
  129. DispatchEvent(&mouse);
  130. // Verify also that |mouse| was seen by the child nodes contained in both
  131. // event processors and that the event was not handled.
  132. EXPECT_EQ(1, target_handler->num_mouse_events());
  133. EXPECT_TRUE(second_root->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED));
  134. EXPECT_FALSE(mouse.handled());
  135. second_root->child_at(0)->ResetReceivedEvents();
  136. root()->child_at(0)->ResetReceivedEvents();
  137. target_handler->Reset();
  138. // Indicate that the child of the second root should handle events, and
  139. // dispatch another mouse event to verify that it is marked as handled.
  140. second_root->child_at(0)->set_mark_events_as_handled(true);
  141. MouseEvent mouse2(ET_MOUSE_MOVED, gfx::Point(10, 10), gfx::Point(10, 10),
  142. EventTimeForNow(), EF_NONE, EF_NONE);
  143. DispatchEvent(&mouse2);
  144. EXPECT_EQ(1, target_handler->num_mouse_events());
  145. EXPECT_TRUE(second_root->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED));
  146. EXPECT_TRUE(mouse2.handled());
  147. }
  148. // Verifies that OnEventProcessingFinished() is called when an event
  149. // has been handled.
  150. TEST_F(EventProcessorTest, OnEventProcessingFinished) {
  151. std::unique_ptr<TestEventTarget> child(new TestEventTarget());
  152. child->set_mark_events_as_handled(true);
  153. SetTarget(child.get());
  154. root()->AddChild(std::move(child));
  155. // Dispatch a mouse event. We expect the event to be seen by the target,
  156. // handled, and we expect OnEventProcessingFinished() to be invoked once.
  157. MouseEvent mouse(ET_MOUSE_MOVED, gfx::Point(10, 10), gfx::Point(10, 10),
  158. EventTimeForNow(), EF_NONE, EF_NONE);
  159. DispatchEvent(&mouse);
  160. EXPECT_TRUE(root()->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED));
  161. EXPECT_FALSE(root()->DidReceiveEvent(ET_MOUSE_MOVED));
  162. EXPECT_TRUE(mouse.handled());
  163. EXPECT_EQ(1, processor()->num_times_processing_finished());
  164. }
  165. // Verifies that OnEventProcessingStarted() has been called when starting to
  166. // process an event, and that processing does not take place if
  167. // OnEventProcessingStarted() marks the event as handled. Also verifies that
  168. // OnEventProcessingFinished() is also called in either case.
  169. TEST_F(EventProcessorTest, OnEventProcessingStarted) {
  170. std::unique_ptr<TestEventTarget> child(new TestEventTarget());
  171. SetTarget(child.get());
  172. root()->AddChild(std::move(child));
  173. // Dispatch a mouse event. We expect the event to be seen by the target,
  174. // OnEventProcessingStarted() should be called once, and
  175. // OnEventProcessingFinished() should be called once. The event should
  176. // remain unhandled.
  177. MouseEvent mouse(ET_MOUSE_MOVED, gfx::Point(10, 10), gfx::Point(10, 10),
  178. EventTimeForNow(), EF_NONE, EF_NONE);
  179. DispatchEvent(&mouse);
  180. EXPECT_TRUE(root()->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED));
  181. EXPECT_FALSE(root()->DidReceiveEvent(ET_MOUSE_MOVED));
  182. EXPECT_FALSE(mouse.handled());
  183. EXPECT_EQ(1, processor()->num_times_processing_started());
  184. EXPECT_EQ(1, processor()->num_times_processing_finished());
  185. processor()->Reset();
  186. root()->ResetReceivedEvents();
  187. root()->child_at(0)->ResetReceivedEvents();
  188. // Dispatch another mouse event, but with OnEventProcessingStarted() marking
  189. // the event as handled to prevent processing. We expect the event to not be
  190. // seen by the target this time, but OnEventProcessingStarted() and
  191. // OnEventProcessingFinished() should both still be called once.
  192. processor()->set_should_processing_occur(false);
  193. MouseEvent mouse2(ET_MOUSE_MOVED, gfx::Point(10, 10), gfx::Point(10, 10),
  194. EventTimeForNow(), EF_NONE, EF_NONE);
  195. DispatchEvent(&mouse2);
  196. EXPECT_FALSE(root()->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED));
  197. EXPECT_FALSE(root()->DidReceiveEvent(ET_MOUSE_MOVED));
  198. EXPECT_TRUE(mouse2.handled());
  199. EXPECT_EQ(1, processor()->num_times_processing_started());
  200. EXPECT_EQ(1, processor()->num_times_processing_finished());
  201. }
  202. // Tests that unhandled events are correctly dispatched to the next-best
  203. // target as decided by the TestEventTargeter.
  204. TEST_F(EventProcessorTest, DispatchToNextBestTarget) {
  205. std::unique_ptr<TestEventTarget> child(new TestEventTarget());
  206. std::unique_ptr<TestEventTarget> grandchild(new TestEventTarget());
  207. // Install a TestEventTargeter which permits bubbling.
  208. root()->SetEventTargeter(
  209. base::WrapUnique(new TestEventTargeter(grandchild.get(), true)));
  210. child->AddChild(std::move(grandchild));
  211. root()->AddChild(std::move(child));
  212. ASSERT_EQ(1u, root()->child_count());
  213. ASSERT_EQ(1u, root()->child_at(0)->child_count());
  214. ASSERT_EQ(0u, root()->child_at(0)->child_at(0)->child_count());
  215. TestEventTarget* child_r = root()->child_at(0);
  216. TestEventTarget* grandchild_r = child_r->child_at(0);
  217. // When the root has a TestEventTargeter installed which permits bubbling,
  218. // events targeted at the grandchild target should be dispatched to all three
  219. // targets.
  220. KeyEvent key_event(ET_KEY_PRESSED, VKEY_ESCAPE, EF_NONE);
  221. DispatchEvent(&key_event);
  222. EXPECT_TRUE(root()->DidReceiveEvent(ET_KEY_PRESSED));
  223. EXPECT_TRUE(child_r->DidReceiveEvent(ET_KEY_PRESSED));
  224. EXPECT_TRUE(grandchild_r->DidReceiveEvent(ET_KEY_PRESSED));
  225. root()->ResetReceivedEvents();
  226. child_r->ResetReceivedEvents();
  227. grandchild_r->ResetReceivedEvents();
  228. // Add a pre-target handler on the child of the root that will mark the event
  229. // as handled. No targets in the hierarchy should receive the event.
  230. TestEventHandler handler;
  231. child_r->AddPreTargetHandler(&handler);
  232. key_event = KeyEvent(ET_KEY_PRESSED, VKEY_ESCAPE, EF_NONE);
  233. DispatchEvent(&key_event);
  234. EXPECT_FALSE(root()->DidReceiveEvent(ET_KEY_PRESSED));
  235. EXPECT_FALSE(child_r->DidReceiveEvent(ET_KEY_PRESSED));
  236. EXPECT_FALSE(grandchild_r->DidReceiveEvent(ET_KEY_PRESSED));
  237. EXPECT_EQ(1, handler.num_key_events());
  238. handler.Reset();
  239. // Add a post-target handler on the child of the root that will mark the event
  240. // as handled. Only the grandchild (the initial target) should receive the
  241. // event.
  242. child_r->RemovePreTargetHandler(&handler);
  243. child_r->AddPostTargetHandler(&handler);
  244. key_event = KeyEvent(ET_KEY_PRESSED, VKEY_ESCAPE, EF_NONE);
  245. DispatchEvent(&key_event);
  246. EXPECT_FALSE(root()->DidReceiveEvent(ET_KEY_PRESSED));
  247. EXPECT_FALSE(child_r->DidReceiveEvent(ET_KEY_PRESSED));
  248. EXPECT_TRUE(grandchild_r->DidReceiveEvent(ET_KEY_PRESSED));
  249. EXPECT_EQ(1, handler.num_key_events());
  250. handler.Reset();
  251. grandchild_r->ResetReceivedEvents();
  252. child_r->RemovePostTargetHandler(&handler);
  253. // Mark the event as handled when it reaches the EP_TARGET phase of
  254. // dispatch at the child of the root. The child and grandchild
  255. // targets should both receive the event, but the root should not.
  256. child_r->set_mark_events_as_handled(true);
  257. key_event = KeyEvent(ET_KEY_PRESSED, VKEY_ESCAPE, EF_NONE);
  258. DispatchEvent(&key_event);
  259. EXPECT_FALSE(root()->DidReceiveEvent(ET_KEY_PRESSED));
  260. EXPECT_TRUE(child_r->DidReceiveEvent(ET_KEY_PRESSED));
  261. EXPECT_TRUE(grandchild_r->DidReceiveEvent(ET_KEY_PRESSED));
  262. root()->ResetReceivedEvents();
  263. child_r->ResetReceivedEvents();
  264. grandchild_r->ResetReceivedEvents();
  265. child_r->set_mark_events_as_handled(false);
  266. }
  267. // Tests that unhandled events are seen by the correct sequence of
  268. // targets, pre-target handlers, and post-target handlers when
  269. // a TestEventTargeter is installed on the root target which permits bubbling.
  270. TEST_F(EventProcessorTest, HandlerSequence) {
  271. std::unique_ptr<TestEventTarget> child(new TestEventTarget());
  272. std::unique_ptr<TestEventTarget> grandchild(new TestEventTarget());
  273. // Install a TestEventTargeter which permits bubbling.
  274. root()->SetEventTargeter(
  275. base::WrapUnique(new TestEventTargeter(grandchild.get(), true)));
  276. child->AddChild(std::move(grandchild));
  277. root()->AddChild(std::move(child));
  278. ASSERT_EQ(1u, root()->child_count());
  279. ASSERT_EQ(1u, root()->child_at(0)->child_count());
  280. ASSERT_EQ(0u, root()->child_at(0)->child_at(0)->child_count());
  281. TestEventTarget* child_r = root()->child_at(0);
  282. TestEventTarget* grandchild_r = child_r->child_at(0);
  283. HandlerSequenceRecorder recorder;
  284. root()->set_target_name("R");
  285. root()->set_recorder(&recorder);
  286. child_r->set_target_name("C");
  287. child_r->set_recorder(&recorder);
  288. grandchild_r->set_target_name("G");
  289. grandchild_r->set_recorder(&recorder);
  290. TestEventHandler pre_root;
  291. pre_root.set_handler_name("PreR");
  292. pre_root.set_recorder(&recorder);
  293. root()->AddPreTargetHandler(&pre_root);
  294. TestEventHandler pre_child;
  295. pre_child.set_handler_name("PreC");
  296. pre_child.set_recorder(&recorder);
  297. child_r->AddPreTargetHandler(&pre_child);
  298. TestEventHandler pre_grandchild;
  299. pre_grandchild.set_handler_name("PreG");
  300. pre_grandchild.set_recorder(&recorder);
  301. grandchild_r->AddPreTargetHandler(&pre_grandchild);
  302. TestEventHandler post_root;
  303. post_root.set_handler_name("PostR");
  304. post_root.set_recorder(&recorder);
  305. root()->AddPostTargetHandler(&post_root);
  306. TestEventHandler post_child;
  307. post_child.set_handler_name("PostC");
  308. post_child.set_recorder(&recorder);
  309. child_r->AddPostTargetHandler(&post_child);
  310. TestEventHandler post_grandchild;
  311. post_grandchild.set_handler_name("PostG");
  312. post_grandchild.set_recorder(&recorder);
  313. grandchild_r->AddPostTargetHandler(&post_grandchild);
  314. MouseEvent mouse(ET_MOUSE_MOVED, gfx::Point(10, 10), gfx::Point(10, 10),
  315. EventTimeForNow(), EF_NONE, EF_NONE);
  316. DispatchEvent(&mouse);
  317. std::string expected[] = { "PreR", "PreC", "PreG", "G", "PostG", "PostC",
  318. "PostR", "PreR", "PreC", "C", "PostC", "PostR", "PreR", "R", "PostR" };
  319. EXPECT_EQ(std::vector<std::string>(expected, expected + std::size(expected)),
  320. recorder);
  321. root()->RemovePreTargetHandler(&pre_root);
  322. child_r->RemovePreTargetHandler(&pre_child);
  323. grandchild_r->RemovePreTargetHandler(&pre_grandchild);
  324. }
  325. namespace {
  326. class SelfDestroyingEventProcessor : public TestEventProcessor {
  327. public:
  328. SelfDestroyingEventProcessor() = default;
  329. SelfDestroyingEventProcessor(const SelfDestroyingEventProcessor&) = delete;
  330. SelfDestroyingEventProcessor& operator=(const SelfDestroyingEventProcessor&) =
  331. delete;
  332. ~SelfDestroyingEventProcessor() override = default;
  333. protected:
  334. EventDispatchDetails PostDispatchEvent(EventTarget* target,
  335. const Event& event) override;
  336. };
  337. class SelfDestroyingTestEventTarget : public TestEventTarget {
  338. public:
  339. SelfDestroyingTestEventTarget()
  340. : processor_(new SelfDestroyingEventProcessor()) {}
  341. SelfDestroyingTestEventTarget(const SelfDestroyingTestEventTarget&) = delete;
  342. SelfDestroyingTestEventTarget& operator=(
  343. const SelfDestroyingTestEventTarget&) = delete;
  344. TestEventProcessor* processor() { return processor_.get(); }
  345. void DestroyProcessor() { processor_.reset(); }
  346. private:
  347. std::unique_ptr<SelfDestroyingEventProcessor> processor_;
  348. };
  349. EventDispatchDetails SelfDestroyingEventProcessor::PostDispatchEvent(
  350. EventTarget* target,
  351. const Event& event) {
  352. static_cast<SelfDestroyingTestEventTarget*>(target)->DestroyProcessor();
  353. return EventDispatchDetails();
  354. }
  355. } // namespace
  356. TEST(EventProcessorCrashTest, Basic) {
  357. std::unique_ptr<TestEventTarget> root(new TestEventTarget());
  358. std::unique_ptr<SelfDestroyingTestEventTarget> target(
  359. new SelfDestroyingTestEventTarget());
  360. root->SetEventTargeter(
  361. std::make_unique<TestEventTargeter>(target.get(), false));
  362. TestEventProcessor* processor = target->processor();
  363. processor->SetRoot(std::move(root));
  364. MouseEvent mouse(ET_MOUSE_MOVED, gfx::Point(10, 10), gfx::Point(10, 10),
  365. EventTimeForNow(), EF_NONE, EF_NONE);
  366. EXPECT_TRUE(processor->OnEventFromSource(&mouse).dispatcher_destroyed);
  367. }
  368. namespace {
  369. class SelfDestroyingTestEventTargeter : public TestEventTargeter {
  370. public:
  371. explicit SelfDestroyingTestEventTargeter(std::unique_ptr<EventTarget> root)
  372. : TestEventTargeter(nullptr, false),
  373. processor_(new TestEventProcessor()) {
  374. processor_->SetRoot(std::move(root));
  375. }
  376. SelfDestroyingTestEventTargeter(const SelfDestroyingTestEventTarget&) =
  377. delete;
  378. SelfDestroyingTestEventTargeter& operator=(
  379. const SelfDestroyingTestEventTargeter&) = delete;
  380. ~SelfDestroyingTestEventTargeter() override = default;
  381. // EventTargeter:
  382. EventTarget* FindTargetForEvent(EventTarget* root, Event* event) override {
  383. processor_.reset();
  384. return nullptr;
  385. }
  386. EventProcessor* processor() { return processor_.get(); }
  387. private:
  388. std::unique_ptr<TestEventProcessor> processor_;
  389. };
  390. } // namespace
  391. TEST(EventProcessorCrashTest, DestroyDuringFindTarget) {
  392. std::unique_ptr<TestEventTarget> root(new TestEventTarget());
  393. auto* root_ptr = root.get();
  394. auto event_targeter =
  395. std::make_unique<SelfDestroyingTestEventTargeter>(std::move(root));
  396. auto* processor = event_targeter->processor();
  397. root_ptr->SetEventTargeter(std::move(event_targeter));
  398. MouseEvent mouse(ET_MOUSE_MOVED, gfx::Point(10, 10), gfx::Point(10, 10),
  399. EventTimeForNow(), EF_NONE, EF_NONE);
  400. EXPECT_TRUE(processor->OnEventFromSource(&mouse).dispatcher_destroyed);
  401. }
  402. } // namespace test
  403. } // namespace ui