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/gesture_detection/bitset_32.h"
11 #include "ui/events/gesture_detection/motion_event.h"
13 using base::TimeTicks
;
19 PointerProperties
CreatePointer() {
20 PointerProperties pointer
;
21 pointer
.touch_major
= MockMotionEvent::TOUCH_MAJOR
;
25 PointerProperties
CreatePointer(float x
, float y
, int id
) {
26 PointerProperties
pointer(x
, y
, MockMotionEvent::TOUCH_MAJOR
);
33 MockMotionEvent::MockMotionEvent()
34 : MotionEventGeneric(ACTION_CANCEL
, base::TimeTicks(), CreatePointer()) {
37 MockMotionEvent::MockMotionEvent(Action action
)
38 : MotionEventGeneric(action
, base::TimeTicks(), CreatePointer()) {
41 MockMotionEvent::MockMotionEvent(Action action
,
45 : MotionEventGeneric(action
, time
, CreatePointer(x0
, y0
, 0)) {
48 MockMotionEvent::MockMotionEvent(Action action
,
54 : MotionEventGeneric(action
, time
, CreatePointer(x0
, y0
, 0)) {
56 if (action
== ACTION_POINTER_UP
|| action
== ACTION_POINTER_DOWN
)
60 MockMotionEvent::MockMotionEvent(Action action
,
68 : MotionEventGeneric(action
, time
, CreatePointer(x0
, y0
, 0)) {
71 if (action
== ACTION_POINTER_UP
|| action
== ACTION_POINTER_DOWN
)
75 MockMotionEvent::MockMotionEvent(Action action
,
77 const std::vector
<gfx::PointF
>& positions
) {
80 if (action
== ACTION_POINTER_UP
|| action
== ACTION_POINTER_DOWN
)
81 set_action_index(static_cast<int>(positions
.size()) - 1);
82 for (size_t i
= 0; i
< positions
.size(); ++i
)
83 PushPointer(positions
[i
].x(), positions
[i
].y());
86 MockMotionEvent::MockMotionEvent(const MockMotionEvent
& other
)
87 : MotionEventGeneric(other
) {
90 MockMotionEvent::~MockMotionEvent() {
93 void MockMotionEvent::PressPoint(float x
, float y
) {
96 if (GetPointerCount() > 1) {
97 set_action_index(static_cast<int>(GetPointerCount()) - 1);
98 set_action(ACTION_POINTER_DOWN
);
100 set_action(ACTION_DOWN
);
104 void MockMotionEvent::MovePoint(size_t index
, float x
, float y
) {
106 DCHECK_LT(index
, GetPointerCount());
107 PointerProperties
& p
= pointer(index
);
114 set_action(ACTION_MOVE
);
117 void MockMotionEvent::ReleasePoint() {
119 DCHECK_GT(GetPointerCount(), 0U);
120 if (GetPointerCount() > 1) {
121 set_action_index(static_cast<int>(GetPointerCount()) - 1);
122 set_action(ACTION_POINTER_UP
);
124 set_action(ACTION_UP
);
128 void MockMotionEvent::CancelPoint() {
130 DCHECK_GT(GetPointerCount(), 0U);
131 set_action(ACTION_CANCEL
);
134 void MockMotionEvent::SetTouchMajor(float new_touch_major
) {
135 for (size_t i
= 0; i
< GetPointerCount(); ++i
)
136 pointer(i
).touch_major
= new_touch_major
;
139 void MockMotionEvent::SetRawOffset(float raw_offset_x
, float raw_offset_y
) {
140 for (size_t i
= 0; i
< GetPointerCount(); ++i
) {
141 pointer(i
).raw_x
= pointer(i
).x
+ raw_offset_x
;
142 pointer(i
).raw_y
= pointer(i
).y
+ raw_offset_y
;
146 void MockMotionEvent::SetToolType(size_t pointer_index
, ToolType tool_type
) {
147 DCHECK_LT(pointer_index
, GetPointerCount());
148 pointer(pointer_index
).tool_type
= tool_type
;
151 void MockMotionEvent::PushPointer(float x
, float y
) {
152 MotionEventGeneric::PushPointer(
153 CreatePointer(x
, y
, static_cast<int>(GetPointerCount())));
156 void MockMotionEvent::ResolvePointers() {
157 set_action_index(-1);
158 switch (GetAction()) {
160 case ACTION_POINTER_UP
:
169 std::string
ToString(const MotionEvent
& event
) {
170 std::stringstream ss
;
171 ss
<< "MotionEvent {"
172 << "\n ID: " << event
.GetId() << "\n Action: " << event
.GetAction()
173 << "\n ActionIndex: " << event
.GetActionIndex()
174 << "\n Flags: " << event
.GetFlags()
175 << "\n ButtonState: " << event
.GetButtonState() << "\n Pointers: [";
176 const size_t pointer_count
= event
.GetPointerCount();
177 const size_t history_size
= event
.GetHistorySize();
179 BitSet32 pointer_ids
;
180 for (size_t i
= 0; i
< pointer_count
; ++i
) {
181 pointer_ids
.mark_bit(event
.GetPointerId(i
));
183 // Print the pointers sorted by id.
184 while (!pointer_ids
.is_empty()) {
185 int pi
= event
.FindPointerIndexOfId(pointer_ids
.first_marked_bit());
187 pointer_ids
.clear_first_marked_bit();
189 << "\n Pos: (" << event
.GetX(pi
) << ", " << event
.GetY(pi
) << ")"
190 << "\n RawPos: (" << event
.GetX(pi
) << ", " << event
.GetY(pi
) << ")"
191 << "\n Size: (" << event
.GetTouchMajor(pi
) << ", "
192 << event
.GetTouchMinor(pi
) << ")"
193 << "\n Orientation: " << event
.GetOrientation(pi
)
194 << "\n Pressure: " << event
.GetOrientation(pi
)
195 << "\n Tool: " << event
.GetToolType(pi
);
197 ss
<< "\n History: [";
198 for (size_t h
= 0; h
< history_size
; ++h
) {
199 ss
<< "\n { " << event
.GetHistoricalX(pi
, h
) << ", "
200 << event
.GetHistoricalY(pi
, h
) << ", "
201 << event
.GetHistoricalTouchMajor(pi
, h
) << ", "
202 << event
.GetHistoricalEventTime(pi
).ToInternalValue() << " }";
203 if (h
+ 1 < history_size
)
209 if (i
+ 1 < pointer_count
)