Merge changes Ibdd75ae6,If628a505 into main
* changes:
s/TestInputListenerMatchers.h/TestEventMatchers.h
Move all matchers from dispatcher tests to TestInputListenerMatchers
diff --git a/services/inputflinger/tests/Android.bp b/services/inputflinger/tests/Android.bp
index 76b04b6..64e8825 100644
--- a/services/inputflinger/tests/Android.bp
+++ b/services/inputflinger/tests/Android.bp
@@ -66,7 +66,6 @@
"SyncQueue_test.cpp",
"TimerProvider_test.cpp",
"TestInputListener.cpp",
- "TestInputListenerMatchers.cpp",
"TouchpadInputMapper_test.cpp",
"KeyboardInputMapper_test.cpp",
"UinputDevice.cpp",
diff --git a/services/inputflinger/tests/CapturedTouchpadEventConverter_test.cpp b/services/inputflinger/tests/CapturedTouchpadEventConverter_test.cpp
index 99a6a1f..b738abf 100644
--- a/services/inputflinger/tests/CapturedTouchpadEventConverter_test.cpp
+++ b/services/inputflinger/tests/CapturedTouchpadEventConverter_test.cpp
@@ -29,8 +29,8 @@
#include "FakeInputReaderPolicy.h"
#include "InstrumentedInputReader.h"
#include "TestConstants.h"
+#include "TestEventMatchers.h"
#include "TestInputListener.h"
-#include "TestInputListenerMatchers.h"
namespace android {
diff --git a/services/inputflinger/tests/CursorInputMapper_test.cpp b/services/inputflinger/tests/CursorInputMapper_test.cpp
index e630915..b55c9cc 100644
--- a/services/inputflinger/tests/CursorInputMapper_test.cpp
+++ b/services/inputflinger/tests/CursorInputMapper_test.cpp
@@ -22,7 +22,7 @@
#include "FakePointerController.h"
#include "InputMapperTest.h"
#include "InterfaceMocks.h"
-#include "TestInputListenerMatchers.h"
+#include "TestEventMatchers.h"
#define TAG "CursorInputMapper_test"
diff --git a/services/inputflinger/tests/GestureConverter_test.cpp b/services/inputflinger/tests/GestureConverter_test.cpp
index 74ce359..d7dc800 100644
--- a/services/inputflinger/tests/GestureConverter_test.cpp
+++ b/services/inputflinger/tests/GestureConverter_test.cpp
@@ -27,8 +27,8 @@
#include "InstrumentedInputReader.h"
#include "NotifyArgs.h"
#include "TestConstants.h"
+#include "TestEventMatchers.h"
#include "TestInputListener.h"
-#include "TestInputListenerMatchers.h"
#include "include/gestures.h"
#include "ui/Rotation.h"
diff --git a/services/inputflinger/tests/InputDispatcher_test.cpp b/services/inputflinger/tests/InputDispatcher_test.cpp
index 7925a27..2f63b2a 100644
--- a/services/inputflinger/tests/InputDispatcher_test.cpp
+++ b/services/inputflinger/tests/InputDispatcher_test.cpp
@@ -16,7 +16,7 @@
#include "../dispatcher/InputDispatcher.h"
#include "../BlockingQueue.h"
-#include "TestInputListenerMatchers.h"
+#include "TestEventMatchers.h"
#include <NotifyArgsBuilders.h>
#include <android-base/properties.h>
@@ -136,16 +136,6 @@
using ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID;
-struct PointF {
- float x;
- float y;
- auto operator<=>(const PointF&) const = default;
-};
-
-inline std::string pointFToString(const PointF& p) {
- return std::string("(") + std::to_string(p.x) + ", " + std::to_string(p.y) + ")";
-}
-
/**
* Return a DOWN key event with KEYCODE_A.
*/
@@ -158,64 +148,6 @@
return event;
}
-MATCHER_P(WithDownTime, downTime, "InputEvent with specified downTime") {
- *result_listener << "expected downTime " << downTime << ", but got " << arg.getDownTime();
- return arg.getDownTime() == downTime;
-}
-
-MATCHER_P(WithSource, source, "InputEvent with specified source") {
- *result_listener << "expected source " << inputEventSourceToString(source) << ", but got "
- << inputEventSourceToString(arg.getSource());
- return arg.getSource() == source;
-}
-
-MATCHER_P(WithFlags, flags, "InputEvent with specified flags") {
- *result_listener << "expected flags " << std::hex << flags << ", but got " << arg.getFlags();
- return arg.getFlags() == flags;
-}
-
-MATCHER_P2(WithCoords, x, y, "MotionEvent with specified coordinates") {
- if (arg.getPointerCount() != 1) {
- *result_listener << "Expected 1 pointer, got " << arg.getPointerCount();
- return false;
- }
- const float receivedX = arg.getX(/*pointerIndex=*/0);
- const float receivedY = arg.getY(/*pointerIndex=*/0);
- *result_listener << "expected coords (" << x << ", " << y << "), but got (" << receivedX << ", "
- << receivedY << ")";
- return receivedX == x && receivedY == y;
-}
-
-MATCHER_P2(WithRawCoords, x, y, "MotionEvent with specified raw coordinates") {
- if (arg.getPointerCount() != 1) {
- *result_listener << "Expected 1 pointer, got " << arg.getPointerCount();
- return false;
- }
- const float receivedX = arg.getRawX(/*pointerIndex=*/0);
- const float receivedY = arg.getRawY(/*pointerIndex=*/0);
- *result_listener << "expected raw coords (" << x << ", " << y << "), but got (" << receivedX
- << ", " << receivedY << ")";
- return receivedX == x && receivedY == y;
-}
-
-MATCHER_P(WithPointerCount, pointerCount, "MotionEvent with specified number of pointers") {
- *result_listener << "expected pointerCount " << pointerCount << ", but got "
- << arg.getPointerCount();
- return arg.getPointerCount() == pointerCount;
-}
-
-MATCHER_P(WithPointers, pointers, "MotionEvent with specified pointers") {
- // Build a map for the received pointers, by pointer id
- std::map<int32_t /*pointerId*/, PointF> actualPointers;
- for (size_t pointerIndex = 0; pointerIndex < arg.getPointerCount(); pointerIndex++) {
- const int32_t pointerId = arg.getPointerId(pointerIndex);
- actualPointers[pointerId] = {arg.getX(pointerIndex), arg.getY(pointerIndex)};
- }
- *result_listener << "expected pointers " << dumpMap(pointers, constToString, pointFToString)
- << ", but got " << dumpMap(actualPointers, constToString, pointFToString);
- return pointers == actualPointers;
-}
-
// --- FakeInputDispatcherPolicy ---
class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface {
diff --git a/services/inputflinger/tests/InputReader_test.cpp b/services/inputflinger/tests/InputReader_test.cpp
index 6032e30..64ae9e8 100644
--- a/services/inputflinger/tests/InputReader_test.cpp
+++ b/services/inputflinger/tests/InputReader_test.cpp
@@ -31,8 +31,8 @@
#include <SensorInputMapper.h>
#include <SingleTouchInputMapper.h>
#include <SwitchInputMapper.h>
+#include <TestEventMatchers.h>
#include <TestInputListener.h>
-#include <TestInputListenerMatchers.h>
#include <TouchInputMapper.h>
#include <UinputDevice.h>
#include <VibratorInputMapper.h>
diff --git a/services/inputflinger/tests/TestEventMatchers.h b/services/inputflinger/tests/TestEventMatchers.h
new file mode 100644
index 0000000..ee6ff53
--- /dev/null
+++ b/services/inputflinger/tests/TestEventMatchers.h
@@ -0,0 +1,611 @@
+/*
+ * Copyright (C) 2022 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.
+ */
+
+#pragma once
+
+#include <cmath>
+#include <compare>
+
+#include <android-base/stringprintf.h>
+#include <android/input.h>
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+#include <input/Input.h>
+#include <input/PrintTools.h>
+
+#include "NotifyArgs.h"
+#include "TestConstants.h"
+
+namespace android {
+
+struct PointF {
+ float x;
+ float y;
+ auto operator<=>(const PointF&) const = default;
+};
+
+inline std::string pointFToString(const PointF& p) {
+ return std::string("(") + std::to_string(p.x) + ", " + std::to_string(p.y) + ")";
+}
+
+/// Source
+class WithSourceMatcher {
+public:
+ using is_gtest_matcher = void;
+ explicit WithSourceMatcher(uint32_t source) : mSource(source) {}
+
+ bool MatchAndExplain(const NotifyMotionArgs& args, std::ostream*) const {
+ return mSource == args.source;
+ }
+
+ bool MatchAndExplain(const NotifyKeyArgs& args, std::ostream*) const {
+ return mSource == args.source;
+ }
+
+ bool MatchAndExplain(const InputEvent& event, std::ostream*) const {
+ return mSource == event.getSource();
+ }
+
+ void DescribeTo(std::ostream* os) const {
+ *os << "with source " << inputEventSourceToString(mSource);
+ }
+
+ void DescribeNegationTo(std::ostream* os) const { *os << "wrong source"; }
+
+private:
+ const uint32_t mSource;
+};
+
+inline WithSourceMatcher WithSource(uint32_t source) {
+ return WithSourceMatcher(source);
+}
+
+/// Key action
+class WithKeyActionMatcher {
+public:
+ using is_gtest_matcher = void;
+ explicit WithKeyActionMatcher(int32_t action) : mAction(action) {}
+
+ bool MatchAndExplain(const NotifyKeyArgs& args, std::ostream*) const {
+ return mAction == args.action;
+ }
+
+ bool MatchAndExplain(const KeyEvent& event, std::ostream*) const {
+ return mAction == event.getAction();
+ }
+
+ void DescribeTo(std::ostream* os) const {
+ *os << "with key action " << KeyEvent::actionToString(mAction);
+ }
+
+ void DescribeNegationTo(std::ostream* os) const { *os << "wrong action"; }
+
+private:
+ const int32_t mAction;
+};
+
+inline WithKeyActionMatcher WithKeyAction(int32_t action) {
+ return WithKeyActionMatcher(action);
+}
+
+/// Motion action
+class WithMotionActionMatcher {
+public:
+ using is_gtest_matcher = void;
+ explicit WithMotionActionMatcher(int32_t action) : mAction(action) {}
+
+ bool MatchAndExplain(const NotifyMotionArgs& args, std::ostream*) const {
+ bool matches = mAction == args.action;
+ if (args.action == AMOTION_EVENT_ACTION_CANCEL) {
+ matches &= (args.flags & AMOTION_EVENT_FLAG_CANCELED) != 0;
+ }
+ return matches;
+ }
+
+ bool MatchAndExplain(const MotionEvent& event, std::ostream*) const {
+ bool matches = mAction == event.getAction();
+ if (event.getAction() == AMOTION_EVENT_ACTION_CANCEL) {
+ matches &= (event.getFlags() & AMOTION_EVENT_FLAG_CANCELED) != 0;
+ }
+ return matches;
+ }
+
+ void DescribeTo(std::ostream* os) const {
+ *os << "with motion action " << MotionEvent::actionToString(mAction);
+ if (mAction == AMOTION_EVENT_ACTION_CANCEL) {
+ *os << " and FLAG_CANCELED";
+ }
+ }
+
+ void DescribeNegationTo(std::ostream* os) const { *os << "wrong action"; }
+
+private:
+ const int32_t mAction;
+};
+
+inline WithMotionActionMatcher WithMotionAction(int32_t action) {
+ return WithMotionActionMatcher(action);
+}
+
+/// Display Id
+class WithDisplayIdMatcher {
+public:
+ using is_gtest_matcher = void;
+ explicit WithDisplayIdMatcher(int32_t displayId) : mDisplayId(displayId) {}
+
+ bool MatchAndExplain(const NotifyMotionArgs& args, std::ostream*) const {
+ return mDisplayId == args.displayId;
+ }
+
+ bool MatchAndExplain(const NotifyKeyArgs& args, std::ostream*) const {
+ return mDisplayId == args.displayId;
+ }
+
+ bool MatchAndExplain(const InputEvent& event, std::ostream*) const {
+ return mDisplayId == event.getDisplayId();
+ }
+
+ void DescribeTo(std::ostream* os) const { *os << "with display id " << mDisplayId; }
+
+ void DescribeNegationTo(std::ostream* os) const { *os << "wrong display id"; }
+
+private:
+ const int32_t mDisplayId;
+};
+
+inline WithDisplayIdMatcher WithDisplayId(int32_t displayId) {
+ return WithDisplayIdMatcher(displayId);
+}
+
+/// Device Id
+class WithDeviceIdMatcher {
+public:
+ using is_gtest_matcher = void;
+ explicit WithDeviceIdMatcher(int32_t deviceId) : mDeviceId(deviceId) {}
+
+ bool MatchAndExplain(const NotifyMotionArgs& args, std::ostream*) const {
+ return mDeviceId == args.deviceId;
+ }
+
+ bool MatchAndExplain(const NotifyKeyArgs& args, std::ostream*) const {
+ return mDeviceId == args.deviceId;
+ }
+
+ bool MatchAndExplain(const NotifyDeviceResetArgs& args, std::ostream*) const {
+ return mDeviceId == args.deviceId;
+ }
+
+ bool MatchAndExplain(const InputEvent& event, std::ostream*) const {
+ return mDeviceId == event.getDeviceId();
+ }
+
+ void DescribeTo(std::ostream* os) const { *os << "with device id " << mDeviceId; }
+
+ void DescribeNegationTo(std::ostream* os) const { *os << "wrong device id"; }
+
+private:
+ const int32_t mDeviceId;
+};
+
+inline WithDeviceIdMatcher WithDeviceId(int32_t deviceId) {
+ return WithDeviceIdMatcher(deviceId);
+}
+
+/// Flags
+class WithFlagsMatcher {
+public:
+ using is_gtest_matcher = void;
+ explicit WithFlagsMatcher(int32_t flags) : mFlags(flags) {}
+
+ bool MatchAndExplain(const NotifyMotionArgs& args, std::ostream*) const {
+ return mFlags == args.flags;
+ }
+
+ bool MatchAndExplain(const NotifyKeyArgs& args, std::ostream*) const {
+ return mFlags == args.flags;
+ }
+
+ bool MatchAndExplain(const MotionEvent& event, std::ostream*) const {
+ return mFlags == event.getFlags();
+ }
+
+ bool MatchAndExplain(const KeyEvent& event, std::ostream*) const {
+ return mFlags == event.getFlags();
+ }
+
+ void DescribeTo(std::ostream* os) const {
+ *os << "with flags " << base::StringPrintf("0x%x", mFlags);
+ }
+
+ void DescribeNegationTo(std::ostream* os) const { *os << "wrong flags"; }
+
+private:
+ const int32_t mFlags;
+};
+
+inline WithFlagsMatcher WithFlags(int32_t flags) {
+ return WithFlagsMatcher(flags);
+}
+
+/// DownTime
+class WithDownTimeMatcher {
+public:
+ using is_gtest_matcher = void;
+ explicit WithDownTimeMatcher(nsecs_t downTime) : mDownTime(downTime) {}
+
+ bool MatchAndExplain(const NotifyMotionArgs& args, std::ostream*) const {
+ return mDownTime == args.downTime;
+ }
+
+ bool MatchAndExplain(const NotifyKeyArgs& args, std::ostream*) const {
+ return mDownTime == args.downTime;
+ }
+
+ bool MatchAndExplain(const MotionEvent& event, std::ostream*) const {
+ return mDownTime == event.getDownTime();
+ }
+
+ bool MatchAndExplain(const KeyEvent& event, std::ostream*) const {
+ return mDownTime == event.getDownTime();
+ }
+
+ void DescribeTo(std::ostream* os) const { *os << "with down time " << mDownTime; }
+
+ void DescribeNegationTo(std::ostream* os) const { *os << "wrong down time"; }
+
+private:
+ const nsecs_t mDownTime;
+};
+
+inline WithDownTimeMatcher WithDownTime(nsecs_t downTime) {
+ return WithDownTimeMatcher(downTime);
+}
+
+/// Coordinate matcher
+class WithCoordsMatcher {
+public:
+ using is_gtest_matcher = void;
+ explicit WithCoordsMatcher(size_t pointerIndex, float x, float y)
+ : mPointerIndex(pointerIndex), mX(x), mY(y) {}
+
+ bool MatchAndExplain(const MotionEvent& event, std::ostream* os) const {
+ if (mPointerIndex >= event.getPointerCount()) {
+ *os << "Pointer index " << mPointerIndex << " is out of bounds";
+ return false;
+ }
+
+ bool matches = mX == event.getX(mPointerIndex) && mY == event.getY(mPointerIndex);
+ if (!matches) {
+ *os << "expected coords (" << mX << ", " << mY << ") at pointer index " << mPointerIndex
+ << ", but got (" << event.getX(mPointerIndex) << ", " << event.getY(mPointerIndex)
+ << ")";
+ }
+ return matches;
+ }
+
+ bool MatchAndExplain(const NotifyMotionArgs& event, std::ostream* os) const {
+ if (mPointerIndex >= event.pointerCoords.size()) {
+ *os << "Pointer index " << mPointerIndex << " is out of bounds";
+ return false;
+ }
+
+ bool matches = mX == event.pointerCoords[mPointerIndex].getX() &&
+ mY == event.pointerCoords[mPointerIndex].getY();
+ if (!matches) {
+ *os << "expected coords (" << mX << ", " << mY << ") at pointer index " << mPointerIndex
+ << ", but got (" << event.pointerCoords[mPointerIndex].getX() << ", "
+ << event.pointerCoords[mPointerIndex].getY() << ")";
+ }
+ return matches;
+ }
+
+ void DescribeTo(std::ostream* os) const {
+ *os << "with coords (" << mX << ", " << mY << ") at pointer index " << mPointerIndex;
+ }
+
+ void DescribeNegationTo(std::ostream* os) const { *os << "wrong coords"; }
+
+private:
+ const size_t mPointerIndex;
+ const float mX;
+ const float mY;
+};
+
+inline WithCoordsMatcher WithCoords(float x, float y) {
+ return WithCoordsMatcher(0, x, y);
+}
+
+inline WithCoordsMatcher WithPointerCoords(size_t pointerIndex, float x, float y) {
+ return WithCoordsMatcher(pointerIndex, x, y);
+}
+
+/// Raw coordinate matcher
+class WithRawCoordsMatcher {
+public:
+ using is_gtest_matcher = void;
+ explicit WithRawCoordsMatcher(size_t pointerIndex, float rawX, float rawY)
+ : mPointerIndex(pointerIndex), mRawX(rawX), mRawY(rawY) {}
+
+ bool MatchAndExplain(const MotionEvent& event, std::ostream* os) const {
+ if (mPointerIndex >= event.getPointerCount()) {
+ *os << "Pointer index " << mPointerIndex << " is out of bounds";
+ return false;
+ }
+
+ bool matches =
+ mRawX == event.getRawX(mPointerIndex) && mRawY == event.getRawY(mPointerIndex);
+ if (!matches) {
+ *os << "expected raw coords (" << mRawX << ", " << mRawY << ") at pointer index "
+ << mPointerIndex << ", but got (" << event.getRawX(mPointerIndex) << ", "
+ << event.getRawY(mPointerIndex) << ")";
+ }
+ return matches;
+ }
+
+ void DescribeTo(std::ostream* os) const {
+ *os << "with raw coords (" << mRawX << ", " << mRawY << ") at pointer index "
+ << mPointerIndex;
+ }
+
+ void DescribeNegationTo(std::ostream* os) const { *os << "wrong raw coords"; }
+
+private:
+ const size_t mPointerIndex;
+ const float mRawX;
+ const float mRawY;
+};
+
+inline WithRawCoordsMatcher WithRawCoords(float rawX, float rawY) {
+ return WithRawCoordsMatcher(0, rawX, rawY);
+}
+
+inline WithRawCoordsMatcher WithPointerRawCoords(size_t pointerIndex, float rawX, float rawY) {
+ return WithRawCoordsMatcher(pointerIndex, rawX, rawY);
+}
+
+/// Pointer count
+class WithPointerCountMatcher {
+public:
+ using is_gtest_matcher = void;
+ explicit WithPointerCountMatcher(size_t pointerCount) : mPointerCount(pointerCount) {}
+
+ bool MatchAndExplain(const MotionEvent& event, std::ostream* os) const {
+ if (event.getPointerCount() != mPointerCount) {
+ *os << "expected pointer count " << mPointerCount << ", but got "
+ << event.getPointerCount();
+ return false;
+ }
+ return true;
+ }
+
+ bool MatchAndExplain(const NotifyMotionArgs& event, std::ostream* os) const {
+ if (event.pointerCoords.size() != mPointerCount) {
+ *os << "expected pointer count " << mPointerCount << ", but got "
+ << event.pointerCoords.size();
+ return false;
+ }
+ return true;
+ }
+
+ void DescribeTo(std::ostream* os) const { *os << "with pointer count " << mPointerCount; }
+
+ void DescribeNegationTo(std::ostream* os) const { *os << "wrong pointer count"; }
+
+private:
+ const size_t mPointerCount;
+};
+
+inline WithPointerCountMatcher WithPointerCount(size_t pointerCount) {
+ return WithPointerCountMatcher(pointerCount);
+}
+
+/// Pointers matcher
+class WithPointersMatcher {
+public:
+ using is_gtest_matcher = void;
+ explicit WithPointersMatcher(std::map<int32_t, PointF> pointers) : mPointers(pointers) {}
+
+ bool MatchAndExplain(const MotionEvent& event, std::ostream* os) const {
+ std::map<int32_t, PointF> actualPointers;
+ for (size_t pointerIndex = 0; pointerIndex < event.getPointerCount(); pointerIndex++) {
+ const int32_t pointerId = event.getPointerId(pointerIndex);
+ actualPointers[pointerId] = {event.getX(pointerIndex), event.getY(pointerIndex)};
+ }
+
+ if (mPointers != actualPointers) {
+ *os << "expected pointers " << dumpMap(mPointers, constToString, pointFToString)
+ << ", but got " << dumpMap(actualPointers, constToString, pointFToString);
+ return false;
+ }
+ return true;
+ }
+
+ bool MatchAndExplain(const NotifyMotionArgs& event, std::ostream* os) const {
+ std::map<int32_t, PointF> actualPointers;
+ for (size_t pointerIndex = 0; pointerIndex < event.pointerCoords.size(); pointerIndex++) {
+ const int32_t pointerId = event.pointerProperties[pointerIndex].id;
+ actualPointers[pointerId] = {event.pointerCoords[pointerIndex].getX(),
+ event.pointerCoords[pointerIndex].getY()};
+ }
+
+ if (mPointers != actualPointers) {
+ *os << "expected pointers " << dumpMap(mPointers, constToString, pointFToString)
+ << ", but got " << dumpMap(actualPointers, constToString, pointFToString);
+ return false;
+ }
+ return true;
+ }
+
+ void DescribeTo(std::ostream* os) const {
+ *os << "with pointers " << dumpMap(mPointers, constToString, pointFToString);
+ }
+
+ void DescribeNegationTo(std::ostream* os) const { *os << "wrong pointers"; }
+
+private:
+ const std::map<int32_t, PointF> mPointers;
+};
+
+inline WithPointersMatcher WithPointers(
+ const std::map<int32_t /*id*/, PointF /*coords*/>& pointers) {
+ return WithPointersMatcher(pointers);
+}
+
+MATCHER_P(WithKeyCode, keyCode, "KeyEvent with specified key code") {
+ *result_listener << "expected key code " << keyCode << ", but got " << arg.keyCode;
+ return arg.keyCode == keyCode;
+}
+
+MATCHER_P(WithRepeatCount, repeatCount, "KeyEvent with specified repeat count") {
+ return arg.getRepeatCount() == repeatCount;
+}
+
+MATCHER_P2(WithPointerId, index, id, "MotionEvent with specified pointer ID for pointer index") {
+ const auto argPointerId = arg.pointerProperties[index].id;
+ *result_listener << "expected pointer with index " << index << " to have ID " << argPointerId;
+ return argPointerId == id;
+}
+
+MATCHER_P2(WithCursorPosition, x, y, "InputEvent with specified cursor position") {
+ const auto argX = arg.xCursorPosition;
+ const auto argY = arg.yCursorPosition;
+ *result_listener << "expected cursor position (" << x << ", " << y << "), but got (" << argX
+ << ", " << argY << ")";
+ return (isnan(x) ? isnan(argX) : x == argX) && (isnan(y) ? isnan(argY) : y == argY);
+}
+
+MATCHER_P2(WithRelativeMotion, x, y, "InputEvent with specified relative motion") {
+ const auto argX = arg.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
+ const auto argY = arg.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
+ *result_listener << "expected relative motion (" << x << ", " << y << "), but got (" << argX
+ << ", " << argY << ")";
+ return argX == x && argY == y;
+}
+
+MATCHER_P3(WithGestureOffset, dx, dy, epsilon,
+ "InputEvent with specified touchpad gesture offset") {
+ const auto argGestureX = arg.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET);
+ const auto argGestureY = arg.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET);
+ const double xDiff = fabs(argGestureX - dx);
+ const double yDiff = fabs(argGestureY - dy);
+ *result_listener << "expected gesture offset (" << dx << ", " << dy << ") within " << epsilon
+ << ", but got (" << argGestureX << ", " << argGestureY << ")";
+ return xDiff <= epsilon && yDiff <= epsilon;
+}
+
+MATCHER_P3(WithGestureScrollDistance, x, y, epsilon,
+ "InputEvent with specified touchpad gesture scroll distance") {
+ const auto argXDistance =
+ arg.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_SCROLL_X_DISTANCE);
+ const auto argYDistance =
+ arg.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_SCROLL_Y_DISTANCE);
+ const double xDiff = fabs(argXDistance - x);
+ const double yDiff = fabs(argYDistance - y);
+ *result_listener << "expected gesture offset (" << x << ", " << y << ") within " << epsilon
+ << ", but got (" << argXDistance << ", " << argYDistance << ")";
+ return xDiff <= epsilon && yDiff <= epsilon;
+}
+
+MATCHER_P2(WithGesturePinchScaleFactor, factor, epsilon,
+ "InputEvent with specified touchpad pinch gesture scale factor") {
+ const auto argScaleFactor =
+ arg.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_PINCH_SCALE_FACTOR);
+ *result_listener << "expected gesture scale factor " << factor << " within " << epsilon
+ << " but got " << argScaleFactor;
+ return fabs(argScaleFactor - factor) <= epsilon;
+}
+
+MATCHER_P(WithGestureSwipeFingerCount, count,
+ "InputEvent with specified touchpad swipe finger count") {
+ const auto argFingerCount =
+ arg.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_SWIPE_FINGER_COUNT);
+ *result_listener << "expected gesture swipe finger count " << count << " but got "
+ << argFingerCount;
+ return fabs(argFingerCount - count) <= EPSILON;
+}
+
+MATCHER_P(WithPressure, pressure, "InputEvent with specified pressure") {
+ const auto argPressure = arg.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE);
+ *result_listener << "expected pressure " << pressure << ", but got " << argPressure;
+ return argPressure == pressure;
+}
+
+MATCHER_P2(WithTouchDimensions, maj, min, "InputEvent with specified touch dimensions") {
+ const auto argMajor = arg.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR);
+ const auto argMinor = arg.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR);
+ *result_listener << "expected touch dimensions " << maj << " major x " << min
+ << " minor, but got " << argMajor << " major x " << argMinor << " minor";
+ return argMajor == maj && argMinor == min;
+}
+
+MATCHER_P2(WithToolDimensions, maj, min, "InputEvent with specified tool dimensions") {
+ const auto argMajor = arg.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR);
+ const auto argMinor = arg.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR);
+ *result_listener << "expected tool dimensions " << maj << " major x " << min
+ << " minor, but got " << argMajor << " major x " << argMinor << " minor";
+ return argMajor == maj && argMinor == min;
+}
+
+MATCHER_P(WithToolType, toolType, "InputEvent with specified tool type") {
+ const auto argToolType = arg.pointerProperties[0].toolType;
+ *result_listener << "expected tool type " << ftl::enum_string(toolType) << ", but got "
+ << ftl::enum_string(argToolType);
+ return argToolType == toolType;
+}
+
+MATCHER_P2(WithPointerToolType, pointer, toolType,
+ "InputEvent with specified tool type for pointer") {
+ const auto argToolType = arg.pointerProperties[pointer].toolType;
+ *result_listener << "expected pointer " << pointer << " to have tool type "
+ << ftl::enum_string(toolType) << ", but got " << ftl::enum_string(argToolType);
+ return argToolType == toolType;
+}
+
+MATCHER_P(WithMotionClassification, classification,
+ "InputEvent with specified MotionClassification") {
+ *result_listener << "expected classification " << motionClassificationToString(classification)
+ << ", but got " << motionClassificationToString(arg.classification);
+ return arg.classification == classification;
+}
+
+MATCHER_P(WithButtonState, buttons, "InputEvent with specified button state") {
+ *result_listener << "expected button state " << buttons << ", but got " << arg.buttonState;
+ return arg.buttonState == buttons;
+}
+
+MATCHER_P(WithActionButton, actionButton, "InputEvent with specified action button") {
+ *result_listener << "expected action button " << actionButton << ", but got "
+ << arg.actionButton;
+ return arg.actionButton == actionButton;
+}
+
+MATCHER_P(WithEventTime, eventTime, "InputEvent with specified eventTime") {
+ *result_listener << "expected event time " << eventTime << ", but got " << arg.eventTime;
+ return arg.eventTime == eventTime;
+}
+
+MATCHER_P(WithDownTime, downTime, "InputEvent with specified downTime") {
+ *result_listener << "expected down time " << downTime << ", but got " << arg.downTime;
+ return arg.downTime == downTime;
+}
+
+MATCHER_P2(WithPrecision, xPrecision, yPrecision, "MotionEvent with specified precision") {
+ *result_listener << "expected x-precision " << xPrecision << " and y-precision " << yPrecision
+ << ", but got " << arg.xPrecision << " and " << arg.yPrecision;
+ return arg.xPrecision == xPrecision && arg.yPrecision == yPrecision;
+}
+
+} // namespace android
diff --git a/services/inputflinger/tests/TestInputListenerMatchers.cpp b/services/inputflinger/tests/TestInputListenerMatchers.cpp
deleted file mode 100644
index 1464e60..0000000
--- a/services/inputflinger/tests/TestInputListenerMatchers.cpp
+++ /dev/null
@@ -1,37 +0,0 @@
-/*
- * Copyright 2023 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 "TestInputListenerMatchers.h"
-
-namespace android {
-
-WithKeyActionMatcher WithKeyAction(int32_t action) {
- return WithKeyActionMatcher(action);
-}
-
-WithMotionActionMatcher WithMotionAction(int32_t action) {
- return WithMotionActionMatcher(action);
-}
-
-WithDisplayIdMatcher WithDisplayId(int32_t displayId) {
- return WithDisplayIdMatcher(displayId);
-}
-
-WithDeviceIdMatcher WithDeviceId(int32_t deviceId) {
- return WithDeviceIdMatcher(deviceId);
-}
-
-} // namespace android
diff --git a/services/inputflinger/tests/TestInputListenerMatchers.h b/services/inputflinger/tests/TestInputListenerMatchers.h
deleted file mode 100644
index 31ad569..0000000
--- a/services/inputflinger/tests/TestInputListenerMatchers.h
+++ /dev/null
@@ -1,330 +0,0 @@
-/*
- * Copyright (C) 2022 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.
- */
-
-#pragma once
-
-#include <cmath>
-
-#include <android/input.h>
-#include <gmock/gmock.h>
-#include <gtest/gtest.h>
-#include <input/Input.h>
-
-#include "NotifyArgs.h"
-#include "TestConstants.h"
-
-namespace android {
-
-MATCHER_P(WithSource, source, "InputEvent with specified source") {
- *result_listener << "expected source " << inputEventSourceToString(source) << ", but got "
- << inputEventSourceToString(arg.source);
- return arg.source == source;
-}
-
-/// Key action
-class WithKeyActionMatcher {
-public:
- using is_gtest_matcher = void;
- explicit WithKeyActionMatcher(int32_t action) : mAction(action) {}
-
- bool MatchAndExplain(const NotifyKeyArgs& args, std::ostream*) const {
- return mAction == args.action;
- }
-
- bool MatchAndExplain(const KeyEvent& event, std::ostream*) const {
- return mAction == event.getAction();
- }
-
- void DescribeTo(std::ostream* os) const {
- *os << "with key action " << KeyEvent::actionToString(mAction);
- }
-
- void DescribeNegationTo(std::ostream* os) const { *os << "wrong action"; }
-
-private:
- const int32_t mAction;
-};
-
-WithKeyActionMatcher WithKeyAction(int32_t action);
-
-/// Motion action
-class WithMotionActionMatcher {
-public:
- using is_gtest_matcher = void;
- explicit WithMotionActionMatcher(int32_t action) : mAction(action) {}
-
- bool MatchAndExplain(const NotifyMotionArgs& args, std::ostream*) const {
- bool matches = mAction == args.action;
- if (args.action == AMOTION_EVENT_ACTION_CANCEL) {
- matches &= (args.flags & AMOTION_EVENT_FLAG_CANCELED) != 0;
- }
- return matches;
- }
-
- bool MatchAndExplain(const MotionEvent& event, std::ostream*) const {
- bool matches = mAction == event.getAction();
- if (event.getAction() == AMOTION_EVENT_ACTION_CANCEL) {
- matches &= (event.getFlags() & AMOTION_EVENT_FLAG_CANCELED) != 0;
- }
- return matches;
- }
-
- void DescribeTo(std::ostream* os) const {
- *os << "with motion action " << MotionEvent::actionToString(mAction);
- if (mAction == AMOTION_EVENT_ACTION_CANCEL) {
- *os << " and FLAG_CANCELED";
- }
- }
-
- void DescribeNegationTo(std::ostream* os) const { *os << "wrong action"; }
-
-private:
- const int32_t mAction;
-};
-
-WithMotionActionMatcher WithMotionAction(int32_t action);
-
-/// Display Id
-class WithDisplayIdMatcher {
-public:
- using is_gtest_matcher = void;
- explicit WithDisplayIdMatcher(int32_t displayId) : mDisplayId(displayId) {}
-
- bool MatchAndExplain(const NotifyMotionArgs& args, std::ostream*) const {
- return mDisplayId == args.displayId;
- }
-
- bool MatchAndExplain(const NotifyKeyArgs& args, std::ostream*) const {
- return mDisplayId == args.displayId;
- }
-
- bool MatchAndExplain(const InputEvent& event, std::ostream*) const {
- return mDisplayId == event.getDisplayId();
- }
-
- void DescribeTo(std::ostream* os) const { *os << "with display id " << mDisplayId; }
-
- void DescribeNegationTo(std::ostream* os) const { *os << "wrong display id"; }
-
-private:
- const int32_t mDisplayId;
-};
-
-WithDisplayIdMatcher WithDisplayId(int32_t displayId);
-
-/// Device Id
-class WithDeviceIdMatcher {
-public:
- using is_gtest_matcher = void;
- explicit WithDeviceIdMatcher(int32_t deviceId) : mDeviceId(deviceId) {}
-
- bool MatchAndExplain(const NotifyMotionArgs& args, std::ostream*) const {
- return mDeviceId == args.deviceId;
- }
-
- bool MatchAndExplain(const NotifyKeyArgs& args, std::ostream*) const {
- return mDeviceId == args.deviceId;
- }
-
- bool MatchAndExplain(const NotifyDeviceResetArgs& args, std::ostream*) const {
- return mDeviceId == args.deviceId;
- }
-
- bool MatchAndExplain(const InputEvent& event, std::ostream*) const {
- return mDeviceId == event.getDeviceId();
- }
-
- void DescribeTo(std::ostream* os) const { *os << "with device id " << mDeviceId; }
-
- void DescribeNegationTo(std::ostream* os) const { *os << "wrong device id"; }
-
-private:
- const int32_t mDeviceId;
-};
-
-WithDeviceIdMatcher WithDeviceId(int32_t deviceId);
-
-MATCHER_P(WithKeyCode, keyCode, "KeyEvent with specified key code") {
- *result_listener << "expected key code " << keyCode << ", but got " << arg.keyCode;
- return arg.keyCode == keyCode;
-}
-
-MATCHER_P(WithRepeatCount, repeatCount, "KeyEvent with specified repeat count") {
- return arg.getRepeatCount() == repeatCount;
-}
-
-MATCHER_P(WithPointerCount, count, "MotionEvent with specified number of pointers") {
- *result_listener << "expected " << count << " pointer(s), but got " << arg.getPointerCount();
- return arg.getPointerCount() == count;
-}
-
-MATCHER_P2(WithPointerId, index, id, "MotionEvent with specified pointer ID for pointer index") {
- const auto argPointerId = arg.pointerProperties[index].id;
- *result_listener << "expected pointer with index " << index << " to have ID " << argPointerId;
- return argPointerId == id;
-}
-
-MATCHER_P2(WithCoords, x, y, "InputEvent with specified coords") {
- const auto argX = arg.pointerCoords[0].getX();
- const auto argY = arg.pointerCoords[0].getY();
- *result_listener << "expected coords (" << x << ", " << y << "), but got (" << argX << ", "
- << argY << ")";
- return argX == x && argY == y;
-}
-
-MATCHER_P2(WithCursorPosition, x, y, "InputEvent with specified cursor position") {
- const auto argX = arg.xCursorPosition;
- const auto argY = arg.yCursorPosition;
- *result_listener << "expected cursor position (" << x << ", " << y << "), but got (" << argX
- << ", " << argY << ")";
- return (isnan(x) ? isnan(argX) : x == argX) && (isnan(y) ? isnan(argY) : y == argY);
-}
-
-MATCHER_P3(WithPointerCoords, pointer, x, y, "InputEvent with specified coords for pointer") {
- const auto argX = arg.pointerCoords[pointer].getX();
- const auto argY = arg.pointerCoords[pointer].getY();
- *result_listener << "expected pointer " << pointer << " to have coords (" << x << ", " << y
- << "), but got (" << argX << ", " << argY << ")";
- return argX == x && argY == y;
-}
-
-MATCHER_P2(WithRelativeMotion, x, y, "InputEvent with specified relative motion") {
- const auto argX = arg.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
- const auto argY = arg.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
- *result_listener << "expected relative motion (" << x << ", " << y << "), but got (" << argX
- << ", " << argY << ")";
- return argX == x && argY == y;
-}
-
-MATCHER_P3(WithGestureOffset, dx, dy, epsilon,
- "InputEvent with specified touchpad gesture offset") {
- const auto argGestureX = arg.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET);
- const auto argGestureY = arg.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET);
- const double xDiff = fabs(argGestureX - dx);
- const double yDiff = fabs(argGestureY - dy);
- *result_listener << "expected gesture offset (" << dx << ", " << dy << ") within " << epsilon
- << ", but got (" << argGestureX << ", " << argGestureY << ")";
- return xDiff <= epsilon && yDiff <= epsilon;
-}
-
-MATCHER_P3(WithGestureScrollDistance, x, y, epsilon,
- "InputEvent with specified touchpad gesture scroll distance") {
- const auto argXDistance =
- arg.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_SCROLL_X_DISTANCE);
- const auto argYDistance =
- arg.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_SCROLL_Y_DISTANCE);
- const double xDiff = fabs(argXDistance - x);
- const double yDiff = fabs(argYDistance - y);
- *result_listener << "expected gesture offset (" << x << ", " << y << ") within " << epsilon
- << ", but got (" << argXDistance << ", " << argYDistance << ")";
- return xDiff <= epsilon && yDiff <= epsilon;
-}
-
-MATCHER_P2(WithGesturePinchScaleFactor, factor, epsilon,
- "InputEvent with specified touchpad pinch gesture scale factor") {
- const auto argScaleFactor =
- arg.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_PINCH_SCALE_FACTOR);
- *result_listener << "expected gesture scale factor " << factor << " within " << epsilon
- << " but got " << argScaleFactor;
- return fabs(argScaleFactor - factor) <= epsilon;
-}
-
-MATCHER_P(WithGestureSwipeFingerCount, count,
- "InputEvent with specified touchpad swipe finger count") {
- const auto argFingerCount =
- arg.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_SWIPE_FINGER_COUNT);
- *result_listener << "expected gesture swipe finger count " << count << " but got "
- << argFingerCount;
- return fabs(argFingerCount - count) <= EPSILON;
-}
-
-MATCHER_P(WithPressure, pressure, "InputEvent with specified pressure") {
- const auto argPressure = arg.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE);
- *result_listener << "expected pressure " << pressure << ", but got " << argPressure;
- return argPressure == pressure;
-}
-
-MATCHER_P2(WithTouchDimensions, maj, min, "InputEvent with specified touch dimensions") {
- const auto argMajor = arg.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR);
- const auto argMinor = arg.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR);
- *result_listener << "expected touch dimensions " << maj << " major x " << min
- << " minor, but got " << argMajor << " major x " << argMinor << " minor";
- return argMajor == maj && argMinor == min;
-}
-
-MATCHER_P2(WithToolDimensions, maj, min, "InputEvent with specified tool dimensions") {
- const auto argMajor = arg.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR);
- const auto argMinor = arg.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR);
- *result_listener << "expected tool dimensions " << maj << " major x " << min
- << " minor, but got " << argMajor << " major x " << argMinor << " minor";
- return argMajor == maj && argMinor == min;
-}
-
-MATCHER_P(WithToolType, toolType, "InputEvent with specified tool type") {
- const auto argToolType = arg.pointerProperties[0].toolType;
- *result_listener << "expected tool type " << ftl::enum_string(toolType) << ", but got "
- << ftl::enum_string(argToolType);
- return argToolType == toolType;
-}
-
-MATCHER_P2(WithPointerToolType, pointer, toolType,
- "InputEvent with specified tool type for pointer") {
- const auto argToolType = arg.pointerProperties[pointer].toolType;
- *result_listener << "expected pointer " << pointer << " to have tool type "
- << ftl::enum_string(toolType) << ", but got " << ftl::enum_string(argToolType);
- return argToolType == toolType;
-}
-
-MATCHER_P(WithFlags, flags, "InputEvent with specified flags") {
- *result_listener << "expected flags " << flags << ", but got " << arg.flags;
- return arg.flags == static_cast<int32_t>(flags);
-}
-
-MATCHER_P(WithMotionClassification, classification,
- "InputEvent with specified MotionClassification") {
- *result_listener << "expected classification " << motionClassificationToString(classification)
- << ", but got " << motionClassificationToString(arg.classification);
- return arg.classification == classification;
-}
-
-MATCHER_P(WithButtonState, buttons, "InputEvent with specified button state") {
- *result_listener << "expected button state " << buttons << ", but got " << arg.buttonState;
- return arg.buttonState == buttons;
-}
-
-MATCHER_P(WithActionButton, actionButton, "InputEvent with specified action button") {
- *result_listener << "expected action button " << actionButton << ", but got "
- << arg.actionButton;
- return arg.actionButton == actionButton;
-}
-
-MATCHER_P(WithEventTime, eventTime, "InputEvent with specified eventTime") {
- *result_listener << "expected event time " << eventTime << ", but got " << arg.eventTime;
- return arg.eventTime == eventTime;
-}
-
-MATCHER_P(WithDownTime, downTime, "InputEvent with specified downTime") {
- *result_listener << "expected down time " << downTime << ", but got " << arg.downTime;
- return arg.downTime == downTime;
-}
-
-MATCHER_P2(WithPrecision, xPrecision, yPrecision, "MotionEvent with specified precision") {
- *result_listener << "expected x-precision " << xPrecision << " and y-precision " << yPrecision
- << ", but got " << arg.xPrecision << " and " << arg.yPrecision;
- return arg.xPrecision == xPrecision && arg.yPrecision == yPrecision;
-}
-
-} // namespace android
diff --git a/services/inputflinger/tests/TouchpadInputMapper_test.cpp b/services/inputflinger/tests/TouchpadInputMapper_test.cpp
index 02abf9f..6203a1d 100644
--- a/services/inputflinger/tests/TouchpadInputMapper_test.cpp
+++ b/services/inputflinger/tests/TouchpadInputMapper_test.cpp
@@ -23,7 +23,7 @@
#include "FakePointerController.h"
#include "InputMapperTest.h"
#include "InterfaceMocks.h"
-#include "TestInputListenerMatchers.h"
+#include "TestEventMatchers.h"
#define TAG "TouchpadInputMapper_test"
diff --git a/services/inputflinger/tests/UnwantedInteractionBlocker_test.cpp b/services/inputflinger/tests/UnwantedInteractionBlocker_test.cpp
index 7cfcf71..78f7291 100644
--- a/services/inputflinger/tests/UnwantedInteractionBlocker_test.cpp
+++ b/services/inputflinger/tests/UnwantedInteractionBlocker_test.cpp
@@ -23,8 +23,8 @@
#include <thread>
#include "ui/events/ozone/evdev/touch_filter/neural_stylus_palm_detection_filter.h"
+#include "TestEventMatchers.h"
#include "TestInputListener.h"
-#include "TestInputListenerMatchers.h"
using ::testing::AllOf;