1 /*
2 * Copyright (C) 2019 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 #include "../InputProcessor.h"
18 #include <gtest/gtest.h>
19
20 #include "TestInputListener.h"
21
22 #include <aidl/android/hardware/input/processor/BnInputProcessor.h>
23 #include <aidl/android/hardware/input/processor/IInputProcessor.h>
24 #include <android/binder_manager.h>
25 #include <android/binder_process.h>
26
27 using namespace aidl::android::hardware::input;
28 using aidl::android::hardware::input::common::Classification;
29 using aidl::android::hardware::input::processor::IInputProcessor;
30
31 namespace android {
32
33 // --- InputProcessorTest ---
34
generateBasicMotionArgs()35 static NotifyMotionArgs generateBasicMotionArgs() {
36 // Create a basic motion event for testing
37 PointerProperties properties;
38 properties.id = 0;
39 properties.toolType = ToolType::FINGER;
40
41 PointerCoords coords;
42 coords.clear();
43 coords.setAxisValue(AMOTION_EVENT_AXIS_X, 1);
44 coords.setAxisValue(AMOTION_EVENT_AXIS_Y, 1);
45 static constexpr nsecs_t downTime = 2;
46 NotifyMotionArgs motionArgs(/*sequenceNum=*/1, /*eventTime=*/downTime, /*readTime=*/2,
47 /*deviceId=*/3, AINPUT_SOURCE_ANY, ui::LogicalDisplayId::DEFAULT,
48 /*policyFlags=*/4, AMOTION_EVENT_ACTION_DOWN, /*actionButton=*/0,
49 /*flags=*/0, AMETA_NONE, /*buttonState=*/0,
50 MotionClassification::NONE, AMOTION_EVENT_EDGE_FLAG_NONE,
51 /*pointerCount=*/1, &properties, &coords, /*xPrecision=*/0,
52 /*yPrecision=*/0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
53 AMOTION_EVENT_INVALID_CURSOR_POSITION, downTime,
54 /*videoFrames=*/{});
55 return motionArgs;
56 }
57
58 class InputProcessorTest : public testing::Test {
59 protected:
60 TestInputListener mTestListener;
61 std::unique_ptr<InputProcessorInterface> mProcessor;
62
SetUp()63 void SetUp() override { mProcessor = std::make_unique<InputProcessor>(mTestListener); }
64 };
65
TEST_F(InputProcessorTest,SendToNextStage_NotifyKeyArgs)66 TEST_F(InputProcessorTest, SendToNextStage_NotifyKeyArgs) {
67 // Create a basic key event and send to processor
68 NotifyKeyArgs args(/*sequenceNum=*/1, /*eventTime=*/2, /*readTime=*/21, /*deviceId=*/3,
69 AINPUT_SOURCE_KEYBOARD, ui::LogicalDisplayId::DEFAULT, /*policyFlags=*/0,
70 AKEY_EVENT_ACTION_DOWN, /*flags=*/4, AKEYCODE_HOME, /*scanCode=*/5,
71 AMETA_NONE, /*downTime=*/6);
72
73 mProcessor->notifyKey(args);
74 ASSERT_NO_FATAL_FAILURE(mTestListener.assertNotifyKeyWasCalled(testing::Eq(args)));
75 }
76
77 /**
78 * Create a basic motion event and send it to input processor.
79 * Expect that the event is received by the next input stage, unmodified.
80 */
TEST_F(InputProcessorTest,SendToNextStage_NotifyMotionArgs)81 TEST_F(InputProcessorTest, SendToNextStage_NotifyMotionArgs) {
82 NotifyMotionArgs motionArgs = generateBasicMotionArgs();
83 mProcessor->notifyMotion(motionArgs);
84 ASSERT_NO_FATAL_FAILURE(mTestListener.assertNotifyMotionWasCalled(testing::Eq(motionArgs)));
85 }
86
87 /**
88 * Create a basic switch event and send it to input processor.
89 * Expect that the event is received by the next input stage, unmodified.
90 */
TEST_F(InputProcessorTest,SendToNextStage_NotifySwitchArgs)91 TEST_F(InputProcessorTest, SendToNextStage_NotifySwitchArgs) {
92 NotifySwitchArgs args(/*sequenceNum=*/1, /*eventTime=*/2, /*policyFlags=*/3,
93 /*switchValues=*/4, /*switchMask=*/5);
94
95 mProcessor->notifySwitch(args);
96 NotifySwitchArgs outArgs;
97 ASSERT_NO_FATAL_FAILURE(mTestListener.assertNotifySwitchWasCalled(&outArgs));
98 ASSERT_EQ(args, outArgs);
99 }
100
101 /**
102 * Create a basic device reset event and send it to input processor.
103 * Expect that the event is received by the next input stage, unmodified.
104 */
TEST_F(InputProcessorTest,SendToNextStage_NotifyDeviceResetArgs)105 TEST_F(InputProcessorTest, SendToNextStage_NotifyDeviceResetArgs) {
106 NotifyDeviceResetArgs args(/*sequenceNum=*/1, /*eventTime=*/2, /*deviceId=*/3);
107
108 mProcessor->notifyDeviceReset(args);
109 NotifyDeviceResetArgs outArgs;
110 ASSERT_NO_FATAL_FAILURE(mTestListener.assertNotifyDeviceResetWasCalled(&outArgs));
111 ASSERT_EQ(args, outArgs);
112 }
113
TEST_F(InputProcessorTest,SetMotionClassifier_Enabled)114 TEST_F(InputProcessorTest, SetMotionClassifier_Enabled) {
115 mProcessor->setMotionClassifierEnabled(true);
116 }
117
TEST_F(InputProcessorTest,SetMotionClassifier_Disabled)118 TEST_F(InputProcessorTest, SetMotionClassifier_Disabled) {
119 mProcessor->setMotionClassifierEnabled(false);
120 }
121
122 /**
123 * Try to break it by calling setMotionClassifierEnabled multiple times.
124 */
TEST_F(InputProcessorTest,SetMotionClassifier_Multiple)125 TEST_F(InputProcessorTest, SetMotionClassifier_Multiple) {
126 mProcessor->setMotionClassifierEnabled(true);
127 mProcessor->setMotionClassifierEnabled(true);
128 mProcessor->setMotionClassifierEnabled(true);
129 mProcessor->setMotionClassifierEnabled(false);
130 mProcessor->setMotionClassifierEnabled(false);
131 mProcessor->setMotionClassifierEnabled(true);
132 mProcessor->setMotionClassifierEnabled(true);
133 mProcessor->setMotionClassifierEnabled(true);
134 }
135
136 /**
137 * A minimal implementation of IInputProcessor.
138 */
139 class TestHal : public aidl::android::hardware::input::processor::BnInputProcessor {
classify(const::aidl::android::hardware::input::common::MotionEvent & in_event,::aidl::android::hardware::input::common::Classification * _aidl_return)140 ::ndk::ScopedAStatus classify(
141 const ::aidl::android::hardware::input::common::MotionEvent& in_event,
142 ::aidl::android::hardware::input::common::Classification* _aidl_return) override {
143 *_aidl_return = Classification::NONE;
144 return ndk::ScopedAStatus::ok();
145 }
reset()146 ::ndk::ScopedAStatus reset() override { return ndk::ScopedAStatus::ok(); }
resetDevice(int32_t in_deviceId)147 ::ndk::ScopedAStatus resetDevice(int32_t in_deviceId) override {
148 return ndk::ScopedAStatus::ok();
149 }
150 };
151
152 // --- MotionClassifierTest ---
153
154 class MotionClassifierTest : public testing::Test {
155 protected:
156 std::unique_ptr<MotionClassifierInterface> mMotionClassifier;
157
SetUp()158 void SetUp() override {
159 std::shared_ptr<IInputProcessor> service = ndk::SharedRefBase::make<TestHal>();
160 mMotionClassifier = MotionClassifier::create(std::move(service));
161 }
162 };
163
164 /**
165 * Since MotionClassifier creates a new thread to communicate with HAL,
166 * it's not really expected to ever exit. However, for testing purposes,
167 * we need to ensure that it is able to exit cleanly.
168 * If the thread is not properly cleaned up, it will generate SIGABRT.
169 * The logic for exiting the thread and cleaning up the resources is inside
170 * the destructor. Here, we just make sure the destructor does not crash.
171 */
TEST_F(MotionClassifierTest,Destructor_DoesNotCrash)172 TEST_F(MotionClassifierTest, Destructor_DoesNotCrash) {
173 mMotionClassifier = nullptr;
174 }
175
176 /**
177 * Make sure MotionClassifier can handle events that don't have any
178 * video frames.
179 */
TEST_F(MotionClassifierTest,Classify_NoVideoFrames)180 TEST_F(MotionClassifierTest, Classify_NoVideoFrames) {
181 NotifyMotionArgs motionArgs = generateBasicMotionArgs();
182
183 // We are not checking the return value, because we can't be making assumptions
184 // about the HAL operation, since it will be highly hardware-dependent
185 ASSERT_NO_FATAL_FAILURE(mMotionClassifier->classify(motionArgs));
186 }
187
188 /**
189 * Make sure nothing crashes when a videoFrame is sent.
190 */
TEST_F(MotionClassifierTest,Classify_OneVideoFrame)191 TEST_F(MotionClassifierTest, Classify_OneVideoFrame) {
192 NotifyMotionArgs motionArgs = generateBasicMotionArgs();
193
194 std::vector<int16_t> videoData = {1, 2, 3, 4};
195 timeval timestamp = {1, 1};
196 TouchVideoFrame frame(2, 2, std::move(videoData), timestamp);
197 motionArgs.videoFrames = {frame};
198
199 // We are not checking the return value, because we can't be making assumptions
200 // about the HAL operation, since it will be highly hardware-dependent
201 ASSERT_NO_FATAL_FAILURE(mMotionClassifier->classify(motionArgs));
202 }
203
204 /**
205 * Make sure nothing crashes when 2 videoFrames are sent.
206 */
TEST_F(MotionClassifierTest,Classify_TwoVideoFrames)207 TEST_F(MotionClassifierTest, Classify_TwoVideoFrames) {
208 NotifyMotionArgs motionArgs = generateBasicMotionArgs();
209
210 std::vector<int16_t> videoData1 = {1, 2, 3, 4};
211 timeval timestamp1 = {1, 1};
212 TouchVideoFrame frame1(2, 2, std::move(videoData1), timestamp1);
213
214 std::vector<int16_t> videoData2 = {6, 6, 6, 6};
215 timeval timestamp2 = {1, 2};
216 TouchVideoFrame frame2(2, 2, std::move(videoData2), timestamp2);
217
218 motionArgs.videoFrames = {frame1, frame2};
219
220 // We are not checking the return value, because we can't be making assumptions
221 // about the HAL operation, since it will be highly hardware-dependent
222 ASSERT_NO_FATAL_FAILURE(mMotionClassifier->classify(motionArgs));
223 }
224
225 /**
226 * Make sure MotionClassifier does not crash when it is reset.
227 */
TEST_F(MotionClassifierTest,Reset_DoesNotCrash)228 TEST_F(MotionClassifierTest, Reset_DoesNotCrash) {
229 ASSERT_NO_FATAL_FAILURE(mMotionClassifier->reset());
230 }
231
232 /**
233 * Make sure MotionClassifier does not crash when a device is reset.
234 */
TEST_F(MotionClassifierTest,DeviceReset_DoesNotCrash)235 TEST_F(MotionClassifierTest, DeviceReset_DoesNotCrash) {
236 NotifyDeviceResetArgs args(/*sequenceNum=*/1, /*eventTime=*/2, /*deviceId=*/3);
237 ASSERT_NO_FATAL_FAILURE(mMotionClassifier->reset(args));
238 }
239
240 } // namespace android
241