Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "ui/base/events/event_dispatcher.h" | |
| 6 | |
| 7 #include "testing/gtest/include/gtest/gtest.h" | |
| 8 | |
| 9 namespace ui { | |
| 10 | |
| 11 namespace { | |
| 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 { | |
| 34 public: | |
| 35 TestTarget() : parent_(NULL) {} | |
| 36 virtual ~TestTarget() {} | |
| 37 | |
| 38 void set_parent(TestTarget* parent) { parent_ = parent; } | |
| 39 | |
| 40 void AddHandlerId(int id) { | |
| 41 handler_list_.push_back(id); | |
| 42 } | |
| 43 | |
| 44 const std::vector<int>& handler_list() const { return handler_list_; } | |
| 45 | |
| 46 void Reset() { | |
| 47 handler_list_.clear(); | |
| 48 } | |
| 49 | |
| 50 private: | |
| 51 // Overridden from EventTarget: | |
| 52 virtual bool CanAcceptEvents() OVERRIDE { | |
| 53 return true; | |
| 54 } | |
| 55 | |
| 56 virtual EventTarget* GetParentTarget() OVERRIDE { | |
| 57 return parent_; | |
| 58 } | |
| 59 | |
| 60 TestTarget* parent_; | |
| 61 std::vector<int> handler_list_; | |
| 62 | |
| 63 DISALLOW_COPY_AND_ASSIGN(TestTarget); | |
| 64 }; | |
| 65 | |
| 66 class TestEventHandler : public EventHandler { | |
| 67 public: | |
| 68 TestEventHandler(int id) | |
| 69 : id_(id), | |
| 70 event_result_(ER_UNHANDLED) { | |
| 71 } | |
| 72 | |
| 73 virtual ~TestEventHandler() {} | |
| 74 | |
| 75 void ReceivedEventForTarget(EventTarget* target) { | |
| 76 static_cast<TestTarget*>(target)->AddHandlerId(id_); | |
| 77 } | |
| 78 | |
| 79 void set_event_result(EventResult result) { event_result_ = result; } | |
| 80 | |
| 81 private: | |
| 82 // Overridden from EventHandler: | |
| 83 virtual EventResult OnKeyEvent(KeyEvent* event) OVERRIDE { | |
| 84 ReceivedEventForTarget(event->target()); | |
| 85 return event_result_; | |
| 86 } | |
| 87 | |
| 88 virtual EventResult OnMouseEvent(MouseEvent* event) OVERRIDE { | |
| 89 ReceivedEventForTarget(event->target()); | |
| 90 return event_result_; | |
| 91 } | |
| 92 | |
| 93 virtual EventResult OnScrollEvent(ScrollEvent* event) OVERRIDE { | |
| 94 ReceivedEventForTarget(event->target()); | |
| 95 return event_result_; | |
| 96 } | |
| 97 | |
| 98 virtual TouchStatus OnTouchEvent(TouchEvent* event) OVERRIDE { | |
| 99 ReceivedEventForTarget(event->target()); | |
| 100 return ui::TOUCH_STATUS_UNKNOWN; | |
| 101 } | |
| 102 | |
| 103 virtual EventResult OnGestureEvent(GestureEvent* event) OVERRIDE { | |
| 104 ReceivedEventForTarget(event->target()); | |
| 105 return event_result_; | |
| 106 } | |
| 107 | |
| 108 int id_; | |
| 109 EventResult event_result_; | |
| 110 | |
| 111 DISALLOW_COPY_AND_ASSIGN(TestEventHandler); | |
| 112 }; | |
| 113 | |
| 114 } // namespace | |
| 115 | |
| 116 TEST(EventDispatcherTest, EventDispatchOrder) { | |
| 117 TestEventDispatcher dispatcher; | |
| 118 TestTarget parent, child; | |
| 119 TestEventHandler h1(1), h2(2), h3(3), h4(4); | |
| 120 TestEventHandler h5(5), h6(6), h7(7), h8(8); | |
| 121 | |
| 122 child.set_parent(&parent); | |
| 123 | |
| 124 parent.AddPreTargetHandler(&h1); | |
| 125 parent.AddPreTargetHandler(&h2); | |
| 126 | |
| 127 child.AddPreTargetHandler(&h3); | |
| 128 child.AddPreTargetHandler(&h4); | |
| 129 | |
| 130 child.AddPostTargetHandler(&h5); | |
| 131 child.AddPostTargetHandler(&h6); | |
| 132 | |
| 133 parent.AddPostTargetHandler(&h7); | |
| 134 parent.AddPostTargetHandler(&h8); | |
| 135 | |
| 136 ui::MouseEvent mouse(ui::ET_MOUSE_MOVED, gfx::Point(3, 4), | |
| 137 gfx::Point(3, 4), 0); | |
| 138 int result = dispatcher.ProcessEvent(&child, &mouse); | |
| 139 EXPECT_FALSE(result & ER_CONSUMED); | |
| 140 EXPECT_FALSE(result & ER_HANDLED); | |
| 141 | |
| 142 // Note that the pre-handlers for the target itself (i.e. |child|) will not | |
| 143 // receive these events. | |
|
Ben Goodger (Google)
2012/09/09 19:43:16
Is this because you haven't made this behavioral c
sadrul
2012/09/10 05:04:01
Indeed. I have filed a bug and referenced it from
| |
| 144 int expected[] = { 1, 2, 5, 6, 7, 8 }; | |
| 145 EXPECT_EQ( | |
| 146 std::vector<int>(expected, expected + sizeof(expected) / sizeof(int)), | |
| 147 child.handler_list()); | |
| 148 | |
| 149 child.Reset(); | |
| 150 | |
| 151 h1.set_event_result(ER_HANDLED); | |
| 152 result = dispatcher.ProcessEvent(&child, &mouse); | |
| 153 EXPECT_FALSE(result & ER_CONSUMED); | |
| 154 EXPECT_TRUE(result & ER_HANDLED); | |
| 155 EXPECT_EQ( | |
| 156 std::vector<int>(expected, expected + sizeof(expected) / sizeof(int)), | |
| 157 child.handler_list()); | |
| 158 | |
| 159 child.Reset(); | |
| 160 | |
| 161 int nexpected[] = { 1, 2, 5 }; | |
| 162 h5.set_event_result(ER_CONSUMED); | |
| 163 result = dispatcher.ProcessEvent(&child, &mouse); | |
| 164 EXPECT_TRUE(result & ER_CONSUMED); | |
| 165 EXPECT_TRUE(result & ER_HANDLED); | |
| 166 EXPECT_EQ( | |
| 167 std::vector<int>(nexpected, nexpected + sizeof(nexpected) / sizeof(int)), | |
| 168 child.handler_list()); | |
| 169 | |
| 170 child.Reset(); | |
| 171 | |
| 172 int exp[] = { 1 }; | |
| 173 h1.set_event_result(ER_CONSUMED); | |
| 174 result = dispatcher.ProcessEvent(&child, &mouse); | |
| 175 EXPECT_TRUE(result & ER_CONSUMED); | |
| 176 EXPECT_FALSE(result & ER_HANDLED); | |
| 177 EXPECT_EQ( | |
| 178 std::vector<int>(exp, exp + sizeof(exp) / sizeof(int)), | |
| 179 child.handler_list()); | |
| 180 } | |
| 181 | |
| 182 } // namespace ui | |
| OLD | NEW |