| 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 <utility> | 5 #include <utility> |
| 6 #include <vector> | 6 #include <vector> |
| 7 | 7 |
| 8 #include "base/macros.h" | 8 #include "base/macros.h" |
| 9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 std::unique_ptr<TestEventTarget> child(new TestEventTarget()); | 64 std::unique_ptr<TestEventTarget> child(new TestEventTarget()); |
| 65 child->SetEventTargeter( | 65 child->SetEventTargeter( |
| 66 base::MakeUnique<TestEventTargeter>(child.get(), false)); | 66 base::MakeUnique<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( |
| 71 EventTimeForNow(), EF_NONE, EF_NONE); | 71 ET_MOUSE_MOVED, gfx::Point(10, 10), gfx::Point(10, 10), EventTimeForNow(), |
| 72 EF_NONE, EF_NONE, |
| 73 ui::PointerDetails(ui::EventPointerType::POINTER_TYPE_MOUSE)); |
| 72 DispatchEvent(&mouse); | 74 DispatchEvent(&mouse); |
| 73 EXPECT_TRUE(root()->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED)); | 75 EXPECT_TRUE(root()->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED)); |
| 74 EXPECT_FALSE(root()->DidReceiveEvent(ET_MOUSE_MOVED)); | 76 EXPECT_FALSE(root()->DidReceiveEvent(ET_MOUSE_MOVED)); |
| 75 | 77 |
| 76 SetTarget(root()); | 78 SetTarget(root()); |
| 77 root()->RemoveChild(root()->child_at(0)); | 79 root()->RemoveChild(root()->child_at(0)); |
| 78 DispatchEvent(&mouse); | 80 DispatchEvent(&mouse); |
| 79 EXPECT_TRUE(root()->DidReceiveEvent(ET_MOUSE_MOVED)); | 81 EXPECT_TRUE(root()->DidReceiveEvent(ET_MOUSE_MOVED)); |
| 80 } | 82 } |
| 81 | 83 |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 141 | 143 |
| 142 // Indicate that an event which is dispatched to the child target owned by the | 144 // Indicate that an event which is dispatched to the child target owned by the |
| 143 // first event processor should be handled by |target_handler| instead. | 145 // first event processor should be handled by |target_handler| instead. |
| 144 std::unique_ptr<TestEventHandler> target_handler( | 146 std::unique_ptr<TestEventHandler> target_handler( |
| 145 new ReDispatchEventHandler(second_processor.get(), root()->child_at(0))); | 147 new ReDispatchEventHandler(second_processor.get(), root()->child_at(0))); |
| 146 ignore_result(root()->child_at(0)->SetTargetHandler(target_handler.get())); | 148 ignore_result(root()->child_at(0)->SetTargetHandler(target_handler.get())); |
| 147 | 149 |
| 148 // Dispatch a mouse event to the tree of event targets owned by the first | 150 // Dispatch a mouse event to the tree of event targets owned by the first |
| 149 // event processor, checking in ReDispatchEventHandler that the phase and | 151 // event processor, checking in ReDispatchEventHandler that the phase and |
| 150 // target information of the event is correct. | 152 // target information of the event is correct. |
| 151 MouseEvent mouse(ET_MOUSE_MOVED, gfx::Point(10, 10), gfx::Point(10, 10), | 153 MouseEvent mouse( |
| 152 EventTimeForNow(), EF_NONE, EF_NONE); | 154 ET_MOUSE_MOVED, gfx::Point(10, 10), gfx::Point(10, 10), EventTimeForNow(), |
| 155 EF_NONE, EF_NONE, |
| 156 ui::PointerDetails(ui::EventPointerType::POINTER_TYPE_MOUSE)); |
| 153 DispatchEvent(&mouse); | 157 DispatchEvent(&mouse); |
| 154 | 158 |
| 155 // Verify also that |mouse| was seen by the child nodes contained in both | 159 // Verify also that |mouse| was seen by the child nodes contained in both |
| 156 // event processors and that the event was not handled. | 160 // event processors and that the event was not handled. |
| 157 EXPECT_EQ(1, target_handler->num_mouse_events()); | 161 EXPECT_EQ(1, target_handler->num_mouse_events()); |
| 158 EXPECT_TRUE(second_root->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED)); | 162 EXPECT_TRUE(second_root->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED)); |
| 159 EXPECT_FALSE(mouse.handled()); | 163 EXPECT_FALSE(mouse.handled()); |
| 160 second_root->child_at(0)->ResetReceivedEvents(); | 164 second_root->child_at(0)->ResetReceivedEvents(); |
| 161 root()->child_at(0)->ResetReceivedEvents(); | 165 root()->child_at(0)->ResetReceivedEvents(); |
| 162 | 166 |
| 163 target_handler->Reset(); | 167 target_handler->Reset(); |
| 164 | 168 |
| 165 // Indicate that the child of the second root should handle events, and | 169 // Indicate that the child of the second root should handle events, and |
| 166 // dispatch another mouse event to verify that it is marked as handled. | 170 // dispatch another mouse event to verify that it is marked as handled. |
| 167 second_root->child_at(0)->set_mark_events_as_handled(true); | 171 second_root->child_at(0)->set_mark_events_as_handled(true); |
| 168 MouseEvent mouse2(ET_MOUSE_MOVED, gfx::Point(10, 10), gfx::Point(10, 10), | 172 MouseEvent mouse2( |
| 169 EventTimeForNow(), EF_NONE, EF_NONE); | 173 ET_MOUSE_MOVED, gfx::Point(10, 10), gfx::Point(10, 10), EventTimeForNow(), |
| 174 EF_NONE, EF_NONE, |
| 175 ui::PointerDetails(ui::EventPointerType::POINTER_TYPE_MOUSE)); |
| 170 DispatchEvent(&mouse2); | 176 DispatchEvent(&mouse2); |
| 171 EXPECT_EQ(1, target_handler->num_mouse_events()); | 177 EXPECT_EQ(1, target_handler->num_mouse_events()); |
| 172 EXPECT_TRUE(second_root->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED)); | 178 EXPECT_TRUE(second_root->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED)); |
| 173 EXPECT_TRUE(mouse2.handled()); | 179 EXPECT_TRUE(mouse2.handled()); |
| 174 } | 180 } |
| 175 | 181 |
| 176 // Verifies that OnEventProcessingFinished() is called when an event | 182 // Verifies that OnEventProcessingFinished() is called when an event |
| 177 // has been handled. | 183 // has been handled. |
| 178 TEST_F(EventProcessorTest, OnEventProcessingFinished) { | 184 TEST_F(EventProcessorTest, OnEventProcessingFinished) { |
| 179 std::unique_ptr<TestEventTarget> child(new TestEventTarget()); | 185 std::unique_ptr<TestEventTarget> child(new TestEventTarget()); |
| 180 child->set_mark_events_as_handled(true); | 186 child->set_mark_events_as_handled(true); |
| 181 SetTarget(child.get()); | 187 SetTarget(child.get()); |
| 182 root()->AddChild(std::move(child)); | 188 root()->AddChild(std::move(child)); |
| 183 | 189 |
| 184 // Dispatch a mouse event. We expect the event to be seen by the target, | 190 // Dispatch a mouse event. We expect the event to be seen by the target, |
| 185 // handled, and we expect OnEventProcessingFinished() to be invoked once. | 191 // handled, and we expect OnEventProcessingFinished() to be invoked once. |
| 186 MouseEvent mouse(ET_MOUSE_MOVED, gfx::Point(10, 10), gfx::Point(10, 10), | 192 MouseEvent mouse( |
| 187 EventTimeForNow(), EF_NONE, EF_NONE); | 193 ET_MOUSE_MOVED, gfx::Point(10, 10), gfx::Point(10, 10), EventTimeForNow(), |
| 194 EF_NONE, EF_NONE, |
| 195 ui::PointerDetails(ui::EventPointerType::POINTER_TYPE_MOUSE)); |
| 188 DispatchEvent(&mouse); | 196 DispatchEvent(&mouse); |
| 189 EXPECT_TRUE(root()->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED)); | 197 EXPECT_TRUE(root()->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED)); |
| 190 EXPECT_FALSE(root()->DidReceiveEvent(ET_MOUSE_MOVED)); | 198 EXPECT_FALSE(root()->DidReceiveEvent(ET_MOUSE_MOVED)); |
| 191 EXPECT_TRUE(mouse.handled()); | 199 EXPECT_TRUE(mouse.handled()); |
| 192 EXPECT_EQ(1, processor()->num_times_processing_finished()); | 200 EXPECT_EQ(1, processor()->num_times_processing_finished()); |
| 193 } | 201 } |
| 194 | 202 |
| 195 // Verifies that OnEventProcessingStarted() has been called when starting to | 203 // Verifies that OnEventProcessingStarted() has been called when starting to |
| 196 // process an event, and that processing does not take place if | 204 // process an event, and that processing does not take place if |
| 197 // OnEventProcessingStarted() marks the event as handled. Also verifies that | 205 // OnEventProcessingStarted() marks the event as handled. Also verifies that |
| 198 // OnEventProcessingFinished() is also called in either case. | 206 // OnEventProcessingFinished() is also called in either case. |
| 199 TEST_F(EventProcessorTest, OnEventProcessingStarted) { | 207 TEST_F(EventProcessorTest, OnEventProcessingStarted) { |
| 200 std::unique_ptr<TestEventTarget> child(new TestEventTarget()); | 208 std::unique_ptr<TestEventTarget> child(new TestEventTarget()); |
| 201 SetTarget(child.get()); | 209 SetTarget(child.get()); |
| 202 root()->AddChild(std::move(child)); | 210 root()->AddChild(std::move(child)); |
| 203 | 211 |
| 204 // Dispatch a mouse event. We expect the event to be seen by the target, | 212 // Dispatch a mouse event. We expect the event to be seen by the target, |
| 205 // OnEventProcessingStarted() should be called once, and | 213 // OnEventProcessingStarted() should be called once, and |
| 206 // OnEventProcessingFinished() should be called once. The event should | 214 // OnEventProcessingFinished() should be called once. The event should |
| 207 // remain unhandled. | 215 // remain unhandled. |
| 208 MouseEvent mouse(ET_MOUSE_MOVED, gfx::Point(10, 10), gfx::Point(10, 10), | 216 MouseEvent mouse( |
| 209 EventTimeForNow(), EF_NONE, EF_NONE); | 217 ET_MOUSE_MOVED, gfx::Point(10, 10), gfx::Point(10, 10), EventTimeForNow(), |
| 218 EF_NONE, EF_NONE, |
| 219 ui::PointerDetails(ui::EventPointerType::POINTER_TYPE_MOUSE)); |
| 210 DispatchEvent(&mouse); | 220 DispatchEvent(&mouse); |
| 211 EXPECT_TRUE(root()->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED)); | 221 EXPECT_TRUE(root()->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED)); |
| 212 EXPECT_FALSE(root()->DidReceiveEvent(ET_MOUSE_MOVED)); | 222 EXPECT_FALSE(root()->DidReceiveEvent(ET_MOUSE_MOVED)); |
| 213 EXPECT_FALSE(mouse.handled()); | 223 EXPECT_FALSE(mouse.handled()); |
| 214 EXPECT_EQ(1, processor()->num_times_processing_started()); | 224 EXPECT_EQ(1, processor()->num_times_processing_started()); |
| 215 EXPECT_EQ(1, processor()->num_times_processing_finished()); | 225 EXPECT_EQ(1, processor()->num_times_processing_finished()); |
| 216 processor()->Reset(); | 226 processor()->Reset(); |
| 217 root()->ResetReceivedEvents(); | 227 root()->ResetReceivedEvents(); |
| 218 root()->child_at(0)->ResetReceivedEvents(); | 228 root()->child_at(0)->ResetReceivedEvents(); |
| 219 | 229 |
| 220 // Dispatch another mouse event, but with OnEventProcessingStarted() marking | 230 // Dispatch another mouse event, but with OnEventProcessingStarted() marking |
| 221 // the event as handled to prevent processing. We expect the event to not be | 231 // the event as handled to prevent processing. We expect the event to not be |
| 222 // seen by the target this time, but OnEventProcessingStarted() and | 232 // seen by the target this time, but OnEventProcessingStarted() and |
| 223 // OnEventProcessingFinished() should both still be called once. | 233 // OnEventProcessingFinished() should both still be called once. |
| 224 processor()->set_should_processing_occur(false); | 234 processor()->set_should_processing_occur(false); |
| 225 MouseEvent mouse2(ET_MOUSE_MOVED, gfx::Point(10, 10), gfx::Point(10, 10), | 235 MouseEvent mouse2( |
| 226 EventTimeForNow(), EF_NONE, EF_NONE); | 236 ET_MOUSE_MOVED, gfx::Point(10, 10), gfx::Point(10, 10), EventTimeForNow(), |
| 237 EF_NONE, EF_NONE, |
| 238 ui::PointerDetails(ui::EventPointerType::POINTER_TYPE_MOUSE)); |
| 227 DispatchEvent(&mouse2); | 239 DispatchEvent(&mouse2); |
| 228 EXPECT_FALSE(root()->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED)); | 240 EXPECT_FALSE(root()->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED)); |
| 229 EXPECT_FALSE(root()->DidReceiveEvent(ET_MOUSE_MOVED)); | 241 EXPECT_FALSE(root()->DidReceiveEvent(ET_MOUSE_MOVED)); |
| 230 EXPECT_TRUE(mouse2.handled()); | 242 EXPECT_TRUE(mouse2.handled()); |
| 231 EXPECT_EQ(1, processor()->num_times_processing_started()); | 243 EXPECT_EQ(1, processor()->num_times_processing_started()); |
| 232 EXPECT_EQ(1, processor()->num_times_processing_finished()); | 244 EXPECT_EQ(1, processor()->num_times_processing_finished()); |
| 233 } | 245 } |
| 234 | 246 |
| 235 // Tests that unhandled events are correctly dispatched to the next-best | 247 // Tests that unhandled events are correctly dispatched to the next-best |
| 236 // target as decided by the TestEventTargeter. | 248 // target as decided by the TestEventTargeter. |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 356 TestEventHandler post_child; | 368 TestEventHandler post_child; |
| 357 post_child.set_handler_name("PostC"); | 369 post_child.set_handler_name("PostC"); |
| 358 post_child.set_recorder(&recorder); | 370 post_child.set_recorder(&recorder); |
| 359 child_r->AddPostTargetHandler(&post_child); | 371 child_r->AddPostTargetHandler(&post_child); |
| 360 | 372 |
| 361 TestEventHandler post_grandchild; | 373 TestEventHandler post_grandchild; |
| 362 post_grandchild.set_handler_name("PostG"); | 374 post_grandchild.set_handler_name("PostG"); |
| 363 post_grandchild.set_recorder(&recorder); | 375 post_grandchild.set_recorder(&recorder); |
| 364 grandchild_r->AddPostTargetHandler(&post_grandchild); | 376 grandchild_r->AddPostTargetHandler(&post_grandchild); |
| 365 | 377 |
| 366 MouseEvent mouse(ET_MOUSE_MOVED, gfx::Point(10, 10), gfx::Point(10, 10), | 378 MouseEvent mouse( |
| 367 EventTimeForNow(), EF_NONE, EF_NONE); | 379 ET_MOUSE_MOVED, gfx::Point(10, 10), gfx::Point(10, 10), EventTimeForNow(), |
| 380 EF_NONE, EF_NONE, |
| 381 ui::PointerDetails(ui::EventPointerType::POINTER_TYPE_MOUSE)); |
| 368 DispatchEvent(&mouse); | 382 DispatchEvent(&mouse); |
| 369 | 383 |
| 370 std::string expected[] = { "PreR", "PreC", "PreG", "G", "PostG", "PostC", | 384 std::string expected[] = { "PreR", "PreC", "PreG", "G", "PostG", "PostC", |
| 371 "PostR", "PreR", "PreC", "C", "PostC", "PostR", "PreR", "R", "PostR" }; | 385 "PostR", "PreR", "PreC", "C", "PostC", "PostR", "PreR", "R", "PostR" }; |
| 372 EXPECT_EQ(std::vector<std::string>( | 386 EXPECT_EQ(std::vector<std::string>( |
| 373 expected, expected + arraysize(expected)), recorder); | 387 expected, expected + arraysize(expected)), recorder); |
| 374 } | 388 } |
| 375 | 389 |
| 376 } // namespace test | 390 } // namespace test |
| 377 } // namespace ui | 391 } // namespace ui |
| OLD | NEW |