OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/events/event.h" | |
6 | |
7 #include <utility> | 5 #include <utility> |
8 #include <vector> | 6 #include <vector> |
9 | 7 |
10 #include "base/macros.h" | 8 #include "base/macros.h" |
| 9 #include "base/memory/ptr_util.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 #include "ui/events/event.h" |
12 #include "ui/events/event_target_iterator.h" | 12 #include "ui/events/event_target_iterator.h" |
13 #include "ui/events/event_targeter.h" | 13 #include "ui/events/event_targeter.h" |
14 #include "ui/events/event_utils.h" | 14 #include "ui/events/event_utils.h" |
15 #include "ui/events/test/events_test_utils.h" | 15 #include "ui/events/test/events_test_utils.h" |
16 #include "ui/events/test/test_event_handler.h" | 16 #include "ui/events/test/test_event_handler.h" |
17 #include "ui/events/test/test_event_processor.h" | 17 #include "ui/events/test/test_event_processor.h" |
18 #include "ui/events/test/test_event_target.h" | 18 #include "ui/events/test/test_event_target.h" |
19 #include "ui/events/test/test_event_targeter.h" | 19 #include "ui/events/test/test_event_targeter.h" |
20 | 20 |
21 typedef std::vector<std::string> HandlerSequenceRecorder; | 21 typedef std::vector<std::string> HandlerSequenceRecorder; |
22 | 22 |
23 namespace ui { | 23 namespace ui { |
24 namespace test { | 24 namespace test { |
25 | 25 |
26 class EventProcessorTest : public testing::Test { | 26 class EventProcessorTest : public testing::Test { |
27 public: | 27 public: |
28 EventProcessorTest() {} | 28 EventProcessorTest() {} |
29 ~EventProcessorTest() override {} | 29 ~EventProcessorTest() override {} |
30 | 30 |
31 protected: | 31 protected: |
32 // testing::Test: | 32 // testing::Test: |
33 void SetUp() override { | 33 void SetUp() override { |
34 processor_.SetRoot(make_scoped_ptr(new TestEventTarget())); | 34 processor_.SetRoot(base::WrapUnique(new TestEventTarget())); |
35 processor_.Reset(); | 35 processor_.Reset(); |
36 root()->SetEventTargeter( | 36 root()->SetEventTargeter( |
37 make_scoped_ptr(new TestEventTargeter(root(), false))); | 37 base::WrapUnique(new TestEventTargeter(root(), false))); |
38 } | 38 } |
39 | 39 |
40 TestEventTarget* root() { | 40 TestEventTarget* root() { |
41 return static_cast<TestEventTarget*>(processor_.GetRootTarget()); | 41 return static_cast<TestEventTarget*>(processor_.GetRootTarget()); |
42 } | 42 } |
43 | 43 |
44 TestEventProcessor* processor() { | 44 TestEventProcessor* processor() { |
45 return &processor_; | 45 return &processor_; |
46 } | 46 } |
47 | 47 |
48 void DispatchEvent(Event* event) { | 48 void DispatchEvent(Event* event) { |
49 processor_.OnEventFromSource(event); | 49 processor_.OnEventFromSource(event); |
50 } | 50 } |
51 | 51 |
52 void SetTarget(TestEventTarget* target) { | 52 void SetTarget(TestEventTarget* target) { |
53 static_cast<TestEventTargeter*>(root()->GetEventTargeter()) | 53 static_cast<TestEventTargeter*>(root()->GetEventTargeter()) |
54 ->set_target(target); | 54 ->set_target(target); |
55 } | 55 } |
56 | 56 |
57 private: | 57 private: |
58 TestEventProcessor processor_; | 58 TestEventProcessor processor_; |
59 | 59 |
60 DISALLOW_COPY_AND_ASSIGN(EventProcessorTest); | 60 DISALLOW_COPY_AND_ASSIGN(EventProcessorTest); |
61 }; | 61 }; |
62 | 62 |
63 TEST_F(EventProcessorTest, Basic) { | 63 TEST_F(EventProcessorTest, Basic) { |
64 scoped_ptr<TestEventTarget> child(new TestEventTarget()); | 64 std::unique_ptr<TestEventTarget> child(new TestEventTarget()); |
65 child->SetEventTargeter( | 65 child->SetEventTargeter( |
66 make_scoped_ptr(new TestEventTargeter(child.get(), false))); | 66 base::WrapUnique(new TestEventTargeter(child.get(), false))); |
67 SetTarget(child.get()); | 67 SetTarget(child.get()); |
68 root()->AddChild(std::move(child)); | 68 root()->AddChild(std::move(child)); |
69 | 69 |
70 MouseEvent mouse(ET_MOUSE_MOVED, gfx::Point(10, 10), gfx::Point(10, 10), | 70 MouseEvent mouse(ET_MOUSE_MOVED, gfx::Point(10, 10), gfx::Point(10, 10), |
71 EventTimeForNow(), EF_NONE, EF_NONE); | 71 EventTimeForNow(), EF_NONE, EF_NONE); |
72 DispatchEvent(&mouse); | 72 DispatchEvent(&mouse); |
73 EXPECT_TRUE(root()->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED)); | 73 EXPECT_TRUE(root()->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED)); |
74 EXPECT_FALSE(root()->DidReceiveEvent(ET_MOUSE_MOVED)); | 74 EXPECT_FALSE(root()->DidReceiveEvent(ET_MOUSE_MOVED)); |
75 | 75 |
76 SetTarget(root()); | 76 SetTarget(root()); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
114 EventTarget* expected_target_; | 114 EventTarget* expected_target_; |
115 | 115 |
116 DISALLOW_COPY_AND_ASSIGN(ReDispatchEventHandler); | 116 DISALLOW_COPY_AND_ASSIGN(ReDispatchEventHandler); |
117 }; | 117 }; |
118 | 118 |
119 // Verifies that the phase and target information of an event is not mutated | 119 // Verifies that the phase and target information of an event is not mutated |
120 // as a result of sending the event to an event processor while it is still | 120 // as a result of sending the event to an event processor while it is still |
121 // being processed by another event processor. | 121 // being processed by another event processor. |
122 TEST_F(EventProcessorTest, NestedEventProcessing) { | 122 TEST_F(EventProcessorTest, NestedEventProcessing) { |
123 // Add one child to the default event processor used in this test suite. | 123 // Add one child to the default event processor used in this test suite. |
124 scoped_ptr<TestEventTarget> child(new TestEventTarget()); | 124 std::unique_ptr<TestEventTarget> child(new TestEventTarget()); |
125 SetTarget(child.get()); | 125 SetTarget(child.get()); |
126 root()->AddChild(std::move(child)); | 126 root()->AddChild(std::move(child)); |
127 | 127 |
128 // Define a second root target and child. | 128 // Define a second root target and child. |
129 scoped_ptr<EventTarget> second_root_scoped(new TestEventTarget()); | 129 std::unique_ptr<EventTarget> second_root_scoped(new TestEventTarget()); |
130 TestEventTarget* second_root = | 130 TestEventTarget* second_root = |
131 static_cast<TestEventTarget*>(second_root_scoped.get()); | 131 static_cast<TestEventTarget*>(second_root_scoped.get()); |
132 scoped_ptr<TestEventTarget> second_child(new TestEventTarget()); | 132 std::unique_ptr<TestEventTarget> second_child(new TestEventTarget()); |
133 second_root->SetEventTargeter( | 133 second_root->SetEventTargeter( |
134 make_scoped_ptr(new TestEventTargeter(second_child.get(), false))); | 134 base::WrapUnique(new TestEventTargeter(second_child.get(), false))); |
135 second_root->AddChild(std::move(second_child)); | 135 second_root->AddChild(std::move(second_child)); |
136 | 136 |
137 // Define a second event processor which owns the second root. | 137 // Define a second event processor which owns the second root. |
138 scoped_ptr<TestEventProcessor> second_processor(new TestEventProcessor()); | 138 std::unique_ptr<TestEventProcessor> second_processor( |
| 139 new TestEventProcessor()); |
139 second_processor->SetRoot(std::move(second_root_scoped)); | 140 second_processor->SetRoot(std::move(second_root_scoped)); |
140 | 141 |
141 // Indicate that an event which is dispatched to the child target owned by the | 142 // Indicate that an event which is dispatched to the child target owned by the |
142 // first event processor should be handled by |target_handler| instead. | 143 // first event processor should be handled by |target_handler| instead. |
143 scoped_ptr<TestEventHandler> target_handler( | 144 std::unique_ptr<TestEventHandler> target_handler( |
144 new ReDispatchEventHandler(second_processor.get(), root()->child_at(0))); | 145 new ReDispatchEventHandler(second_processor.get(), root()->child_at(0))); |
145 ignore_result(root()->child_at(0)->SetTargetHandler(target_handler.get())); | 146 ignore_result(root()->child_at(0)->SetTargetHandler(target_handler.get())); |
146 | 147 |
147 // Dispatch a mouse event to the tree of event targets owned by the first | 148 // Dispatch a mouse event to the tree of event targets owned by the first |
148 // event processor, checking in ReDispatchEventHandler that the phase and | 149 // event processor, checking in ReDispatchEventHandler that the phase and |
149 // target information of the event is correct. | 150 // target information of the event is correct. |
150 MouseEvent mouse(ET_MOUSE_MOVED, gfx::Point(10, 10), gfx::Point(10, 10), | 151 MouseEvent mouse(ET_MOUSE_MOVED, gfx::Point(10, 10), gfx::Point(10, 10), |
151 EventTimeForNow(), EF_NONE, EF_NONE); | 152 EventTimeForNow(), EF_NONE, EF_NONE); |
152 DispatchEvent(&mouse); | 153 DispatchEvent(&mouse); |
153 | 154 |
(...skipping 14 matching lines...) Expand all Loading... |
168 EventTimeForNow(), EF_NONE, EF_NONE); | 169 EventTimeForNow(), EF_NONE, EF_NONE); |
169 DispatchEvent(&mouse2); | 170 DispatchEvent(&mouse2); |
170 EXPECT_EQ(1, target_handler->num_mouse_events()); | 171 EXPECT_EQ(1, target_handler->num_mouse_events()); |
171 EXPECT_TRUE(second_root->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED)); | 172 EXPECT_TRUE(second_root->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED)); |
172 EXPECT_TRUE(mouse2.handled()); | 173 EXPECT_TRUE(mouse2.handled()); |
173 } | 174 } |
174 | 175 |
175 // Verifies that OnEventProcessingFinished() is called when an event | 176 // Verifies that OnEventProcessingFinished() is called when an event |
176 // has been handled. | 177 // has been handled. |
177 TEST_F(EventProcessorTest, OnEventProcessingFinished) { | 178 TEST_F(EventProcessorTest, OnEventProcessingFinished) { |
178 scoped_ptr<TestEventTarget> child(new TestEventTarget()); | 179 std::unique_ptr<TestEventTarget> child(new TestEventTarget()); |
179 child->set_mark_events_as_handled(true); | 180 child->set_mark_events_as_handled(true); |
180 SetTarget(child.get()); | 181 SetTarget(child.get()); |
181 root()->AddChild(std::move(child)); | 182 root()->AddChild(std::move(child)); |
182 | 183 |
183 // Dispatch a mouse event. We expect the event to be seen by the target, | 184 // Dispatch a mouse event. We expect the event to be seen by the target, |
184 // handled, and we expect OnEventProcessingFinished() to be invoked once. | 185 // handled, and we expect OnEventProcessingFinished() to be invoked once. |
185 MouseEvent mouse(ET_MOUSE_MOVED, gfx::Point(10, 10), gfx::Point(10, 10), | 186 MouseEvent mouse(ET_MOUSE_MOVED, gfx::Point(10, 10), gfx::Point(10, 10), |
186 EventTimeForNow(), EF_NONE, EF_NONE); | 187 EventTimeForNow(), EF_NONE, EF_NONE); |
187 DispatchEvent(&mouse); | 188 DispatchEvent(&mouse); |
188 EXPECT_TRUE(root()->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED)); | 189 EXPECT_TRUE(root()->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED)); |
189 EXPECT_FALSE(root()->DidReceiveEvent(ET_MOUSE_MOVED)); | 190 EXPECT_FALSE(root()->DidReceiveEvent(ET_MOUSE_MOVED)); |
190 EXPECT_TRUE(mouse.handled()); | 191 EXPECT_TRUE(mouse.handled()); |
191 EXPECT_EQ(1, processor()->num_times_processing_finished()); | 192 EXPECT_EQ(1, processor()->num_times_processing_finished()); |
192 } | 193 } |
193 | 194 |
194 // Verifies that OnEventProcessingStarted() has been called when starting to | 195 // Verifies that OnEventProcessingStarted() has been called when starting to |
195 // process an event, and that processing does not take place if | 196 // process an event, and that processing does not take place if |
196 // OnEventProcessingStarted() marks the event as handled. Also verifies that | 197 // OnEventProcessingStarted() marks the event as handled. Also verifies that |
197 // OnEventProcessingFinished() is also called in either case. | 198 // OnEventProcessingFinished() is also called in either case. |
198 TEST_F(EventProcessorTest, OnEventProcessingStarted) { | 199 TEST_F(EventProcessorTest, OnEventProcessingStarted) { |
199 scoped_ptr<TestEventTarget> child(new TestEventTarget()); | 200 std::unique_ptr<TestEventTarget> child(new TestEventTarget()); |
200 SetTarget(child.get()); | 201 SetTarget(child.get()); |
201 root()->AddChild(std::move(child)); | 202 root()->AddChild(std::move(child)); |
202 | 203 |
203 // Dispatch a mouse event. We expect the event to be seen by the target, | 204 // Dispatch a mouse event. We expect the event to be seen by the target, |
204 // OnEventProcessingStarted() should be called once, and | 205 // OnEventProcessingStarted() should be called once, and |
205 // OnEventProcessingFinished() should be called once. The event should | 206 // OnEventProcessingFinished() should be called once. The event should |
206 // remain unhandled. | 207 // remain unhandled. |
207 MouseEvent mouse(ET_MOUSE_MOVED, gfx::Point(10, 10), gfx::Point(10, 10), | 208 MouseEvent mouse(ET_MOUSE_MOVED, gfx::Point(10, 10), gfx::Point(10, 10), |
208 EventTimeForNow(), EF_NONE, EF_NONE); | 209 EventTimeForNow(), EF_NONE, EF_NONE); |
209 DispatchEvent(&mouse); | 210 DispatchEvent(&mouse); |
(...skipping 17 matching lines...) Expand all Loading... |
227 EXPECT_FALSE(root()->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED)); | 228 EXPECT_FALSE(root()->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED)); |
228 EXPECT_FALSE(root()->DidReceiveEvent(ET_MOUSE_MOVED)); | 229 EXPECT_FALSE(root()->DidReceiveEvent(ET_MOUSE_MOVED)); |
229 EXPECT_TRUE(mouse2.handled()); | 230 EXPECT_TRUE(mouse2.handled()); |
230 EXPECT_EQ(1, processor()->num_times_processing_started()); | 231 EXPECT_EQ(1, processor()->num_times_processing_started()); |
231 EXPECT_EQ(1, processor()->num_times_processing_finished()); | 232 EXPECT_EQ(1, processor()->num_times_processing_finished()); |
232 } | 233 } |
233 | 234 |
234 // Tests that unhandled events are correctly dispatched to the next-best | 235 // Tests that unhandled events are correctly dispatched to the next-best |
235 // target as decided by the TestEventTargeter. | 236 // target as decided by the TestEventTargeter. |
236 TEST_F(EventProcessorTest, DispatchToNextBestTarget) { | 237 TEST_F(EventProcessorTest, DispatchToNextBestTarget) { |
237 scoped_ptr<TestEventTarget> child(new TestEventTarget()); | 238 std::unique_ptr<TestEventTarget> child(new TestEventTarget()); |
238 scoped_ptr<TestEventTarget> grandchild(new TestEventTarget()); | 239 std::unique_ptr<TestEventTarget> grandchild(new TestEventTarget()); |
239 | 240 |
240 // Install a TestEventTargeter which permits bubbling. | 241 // Install a TestEventTargeter which permits bubbling. |
241 root()->SetEventTargeter( | 242 root()->SetEventTargeter( |
242 make_scoped_ptr(new TestEventTargeter(grandchild.get(), true))); | 243 base::WrapUnique(new TestEventTargeter(grandchild.get(), true))); |
243 child->AddChild(std::move(grandchild)); | 244 child->AddChild(std::move(grandchild)); |
244 root()->AddChild(std::move(child)); | 245 root()->AddChild(std::move(child)); |
245 | 246 |
246 ASSERT_EQ(1u, root()->child_count()); | 247 ASSERT_EQ(1u, root()->child_count()); |
247 ASSERT_EQ(1u, root()->child_at(0)->child_count()); | 248 ASSERT_EQ(1u, root()->child_at(0)->child_count()); |
248 ASSERT_EQ(0u, root()->child_at(0)->child_at(0)->child_count()); | 249 ASSERT_EQ(0u, root()->child_at(0)->child_at(0)->child_count()); |
249 | 250 |
250 TestEventTarget* child_r = root()->child_at(0); | 251 TestEventTarget* child_r = root()->child_at(0); |
251 TestEventTarget* grandchild_r = child_r->child_at(0); | 252 TestEventTarget* grandchild_r = child_r->child_at(0); |
252 | 253 |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
301 root()->ResetReceivedEvents(); | 302 root()->ResetReceivedEvents(); |
302 child_r->ResetReceivedEvents(); | 303 child_r->ResetReceivedEvents(); |
303 grandchild_r->ResetReceivedEvents(); | 304 grandchild_r->ResetReceivedEvents(); |
304 child_r->set_mark_events_as_handled(false); | 305 child_r->set_mark_events_as_handled(false); |
305 } | 306 } |
306 | 307 |
307 // Tests that unhandled events are seen by the correct sequence of | 308 // Tests that unhandled events are seen by the correct sequence of |
308 // targets, pre-target handlers, and post-target handlers when | 309 // targets, pre-target handlers, and post-target handlers when |
309 // a TestEventTargeter is installed on the root target which permits bubbling. | 310 // a TestEventTargeter is installed on the root target which permits bubbling. |
310 TEST_F(EventProcessorTest, HandlerSequence) { | 311 TEST_F(EventProcessorTest, HandlerSequence) { |
311 scoped_ptr<TestEventTarget> child(new TestEventTarget()); | 312 std::unique_ptr<TestEventTarget> child(new TestEventTarget()); |
312 scoped_ptr<TestEventTarget> grandchild(new TestEventTarget()); | 313 std::unique_ptr<TestEventTarget> grandchild(new TestEventTarget()); |
313 | 314 |
314 // Install a TestEventTargeter which permits bubbling. | 315 // Install a TestEventTargeter which permits bubbling. |
315 root()->SetEventTargeter( | 316 root()->SetEventTargeter( |
316 make_scoped_ptr(new TestEventTargeter(grandchild.get(), true))); | 317 base::WrapUnique(new TestEventTargeter(grandchild.get(), true))); |
317 child->AddChild(std::move(grandchild)); | 318 child->AddChild(std::move(grandchild)); |
318 root()->AddChild(std::move(child)); | 319 root()->AddChild(std::move(child)); |
319 | 320 |
320 ASSERT_EQ(1u, root()->child_count()); | 321 ASSERT_EQ(1u, root()->child_count()); |
321 ASSERT_EQ(1u, root()->child_at(0)->child_count()); | 322 ASSERT_EQ(1u, root()->child_at(0)->child_count()); |
322 ASSERT_EQ(0u, root()->child_at(0)->child_at(0)->child_count()); | 323 ASSERT_EQ(0u, root()->child_at(0)->child_at(0)->child_count()); |
323 | 324 |
324 TestEventTarget* child_r = root()->child_at(0); | 325 TestEventTarget* child_r = root()->child_at(0); |
325 TestEventTarget* grandchild_r = child_r->child_at(0); | 326 TestEventTarget* grandchild_r = child_r->child_at(0); |
326 | 327 |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
367 DispatchEvent(&mouse); | 368 DispatchEvent(&mouse); |
368 | 369 |
369 std::string expected[] = { "PreR", "PreC", "PreG", "G", "PostG", "PostC", | 370 std::string expected[] = { "PreR", "PreC", "PreG", "G", "PostG", "PostC", |
370 "PostR", "PreR", "PreC", "C", "PostC", "PostR", "PreR", "R", "PostR" }; | 371 "PostR", "PreR", "PreC", "C", "PostC", "PostR", "PreR", "R", "PostR" }; |
371 EXPECT_EQ(std::vector<std::string>( | 372 EXPECT_EQ(std::vector<std::string>( |
372 expected, expected + arraysize(expected)), recorder); | 373 expected, expected + arraysize(expected)), recorder); |
373 } | 374 } |
374 | 375 |
375 } // namespace test | 376 } // namespace test |
376 } // namespace ui | 377 } // namespace ui |
OLD | NEW |