d6e0a91b5a1f9f39adff2eca69b8e41e417f67a0
[platform/framework/web/crosswalk.git] / src / ui / events / gesture_detection / mock_motion_event.cc
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/gesture_detection/mock_motion_event.h"
6
7 #include "base/logging.h"
8
9 using base::TimeTicks;
10
11 namespace ui {
12
13 MockMotionEvent::MockMotionEvent()
14     : action(ACTION_CANCEL), pointer_count(1), touch_major(TOUCH_MAJOR), id(0) {
15 }
16
17 MockMotionEvent::MockMotionEvent(Action action)
18     : action(action), pointer_count(1), touch_major(TOUCH_MAJOR), id(0) {
19 }
20
21 MockMotionEvent::MockMotionEvent(Action action,
22                                  TimeTicks time,
23                                  float x,
24                                  float y)
25     : action(action),
26       pointer_count(1),
27       time(time),
28       touch_major(TOUCH_MAJOR),
29       id(0) {
30   points[0].SetPoint(x, y);
31 }
32
33 MockMotionEvent::MockMotionEvent(Action action,
34                                  TimeTicks time,
35                                  float x0,
36                                  float y0,
37                                  float x1,
38                                  float y1)
39     : action(action),
40       pointer_count(2),
41       time(time),
42       touch_major(TOUCH_MAJOR),
43       id(0) {
44   points[0].SetPoint(x0, y0);
45   points[1].SetPoint(x1, y1);
46 }
47
48 MockMotionEvent::MockMotionEvent(Action action,
49                                  TimeTicks time,
50                                  float x0,
51                                  float y0,
52                                  float x1,
53                                  float y1,
54                                  float x2,
55                                  float y2)
56     : action(action),
57       pointer_count(3),
58       time(time),
59       touch_major(TOUCH_MAJOR),
60       id(0) {
61   points[0].SetPoint(x0, y0);
62   points[1].SetPoint(x1, y1);
63   points[2].SetPoint(x2, y2);
64 }
65
66 MockMotionEvent::MockMotionEvent(const MockMotionEvent& other)
67     : action(other.action),
68       pointer_count(other.pointer_count),
69       time(other.time),
70       touch_major(other.touch_major),
71       id(other.GetId()) {
72   for (size_t i = 0; i < pointer_count; ++i)
73     points[i] = other.points[i];
74 }
75
76 MockMotionEvent::~MockMotionEvent() {}
77
78 MotionEvent::Action MockMotionEvent::GetAction() const { return action; }
79
80 int MockMotionEvent::GetActionIndex() const {
81   return static_cast<int>(pointer_count) - 1;
82 }
83
84 size_t MockMotionEvent::GetPointerCount() const { return pointer_count; }
85
86 int MockMotionEvent::GetId() const {
87   return id;
88 }
89
90 int MockMotionEvent::GetPointerId(size_t pointer_index) const {
91   DCHECK(pointer_index < pointer_count);
92   return static_cast<int>(pointer_index);
93 }
94
95 float MockMotionEvent::GetX(size_t pointer_index) const {
96   return points[pointer_index].x();
97 }
98
99 float MockMotionEvent::GetY(size_t pointer_index) const {
100   return points[pointer_index].y();
101 }
102
103 float MockMotionEvent::GetRawX(size_t pointer_index) const {
104   return GetX(pointer_index) + raw_offset.x();
105 }
106
107 float MockMotionEvent::GetRawY(size_t pointer_index) const {
108   return GetY(pointer_index) + raw_offset.y();
109 }
110
111 float MockMotionEvent::GetTouchMajor(size_t pointer_index) const {
112   return touch_major;
113 }
114
115 float MockMotionEvent::GetPressure(size_t pointer_index) const {
116   return 0;
117 }
118
119 TimeTicks MockMotionEvent::GetEventTime() const { return time; }
120
121 size_t MockMotionEvent::GetHistorySize() const { return 0; }
122
123 TimeTicks MockMotionEvent::GetHistoricalEventTime(
124     size_t historical_index) const {
125   return TimeTicks();
126 }
127
128 float MockMotionEvent::GetHistoricalTouchMajor(size_t pointer_index,
129                                                size_t historical_index) const {
130   return 0;
131 }
132
133 float MockMotionEvent::GetHistoricalX(size_t pointer_index,
134                                       size_t historical_index) const {
135   return 0;
136 }
137
138 float MockMotionEvent::GetHistoricalY(size_t pointer_index,
139                                       size_t historical_index) const {
140   return 0;
141 }
142
143 scoped_ptr<MotionEvent> MockMotionEvent::Clone() const {
144   return scoped_ptr<MotionEvent>(new MockMotionEvent(*this));
145 }
146
147 scoped_ptr<MotionEvent> MockMotionEvent::Cancel() const {
148   scoped_ptr<MockMotionEvent> cancel_event(new MockMotionEvent(*this));
149   cancel_event->action = MotionEvent::ACTION_CANCEL;
150   return cancel_event.PassAs<MotionEvent>();
151 }
152
153 void MockMotionEvent::SetId(int new_id) {
154   id = new_id;
155 }
156
157 void MockMotionEvent::SetTime(base::TimeTicks new_time) {
158   time = new_time;
159 }
160
161 void MockMotionEvent::PressPoint(float x, float y) {
162   // Reset the pointer count if the previously released and/or cancelled pointer
163   // was the last pointer in the event.
164   if (pointer_count == 1 && (action == ACTION_UP || action == ACTION_CANCEL))
165     pointer_count = 0;
166
167   DCHECK_LT(pointer_count, static_cast<size_t>(MAX_POINTERS));
168   points[pointer_count++] = gfx::PointF(x, y);
169   action = pointer_count > 1 ? ACTION_POINTER_DOWN : ACTION_DOWN;
170 }
171
172 void MockMotionEvent::MovePoint(size_t index, float x, float y) {
173   DCHECK_LT(index, pointer_count);
174   points[index] = gfx::PointF(x, y);
175   action = ACTION_MOVE;
176 }
177
178 void MockMotionEvent::ReleasePoint() {
179   DCHECK_GT(pointer_count, 0U);
180   if (pointer_count > 1) {
181     --pointer_count;
182     action = ACTION_POINTER_UP;
183   } else {
184     action = ACTION_UP;
185   }
186 }
187
188 void MockMotionEvent::CancelPoint() {
189   DCHECK_GT(pointer_count, 0U);
190   if (pointer_count > 1)
191     --pointer_count;
192   action = ACTION_CANCEL;
193 }
194
195 void MockMotionEvent::SetTouchMajor(float new_touch_major) {
196   touch_major = new_touch_major;
197 }
198
199 void MockMotionEvent::SetRawOffset(float raw_offset_x, float raw_offset_y) {
200   raw_offset.set_x(raw_offset_x);
201   raw_offset.set_y(raw_offset_y);
202 }
203
204 }  // namespace ui