InputEvent_test.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. /*
  2. * Copyright (C) 2011 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <math.h>
  17. #include <binder/Parcel.h>
  18. #include <gtest/gtest.h>
  19. #include <input/Input.h>
  20. namespace android {
  21. class BaseTest : public testing::Test {
  22. protected:
  23. virtual void SetUp() { }
  24. virtual void TearDown() { }
  25. };
  26. // --- PointerCoordsTest ---
  27. class PointerCoordsTest : public BaseTest {
  28. };
  29. TEST_F(PointerCoordsTest, ClearSetsBitsToZero) {
  30. PointerCoords coords;
  31. coords.clear();
  32. ASSERT_EQ(0ULL, coords.bits);
  33. }
  34. TEST_F(PointerCoordsTest, AxisValues) {
  35. float* valuePtr;
  36. PointerCoords coords;
  37. coords.clear();
  38. // Check invariants when no axes are present.
  39. ASSERT_EQ(0, coords.getAxisValue(0))
  40. << "getAxisValue should return zero because axis is not present";
  41. ASSERT_EQ(0, coords.getAxisValue(1))
  42. << "getAxisValue should return zero because axis is not present";
  43. // Set first axis.
  44. ASSERT_EQ(OK, coords.setAxisValue(1, 5));
  45. ASSERT_EQ(5, coords.values[0]);
  46. ASSERT_EQ(0x4000000000000000ULL, coords.bits);
  47. ASSERT_EQ(0, coords.getAxisValue(0))
  48. << "getAxisValue should return zero because axis is not present";
  49. ASSERT_EQ(5, coords.getAxisValue(1))
  50. << "getAxisValue should return value of axis";
  51. // Set an axis with a higher id than all others. (appending value at the end)
  52. ASSERT_EQ(OK, coords.setAxisValue(3, 2));
  53. ASSERT_EQ(0x5000000000000000ULL, coords.bits);
  54. ASSERT_EQ(5, coords.values[0]);
  55. ASSERT_EQ(2, coords.values[1]);
  56. ASSERT_EQ(0, coords.getAxisValue(0))
  57. << "getAxisValue should return zero because axis is not present";
  58. ASSERT_EQ(5, coords.getAxisValue(1))
  59. << "getAxisValue should return value of axis";
  60. ASSERT_EQ(0, coords.getAxisValue(2))
  61. << "getAxisValue should return zero because axis is not present";
  62. ASSERT_EQ(2, coords.getAxisValue(3))
  63. << "getAxisValue should return value of axis";
  64. // Set an axis with an id lower than all others. (prepending value at beginning)
  65. ASSERT_EQ(OK, coords.setAxisValue(0, 4));
  66. ASSERT_EQ(0xd000000000000000ULL, coords.bits);
  67. ASSERT_EQ(4, coords.values[0]);
  68. ASSERT_EQ(5, coords.values[1]);
  69. ASSERT_EQ(2, coords.values[2]);
  70. ASSERT_EQ(4, coords.getAxisValue(0))
  71. << "getAxisValue should return value of axis";
  72. ASSERT_EQ(5, coords.getAxisValue(1))
  73. << "getAxisValue should return value of axis";
  74. ASSERT_EQ(0, coords.getAxisValue(2))
  75. << "getAxisValue should return zero because axis is not present";
  76. ASSERT_EQ(2, coords.getAxisValue(3))
  77. << "getAxisValue should return value of axis";
  78. // Set an axis with an id between the others. (inserting value in the middle)
  79. ASSERT_EQ(OK, coords.setAxisValue(2, 1));
  80. ASSERT_EQ(0xf000000000000000ULL, coords.bits);
  81. ASSERT_EQ(4, coords.values[0]);
  82. ASSERT_EQ(5, coords.values[1]);
  83. ASSERT_EQ(1, coords.values[2]);
  84. ASSERT_EQ(2, coords.values[3]);
  85. ASSERT_EQ(4, coords.getAxisValue(0))
  86. << "getAxisValue should return value of axis";
  87. ASSERT_EQ(5, coords.getAxisValue(1))
  88. << "getAxisValue should return value of axis";
  89. ASSERT_EQ(1, coords.getAxisValue(2))
  90. << "getAxisValue should return value of axis";
  91. ASSERT_EQ(2, coords.getAxisValue(3))
  92. << "getAxisValue should return value of axis";
  93. // Set an existing axis value in place.
  94. ASSERT_EQ(OK, coords.setAxisValue(1, 6));
  95. ASSERT_EQ(0xf000000000000000ULL, coords.bits);
  96. ASSERT_EQ(4, coords.values[0]);
  97. ASSERT_EQ(6, coords.values[1]);
  98. ASSERT_EQ(1, coords.values[2]);
  99. ASSERT_EQ(2, coords.values[3]);
  100. ASSERT_EQ(4, coords.getAxisValue(0))
  101. << "getAxisValue should return value of axis";
  102. ASSERT_EQ(6, coords.getAxisValue(1))
  103. << "getAxisValue should return value of axis";
  104. ASSERT_EQ(1, coords.getAxisValue(2))
  105. << "getAxisValue should return value of axis";
  106. ASSERT_EQ(2, coords.getAxisValue(3))
  107. << "getAxisValue should return value of axis";
  108. // Set maximum number of axes.
  109. for (size_t axis = 4; axis < PointerCoords::MAX_AXES; axis++) {
  110. ASSERT_EQ(OK, coords.setAxisValue(axis, axis));
  111. }
  112. ASSERT_EQ(PointerCoords::MAX_AXES, __builtin_popcountll(coords.bits));
  113. // Try to set one more axis beyond maximum number.
  114. // Ensure bits are unchanged.
  115. ASSERT_EQ(NO_MEMORY, coords.setAxisValue(PointerCoords::MAX_AXES, 100));
  116. ASSERT_EQ(PointerCoords::MAX_AXES, __builtin_popcountll(coords.bits));
  117. }
  118. TEST_F(PointerCoordsTest, Parcel) {
  119. Parcel parcel;
  120. PointerCoords inCoords;
  121. inCoords.clear();
  122. PointerCoords outCoords;
  123. // Round trip with empty coords.
  124. inCoords.writeToParcel(&parcel);
  125. parcel.setDataPosition(0);
  126. outCoords.readFromParcel(&parcel);
  127. ASSERT_EQ(0ULL, outCoords.bits);
  128. // Round trip with some values.
  129. parcel.freeData();
  130. inCoords.setAxisValue(2, 5);
  131. inCoords.setAxisValue(5, 8);
  132. inCoords.writeToParcel(&parcel);
  133. parcel.setDataPosition(0);
  134. outCoords.readFromParcel(&parcel);
  135. ASSERT_EQ(outCoords.bits, inCoords.bits);
  136. ASSERT_EQ(outCoords.values[0], inCoords.values[0]);
  137. ASSERT_EQ(outCoords.values[1], inCoords.values[1]);
  138. }
  139. // --- KeyEventTest ---
  140. class KeyEventTest : public BaseTest {
  141. };
  142. TEST_F(KeyEventTest, Properties) {
  143. KeyEvent event;
  144. // Initialize and get properties.
  145. const nsecs_t ARBITRARY_DOWN_TIME = 1;
  146. const nsecs_t ARBITRARY_EVENT_TIME = 2;
  147. event.initialize(2, AINPUT_SOURCE_GAMEPAD, AKEY_EVENT_ACTION_DOWN,
  148. AKEY_EVENT_FLAG_FROM_SYSTEM, AKEYCODE_BUTTON_X, 121,
  149. AMETA_ALT_ON, 1, ARBITRARY_DOWN_TIME, ARBITRARY_EVENT_TIME);
  150. ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, event.getType());
  151. ASSERT_EQ(2, event.getDeviceId());
  152. ASSERT_EQ(AINPUT_SOURCE_GAMEPAD, event.getSource());
  153. ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, event.getAction());
  154. ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, event.getFlags());
  155. ASSERT_EQ(AKEYCODE_BUTTON_X, event.getKeyCode());
  156. ASSERT_EQ(121, event.getScanCode());
  157. ASSERT_EQ(AMETA_ALT_ON, event.getMetaState());
  158. ASSERT_EQ(1, event.getRepeatCount());
  159. ASSERT_EQ(ARBITRARY_DOWN_TIME, event.getDownTime());
  160. ASSERT_EQ(ARBITRARY_EVENT_TIME, event.getEventTime());
  161. // Set source.
  162. event.setSource(AINPUT_SOURCE_JOYSTICK);
  163. ASSERT_EQ(AINPUT_SOURCE_JOYSTICK, event.getSource());
  164. }
  165. // --- MotionEventTest ---
  166. class MotionEventTest : public BaseTest {
  167. protected:
  168. static const nsecs_t ARBITRARY_DOWN_TIME;
  169. static const nsecs_t ARBITRARY_EVENT_TIME;
  170. static const float X_OFFSET;
  171. static const float Y_OFFSET;
  172. void initializeEventWithHistory(MotionEvent* event);
  173. void assertEqualsEventWithHistory(const MotionEvent* event);
  174. };
  175. const nsecs_t MotionEventTest::ARBITRARY_DOWN_TIME = 1;
  176. const nsecs_t MotionEventTest::ARBITRARY_EVENT_TIME = 2;
  177. const float MotionEventTest::X_OFFSET = 1.0f;
  178. const float MotionEventTest::Y_OFFSET = 1.1f;
  179. void MotionEventTest::initializeEventWithHistory(MotionEvent* event) {
  180. PointerProperties pointerProperties[2];
  181. pointerProperties[0].clear();
  182. pointerProperties[0].id = 1;
  183. pointerProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
  184. pointerProperties[1].clear();
  185. pointerProperties[1].id = 2;
  186. pointerProperties[1].toolType = AMOTION_EVENT_TOOL_TYPE_STYLUS;
  187. PointerCoords pointerCoords[2];
  188. pointerCoords[0].clear();
  189. pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 10);
  190. pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 11);
  191. pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 12);
  192. pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_SIZE, 13);
  193. pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, 14);
  194. pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, 15);
  195. pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, 16);
  196. pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, 17);
  197. pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, 18);
  198. pointerCoords[1].clear();
  199. pointerCoords[1].setAxisValue(AMOTION_EVENT_AXIS_X, 20);
  200. pointerCoords[1].setAxisValue(AMOTION_EVENT_AXIS_Y, 21);
  201. pointerCoords[1].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 22);
  202. pointerCoords[1].setAxisValue(AMOTION_EVENT_AXIS_SIZE, 23);
  203. pointerCoords[1].setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, 24);
  204. pointerCoords[1].setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, 25);
  205. pointerCoords[1].setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, 26);
  206. pointerCoords[1].setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, 27);
  207. pointerCoords[1].setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, 28);
  208. event->initialize(2, AINPUT_SOURCE_TOUCHSCREEN, AMOTION_EVENT_ACTION_MOVE, 0,
  209. AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED,
  210. AMOTION_EVENT_EDGE_FLAG_TOP, AMETA_ALT_ON, AMOTION_EVENT_BUTTON_PRIMARY,
  211. X_OFFSET, Y_OFFSET, 2.0f, 2.1f,
  212. ARBITRARY_DOWN_TIME, ARBITRARY_EVENT_TIME,
  213. 2, pointerProperties, pointerCoords);
  214. pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 110);
  215. pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 111);
  216. pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 112);
  217. pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_SIZE, 113);
  218. pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, 114);
  219. pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, 115);
  220. pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, 116);
  221. pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, 117);
  222. pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, 118);
  223. pointerCoords[1].setAxisValue(AMOTION_EVENT_AXIS_X, 120);
  224. pointerCoords[1].setAxisValue(AMOTION_EVENT_AXIS_Y, 121);
  225. pointerCoords[1].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 122);
  226. pointerCoords[1].setAxisValue(AMOTION_EVENT_AXIS_SIZE, 123);
  227. pointerCoords[1].setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, 124);
  228. pointerCoords[1].setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, 125);
  229. pointerCoords[1].setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, 126);
  230. pointerCoords[1].setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, 127);
  231. pointerCoords[1].setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, 128);
  232. event->addSample(ARBITRARY_EVENT_TIME + 1, pointerCoords);
  233. pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 210);
  234. pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 211);
  235. pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 212);
  236. pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_SIZE, 213);
  237. pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, 214);
  238. pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, 215);
  239. pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, 216);
  240. pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, 217);
  241. pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, 218);
  242. pointerCoords[1].setAxisValue(AMOTION_EVENT_AXIS_X, 220);
  243. pointerCoords[1].setAxisValue(AMOTION_EVENT_AXIS_Y, 221);
  244. pointerCoords[1].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 222);
  245. pointerCoords[1].setAxisValue(AMOTION_EVENT_AXIS_SIZE, 223);
  246. pointerCoords[1].setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, 224);
  247. pointerCoords[1].setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, 225);
  248. pointerCoords[1].setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, 226);
  249. pointerCoords[1].setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, 227);
  250. pointerCoords[1].setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, 228);
  251. event->addSample(ARBITRARY_EVENT_TIME + 2, pointerCoords);
  252. }
  253. void MotionEventTest::assertEqualsEventWithHistory(const MotionEvent* event) {
  254. // Check properties.
  255. ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType());
  256. ASSERT_EQ(2, event->getDeviceId());
  257. ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, event->getSource());
  258. ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, event->getAction());
  259. ASSERT_EQ(AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED, event->getFlags());
  260. ASSERT_EQ(AMOTION_EVENT_EDGE_FLAG_TOP, event->getEdgeFlags());
  261. ASSERT_EQ(AMETA_ALT_ON, event->getMetaState());
  262. ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, event->getButtonState());
  263. ASSERT_EQ(X_OFFSET, event->getXOffset());
  264. ASSERT_EQ(Y_OFFSET, event->getYOffset());
  265. ASSERT_EQ(2.0f, event->getXPrecision());
  266. ASSERT_EQ(2.1f, event->getYPrecision());
  267. ASSERT_EQ(ARBITRARY_DOWN_TIME, event->getDownTime());
  268. ASSERT_EQ(2U, event->getPointerCount());
  269. ASSERT_EQ(1, event->getPointerId(0));
  270. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, event->getToolType(0));
  271. ASSERT_EQ(2, event->getPointerId(1));
  272. ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, event->getToolType(1));
  273. ASSERT_EQ(2U, event->getHistorySize());
  274. // Check data.
  275. ASSERT_EQ(ARBITRARY_EVENT_TIME, event->getHistoricalEventTime(0));
  276. ASSERT_EQ(ARBITRARY_EVENT_TIME + 1, event->getHistoricalEventTime(1));
  277. ASSERT_EQ(ARBITRARY_EVENT_TIME + 2, event->getEventTime());
  278. ASSERT_EQ(11, event->getHistoricalRawPointerCoords(0, 0)->
  279. getAxisValue(AMOTION_EVENT_AXIS_Y));
  280. ASSERT_EQ(21, event->getHistoricalRawPointerCoords(1, 0)->
  281. getAxisValue(AMOTION_EVENT_AXIS_Y));
  282. ASSERT_EQ(111, event->getHistoricalRawPointerCoords(0, 1)->
  283. getAxisValue(AMOTION_EVENT_AXIS_Y));
  284. ASSERT_EQ(121, event->getHistoricalRawPointerCoords(1, 1)->
  285. getAxisValue(AMOTION_EVENT_AXIS_Y));
  286. ASSERT_EQ(211, event->getRawPointerCoords(0)->
  287. getAxisValue(AMOTION_EVENT_AXIS_Y));
  288. ASSERT_EQ(221, event->getRawPointerCoords(1)->
  289. getAxisValue(AMOTION_EVENT_AXIS_Y));
  290. ASSERT_EQ(11, event->getHistoricalRawAxisValue(AMOTION_EVENT_AXIS_Y, 0, 0));
  291. ASSERT_EQ(21, event->getHistoricalRawAxisValue(AMOTION_EVENT_AXIS_Y, 1, 0));
  292. ASSERT_EQ(111, event->getHistoricalRawAxisValue(AMOTION_EVENT_AXIS_Y, 0, 1));
  293. ASSERT_EQ(121, event->getHistoricalRawAxisValue(AMOTION_EVENT_AXIS_Y, 1, 1));
  294. ASSERT_EQ(211, event->getRawAxisValue(AMOTION_EVENT_AXIS_Y, 0));
  295. ASSERT_EQ(221, event->getRawAxisValue(AMOTION_EVENT_AXIS_Y, 1));
  296. ASSERT_EQ(10, event->getHistoricalRawX(0, 0));
  297. ASSERT_EQ(20, event->getHistoricalRawX(1, 0));
  298. ASSERT_EQ(110, event->getHistoricalRawX(0, 1));
  299. ASSERT_EQ(120, event->getHistoricalRawX(1, 1));
  300. ASSERT_EQ(210, event->getRawX(0));
  301. ASSERT_EQ(220, event->getRawX(1));
  302. ASSERT_EQ(11, event->getHistoricalRawY(0, 0));
  303. ASSERT_EQ(21, event->getHistoricalRawY(1, 0));
  304. ASSERT_EQ(111, event->getHistoricalRawY(0, 1));
  305. ASSERT_EQ(121, event->getHistoricalRawY(1, 1));
  306. ASSERT_EQ(211, event->getRawY(0));
  307. ASSERT_EQ(221, event->getRawY(1));
  308. ASSERT_EQ(X_OFFSET + 10, event->getHistoricalX(0, 0));
  309. ASSERT_EQ(X_OFFSET + 20, event->getHistoricalX(1, 0));
  310. ASSERT_EQ(X_OFFSET + 110, event->getHistoricalX(0, 1));
  311. ASSERT_EQ(X_OFFSET + 120, event->getHistoricalX(1, 1));
  312. ASSERT_EQ(X_OFFSET + 210, event->getX(0));
  313. ASSERT_EQ(X_OFFSET + 220, event->getX(1));
  314. ASSERT_EQ(Y_OFFSET + 11, event->getHistoricalY(0, 0));
  315. ASSERT_EQ(Y_OFFSET + 21, event->getHistoricalY(1, 0));
  316. ASSERT_EQ(Y_OFFSET + 111, event->getHistoricalY(0, 1));
  317. ASSERT_EQ(Y_OFFSET + 121, event->getHistoricalY(1, 1));
  318. ASSERT_EQ(Y_OFFSET + 211, event->getY(0));
  319. ASSERT_EQ(Y_OFFSET + 221, event->getY(1));
  320. ASSERT_EQ(12, event->getHistoricalPressure(0, 0));
  321. ASSERT_EQ(22, event->getHistoricalPressure(1, 0));
  322. ASSERT_EQ(112, event->getHistoricalPressure(0, 1));
  323. ASSERT_EQ(122, event->getHistoricalPressure(1, 1));
  324. ASSERT_EQ(212, event->getPressure(0));
  325. ASSERT_EQ(222, event->getPressure(1));
  326. ASSERT_EQ(13, event->getHistoricalSize(0, 0));
  327. ASSERT_EQ(23, event->getHistoricalSize(1, 0));
  328. ASSERT_EQ(113, event->getHistoricalSize(0, 1));
  329. ASSERT_EQ(123, event->getHistoricalSize(1, 1));
  330. ASSERT_EQ(213, event->getSize(0));
  331. ASSERT_EQ(223, event->getSize(1));
  332. ASSERT_EQ(14, event->getHistoricalTouchMajor(0, 0));
  333. ASSERT_EQ(24, event->getHistoricalTouchMajor(1, 0));
  334. ASSERT_EQ(114, event->getHistoricalTouchMajor(0, 1));
  335. ASSERT_EQ(124, event->getHistoricalTouchMajor(1, 1));
  336. ASSERT_EQ(214, event->getTouchMajor(0));
  337. ASSERT_EQ(224, event->getTouchMajor(1));
  338. ASSERT_EQ(15, event->getHistoricalTouchMinor(0, 0));
  339. ASSERT_EQ(25, event->getHistoricalTouchMinor(1, 0));
  340. ASSERT_EQ(115, event->getHistoricalTouchMinor(0, 1));
  341. ASSERT_EQ(125, event->getHistoricalTouchMinor(1, 1));
  342. ASSERT_EQ(215, event->getTouchMinor(0));
  343. ASSERT_EQ(225, event->getTouchMinor(1));
  344. ASSERT_EQ(16, event->getHistoricalToolMajor(0, 0));
  345. ASSERT_EQ(26, event->getHistoricalToolMajor(1, 0));
  346. ASSERT_EQ(116, event->getHistoricalToolMajor(0, 1));
  347. ASSERT_EQ(126, event->getHistoricalToolMajor(1, 1));
  348. ASSERT_EQ(216, event->getToolMajor(0));
  349. ASSERT_EQ(226, event->getToolMajor(1));
  350. ASSERT_EQ(17, event->getHistoricalToolMinor(0, 0));
  351. ASSERT_EQ(27, event->getHistoricalToolMinor(1, 0));
  352. ASSERT_EQ(117, event->getHistoricalToolMinor(0, 1));
  353. ASSERT_EQ(127, event->getHistoricalToolMinor(1, 1));
  354. ASSERT_EQ(217, event->getToolMinor(0));
  355. ASSERT_EQ(227, event->getToolMinor(1));
  356. ASSERT_EQ(18, event->getHistoricalOrientation(0, 0));
  357. ASSERT_EQ(28, event->getHistoricalOrientation(1, 0));
  358. ASSERT_EQ(118, event->getHistoricalOrientation(0, 1));
  359. ASSERT_EQ(128, event->getHistoricalOrientation(1, 1));
  360. ASSERT_EQ(218, event->getOrientation(0));
  361. ASSERT_EQ(228, event->getOrientation(1));
  362. }
  363. TEST_F(MotionEventTest, Properties) {
  364. MotionEvent event;
  365. // Initialize, add samples and check properties.
  366. initializeEventWithHistory(&event);
  367. ASSERT_NO_FATAL_FAILURE(assertEqualsEventWithHistory(&event));
  368. // Set source.
  369. event.setSource(AINPUT_SOURCE_JOYSTICK);
  370. ASSERT_EQ(AINPUT_SOURCE_JOYSTICK, event.getSource());
  371. // Set action.
  372. event.setAction(AMOTION_EVENT_ACTION_CANCEL);
  373. ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, event.getAction());
  374. // Set meta state.
  375. event.setMetaState(AMETA_CTRL_ON);
  376. ASSERT_EQ(AMETA_CTRL_ON, event.getMetaState());
  377. }
  378. TEST_F(MotionEventTest, CopyFrom_KeepHistory) {
  379. MotionEvent event;
  380. initializeEventWithHistory(&event);
  381. MotionEvent copy;
  382. copy.copyFrom(&event, true /*keepHistory*/);
  383. ASSERT_NO_FATAL_FAILURE(assertEqualsEventWithHistory(&event));
  384. }
  385. TEST_F(MotionEventTest, CopyFrom_DoNotKeepHistory) {
  386. MotionEvent event;
  387. initializeEventWithHistory(&event);
  388. MotionEvent copy;
  389. copy.copyFrom(&event, false /*keepHistory*/);
  390. ASSERT_EQ(event.getPointerCount(), copy.getPointerCount());
  391. ASSERT_EQ(0U, copy.getHistorySize());
  392. ASSERT_EQ(event.getPointerId(0), copy.getPointerId(0));
  393. ASSERT_EQ(event.getPointerId(1), copy.getPointerId(1));
  394. ASSERT_EQ(event.getEventTime(), copy.getEventTime());
  395. ASSERT_EQ(event.getX(0), copy.getX(0));
  396. }
  397. TEST_F(MotionEventTest, OffsetLocation) {
  398. MotionEvent event;
  399. initializeEventWithHistory(&event);
  400. event.offsetLocation(5.0f, -2.0f);
  401. ASSERT_EQ(X_OFFSET + 5.0f, event.getXOffset());
  402. ASSERT_EQ(Y_OFFSET - 2.0f, event.getYOffset());
  403. }
  404. TEST_F(MotionEventTest, Scale) {
  405. MotionEvent event;
  406. initializeEventWithHistory(&event);
  407. event.scale(2.0f);
  408. ASSERT_EQ(X_OFFSET * 2, event.getXOffset());
  409. ASSERT_EQ(Y_OFFSET * 2, event.getYOffset());
  410. ASSERT_EQ(210 * 2, event.getRawX(0));
  411. ASSERT_EQ(211 * 2, event.getRawY(0));
  412. ASSERT_EQ((X_OFFSET + 210) * 2, event.getX(0));
  413. ASSERT_EQ((Y_OFFSET + 211) * 2, event.getY(0));
  414. ASSERT_EQ(212, event.getPressure(0));
  415. ASSERT_EQ(213, event.getSize(0));
  416. ASSERT_EQ(214 * 2, event.getTouchMajor(0));
  417. ASSERT_EQ(215 * 2, event.getTouchMinor(0));
  418. ASSERT_EQ(216 * 2, event.getToolMajor(0));
  419. ASSERT_EQ(217 * 2, event.getToolMinor(0));
  420. ASSERT_EQ(218, event.getOrientation(0));
  421. }
  422. TEST_F(MotionEventTest, Parcel) {
  423. Parcel parcel;
  424. MotionEvent inEvent;
  425. initializeEventWithHistory(&inEvent);
  426. MotionEvent outEvent;
  427. // Round trip.
  428. inEvent.writeToParcel(&parcel);
  429. parcel.setDataPosition(0);
  430. outEvent.readFromParcel(&parcel);
  431. ASSERT_NO_FATAL_FAILURE(assertEqualsEventWithHistory(&outEvent));
  432. }
  433. static void setRotationMatrix(float matrix[9], float angle) {
  434. float sin = sinf(angle);
  435. float cos = cosf(angle);
  436. matrix[0] = cos;
  437. matrix[1] = -sin;
  438. matrix[2] = 0;
  439. matrix[3] = sin;
  440. matrix[4] = cos;
  441. matrix[5] = 0;
  442. matrix[6] = 0;
  443. matrix[7] = 0;
  444. matrix[8] = 1.0f;
  445. }
  446. TEST_F(MotionEventTest, Transform) {
  447. // Generate some points on a circle.
  448. // Each point 'i' is a point on a circle of radius ROTATION centered at (3,2) at an angle
  449. // of ARC * i degrees clockwise relative to the Y axis.
  450. // The geometrical representation is irrelevant to the test, it's just easy to generate
  451. // and check rotation. We set the orientation to the same angle.
  452. // Coordinate system: down is increasing Y, right is increasing X.
  453. const float PI_180 = float(M_PI / 180);
  454. const float RADIUS = 10;
  455. const float ARC = 36;
  456. const float ROTATION = ARC * 2;
  457. const size_t pointerCount = 11;
  458. PointerProperties pointerProperties[pointerCount];
  459. PointerCoords pointerCoords[pointerCount];
  460. for (size_t i = 0; i < pointerCount; i++) {
  461. float angle = float(i * ARC * PI_180);
  462. pointerProperties[i].clear();
  463. pointerProperties[i].id = i;
  464. pointerCoords[i].clear();
  465. pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, sinf(angle) * RADIUS + 3);
  466. pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, -cosf(angle) * RADIUS + 2);
  467. pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, angle);
  468. }
  469. MotionEvent event;
  470. event.initialize(0, 0, AMOTION_EVENT_ACTION_MOVE, 0, 0, 0, 0, 0,
  471. 0, 0, 0, 0, 0, 0, pointerCount, pointerProperties, pointerCoords);
  472. float originalRawX = 0 + 3;
  473. float originalRawY = -RADIUS + 2;
  474. // Check original raw X and Y assumption.
  475. ASSERT_NEAR(originalRawX, event.getRawX(0), 0.001);
  476. ASSERT_NEAR(originalRawY, event.getRawY(0), 0.001);
  477. // Now translate the motion event so the circle's origin is at (0,0).
  478. event.offsetLocation(-3, -2);
  479. // Offsetting the location should preserve the raw X and Y of the first point.
  480. ASSERT_NEAR(originalRawX, event.getRawX(0), 0.001);
  481. ASSERT_NEAR(originalRawY, event.getRawY(0), 0.001);
  482. // Apply a rotation about the origin by ROTATION degrees clockwise.
  483. float matrix[9];
  484. setRotationMatrix(matrix, ROTATION * PI_180);
  485. event.transform(matrix);
  486. // Check the points.
  487. for (size_t i = 0; i < pointerCount; i++) {
  488. float angle = float((i * ARC + ROTATION) * PI_180);
  489. ASSERT_NEAR(sinf(angle) * RADIUS, event.getX(i), 0.001);
  490. ASSERT_NEAR(-cosf(angle) * RADIUS, event.getY(i), 0.001);
  491. ASSERT_NEAR(tanf(angle), tanf(event.getOrientation(i)), 0.1);
  492. }
  493. // Applying the transformation should preserve the raw X and Y of the first point.
  494. ASSERT_NEAR(originalRawX, event.getRawX(0), 0.001);
  495. ASSERT_NEAR(originalRawY, event.getRawY(0), 0.001);
  496. }
  497. } // namespace android