| 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 |
| 11 namespace { | 11 namespace { |
| 12 | 12 |
| 13 class TestEventDispatcher : public EventDispatcher { | |
| 14 public: | |
| 15 TestEventDispatcher() {} | |
| 16 virtual ~TestEventDispatcher() {} | |
| 17 | |
| 18 private: | |
| 19 // Overridden from EventDispatcher: | |
| 20 virtual bool CanDispatchToTarget(EventTarget* target) OVERRIDE { | |
| 21 return true; | |
| 22 } | |
| 23 | |
| 24 virtual void ProcessPreTargetList(EventHandlerList* list) OVERRIDE { | |
| 25 } | |
| 26 | |
| 27 virtual void ProcessPostTargetList(EventHandlerList* list) OVERRIDE { | |
| 28 } | |
| 29 | |
| 30 DISALLOW_COPY_AND_ASSIGN(TestEventDispatcher); | |
| 31 }; | |
| 32 | |
| 33 class TestTarget : public EventTarget { | 13 class TestTarget : public EventTarget { |
| 34 public: | 14 public: |
| 35 TestTarget() : parent_(NULL) {} | 15 TestTarget() : parent_(NULL) {} |
| 36 virtual ~TestTarget() {} | 16 virtual ~TestTarget() {} |
| 37 | 17 |
| 38 void set_parent(TestTarget* parent) { parent_ = parent; } | 18 void set_parent(TestTarget* parent) { parent_ = parent; } |
| 39 | 19 |
| 40 void AddHandlerId(int id) { | 20 void AddHandlerId(int id) { |
| 41 handler_list_.push_back(id); | 21 handler_list_.push_back(id); |
| 42 } | 22 } |
| (...skipping 17 matching lines...) Expand all Loading... |
| 60 TestTarget* parent_; | 40 TestTarget* parent_; |
| 61 std::vector<int> handler_list_; | 41 std::vector<int> handler_list_; |
| 62 | 42 |
| 63 DISALLOW_COPY_AND_ASSIGN(TestTarget); | 43 DISALLOW_COPY_AND_ASSIGN(TestTarget); |
| 64 }; | 44 }; |
| 65 | 45 |
| 66 class TestEventHandler : public EventHandler { | 46 class TestEventHandler : public EventHandler { |
| 67 public: | 47 public: |
| 68 TestEventHandler(int id) | 48 TestEventHandler(int id) |
| 69 : id_(id), | 49 : id_(id), |
| 70 event_result_(ER_UNHANDLED) { | 50 event_result_(ER_UNHANDLED), |
| 51 expected_phase_(EP_PREDISPATCH) { |
| 71 } | 52 } |
| 72 | 53 |
| 73 virtual ~TestEventHandler() {} | 54 virtual ~TestEventHandler() {} |
| 74 | 55 |
| 75 void ReceivedEventForTarget(EventTarget* target) { | 56 void ReceivedEvent(Event* event) { |
| 76 static_cast<TestTarget*>(target)->AddHandlerId(id_); | 57 EXPECT_EQ(expected_phase_, event->phase()); |
| 58 static_cast<TestTarget*>(event->target())->AddHandlerId(id_); |
| 77 } | 59 } |
| 78 | 60 |
| 79 void set_event_result(EventResult result) { event_result_ = result; } | 61 void set_event_result(EventResult result) { event_result_ = result; } |
| 62 void set_expected_phase(EventPhase phase) { expected_phase_ = phase; } |
| 80 | 63 |
| 81 private: | 64 private: |
| 82 // Overridden from EventHandler: | 65 // Overridden from EventHandler: |
| 83 virtual EventResult OnKeyEvent(KeyEvent* event) OVERRIDE { | 66 virtual EventResult OnKeyEvent(KeyEvent* event) OVERRIDE { |
| 84 ReceivedEventForTarget(event->target()); | 67 ReceivedEvent(event); |
| 85 return event_result_; | 68 return event_result_; |
| 86 } | 69 } |
| 87 | 70 |
| 88 virtual EventResult OnMouseEvent(MouseEvent* event) OVERRIDE { | 71 virtual EventResult OnMouseEvent(MouseEvent* event) OVERRIDE { |
| 89 ReceivedEventForTarget(event->target()); | 72 ReceivedEvent(event); |
| 90 return event_result_; | 73 return event_result_; |
| 91 } | 74 } |
| 92 | 75 |
| 93 virtual EventResult OnScrollEvent(ScrollEvent* event) OVERRIDE { | 76 virtual EventResult OnScrollEvent(ScrollEvent* event) OVERRIDE { |
| 94 ReceivedEventForTarget(event->target()); | 77 ReceivedEvent(event); |
| 95 return event_result_; | 78 return event_result_; |
| 96 } | 79 } |
| 97 | 80 |
| 98 virtual TouchStatus OnTouchEvent(TouchEvent* event) OVERRIDE { | 81 virtual TouchStatus OnTouchEvent(TouchEvent* event) OVERRIDE { |
| 99 ReceivedEventForTarget(event->target()); | 82 ReceivedEvent(event); |
| 100 return ui::TOUCH_STATUS_UNKNOWN; | 83 return ui::TOUCH_STATUS_UNKNOWN; |
| 101 } | 84 } |
| 102 | 85 |
| 103 virtual EventResult OnGestureEvent(GestureEvent* event) OVERRIDE { | 86 virtual EventResult OnGestureEvent(GestureEvent* event) OVERRIDE { |
| 104 ReceivedEventForTarget(event->target()); | 87 ReceivedEvent(event); |
| 105 return event_result_; | 88 return event_result_; |
| 106 } | 89 } |
| 107 | 90 |
| 108 int id_; | 91 int id_; |
| 109 EventResult event_result_; | 92 EventResult event_result_; |
| 93 EventPhase expected_phase_; |
| 110 | 94 |
| 111 DISALLOW_COPY_AND_ASSIGN(TestEventHandler); | 95 DISALLOW_COPY_AND_ASSIGN(TestEventHandler); |
| 112 }; | 96 }; |
| 113 | 97 |
| 98 class TestEventDispatcher : public EventDispatcher { |
| 99 public: |
| 100 TestEventDispatcher() {} |
| 101 virtual ~TestEventDispatcher() {} |
| 102 |
| 103 private: |
| 104 // Overridden from EventDispatcher: |
| 105 virtual bool CanDispatchToTarget(EventTarget* target) OVERRIDE { |
| 106 return true; |
| 107 } |
| 108 |
| 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); |
| 122 }; |
| 123 |
| 114 } // namespace | 124 } // namespace |
| 115 | 125 |
| 116 TEST(EventDispatcherTest, EventDispatchOrder) { | 126 TEST(EventDispatcherTest, EventDispatchOrder) { |
| 117 TestEventDispatcher dispatcher; | 127 TestEventDispatcher dispatcher; |
| 118 TestTarget parent, child; | 128 TestTarget parent, child; |
| 119 TestEventHandler h1(1), h2(2), h3(3), h4(4); | 129 TestEventHandler h1(1), h2(2), h3(3), h4(4); |
| 120 TestEventHandler h5(5), h6(6), h7(7), h8(8); | 130 TestEventHandler h5(5), h6(6), h7(7), h8(8); |
| 121 | 131 |
| 122 child.set_parent(&parent); | 132 child.set_parent(&parent); |
| 123 | 133 |
| 124 parent.AddPreTargetHandler(&h1); | 134 parent.AddPreTargetHandler(&h1); |
| 125 parent.AddPreTargetHandler(&h2); | 135 parent.AddPreTargetHandler(&h2); |
| 126 | 136 |
| 127 child.AddPreTargetHandler(&h3); | 137 child.AddPreTargetHandler(&h3); |
| 128 child.AddPreTargetHandler(&h4); | 138 child.AddPreTargetHandler(&h4); |
| 129 | 139 |
| 130 child.AddPostTargetHandler(&h5); | 140 child.AddPostTargetHandler(&h5); |
| 131 child.AddPostTargetHandler(&h6); | 141 child.AddPostTargetHandler(&h6); |
| 132 | 142 |
| 133 parent.AddPostTargetHandler(&h7); | 143 parent.AddPostTargetHandler(&h7); |
| 134 parent.AddPostTargetHandler(&h8); | 144 parent.AddPostTargetHandler(&h8); |
| 135 | 145 |
| 136 MouseEvent mouse(ui::ET_MOUSE_MOVED, gfx::Point(3, 4), | 146 MouseEvent mouse(ui::ET_MOUSE_MOVED, gfx::Point(3, 4), |
| 137 gfx::Point(3, 4), 0); | 147 gfx::Point(3, 4), 0); |
| 148 Event::DispatcherApi event_mod(&mouse); |
| 138 int result = dispatcher.ProcessEvent(&child, &mouse); | 149 int result = dispatcher.ProcessEvent(&child, &mouse); |
| 139 EXPECT_FALSE(result & ER_CONSUMED); | 150 EXPECT_FALSE(result & ER_CONSUMED); |
| 140 EXPECT_FALSE(result & ER_HANDLED); | 151 EXPECT_FALSE(result & ER_HANDLED); |
| 141 | 152 |
| 142 int expected[] = { 1, 2, 3, 4, 5, 6, 7, 8 }; | 153 int expected[] = { 1, 2, 3, 4, 5, 6, 7, 8 }; |
| 143 EXPECT_EQ( | 154 EXPECT_EQ( |
| 144 std::vector<int>(expected, expected + sizeof(expected) / sizeof(int)), | 155 std::vector<int>(expected, expected + sizeof(expected) / sizeof(int)), |
| 145 child.handler_list()); | 156 child.handler_list()); |
| 146 | 157 |
| 147 child.Reset(); | 158 child.Reset(); |
| 159 event_mod.set_phase(EP_PREDISPATCH); |
| 160 event_mod.set_result(ER_UNHANDLED); |
| 148 | 161 |
| 149 h1.set_event_result(ER_HANDLED); | 162 h1.set_event_result(ER_HANDLED); |
| 150 result = dispatcher.ProcessEvent(&child, &mouse); | 163 result = dispatcher.ProcessEvent(&child, &mouse); |
| 164 EXPECT_EQ(result, mouse.result()); |
| 165 EXPECT_EQ(EP_POSTDISPATCH, mouse.phase()); |
| 151 EXPECT_FALSE(result & ER_CONSUMED); | 166 EXPECT_FALSE(result & ER_CONSUMED); |
| 152 EXPECT_TRUE(result & ER_HANDLED); | 167 EXPECT_TRUE(result & ER_HANDLED); |
| 153 EXPECT_EQ( | 168 EXPECT_EQ( |
| 154 std::vector<int>(expected, expected + sizeof(expected) / sizeof(int)), | 169 std::vector<int>(expected, expected + sizeof(expected) / sizeof(int)), |
| 155 child.handler_list()); | 170 child.handler_list()); |
| 156 | 171 |
| 157 child.Reset(); | 172 child.Reset(); |
| 173 event_mod.set_phase(EP_PREDISPATCH); |
| 174 event_mod.set_result(ER_UNHANDLED); |
| 158 | 175 |
| 159 int nexpected[] = { 1, 2, 3, 4, 5 }; | 176 int nexpected[] = { 1, 2, 3, 4, 5 }; |
| 160 h5.set_event_result(ER_CONSUMED); | 177 h5.set_event_result(ER_CONSUMED); |
| 161 result = dispatcher.ProcessEvent(&child, &mouse); | 178 result = dispatcher.ProcessEvent(&child, &mouse); |
| 179 EXPECT_EQ(result, mouse.result()); |
| 180 EXPECT_EQ(EP_POSTDISPATCH, mouse.phase()); |
| 162 EXPECT_TRUE(result & ER_CONSUMED); | 181 EXPECT_TRUE(result & ER_CONSUMED); |
| 163 EXPECT_TRUE(result & ER_HANDLED); | 182 EXPECT_TRUE(result & ER_HANDLED); |
| 164 EXPECT_EQ( | 183 EXPECT_EQ( |
| 165 std::vector<int>(nexpected, nexpected + sizeof(nexpected) / sizeof(int)), | 184 std::vector<int>(nexpected, nexpected + sizeof(nexpected) / sizeof(int)), |
| 166 child.handler_list()); | 185 child.handler_list()); |
| 167 | 186 |
| 168 child.Reset(); | 187 child.Reset(); |
| 188 event_mod.set_phase(EP_PREDISPATCH); |
| 189 event_mod.set_result(ER_UNHANDLED); |
| 169 | 190 |
| 170 int exp[] = { 1 }; | 191 int exp[] = { 1 }; |
| 171 h1.set_event_result(ER_CONSUMED); | 192 h1.set_event_result(ER_CONSUMED); |
| 172 result = dispatcher.ProcessEvent(&child, &mouse); | 193 result = dispatcher.ProcessEvent(&child, &mouse); |
| 194 EXPECT_EQ(EP_POSTDISPATCH, mouse.phase()); |
| 195 EXPECT_EQ(result, mouse.result()); |
| 173 EXPECT_TRUE(result & ER_CONSUMED); | 196 EXPECT_TRUE(result & ER_CONSUMED); |
| 174 EXPECT_FALSE(result & ER_HANDLED); | 197 EXPECT_FALSE(result & ER_HANDLED); |
| 175 EXPECT_EQ( | 198 EXPECT_EQ( |
| 176 std::vector<int>(exp, exp + sizeof(exp) / sizeof(int)), | 199 std::vector<int>(exp, exp + sizeof(exp) / sizeof(int)), |
| 177 child.handler_list()); | 200 child.handler_list()); |
| 178 } | 201 } |
| 179 | 202 |
| 180 } // namespace ui | 203 } // namespace ui |
| OLD | NEW |