| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/events/test/mock_motion_event.h" | 5 #include "ui/events/test/mock_motion_event.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 | 8 |
| 9 using base::TimeTicks; | 9 using base::TimeTicks; |
| 10 | 10 |
| 11 namespace ui { | 11 namespace ui { |
| 12 namespace test { | 12 namespace test { |
| 13 namespace { |
| 14 |
| 15 PointerProperties CreatePointer() { |
| 16 PointerProperties pointer; |
| 17 pointer.touch_major = MockMotionEvent::TOUCH_MAJOR; |
| 18 return pointer; |
| 19 } |
| 20 |
| 21 PointerProperties CreatePointer(float x, float y, int id) { |
| 22 PointerProperties pointer(x, y); |
| 23 pointer.touch_major = MockMotionEvent::TOUCH_MAJOR; |
| 24 pointer.id = id; |
| 25 return pointer; |
| 26 } |
| 27 |
| 28 |
| 29 } // namespace |
| 13 | 30 |
| 14 MockMotionEvent::MockMotionEvent() | 31 MockMotionEvent::MockMotionEvent() |
| 15 : action(ACTION_CANCEL), pointer_count(1), touch_major(TOUCH_MAJOR), id(0), | 32 : MotionEventGeneric(ACTION_CANCEL, base::TimeTicks(), CreatePointer()) { |
| 16 button_state(0) { | |
| 17 } | 33 } |
| 18 | 34 |
| 19 MockMotionEvent::MockMotionEvent(Action action) | 35 MockMotionEvent::MockMotionEvent(Action action) |
| 20 : action(action), pointer_count(1), touch_major(TOUCH_MAJOR), id(0), | 36 : MotionEventGeneric(action, base::TimeTicks(), CreatePointer()) { |
| 21 button_state(0) { | |
| 22 } | 37 } |
| 23 | 38 |
| 24 MockMotionEvent::MockMotionEvent(Action action, | 39 MockMotionEvent::MockMotionEvent(Action action, |
| 25 TimeTicks time, | 40 TimeTicks time, |
| 26 float x, | 41 float x0, |
| 27 float y) | 42 float y0) |
| 28 : action(action), | 43 : MotionEventGeneric(action, time, CreatePointer(x0, y0, 0)) { |
| 29 pointer_count(1), | |
| 30 time(time), | |
| 31 touch_major(TOUCH_MAJOR), | |
| 32 id(0), | |
| 33 button_state(0) { | |
| 34 points[0].SetPoint(x, y); | |
| 35 tool_types[0] = TOOL_TYPE_UNKNOWN; | |
| 36 } | 44 } |
| 37 | 45 |
| 38 MockMotionEvent::MockMotionEvent(Action action, | 46 MockMotionEvent::MockMotionEvent(Action action, |
| 39 TimeTicks time, | 47 TimeTicks time, |
| 40 float x0, | 48 float x0, |
| 41 float y0, | 49 float y0, |
| 42 float x1, | 50 float x1, |
| 43 float y1) | 51 float y1) |
| 44 : action(action), | 52 : MotionEventGeneric(action, time, CreatePointer(x0, y0, 0)) { |
| 45 pointer_count(2), | 53 PushPointer(x1, y1); |
| 46 time(time), | 54 if (action == ACTION_POINTER_UP || action == ACTION_POINTER_DOWN) |
| 47 touch_major(TOUCH_MAJOR), | 55 set_action_index(1); |
| 48 id(0), | |
| 49 button_state(0) { | |
| 50 points[0].SetPoint(x0, y0); | |
| 51 tool_types[0] = TOOL_TYPE_UNKNOWN; | |
| 52 points[1].SetPoint(x1, y1); | |
| 53 tool_types[1] = TOOL_TYPE_UNKNOWN; | |
| 54 } | 56 } |
| 55 | 57 |
| 56 MockMotionEvent::MockMotionEvent(Action action, | 58 MockMotionEvent::MockMotionEvent(Action action, |
| 57 TimeTicks time, | 59 TimeTicks time, |
| 58 float x0, | 60 float x0, |
| 59 float y0, | 61 float y0, |
| 60 float x1, | 62 float x1, |
| 61 float y1, | 63 float y1, |
| 62 float x2, | 64 float x2, |
| 63 float y2) | 65 float y2) |
| 64 : action(action), | 66 : MotionEventGeneric(action, time, CreatePointer(x0, y0, 0)) { |
| 65 pointer_count(3), | 67 PushPointer(x1, y1); |
| 66 time(time), | 68 PushPointer(x2, y2); |
| 67 touch_major(TOUCH_MAJOR), | 69 if (action == ACTION_POINTER_UP || action == ACTION_POINTER_DOWN) |
| 68 id(0), | 70 set_action_index(2); |
| 69 button_state(0) { | 71 } |
| 70 points[0].SetPoint(x0, y0); | 72 |
| 71 tool_types[0] = TOOL_TYPE_UNKNOWN; | 73 MockMotionEvent::MockMotionEvent(Action action, |
| 72 points[1].SetPoint(x1, y1); | 74 base::TimeTicks time, |
| 73 tool_types[1] = TOOL_TYPE_UNKNOWN; | 75 const std::vector<gfx::PointF>& positions) { |
| 74 points[2].SetPoint(x2, y2); | 76 set_action(action); |
| 75 tool_types[2] = TOOL_TYPE_UNKNOWN; | 77 set_event_time(time); |
| 78 if (action == ACTION_POINTER_UP || action == ACTION_POINTER_DOWN) |
| 79 set_action_index(static_cast<int>(positions.size()) - 1); |
| 80 for (size_t i = 0; i < positions.size(); ++i) |
| 81 PushPointer(positions[i].x(), positions[i].y()); |
| 76 } | 82 } |
| 77 | 83 |
| 78 MockMotionEvent::MockMotionEvent(const MockMotionEvent& other) | 84 MockMotionEvent::MockMotionEvent(const MockMotionEvent& other) |
| 79 : action(other.action), | 85 : MotionEventGeneric(other) { |
| 80 pointer_count(other.pointer_count), | |
| 81 time(other.time), | |
| 82 touch_major(other.touch_major), | |
| 83 id(other.GetId()), | |
| 84 button_state(other.GetButtonState()) { | |
| 85 for (size_t i = 0; i < pointer_count; ++i) { | |
| 86 points[i] = other.points[i]; | |
| 87 tool_types[i] = other.tool_types[i]; | |
| 88 } | |
| 89 } | 86 } |
| 90 | 87 |
| 91 MockMotionEvent::~MockMotionEvent() {} | 88 MockMotionEvent::~MockMotionEvent() {} |
| 92 | 89 |
| 93 MotionEvent::Action MockMotionEvent::GetAction() const { return action; } | |
| 94 | |
| 95 int MockMotionEvent::GetActionIndex() const { | |
| 96 return static_cast<int>(pointer_count) - 1; | |
| 97 } | |
| 98 | |
| 99 size_t MockMotionEvent::GetPointerCount() const { return pointer_count; } | |
| 100 | |
| 101 int MockMotionEvent::GetId() const { | |
| 102 return id; | |
| 103 } | |
| 104 | |
| 105 int MockMotionEvent::GetPointerId(size_t pointer_index) const { | |
| 106 DCHECK(pointer_index < pointer_count); | |
| 107 return static_cast<int>(pointer_index); | |
| 108 } | |
| 109 | |
| 110 float MockMotionEvent::GetX(size_t pointer_index) const { | |
| 111 return points[pointer_index].x(); | |
| 112 } | |
| 113 | |
| 114 float MockMotionEvent::GetY(size_t pointer_index) const { | |
| 115 return points[pointer_index].y(); | |
| 116 } | |
| 117 | |
| 118 float MockMotionEvent::GetRawX(size_t pointer_index) const { | |
| 119 return GetX(pointer_index) + raw_offset.x(); | |
| 120 } | |
| 121 | |
| 122 float MockMotionEvent::GetRawY(size_t pointer_index) const { | |
| 123 return GetY(pointer_index) + raw_offset.y(); | |
| 124 } | |
| 125 | |
| 126 float MockMotionEvent::GetTouchMajor(size_t pointer_index) const { | |
| 127 return touch_major; | |
| 128 } | |
| 129 | |
| 130 float MockMotionEvent::GetPressure(size_t pointer_index) const { | |
| 131 return 0; | |
| 132 } | |
| 133 | |
| 134 TimeTicks MockMotionEvent::GetEventTime() const { return time; } | |
| 135 | |
| 136 size_t MockMotionEvent::GetHistorySize() const { return 0; } | |
| 137 | |
| 138 TimeTicks MockMotionEvent::GetHistoricalEventTime( | |
| 139 size_t historical_index) const { | |
| 140 return TimeTicks(); | |
| 141 } | |
| 142 | |
| 143 float MockMotionEvent::GetHistoricalTouchMajor(size_t pointer_index, | |
| 144 size_t historical_index) const { | |
| 145 return 0; | |
| 146 } | |
| 147 | |
| 148 float MockMotionEvent::GetHistoricalX(size_t pointer_index, | |
| 149 size_t historical_index) const { | |
| 150 return 0; | |
| 151 } | |
| 152 | |
| 153 float MockMotionEvent::GetHistoricalY(size_t pointer_index, | |
| 154 size_t historical_index) const { | |
| 155 return 0; | |
| 156 } | |
| 157 | |
| 158 MotionEvent::ToolType MockMotionEvent::GetToolType(size_t pointer_index) const { | |
| 159 DCHECK_LT(pointer_index, pointer_count); | |
| 160 return tool_types[pointer_index]; | |
| 161 } | |
| 162 | |
| 163 int MockMotionEvent::GetButtonState() const { | |
| 164 return button_state; | |
| 165 } | |
| 166 | |
| 167 scoped_ptr<MotionEvent> MockMotionEvent::Clone() const { | 90 scoped_ptr<MotionEvent> MockMotionEvent::Clone() const { |
| 168 return scoped_ptr<MotionEvent>(new MockMotionEvent(*this)); | 91 return scoped_ptr<MotionEvent>(new MockMotionEvent(*this)); |
| 169 } | 92 } |
| 170 | 93 |
| 171 scoped_ptr<MotionEvent> MockMotionEvent::Cancel() const { | 94 scoped_ptr<MotionEvent> MockMotionEvent::Cancel() const { |
| 172 scoped_ptr<MockMotionEvent> cancel_event(new MockMotionEvent(*this)); | 95 scoped_ptr<MockMotionEvent> event(new MockMotionEvent(*this)); |
| 173 cancel_event->action = MotionEvent::ACTION_CANCEL; | 96 event->set_action(MotionEvent::ACTION_CANCEL); |
| 174 return cancel_event.PassAs<MotionEvent>(); | 97 return event.PassAs<MotionEvent>(); |
| 175 } | |
| 176 | |
| 177 void MockMotionEvent::SetId(int new_id) { | |
| 178 id = new_id; | |
| 179 } | |
| 180 | |
| 181 void MockMotionEvent::SetTime(base::TimeTicks new_time) { | |
| 182 time = new_time; | |
| 183 } | 98 } |
| 184 | 99 |
| 185 void MockMotionEvent::PressPoint(float x, float y) { | 100 void MockMotionEvent::PressPoint(float x, float y) { |
| 186 // Reset the pointer count if the previously released and/or cancelled pointer | 101 ResolvePointers(); |
| 187 // was the last pointer in the event. | 102 PushPointer(x, y); |
| 188 if (pointer_count == 1 && (action == ACTION_UP || action == ACTION_CANCEL)) | 103 if (GetPointerCount() > 1) { |
| 189 pointer_count = 0; | 104 set_action_index(static_cast<int>(GetPointerCount()) - 1); |
| 190 | 105 set_action(ACTION_POINTER_DOWN); |
| 191 DCHECK_LT(pointer_count, static_cast<size_t>(MAX_POINTERS)); | 106 } else { |
| 192 points[pointer_count++] = gfx::PointF(x, y); | 107 set_action(ACTION_DOWN); |
| 193 tool_types[pointer_count] = TOOL_TYPE_UNKNOWN; | 108 } |
| 194 action = pointer_count > 1 ? ACTION_POINTER_DOWN : ACTION_DOWN; | |
| 195 } | 109 } |
| 196 | 110 |
| 197 void MockMotionEvent::MovePoint(size_t index, float x, float y) { | 111 void MockMotionEvent::MovePoint(size_t index, float x, float y) { |
| 198 DCHECK_LT(index, pointer_count); | 112 ResolvePointers(); |
| 199 points[index] = gfx::PointF(x, y); | 113 DCHECK_LT(index, GetPointerCount()); |
| 200 tool_types[index] = TOOL_TYPE_UNKNOWN; | 114 PointerProperties& p = pointer(index); |
| 201 action = ACTION_MOVE; | 115 float dx = x - p.x; |
| 116 float dy = x - p.y; |
| 117 p.x = x; |
| 118 p.y = y; |
| 119 p.raw_x += dx; |
| 120 p.raw_y += dy; |
| 121 set_action(ACTION_MOVE); |
| 202 } | 122 } |
| 203 | 123 |
| 204 void MockMotionEvent::ReleasePoint() { | 124 void MockMotionEvent::ReleasePoint() { |
| 205 DCHECK_GT(pointer_count, 0U); | 125 ResolvePointers(); |
| 206 if (pointer_count > 1) { | 126 DCHECK_GT(GetPointerCount(), 0U); |
| 207 --pointer_count; | 127 if (GetPointerCount() > 1) { |
| 208 action = ACTION_POINTER_UP; | 128 set_action_index(static_cast<int>(GetPointerCount()) - 1); |
| 129 set_action(ACTION_POINTER_UP); |
| 209 } else { | 130 } else { |
| 210 action = ACTION_UP; | 131 set_action(ACTION_UP); |
| 211 } | 132 } |
| 212 } | 133 } |
| 213 | 134 |
| 214 void MockMotionEvent::CancelPoint() { | 135 void MockMotionEvent::CancelPoint() { |
| 215 DCHECK_GT(pointer_count, 0U); | 136 ResolvePointers(); |
| 216 if (pointer_count > 1) | 137 DCHECK_GT(GetPointerCount(), 0U); |
| 217 --pointer_count; | 138 set_action(ACTION_CANCEL); |
| 218 action = ACTION_CANCEL; | |
| 219 } | 139 } |
| 220 | 140 |
| 221 void MockMotionEvent::SetTouchMajor(float new_touch_major) { | 141 void MockMotionEvent::SetTouchMajor(float new_touch_major) { |
| 222 touch_major = new_touch_major; | 142 for (size_t i = 0; i < GetPointerCount(); ++i) |
| 143 pointer(i).touch_major = new_touch_major; |
| 223 } | 144 } |
| 224 | 145 |
| 225 void MockMotionEvent::SetRawOffset(float raw_offset_x, float raw_offset_y) { | 146 void MockMotionEvent::SetRawOffset(float raw_offset_x, float raw_offset_y) { |
| 226 raw_offset.set_x(raw_offset_x); | 147 for (size_t i = 0; i < GetPointerCount(); ++i) { |
| 227 raw_offset.set_y(raw_offset_y); | 148 pointer(i).raw_x = pointer(i).x + raw_offset_x; |
| 149 pointer(i).raw_y = pointer(i).y + raw_offset_y; |
| 150 } |
| 228 } | 151 } |
| 229 | 152 |
| 230 void MockMotionEvent::SetToolType(size_t pointer_index, ToolType tool_type) { | 153 void MockMotionEvent::SetToolType(size_t pointer_index, ToolType tool_type) { |
| 231 DCHECK_LT(pointer_index, pointer_count); | 154 DCHECK_LT(pointer_index, GetPointerCount()); |
| 232 tool_types[pointer_index] = tool_type; | 155 pointer(pointer_index).tool_type = tool_type; |
| 233 } | 156 } |
| 234 | 157 |
| 235 void MockMotionEvent::SetButtonState(int new_button_state) { | 158 void MockMotionEvent::PushPointer(float x, float y) { |
| 236 button_state = new_button_state; | 159 MotionEventGeneric::PushPointer( |
| 160 CreatePointer(x, y, static_cast<int>(GetPointerCount()))); |
| 161 } |
| 162 |
| 163 void MockMotionEvent::ResolvePointers() { |
| 164 set_action_index(-1); |
| 165 switch (GetAction()) { |
| 166 case ACTION_UP: |
| 167 case ACTION_POINTER_UP: |
| 168 case ACTION_CANCEL: |
| 169 PopPointer(); |
| 170 return; |
| 171 default: |
| 172 break; |
| 173 } |
| 237 } | 174 } |
| 238 | 175 |
| 239 } // namespace test | 176 } // namespace test |
| 240 } // namespace ui | 177 } // namespace ui |
| OLD | NEW |