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 <vector> | 5 #include <vector> |
6 | 6 |
7 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
8 #include "ui/events/event.h" | 8 #include "ui/events/event.h" |
9 #include "ui/events/event_targeter.h" | 9 #include "ui/events/event_targeter.h" |
10 #include "ui/events/test/events_test_utils.h" | 10 #include "ui/events/test/events_test_utils.h" |
11 #include "ui/events/test/test_event_handler.h" | 11 #include "ui/events/test/test_event_handler.h" |
12 #include "ui/events/test/test_event_processor.h" | 12 #include "ui/events/test/test_event_processor.h" |
13 #include "ui/events/test/test_event_target.h" | 13 #include "ui/events/test/test_event_target.h" |
14 | 14 |
15 typedef std::vector<std::string> HandlerSequenceRecorder; | 15 typedef std::vector<std::string> HandlerSequenceRecorder; |
16 | 16 |
17 namespace ui { | 17 namespace ui { |
18 namespace test { | 18 namespace test { |
19 | 19 |
20 class EventProcessorTest : public testing::Test { | 20 class EventProcessorTest : public testing::Test { |
21 public: | 21 public: |
22 EventProcessorTest() {} | 22 EventProcessorTest() {} |
23 virtual ~EventProcessorTest() {} | 23 virtual ~EventProcessorTest() {} |
24 | 24 |
25 // testing::Test: | 25 // testing::Test: |
26 virtual void SetUp() OVERRIDE { | 26 virtual void SetUp() OVERRIDE { |
27 processor_.SetRoot(scoped_ptr<EventTarget>(new TestEventTarget())); | 27 processor_.SetRoot(scoped_ptr<EventTarget>(new TestEventTarget())); |
28 processor_.ResetCounts(); | 28 processor_.Reset(); |
29 root()->SetEventTargeter(make_scoped_ptr(new EventTargeter())); | 29 root()->SetEventTargeter(make_scoped_ptr(new EventTargeter())); |
30 } | 30 } |
31 | 31 |
32 TestEventTarget* root() { | 32 TestEventTarget* root() { |
33 return static_cast<TestEventTarget*>(processor_.GetRootTarget()); | 33 return static_cast<TestEventTarget*>(processor_.GetRootTarget()); |
34 } | 34 } |
35 | 35 |
36 TestEventProcessor* processor() { | 36 TestEventProcessor* processor() { |
37 return &processor_; | 37 return &processor_; |
38 } | 38 } |
(...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
273 // handled, and we expect OnEventProcessingFinished() to be invoked once. | 273 // handled, and we expect OnEventProcessingFinished() to be invoked once. |
274 MouseEvent mouse(ET_MOUSE_MOVED, gfx::Point(10, 10), gfx::Point(10, 10), | 274 MouseEvent mouse(ET_MOUSE_MOVED, gfx::Point(10, 10), gfx::Point(10, 10), |
275 EF_NONE, EF_NONE); | 275 EF_NONE, EF_NONE); |
276 DispatchEvent(&mouse); | 276 DispatchEvent(&mouse); |
277 EXPECT_TRUE(root()->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED)); | 277 EXPECT_TRUE(root()->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED)); |
278 EXPECT_FALSE(root()->DidReceiveEvent(ET_MOUSE_MOVED)); | 278 EXPECT_FALSE(root()->DidReceiveEvent(ET_MOUSE_MOVED)); |
279 EXPECT_TRUE(mouse.handled()); | 279 EXPECT_TRUE(mouse.handled()); |
280 EXPECT_EQ(1, processor()->num_times_processing_finished()); | 280 EXPECT_EQ(1, processor()->num_times_processing_finished()); |
281 } | 281 } |
282 | 282 |
| 283 // Verifies that OnEventProcessingStarted() has been called when starting to |
| 284 // process an event, and that processing does not take place if |
| 285 // OnEventProcessingStarted() marks the event as handled. Also verifies that |
| 286 // OnEventProcessingFinished() is also called in either case. |
| 287 TEST_F(EventProcessorTest, OnEventProcessingStarted) { |
| 288 scoped_ptr<TestEventTarget> child(new TestEventTarget()); |
| 289 root()->AddChild(child.Pass()); |
| 290 |
| 291 // Dispatch a mouse event. We expect the event to be seen by the target, |
| 292 // OnEventProcessingStarted() should be called once, and |
| 293 // OnEventProcessingFinished() should be called once. The event should |
| 294 // remain unhandled. |
| 295 MouseEvent mouse( |
| 296 ET_MOUSE_MOVED, gfx::Point(10, 10), gfx::Point(10, 10), EF_NONE, EF_NONE); |
| 297 DispatchEvent(&mouse); |
| 298 EXPECT_TRUE(root()->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED)); |
| 299 EXPECT_FALSE(root()->DidReceiveEvent(ET_MOUSE_MOVED)); |
| 300 EXPECT_FALSE(mouse.handled()); |
| 301 EXPECT_EQ(1, processor()->num_times_processing_started()); |
| 302 EXPECT_EQ(1, processor()->num_times_processing_finished()); |
| 303 processor()->Reset(); |
| 304 root()->ResetReceivedEvents(); |
| 305 root()->child_at(0)->ResetReceivedEvents(); |
| 306 |
| 307 // Dispatch another mouse event, but with OnEventProcessingStarted() marking |
| 308 // the event as handled to prevent processing. We expect the event to not be |
| 309 // seen by the target this time, but OnEventProcessingStarted() and |
| 310 // OnEventProcessingFinished() should both still be called once. |
| 311 processor()->set_should_processing_occur(false); |
| 312 MouseEvent mouse2( |
| 313 ET_MOUSE_MOVED, gfx::Point(10, 10), gfx::Point(10, 10), EF_NONE, EF_NONE); |
| 314 DispatchEvent(&mouse2); |
| 315 EXPECT_FALSE(root()->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED)); |
| 316 EXPECT_FALSE(root()->DidReceiveEvent(ET_MOUSE_MOVED)); |
| 317 EXPECT_TRUE(mouse2.handled()); |
| 318 EXPECT_EQ(1, processor()->num_times_processing_started()); |
| 319 EXPECT_EQ(1, processor()->num_times_processing_finished()); |
| 320 } |
| 321 |
283 class IgnoreEventTargeter : public EventTargeter { | 322 class IgnoreEventTargeter : public EventTargeter { |
284 public: | 323 public: |
285 IgnoreEventTargeter() {} | 324 IgnoreEventTargeter() {} |
286 virtual ~IgnoreEventTargeter() {} | 325 virtual ~IgnoreEventTargeter() {} |
287 | 326 |
288 private: | 327 private: |
289 // EventTargeter: | 328 // EventTargeter: |
290 virtual bool SubtreeShouldBeExploredForEvent( | 329 virtual bool SubtreeShouldBeExploredForEvent( |
291 EventTarget* target, const LocatedEvent& event) OVERRIDE { | 330 EventTarget* target, const LocatedEvent& event) OVERRIDE { |
292 return false; | 331 return false; |
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
477 DispatchEvent(&mouse); | 516 DispatchEvent(&mouse); |
478 | 517 |
479 std::string expected[] = { "PreR", "PreC", "PreG", "G", "PostG", "PostC", | 518 std::string expected[] = { "PreR", "PreC", "PreG", "G", "PostG", "PostC", |
480 "PostR", "PreR", "PreC", "C", "PostC", "PostR", "PreR", "R", "PostR" }; | 519 "PostR", "PreR", "PreC", "C", "PostC", "PostR", "PreR", "R", "PostR" }; |
481 EXPECT_EQ(std::vector<std::string>( | 520 EXPECT_EQ(std::vector<std::string>( |
482 expected, expected + arraysize(expected)), recorder); | 521 expected, expected + arraysize(expected)), recorder); |
483 } | 522 } |
484 | 523 |
485 } // namespace test | 524 } // namespace test |
486 } // namespace ui | 525 } // namespace ui |
OLD | NEW |