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 namespace { 14 15 PointerProperties CreatePointer() { 16 PointerProperties pointer; 17 pointer.touch_major = MockMotionEvent::TOUCH_MAJOR; 18 return pointer; 19 } 20 21 PointerProperties 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 31 MockMotionEvent::MockMotionEvent() 32 : MotionEventGeneric(ACTION_CANCEL, base::TimeTicks(), CreatePointer()) { 33 } 34 35 MockMotionEvent::MockMotionEvent(Action action) 36 : MotionEventGeneric(action, base::TimeTicks(), CreatePointer()) { 37 } 38 39 MockMotionEvent::MockMotionEvent(Action action, 40 TimeTicks time, 41 float x0, 42 float y0) 43 : MotionEventGeneric(action, time, CreatePointer(x0, y0, 0)) { 44 } 45 46 MockMotionEvent::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 58 MockMotionEvent::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 73 MockMotionEvent::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 84 MockMotionEvent::MockMotionEvent(const MockMotionEvent& other) 85 : MotionEventGeneric(other) { 86 } 87 88 MockMotionEvent::~MockMotionEvent() {} 89 90 scoped_ptr<MotionEvent> MockMotionEvent::Clone() const { 91 return scoped_ptr<MotionEvent>(new MockMotionEvent(*this)); 92 } 93 94 scoped_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 100 void 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 111 void 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 124 void 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 135 void MockMotionEvent::CancelPoint() { 136 ResolvePointers(); 137 DCHECK_GT(GetPointerCount(), 0U); 138 set_action(ACTION_CANCEL); 139 } 140 141 void 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 146 void 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 153 void 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 158 void MockMotionEvent::PushPointer(float x, float y) { 159 MotionEventGeneric::PushPointer( 160 CreatePointer(x, y, static_cast<int>(GetPointerCount()))); 161 } 162 163 void 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