OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/base/events/event_dispatcher.h" | 5 #include "ui/base/events/event_dispatcher.h" |
6 | 6 |
7 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
8 | 8 |
9 namespace ui { | 9 namespace ui { |
10 | 10 |
(...skipping 30 matching lines...) Expand all Loading... |
41 std::vector<int> handler_list_; | 41 std::vector<int> handler_list_; |
42 | 42 |
43 DISALLOW_COPY_AND_ASSIGN(TestTarget); | 43 DISALLOW_COPY_AND_ASSIGN(TestTarget); |
44 }; | 44 }; |
45 | 45 |
46 class TestEventHandler : public EventHandler { | 46 class TestEventHandler : public EventHandler { |
47 public: | 47 public: |
48 TestEventHandler(int id) | 48 TestEventHandler(int id) |
49 : id_(id), | 49 : id_(id), |
50 event_result_(ER_UNHANDLED), | 50 event_result_(ER_UNHANDLED), |
51 expected_phase_(EP_PREDISPATCH) { | 51 expect_pre_target_(false), |
| 52 expect_post_target_(false), |
| 53 received_pre_target_(false) { |
52 } | 54 } |
53 | 55 |
54 virtual ~TestEventHandler() {} | 56 virtual ~TestEventHandler() {} |
55 | 57 |
56 void ReceivedEvent(Event* event) { | 58 void ReceivedEvent(Event* event) { |
57 EXPECT_EQ(expected_phase_, event->phase()); | |
58 static_cast<TestTarget*>(event->target())->AddHandlerId(id_); | 59 static_cast<TestTarget*>(event->target())->AddHandlerId(id_); |
| 60 if (event->phase() == ui::EP_POSTTARGET) { |
| 61 EXPECT_TRUE(expect_post_target_); |
| 62 if (expect_pre_target_) |
| 63 EXPECT_TRUE(received_pre_target_); |
| 64 } else if (event->phase() == ui::EP_PRETARGET) { |
| 65 EXPECT_TRUE(expect_pre_target_); |
| 66 received_pre_target_ = true; |
| 67 } else { |
| 68 NOTREACHED(); |
| 69 } |
59 } | 70 } |
60 | 71 |
61 void set_event_result(EventResult result) { event_result_ = result; } | 72 void set_event_result(EventResult result) { event_result_ = result; } |
62 void set_expected_phase(EventPhase phase) { expected_phase_ = phase; } | 73 |
| 74 void set_expect_pre_target(bool expect) { expect_pre_target_ = expect; } |
| 75 void set_expect_post_target(bool expect) { expect_post_target_ = expect; } |
63 | 76 |
64 private: | 77 private: |
65 // Overridden from EventHandler: | 78 // Overridden from EventHandler: |
66 virtual EventResult OnKeyEvent(KeyEvent* event) OVERRIDE { | 79 virtual EventResult OnKeyEvent(KeyEvent* event) OVERRIDE { |
67 ReceivedEvent(event); | 80 ReceivedEvent(event); |
68 return event_result_; | 81 return event_result_; |
69 } | 82 } |
70 | 83 |
71 virtual EventResult OnMouseEvent(MouseEvent* event) OVERRIDE { | 84 virtual EventResult OnMouseEvent(MouseEvent* event) OVERRIDE { |
72 ReceivedEvent(event); | 85 ReceivedEvent(event); |
(...skipping 10 matching lines...) Expand all Loading... |
83 return ui::TOUCH_STATUS_UNKNOWN; | 96 return ui::TOUCH_STATUS_UNKNOWN; |
84 } | 97 } |
85 | 98 |
86 virtual EventResult OnGestureEvent(GestureEvent* event) OVERRIDE { | 99 virtual EventResult OnGestureEvent(GestureEvent* event) OVERRIDE { |
87 ReceivedEvent(event); | 100 ReceivedEvent(event); |
88 return event_result_; | 101 return event_result_; |
89 } | 102 } |
90 | 103 |
91 int id_; | 104 int id_; |
92 EventResult event_result_; | 105 EventResult event_result_; |
93 EventPhase expected_phase_; | 106 bool expect_pre_target_; |
| 107 bool expect_post_target_; |
| 108 bool received_pre_target_; |
94 | 109 |
95 DISALLOW_COPY_AND_ASSIGN(TestEventHandler); | 110 DISALLOW_COPY_AND_ASSIGN(TestEventHandler); |
96 }; | 111 }; |
97 | 112 |
98 class TestEventDispatcher : public EventDispatcher { | 113 class TestEventDispatcher : public EventDispatcher { |
99 public: | 114 public: |
100 TestEventDispatcher() {} | 115 TestEventDispatcher() {} |
101 virtual ~TestEventDispatcher() {} | 116 virtual ~TestEventDispatcher() {} |
102 | 117 |
103 private: | 118 private: |
104 // Overridden from EventDispatcher: | 119 // Overridden from EventDispatcher: |
105 virtual bool CanDispatchToTarget(EventTarget* target) OVERRIDE { | 120 virtual bool CanDispatchToTarget(EventTarget* target) OVERRIDE { |
106 return true; | 121 return true; |
107 } | 122 } |
108 | 123 |
109 virtual void ProcessPreTargetList(EventHandlerList* list) OVERRIDE { | |
110 for (EventHandlerList::iterator i = list->begin(); i != list->end(); ++i) { | |
111 static_cast<TestEventHandler*>(*i)->set_expected_phase(EP_PRETARGET); | |
112 } | |
113 } | |
114 | |
115 virtual void ProcessPostTargetList(EventHandlerList* list) OVERRIDE { | |
116 for (EventHandlerList::iterator i = list->begin(); i != list->end(); ++i) { | |
117 static_cast<TestEventHandler*>(*i)->set_expected_phase(EP_POSTTARGET); | |
118 } | |
119 } | |
120 | |
121 DISALLOW_COPY_AND_ASSIGN(TestEventDispatcher); | 124 DISALLOW_COPY_AND_ASSIGN(TestEventDispatcher); |
122 }; | 125 }; |
123 | 126 |
124 } // namespace | 127 } // namespace |
125 | 128 |
126 TEST(EventDispatcherTest, EventDispatchOrder) { | 129 TEST(EventDispatcherTest, EventDispatchOrder) { |
127 TestEventDispatcher dispatcher; | 130 TestEventDispatcher dispatcher; |
128 TestTarget parent, child; | 131 TestTarget parent, child; |
129 TestEventHandler h1(1), h2(2), h3(3), h4(4); | 132 TestEventHandler h1(1), h2(2), h3(3), h4(4); |
130 TestEventHandler h5(5), h6(6), h7(7), h8(8); | 133 TestEventHandler h5(5), h6(6), h7(7), h8(8); |
131 | 134 |
132 child.set_parent(&parent); | 135 child.set_parent(&parent); |
133 | 136 |
134 parent.AddPreTargetHandler(&h1); | 137 parent.AddPreTargetHandler(&h1); |
135 parent.AddPreTargetHandler(&h2); | 138 parent.AddPreTargetHandler(&h2); |
136 | 139 |
137 child.AddPreTargetHandler(&h3); | 140 child.AddPreTargetHandler(&h3); |
138 child.AddPreTargetHandler(&h4); | 141 child.AddPreTargetHandler(&h4); |
139 | 142 |
| 143 h1.set_expect_pre_target(true); |
| 144 h2.set_expect_pre_target(true); |
| 145 h3.set_expect_pre_target(true); |
| 146 h4.set_expect_pre_target(true); |
| 147 |
140 child.AddPostTargetHandler(&h5); | 148 child.AddPostTargetHandler(&h5); |
141 child.AddPostTargetHandler(&h6); | 149 child.AddPostTargetHandler(&h6); |
142 | 150 |
143 parent.AddPostTargetHandler(&h7); | 151 parent.AddPostTargetHandler(&h7); |
144 parent.AddPostTargetHandler(&h8); | 152 parent.AddPostTargetHandler(&h8); |
145 | 153 |
| 154 h5.set_expect_post_target(true); |
| 155 h6.set_expect_post_target(true); |
| 156 h7.set_expect_post_target(true); |
| 157 h8.set_expect_post_target(true); |
| 158 |
146 MouseEvent mouse(ui::ET_MOUSE_MOVED, gfx::Point(3, 4), | 159 MouseEvent mouse(ui::ET_MOUSE_MOVED, gfx::Point(3, 4), |
147 gfx::Point(3, 4), 0); | 160 gfx::Point(3, 4), 0); |
148 Event::DispatcherApi event_mod(&mouse); | 161 Event::DispatcherApi event_mod(&mouse); |
149 int result = dispatcher.ProcessEvent(&child, &mouse); | 162 int result = dispatcher.ProcessEvent(&child, &mouse); |
150 EXPECT_FALSE(result & ER_CONSUMED); | 163 EXPECT_FALSE(result & ER_CONSUMED); |
151 EXPECT_FALSE(result & ER_HANDLED); | 164 EXPECT_FALSE(result & ER_HANDLED); |
152 | 165 |
153 int expected[] = { 1, 2, 3, 4, 5, 6, 7, 8 }; | 166 int expected[] = { 1, 2, 3, 4, 5, 6, 7, 8 }; |
154 EXPECT_EQ( | 167 EXPECT_EQ( |
155 std::vector<int>(expected, expected + sizeof(expected) / sizeof(int)), | 168 std::vector<int>(expected, expected + sizeof(expected) / sizeof(int)), |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
193 result = dispatcher.ProcessEvent(&child, &mouse); | 206 result = dispatcher.ProcessEvent(&child, &mouse); |
194 EXPECT_EQ(EP_POSTDISPATCH, mouse.phase()); | 207 EXPECT_EQ(EP_POSTDISPATCH, mouse.phase()); |
195 EXPECT_EQ(result, mouse.result()); | 208 EXPECT_EQ(result, mouse.result()); |
196 EXPECT_TRUE(result & ER_CONSUMED); | 209 EXPECT_TRUE(result & ER_CONSUMED); |
197 EXPECT_FALSE(result & ER_HANDLED); | 210 EXPECT_FALSE(result & ER_HANDLED); |
198 EXPECT_EQ( | 211 EXPECT_EQ( |
199 std::vector<int>(exp, exp + sizeof(exp) / sizeof(int)), | 212 std::vector<int>(exp, exp + sizeof(exp) / sizeof(int)), |
200 child.handler_list()); | 213 child.handler_list()); |
201 } | 214 } |
202 | 215 |
| 216 // Tests that the event-phases are correct. |
| 217 TEST(EventDispatcherTest, EventDispatchPhase) { |
| 218 TestEventDispatcher dispatcher; |
| 219 TestTarget target; |
| 220 |
| 221 TestEventHandler handler(11); |
| 222 |
| 223 target.AddPreTargetHandler(&handler); |
| 224 target.AddPostTargetHandler(&handler); |
| 225 handler.set_expect_pre_target(true); |
| 226 handler.set_expect_post_target(true); |
| 227 |
| 228 MouseEvent mouse(ui::ET_MOUSE_MOVED, gfx::Point(3, 4), |
| 229 gfx::Point(3, 4), 0); |
| 230 Event::DispatcherApi event_mod(&mouse); |
| 231 int result = dispatcher.ProcessEvent(&target, &mouse); |
| 232 EXPECT_EQ(ER_UNHANDLED, result); |
| 233 |
| 234 int handlers[] = { 11, 11 }; |
| 235 EXPECT_EQ( |
| 236 std::vector<int>(handlers, handlers + sizeof(handlers) / sizeof(int)), |
| 237 target.handler_list()); |
| 238 } |
| 239 |
203 } // namespace ui | 240 } // namespace ui |
OLD | NEW |