InputDispatcher_test.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * Copyright (C) 2010 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 "../InputDispatcher.h"
  17. #include <gtest/gtest.h>
  18. #include <linux/input.h>
  19. namespace android {
  20. // An arbitrary time value.
  21. static const nsecs_t ARBITRARY_TIME = 1234;
  22. // An arbitrary device id.
  23. static const int32_t DEVICE_ID = 1;
  24. // An arbitrary display id.
  25. static const int32_t DISPLAY_ID = 0;
  26. // An arbitrary injector pid / uid pair that has permission to inject events.
  27. static const int32_t INJECTOR_PID = 999;
  28. static const int32_t INJECTOR_UID = 1001;
  29. // --- FakeInputDispatcherPolicy ---
  30. class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface {
  31. InputDispatcherConfiguration mConfig;
  32. protected:
  33. virtual ~FakeInputDispatcherPolicy() {
  34. }
  35. public:
  36. FakeInputDispatcherPolicy() {
  37. }
  38. private:
  39. virtual void notifyConfigurationChanged(nsecs_t) {
  40. }
  41. virtual nsecs_t notifyANR(const sp<InputApplicationHandle>&,
  42. const sp<InputWindowHandle>&,
  43. const String8&) {
  44. return 0;
  45. }
  46. virtual void notifyInputChannelBroken(const sp<InputWindowHandle>&) {
  47. }
  48. virtual void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) {
  49. *outConfig = mConfig;
  50. }
  51. virtual bool filterInputEvent(const InputEvent*, uint32_t) {
  52. return true;
  53. }
  54. virtual void interceptKeyBeforeQueueing(const KeyEvent*, uint32_t&) {
  55. }
  56. virtual void interceptMotionBeforeQueueing(nsecs_t, uint32_t&) {
  57. }
  58. virtual nsecs_t interceptKeyBeforeDispatching(const sp<InputWindowHandle>&,
  59. const KeyEvent*, uint32_t) {
  60. return 0;
  61. }
  62. virtual bool dispatchUnhandledKey(const sp<InputWindowHandle>&,
  63. const KeyEvent*, uint32_t, KeyEvent*) {
  64. return false;
  65. }
  66. virtual void notifySwitch(nsecs_t, uint32_t, uint32_t, uint32_t) {
  67. }
  68. virtual void pokeUserActivity(nsecs_t, int32_t) {
  69. }
  70. virtual bool checkInjectEventsPermissionNonReentrant(int32_t, int32_t) {
  71. return false;
  72. }
  73. };
  74. // --- InputDispatcherTest ---
  75. class InputDispatcherTest : public testing::Test {
  76. protected:
  77. sp<FakeInputDispatcherPolicy> mFakePolicy;
  78. sp<InputDispatcher> mDispatcher;
  79. virtual void SetUp() {
  80. mFakePolicy = new FakeInputDispatcherPolicy();
  81. mDispatcher = new InputDispatcher(mFakePolicy);
  82. }
  83. virtual void TearDown() {
  84. mFakePolicy.clear();
  85. mDispatcher.clear();
  86. }
  87. };
  88. TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) {
  89. KeyEvent event;
  90. // Rejects undefined key actions.
  91. event.initialize(DEVICE_ID, AINPUT_SOURCE_KEYBOARD,
  92. /*action*/ -1, 0,
  93. AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME, ARBITRARY_TIME);
  94. ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
  95. &event, DISPLAY_ID,
  96. INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
  97. << "Should reject key events with undefined action.";
  98. // Rejects ACTION_MULTIPLE since it is not supported despite being defined in the API.
  99. event.initialize(DEVICE_ID, AINPUT_SOURCE_KEYBOARD,
  100. AKEY_EVENT_ACTION_MULTIPLE, 0,
  101. AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME, ARBITRARY_TIME);
  102. ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
  103. &event, DISPLAY_ID,
  104. INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
  105. << "Should reject key events with ACTION_MULTIPLE.";
  106. }
  107. TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) {
  108. MotionEvent event;
  109. PointerProperties pointerProperties[MAX_POINTERS + 1];
  110. PointerCoords pointerCoords[MAX_POINTERS + 1];
  111. for (int i = 0; i <= MAX_POINTERS; i++) {
  112. pointerProperties[i].clear();
  113. pointerProperties[i].id = i;
  114. pointerCoords[i].clear();
  115. }
  116. // Rejects undefined motion actions.
  117. event.initialize(DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN,
  118. /*action*/ -1, 0, 0, 0, AMETA_NONE, 0, 0, 0, 0, 0,
  119. ARBITRARY_TIME, ARBITRARY_TIME,
  120. /*pointerCount*/ 1, pointerProperties, pointerCoords);
  121. ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
  122. &event, DISPLAY_ID,
  123. INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
  124. << "Should reject motion events with undefined action.";
  125. // Rejects pointer down with invalid index.
  126. event.initialize(DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN,
  127. AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
  128. 0, 0, 0, AMETA_NONE, 0, 0, 0, 0, 0,
  129. ARBITRARY_TIME, ARBITRARY_TIME,
  130. /*pointerCount*/ 1, pointerProperties, pointerCoords);
  131. ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
  132. &event, DISPLAY_ID,
  133. INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
  134. << "Should reject motion events with pointer down index too large.";
  135. event.initialize(DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN,
  136. AMOTION_EVENT_ACTION_POINTER_DOWN | (-1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
  137. 0, 0, 0, AMETA_NONE, 0, 0, 0, 0, 0,
  138. ARBITRARY_TIME, ARBITRARY_TIME,
  139. /*pointerCount*/ 1, pointerProperties, pointerCoords);
  140. ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
  141. &event, DISPLAY_ID,
  142. INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
  143. << "Should reject motion events with pointer down index too small.";
  144. // Rejects pointer up with invalid index.
  145. event.initialize(DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN,
  146. AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
  147. 0, 0, 0, AMETA_NONE, 0, 0, 0, 0, 0,
  148. ARBITRARY_TIME, ARBITRARY_TIME,
  149. /*pointerCount*/ 1, pointerProperties, pointerCoords);
  150. ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
  151. &event, DISPLAY_ID,
  152. INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
  153. << "Should reject motion events with pointer up index too large.";
  154. event.initialize(DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN,
  155. AMOTION_EVENT_ACTION_POINTER_UP | (-1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
  156. 0, 0, 0, AMETA_NONE, 0, 0, 0, 0, 0,
  157. ARBITRARY_TIME, ARBITRARY_TIME,
  158. /*pointerCount*/ 1, pointerProperties, pointerCoords);
  159. ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
  160. &event, DISPLAY_ID,
  161. INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
  162. << "Should reject motion events with pointer up index too small.";
  163. // Rejects motion events with invalid number of pointers.
  164. event.initialize(DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN,
  165. AMOTION_EVENT_ACTION_DOWN, 0, 0, 0, AMETA_NONE, 0, 0, 0, 0, 0,
  166. ARBITRARY_TIME, ARBITRARY_TIME,
  167. /*pointerCount*/ 0, pointerProperties, pointerCoords);
  168. ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
  169. &event, DISPLAY_ID,
  170. INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
  171. << "Should reject motion events with 0 pointers.";
  172. event.initialize(DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN,
  173. AMOTION_EVENT_ACTION_DOWN, 0, 0, 0, AMETA_NONE, 0, 0, 0, 0, 0,
  174. ARBITRARY_TIME, ARBITRARY_TIME,
  175. /*pointerCount*/ MAX_POINTERS + 1, pointerProperties, pointerCoords);
  176. ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
  177. &event, DISPLAY_ID,
  178. INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
  179. << "Should reject motion events with more than MAX_POINTERS pointers.";
  180. // Rejects motion events with invalid pointer ids.
  181. pointerProperties[0].id = -1;
  182. event.initialize(DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN,
  183. AMOTION_EVENT_ACTION_DOWN, 0, 0, 0, AMETA_NONE, 0, 0, 0, 0, 0,
  184. ARBITRARY_TIME, ARBITRARY_TIME,
  185. /*pointerCount*/ 1, pointerProperties, pointerCoords);
  186. ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
  187. &event, DISPLAY_ID,
  188. INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
  189. << "Should reject motion events with pointer ids less than 0.";
  190. pointerProperties[0].id = MAX_POINTER_ID + 1;
  191. event.initialize(DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN,
  192. AMOTION_EVENT_ACTION_DOWN, 0, 0, 0, AMETA_NONE, 0, 0, 0, 0, 0,
  193. ARBITRARY_TIME, ARBITRARY_TIME,
  194. /*pointerCount*/ 1, pointerProperties, pointerCoords);
  195. ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
  196. &event, DISPLAY_ID,
  197. INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
  198. << "Should reject motion events with pointer ids greater than MAX_POINTER_ID.";
  199. // Rejects motion events with duplicate pointer ids.
  200. pointerProperties[0].id = 1;
  201. pointerProperties[1].id = 1;
  202. event.initialize(DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN,
  203. AMOTION_EVENT_ACTION_DOWN, 0, 0, 0, AMETA_NONE, 0, 0, 0, 0, 0,
  204. ARBITRARY_TIME, ARBITRARY_TIME,
  205. /*pointerCount*/ 2, pointerProperties, pointerCoords);
  206. ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
  207. &event, DISPLAY_ID,
  208. INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
  209. << "Should reject motion events with duplicate pointer ids.";
  210. }
  211. } // namespace android