motion_event_test_utils.cc 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. #include "ui/events/test/motion_event_test_utils.h"
  5. #include <sstream>
  6. #include "base/check_op.h"
  7. #include "ui/events/base_event_utils.h"
  8. #include "ui/events/gesture_detection/bitset_32.h"
  9. #include "ui/events/gesture_detection/motion_event.h"
  10. using base::TimeTicks;
  11. namespace ui {
  12. namespace test {
  13. namespace {
  14. PointerProperties CreatePointer() {
  15. PointerProperties pointer;
  16. pointer.touch_major = MockMotionEvent::TOUCH_MAJOR;
  17. return pointer;
  18. }
  19. PointerProperties CreatePointer(float x, float y, int id) {
  20. PointerProperties pointer(x, y, MockMotionEvent::TOUCH_MAJOR);
  21. pointer.id = id;
  22. return pointer;
  23. }
  24. } // namespace
  25. MockMotionEvent::MockMotionEvent()
  26. : MotionEventGeneric(Action::CANCEL, base::TimeTicks(), CreatePointer()) {}
  27. MockMotionEvent::MockMotionEvent(Action action)
  28. : MotionEventGeneric(action, base::TimeTicks(), CreatePointer()) {
  29. }
  30. MockMotionEvent::MockMotionEvent(Action action,
  31. TimeTicks time,
  32. float x0,
  33. float y0)
  34. : MotionEventGeneric(action, time, CreatePointer(x0, y0, 0)) {
  35. }
  36. MockMotionEvent::MockMotionEvent(Action action,
  37. TimeTicks time,
  38. float x0,
  39. float y0,
  40. float x1,
  41. float y1)
  42. : MotionEventGeneric(action, time, CreatePointer(x0, y0, 0)) {
  43. PushPointer(x1, y1);
  44. if (action == Action::POINTER_UP || action == Action::POINTER_DOWN)
  45. set_action_index(1);
  46. }
  47. MockMotionEvent::MockMotionEvent(Action action,
  48. TimeTicks time,
  49. float x0,
  50. float y0,
  51. float x1,
  52. float y1,
  53. float x2,
  54. float y2)
  55. : MotionEventGeneric(action, time, CreatePointer(x0, y0, 0)) {
  56. PushPointer(x1, y1);
  57. PushPointer(x2, y2);
  58. if (action == Action::POINTER_UP || action == Action::POINTER_DOWN)
  59. set_action_index(2);
  60. }
  61. MockMotionEvent::MockMotionEvent(Action action,
  62. base::TimeTicks time,
  63. const std::vector<gfx::PointF>& positions) {
  64. set_action(action);
  65. set_event_time(time);
  66. set_unique_event_id(ui::GetNextTouchEventId());
  67. if (action == Action::POINTER_UP || action == Action::POINTER_DOWN)
  68. set_action_index(static_cast<int>(positions.size()) - 1);
  69. for (size_t i = 0; i < positions.size(); ++i)
  70. PushPointer(positions[i].x(), positions[i].y());
  71. }
  72. MockMotionEvent::MockMotionEvent(const MockMotionEvent& other)
  73. : MotionEventGeneric(other) {
  74. }
  75. MockMotionEvent::~MockMotionEvent() {
  76. }
  77. MockMotionEvent& MockMotionEvent::PressPoint(float x, float y) {
  78. UpdatePointersAndID();
  79. PushPointer(x, y);
  80. if (GetPointerCount() > 1) {
  81. set_action_index(static_cast<int>(GetPointerCount()) - 1);
  82. set_action(Action::POINTER_DOWN);
  83. } else {
  84. set_action(Action::DOWN);
  85. }
  86. return *this;
  87. }
  88. MockMotionEvent& MockMotionEvent::MovePoint(size_t index, float x, float y) {
  89. UpdatePointersAndID();
  90. DCHECK_LT(index, GetPointerCount());
  91. PointerProperties& p = pointer(index);
  92. float dx = x - p.x;
  93. float dy = x - p.y;
  94. p.x = x;
  95. p.y = y;
  96. p.raw_x += dx;
  97. p.raw_y += dy;
  98. set_action(Action::MOVE);
  99. return *this;
  100. }
  101. MockMotionEvent& MockMotionEvent::ReleasePoint() {
  102. DCHECK_GT(GetPointerCount(), 0U);
  103. switch (GetAction()) {
  104. // If the previous action is one of those who need removing a pointer in
  105. // UpdatePointersAndID, then the last index will be GetPointerCount() - 2.
  106. case Action::POINTER_UP:
  107. case Action::UP:
  108. case Action::CANCEL:
  109. return ReleasePointAtIndex(GetPointerCount() - 2);
  110. default:
  111. break;
  112. }
  113. return ReleasePointAtIndex(GetPointerCount() - 1);
  114. }
  115. MockMotionEvent& MockMotionEvent::ReleasePointAtIndex(size_t index) {
  116. UpdatePointersAndID();
  117. DCHECK_LT(index, GetPointerCount());
  118. if (GetPointerCount() > 1) {
  119. set_action_index(static_cast<int>(index));
  120. set_action(Action::POINTER_UP);
  121. } else {
  122. set_action(Action::UP);
  123. }
  124. return *this;
  125. }
  126. MockMotionEvent& MockMotionEvent::CancelPoint() {
  127. UpdatePointersAndID();
  128. DCHECK_GT(GetPointerCount(), 0U);
  129. set_action(Action::CANCEL);
  130. return *this;
  131. }
  132. MockMotionEvent& MockMotionEvent::SetTouchMajor(float new_touch_major) {
  133. for (size_t i = 0; i < GetPointerCount(); ++i)
  134. pointer(i).touch_major = new_touch_major;
  135. return *this;
  136. }
  137. MockMotionEvent& MockMotionEvent::SetRawOffset(float raw_offset_x,
  138. float raw_offset_y) {
  139. for (size_t i = 0; i < GetPointerCount(); ++i) {
  140. pointer(i).raw_x = pointer(i).x + raw_offset_x;
  141. pointer(i).raw_y = pointer(i).y + raw_offset_y;
  142. }
  143. return *this;
  144. }
  145. MockMotionEvent& MockMotionEvent::SetToolType(size_t pointer_index,
  146. ToolType tool_type) {
  147. DCHECK_LT(pointer_index, GetPointerCount());
  148. pointer(pointer_index).tool_type = tool_type;
  149. return *this;
  150. }
  151. void MockMotionEvent::PushPointer(float x, float y) {
  152. MotionEventGeneric::PushPointer(
  153. CreatePointer(x, y, static_cast<int>(GetPointerCount())));
  154. }
  155. void MockMotionEvent::UpdatePointersAndID() {
  156. set_unique_event_id(ui::GetNextTouchEventId());
  157. switch (GetAction()) {
  158. case Action::POINTER_UP: {
  159. int index = GetActionIndex();
  160. DCHECK_LT(index, static_cast<int>(GetPointerCount()));
  161. RemovePointerAt(index);
  162. break;
  163. }
  164. case Action::UP:
  165. case Action::CANCEL:
  166. PopPointer();
  167. break;
  168. default:
  169. break;
  170. }
  171. set_action_index(-1);
  172. }
  173. MockMotionEvent& MockMotionEvent::SetPrimaryPointerId(int id) {
  174. DCHECK_GT(GetPointerCount(), 0U);
  175. pointer(0).id = id;
  176. return *this;
  177. }
  178. MotionEvent::Classification MockMotionEvent::GetClassification() const {
  179. return gesture_classification_;
  180. }
  181. std::string ToString(const MotionEvent& event) {
  182. std::stringstream ss;
  183. ss << "MotionEvent {"
  184. << "\n Action: " << event.GetAction();
  185. if (event.GetAction() == MotionEvent::Action::POINTER_DOWN ||
  186. event.GetAction() == MotionEvent::Action::POINTER_UP)
  187. ss << "\n ActionIndex: " << event.GetActionIndex();
  188. ss << "\n Flags: " << event.GetFlags()
  189. << "\n ButtonState: " << event.GetButtonState() << "\n Pointers: [";
  190. const size_t pointer_count = event.GetPointerCount();
  191. const size_t history_size = event.GetHistorySize();
  192. BitSet32 pointer_ids;
  193. for (size_t i = 0; i < pointer_count; ++i) {
  194. pointer_ids.mark_bit(event.GetPointerId(i));
  195. // Print the pointers sorted by id.
  196. while (!pointer_ids.is_empty()) {
  197. int pi = event.FindPointerIndexOfId(pointer_ids.first_marked_bit());
  198. DCHECK_GE(pi, 0);
  199. pointer_ids.clear_first_marked_bit();
  200. ss << "{"
  201. << "\n PointerId: (" << event.GetPointerId(pi) << ")"
  202. << "\n Pos: (" << event.GetX(pi) << ", " << event.GetY(pi) << ")"
  203. << "\n RawPos: (" << event.GetX(pi) << ", " << event.GetY(pi) << ")"
  204. << "\n Size: (" << event.GetTouchMajor(pi) << ", "
  205. << event.GetTouchMinor(pi) << ")"
  206. << "\n Orientation: " << event.GetOrientation(pi)
  207. << "\n Pressure: " << event.GetPressure(pi)
  208. << "\n TiltX: " << event.GetTiltX(pi)
  209. << "\n TiltY: " << event.GetTiltY(pi)
  210. << "\n Tool: " << event.GetToolType(pi);
  211. if (history_size) {
  212. ss << "\n History: [";
  213. for (size_t h = 0; h < history_size; ++h) {
  214. ss << "\n { " << event.GetHistoricalX(pi, h) << ", "
  215. << event.GetHistoricalY(pi, h) << ", "
  216. << event.GetHistoricalTouchMajor(pi, h) << ", "
  217. << event.GetHistoricalEventTime(pi) << " }";
  218. if (h + 1 < history_size)
  219. ss << ",";
  220. }
  221. ss << "\n ]";
  222. }
  223. ss << "\n }";
  224. if (i + 1 < pointer_count)
  225. ss << ", ";
  226. }
  227. ss << "]\n}";
  228. }
  229. return ss.str();
  230. }
  231. } // namespace test
  232. } // namespace ui