| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 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 |  | 
 | 17 | #include "../InputDispatcher.h" | 
 | 18 |  | 
| Robert Carr | 803535b | 2018-08-02 16:38:15 -0700 | [diff] [blame] | 19 | #include <binder/Binder.h> | 
 | 20 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 21 | #include <gtest/gtest.h> | 
 | 22 | #include <linux/input.h> | 
 | 23 |  | 
 | 24 | namespace android { | 
 | 25 |  | 
 | 26 | // An arbitrary time value. | 
 | 27 | static const nsecs_t ARBITRARY_TIME = 1234; | 
 | 28 |  | 
 | 29 | // An arbitrary device id. | 
 | 30 | static const int32_t DEVICE_ID = 1; | 
 | 31 |  | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 32 | // An arbitrary display id. | 
| Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 33 | static const int32_t DISPLAY_ID = ADISPLAY_ID_DEFAULT; | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 34 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 35 | // An arbitrary injector pid / uid pair that has permission to inject events. | 
 | 36 | static const int32_t INJECTOR_PID = 999; | 
 | 37 | static const int32_t INJECTOR_UID = 1001; | 
 | 38 |  | 
 | 39 |  | 
 | 40 | // --- FakeInputDispatcherPolicy --- | 
 | 41 |  | 
 | 42 | class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface { | 
 | 43 |     InputDispatcherConfiguration mConfig; | 
 | 44 |  | 
 | 45 | protected: | 
 | 46 |     virtual ~FakeInputDispatcherPolicy() { | 
 | 47 |     } | 
 | 48 |  | 
 | 49 | public: | 
 | 50 |     FakeInputDispatcherPolicy() { | 
 | 51 |     } | 
 | 52 |  | 
 | 53 | private: | 
| Narayan Kamath | 39efe3e | 2014-10-17 10:37:08 +0100 | [diff] [blame] | 54 |     virtual void notifyConfigurationChanged(nsecs_t) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 55 |     } | 
 | 56 |  | 
| Narayan Kamath | 39efe3e | 2014-10-17 10:37:08 +0100 | [diff] [blame] | 57 |     virtual nsecs_t notifyANR(const sp<InputApplicationHandle>&, | 
| Robert Carr | 803535b | 2018-08-02 16:38:15 -0700 | [diff] [blame] | 58 |             const sp<IBinder>&, | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 59 |             const std::string&) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 60 |         return 0; | 
 | 61 |     } | 
 | 62 |  | 
| Robert Carr | 803535b | 2018-08-02 16:38:15 -0700 | [diff] [blame] | 63 |     virtual void notifyInputChannelBroken(const sp<IBinder>&) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 64 |     } | 
 | 65 |  | 
| Robert Carr | 740167f | 2018-10-11 19:03:41 -0700 | [diff] [blame] | 66 |     virtual void notifyFocusChanged(const sp<IBinder>&) { | 
 | 67 |     } | 
 | 68 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 69 |     virtual void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) { | 
 | 70 |         *outConfig = mConfig; | 
 | 71 |     } | 
 | 72 |  | 
| Narayan Kamath | 39efe3e | 2014-10-17 10:37:08 +0100 | [diff] [blame] | 73 |     virtual bool filterInputEvent(const InputEvent*, uint32_t) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 74 |         return true; | 
 | 75 |     } | 
 | 76 |  | 
| Narayan Kamath | 39efe3e | 2014-10-17 10:37:08 +0100 | [diff] [blame] | 77 |     virtual void interceptKeyBeforeQueueing(const KeyEvent*, uint32_t&) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 78 |     } | 
 | 79 |  | 
| Narayan Kamath | 39efe3e | 2014-10-17 10:37:08 +0100 | [diff] [blame] | 80 |     virtual void interceptMotionBeforeQueueing(nsecs_t, uint32_t&) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 81 |     } | 
 | 82 |  | 
| Robert Carr | 803535b | 2018-08-02 16:38:15 -0700 | [diff] [blame] | 83 |     virtual nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, | 
| Narayan Kamath | 39efe3e | 2014-10-17 10:37:08 +0100 | [diff] [blame] | 84 |             const KeyEvent*, uint32_t) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 85 |         return 0; | 
 | 86 |     } | 
 | 87 |  | 
| Robert Carr | 803535b | 2018-08-02 16:38:15 -0700 | [diff] [blame] | 88 |     virtual bool dispatchUnhandledKey(const sp<IBinder>&, | 
| Narayan Kamath | 39efe3e | 2014-10-17 10:37:08 +0100 | [diff] [blame] | 89 |             const KeyEvent*, uint32_t, KeyEvent*) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 90 |         return false; | 
 | 91 |     } | 
 | 92 |  | 
| Narayan Kamath | 39efe3e | 2014-10-17 10:37:08 +0100 | [diff] [blame] | 93 |     virtual void notifySwitch(nsecs_t, uint32_t, uint32_t, uint32_t) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 94 |     } | 
 | 95 |  | 
| Narayan Kamath | 39efe3e | 2014-10-17 10:37:08 +0100 | [diff] [blame] | 96 |     virtual void pokeUserActivity(nsecs_t, int32_t) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 97 |     } | 
 | 98 |  | 
