blob: dd2e40c02506e37a9edb4a1b66c7b18d61405baf [file] [log] [blame]
Paul Ramirez00cf5d02024-09-05 17:10:12 +00001/**
2 * Copyright 2024 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
19#include <ostream>
20
21#include <input/Input.h>
22
23namespace android {
24
25/**
26 * This file contains a copy of Matchers from .../inputflinger/tests/TestEventMatchers.h. Ideally,
27 * implementations must not be duplicated.
28 * TODO(b/365606513): Find a way to share TestEventMatchers.h between inputflinger and libinput.
29 */
30
31class WithDeviceIdMatcher {
32public:
33 using is_gtest_matcher = void;
34 explicit WithDeviceIdMatcher(DeviceId deviceId) : mDeviceId(deviceId) {}
35
36 bool MatchAndExplain(const InputEvent& event, std::ostream*) const {
37 return mDeviceId == event.getDeviceId();
38 }
39
40 void DescribeTo(std::ostream* os) const { *os << "with device id " << mDeviceId; }
41
42 void DescribeNegationTo(std::ostream* os) const { *os << "wrong device id"; }
43
44private:
45 const DeviceId mDeviceId;
46};
47
48inline WithDeviceIdMatcher WithDeviceId(int32_t deviceId) {
49 return WithDeviceIdMatcher(deviceId);
50}
51
52class WithMotionActionMatcher {
53public:
54 using is_gtest_matcher = void;
55 explicit WithMotionActionMatcher(int32_t action) : mAction(action) {}
56
57 bool MatchAndExplain(const MotionEvent& event, std::ostream*) const {
58 bool matches = mAction == event.getAction();
59 if (event.getAction() == AMOTION_EVENT_ACTION_CANCEL) {
60 matches &= (event.getFlags() & AMOTION_EVENT_FLAG_CANCELED) != 0;
61 }
62 return matches;
63 }
64
65 void DescribeTo(std::ostream* os) const {
66 *os << "with motion action " << MotionEvent::actionToString(mAction);
67 if (mAction == AMOTION_EVENT_ACTION_CANCEL) {
68 *os << " and FLAG_CANCELED";
69 }
70 }
71
72 void DescribeNegationTo(std::ostream* os) const { *os << "wrong action"; }
73
74private:
75 const int32_t mAction;
76};
77
78inline WithMotionActionMatcher WithMotionAction(int32_t action) {
79 return WithMotionActionMatcher(action);
80}
81
82class MotionEventIsResampledMatcher {
83public:
84 using is_gtest_matcher = void;
85
86 bool MatchAndExplain(const MotionEvent& motionEvent, std::ostream*) const {
87 const size_t numSamples = motionEvent.getHistorySize() + 1;
88 const size_t numPointers = motionEvent.getPointerCount();
89 if (numPointers <= 0 || numSamples <= 0) {
90 return false;
91 }
92 for (size_t i = 0; i < numPointers; ++i) {
93 const PointerCoords& pointerCoords =
94 motionEvent.getSamplePointerCoords()[numSamples * numPointers + i];
95 if (!pointerCoords.isResampled) {
96 return false;
97 }
98 }
99 return true;
100 }
101
102 void DescribeTo(std::ostream* os) const { *os << "MotionEvent is resampled."; }
103
104 void DescribeNegationTo(std::ostream* os) const { *os << "MotionEvent is not resampled."; }
105};
106
107inline MotionEventIsResampledMatcher MotionEventIsResampled() {
108 return MotionEventIsResampledMatcher();
109}
110} // namespace android