2014-02-10 23:10:22 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "../InputDispatcher.h"
|
|
|
|
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
#include <linux/input.h>
|
|
|
|
|
|
|
|
namespace android {
|
|
|
|
|
|
|
|
// An arbitrary time value.
|
|
|
|
static const nsecs_t ARBITRARY_TIME = 1234;
|
|
|
|
|
|
|
|
// An arbitrary device id.
|
|
|
|
static const int32_t DEVICE_ID = 1;
|
|
|
|
|
2014-02-11 22:28:48 +00:00
|
|
|
// An arbitrary display id.
|
|
|
|
static const int32_t DISPLAY_ID = 0;
|
|
|
|
|
2014-02-10 23:10:22 +00:00
|
|
|
// An arbitrary injector pid / uid pair that has permission to inject events.
|
|
|
|
static const int32_t INJECTOR_PID = 999;
|
|
|
|
static const int32_t INJECTOR_UID = 1001;
|
|
|
|
|
|
|
|
|
|
|
|
// --- FakeInputDispatcherPolicy ---
|
|
|
|
|
|
|
|
class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface {
|
|
|
|
InputDispatcherConfiguration mConfig;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual ~FakeInputDispatcherPolicy() {
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
FakeInputDispatcherPolicy() {
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2014-10-17 09:37:08 +00:00
|
|
|
virtual void notifyConfigurationChanged(nsecs_t) {
|
2014-02-10 23:10:22 +00:00
|
|
|
}
|
|
|
|
|
2014-10-17 09:37:08 +00:00
|
|
|
virtual nsecs_t notifyANR(const sp<InputApplicationHandle>&,
|
|
|
|
const sp<InputWindowHandle>&,
|
|
|
|
const String8&) {
|
2014-02-10 23:10:22 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-10-17 09:37:08 +00:00
|
|
|
virtual void notifyInputChannelBroken(const sp<InputWindowHandle>&) {
|
2014-02-10 23:10:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) {
|
|
|
|
*outConfig = mConfig;
|
|
|
|
}
|
|
|
|
|
2014-10-17 09:37:08 +00:00
|
|
|
virtual bool filterInputEvent(const InputEvent*, uint32_t) {
|
2014-02-10 23:10:22 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-10-17 09:37:08 +00:00
|
|
|
virtual void interceptKeyBeforeQueueing(const KeyEvent*, uint32_t&) {
|
2014-02-10 23:10:22 +00:00
|
|
|
}
|
|
|
|
|
2014-10-17 09:37:08 +00:00
|
|
|
virtual void interceptMotionBeforeQueueing(nsecs_t, uint32_t&) {
|
2014-02-10 23:10:22 +00:00
|
|
|
}
|
|
|
|
|
2014-10-17 09:37:08 +00:00
|
|
|
virtual nsecs_t interceptKeyBeforeDispatching(const sp<InputWindowHandle>&,
|
|
|
|
const KeyEvent*, uint32_t) {
|
2014-02-10 23:10:22 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-10-17 09:37:08 +00:00
|
|
|
virtual bool dispatchUnhandledKey(const sp<InputWindowHandle>&,
|
|
|
|
const KeyEvent*, uint32_t, KeyEvent*) {
|
2014-02-10 23:10:22 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-10-17 09:37:08 +00:00
|
|
|
virtual void notifySwitch(nsecs_t, uint32_t, uint32_t, uint32_t) {
|
2014-02-10 23:10:22 +00:00
|
|
|
}
|
|
|
|
|
2014-10-17 09:37:08 +00:00
|
|
|
virtual void pokeUserActivity(nsecs_t, int32_t) {
|
2014-02-10 23:10:22 +00:00
|
|
|
}
|
|
|
|
|
2014-10-17 09:37:08 +00:00
|
|
|
virtual bool checkInjectEventsPermissionNonReentrant(int32_t, int32_t) {
|
2014-02-10 23:10:22 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// --- InputDispatcherTest ---
|
|
|
|
|
|
|
|
class InputDispatcherTest : public testing::Test {
|
|
|
|
protected:
|
|
|
|
sp<FakeInputDispatcherPolicy> mFakePolicy;
|
|
|
|
sp<InputDispatcher> mDispatcher;
|
|
|
|
|
|
|
|
virtual void SetUp() {
|
|
|
|
mFakePolicy = new FakeInputDispatcherPolicy();
|
|
|
|
mDispatcher = new InputDispatcher(mFakePolicy);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void TearDown() {
|
|
|
|
mFakePolicy.clear();
|
|
|
|
mDispatcher.clear();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) {
|
|
|
|
KeyEvent event;
|
|
|
|
|
|
|
|
// Rejects undefined key actions.
|
|
|
|
event.initialize(DEVICE_ID, AINPUT_SOURCE_KEYBOARD,
|
|
|
|
/*action*/ -1, 0,
|
|
|
|
AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME, ARBITRARY_TIME);
|
2014-02-11 22:28:48 +00:00
|
|
|
ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
|
|
|
|
&event, DISPLAY_ID,
|
2014-02-10 23:10:22 +00:00
|
|
|
INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
|
|
|
|
<< "Should reject key events with undefined action.";
|
|
|
|
|
|
|
|
// Rejects ACTION_MULTIPLE since it is not supported despite being defined in the API.
|
|
|
|
event.initialize(DEVICE_ID, AINPUT_SOURCE_KEYBOARD,
|
|
|
|
AKEY_EVENT_ACTION_MULTIPLE, 0,
|
|
|
|
AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME, ARBITRARY_TIME);
|
2014-02-11 22:28:48 +00:00
|
|
|
ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
|
|
|
|
&event, DISPLAY_ID,
|
2014-02-10 23:10:22 +00:00
|
|
|
INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
|
|
|
|
<< "Should reject key events with ACTION_MULTIPLE.";
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) {
|
|
|
|
MotionEvent event;
|
|
|
|
PointerProperties pointerProperties[MAX_POINTERS + 1];
|
|
|
|
PointerCoords pointerCoords[MAX_POINTERS + 1];
|
|
|
|
for (int i = 0; i <= MAX_POINTERS; i++) {
|
|
|
|
pointerProperties[i].clear();
|
|
|
|
pointerProperties[i].id = i;
|
|
|
|
pointerCoords[i].clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rejects undefined motion actions.
|
|
|
|
event.initialize(DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN,
|
2015-05-14 13:48:03 +00:00
|
|
|
/*action*/ -1, 0, 0, 0, AMETA_NONE, 0, 0, 0, 0, 0,
|
2014-02-10 23:10:22 +00:00
|
|
|
ARBITRARY_TIME, ARBITRARY_TIME,
|
|
|
|
/*pointerCount*/ 1, pointerProperties, pointerCoords);
|
2014-02-11 22:28:48 +00:00
|
|
|
ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
|
|
|
|
&event, DISPLAY_ID,
|
2014-02-10 23:10:22 +00:00
|
|
|
INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
|
|
|
|
<< "Should reject motion events with undefined action.";
|
|
|
|
|
|
|
|
// Rejects pointer down with invalid index.
|
|
|
|
event.initialize(DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN,
|
|
|
|
AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
|
2015-05-14 13:48:03 +00:00
|
|
|
0, 0, 0, AMETA_NONE, 0, 0, 0, 0, 0,
|
2014-02-10 23:10:22 +00:00
|
|
|
ARBITRARY_TIME, ARBITRARY_TIME,
|
|
|
|
/*pointerCount*/ 1, pointerProperties, pointerCoords);
|
2014-02-11 22:28:48 +00:00
|
|
|
ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
|
|
|
|
&event, DISPLAY_ID,
|
2014-02-10 23:10:22 +00:00
|
|
|
INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
|
|
|
|
<< "Should reject motion events with pointer down index too large.";
|
|
|
|
|
|
|
|
event.initialize(DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN,
|
|
|
|
AMOTION_EVENT_ACTION_POINTER_DOWN | (-1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
|
2015-05-14 13:48:03 +00:00
|
|
|
0, 0, 0, AMETA_NONE, 0, 0, 0, 0, 0,
|
2014-02-10 23:10:22 +00:00
|
|
|
ARBITRARY_TIME, ARBITRARY_TIME,
|
|
|
|
/*pointerCount*/ 1, pointerProperties, pointerCoords);
|
2014-02-11 22:28:48 +00:00
|
|
|
ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
|
|
|
|
&event, DISPLAY_ID,
|
2014-02-10 23:10:22 +00:00
|
|
|
INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
|
|
|
|
<< "Should reject motion events with pointer down index too small.";
|
|
|
|
|
|
|
|
// Rejects pointer up with invalid index.
|
|
|
|
event.initialize(DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN,
|
|
|
|
AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
|
2015-05-14 13:48:03 +00:00
|
|
|
0, 0, 0, AMETA_NONE, 0, 0, 0, 0, 0,
|
2014-02-10 23:10:22 +00:00
|
|
|
ARBITRARY_TIME, ARBITRARY_TIME,
|
|
|
|
/*pointerCount*/ 1, pointerProperties, pointerCoords);
|
2014-02-11 22:28:48 +00:00
|
|
|
ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
|
|
|
|
&event, DISPLAY_ID,
|
2014-02-10 23:10:22 +00:00
|
|
|
INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
|
|
|
|
<< "Should reject motion events with pointer up index too large.";
|
|
|
|
|
|
|
|
event.initialize(DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN,
|
|
|
|
AMOTION_EVENT_ACTION_POINTER_UP | (-1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
|
2015-05-14 13:48:03 +00:00
|
|
|
0, 0, 0, AMETA_NONE, 0, 0, 0, 0, 0,
|
2014-02-10 23:10:22 +00:00
|
|
|
ARBITRARY_TIME, ARBITRARY_TIME,
|
|
|
|
/*pointerCount*/ 1, pointerProperties, pointerCoords);
|
2014-02-11 22:28:48 +00:00
|
|
|
ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
|
|
|
|
&event, DISPLAY_ID,
|
2014-02-10 23:10:22 +00:00
|
|
|
INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
|
|
|
|
<< "Should reject motion events with pointer up index too small.";
|
|
|
|
|
|
|
|
// Rejects motion events with invalid number of pointers.
|
|
|
|
event.initialize(DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN,
|
2015-05-14 13:48:03 +00:00
|
|
|
AMOTION_EVENT_ACTION_DOWN, 0, 0, 0, AMETA_NONE, 0, 0, 0, 0, 0,
|
2014-02-10 23:10:22 +00:00
|
|
|
ARBITRARY_TIME, ARBITRARY_TIME,
|
|
|
|
/*pointerCount*/ 0, pointerProperties, pointerCoords);
|
2014-02-11 22:28:48 +00:00
|
|
|
ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
|
|
|
|
&event, DISPLAY_ID,
|
2014-02-10 23:10:22 +00:00
|
|
|
INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
|
|
|
|
<< "Should reject motion events with 0 pointers.";
|
|
|
|
|
|
|
|
event.initialize(DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN,
|
2015-05-14 13:48:03 +00:00
|
|
|
AMOTION_EVENT_ACTION_DOWN, 0, 0, 0, AMETA_NONE, 0, 0, 0, 0, 0,
|
2014-02-10 23:10:22 +00:00
|
|
|
ARBITRARY_TIME, ARBITRARY_TIME,
|
|
|
|
/*pointerCount*/ MAX_POINTERS + 1, pointerProperties, pointerCoords);
|
2014-02-11 22:28:48 +00:00
|
|
|
ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
|
|
|
|
&event, DISPLAY_ID,
|
2014-02-10 23:10:22 +00:00
|
|
|
INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
|
|
|
|
<< "Should reject motion events with more than MAX_POINTERS pointers.";
|
|
|
|
|
|
|
|
// Rejects motion events with invalid pointer ids.
|
|
|
|
pointerProperties[0].id = -1;
|
|
|
|
event.initialize(DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN,
|
2015-05-14 13:48:03 +00:00
|
|
|
AMOTION_EVENT_ACTION_DOWN, 0, 0, 0, AMETA_NONE, 0, 0, 0, 0, 0,
|
2014-02-10 23:10:22 +00:00
|
|
|
ARBITRARY_TIME, ARBITRARY_TIME,
|
|
|
|
/*pointerCount*/ 1, pointerProperties, pointerCoords);
|
2014-02-11 22:28:48 +00:00
|
|
|
ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
|
|
|
|
&event, DISPLAY_ID,
|
2014-02-10 23:10:22 +00:00
|
|
|
INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
|
|
|
|
<< "Should reject motion events with pointer ids less than 0.";
|
|
|
|
|
|
|
|
pointerProperties[0].id = MAX_POINTER_ID + 1;
|
|
|
|
event.initialize(DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN,
|
2015-05-14 13:48:03 +00:00
|
|
|
AMOTION_EVENT_ACTION_DOWN, 0, 0, 0, AMETA_NONE, 0, 0, 0, 0, 0,
|
2014-02-10 23:10:22 +00:00
|
|
|
ARBITRARY_TIME, ARBITRARY_TIME,
|
|
|
|
/*pointerCount*/ 1, pointerProperties, pointerCoords);
|
2014-02-11 22:28:48 +00:00
|
|
|
ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
|
|
|
|
&event, DISPLAY_ID,
|
2014-02-10 23:10:22 +00:00
|
|
|
INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
|
|
|
|
<< "Should reject motion events with pointer ids greater than MAX_POINTER_ID.";
|
|
|
|
|
|
|
|
// Rejects motion events with duplicate pointer ids.
|
|
|
|
pointerProperties[0].id = 1;
|
|
|
|
pointerProperties[1].id = 1;
|
|
|
|
event.initialize(DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN,
|
2015-05-14 13:48:03 +00:00
|
|
|
AMOTION_EVENT_ACTION_DOWN, 0, 0, 0, AMETA_NONE, 0, 0, 0, 0, 0,
|
2014-02-10 23:10:22 +00:00
|
|
|
ARBITRARY_TIME, ARBITRARY_TIME,
|
|
|
|
/*pointerCount*/ 2, pointerProperties, pointerCoords);
|
2014-02-11 22:28:48 +00:00
|
|
|
ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
|
|
|
|
&event, DISPLAY_ID,
|
2014-02-10 23:10:22 +00:00
|
|
|
INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
|
|
|
|
<< "Should reject motion events with duplicate pointer ids.";
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace android
|