| 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 <vector> | 8 #include <vector> |
| 6 | 9 |
| 7 #include "base/macros.h" | 10 #include "base/macros.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| 9 #include "ui/events/event.h" | |
| 10 #include "ui/events/event_target_iterator.h" | 12 #include "ui/events/event_target_iterator.h" |
| 11 #include "ui/events/event_targeter.h" | 13 #include "ui/events/event_targeter.h" |
| 12 #include "ui/events/event_utils.h" | 14 #include "ui/events/event_utils.h" |
| 13 #include "ui/events/test/events_test_utils.h" | 15 #include "ui/events/test/events_test_utils.h" |
| 14 #include "ui/events/test/test_event_handler.h" | 16 #include "ui/events/test/test_event_handler.h" |
| 15 #include "ui/events/test/test_event_processor.h" | 17 #include "ui/events/test/test_event_processor.h" |
| 16 #include "ui/events/test/test_event_target.h" | 18 #include "ui/events/test/test_event_target.h" |
| 17 #include "ui/events/test/test_event_targeter.h" | 19 #include "ui/events/test/test_event_targeter.h" |
| 18 | 20 |
| 19 typedef std::vector<std::string> HandlerSequenceRecorder; | 21 typedef std::vector<std::string> HandlerSequenceRecorder; |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 56 TestEventProcessor processor_; | 58 TestEventProcessor processor_; |
| 57 | 59 |
| 58 DISALLOW_COPY_AND_ASSIGN(EventProcessorTest); | 60 DISALLOW_COPY_AND_ASSIGN(EventProcessorTest); |
| 59 }; | 61 }; |
| 60 | 62 |
| 61 TEST_F(EventProcessorTest, Basic) { | 63 TEST_F(EventProcessorTest, Basic) { |
| 62 scoped_ptr<TestEventTarget> child(new TestEventTarget()); | 64 scoped_ptr<TestEventTarget> child(new TestEventTarget()); |
| 63 child->SetEventTargeter( | 65 child->SetEventTargeter( |
| 64 make_scoped_ptr(new TestEventTargeter(child.get(), false))); | 66 make_scoped_ptr(new TestEventTargeter(child.get(), false))); |
| 65 SetTarget(child.get()); | 67 SetTarget(child.get()); |
| 66 root()->AddChild(child.Pass()); | 68 root()->AddChild(std::move(child)); |
| 67 | 69 |
| 68 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), |
| 69 EventTimeForNow(), EF_NONE, EF_NONE); | 71 EventTimeForNow(), EF_NONE, EF_NONE); |
| 70 DispatchEvent(&mouse); | 72 DispatchEvent(&mouse); |
| 71 EXPECT_TRUE(root()->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED)); | 73 EXPECT_TRUE(root()->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED)); |
| 72 EXPECT_FALSE(root()->DidReceiveEvent(ET_MOUSE_MOVED)); | 74 EXPECT_FALSE(root()->DidReceiveEvent(ET_MOUSE_MOVED)); |
| 73 | 75 |
| 74 SetTarget(root()); | 76 SetTarget(root()); |
| 75 root()->RemoveChild(root()->child_at(0)); | 77 root()->RemoveChild(root()->child_at(0)); |
| 76 DispatchEvent(&mouse); | 78 DispatchEvent(&mouse); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 114 DISALLOW_COPY_AND_ASSIGN(ReDispatchEventHandler); | 116 DISALLOW_COPY_AND_ASSIGN(ReDispatchEventHandler); |
| 115 }; | 117 }; |
| 116 | 118 |
| 117 // 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 |
| 118 // 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 |
| 119 // being processed by another event processor. | 121 // being processed by another event processor. |
| 120 TEST_F(EventProcessorTest, NestedEventProcessing) { | 122 TEST_F(EventProcessorTest, NestedEventProcessing) { |
| 121 // 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. |
| 122 scoped_ptr<TestEventTarget> child(new TestEventTarget()); | 124 scoped_ptr<TestEventTarget> child(new TestEventTarget()); |
| 123 SetTarget(child.get()); | 125 SetTarget(child.get()); |
| 124 root()->AddChild(child.Pass()); | 126 root()->AddChild(std::move(child)); |
| 125 | 127 |
| 126 // Define a second root target and child. | 128 // Define a second root target and child. |
| 127 scoped_ptr<EventTarget> second_root_scoped(new TestEventTarget()); | 129 scoped_ptr<EventTarget> second_root_scoped(new TestEventTarget()); |
| 128 TestEventTarget* second_root = | 130 TestEventTarget* second_root = |
| 129 static_cast<TestEventTarget*>(second_root_scoped.get()); | 131 static_cast<TestEventTarget*>(second_root_scoped.get()); |
| 130 scoped_ptr<TestEventTarget> second_child(new TestEventTarget()); | 132 scoped_ptr<TestEventTarget> second_child(new TestEventTarget()); |
| 131 second_root->SetEventTargeter( | 133 second_root->SetEventTargeter( |
| 132 make_scoped_ptr(new TestEventTargeter(second_child.get(), false))); | 134 make_scoped_ptr(new TestEventTargeter(second_child.get(), false))); |
| 133 second_root->AddChild(second_child.Pass()); | 135 second_root->AddChild(std::move(second_child)); |
| 134 | 136 |
| 135 // Define a second event processor which owns the second root. | 137 // Define a second event processor which owns the second root. |
| 136 scoped_ptr<TestEventProcessor> second_processor(new TestEventProcessor()); | 138 scoped_ptr<TestEventProcessor> second_processor(new TestEventProcessor()); |
| 137 second_processor->SetRoot(second_root_scoped.Pass()); | 139 second_processor->SetRoot(std::move(second_root_scoped)); |
| 138 | 140 |
| 139 // Indicate that an event which is dispatched to the child target owned by the | 141 // Indicate that an event which is dispatched to the child target owned by the |
| 140 // first event processor should be handled by |target_handler| instead. | 142 // first event processor should be handled by |target_handler| instead. |
| 141 scoped_ptr<TestEventHandler> target_handler( | 143 scoped_ptr<TestEventHandler> target_handler( |
| 142 new ReDispatchEventHandler(second_processor.get(), root()->child_at(0))); | 144 new ReDispatchEventHandler(second_processor.get(), root()->child_at(0))); |
| 143 ignore_result(root()->child_at(0)->SetTargetHandler(target_handler.get())); | 145 ignore_result(root()->child_at(0)->SetTargetHandler(target_handler.get())); |
| 144 | 146 |
| 145 // Dispatch a mouse event to the tree of event targets owned by the first | 147 // Dispatch a mouse event to the tree of event targets owned by the first |
| 146 // event processor, checking in ReDispatchEventHandler that the phase and | 148 // event processor, checking in ReDispatchEventHandler that the phase and |
| 147 // target information of the event is correct. | 149 // target information of the event is correct. |
| (...skipping 21 matching lines...) Expand all Loading... |
| 169 EXPECT_TRUE(second_root->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED)); | 171 EXPECT_TRUE(second_root->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED)); |
| 170 EXPECT_TRUE(mouse2.handled()); | 172 EXPECT_TRUE(mouse2.handled()); |
| 171 } | 173 } |
| 172 | 174 |
| 173 // Verifies that OnEventProcessingFinished() is called when an event | 175 // Verifies that OnEventProcessingFinished() is called when an event |
| 174 // has been handled. | 176 // has been handled. |
| 175 TEST_F(EventProcessorTest, OnEventProcessingFinished) { | 177 TEST_F(EventProcessorTest, OnEventProcessingFinished) { |
| 176 scoped_ptr<TestEventTarget> child(new TestEventTarget()); | 178 scoped_ptr<TestEventTarget> child(new TestEventTarget()); |
| 177 child->set_mark_events_as_handled(true); | 179 child->set_mark_events_as_handled(true); |
| 178 SetTarget(child.get()); | 180 SetTarget(child.get()); |
| 179 root()->AddChild(child.Pass()); | 181 root()->AddChild(std::move(child)); |
| 180 | 182 |
| 181 // Dispatch a mouse event. We expect the event to be seen by the target, | 183 // Dispatch a mouse event. We expect the event to be seen by the target, |
| 182 // handled, and we expect OnEventProcessingFinished() to be invoked once. | 184 // handled, and we expect OnEventProcessingFinished() to be invoked once. |
| 183 MouseEvent mouse(ET_MOUSE_MOVED, gfx::Point(10, 10), gfx::Point(10, 10), | 185 MouseEvent mouse(ET_MOUSE_MOVED, gfx::Point(10, 10), gfx::Point(10, 10), |
| 184 EventTimeForNow(), EF_NONE, EF_NONE); | 186 EventTimeForNow(), EF_NONE, EF_NONE); |
| 185 DispatchEvent(&mouse); | 187 DispatchEvent(&mouse); |
| 186 EXPECT_TRUE(root()->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED)); | 188 EXPECT_TRUE(root()->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED)); |
| 187 EXPECT_FALSE(root()->DidReceiveEvent(ET_MOUSE_MOVED)); | 189 EXPECT_FALSE(root()->DidReceiveEvent(ET_MOUSE_MOVED)); |
| 188 EXPECT_TRUE(mouse.handled()); | 190 EXPECT_TRUE(mouse.handled()); |
| 189 EXPECT_EQ(1, processor()->num_times_processing_finished()); | 191 EXPECT_EQ(1, processor()->num_times_processing_finished()); |
| 190 } | 192 } |
| 191 | 193 |
| 192 // Verifies that OnEventProcessingStarted() has been called when starting to | 194 // Verifies that OnEventProcessingStarted() has been called when starting to |
| 193 // process an event, and that processing does not take place if | 195 // process an event, and that processing does not take place if |
| 194 // OnEventProcessingStarted() marks the event as handled. Also verifies that | 196 // OnEventProcessingStarted() marks the event as handled. Also verifies that |
| 195 // OnEventProcessingFinished() is also called in either case. | 197 // OnEventProcessingFinished() is also called in either case. |
| 196 TEST_F(EventProcessorTest, OnEventProcessingStarted) { | 198 TEST_F(EventProcessorTest, OnEventProcessingStarted) { |
| 197 scoped_ptr<TestEventTarget> child(new TestEventTarget()); | 199 scoped_ptr<TestEventTarget> child(new TestEventTarget()); |
| 198 SetTarget(child.get()); | 200 SetTarget(child.get()); |
| 199 root()->AddChild(child.Pass()); | 201 root()->AddChild(std::move(child)); |
| 200 | 202 |
| 201 // Dispatch a mouse event. We expect the event to be seen by the target, | 203 // Dispatch a mouse event. We expect the event to be seen by the target, |
| 202 // OnEventProcessingStarted() should be called once, and | 204 // OnEventProcessingStarted() should be called once, and |
| 203 // OnEventProcessingFinished() should be called once. The event should | 205 // OnEventProcessingFinished() should be called once. The event should |
| 204 // remain unhandled. | 206 // remain unhandled. |
| 205 MouseEvent mouse(ET_MOUSE_MOVED, gfx::Point(10, 10), gfx::Point(10, 10), | 207 MouseEvent mouse(ET_MOUSE_MOVED, gfx::Point(10, 10), gfx::Point(10, 10), |
| 206 EventTimeForNow(), EF_NONE, EF_NONE); | 208 EventTimeForNow(), EF_NONE, EF_NONE); |
| 207 DispatchEvent(&mouse); | 209 DispatchEvent(&mouse); |
| 208 EXPECT_TRUE(root()->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED)); | 210 EXPECT_TRUE(root()->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED)); |
| 209 EXPECT_FALSE(root()->DidReceiveEvent(ET_MOUSE_MOVED)); | 211 EXPECT_FALSE(root()->DidReceiveEvent(ET_MOUSE_MOVED)); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 231 | 233 |
| 232 // Tests that unhandled events are correctly dispatched to the next-best | 234 // Tests that unhandled events are correctly dispatched to the next-best |
| 233 // target as decided by the TestEventTargeter. | 235 // target as decided by the TestEventTargeter. |
| 234 TEST_F(EventProcessorTest, DispatchToNextBestTarget) { | 236 TEST_F(EventProcessorTest, DispatchToNextBestTarget) { |
| 235 scoped_ptr<TestEventTarget> child(new TestEventTarget()); | 237 scoped_ptr<TestEventTarget> child(new TestEventTarget()); |
| 236 scoped_ptr<TestEventTarget> grandchild(new TestEventTarget()); | 238 scoped_ptr<TestEventTarget> grandchild(new TestEventTarget()); |
| 237 | 239 |
| 238 // Install a TestEventTargeter which permits bubbling. | 240 // Install a TestEventTargeter which permits bubbling. |
| 239 root()->SetEventTargeter( | 241 root()->SetEventTargeter( |
| 240 make_scoped_ptr(new TestEventTargeter(grandchild.get(), true))); | 242 make_scoped_ptr(new TestEventTargeter(grandchild.get(), true))); |
| 241 child->AddChild(grandchild.Pass()); | 243 child->AddChild(std::move(grandchild)); |
| 242 root()->AddChild(child.Pass()); | 244 root()->AddChild(std::move(child)); |
| 243 | 245 |
| 244 ASSERT_EQ(1u, root()->child_count()); | 246 ASSERT_EQ(1u, root()->child_count()); |
| 245 ASSERT_EQ(1u, root()->child_at(0)->child_count()); | 247 ASSERT_EQ(1u, root()->child_at(0)->child_count()); |
| 246 ASSERT_EQ(0u, root()->child_at(0)->child_at(0)->child_count()); | 248 ASSERT_EQ(0u, root()->child_at(0)->child_at(0)->child_count()); |
| 247 | 249 |
| 248 TestEventTarget* child_r = root()->child_at(0); | 250 TestEventTarget* child_r = root()->child_at(0); |
| 249 TestEventTarget* grandchild_r = child_r->child_at(0); | 251 TestEventTarget* grandchild_r = child_r->child_at(0); |
| 250 | 252 |
| 251 // When the root has a TestEventTargeter installed which permits bubbling, | 253 // When the root has a TestEventTargeter installed which permits bubbling, |
| 252 // events targeted at the grandchild target should be dispatched to all three | 254 // events targeted at the grandchild target should be dispatched to all three |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 305 // Tests that unhandled events are seen by the correct sequence of | 307 // Tests that unhandled events are seen by the correct sequence of |
| 306 // targets, pre-target handlers, and post-target handlers when | 308 // targets, pre-target handlers, and post-target handlers when |
| 307 // a TestEventTargeter is installed on the root target which permits bubbling. | 309 // a TestEventTargeter is installed on the root target which permits bubbling. |
| 308 TEST_F(EventProcessorTest, HandlerSequence) { | 310 TEST_F(EventProcessorTest, HandlerSequence) { |
| 309 scoped_ptr<TestEventTarget> child(new TestEventTarget()); | 311 scoped_ptr<TestEventTarget> child(new TestEventTarget()); |
| 310 scoped_ptr<TestEventTarget> grandchild(new TestEventTarget()); | 312 scoped_ptr<TestEventTarget> grandchild(new TestEventTarget()); |
| 311 | 313 |
| 312 // Install a TestEventTargeter which permits bubbling. | 314 // Install a TestEventTargeter which permits bubbling. |
| 313 root()->SetEventTargeter( | 315 root()->SetEventTargeter( |
| 314 make_scoped_ptr(new TestEventTargeter(grandchild.get(), true))); | 316 make_scoped_ptr(new TestEventTargeter(grandchild.get(), true))); |
| 315 child->AddChild(grandchild.Pass()); | 317 child->AddChild(std::move(grandchild)); |
| 316 root()->AddChild(child.Pass()); | 318 root()->AddChild(std::move(child)); |
| 317 | 319 |
| 318 ASSERT_EQ(1u, root()->child_count()); | 320 ASSERT_EQ(1u, root()->child_count()); |
| 319 ASSERT_EQ(1u, root()->child_at(0)->child_count()); | 321 ASSERT_EQ(1u, root()->child_at(0)->child_count()); |
| 320 ASSERT_EQ(0u, root()->child_at(0)->child_at(0)->child_count()); | 322 ASSERT_EQ(0u, root()->child_at(0)->child_at(0)->child_count()); |
| 321 | 323 |
| 322 TestEventTarget* child_r = root()->child_at(0); | 324 TestEventTarget* child_r = root()->child_at(0); |
| 323 TestEventTarget* grandchild_r = child_r->child_at(0); | 325 TestEventTarget* grandchild_r = child_r->child_at(0); |
| 324 | 326 |
| 325 HandlerSequenceRecorder recorder; | 327 HandlerSequenceRecorder recorder; |
| 326 root()->set_target_name("R"); | 328 root()->set_target_name("R"); |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 365 DispatchEvent(&mouse); | 367 DispatchEvent(&mouse); |
| 366 | 368 |
| 367 std::string expected[] = { "PreR", "PreC", "PreG", "G", "PostG", "PostC", | 369 std::string expected[] = { "PreR", "PreC", "PreG", "G", "PostG", "PostC", |
| 368 "PostR", "PreR", "PreC", "C", "PostC", "PostR", "PreR", "R", "PostR" }; | 370 "PostR", "PreR", "PreC", "C", "PostC", "PostR", "PreR", "R", "PostR" }; |
| 369 EXPECT_EQ(std::vector<std::string>( | 371 EXPECT_EQ(std::vector<std::string>( |
| 370 expected, expected + arraysize(expected)), recorder); | 372 expected, expected + arraysize(expected)), recorder); |
| 371 } | 373 } |
| 372 | 374 |
| 373 } // namespace test | 375 } // namespace test |
| 374 } // namespace ui | 376 } // namespace ui |
| OLD | NEW |