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/motion_event_test_utils.h" |
| 6 |
| 7 #include <sstream> |
6 | 8 |
7 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "ui/events/base_event_utils.h" |
| 11 #include "ui/events/gesture_detection/bitset_32.h" |
| 12 #include "ui/events/gesture_detection/motion_event.h" |
8 | 13 |
9 using base::TimeTicks; | 14 using base::TimeTicks; |
10 | 15 |
11 namespace ui { | 16 namespace ui { |
12 namespace test { | 17 namespace test { |
13 namespace { | 18 namespace { |
14 | 19 |
15 PointerProperties CreatePointer() { | 20 PointerProperties CreatePointer() { |
16 PointerProperties pointer; | 21 PointerProperties pointer; |
17 pointer.touch_major = MockMotionEvent::TOUCH_MAJOR; | 22 pointer.touch_major = MockMotionEvent::TOUCH_MAJOR; |
18 return pointer; | 23 return pointer; |
19 } | 24 } |
20 | 25 |
21 PointerProperties CreatePointer(float x, float y, int id) { | 26 PointerProperties CreatePointer(float x, float y, int id) { |
22 PointerProperties pointer(x, y); | 27 PointerProperties pointer(x, y, MockMotionEvent::TOUCH_MAJOR); |
23 pointer.touch_major = MockMotionEvent::TOUCH_MAJOR; | |
24 pointer.id = id; | 28 pointer.id = id; |
25 return pointer; | 29 return pointer; |
26 } | 30 } |
27 | 31 |
28 | |
29 } // namespace | 32 } // namespace |
30 | 33 |
31 MockMotionEvent::MockMotionEvent() | 34 MockMotionEvent::MockMotionEvent() |
32 : MotionEventGeneric(ACTION_CANCEL, base::TimeTicks(), CreatePointer()) { | 35 : MotionEventGeneric(ACTION_CANCEL, base::TimeTicks(), CreatePointer()) { |
33 } | 36 } |
34 | 37 |
35 MockMotionEvent::MockMotionEvent(Action action) | 38 MockMotionEvent::MockMotionEvent(Action action) |
36 : MotionEventGeneric(action, base::TimeTicks(), CreatePointer()) { | 39 : MotionEventGeneric(action, base::TimeTicks(), CreatePointer()) { |
37 } | 40 } |
38 | 41 |
(...skipping 29 matching lines...) Expand all Loading... |
68 PushPointer(x2, y2); | 71 PushPointer(x2, y2); |
69 if (action == ACTION_POINTER_UP || action == ACTION_POINTER_DOWN) | 72 if (action == ACTION_POINTER_UP || action == ACTION_POINTER_DOWN) |
70 set_action_index(2); | 73 set_action_index(2); |
71 } | 74 } |
72 | 75 |
73 MockMotionEvent::MockMotionEvent(Action action, | 76 MockMotionEvent::MockMotionEvent(Action action, |
74 base::TimeTicks time, | 77 base::TimeTicks time, |
75 const std::vector<gfx::PointF>& positions) { | 78 const std::vector<gfx::PointF>& positions) { |
76 set_action(action); | 79 set_action(action); |
77 set_event_time(time); | 80 set_event_time(time); |
| 81 set_unique_event_id(ui::GetNextTouchEventId()); |
78 if (action == ACTION_POINTER_UP || action == ACTION_POINTER_DOWN) | 82 if (action == ACTION_POINTER_UP || action == ACTION_POINTER_DOWN) |
79 set_action_index(static_cast<int>(positions.size()) - 1); | 83 set_action_index(static_cast<int>(positions.size()) - 1); |
80 for (size_t i = 0; i < positions.size(); ++i) | 84 for (size_t i = 0; i < positions.size(); ++i) |
81 PushPointer(positions[i].x(), positions[i].y()); | 85 PushPointer(positions[i].x(), positions[i].y()); |
82 } | 86 } |
83 | 87 |
84 MockMotionEvent::MockMotionEvent(const MockMotionEvent& other) | 88 MockMotionEvent::MockMotionEvent(const MockMotionEvent& other) |
85 : MotionEventGeneric(other) { | 89 : MotionEventGeneric(other) { |
86 } | 90 } |
87 | 91 |
88 MockMotionEvent::~MockMotionEvent() {} | 92 MockMotionEvent::~MockMotionEvent() { |
89 | |
90 scoped_ptr<MotionEvent> MockMotionEvent::Clone() const { | |
91 return scoped_ptr<MotionEvent>(new MockMotionEvent(*this)); | |
92 } | 93 } |
93 | 94 |
94 scoped_ptr<MotionEvent> MockMotionEvent::Cancel() const { | 95 MockMotionEvent& MockMotionEvent::PressPoint(float x, float y) { |
95 scoped_ptr<MockMotionEvent> event(new MockMotionEvent(*this)); | 96 UpdatePointersAndID(); |
96 event->set_action(MotionEvent::ACTION_CANCEL); | |
97 return event.Pass(); | |
98 } | |
99 | |
100 void MockMotionEvent::PressPoint(float x, float y) { | |
101 ResolvePointers(); | |
102 PushPointer(x, y); | 97 PushPointer(x, y); |
103 if (GetPointerCount() > 1) { | 98 if (GetPointerCount() > 1) { |
104 set_action_index(static_cast<int>(GetPointerCount()) - 1); | 99 set_action_index(static_cast<int>(GetPointerCount()) - 1); |
105 set_action(ACTION_POINTER_DOWN); | 100 set_action(ACTION_POINTER_DOWN); |
106 } else { | 101 } else { |
107 set_action(ACTION_DOWN); | 102 set_action(ACTION_DOWN); |
108 } | 103 } |
| 104 return *this; |
109 } | 105 } |
110 | 106 |
111 void MockMotionEvent::MovePoint(size_t index, float x, float y) { | 107 MockMotionEvent& MockMotionEvent::MovePoint(size_t index, float x, float y) { |
112 ResolvePointers(); | 108 UpdatePointersAndID(); |
113 DCHECK_LT(index, GetPointerCount()); | 109 DCHECK_LT(index, GetPointerCount()); |
114 PointerProperties& p = pointer(index); | 110 PointerProperties& p = pointer(index); |
115 float dx = x - p.x; | 111 float dx = x - p.x; |
116 float dy = x - p.y; | 112 float dy = x - p.y; |
117 p.x = x; | 113 p.x = x; |
118 p.y = y; | 114 p.y = y; |
119 p.raw_x += dx; | 115 p.raw_x += dx; |
120 p.raw_y += dy; | 116 p.raw_y += dy; |
121 set_action(ACTION_MOVE); | 117 set_action(ACTION_MOVE); |
| 118 return *this; |
122 } | 119 } |
123 | 120 |
124 void MockMotionEvent::ReleasePoint() { | 121 MockMotionEvent& MockMotionEvent::ReleasePoint() { |
125 ResolvePointers(); | 122 UpdatePointersAndID(); |
126 DCHECK_GT(GetPointerCount(), 0U); | 123 DCHECK_GT(GetPointerCount(), 0U); |
127 if (GetPointerCount() > 1) { | 124 if (GetPointerCount() > 1) { |
128 set_action_index(static_cast<int>(GetPointerCount()) - 1); | 125 set_action_index(static_cast<int>(GetPointerCount()) - 1); |
129 set_action(ACTION_POINTER_UP); | 126 set_action(ACTION_POINTER_UP); |
130 } else { | 127 } else { |
131 set_action(ACTION_UP); | 128 set_action(ACTION_UP); |
132 } | 129 } |
| 130 return *this; |
133 } | 131 } |
134 | 132 |
135 void MockMotionEvent::CancelPoint() { | 133 MockMotionEvent& MockMotionEvent::CancelPoint() { |
136 ResolvePointers(); | 134 UpdatePointersAndID(); |
137 DCHECK_GT(GetPointerCount(), 0U); | 135 DCHECK_GT(GetPointerCount(), 0U); |
138 set_action(ACTION_CANCEL); | 136 set_action(ACTION_CANCEL); |
| 137 return *this; |
139 } | 138 } |
140 | 139 |
141 void MockMotionEvent::SetTouchMajor(float new_touch_major) { | 140 MockMotionEvent& MockMotionEvent::SetTouchMajor(float new_touch_major) { |
142 for (size_t i = 0; i < GetPointerCount(); ++i) | 141 for (size_t i = 0; i < GetPointerCount(); ++i) |
143 pointer(i).touch_major = new_touch_major; | 142 pointer(i).touch_major = new_touch_major; |
| 143 return *this; |
144 } | 144 } |
145 | 145 |
146 void MockMotionEvent::SetRawOffset(float raw_offset_x, float raw_offset_y) { | 146 MockMotionEvent& MockMotionEvent::SetRawOffset(float raw_offset_x, |
| 147 float raw_offset_y) { |
147 for (size_t i = 0; i < GetPointerCount(); ++i) { | 148 for (size_t i = 0; i < GetPointerCount(); ++i) { |
148 pointer(i).raw_x = pointer(i).x + raw_offset_x; | 149 pointer(i).raw_x = pointer(i).x + raw_offset_x; |
149 pointer(i).raw_y = pointer(i).y + raw_offset_y; | 150 pointer(i).raw_y = pointer(i).y + raw_offset_y; |
150 } | 151 } |
| 152 return *this; |
151 } | 153 } |
152 | 154 |
153 void MockMotionEvent::SetToolType(size_t pointer_index, ToolType tool_type) { | 155 MockMotionEvent& MockMotionEvent::SetToolType(size_t pointer_index, |
| 156 ToolType tool_type) { |
154 DCHECK_LT(pointer_index, GetPointerCount()); | 157 DCHECK_LT(pointer_index, GetPointerCount()); |
155 pointer(pointer_index).tool_type = tool_type; | 158 pointer(pointer_index).tool_type = tool_type; |
| 159 return *this; |
156 } | 160 } |
157 | 161 |
158 void MockMotionEvent::PushPointer(float x, float y) { | 162 void MockMotionEvent::PushPointer(float x, float y) { |
159 MotionEventGeneric::PushPointer( | 163 MotionEventGeneric::PushPointer( |
160 CreatePointer(x, y, static_cast<int>(GetPointerCount()))); | 164 CreatePointer(x, y, static_cast<int>(GetPointerCount()))); |
161 } | 165 } |
162 | 166 |
163 void MockMotionEvent::ResolvePointers() { | 167 void MockMotionEvent::UpdatePointersAndID() { |
164 set_action_index(-1); | 168 set_action_index(-1); |
| 169 set_unique_event_id(ui::GetNextTouchEventId()); |
165 switch (GetAction()) { | 170 switch (GetAction()) { |
166 case ACTION_UP: | 171 case ACTION_UP: |
167 case ACTION_POINTER_UP: | 172 case ACTION_POINTER_UP: |
168 case ACTION_CANCEL: | 173 case ACTION_CANCEL: |
169 PopPointer(); | 174 PopPointer(); |
170 return; | 175 return; |
171 default: | 176 default: |
172 break; | 177 break; |
173 } | 178 } |
174 } | 179 } |
175 | 180 |
| 181 MockMotionEvent& MockMotionEvent::SetPrimaryPointerId(int id) { |
| 182 DCHECK_GT(GetPointerCount(), 0U); |
| 183 pointer(0).id = id; |
| 184 return *this; |
| 185 } |
| 186 |
| 187 std::string ToString(const MotionEvent& event) { |
| 188 std::stringstream ss; |
| 189 ss << "MotionEvent {" |
| 190 << "\n Action: " << event.GetAction(); |
| 191 if (event.GetAction() == MotionEvent::ACTION_POINTER_DOWN || |
| 192 event.GetAction() == MotionEvent::ACTION_POINTER_UP) |
| 193 ss << "\n ActionIndex: " << event.GetActionIndex(); |
| 194 ss << "\n Flags: " << event.GetFlags() |
| 195 << "\n ButtonState: " << event.GetButtonState() << "\n Pointers: ["; |
| 196 const size_t pointer_count = event.GetPointerCount(); |
| 197 const size_t history_size = event.GetHistorySize(); |
| 198 |
| 199 BitSet32 pointer_ids; |
| 200 for (size_t i = 0; i < pointer_count; ++i) { |
| 201 pointer_ids.mark_bit(event.GetPointerId(i)); |
| 202 |
| 203 // Print the pointers sorted by id. |
| 204 while (!pointer_ids.is_empty()) { |
| 205 int pi = event.FindPointerIndexOfId(pointer_ids.first_marked_bit()); |
| 206 DCHECK_GE(pi, 0); |
| 207 pointer_ids.clear_first_marked_bit(); |
| 208 ss << "{" |
| 209 << "\n PointerId: (" << event.GetPointerId(pi) << ")" |
| 210 << "\n Pos: (" << event.GetX(pi) << ", " << event.GetY(pi) << ")" |
| 211 << "\n RawPos: (" << event.GetX(pi) << ", " << event.GetY(pi) << ")" |
| 212 << "\n Size: (" << event.GetTouchMajor(pi) << ", " |
| 213 << event.GetTouchMinor(pi) << ")" |
| 214 << "\n Orientation: " << event.GetOrientation(pi) |
| 215 << "\n Pressure: " << event.GetPressure(pi) |
| 216 << "\n Tool: " << event.GetToolType(pi); |
| 217 if (history_size) { |
| 218 ss << "\n History: ["; |
| 219 for (size_t h = 0; h < history_size; ++h) { |
| 220 ss << "\n { " << event.GetHistoricalX(pi, h) << ", " |
| 221 << event.GetHistoricalY(pi, h) << ", " |
| 222 << event.GetHistoricalTouchMajor(pi, h) << ", " |
| 223 << event.GetHistoricalEventTime(pi).ToInternalValue() << " }"; |
| 224 if (h + 1 < history_size) |
| 225 ss << ","; |
| 226 } |
| 227 ss << "\n ]"; |
| 228 } |
| 229 ss << "\n }"; |
| 230 if (i + 1 < pointer_count) |
| 231 ss << ", "; |
| 232 } |
| 233 ss << "]\n}"; |
| 234 } |
| 235 |
| 236 return ss.str(); |
| 237 } |
| 238 |
176 } // namespace test | 239 } // namespace test |
177 } // namespace ui | 240 } // namespace ui |
OLD | NEW |