Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / ui / events / event_processor_unittest.cc
bloba0ab5b63f9088412978cd78d284262e34fbd909e
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.
5 #include <vector>
7 #include "testing/gtest/include/gtest/gtest.h"
8 #include "ui/events/event.h"
9 #include "ui/events/event_target_iterator.h"
10 #include "ui/events/event_targeter.h"
11 #include "ui/events/event_utils.h"
12 #include "ui/events/test/events_test_utils.h"
13 #include "ui/events/test/test_event_handler.h"
14 #include "ui/events/test/test_event_processor.h"
15 #include "ui/events/test/test_event_target.h"
16 #include "ui/events/test/test_event_targeter.h"
18 typedef std::vector<std::string> HandlerSequenceRecorder;
20 namespace ui {
21 namespace test {
23 class EventProcessorTest : public testing::Test {
24 public:
25 EventProcessorTest() {}
26 ~EventProcessorTest() override {}
28 protected:
29 // testing::Test:
30 void SetUp() override {
31 processor_.SetRoot(make_scoped_ptr(new TestEventTarget()));
32 processor_.Reset();
33 root()->SetEventTargeter(
34 make_scoped_ptr(new TestEventTargeter(root(), false)));
37 TestEventTarget* root() {
38 return static_cast<TestEventTarget*>(processor_.GetRootTarget());
41 TestEventProcessor* processor() {
42 return &processor_;
45 void DispatchEvent(Event* event) {
46 processor_.OnEventFromSource(event);
49 void SetTarget(TestEventTarget* target) {
50 static_cast<TestEventTargeter*>(root()->GetEventTargeter())
51 ->set_target(target);
54 private:
55 TestEventProcessor processor_;
57 DISALLOW_COPY_AND_ASSIGN(EventProcessorTest);
60 TEST_F(EventProcessorTest, Basic) {
61 scoped_ptr<TestEventTarget> child(new TestEventTarget());
62 child->SetEventTargeter(
63 make_scoped_ptr(new TestEventTargeter(child.get(), false)));
64 SetTarget(child.get());
65 root()->AddChild(child.Pass());
67 MouseEvent mouse(ET_MOUSE_MOVED, gfx::Point(10, 10), gfx::Point(10, 10),
68 EventTimeForNow(), EF_NONE, EF_NONE);
69 DispatchEvent(&mouse);
70 EXPECT_TRUE(root()->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED));
71 EXPECT_FALSE(root()->DidReceiveEvent(ET_MOUSE_MOVED));
73 SetTarget(root());
74 root()->RemoveChild(root()->child_at(0));
75 DispatchEvent(&mouse);
76 EXPECT_TRUE(root()->DidReceiveEvent(ET_MOUSE_MOVED));
79 // ReDispatchEventHandler is used to receive mouse events and forward them
80 // to a specified EventProcessor. Verifies that the event has the correct
81 // target and phase both before and after the nested event processing. Also
82 // verifies that the location of the event remains the same after it has
83 // been processed by the second EventProcessor.
84 class ReDispatchEventHandler : public TestEventHandler {
85 public:
86 ReDispatchEventHandler(EventProcessor* processor, EventTarget* target)
87 : processor_(processor), expected_target_(target) {}
88 ~ReDispatchEventHandler() override {}
90 // TestEventHandler:
91 void OnMouseEvent(MouseEvent* event) override {
92 TestEventHandler::OnMouseEvent(event);
94 EXPECT_EQ(expected_target_, event->target());
95 EXPECT_EQ(EP_TARGET, event->phase());
97 gfx::Point location(event->location());
98 EventDispatchDetails details = processor_->OnEventFromSource(event);
99 EXPECT_FALSE(details.dispatcher_destroyed);
100 EXPECT_FALSE(details.target_destroyed);
102 // The nested event-processing should not have mutated the target,
103 // phase, or location of |event|.
104 EXPECT_EQ(expected_target_, event->target());
105 EXPECT_EQ(EP_TARGET, event->phase());
106 EXPECT_EQ(location, event->location());
109 private:
110 EventProcessor* processor_;
111 EventTarget* expected_target_;
113 DISALLOW_COPY_AND_ASSIGN(ReDispatchEventHandler);
116 // Verifies that the phase and target information of an event is not mutated
117 // as a result of sending the event to an event processor while it is still
118 // being processed by another event processor.
119 TEST_F(EventProcessorTest, NestedEventProcessing) {
120 // Add one child to the default event processor used in this test suite.
121 scoped_ptr<TestEventTarget> child(new TestEventTarget());
122 SetTarget(child.get());
123 root()->AddChild(child.Pass());
125 // Define a second root target and child.
126 scoped_ptr<EventTarget> second_root_scoped(new TestEventTarget());
127 TestEventTarget* second_root =
128 static_cast<TestEventTarget*>(second_root_scoped.get());
129 scoped_ptr<TestEventTarget> second_child(new TestEventTarget());
130 second_root->SetEventTargeter(
131 make_scoped_ptr(new TestEventTargeter(second_child.get(), false)));
132 second_root->AddChild(second_child.Pass());
134 // Define a second event processor which owns the second root.
135 scoped_ptr<TestEventProcessor> second_processor(new TestEventProcessor());
136 second_processor->SetRoot(second_root_scoped.Pass());
138 // Indicate that an event which is dispatched to the child target owned by the
139 // first event processor should be handled by |target_handler| instead.
140 scoped_ptr<TestEventHandler> target_handler(
141 new ReDispatchEventHandler(second_processor.get(), root()->child_at(0)));
142 root()->child_at(0)->set_target_handler(target_handler.get());
144 // Dispatch a mouse event to the tree of event targets owned by the first
145 // event processor, checking in ReDispatchEventHandler that the phase and
146 // target information of the event is correct.
147 MouseEvent mouse(ET_MOUSE_MOVED, gfx::Point(10, 10), gfx::Point(10, 10),
148 EventTimeForNow(), EF_NONE, EF_NONE);
149 DispatchEvent(&mouse);
151 // Verify also that |mouse| was seen by the child nodes contained in both
152 // event processors and that the event was not handled.
153 EXPECT_TRUE(root()->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED));
154 EXPECT_TRUE(second_root->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED));
155 EXPECT_FALSE(mouse.handled());
156 second_root->child_at(0)->ResetReceivedEvents();
157 root()->child_at(0)->ResetReceivedEvents();
159 // Indicate that the child of the second root should handle events, and
160 // dispatch another mouse event to verify that it is marked as handled.
161 second_root->child_at(0)->set_mark_events_as_handled(true);
162 MouseEvent mouse2(ET_MOUSE_MOVED, gfx::Point(10, 10), gfx::Point(10, 10),
163 EventTimeForNow(), EF_NONE, EF_NONE);
164 DispatchEvent(&mouse2);
165 EXPECT_TRUE(root()->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED));
166 EXPECT_TRUE(second_root->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED));
167 EXPECT_TRUE(mouse2.handled());
170 // Verifies that OnEventProcessingFinished() is called when an event
171 // has been handled.
172 TEST_F(EventProcessorTest, OnEventProcessingFinished) {
173 scoped_ptr<TestEventTarget> child(new TestEventTarget());
174 child->set_mark_events_as_handled(true);
175 SetTarget(child.get());
176 root()->AddChild(child.Pass());
178 // Dispatch a mouse event. We expect the event to be seen by the target,
179 // handled, and we expect OnEventProcessingFinished() to be invoked once.
180 MouseEvent mouse(ET_MOUSE_MOVED, gfx::Point(10, 10), gfx::Point(10, 10),
181 EventTimeForNow(), EF_NONE, EF_NONE);
182 DispatchEvent(&mouse);
183 EXPECT_TRUE(root()->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED));
184 EXPECT_FALSE(root()->DidReceiveEvent(ET_MOUSE_MOVED));
185 EXPECT_TRUE(mouse.handled());
186 EXPECT_EQ(1, processor()->num_times_processing_finished());
189 // Verifies that OnEventProcessingStarted() has been called when starting to
190 // process an event, and that processing does not take place if
191 // OnEventProcessingStarted() marks the event as handled. Also verifies that
192 // OnEventProcessingFinished() is also called in either case.
193 TEST_F(EventProcessorTest, OnEventProcessingStarted) {
194 scoped_ptr<TestEventTarget> child(new TestEventTarget());
195 SetTarget(child.get());
196 root()->AddChild(child.Pass());
198 // Dispatch a mouse event. We expect the event to be seen by the target,
199 // OnEventProcessingStarted() should be called once, and
200 // OnEventProcessingFinished() should be called once. The event should
201 // remain unhandled.
202 MouseEvent mouse(ET_MOUSE_MOVED, gfx::Point(10, 10), gfx::Point(10, 10),
203 EventTimeForNow(), EF_NONE, EF_NONE);
204 DispatchEvent(&mouse);
205 EXPECT_TRUE(root()->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED));
206 EXPECT_FALSE(root()->DidReceiveEvent(ET_MOUSE_MOVED));
207 EXPECT_FALSE(mouse.handled());
208 EXPECT_EQ(1, processor()->num_times_processing_started());
209 EXPECT_EQ(1, processor()->num_times_processing_finished());
210 processor()->Reset();
211 root()->ResetReceivedEvents();
212 root()->child_at(0)->ResetReceivedEvents();
214 // Dispatch another mouse event, but with OnEventProcessingStarted() marking
215 // the event as handled to prevent processing. We expect the event to not be
216 // seen by the target this time, but OnEventProcessingStarted() and
217 // OnEventProcessingFinished() should both still be called once.
218 processor()->set_should_processing_occur(false);
219 MouseEvent mouse2(ET_MOUSE_MOVED, gfx::Point(10, 10), gfx::Point(10, 10),
220 EventTimeForNow(), EF_NONE, EF_NONE);
221 DispatchEvent(&mouse2);
222 EXPECT_FALSE(root()->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED));
223 EXPECT_FALSE(root()->DidReceiveEvent(ET_MOUSE_MOVED));
224 EXPECT_TRUE(mouse2.handled());
225 EXPECT_EQ(1, processor()->num_times_processing_started());
226 EXPECT_EQ(1, processor()->num_times_processing_finished());
229 // Tests that unhandled events are correctly dispatched to the next-best
230 // target as decided by the TestEventTargeter.
231 TEST_F(EventProcessorTest, DispatchToNextBestTarget) {
232 scoped_ptr<TestEventTarget> child(new TestEventTarget());
233 scoped_ptr<TestEventTarget> grandchild(new TestEventTarget());
235 // Install a TestEventTargeter which permits bubbling.
236 root()->SetEventTargeter(
237 make_scoped_ptr(new TestEventTargeter(grandchild.get(), true)));
238 child->AddChild(grandchild.Pass());
239 root()->AddChild(child.Pass());
241 ASSERT_EQ(1u, root()->child_count());
242 ASSERT_EQ(1u, root()->child_at(0)->child_count());
243 ASSERT_EQ(0u, root()->child_at(0)->child_at(0)->child_count());
245 TestEventTarget* child_r = root()->child_at(0);
246 TestEventTarget* grandchild_r = child_r->child_at(0);
248 // When the root has a TestEventTargeter installed which permits bubbling,
249 // events targeted at the grandchild target should be dispatched to all three
250 // targets.
251 KeyEvent key_event(ET_KEY_PRESSED, VKEY_ESCAPE, EF_NONE);
252 DispatchEvent(&key_event);
253 EXPECT_TRUE(root()->DidReceiveEvent(ET_KEY_PRESSED));
254 EXPECT_TRUE(child_r->DidReceiveEvent(ET_KEY_PRESSED));
255 EXPECT_TRUE(grandchild_r->DidReceiveEvent(ET_KEY_PRESSED));
256 root()->ResetReceivedEvents();
257 child_r->ResetReceivedEvents();
258 grandchild_r->ResetReceivedEvents();
260 // Add a pre-target handler on the child of the root that will mark the event
261 // as handled. No targets in the hierarchy should receive the event.
262 TestEventHandler handler;
263 child_r->AddPreTargetHandler(&handler);
264 key_event = KeyEvent(ET_KEY_PRESSED, VKEY_ESCAPE, EF_NONE);
265 DispatchEvent(&key_event);
266 EXPECT_FALSE(root()->DidReceiveEvent(ET_KEY_PRESSED));
267 EXPECT_FALSE(child_r->DidReceiveEvent(ET_KEY_PRESSED));
268 EXPECT_FALSE(grandchild_r->DidReceiveEvent(ET_KEY_PRESSED));
269 EXPECT_EQ(1, handler.num_key_events());
270 handler.Reset();
272 // Add a post-target handler on the child of the root that will mark the event
273 // as handled. Only the grandchild (the initial target) should receive the
274 // event.
275 child_r->RemovePreTargetHandler(&handler);
276 child_r->AddPostTargetHandler(&handler);
277 key_event = KeyEvent(ET_KEY_PRESSED, VKEY_ESCAPE, EF_NONE);
278 DispatchEvent(&key_event);
279 EXPECT_FALSE(root()->DidReceiveEvent(ET_KEY_PRESSED));
280 EXPECT_FALSE(child_r->DidReceiveEvent(ET_KEY_PRESSED));
281 EXPECT_TRUE(grandchild_r->DidReceiveEvent(ET_KEY_PRESSED));
282 EXPECT_EQ(1, handler.num_key_events());
283 handler.Reset();
284 grandchild_r->ResetReceivedEvents();
285 child_r->RemovePostTargetHandler(&handler);
287 // Mark the event as handled when it reaches the EP_TARGET phase of
288 // dispatch at the child of the root. The child and grandchild
289 // targets should both receive the event, but the root should not.
290 child_r->set_mark_events_as_handled(true);
291 key_event = KeyEvent(ET_KEY_PRESSED, VKEY_ESCAPE, EF_NONE);
292 DispatchEvent(&key_event);
293 EXPECT_FALSE(root()->DidReceiveEvent(ET_KEY_PRESSED));
294 EXPECT_TRUE(child_r->DidReceiveEvent(ET_KEY_PRESSED));
295 EXPECT_TRUE(grandchild_r->DidReceiveEvent(ET_KEY_PRESSED));
296 root()->ResetReceivedEvents();
297 child_r->ResetReceivedEvents();
298 grandchild_r->ResetReceivedEvents();
299 child_r->set_mark_events_as_handled(false);
302 // Tests that unhandled events are seen by the correct sequence of
303 // targets, pre-target handlers, and post-target handlers when
304 // a TestEventTargeter is installed on the root target which permits bubbling.
305 TEST_F(EventProcessorTest, HandlerSequence) {
306 scoped_ptr<TestEventTarget> child(new TestEventTarget());
307 scoped_ptr<TestEventTarget> grandchild(new TestEventTarget());
309 // Install a TestEventTargeter which permits bubbling.
310 root()->SetEventTargeter(
311 make_scoped_ptr(new TestEventTargeter(grandchild.get(), true)));
312 child->AddChild(grandchild.Pass());
313 root()->AddChild(child.Pass());
315 ASSERT_EQ(1u, root()->child_count());
316 ASSERT_EQ(1u, root()->child_at(0)->child_count());
317 ASSERT_EQ(0u, root()->child_at(0)->child_at(0)->child_count());
319 TestEventTarget* child_r = root()->child_at(0);
320 TestEventTarget* grandchild_r = child_r->child_at(0);
322 HandlerSequenceRecorder recorder;
323 root()->set_target_name("R");
324 root()->set_recorder(&recorder);
325 child_r->set_target_name("C");
326 child_r->set_recorder(&recorder);
327 grandchild_r->set_target_name("G");
328 grandchild_r->set_recorder(&recorder);
330 TestEventHandler pre_root;
331 pre_root.set_handler_name("PreR");
332 pre_root.set_recorder(&recorder);
333 root()->AddPreTargetHandler(&pre_root);
335 TestEventHandler pre_child;
336 pre_child.set_handler_name("PreC");
337 pre_child.set_recorder(&recorder);
338 child_r->AddPreTargetHandler(&pre_child);
340 TestEventHandler pre_grandchild;
341 pre_grandchild.set_handler_name("PreG");
342 pre_grandchild.set_recorder(&recorder);
343 grandchild_r->AddPreTargetHandler(&pre_grandchild);
345 TestEventHandler post_root;
346 post_root.set_handler_name("PostR");
347 post_root.set_recorder(&recorder);
348 root()->AddPostTargetHandler(&post_root);
350 TestEventHandler post_child;
351 post_child.set_handler_name("PostC");
352 post_child.set_recorder(&recorder);
353 child_r->AddPostTargetHandler(&post_child);
355 TestEventHandler post_grandchild;
356 post_grandchild.set_handler_name("PostG");
357 post_grandchild.set_recorder(&recorder);
358 grandchild_r->AddPostTargetHandler(&post_grandchild);
360 MouseEvent mouse(ET_MOUSE_MOVED, gfx::Point(10, 10), gfx::Point(10, 10),
361 EventTimeForNow(), EF_NONE, EF_NONE);
362 DispatchEvent(&mouse);
364 std::string expected[] = { "PreR", "PreC", "PreG", "G", "PostG", "PostC",
365 "PostR", "PreR", "PreC", "C", "PostC", "PostR", "PreR", "R", "PostR" };
366 EXPECT_EQ(std::vector<std::string>(
367 expected, expected + arraysize(expected)), recorder);
370 } // namespace test
371 } // namespace ui