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
3 // found in the LICENSE file.
5 #include "ui/events/test/motion_event_test_utils.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"
14 using base::TimeTicks
;
20 PointerProperties
CreatePointer() {
21 PointerProperties pointer
;
22 pointer
.touch_major
= MockMotionEvent::TOUCH_MAJOR
;
26 PointerProperties
CreatePointer(float x
, float y
, int id
) {
27 PointerProperties
pointer(x
, y
, MockMotionEvent::TOUCH_MAJOR
);
34 MockMotionEvent::MockMotionEvent()
35 : MotionEventGeneric(ACTION_CANCEL
, base::TimeTicks(), CreatePointer()) {
38 MockMotionEvent::MockMotionEvent(Action action
)
39 : MotionEventGeneric(action
, base::TimeTicks(), CreatePointer()) {
42 MockMotionEvent::MockMotionEvent(Action action
,
46 : MotionEventGeneric(action
, time
, CreatePointer(x0
, y0
, 0)) {
49 MockMotionEvent::MockMotionEvent(Action action
,
55 : MotionEventGeneric(action
, time
, CreatePointer(x0
, y0
, 0)) {
57 if (action
== ACTION_POINTER_UP
|| action
== ACTION_POINTER_DOWN
)
61 MockMotionEvent::MockMotionEvent(Action action
,
69 : MotionEventGeneric(action
, time
, CreatePointer(x0
, y0
, 0)) {
72 if (action
== ACTION_POINTER_UP
|| action
== ACTION_POINTER_DOWN
)
76 MockMotionEvent::MockMotionEvent(Action action
,
78 const std::vector
<gfx::PointF
>& positions
) {
81 set_unique_event_id(ui::GetNextTouchEventId());
82 if (action
== ACTION_POINTER_UP
|| action
== ACTION_POINTER_DOWN
)
83 set_action_index(static_cast<int>(positions
.size()) - 1);
84 for (size_t i
= 0; i
< positions
.size(); ++i
)
85 PushPointer(positions
[i
].x(), positions
[i
].y());
88 MockMotionEvent::MockMotionEvent(const MockMotionEvent
& other
)
89 : MotionEventGeneric(other
) {
92 MockMotionEvent::~MockMotionEvent() {
95 MockMotionEvent
& MockMotionEvent::PressPoint(float x
, float y
) {
96 UpdatePointersAndID();
98 if (GetPointerCount() > 1) {
99 set_action_index(static_cast<int>(GetPointerCount()) - 1);
100 set_action(ACTION_POINTER_DOWN
);
102 set_action(ACTION_DOWN
);
107 MockMotionEvent
& MockMotionEvent::MovePoint(size_t index
, float x
, float y
) {
108 UpdatePointersAndID();
109 DCHECK_LT(index
, GetPointerCount());
110 PointerProperties
& p
= pointer(index
);
117 set_action(ACTION_MOVE
);
121 MockMotionEvent
& MockMotionEvent::ReleasePoint() {
122 UpdatePointersAndID();
123 DCHECK_GT(GetPointerCount(), 0U);
124 if (GetPointerCount() > 1) {
125 set_action_index(static_cast<int>(GetPointerCount()) - 1);
126 set_action(ACTION_POINTER_UP
);
128 set_action(ACTION_UP
);
133 MockMotionEvent
& MockMotionEvent::CancelPoint() {
134 UpdatePointersAndID();
135 DCHECK_GT(GetPointerCount(), 0U);
136 set_action(ACTION_CANCEL
);
140 MockMotionEvent
& MockMotionEvent::SetTouchMajor(float new_touch_major
) {
141 for (size_t i
= 0; i
< GetPointerCount(); ++i
)
142 pointer(i
).touch_major
= new_touch_major
;
146 MockMotionEvent
& MockMotionEvent::SetRawOffset(float raw_offset_x
,
147 float raw_offset_y
) {
148 for (size_t i
= 0; i
< GetPointerCount(); ++i
) {
149 pointer(i
).raw_x
= pointer(i
).x
+ raw_offset_x
;
150 pointer(i
).raw_y
= pointer(i
).y
+ raw_offset_y
;
155 MockMotionEvent
& MockMotionEvent::SetToolType(size_t pointer_index
,
156 ToolType tool_type
) {
157 DCHECK_LT(pointer_index
, GetPointerCount());
158 pointer(pointer_index
).tool_type
= tool_type
;
162 void MockMotionEvent::PushPointer(float x
, float y
) {
163 MotionEventGeneric::PushPointer(
164 CreatePointer(x
, y
, static_cast<int>(GetPointerCount())));
167 void MockMotionEvent::UpdatePointersAndID() {
168 set_action_index(-1);
169 set_unique_event_id(ui::GetNextTouchEventId());
170 switch (GetAction()) {
172 case ACTION_POINTER_UP
:
181 MockMotionEvent
& MockMotionEvent::SetPrimaryPointerId(int id
) {
182 DCHECK_GT(GetPointerCount(), 0U);
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();
199 BitSet32 pointer_ids
;
200 for (size_t i
= 0; i
< pointer_count
; ++i
) {
201 pointer_ids
.mark_bit(event
.GetPointerId(i
));
203 // Print the pointers sorted by id.
204 while (!pointer_ids
.is_empty()) {
205 int pi
= event
.FindPointerIndexOfId(pointer_ids
.first_marked_bit());
207 pointer_ids
.clear_first_marked_bit();
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
);
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
)
230 if (i
+ 1 < pointer_count
)