summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Siarhei Vishniakou <svv@google.com> 2023-02-21 19:13:16 -0800
committer Siarhei Vishniakou <svv@google.com> 2023-02-22 21:22:37 +0000
commit56e7909f65b32c46edd8537dcfad9574cfc5b0d2 (patch)
treeb13920ef2a2cd3234489dc3bb0a3e416f5253445
parent76a3e59e2bc05e84dc99ba51628ce04686e21cc1 (diff)
Ensure real touch cancels injected events
Sometimes, tests inject events globally. If a test is not hermetic (or, it crashes), it may leave the system in an inconsistent state. To the dispatcher, it would look like the virtual device is still down. Currently, we don't subscribe to any injector death notifications (and don't plan on doing so). After this happens, the user may want to interact with the device, so the notifyMotion call would come in. In this test, make sure that the injected event is canceled when the new gesture is started. Bug: 266382436 Test: m inputflinger_tests && $ANDROID_HOST_OUT/nativetest64/inputflinger_tests/inputflinger_tests Change-Id: I9eeacd1e48cb1cd36a55acc8a129a83edd2784fc Merged-In: I9eeacd1e48cb1cd36a55acc8a129a83edd2784fc (cherry picked from commit d8f2f9034cf69eb05b98a0af55321ddb4e6874a8)
-rw-r--r--services/inputflinger/tests/InputDispatcher_test.cpp42
1 files changed, 42 insertions, 0 deletions
diff --git a/services/inputflinger/tests/InputDispatcher_test.cpp b/services/inputflinger/tests/InputDispatcher_test.cpp
index df1787b8e3..314233ec35 100644
--- a/services/inputflinger/tests/InputDispatcher_test.cpp
+++ b/services/inputflinger/tests/InputDispatcher_test.cpp
@@ -105,6 +105,8 @@ static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 1000ms;
static constexpr int expectedWallpaperFlags =
AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED;
+using ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID;
+
struct PointF {
float x;
float y;
@@ -2598,6 +2600,46 @@ TEST_F(InputDispatcherTest, MixedTouchAndMouseWithPointerDown) {
}
/**
+ * Inject a touch down and then send a new event via 'notifyMotion'. Ensure the new event cancels
+ * the injected event.
+ */
+TEST_F(InputDispatcherTest, UnfinishedInjectedEvent) {
+ std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
+ sp<FakeWindowHandle> window =
+ sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
+ window->setFrame(Rect(0, 0, 400, 400));
+
+ mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
+
+ const int32_t touchDeviceId = 4;
+ NotifyMotionArgs args;
+ // Pretend a test injects an ACTION_DOWN mouse event, but forgets to lift up the touch after
+ // completion.
+ ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
+ injectMotionEvent(mDispatcher,
+ MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
+ .deviceId(ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID)
+ .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE)
+ .x(50)
+ .y(50))
+ .build()));
+ window->consumeMotionEvent(
+ AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(VIRTUAL_KEYBOARD_ID)));
+
+ // Now a real touch comes. Rather than crashing or dropping the real event, the injected pointer
+ // should be canceled and the new gesture should take over.
+ mDispatcher->notifyMotion(&(
+ args = MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
+ .deviceId(touchDeviceId)
+ .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(300).y(100))
+ .build()));
+
+ window->consumeMotionEvent(
+ AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(VIRTUAL_KEYBOARD_ID)));
+ window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
+}
+
+/**
* This test is similar to the test above, but the sequence of injected events is different.
*
* Two windows: a window on the left and a window on the right.