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