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