Add include.
[chromium-blink-merge.git] / ui / events / test / motion_event_test_utils.cc
blob45350fc7d031bba4ab932d7b53b6fa7616d114c0
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"
7 #include <sstream>
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;
15 namespace ui {
16 namespace test {
17 namespace {
19 PointerProperties CreatePointer() {
20 PointerProperties pointer;
21 pointer.touch_major = MockMotionEvent::TOUCH_MAJOR;
22 return pointer;
25 PointerProperties CreatePointer(float x, float y, int id) {
26 PointerProperties pointer(x, y, MockMotionEvent::TOUCH_MAJOR);
27 pointer.id = id;
28 return pointer;
31 } // namespace
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,
42 TimeTicks time,
43 float x0,
44 float y0)
45 : MotionEventGeneric(action, time, CreatePointer(x0, y0, 0)) {
48 MockMotionEvent::MockMotionEvent(Action action,
49 TimeTicks time,
50 float x0,
51 float y0,
52 float x1,
53 float y1)
54 : MotionEventGeneric(action, time, CreatePointer(x0, y0, 0)) {
55 PushPointer(x1, y1);
56 if (action == ACTION_POINTER_UP || action == ACTION_POINTER_DOWN)
57 set_action_index(1);
60 MockMotionEvent::MockMotionEvent(Action action,
61 TimeTicks time,
62 float x0,
63 float y0,
64 float x1,
65 float y1,
66 float x2,
67 float y2)
68 : MotionEventGeneric(action, time, CreatePointer(x0, y0, 0)) {
69 PushPointer(x1, y1);
70 PushPointer(x2, y2);
71 if (action == ACTION_POINTER_UP || action == ACTION_POINTER_DOWN)
72 set_action_index(2);
75 MockMotionEvent::MockMotionEvent(Action action,
76 base::TimeTicks time,
77 const std::vector<gfx::PointF>& positions) {
78 set_action(action);
79 set_event_time(time);
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) {
94 ResolvePointers();
95 PushPointer(x, y);
96 if (GetPointerCount() > 1) {
97 set_action_index(static_cast<int>(GetPointerCount()) - 1);
98 set_action(ACTION_POINTER_DOWN);
99 } else {
100 set_action(ACTION_DOWN);
104 void MockMotionEvent::MovePoint(size_t index, float x, float y) {
105 ResolvePointers();
106 DCHECK_LT(index, GetPointerCount());
107 PointerProperties& p = pointer(index);
108 float dx = x - p.x;
109 float dy = x - p.y;
110 p.x = x;
111 p.y = y;
112 p.raw_x += dx;
113 p.raw_y += dy;
114 set_action(ACTION_MOVE);
117 void MockMotionEvent::ReleasePoint() {
118 ResolvePointers();
119 DCHECK_GT(GetPointerCount(), 0U);
120 if (GetPointerCount() > 1) {
121 set_action_index(static_cast<int>(GetPointerCount()) - 1);
122 set_action(ACTION_POINTER_UP);
123 } else {
124 set_action(ACTION_UP);
128 void MockMotionEvent::CancelPoint() {
129 ResolvePointers();
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()) {
159 case ACTION_UP:
160 case ACTION_POINTER_UP:
161 case ACTION_CANCEL:
162 PopPointer();
163 return;
164 default:
165 break;
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());
186 DCHECK_GE(pi, 0);
187 pointer_ids.clear_first_marked_bit();
188 ss << "{"
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);
196 if (history_size) {
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)
204 ss << ",";
206 ss << "\n ]";
208 ss << "\n }";
209 if (i + 1 < pointer_count)
210 ss << ", ";
212 ss << "]\n}";
215 return ss.str();
218 } // namespace test
219 } // namespace ui