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"
15 PointerProperties
CreatePointer() {
16 PointerProperties pointer
;
17 pointer
.touch_major
= MockMotionEvent::TOUCH_MAJOR
;
21 PointerProperties
CreatePointer(float x
, float y
, int id
) {
22 PointerProperties
pointer(x
, y
);
23 pointer
.touch_major
= MockMotionEvent::TOUCH_MAJOR
;
31 MockMotionEvent::MockMotionEvent()
32 : MotionEventGeneric(ACTION_CANCEL
, base::TimeTicks(), CreatePointer()) {
35 MockMotionEvent::MockMotionEvent(Action action
)
36 : MotionEventGeneric(action
, base::TimeTicks(), CreatePointer()) {
39 MockMotionEvent::MockMotionEvent(Action action
,
43 : MotionEventGeneric(action
, time
, CreatePointer(x0
, y0
, 0)) {
46 MockMotionEvent::MockMotionEvent(Action action
,
52 : MotionEventGeneric(action
, time
, CreatePointer(x0
, y0
, 0)) {
54 if (action
== ACTION_POINTER_UP
|| action
== ACTION_POINTER_DOWN
)
58 MockMotionEvent::MockMotionEvent(Action action
,
66 : MotionEventGeneric(action
, time
, CreatePointer(x0
, y0
, 0)) {
69 if (action
== ACTION_POINTER_UP
|| action
== ACTION_POINTER_DOWN
)
73 MockMotionEvent::MockMotionEvent(Action action
,
75 const std::vector
<gfx::PointF
>& positions
) {
78 if (action
== ACTION_POINTER_UP
|| action
== ACTION_POINTER_DOWN
)
79 set_action_index(static_cast<int>(positions
.size()) - 1);
80 for (size_t i
= 0; i
< positions
.size(); ++i
)
81 PushPointer(positions
[i
].x(), positions
[i
].y());
84 MockMotionEvent::MockMotionEvent(const MockMotionEvent
& other
)
85 : MotionEventGeneric(other
) {
88 MockMotionEvent::~MockMotionEvent() {}
90 scoped_ptr
<MotionEvent
> MockMotionEvent::Clone() const {
91 return scoped_ptr
<MotionEvent
>(new MockMotionEvent(*this));
94 scoped_ptr
<MotionEvent
> MockMotionEvent::Cancel() const {
95 scoped_ptr
<MockMotionEvent
> event(new MockMotionEvent(*this));
96 event
->set_action(MotionEvent::ACTION_CANCEL
);
97 return event
.PassAs
<MotionEvent
>();
100 void MockMotionEvent::PressPoint(float x
, float y
) {
103 if (GetPointerCount() > 1) {
104 set_action_index(static_cast<int>(GetPointerCount()) - 1);
105 set_action(ACTION_POINTER_DOWN
);
107 set_action(ACTION_DOWN
);
111 void MockMotionEvent::MovePoint(size_t index
, float x
, float y
) {
113 DCHECK_LT(index
, GetPointerCount());
114 PointerProperties
& p
= pointer(index
);
121 set_action(ACTION_MOVE
);
124 void MockMotionEvent::ReleasePoint() {
126 DCHECK_GT(GetPointerCount(), 0U);
127 if (GetPointerCount() > 1) {
128 set_action_index(static_cast<int>(GetPointerCount()) - 1);
129 set_action(ACTION_POINTER_UP
);
131 set_action(ACTION_UP
);
135 void MockMotionEvent::CancelPoint() {
137 DCHECK_GT(GetPointerCount(), 0U);
138 set_action(ACTION_CANCEL
);
141 void MockMotionEvent::SetTouchMajor(float new_touch_major
) {
142 for (size_t i
= 0; i
< GetPointerCount(); ++i
)
143 pointer(i
).touch_major
= new_touch_major
;
146 void MockMotionEvent::SetRawOffset(float raw_offset_x
, float raw_offset_y
) {
147 for (size_t i
= 0; i
< GetPointerCount(); ++i
) {
148 pointer(i
).raw_x
= pointer(i
).x
+ raw_offset_x
;
149 pointer(i
).raw_y
= pointer(i
).y
+ raw_offset_y
;
153 void MockMotionEvent::SetToolType(size_t pointer_index
, ToolType tool_type
) {
154 DCHECK_LT(pointer_index
, GetPointerCount());
155 pointer(pointer_index
).tool_type
= tool_type
;
158 void MockMotionEvent::PushPointer(float x
, float y
) {
159 MotionEventGeneric::PushPointer(
160 CreatePointer(x
, y
, static_cast<int>(GetPointerCount())));
163 void MockMotionEvent::ResolvePointers() {
164 set_action_index(-1);
165 switch (GetAction()) {
167 case ACTION_POINTER_UP
: