Add ICU message format support
[chromium-blink-merge.git] / ui / events / test / motion_event_test_utils.cc
blob0a3f80d26efbee7a552588367b416f4238a070e2
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/base_event_utils.h"
11 #include "ui/events/gesture_detection/bitset_32.h"
12 #include "ui/events/gesture_detection/motion_event.h"
14 using base::TimeTicks;
16 namespace ui {
17 namespace test {
18 namespace {
20 PointerProperties CreatePointer() {
21 PointerProperties pointer;
22 pointer.touch_major = MockMotionEvent::TOUCH_MAJOR;
23 return pointer;
26 PointerProperties CreatePointer(float x, float y, int id) {
27 PointerProperties pointer(x, y, MockMotionEvent::TOUCH_MAJOR);
28 pointer.id = id;
29 return pointer;
32 } // namespace
34 MockMotionEvent::MockMotionEvent()
35 : MotionEventGeneric(ACTION_CANCEL, base::TimeTicks(), CreatePointer()) {
38 MockMotionEvent::MockMotionEvent(Action action)
39 : MotionEventGeneric(action, base::TimeTicks(), CreatePointer()) {
42 MockMotionEvent::MockMotionEvent(Action action,
43 TimeTicks time,
44 float x0,
45 float y0)
46 : MotionEventGeneric(action, time, CreatePointer(x0, y0, 0)) {
49 MockMotionEvent::MockMotionEvent(Action action,
50 TimeTicks time,
51 float x0,
52 float y0,
53 float x1,
54 float y1)
55 : MotionEventGeneric(action, time, CreatePointer(x0, y0, 0)) {
56 PushPointer(x1, y1);
57 if (action == ACTION_POINTER_UP || action == ACTION_POINTER_DOWN)
58 set_action_index(1);
61 MockMotionEvent::MockMotionEvent(Action action,
62 TimeTicks time,
63 float x0,
64 float y0,
65 float x1,
66 float y1,
67 float x2,
68 float y2)
69 : MotionEventGeneric(action, time, CreatePointer(x0, y0, 0)) {
70 PushPointer(x1, y1);
71 PushPointer(x2, y2);
72 if (action == ACTION_POINTER_UP || action == ACTION_POINTER_DOWN)
73 set_action_index(2);
76 MockMotionEvent::MockMotionEvent(Action action,
77 base::TimeTicks time,
78 const std::vector<gfx::PointF>& positions) {
79 set_action(action);
80 set_event_time(time);
81 set_unique_event_id(ui::GetNextTouchEventId());
82 if (action == ACTION_POINTER_UP || action == ACTION_POINTER_DOWN)
83 set_action_index(static_cast<int>(positions.size()) - 1);
84 for (size_t i = 0; i < positions.size(); ++i)
85 PushPointer(positions[i].x(), positions[i].y());
88 MockMotionEvent::MockMotionEvent(const MockMotionEvent& other)
89 : MotionEventGeneric(other) {
92 MockMotionEvent::~MockMotionEvent() {
95 MockMotionEvent& MockMotionEvent::PressPoint(float x, float y) {
96 UpdatePointersAndID();
97 PushPointer(x, y);
98 if (GetPointerCount() > 1) {
99 set_action_index(static_cast<int>(GetPointerCount()) - 1);
100 set_action(ACTION_POINTER_DOWN);
101 } else {
102 set_action(ACTION_DOWN);
104 return *this;
107 MockMotionEvent& MockMotionEvent::MovePoint(size_t index, float x, float y) {
108 UpdatePointersAndID();
109 DCHECK_LT(index, GetPointerCount());
110 PointerProperties& p = pointer(index);
111 float dx = x - p.x;
112 float dy = x - p.y;
113 p.x = x;
114 p.y = y;
115 p.raw_x += dx;
116 p.raw_y += dy;
117 set_action(ACTION_MOVE);
118 return *this;
121 MockMotionEvent& MockMotionEvent::ReleasePoint() {
122 UpdatePointersAndID();
123 DCHECK_GT(GetPointerCount(), 0U);
124 if (GetPointerCount() > 1) {
125 set_action_index(static_cast<int>(GetPointerCount()) - 1);
126 set_action(ACTION_POINTER_UP);
127 } else {
128 set_action(ACTION_UP);
130 return *this;
133 MockMotionEvent& MockMotionEvent::CancelPoint() {
134 UpdatePointersAndID();
135 DCHECK_GT(GetPointerCount(), 0U);
136 set_action(ACTION_CANCEL);
137 return *this;
140 MockMotionEvent& MockMotionEvent::SetTouchMajor(float new_touch_major) {
141 for (size_t i = 0; i < GetPointerCount(); ++i)
142 pointer(i).touch_major = new_touch_major;
143 return *this;
146 MockMotionEvent& MockMotionEvent::SetRawOffset(float raw_offset_x,
147 float raw_offset_y) {
148 for (size_t i = 0; i < GetPointerCount(); ++i) {
149 pointer(i).raw_x = pointer(i).x + raw_offset_x;
150 pointer(i).raw_y = pointer(i).y + raw_offset_y;
152 return *this;
155 MockMotionEvent& MockMotionEvent::SetToolType(size_t pointer_index,
156 ToolType tool_type) {
157 DCHECK_LT(pointer_index, GetPointerCount());
158 pointer(pointer_index).tool_type = tool_type;
159 return *this;
162 void MockMotionEvent::PushPointer(float x, float y) {
163 MotionEventGeneric::PushPointer(
164 CreatePointer(x, y, static_cast<int>(GetPointerCount())));
167 void MockMotionEvent::UpdatePointersAndID() {
168 set_action_index(-1);
169 set_unique_event_id(ui::GetNextTouchEventId());
170 switch (GetAction()) {
171 case ACTION_UP:
172 case ACTION_POINTER_UP:
173 case ACTION_CANCEL:
174 PopPointer();
175 return;
176 default:
177 break;
181 MockMotionEvent& MockMotionEvent::SetPrimaryPointerId(int id) {
182 DCHECK_GT(GetPointerCount(), 0U);
183 pointer(0).id = id;
184 return *this;
187 std::string ToString(const MotionEvent& event) {
188 std::stringstream ss;
189 ss << "MotionEvent {"
190 << "\n Action: " << event.GetAction();
191 if (event.GetAction() == MotionEvent::ACTION_POINTER_DOWN ||
192 event.GetAction() == MotionEvent::ACTION_POINTER_UP)
193 ss << "\n ActionIndex: " << event.GetActionIndex();
194 ss << "\n Flags: " << event.GetFlags()
195 << "\n ButtonState: " << event.GetButtonState() << "\n Pointers: [";
196 const size_t pointer_count = event.GetPointerCount();
197 const size_t history_size = event.GetHistorySize();
199 BitSet32 pointer_ids;
200 for (size_t i = 0; i < pointer_count; ++i) {
201 pointer_ids.mark_bit(event.GetPointerId(i));
203 // Print the pointers sorted by id.
204 while (!pointer_ids.is_empty()) {
205 int pi = event.FindPointerIndexOfId(pointer_ids.first_marked_bit());
206 DCHECK_GE(pi, 0);
207 pointer_ids.clear_first_marked_bit();
208 ss << "{"
209 << "\n PointerId: (" << event.GetPointerId(pi) << ")"
210 << "\n Pos: (" << event.GetX(pi) << ", " << event.GetY(pi) << ")"
211 << "\n RawPos: (" << event.GetX(pi) << ", " << event.GetY(pi) << ")"
212 << "\n Size: (" << event.GetTouchMajor(pi) << ", "
213 << event.GetTouchMinor(pi) << ")"
214 << "\n Orientation: " << event.GetOrientation(pi)
215 << "\n Pressure: " << event.GetPressure(pi)
216 << "\n Tool: " << event.GetToolType(pi);
217 if (history_size) {
218 ss << "\n History: [";
219 for (size_t h = 0; h < history_size; ++h) {
220 ss << "\n { " << event.GetHistoricalX(pi, h) << ", "
221 << event.GetHistoricalY(pi, h) << ", "
222 << event.GetHistoricalTouchMajor(pi, h) << ", "
223 << event.GetHistoricalEventTime(pi).ToInternalValue() << " }";
224 if (h + 1 < history_size)
225 ss << ",";
227 ss << "\n ]";
229 ss << "\n }";
230 if (i + 1 < pointer_count)
231 ss << ", ";
233 ss << "]\n}";
236 return ss.str();
239 } // namespace test
240 } // namespace ui