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.
4
5#include "ui/events/test/mock_motion_event.h"
6
7#include "base/logging.h"
8
9using base::TimeTicks;
10
11namespace ui {
12namespace test {
13namespace {
14
15PointerProperties CreatePointer() {
16  PointerProperties pointer;
17  pointer.touch_major = MockMotionEvent::TOUCH_MAJOR;
18  return pointer;
19}
20
21PointerProperties CreatePointer(float x, float y, int id) {
22  PointerProperties pointer(x, y);
23  pointer.touch_major = MockMotionEvent::TOUCH_MAJOR;
24  pointer.id = id;
25  return pointer;
26}
27
28
29}  // namespace
30
31MockMotionEvent::MockMotionEvent()
32    : MotionEventGeneric(ACTION_CANCEL, base::TimeTicks(), CreatePointer()) {
33}
34
35MockMotionEvent::MockMotionEvent(Action action)
36    : MotionEventGeneric(action, base::TimeTicks(), CreatePointer()) {
37}
38
39MockMotionEvent::MockMotionEvent(Action action,
40                                 TimeTicks time,
41                                 float x0,
42                                 float y0)
43    : MotionEventGeneric(action, time, CreatePointer(x0, y0, 0)) {
44}
45
46MockMotionEvent::MockMotionEvent(Action action,
47                                 TimeTicks time,
48                                 float x0,
49                                 float y0,
50                                 float x1,
51                                 float y1)
52    : MotionEventGeneric(action, time, CreatePointer(x0, y0, 0)) {
53  PushPointer(x1, y1);
54  if (action == ACTION_POINTER_UP || action == ACTION_POINTER_DOWN)
55    set_action_index(1);
56}
57
58MockMotionEvent::MockMotionEvent(Action action,
59                                 TimeTicks time,
60                                 float x0,
61                                 float y0,
62                                 float x1,
63                                 float y1,
64                                 float x2,
65                                 float y2)
66    : MotionEventGeneric(action, time, CreatePointer(x0, y0, 0)) {
67  PushPointer(x1, y1);
68  PushPointer(x2, y2);
69  if (action == ACTION_POINTER_UP || action == ACTION_POINTER_DOWN)
70    set_action_index(2);
71}
72
73MockMotionEvent::MockMotionEvent(Action action,
74                                 base::TimeTicks time,
75                                 const std::vector<gfx::PointF>& positions) {
76  set_action(action);
77  set_event_time(time);
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());
82}
83
84MockMotionEvent::MockMotionEvent(const MockMotionEvent& other)
85    : MotionEventGeneric(other) {
86}
87
88MockMotionEvent::~MockMotionEvent() {}
89
90scoped_ptr<MotionEvent> MockMotionEvent::Clone() const {
91  return scoped_ptr<MotionEvent>(new MockMotionEvent(*this));
92}
93
94scoped_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>();
98}
99
100void MockMotionEvent::PressPoint(float x, float y) {
101  ResolvePointers();
102  PushPointer(x, y);
103  if (GetPointerCount() > 1) {
104    set_action_index(static_cast<int>(GetPointerCount()) - 1);
105    set_action(ACTION_POINTER_DOWN);
106  } else {
107    set_action(ACTION_DOWN);
108  }
109}
110
111void MockMotionEvent::MovePoint(size_t index, float x, float y) {
112  ResolvePointers();
113  DCHECK_LT(index, GetPointerCount());
114  PointerProperties& p = pointer(index);
115  float dx = x - p.x;
116  float dy = x - p.y;
117  p.x = x;
118  p.y = y;
119  p.raw_x += dx;
120  p.raw_y += dy;
121  set_action(ACTION_MOVE);
122}
123
124void MockMotionEvent::ReleasePoint() {
125  ResolvePointers();
126  DCHECK_GT(GetPointerCount(), 0U);
127  if (GetPointerCount() > 1) {
128    set_action_index(static_cast<int>(GetPointerCount()) - 1);
129    set_action(ACTION_POINTER_UP);
130  } else {
131    set_action(ACTION_UP);
132  }
133}
134
135void MockMotionEvent::CancelPoint() {
136  ResolvePointers();
137  DCHECK_GT(GetPointerCount(), 0U);
138  set_action(ACTION_CANCEL);
139}
140
141void MockMotionEvent::SetTouchMajor(float new_touch_major) {
142  for (size_t i = 0; i < GetPointerCount(); ++i)
143    pointer(i).touch_major = new_touch_major;
144}
145
146void 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;
150  }
151}
152
153void MockMotionEvent::SetToolType(size_t pointer_index, ToolType tool_type) {
154  DCHECK_LT(pointer_index, GetPointerCount());
155  pointer(pointer_index).tool_type = tool_type;
156}
157
158void MockMotionEvent::PushPointer(float x, float y) {
159  MotionEventGeneric::PushPointer(
160      CreatePointer(x, y, static_cast<int>(GetPointerCount())));
161}
162
163void MockMotionEvent::ResolvePointers() {
164  set_action_index(-1);
165  switch (GetAction()) {
166    case ACTION_UP:
167    case ACTION_POINTER_UP:
168    case ACTION_CANCEL:
169      PopPointer();
170      return;
171    default:
172      break;
173  }
174}
175
176}  // namespace test
177}  // namespace ui
178