Add test for heuristic palm rejection
For short strokes, the palm rejection code has a heuristic - large
touches are canceled.
Since the behaviour is currently present, let's add an integration test
for it.
Separately, we will also remove 'getLinuxToolType' here because
tool_type is not used in the palm rejection code.
However, tool_code is used in that code. So let's add getLinuxToolCode
instead. This piece does not affect the behaviour of the palm rejection
model, but it makes our inputs more correct.
Bug: 241935838
Test: atest inputflinger_tests
Change-Id: Ied9e185bdfc6e892cf3a1069b98101ca8ae9c74b
diff --git a/services/inputflinger/tests/UnwantedInteractionBlocker_test.cpp b/services/inputflinger/tests/UnwantedInteractionBlocker_test.cpp
index 8af3871..9313a45 100644
--- a/services/inputflinger/tests/UnwantedInteractionBlocker_test.cpp
+++ b/services/inputflinger/tests/UnwantedInteractionBlocker_test.cpp
@@ -16,13 +16,17 @@
#include "../UnwantedInteractionBlocker.h"
#include <android-base/silent_death_test.h>
+#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <gui/constants.h>
#include <linux/input.h>
#include <thread>
+#include "ui/events/ozone/evdev/touch_filter/neural_stylus_palm_detection_filter.h"
#include "TestInputListener.h"
+using ::testing::AllOf;
+
namespace android {
constexpr int32_t DEVICE_ID = 3;
@@ -30,6 +34,8 @@
constexpr int32_t Y_RESOLUTION = 11;
constexpr int32_t MAJOR_RESOLUTION = 1;
+const nsecs_t RESAMPLE_PERIOD = ::ui::kResamplePeriod.InNanoseconds();
+
constexpr int POINTER_0_DOWN =
AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
constexpr int POINTER_1_DOWN =
@@ -47,6 +53,19 @@
constexpr int UP = AMOTION_EVENT_ACTION_UP;
constexpr int CANCEL = AMOTION_EVENT_ACTION_CANCEL;
+constexpr int32_t FLAG_CANCELED = AMOTION_EVENT_FLAG_CANCELED;
+
+MATCHER_P(WithAction, action, "MotionEvent with specified action") {
+ bool result = true;
+ if (action == CANCEL) {
+ result &= (arg.flags & FLAG_CANCELED) != 0;
+ }
+ result &= arg.action == action;
+ *result_listener << "expected to receive " << MotionEvent::actionToString(action)
+ << " but received " << MotionEvent::actionToString(arg.action) << " instead.";
+ return result;
+}
+
static nsecs_t toNs(std::chrono::nanoseconds duration) {
return duration.count();
}
@@ -256,7 +275,7 @@
/*newSuppressedPointerIds*/ {1});
ASSERT_EQ(2u, result.size());
assertArgs(result[0], POINTER_1_UP, {{0, {1, 2, 3}}, {1, {4, 5, 6}}, {2, {7, 8, 9}}});
- ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, result[0].flags);
+ ASSERT_EQ(FLAG_CANCELED, result[0].flags);
assertArgs(result[1], MOVE, {{0, {1, 2, 3}}, {2, {7, 8, 9}}});
}
@@ -271,7 +290,7 @@
/*newSuppressedPointerIds*/ {0});
ASSERT_EQ(1u, result.size());
assertArgs(result[0], CANCEL, {{0, {1, 2, 3}}});
- ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, result[0].flags);
+ ASSERT_EQ(FLAG_CANCELED, result[0].flags);
}
/**
@@ -286,7 +305,7 @@
/*newSuppressedPointerIds*/ {1});
ASSERT_EQ(1u, result.size());
assertArgs(result[0], POINTER_1_UP, {{0, {1, 2, 3}}, {1, {4, 5, 6}}, {2, {7, 8, 9}}});
- ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, result[0].flags);
+ ASSERT_EQ(FLAG_CANCELED, result[0].flags);
}
/**
@@ -301,7 +320,7 @@
/*newSuppressedPointerIds*/ {0});
ASSERT_EQ(1u, result.size());
assertArgs(result[0], POINTER_0_UP, {{0, {1, 2, 3}}, {1, {4, 5, 6}}});
- ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, result[0].flags);
+ ASSERT_EQ(FLAG_CANCELED, result[0].flags);
}
/**
@@ -316,7 +335,7 @@
/*newSuppressedPointerIds*/ {0, 1});
ASSERT_EQ(1u, result.size());
assertArgs(result[0], CANCEL, {{0, {1, 2, 3}}, {1, {4, 5, 6}}});
- ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, result[0].flags);
+ ASSERT_EQ(FLAG_CANCELED, result[0].flags);
}
/**
@@ -332,7 +351,7 @@
/*newSuppressedPointerIds*/ {0, 1});
ASSERT_EQ(1u, result.size());
assertArgs(result[0], CANCEL, {{0, {1, 2, 3}}});
- ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, result[0].flags);
+ ASSERT_EQ(FLAG_CANCELED, result[0].flags);
}
/**
@@ -573,6 +592,30 @@
dumpThread.join();
}
+/**
+ * Heuristic filter that's present in the palm rejection model blocks touches early if the size
+ * of the touch is large. This is an integration test that checks that this filter kicks in.
+ */
+TEST_F(UnwantedInteractionBlockerTest, HeuristicFilterWorks) {
+ mBlocker->notifyInputDevicesChanged({generateTestDeviceInfo()});
+ // Small touch down
+ NotifyMotionArgs args1 = generateMotionArgs(0 /*downTime*/, 0 /*eventTime*/, DOWN, {{1, 2, 3}});
+ mBlocker->notifyMotion(&args1);
+ mTestListener.assertNotifyMotionWasCalled(WithAction(DOWN));
+
+ // Large touch oval on the next move
+ NotifyMotionArgs args2 =
+ generateMotionArgs(0 /*downTime*/, RESAMPLE_PERIOD, MOVE, {{4, 5, 200}});
+ mBlocker->notifyMotion(&args2);
+ mTestListener.assertNotifyMotionWasCalled(WithAction(MOVE));
+
+ // Lift up the touch to force the model to decide on whether it's a palm
+ NotifyMotionArgs args3 =
+ generateMotionArgs(0 /*downTime*/, 2 * RESAMPLE_PERIOD, UP, {{4, 5, 200}});
+ mBlocker->notifyMotion(&args3);
+ mTestListener.assertNotifyMotionWasCalled(WithAction(CANCEL));
+}
+
using UnwantedInteractionBlockerTestDeathTest = UnwantedInteractionBlockerTest;
/**
@@ -672,7 +715,7 @@
{{1433.0, 751.0, 44.0}, {1070.0, 771.0, 13.0}}));
ASSERT_EQ(2u, argsList.size());
ASSERT_EQ(POINTER_0_UP, argsList[0].action);
- ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, argsList[0].flags);
+ ASSERT_EQ(FLAG_CANCELED, argsList[0].flags);
ASSERT_EQ(MOVE, argsList[1].action);
ASSERT_EQ(1u, argsList[1].pointerCount);
ASSERT_EQ(0, argsList[1].flags);
@@ -849,7 +892,7 @@
ASSERT_EQ(2u, argsList.size());
// First event - cancel pointer 1
ASSERT_EQ(POINTER_1_UP, argsList[0].action);
- ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, argsList[0].flags);
+ ASSERT_EQ(FLAG_CANCELED, argsList[0].flags);
// Second event - send MOVE for the remaining pointer
ASSERT_EQ(MOVE, argsList[1].action);
ASSERT_EQ(0, argsList[1].flags);
@@ -890,7 +933,7 @@
// Cancel all
ASSERT_EQ(CANCEL, argsList[0].action);
ASSERT_EQ(2u, argsList[0].pointerCount);
- ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, argsList[0].flags);
+ ASSERT_EQ(FLAG_CANCELED, argsList[0].flags);
// Future move events are ignored
argsList = mPalmRejector->processMotion(
@@ -936,7 +979,7 @@
{{1414.0, 702.0, 41.0}, {1059.0, 731.0, 12.0}}));
ASSERT_EQ(1u, argsList.size());
ASSERT_EQ(CANCEL, argsList[0].action) << MotionEvent::actionToString(argsList[0].action);
- ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, argsList[0].flags);
+ ASSERT_EQ(FLAG_CANCELED, argsList[0].flags);
// Future move events should not go to the listener.
argsList = mPalmRejector->processMotion(
@@ -970,7 +1013,7 @@
{{1417.0, 685.0, 41.0}, {1060, 700, 10.0}}));
ASSERT_EQ(2u, argsList.size());
ASSERT_EQ(POINTER_1_UP, argsList[0].action);
- ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, argsList[0].flags);
+ ASSERT_EQ(FLAG_CANCELED, argsList[0].flags);
ASSERT_EQ(MOVE, argsList[1].action) << MotionEvent::actionToString(argsList[1].action);
ASSERT_EQ(0, argsList[1].flags);