| Narayan Kamath | 39efe3e | 2014-10-17 10:37:08 +0100 | [diff] [blame] | 99 |     virtual bool checkInjectEventsPermissionNonReentrant(int32_t, int32_t) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 100 |         return false; | 
 | 101 |     } | 
 | 102 | }; | 
 | 103 |  | 
 | 104 |  | 
 | 105 | // --- InputDispatcherTest --- | 
 | 106 |  | 
 | 107 | class InputDispatcherTest : public testing::Test { | 
 | 108 | protected: | 
 | 109 |     sp<FakeInputDispatcherPolicy> mFakePolicy; | 
 | 110 |     sp<InputDispatcher> mDispatcher; | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 111 |     sp<InputDispatcherThread> mDispatcherThread; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 112 |  | 
 | 113 |     virtual void SetUp() { | 
 | 114 |         mFakePolicy = new FakeInputDispatcherPolicy(); | 
 | 115 |         mDispatcher = new InputDispatcher(mFakePolicy); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 116 |         mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false); | 
 | 117 |         //Start InputDispatcher thread | 
 | 118 |         mDispatcherThread = new InputDispatcherThread(mDispatcher); | 
 | 119 |         mDispatcherThread->run("InputDispatcherTest", PRIORITY_URGENT_DISPLAY); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 120 |     } | 
 | 121 |  | 
 | 122 |     virtual void TearDown() { | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 123 |         mDispatcherThread->requestExit(); | 
 | 124 |         mDispatcherThread.clear(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 125 |         mFakePolicy.clear(); | 
 | 126 |         mDispatcher.clear(); | 
 | 127 |     } | 
 | 128 | }; | 
 | 129 |  | 
 | 130 |  | 
 | 131 | TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) { | 
 | 132 |     KeyEvent event; | 
 | 133 |  | 
 | 134 |     // Rejects undefined key actions. | 
| Siarhei Vishniakou | a62a8dd | 2018-06-08 21:17:33 +0100 | [diff] [blame] | 135 |     event.initialize(DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 136 |             /*action*/ -1, 0, | 
 | 137 |             AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME, ARBITRARY_TIME); | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 138 |     ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent( | 
| Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 139 |             &event, | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 140 |             INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0)) | 
 | 141 |             << "Should reject key events with undefined action."; | 
 | 142 |  | 
 | 143 |     // Rejects ACTION_MULTIPLE since it is not supported despite being defined in the API. | 
| Siarhei Vishniakou | a62a8dd | 2018-06-08 21:17:33 +0100 | [diff] [blame] | 144 |     event.initialize(DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 145 |             AKEY_EVENT_ACTION_MULTIPLE, 0, | 
 | 146 |             AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME, ARBITRARY_TIME); | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 147 |     ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent( | 
| Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 148 |             &event, | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 149 |             INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0)) | 
 | 150 |             << "Should reject key events with ACTION_MULTIPLE."; | 
 | 151 | } | 
 | 152 |  | 
 | 153 | TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) { | 
 | 154 |     MotionEvent event; | 
 | 155 |     PointerProperties pointerProperties[MAX_POINTERS + 1]; | 
 | 156 |     PointerCoords pointerCoords[MAX_POINTERS + 1]; | 
 | 157 |     for (int i = 0; i <= MAX_POINTERS; i++) { | 
 | 158 |         pointerProperties[i].clear(); | 
 | 159 |         pointerProperties[i].id = i; | 
 | 160 |         pointerCoords[i].clear(); | 
 | 161 |     } | 
 | 162 |  | 
 | 163 |     // Rejects undefined motion actions. | 
| Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 164 |     event.initialize(DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID, | 
| Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 165 |             /*action*/ -1, 0, 0, 0, AMETA_NONE, 0, 0, 0, 0, 0, | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 166 |             ARBITRARY_TIME, ARBITRARY_TIME, | 
 | 167 |             /*pointerCount*/ 1, pointerProperties, pointerCoords); | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 168 |     ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent( | 
| Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 169 |             &event, | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 170 |             INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0)) | 
 | 171 |             << "Should reject motion events with undefined action."; | 
 | 172 |  | 
 | 173 |     // Rejects pointer down with invalid index. | 
| Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 174 |     event.initialize(DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID, | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 175 |             AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), | 
| Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 176 |             0, 0, 0, AMETA_NONE, 0, 0, 0, 0, 0, | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 177 |             ARBITRARY_TIME, ARBITRARY_TIME, | 
 | 178 |             /*pointerCount*/ 1, pointerProperties, pointerCoords); | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 179 |     ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent( | 
| Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 180 |             &event, | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 181 |             INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0)) | 
 | 182 |             << "Should reject motion events with pointer down index too large."; | 
 | 183 |  | 
| Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 184 |     event.initialize(DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID, | 
| Dan Albert | 8b10c65 | 2016-02-02 17:08:05 -0800 | [diff] [blame] | 185 |             AMOTION_EVENT_ACTION_POINTER_DOWN | (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), | 
| Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 186 |             0, 0, 0, AMETA_NONE, 0, 0, 0, 0, 0, | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 187 |             ARBITRARY_TIME, ARBITRARY_TIME, | 
 | 188 |             /*pointerCount*/ 1, pointerProperties, pointerCoords); | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 189 |     ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent( | 
| Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 190 |             &event, | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 191 |             INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0)) | 
 | 192 |             << "Should reject motion events with pointer down index too small."; | 
 | 193 |  | 
 | 194 |     // Rejects pointer up with invalid index. | 
| Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 195 |     event.initialize(DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID, | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 196 |             AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), | 
| Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 197 |             0, 0, 0, AMETA_NONE, 0, 0, 0, 0, 0, | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 198 |             ARBITRARY_TIME, ARBITRARY_TIME, | 
 | 199 |             /*pointerCount*/ 1, pointerProperties, pointerCoords); | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 200 |     ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent( | 
| Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 201 |             &event, | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 202 |             INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0)) | 
 | 203 |             << "Should reject motion events with pointer up index too large."; | 
 | 204 |  | 
| Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 205 |     event.initialize(DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID, | 
| Dan Albert | 8b10c65 | 2016-02-02 17:08:05 -0800 | [diff] [blame] | 206 |             AMOTION_EVENT_ACTION_POINTER_UP | (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), | 
| Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 207 |             0, 0, 0, AMETA_NONE, 0, 0, 0, 0, 0, | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 208 |             ARBITRARY_TIME, ARBITRARY_TIME, | 
 | 209 |             /*pointerCount*/ 1, pointerProperties, pointerCoords); | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 210 |     ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent( | 
| Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 211 |             &event, | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 212 |             INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0)) | 
 | 213 |             << "Should reject motion events with pointer up index too small."; | 
 | 214 |  | 
 | 215 |     // Rejects motion events with invalid number of pointers. | 
| Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 216 |     event.initialize(DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID, | 
| Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 217 |             AMOTION_EVENT_ACTION_DOWN, 0, 0, 0, AMETA_NONE, 0, 0, 0, 0, 0, | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 218 |             ARBITRARY_TIME, ARBITRARY_TIME, | 
 | 219 |             /*pointerCount*/ 0, pointerProperties, pointerCoords); | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 220 |     ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent( | 
| Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 221 |             &event, | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 222 |             INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0)) | 
 | 223 |             << "Should reject motion events with 0 pointers."; | 
 | 224 |  | 
| Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 225 |     event.initialize(DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID, | 
| Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 226 |             AMOTION_EVENT_ACTION_DOWN, 0, 0, 0, AMETA_NONE, 0, 0, 0, 0, 0, | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 227 |             ARBITRARY_TIME, ARBITRARY_TIME, | 
 | 228 |             /*pointerCount*/ MAX_POINTERS + 1, pointerProperties, pointerCoords); | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 229 |     ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent( | 
| Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 230 |             &event, | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 231 |             INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0)) | 
 | 232 |             << "Should reject motion events with more than MAX_POINTERS pointers."; | 
 | 233 |  | 
 | 234 |     // Rejects motion events with invalid pointer ids. | 
 | 235 |     pointerProperties[0].id = -1; | 
| Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 236 |     event.initialize(DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID, | 
| Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 237 |             AMOTION_EVENT_ACTION_DOWN, 0, 0, 0, AMETA_NONE, 0, 0, 0, 0, 0, | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 238 |             ARBITRARY_TIME, ARBITRARY_TIME, | 
 | 239 |             /*pointerCount*/ 1, pointerProperties, pointerCoords); | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 240 |     ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent( | 
| Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 241 |             &event, | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 242 |             INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0)) | 
 | 243 |             << "Should reject motion events with pointer ids less than 0."; | 
 | 244 |  | 
 | 245 |     pointerProperties[0].id = MAX_POINTER_ID + 1; | 
| Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 246 |     event.initialize(DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID, | 
| Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 247 |             AMOTION_EVENT_ACTION_DOWN, 0, 0, 0, AMETA_NONE, 0, 0, 0, 0, 0, | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 248 |             ARBITRARY_TIME, ARBITRARY_TIME, | 
 | 249 |             /*pointerCount*/ 1, pointerProperties, pointerCoords); | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 250 |     ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent( | 
| Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 251 |             &event, | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 252 |             INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0)) | 
 | 253 |             << "Should reject motion events with pointer ids greater than MAX_POINTER_ID."; | 
 | 254 |  | 
 | 255 |     // Rejects motion events with duplicate pointer ids. | 
 | 256 |     pointerProperties[0].id = 1; | 
 | 257 |     pointerProperties[1].id = 1; | 
| Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 258 |     event.initialize(DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID, | 
| Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 259 |             AMOTION_EVENT_ACTION_DOWN, 0, 0, 0, AMETA_NONE, 0, 0, 0, 0, 0, | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 260 |             ARBITRARY_TIME, ARBITRARY_TIME, | 
 | 261 |             /*pointerCount*/ 2, pointerProperties, pointerCoords); | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 262 |     ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent( | 
| Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 263 |             &event, | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 264 |             INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0)) | 
 | 265 |             << "Should reject motion events with duplicate pointer ids."; | 
 | 266 | } | 
 | 267 |  | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 268 | // --- InputDispatcherTest SetInputWindowTest --- | 
 | 269 | static const int32_t INJECT_EVENT_TIMEOUT = 500; | 
 | 270 | static const int32_t DISPATCHING_TIMEOUT = 100; | 
 | 271 |  | 
 | 272 | class FakeApplicationHandle : public InputApplicationHandle { | 
 | 273 | public: | 
 | 274 |     FakeApplicationHandle() {} | 
 | 275 |     virtual ~FakeApplicationHandle() {} | 
 | 276 |  | 
 | 277 |     virtual bool updateInfo() { | 
 | 278 |         if (!mInfo) { | 
 | 279 |             mInfo = new InputApplicationInfo(); | 
 | 280 |         } | 
 | 281 |         mInfo->dispatchingTimeout = DISPATCHING_TIMEOUT; | 
 | 282 |         return true; | 
 | 283 |     } | 
 | 284 | }; | 
 | 285 |  | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 286 | class FakeInputReceiver { | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 287 | public: | 
| Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 288 |     void consumeEvent(int32_t expectedEventType, int32_t expectedDisplayId, | 
 | 289 |             int32_t expectedFlags = 0) { | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 290 |         uint32_t consumeSeq; | 
 | 291 |         InputEvent* event; | 
 | 292 |         status_t status = mConsumer->consume(&mEventFactory, false /*consumeBatches*/, -1, | 
 | 293 |             &consumeSeq, &event); | 
 | 294 |  | 
 | 295 |         ASSERT_EQ(OK, status) | 
 | 296 |                 << mName.c_str() << ": consumer consume should return OK."; | 
 | 297 |         ASSERT_TRUE(event != nullptr) | 
 | 298 |                 << mName.c_str() << ": consumer should have returned non-NULL event."; | 
 | 299 |         ASSERT_EQ(expectedEventType, event->getType()) | 
| Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 300 |                 << mName.c_str() << ": event type should match."; | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 301 |  | 
 | 302 |         ASSERT_EQ(expectedDisplayId, event->getDisplayId()) | 
| Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 303 |                 << mName.c_str() << ": event displayId should be the same as expected."; | 
 | 304 |  | 
 | 305 |         int32_t flags; | 
 | 306 |         switch (expectedEventType) { | 
 | 307 |             case AINPUT_EVENT_TYPE_KEY: { | 
 | 308 |                 KeyEvent* typedEvent = static_cast<KeyEvent*>(event); | 
 | 309 |                 flags = typedEvent->getFlags(); | 
 | 310 |                 break; | 
 | 311 |             } | 
 | 312 |             case AINPUT_EVENT_TYPE_MOTION: { | 
 | 313 |                 MotionEvent* typedEvent = static_cast<MotionEvent*>(event); | 
 | 314 |                 flags = typedEvent->getFlags(); | 
 | 315 |                 break; | 
 | 316 |             } | 
 | 317 |             default: { | 
 | 318 |                 FAIL() << mName.c_str() << ": invalid event type: " << expectedEventType; | 
 | 319 |             } | 
 | 320 |         } | 
 | 321 |         ASSERT_EQ(expectedFlags, flags) | 
 | 322 |                 << mName.c_str() << ": event flags should be the same as expected."; | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 323 |  | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 324 |         status = mConsumer->sendFinishedSignal(consumeSeq, handled()); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 325 |         ASSERT_EQ(OK, status) | 
 | 326 |                 << mName.c_str() << ": consumer sendFinishedSignal should return OK."; | 
 | 327 |     } | 
 | 328 |  | 
 | 329 |     void assertNoEvents() { | 
 | 330 |         uint32_t consumeSeq; | 
 | 331 |         InputEvent* event; | 
 | 332 |         status_t status = mConsumer->consume(&mEventFactory, false /*consumeBatches*/, -1, | 
 | 333 |             &consumeSeq, &event); | 
 | 334 |         ASSERT_NE(OK, status) | 
 | 335 |                 << mName.c_str() | 
 | 336 |                 << ": should not have received any events, so consume(..) should not return OK."; | 
 | 337 |     } | 
 | 338 |  | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 339 | protected: | 
 | 340 |         explicit FakeInputReceiver(const sp<InputDispatcher>& dispatcher, | 
 | 341 |             const std::string name, int32_t displayId) : | 
 | 342 |                 mDispatcher(dispatcher), mName(name), mDisplayId(displayId) { | 
 | 343 |             InputChannel::openInputChannelPair(name, mServerChannel, mClientChannel); | 
 | 344 |             mConsumer = new InputConsumer(mClientChannel); | 
 | 345 |         } | 
 | 346 |  | 
 | 347 |         virtual ~FakeInputReceiver() { | 
 | 348 |         } | 
 | 349 |  | 
 | 350 |         // return true if the event has been handled. | 
 | 351 |         virtual bool handled() { | 
 | 352 |             return false; | 
 | 353 |         } | 
 | 354 |  | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 355 |         sp<InputDispatcher> mDispatcher; | 
 | 356 |         sp<InputChannel> mServerChannel, mClientChannel; | 
| Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 357 |         sp<IBinder> mToken; | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 358 |         InputConsumer *mConsumer; | 
 | 359 |         PreallocatedInputEventFactory mEventFactory; | 
 | 360 |  | 
 | 361 |         std::string mName; | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 362 |         int32_t mDisplayId; | 
 | 363 | }; | 
 | 364 |  | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 365 | class FakeWindowHandle : public InputWindowHandle, public FakeInputReceiver { | 
 | 366 | public: | 
 | 367 |     static const int32_t WIDTH = 600; | 
 | 368 |     static const int32_t HEIGHT = 800; | 
 | 369 |  | 
 | 370 |     FakeWindowHandle(const sp<InputApplicationHandle>& inputApplicationHandle, | 
 | 371 |         const sp<InputDispatcher>& dispatcher, const std::string name, int32_t displayId) : | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 372 |             FakeInputReceiver(dispatcher, name, displayId), | 
 | 373 |             mFocused(false) { | 
| Robert Carr | 4e670e5 | 2018-08-15 13:26:12 -0700 | [diff] [blame] | 374 |             mServerChannel->setToken(new BBinder()); | 
| Robert Carr | 803535b | 2018-08-02 16:38:15 -0700 | [diff] [blame] | 375 |             mDispatcher->registerInputChannel(mServerChannel, displayId); | 
| Robert Carr | 740167f | 2018-10-11 19:03:41 -0700 | [diff] [blame] | 376 |   | 
 | 377 |             inputApplicationHandle->updateInfo(); | 
 | 378 |             mInfo.applicationInfo = *inputApplicationHandle->getInfo(); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 379 |     } | 
 | 380 |  | 
 | 381 |     virtual bool updateInfo() { | 
| Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 382 |         mInfo.token = mServerChannel->getToken(); | 
| Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 383 |         mInfo.name = mName; | 
 | 384 |         mInfo.layoutParamsFlags = 0; | 
 | 385 |         mInfo.layoutParamsType = InputWindowInfo::TYPE_APPLICATION; | 
 | 386 |         mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT; | 
 | 387 |         mInfo.frameLeft = 0; | 
 | 388 |         mInfo.frameTop = 0; | 
 | 389 |         mInfo.frameRight = WIDTH; | 
 | 390 |         mInfo.frameBottom = HEIGHT; | 
| Robert Carr | e07e103 | 2018-11-26 12:55:53 -0800 | [diff] [blame] | 391 |         mInfo.globalScaleFactor = 1.0; | 
| Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 392 |         mInfo.addTouchableRegion(Rect(0, 0, WIDTH, HEIGHT)); | 
 | 393 |         mInfo.visible = true; | 
 | 394 |         mInfo.canReceiveKeys = true; | 
 | 395 |         mInfo.hasFocus = mFocused; | 
 | 396 |         mInfo.hasWallpaper = false; | 
 | 397 |         mInfo.paused = false; | 
 | 398 |         mInfo.layer = 0; | 
 | 399 |         mInfo.ownerPid = INJECTOR_PID; | 
 | 400 |         mInfo.ownerUid = INJECTOR_UID; | 
 | 401 |         mInfo.inputFeatures = 0; | 
 | 402 |         mInfo.displayId = mDisplayId; | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 403 |  | 
 | 404 |         return true; | 
 | 405 |     } | 
 | 406 |  | 
 | 407 |     void setFocus() { | 
 | 408 |         mFocused = true; | 
 | 409 |     } | 
 | 410 |  | 
 | 411 |     void assertNoEvents() { | 
 | 412 |         uint32_t consumeSeq; | 
 | 413 |         InputEvent* event; | 
 | 414 |         status_t status = mConsumer->consume(&mEventFactory, false /*consumeBatches*/, -1, | 
 | 415 |             &consumeSeq, &event); | 
 | 416 |         ASSERT_NE(OK, status) | 
 | 417 |                 << mName.c_str() | 
 | 418 |                 << ": should not have received any events, so consume(..) should not return OK."; | 
 | 419 |     } | 
 | 420 | protected: | 
 | 421 |     virtual bool handled() { | 
 | 422 |         return true; | 
 | 423 |     } | 
 | 424 |  | 
 | 425 |     bool mFocused; | 
 | 426 | }; | 
 | 427 |  | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 428 | static int32_t injectKeyDown(const sp<InputDispatcher>& dispatcher, | 
 | 429 |         int32_t displayId = ADISPLAY_ID_NONE) { | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 430 |     KeyEvent event; | 
 | 431 |     nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); | 
 | 432 |  | 
 | 433 |     // Define a valid key down event. | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 434 |     event.initialize(DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId, | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 435 |             AKEY_EVENT_ACTION_DOWN, /* flags */ 0, | 
 | 436 |             AKEYCODE_A, KEY_A, AMETA_NONE, /* repeatCount */ 0, currentTime, currentTime); | 
 | 437 |  | 
 | 438 |     // Inject event until dispatch out. | 
 | 439 |     return dispatcher->injectInputEvent( | 
 | 440 |             &event, | 
 | 441 |             INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_RESULT, | 
 | 442 |             INJECT_EVENT_TIMEOUT, POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER); | 
 | 443 | } | 
 | 444 |  | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 445 | static int32_t injectMotionDown(const sp<InputDispatcher>& dispatcher, int32_t source, | 
 | 446 |         int32_t displayId) { | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 447 |     MotionEvent event; | 
 | 448 |     PointerProperties pointerProperties[1]; | 
 | 449 |     PointerCoords pointerCoords[1]; | 
 | 450 |  | 
 | 451 |     pointerProperties[0].clear(); | 
 | 452 |     pointerProperties[0].id = 0; | 
 | 453 |     pointerProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER; | 
 | 454 |  | 
 | 455 |     pointerCoords[0].clear(); | 
 | 456 |     pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 100); | 
 | 457 |     pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 200); | 
 | 458 |  | 
 | 459 |     nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); | 
 | 460 |     // Define a valid motion down event. | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 461 |     event.initialize(DEVICE_ID, source, displayId, | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 462 |             AMOTION_EVENT_ACTION_DOWN, /* actionButton */0, /* flags */ 0, /* edgeFlags */ 0, | 
 | 463 |             AMETA_NONE, /* buttonState */ 0, /* xOffset */ 0, /* yOffset */ 0, /* xPrecision */ 0, | 
 | 464 |             /* yPrecision */ 0, currentTime, currentTime, /*pointerCount*/ 1, pointerProperties, | 
 | 465 |             pointerCoords); | 
 | 466 |  | 
 | 467 |     // Inject event until dispatch out. | 
 | 468 |     return dispatcher->injectInputEvent( | 
 | 469 |             &event, | 
 | 470 |             INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_RESULT, | 
 | 471 |             INJECT_EVENT_TIMEOUT, POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER); | 
 | 472 | } | 
 | 473 |  | 
 | 474 | TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) { | 
 | 475 |     sp<FakeApplicationHandle> application = new FakeApplicationHandle(); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 476 |     sp<FakeWindowHandle> window = new FakeWindowHandle(application, mDispatcher, "Fake Window", | 
 | 477 |             ADISPLAY_ID_DEFAULT); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 478 |  | 
 | 479 |     Vector<sp<InputWindowHandle>> inputWindowHandles; | 
 | 480 |     inputWindowHandles.add(window); | 
 | 481 |  | 
 | 482 |     mDispatcher->setInputWindows(inputWindowHandles, ADISPLAY_ID_DEFAULT); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 483 |     ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher, | 
 | 484 |             AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 485 |             << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED"; | 
 | 486 |  | 
 | 487 |     // Window should receive motion event. | 
 | 488 |     window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, ADISPLAY_ID_DEFAULT); | 
 | 489 | } | 
 | 490 |  | 
 | 491 | // The foreground window should receive the first touch down event. | 
 | 492 | TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) { | 
 | 493 |     sp<FakeApplicationHandle> application = new FakeApplicationHandle(); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 494 |     sp<FakeWindowHandle> windowTop = new FakeWindowHandle(application, mDispatcher, "Top", | 
 | 495 |             ADISPLAY_ID_DEFAULT); | 
 | 496 |     sp<FakeWindowHandle> windowSecond = new FakeWindowHandle(application, mDispatcher, "Second", | 
 | 497 |             ADISPLAY_ID_DEFAULT); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 498 |  | 
 | 499 |     Vector<sp<InputWindowHandle>> inputWindowHandles; | 
 | 500 |     inputWindowHandles.add(windowTop); | 
 | 501 |     inputWindowHandles.add(windowSecond); | 
 | 502 |  | 
 | 503 |     mDispatcher->setInputWindows(inputWindowHandles, ADISPLAY_ID_DEFAULT); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 504 |     ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher, | 
 | 505 |             AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 506 |             << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED"; | 
 | 507 |  | 
 | 508 |     // Top window should receive the touch down event. Second window should not receive anything. | 
 | 509 |     windowTop->consumeEvent(AINPUT_EVENT_TYPE_MOTION, ADISPLAY_ID_DEFAULT); | 
 | 510 |     windowSecond->assertNoEvents(); | 
 | 511 | } | 
 | 512 |  | 
 | 513 | TEST_F(InputDispatcherTest, SetInputWindow_FocusedWindow) { | 
 | 514 |     sp<FakeApplicationHandle> application = new FakeApplicationHandle(); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 515 |     sp<FakeWindowHandle> windowTop = new FakeWindowHandle(application, mDispatcher, "Top", | 
 | 516 |             ADISPLAY_ID_DEFAULT); | 
 | 517 |     sp<FakeWindowHandle> windowSecond = new FakeWindowHandle(application, mDispatcher, "Second", | 
 | 518 |             ADISPLAY_ID_DEFAULT); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 519 |  | 
 | 520 |     // Set focus application. | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 521 |     mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 522 |  | 
 | 523 |     // Expect one focus window exist in display. | 
 | 524 |     windowSecond->setFocus(); | 
 | 525 |     Vector<sp<InputWindowHandle>> inputWindowHandles; | 
 | 526 |     inputWindowHandles.add(windowTop); | 
 | 527 |     inputWindowHandles.add(windowSecond); | 
 | 528 |  | 
 | 529 |     mDispatcher->setInputWindows(inputWindowHandles, ADISPLAY_ID_DEFAULT); | 
 | 530 |     ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher)) | 
 | 531 |             << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED"; | 
 | 532 |  | 
 | 533 |     // Focused window should receive event. | 
 | 534 |     windowTop->assertNoEvents(); | 
 | 535 |     windowSecond->consumeEvent(AINPUT_EVENT_TYPE_KEY, ADISPLAY_ID_NONE); | 
 | 536 | } | 
 | 537 |  | 
| Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 538 | TEST_F(InputDispatcherTest, SetInputWindow_InputWindowInfo) { | 
 | 539 |     sp<FakeApplicationHandle> application = new FakeApplicationHandle(); | 
 | 540 |  | 
 | 541 |     sp<FakeWindowHandle> windowTop = new FakeWindowHandle(application, mDispatcher, "Top", | 
 | 542 |             ADISPLAY_ID_DEFAULT); | 
 | 543 |     sp<FakeWindowHandle> windowSecond = new FakeWindowHandle(application, mDispatcher, "Second", | 
 | 544 |             ADISPLAY_ID_DEFAULT); | 
 | 545 |  | 
 | 546 |     windowTop->setFocus(); | 
 | 547 |  | 
 | 548 |     Vector<sp<InputWindowHandle>> inputWindowHandles; | 
 | 549 |     inputWindowHandles.add(windowTop); | 
 | 550 |     inputWindowHandles.add(windowSecond); | 
 | 551 |  | 
 | 552 |     mDispatcher->setInputWindows(inputWindowHandles, ADISPLAY_ID_DEFAULT); | 
 | 553 |  | 
 | 554 |     // Release channel for window is no longer valid. | 
 | 555 |     windowTop->releaseChannel(); | 
 | 556 |  | 
 | 557 |     // Test inject a motion down, should timeout because of no target channel. | 
 | 558 |     ASSERT_EQ(INPUT_EVENT_INJECTION_TIMED_OUT, injectKeyDown(mDispatcher)) | 
 | 559 |             << "Inject key event should return INPUT_EVENT_INJECTION_TIMED_OUT"; | 
 | 560 |  | 
 | 561 |     // Top window is invalid, so it should not receive any input event. | 
 | 562 |     windowTop->assertNoEvents(); | 
 | 563 |     windowSecond->assertNoEvents(); | 
 | 564 | } | 
 | 565 |  | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 566 | /* Test InputDispatcher for MultiDisplay */ | 
 | 567 | class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest { | 
 | 568 | public: | 
 | 569 |     static constexpr int32_t SECOND_DISPLAY_ID = 1; | 
 | 570 |     virtual void SetUp() { | 
 | 571 |         InputDispatcherTest::SetUp(); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 572 |  | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 573 |         application1 = new FakeApplicationHandle(); | 
 | 574 |         windowInPrimary = new FakeWindowHandle(application1, mDispatcher, "D_1", | 
 | 575 |                 ADISPLAY_ID_DEFAULT); | 
 | 576 |         Vector<sp<InputWindowHandle>> inputWindowHandles; | 
 | 577 |         inputWindowHandles.push(windowInPrimary); | 
 | 578 |         // Set focus window for primary display, but focused display would be second one. | 
 | 579 |         mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1); | 
 | 580 |         windowInPrimary->setFocus(); | 
 | 581 |         mDispatcher->setInputWindows(inputWindowHandles, ADISPLAY_ID_DEFAULT); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 582 |  | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 583 |         application2 = new FakeApplicationHandle(); | 
 | 584 |         windowInSecondary = new FakeWindowHandle(application2, mDispatcher, "D_2", | 
 | 585 |                 SECOND_DISPLAY_ID); | 
 | 586 |         // Set focus to second display window. | 
 | 587 |         Vector<sp<InputWindowHandle>> inputWindowHandles_Second; | 
 | 588 |         inputWindowHandles_Second.push(windowInSecondary); | 
 | 589 |         // Set focus display to second one. | 
 | 590 |         mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID); | 
 | 591 |         // Set focus window for second display. | 
 | 592 |         mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2); | 
 | 593 |         windowInSecondary->setFocus(); | 
 | 594 |         mDispatcher->setInputWindows(inputWindowHandles_Second, SECOND_DISPLAY_ID); | 
 | 595 |     } | 
 | 596 |  | 
 | 597 |     virtual void TearDown() { | 
 | 598 |         InputDispatcherTest::TearDown(); | 
 | 599 |  | 
 | 600 |         application1.clear(); | 
 | 601 |         windowInPrimary.clear(); | 
 | 602 |         application2.clear(); | 
 | 603 |         windowInSecondary.clear(); | 
 | 604 |     } | 
 | 605 |  | 
 | 606 | protected: | 
 | 607 |     sp<FakeApplicationHandle> application1; | 
 | 608 |     sp<FakeWindowHandle> windowInPrimary; | 
 | 609 |     sp<FakeApplicationHandle> application2; | 
 | 610 |     sp<FakeWindowHandle> windowInSecondary; | 
 | 611 | }; | 
 | 612 |  | 
 | 613 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) { | 
 | 614 |     // Test touch down on primary display. | 
 | 615 |     ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher, | 
 | 616 |             AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 617 |             << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED"; | 
 | 618 |     windowInPrimary->consumeEvent(AINPUT_EVENT_TYPE_MOTION, ADISPLAY_ID_DEFAULT); | 
 | 619 |     windowInSecondary->assertNoEvents(); | 
 | 620 |  | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 621 |     // Test touch down on second display. | 
 | 622 |     ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher, | 
 | 623 |             AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 624 |             << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED"; | 
 | 625 |     windowInPrimary->assertNoEvents(); | 
 | 626 |     windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_MOTION, SECOND_DISPLAY_ID); | 
 | 627 | } | 
 | 628 |  | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 629 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) { | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 630 |     // Test inject a key down with display id specified. | 
 | 631 |     ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)) | 
 | 632 |             << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED"; | 
 | 633 |     windowInPrimary->consumeEvent(AINPUT_EVENT_TYPE_KEY, ADISPLAY_ID_DEFAULT); | 
 | 634 |     windowInSecondary->assertNoEvents(); | 
 | 635 |  | 
 | 636 |     // Test inject a key down without display id specified. | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 637 |     ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher)) | 
 | 638 |             << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED"; | 
 | 639 |     windowInPrimary->assertNoEvents(); | 
 | 640 |     windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_KEY, ADISPLAY_ID_NONE); | 
 | 641 |  | 
 | 642 |     // Remove secondary display. | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 643 |     Vector<sp<InputWindowHandle>> noWindows; | 
 | 644 |     mDispatcher->setInputWindows(noWindows, SECOND_DISPLAY_ID); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 645 |  | 
 | 646 |     // Expect old focus should receive a cancel event. | 
| Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 647 |     windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_KEY, ADISPLAY_ID_NONE, | 
 | 648 |             AKEY_EVENT_FLAG_CANCELED); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 649 |  | 
 | 650 |     // Test inject a key down, should timeout because of no target window. | 
 | 651 |     ASSERT_EQ(INPUT_EVENT_INJECTION_TIMED_OUT, injectKeyDown(mDispatcher)) | 
 | 652 |             << "Inject key event should return INPUT_EVENT_INJECTION_TIMED_OUT"; | 
 | 653 |     windowInPrimary->assertNoEvents(); | 
 | 654 |     windowInSecondary->assertNoEvents(); | 
 | 655 | } | 
 | 656 |  | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 657 | class FakeMonitorReceiver : public FakeInputReceiver, public RefBase { | 
 | 658 | public: | 
 | 659 |     FakeMonitorReceiver(const sp<InputDispatcher>& dispatcher, const std::string name, | 
 | 660 |             int32_t displayId) : FakeInputReceiver(dispatcher, name, displayId) { | 
| Robert Carr | 803535b | 2018-08-02 16:38:15 -0700 | [diff] [blame] | 661 |         mDispatcher->registerInputChannel(mServerChannel, displayId); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 662 |     } | 
 | 663 | }; | 
 | 664 |  | 
 | 665 | // Test per-display input monitors for motion event. | 
 | 666 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) { | 
 | 667 |     sp<FakeMonitorReceiver> monitorInPrimary = | 
 | 668 |             new FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); | 
 | 669 |     sp<FakeMonitorReceiver> monitorInSecondary = | 
 | 670 |             new FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID); | 
 | 671 |  | 
 | 672 |     // Test touch down on primary display. | 
 | 673 |     ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher, | 
 | 674 |             AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) | 
 | 675 |             << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED"; | 
 | 676 |     windowInPrimary->consumeEvent(AINPUT_EVENT_TYPE_MOTION, ADISPLAY_ID_DEFAULT); | 
 | 677 |     monitorInPrimary->consumeEvent(AINPUT_EVENT_TYPE_MOTION, ADISPLAY_ID_DEFAULT); | 
 | 678 |     windowInSecondary->assertNoEvents(); | 
 | 679 |     monitorInSecondary->assertNoEvents(); | 
 | 680 |  | 
 | 681 |     // Test touch down on second display. | 
 | 682 |     ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher, | 
 | 683 |             AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) | 
 | 684 |             << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED"; | 
 | 685 |     windowInPrimary->assertNoEvents(); | 
 | 686 |     monitorInPrimary->assertNoEvents(); | 
 | 687 |     windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_MOTION, SECOND_DISPLAY_ID); | 
 | 688 |     monitorInSecondary->consumeEvent(AINPUT_EVENT_TYPE_MOTION, SECOND_DISPLAY_ID); | 
 | 689 |  | 
 | 690 |     // Test inject a non-pointer motion event. | 
 | 691 |     // If specific a display, it will dispatch to the focused window of particular display, | 
 | 692 |     // or it will dispatch to the focused window of focused display. | 
 | 693 |     ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher, | 
 | 694 |         AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE)) | 
 | 695 |             << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED"; | 
 | 696 |     windowInPrimary->assertNoEvents(); | 
 | 697 |     monitorInPrimary->assertNoEvents(); | 
 | 698 |     windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_MOTION, ADISPLAY_ID_NONE); | 
 | 699 |     monitorInSecondary->consumeEvent(AINPUT_EVENT_TYPE_MOTION, ADISPLAY_ID_NONE); | 
 | 700 | } | 
 | 701 |  | 
 | 702 | // Test per-display input monitors for key event. | 
 | 703 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) { | 
 | 704 |     //Input monitor per display. | 
 | 705 |     sp<FakeMonitorReceiver> monitorInPrimary = | 
 | 706 |             new FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); | 
 | 707 |     sp<FakeMonitorReceiver> monitorInSecondary = | 
 | 708 |             new FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID); | 
 | 709 |  | 
 | 710 |     // Test inject a key down. | 
 | 711 |     ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher)) | 
 | 712 |             << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED"; | 
 | 713 |     windowInPrimary->assertNoEvents(); | 
 | 714 |     monitorInPrimary->assertNoEvents(); | 
 | 715 |     windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_KEY, ADISPLAY_ID_NONE); | 
 | 716 |     monitorInSecondary->consumeEvent(AINPUT_EVENT_TYPE_KEY, ADISPLAY_ID_NONE); | 
 | 717 | } | 
 | 718 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 719 | } // namespace android |