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