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/mock_motion_event.h"
7 #include "base/logging.h"
14 MockMotionEvent::MockMotionEvent()
15 : action(ACTION_CANCEL
), pointer_count(1), touch_major(TOUCH_MAJOR
), id(0),
19 MockMotionEvent::MockMotionEvent(Action action
)
20 : action(action
), pointer_count(1), touch_major(TOUCH_MAJOR
), id(0),
24 MockMotionEvent::MockMotionEvent(Action action
,
31 touch_major(TOUCH_MAJOR
),
34 points
[0].SetPoint(x
, y
);
35 tool_types
[0] = TOOL_TYPE_UNKNOWN
;
38 MockMotionEvent::MockMotionEvent(Action action
,
47 touch_major(TOUCH_MAJOR
),
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
;
56 MockMotionEvent::MockMotionEvent(Action action
,
67 touch_major(TOUCH_MAJOR
),
70 points
[0].SetPoint(x0
, y0
);
71 tool_types
[0] = TOOL_TYPE_UNKNOWN
;
72 points
[1].SetPoint(x1
, y1
);
73 tool_types
[1] = TOOL_TYPE_UNKNOWN
;
74 points
[2].SetPoint(x2
, y2
);
75 tool_types
[2] = TOOL_TYPE_UNKNOWN
;
78 MockMotionEvent::MockMotionEvent(const MockMotionEvent
& other
)
79 : action(other
.action
),
80 pointer_count(other
.pointer_count
),
82 touch_major(other
.touch_major
),
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
];
91 MockMotionEvent::~MockMotionEvent() {}
93 MotionEvent::Action
MockMotionEvent::GetAction() const { return action
; }
95 int MockMotionEvent::GetActionIndex() const {
96 return static_cast<int>(pointer_count
) - 1;
99 size_t MockMotionEvent::GetPointerCount() const { return pointer_count
; }
101 int MockMotionEvent::GetId() const {
105 int MockMotionEvent::GetPointerId(size_t pointer_index
) const {
106 DCHECK(pointer_index
< pointer_count
);
107 return static_cast<int>(pointer_index
);
110 float MockMotionEvent::GetX(size_t pointer_index
) const {
111 return points
[pointer_index
].x();
114 float MockMotionEvent::GetY(size_t pointer_index
) const {
115 return points
[pointer_index
].y();
118 float MockMotionEvent::GetRawX(size_t pointer_index
) const {
119 return GetX(pointer_index
) + raw_offset
.x();
122 float MockMotionEvent::GetRawY(size_t pointer_index
) const {
123 return GetY(pointer_index
) + raw_offset
.y();
126 float MockMotionEvent::GetTouchMajor(size_t pointer_index
) const {
130 float MockMotionEvent::GetPressure(size_t pointer_index
) const {
134 TimeTicks
MockMotionEvent::GetEventTime() const { return time
; }
136 size_t MockMotionEvent::GetHistorySize() const { return 0; }
138 TimeTicks
MockMotionEvent::GetHistoricalEventTime(
139 size_t historical_index
) const {
143 float MockMotionEvent::GetHistoricalTouchMajor(size_t pointer_index
,
144 size_t historical_index
) const {
148 float MockMotionEvent::GetHistoricalX(size_t pointer_index
,
149 size_t historical_index
) const {
153 float MockMotionEvent::GetHistoricalY(size_t pointer_index
,
154 size_t historical_index
) const {
158 MotionEvent::ToolType
MockMotionEvent::GetToolType(size_t pointer_index
) const {
159 DCHECK_LT(pointer_index
, pointer_count
);
160 return tool_types
[pointer_index
];
163 int MockMotionEvent::GetButtonState() const {
167 scoped_ptr
<MotionEvent
> MockMotionEvent::Clone() const {
168 return scoped_ptr
<MotionEvent
>(new MockMotionEvent(*this));
171 scoped_ptr
<MotionEvent
> MockMotionEvent::Cancel() const {
172 scoped_ptr
<MockMotionEvent
> cancel_event(new MockMotionEvent(*this));
173 cancel_event
->action
= MotionEvent::ACTION_CANCEL
;
174 return cancel_event
.PassAs
<MotionEvent
>();
177 void MockMotionEvent::SetId(int new_id
) {
181 void MockMotionEvent::SetTime(base::TimeTicks new_time
) {
185 void MockMotionEvent::PressPoint(float x
, float y
) {
186 // Reset the pointer count if the previously released and/or cancelled pointer
187 // was the last pointer in the event.
188 if (pointer_count
== 1 && (action
== ACTION_UP
|| action
== ACTION_CANCEL
))
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
;
197 void MockMotionEvent::MovePoint(size_t index
, float x
, float y
) {
198 DCHECK_LT(index
, pointer_count
);
199 points
[index
] = gfx::PointF(x
, y
);
200 tool_types
[index
] = TOOL_TYPE_UNKNOWN
;
201 action
= ACTION_MOVE
;
204 void MockMotionEvent::ReleasePoint() {
205 DCHECK_GT(pointer_count
, 0U);
206 if (pointer_count
> 1) {
208 action
= ACTION_POINTER_UP
;
214 void MockMotionEvent::CancelPoint() {
215 DCHECK_GT(pointer_count
, 0U);
216 if (pointer_count
> 1)
218 action
= ACTION_CANCEL
;
221 void MockMotionEvent::SetTouchMajor(float new_touch_major
) {
222 touch_major
= new_touch_major
;
225 void MockMotionEvent::SetRawOffset(float raw_offset_x
, float raw_offset_y
) {
226 raw_offset
.set_x(raw_offset_x
);
227 raw_offset
.set_y(raw_offset_y
);
230 void MockMotionEvent::SetToolType(size_t pointer_index
, ToolType tool_type
) {
231 DCHECK_LT(pointer_index
, pointer_count
);
232 tool_types
[pointer_index
] = tool_type
;
235 void MockMotionEvent::SetButtonState(int new_button_state
) {
236 button_state
= new_button_state
;