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