blob: 368b421e45099ad42b242f8be5d7a4d8009b8871 [file]
/*
* Copyright (C) 2026 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 <android-base/logging.h>
#include <android-base/unique_fd.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <input/Input.h>
#include <input/InputEventLabels.h>
#include <input/VirtualInputDevice.h>
#include <linux/input.h>
#include <poll.h>
#include <array>
namespace android {
namespace {
using testing::Contains;
using testing::ElementsAre;
using testing::Field;
using testing::IsSupersetOf;
using testing::Not;
MATCHER_P3(EvdevEvent, type, code, value, "evdev event with specified type, code, and value") {
bool match = arg.type == type && arg.code == code && arg.value == value;
auto expected = InputEventLookup::getLinuxEvdevLabel(type, code, value);
auto actual = InputEventLookup::getLinuxEvdevLabel(arg.type, arg.code, arg.value);
*result_listener << "expected " << expected.type << " " << expected.code << " "
<< expected.value << ", but got " << actual.type << " " << actual.code << " "
<< actual.value;
return match;
}
// A helper to create a pair of connected fds.
std::array<base::unique_fd, 2> createConnectedFds() {
int fds[2];
if (pipe(fds) != 0) {
LOG(FATAL) << "Failed to create pipe: " << strerror(errno);
}
return {base::unique_fd(fds[0]), base::unique_fd(fds[1])};
}
// Helper to read all currently available input_events from a pipe.
std::vector<input_event> readEvents(int fd) {
std::vector<input_event> events;
struct pollfd pfd = {};
pfd.fd = fd;
pfd.events = POLLIN;
while (poll(&pfd, 1, 0) > 0) {
input_event event;
if (read(fd, &event, sizeof(input_event)) == sizeof(input_event)) {
events.push_back(event);
} else {
break;
}
}
return events;
}
} // namespace
// A helper to create a dummy fd that can be used for virtual input devices.
// Since we don't want to actually interact with /dev/uinput in a unit test,
// we just use a pipe or a temporary file.
base::unique_fd createDummyFd() {
int fds[2];
if (pipe(fds) != 0) {
return base::unique_fd(-1);
}
// Close write end.
close(fds[1]);
return base::unique_fd(fds[0]);
}
TEST(VirtualTouchscreenTest, WriteTouchEvent_InvalidPointerId_DoesNotCrash) {
base::unique_fd fd = createDummyFd();
ASSERT_TRUE(fd.ok());
VirtualTouchscreen touchscreen(std::move(fd));
// Pointer ID -1 was previously allowed by validation but caused a crash in bitset::test.
int32_t pointerId = -1;
int32_t toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
int32_t action = AMOTION_EVENT_ACTION_DOWN;
float x = 100.0f;
float y = 100.0f;
float pressure = 1.0f;
float majorAxisSize = 10.0f;
std::chrono::nanoseconds eventTime(1000);
// This should return false and not crash.
bool result = touchscreen.writeTouchEvent(pointerId, toolType, action, x, y, pressure,
majorAxisSize, eventTime);
EXPECT_FALSE(result);
}
TEST(VirtualTouchscreenTest, WriteTouchEvent_ValidPointerId_DoesNotCrash) {
base::unique_fd fd = createDummyFd();
ASSERT_TRUE(fd.ok());
VirtualTouchscreen touchscreen(std::move(fd));
int32_t pointerId = 0;
int32_t toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
int32_t action = AMOTION_EVENT_ACTION_DOWN;
float x = 100.0f;
float y = 100.0f;
float pressure = 1.0f;
float majorAxisSize = 10.0f;
std::chrono::nanoseconds eventTime(1000);
// This might fail to write to the dummy fd but it should pass the validation.
// In our case, write() to the read-end of a pipe might return an error but not crash.
bool result = touchscreen.writeTouchEvent(pointerId, toolType, action, x, y, pressure,
majorAxisSize, eventTime);
EXPECT_FALSE(result);
}
TEST(VirtualTouchpadTest, WriteTouchEvent_InvalidPointerId_DoesNotCrash) {
auto [readFd, writeFd] = createConnectedFds();
VirtualTouchpad touchpad(std::move(writeFd));
// Pointer ID >= MAX_POINTERS should fail for touchpad.
bool result =
touchpad.writeTouchEvent(/*pointerId=*/MAX_POINTERS + 1, AMOTION_EVENT_TOOL_TYPE_FINGER,
AMOTION_EVENT_ACTION_DOWN, /*x=*/100.0f, /*y=*/100.0f,
/*pressure=*/1.0f, /*majorAxisSize=*/10.0f,
std::chrono::nanoseconds(1000));
EXPECT_FALSE(result);
// Verify it failed because of the invalid ID, NOT because of a pipe write error
// later in the function.
std::vector<input_event> events = readEvents(readFd.get());
EXPECT_TRUE(events.empty());
}
TEST(VirtualTouchpadTest, WriteTouchEvent_FingerCountTransition) {
auto [readFd, writeFd] = createConnectedFds();
VirtualTouchpad touchpad(std::move(writeFd));
std::chrono::nanoseconds time(1000);
// First finger (id 0) down.
touchpad.writeTouchEvent(0, AMOTION_EVENT_TOOL_TYPE_FINGER, AMOTION_EVENT_ACTION_DOWN, 10.0f,
20.0f, 1.0f, 10.0f, time);
touchpad.writeSync(time);
// Expect:
// - BTN_TOUCH(1) to indicate the surface is touched.
// - BTN_TOOL_FINGER(1) to indicate exactly one finger is active.
EXPECT_THAT(readEvents(readFd.get()),
IsSupersetOf({EvdevEvent(EV_KEY, BTN_TOUCH, 1),
EvdevEvent(EV_KEY, BTN_TOOL_FINGER, 1)}));
// Second finger (id 1) down.
touchpad.writeTouchEvent(1, AMOTION_EVENT_TOOL_TYPE_FINGER, AMOTION_EVENT_ACTION_DOWN, 30.0f,
40.0f, 1.0f, 10.0f, time);
touchpad.writeSync(time);
// Expect:
// - BTN_TOOL_FINGER(0) and BTN_TOOL_DOUBLETAP(1) to indicate transition from one finger
// to two active fingers.
EXPECT_THAT(readEvents(readFd.get()),
IsSupersetOf({EvdevEvent(EV_KEY, BTN_TOOL_FINGER, 0),
EvdevEvent(EV_KEY, BTN_TOOL_DOUBLETAP, 1)}));
// Move second finger (id 1)
touchpad.writeTouchEvent(/*pointerId=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER,
AMOTION_EVENT_ACTION_MOVE, 50.0f, 60.0f, 1.0f, 10.0f, time);
touchpad.writeSync(time);
readEvents(readFd.get());
// Release first finger (id 0)
touchpad.writeTouchEvent(0, AMOTION_EVENT_TOOL_TYPE_FINGER, AMOTION_EVENT_ACTION_UP, 10.0f,
20.0f, 1.0f, 10.0f, time);
touchpad.writeSync(time);
// Expect:
// - BTN_TOOL_DOUBLETAP(0) and BTN_TOOL_FINGER(1) to indicate transition from two active
// fingers back to one active finger.
EXPECT_THAT(readEvents(readFd.get()),
IsSupersetOf({EvdevEvent(EV_KEY, BTN_TOOL_DOUBLETAP, 0),
EvdevEvent(EV_KEY, BTN_TOOL_FINGER, 1)}));
// Release second finger (id 1)
touchpad.writeTouchEvent(1, AMOTION_EVENT_TOOL_TYPE_FINGER, AMOTION_EVENT_ACTION_UP, 50.0f,
60.0f, 1.0f, 10.0f, time);
touchpad.writeSync(time);
// Expect:
// - BTN_TOOL_FINGER(0) to indicate no fingers are active.
// - BTN_TOUCH(0) to indicate the surface is no longer touched.
EXPECT_THAT(readEvents(readFd.get()),
IsSupersetOf({EvdevEvent(EV_KEY, BTN_TOOL_FINGER, 0),
EvdevEvent(EV_KEY, BTN_TOUCH, 0)}));
}
TEST(VirtualTouchpadTest, WriteTouchEvent_TrackingIdLifecycle) {
auto [readFd, writeFd] = createConnectedFds();
VirtualTouchpad touchpad(std::move(writeFd));
std::chrono::nanoseconds time(1000);
// Press pointer 2
touchpad.writeTouchEvent(2, AMOTION_EVENT_TOOL_TYPE_FINGER, AMOTION_EVENT_ACTION_DOWN, 10.0f,
20.0f, 1.0f, 10.0f, time);
std::vector<input_event> events = readEvents(readFd.get());
int32_t firstTrackingId = -1;
for (const auto& e : events) {
if (e.type == EV_ABS && e.code == ABS_MT_TRACKING_ID) firstTrackingId = e.value;
}
EXPECT_GE(firstTrackingId, 0);
// Release pointer 2
touchpad.writeTouchEvent(2, AMOTION_EVENT_TOOL_TYPE_FINGER, AMOTION_EVENT_ACTION_UP, 10.0f,
20.0f, 1.0f, 10.0f, time);
EXPECT_THAT(readEvents(readFd.get()),
IsSupersetOf({EvdevEvent(EV_ABS, ABS_MT_TRACKING_ID, -1)}));
// Press pointer 2 again, should get a NEW tracking ID
touchpad.writeTouchEvent(2, AMOTION_EVENT_TOOL_TYPE_FINGER, AMOTION_EVENT_ACTION_DOWN, 10.0f,
20.0f, 1.0f, 10.0f, time);
events = readEvents(readFd.get());
int32_t secondTrackingId = -1;
for (const auto& e : events) {
if (e.type == EV_ABS && e.code == ABS_MT_TRACKING_ID) secondTrackingId = e.value;
}
EXPECT_NE(firstTrackingId, secondTrackingId);
}
TEST(VirtualTouchpadTest, WriteTouchEvent_ExceedsQuintTap_SafelyCaps) {
auto [readFd, writeFd] = createConnectedFds();
VirtualTouchpad touchpad(std::move(writeFd));
std::chrono::nanoseconds time(1000);
// Press 6 fingers simultaneously
for (int i = 0; i < 6; i++) {
touchpad.writeTouchEvent(i, AMOTION_EVENT_TOOL_TYPE_FINGER, AMOTION_EVENT_ACTION_DOWN,
10.0f, 20.0f, 1.0f, 10.0f, time);
}
touchpad.writeSync(time);
// Expect that the 6th finger successfully wrote to the pipe without crashing,
// and that the tool type was safely capped at 5 fingers (QUINTTAP).
EXPECT_THAT(readEvents(readFd.get()), IsSupersetOf({EvdevEvent(EV_KEY, BTN_TOOL_QUINTTAP, 1)}));
// Release the 6th finger
touchpad.writeTouchEvent(5, AMOTION_EVENT_TOOL_TYPE_FINGER, AMOTION_EVENT_ACTION_UP, 10.0f,
20.0f, 1.0f, 10.0f, time);
touchpad.writeSync(time);
std::vector<input_event> events = readEvents(readFd.get());
// Expect that releasing the 6th finger generates no EV_KEY events because
// the tool count is still clamped to 5 (QUINTTAP) and didn't change.
EXPECT_THAT(events, Not(Contains(Field(&input_event::type, EV_KEY))));
}
TEST(VirtualTouchpadTest, WriteTouchEvent_WriteFailure_ReturnsFalse) {
base::unique_fd fd = createDummyFd();
ASSERT_TRUE(fd.ok());
VirtualTouchpad touchpad(std::move(fd));
std::chrono::nanoseconds time(1000);
// This will fail because the write-end of the dummy pipe is closed.
bool result =
touchpad.writeTouchEvent(0, AMOTION_EVENT_TOOL_TYPE_FINGER, AMOTION_EVENT_ACTION_DOWN,
10.0f, 20.0f, 1.0f, 10.0f, time);
EXPECT_FALSE(result);
}
TEST(VirtualTouchpadTest, WriteButtonEvent_ValidButton_WritesEvKeyAndSync) {
auto [readFd, writeFd] = createConnectedFds();
VirtualTouchpad touchpad(std::move(writeFd));
std::chrono::nanoseconds time(1000);
// Press and release button
EXPECT_TRUE(touchpad.writeButtonEvent(AMOTION_EVENT_BUTTON_PRIMARY,
AMOTION_EVENT_ACTION_BUTTON_PRESS, time));
EXPECT_TRUE(touchpad.writeButtonEvent(AMOTION_EVENT_BUTTON_PRIMARY,
AMOTION_EVENT_ACTION_BUTTON_RELEASE, time));
std::vector<input_event> events = readEvents(readFd.get());
EXPECT_THAT(events,
ElementsAre(EvdevEvent(EV_KEY, BTN_LEFT, 1), EvdevEvent(EV_SYN, SYN_REPORT, 0),
EvdevEvent(EV_KEY, BTN_LEFT, 0), EvdevEvent(EV_SYN, SYN_REPORT, 0)));
}
} // namespace android