blob: 3a7712727a26340d7856efdb7c48ea85e84db734 [file] [log] [blame]
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001/*
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>
chaviw98318de2021-05-19 16:45:23 -050019#include <gui/constants.h>
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080020
21#include "TestInputListener.h"
22
Siarhei Vishniakou34d6fef2022-02-01 19:03:45 -080023#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>
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080027
Siarhei Vishniakou34d6fef2022-02-01 19:03:45 -080028using namespace aidl::android::hardware::input;
29using aidl::android::hardware::input::common::Classification;
30using aidl::android::hardware::input::processor::IInputProcessor;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080031
32namespace android {
33
34// --- InputClassifierTest ---
35
36static NotifyMotionArgs generateBasicMotionArgs() {
37 // Create a basic motion event for testing
Siarhei Vishniakoufd3718c2019-02-28 08:16:26 -080038 PointerProperties properties;
39 properties.id = 0;
40 properties.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080041
Siarhei Vishniakoufd3718c2019-02-28 08:16:26 -080042 PointerCoords coords;
43 coords.clear();
44 coords.setAxisValue(AMOTION_EVENT_AXIS_X, 1);
45 coords.setAxisValue(AMOTION_EVENT_AXIS_Y, 1);
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080046 static constexpr nsecs_t downTime = 2;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +000047 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,
Garfield Tan00f511d2019-06-12 16:55:40 -070054 AMOTION_EVENT_INVALID_CURSOR_POSITION, downTime,
55 {} /*videoFrames*/);
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080056 return motionArgs;
57}
58
59class InputClassifierTest : public testing::Test {
60protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -070061 TestInputListener mTestListener;
62 std::unique_ptr<InputClassifierInterface> mClassifier;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080063
Siarhei Vishniakou18050092021-09-01 13:32:49 -070064 void SetUp() override { mClassifier = std::make_unique<InputClassifier>(mTestListener); }
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080065};
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 */
71TEST_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;
Siarhei Vishniakou18050092021-09-01 13:32:49 -070077 ASSERT_NO_FATAL_FAILURE(mTestListener.assertNotifyConfigurationChangedWasCalled(&outArgs));
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080078 ASSERT_EQ(args, outArgs);
79}
80
81TEST_F(InputClassifierTest, SendToNextStage_NotifyKeyArgs) {
82 // Create a basic key event and send to classifier
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +000083 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*/);
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080087
88 mClassifier->notifyKey(&args);
89 NotifyKeyArgs outArgs;
Siarhei Vishniakou18050092021-09-01 13:32:49 -070090 ASSERT_NO_FATAL_FAILURE(mTestListener.assertNotifyKeyWasCalled(&outArgs));
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080091 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 */
99TEST_F(InputClassifierTest, SendToNextStage_NotifyMotionArgs) {
100 NotifyMotionArgs motionArgs = generateBasicMotionArgs();
101 mClassifier->notifyMotion(&motionArgs);
102 NotifyMotionArgs args;
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700103 ASSERT_NO_FATAL_FAILURE(mTestListener.assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800104 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 */
111TEST_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;
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700117 ASSERT_NO_FATAL_FAILURE(mTestListener.assertNotifySwitchWasCalled(&outArgs));
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800118 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 */
125TEST_F(InputClassifierTest, SendToNextStage_NotifyDeviceResetArgs) {
126 NotifyDeviceResetArgs args(1/*sequenceNum*/, 2/*eventTime*/, 3/*deviceId*/);
127
128 mClassifier->notifyDeviceReset(&args);
129 NotifyDeviceResetArgs outArgs;
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700130 ASSERT_NO_FATAL_FAILURE(mTestListener.assertNotifyDeviceResetWasCalled(&outArgs));
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800131 ASSERT_EQ(args, outArgs);
132}
133
Siarhei Vishniakouc9ac19e2020-03-19 11:55:01 -0700134TEST_F(InputClassifierTest, SetMotionClassifier_Enabled) {
135 mClassifier->setMotionClassifierEnabled(true);
136}
137
138TEST_F(InputClassifierTest, SetMotionClassifier_Disabled) {
139 mClassifier->setMotionClassifierEnabled(false);
140}
141
142/**
143 * Try to break it by calling setMotionClassifierEnabled multiple times.
144 */
145TEST_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
Siarhei Vishniakou16523972020-03-04 17:48:39 -0800156/**
157 * A minimal implementation of IInputClassifier.
158 */
Siarhei Vishniakou34d6fef2022-02-01 19:03:45 -0800159class TestHal : public aidl::android::hardware::input::processor::BnInputProcessor {
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 }
166 ::ndk::ScopedAStatus reset() override { return ndk::ScopedAStatus::ok(); }
167 ::ndk::ScopedAStatus resetDevice(int32_t in_deviceId) override {
168 return ndk::ScopedAStatus::ok();
169 }
Siarhei Vishniakou16523972020-03-04 17:48:39 -0800170};
171
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800172// --- MotionClassifierTest ---
173
174class MotionClassifierTest : public testing::Test {
175protected:
176 std::unique_ptr<MotionClassifierInterface> mMotionClassifier;
177
Siarhei Vishniakou34d6fef2022-02-01 19:03:45 -0800178 void SetUp() override {
179 std::shared_ptr<IInputProcessor> service = ndk::SharedRefBase::make<TestHal>();
180 mMotionClassifier = MotionClassifier::create(std::move(service));
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800181 }
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 */
192TEST_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 */
200TEST_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
Siarhei Vishniakou4bdbb6a2019-04-11 09:42:09 -0700205 ASSERT_NO_FATAL_FAILURE(mMotionClassifier->classify(motionArgs));
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800206}
207
208/**
209 * Make sure nothing crashes when a videoFrame is sent.
210 */
211TEST_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
Siarhei Vishniakou4bdbb6a2019-04-11 09:42:09 -0700221 ASSERT_NO_FATAL_FAILURE(mMotionClassifier->classify(motionArgs));
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800222}
223
224/**
225 * Make sure nothing crashes when 2 videoFrames are sent.
226 */
227TEST_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
Siarhei Vishniakou4bdbb6a2019-04-11 09:42:09 -0700242 ASSERT_NO_FATAL_FAILURE(mMotionClassifier->classify(motionArgs));
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800243}
244
245/**
246 * Make sure MotionClassifier does not crash when it is reset.
247 */
248TEST_F(MotionClassifierTest, Reset_DoesNotCrash) {
Siarhei Vishniakou4bdbb6a2019-04-11 09:42:09 -0700249 ASSERT_NO_FATAL_FAILURE(mMotionClassifier->reset());
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800250}
251
252/**
253 * Make sure MotionClassifier does not crash when a device is reset.
254 */
255TEST_F(MotionClassifierTest, DeviceReset_DoesNotCrash) {
256 NotifyDeviceResetArgs args(1/*sequenceNum*/, 2/*eventTime*/, 3/*deviceId*/);
Siarhei Vishniakou4bdbb6a2019-04-11 09:42:09 -0700257 ASSERT_NO_FATAL_FAILURE(mMotionClassifier->reset(args));
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800258}
259
260} // namespace android