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