blob: f13187ddb708ca0e4acd606dd63321415a967be3 [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
23#include <android/hardware/input/classifier/1.0/IInputClassifier.h>
24
25using namespace android::hardware::input;
Siarhei Vishniakou16523972020-03-04 17:48:39 -080026using android::hardware::Return;
27using android::hardware::Void;
28using android::hardware::input::common::V1_0::Classification;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080029
30namespace android {
31
32// --- InputClassifierTest ---
33
34static NotifyMotionArgs generateBasicMotionArgs() {
35 // Create a basic motion event for testing
Siarhei Vishniakoufd3718c2019-02-28 08:16:26 -080036 PointerProperties properties;
37 properties.id = 0;
38 properties.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080039
Siarhei Vishniakoufd3718c2019-02-28 08:16:26 -080040 PointerCoords coords;
41 coords.clear();
42 coords.setAxisValue(AMOTION_EVENT_AXIS_X, 1);
43 coords.setAxisValue(AMOTION_EVENT_AXIS_Y, 1);
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080044 static constexpr nsecs_t downTime = 2;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +000045 NotifyMotionArgs motionArgs(1 /*sequenceNum*/, downTime /*eventTime*/, 2 /*readTime*/,
46 3 /*deviceId*/, AINPUT_SOURCE_ANY, ADISPLAY_ID_DEFAULT,
47 4 /*policyFlags*/, AMOTION_EVENT_ACTION_DOWN, 0 /*actionButton*/,
48 0 /*flags*/, AMETA_NONE, 0 /*buttonState*/,
49 MotionClassification::NONE, AMOTION_EVENT_EDGE_FLAG_NONE,
50 1 /*pointerCount*/, &properties, &coords, 0 /*xPrecision*/,
51 0 /*yPrecision*/, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Garfield Tan00f511d2019-06-12 16:55:40 -070052 AMOTION_EVENT_INVALID_CURSOR_POSITION, downTime,
53 {} /*videoFrames*/);
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080054 return motionArgs;
55}
56
57class InputClassifierTest : public testing::Test {
58protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -070059 TestInputListener mTestListener;
60 std::unique_ptr<InputClassifierInterface> mClassifier;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080061
Siarhei Vishniakou18050092021-09-01 13:32:49 -070062 void SetUp() override { mClassifier = std::make_unique<InputClassifier>(mTestListener); }
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080063};
64
65/**
66 * Create a basic configuration change and send it to input classifier.
67 * Expect that the event is received by the next input stage, unmodified.
68 */
69TEST_F(InputClassifierTest, SendToNextStage_NotifyConfigurationChangedArgs) {
70 // Create a basic configuration change and send to classifier
71 NotifyConfigurationChangedArgs args(1/*sequenceNum*/, 2/*eventTime*/);
72
73 mClassifier->notifyConfigurationChanged(&args);
74 NotifyConfigurationChangedArgs outArgs;
Siarhei Vishniakou18050092021-09-01 13:32:49 -070075 ASSERT_NO_FATAL_FAILURE(mTestListener.assertNotifyConfigurationChangedWasCalled(&outArgs));
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080076 ASSERT_EQ(args, outArgs);
77}
78
79TEST_F(InputClassifierTest, SendToNextStage_NotifyKeyArgs) {
80 // Create a basic key event and send to classifier
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +000081 NotifyKeyArgs args(1 /*sequenceNum*/, 2 /*eventTime*/, 21 /*readTime*/, 3 /*deviceId*/,
82 AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_DEFAULT, 0 /*policyFlags*/,
83 AKEY_EVENT_ACTION_DOWN, 4 /*flags*/, AKEYCODE_HOME, 5 /*scanCode*/,
84 AMETA_NONE, 6 /*downTime*/);
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080085
86 mClassifier->notifyKey(&args);
87 NotifyKeyArgs outArgs;
Siarhei Vishniakou18050092021-09-01 13:32:49 -070088 ASSERT_NO_FATAL_FAILURE(mTestListener.assertNotifyKeyWasCalled(&outArgs));
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080089 ASSERT_EQ(args, outArgs);
90}
91
92
93/**
94 * Create a basic motion event and send it to input classifier.
95 * Expect that the event is received by the next input stage, unmodified.
96 */
97TEST_F(InputClassifierTest, SendToNextStage_NotifyMotionArgs) {
98 NotifyMotionArgs motionArgs = generateBasicMotionArgs();
99 mClassifier->notifyMotion(&motionArgs);
100 NotifyMotionArgs args;
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700101 ASSERT_NO_FATAL_FAILURE(mTestListener.assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800102 ASSERT_EQ(motionArgs, args);
103}
104
105/**
106 * Create a basic switch event and send it to input classifier.
107 * Expect that the event is received by the next input stage, unmodified.
108 */
109TEST_F(InputClassifierTest, SendToNextStage_NotifySwitchArgs) {
110 NotifySwitchArgs args(1/*sequenceNum*/, 2/*eventTime*/, 3/*policyFlags*/, 4/*switchValues*/,
111 5/*switchMask*/);
112
113 mClassifier->notifySwitch(&args);
114 NotifySwitchArgs outArgs;
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700115 ASSERT_NO_FATAL_FAILURE(mTestListener.assertNotifySwitchWasCalled(&outArgs));
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800116 ASSERT_EQ(args, outArgs);
117}
118
119/**
120 * Create a basic device reset event and send it to input classifier.
121 * Expect that the event is received by the next input stage, unmodified.
122 */
123TEST_F(InputClassifierTest, SendToNextStage_NotifyDeviceResetArgs) {
124 NotifyDeviceResetArgs args(1/*sequenceNum*/, 2/*eventTime*/, 3/*deviceId*/);
125
126 mClassifier->notifyDeviceReset(&args);
127 NotifyDeviceResetArgs outArgs;
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700128 ASSERT_NO_FATAL_FAILURE(mTestListener.assertNotifyDeviceResetWasCalled(&outArgs));
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800129 ASSERT_EQ(args, outArgs);
130}
131
Siarhei Vishniakouc9ac19e2020-03-19 11:55:01 -0700132TEST_F(InputClassifierTest, SetMotionClassifier_Enabled) {
133 mClassifier->setMotionClassifierEnabled(true);
134}
135
136TEST_F(InputClassifierTest, SetMotionClassifier_Disabled) {
137 mClassifier->setMotionClassifierEnabled(false);
138}
139
140/**
141 * Try to break it by calling setMotionClassifierEnabled multiple times.
142 */
143TEST_F(InputClassifierTest, SetMotionClassifier_Multiple) {
144 mClassifier->setMotionClassifierEnabled(true);
145 mClassifier->setMotionClassifierEnabled(true);
146 mClassifier->setMotionClassifierEnabled(true);
147 mClassifier->setMotionClassifierEnabled(false);
148 mClassifier->setMotionClassifierEnabled(false);
149 mClassifier->setMotionClassifierEnabled(true);
150 mClassifier->setMotionClassifierEnabled(true);
151 mClassifier->setMotionClassifierEnabled(true);
152}
153
Siarhei Vishniakou16523972020-03-04 17:48:39 -0800154/**
155 * A minimal implementation of IInputClassifier.
156 */
157struct TestHal : public android::hardware::input::classifier::V1_0::IInputClassifier {
158 Return<Classification> classify(
159 const android::hardware::input::common::V1_0::MotionEvent& event) override {
160 return Classification::NONE;
161 };
162 Return<void> reset() override { return Void(); };
163 Return<void> resetDevice(int32_t deviceId) override { return Void(); };
164};
165
166/**
167 * An entity that will be subscribed to the HAL death.
168 */
169class TestDeathRecipient : public android::hardware::hidl_death_recipient {
170public:
171 virtual void serviceDied(uint64_t cookie,
172 const wp<android::hidl::base::V1_0::IBase>& who) override{};
173};
174
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800175// --- MotionClassifierTest ---
176
177class MotionClassifierTest : public testing::Test {
178protected:
179 std::unique_ptr<MotionClassifierInterface> mMotionClassifier;
180
181 virtual void SetUp() override {
Siarhei Vishniakou16523972020-03-04 17:48:39 -0800182 mMotionClassifier = MotionClassifier::create(new TestDeathRecipient());
183 if (mMotionClassifier == nullptr) {
184 // If the device running this test does not have IInputClassifier service,
185 // use the test HAL instead.
186 // Using 'new' to access non-public constructor
187 mMotionClassifier =
188 std::unique_ptr<MotionClassifier>(new MotionClassifier(new TestHal()));
189 }
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800190 }
191};
192
193/**
194 * Since MotionClassifier creates a new thread to communicate with HAL,
195 * it's not really expected to ever exit. However, for testing purposes,
196 * we need to ensure that it is able to exit cleanly.
197 * If the thread is not properly cleaned up, it will generate SIGABRT.
198 * The logic for exiting the thread and cleaning up the resources is inside
199 * the destructor. Here, we just make sure the destructor does not crash.
200 */
201TEST_F(MotionClassifierTest, Destructor_DoesNotCrash) {
202 mMotionClassifier = nullptr;
203}
204
205/**
206 * Make sure MotionClassifier can handle events that don't have any
207 * video frames.
208 */
209TEST_F(MotionClassifierTest, Classify_NoVideoFrames) {
210 NotifyMotionArgs motionArgs = generateBasicMotionArgs();
211
212 // We are not checking the return value, because we can't be making assumptions
213 // about the HAL operation, since it will be highly hardware-dependent
Siarhei Vishniakou4bdbb6a2019-04-11 09:42:09 -0700214 ASSERT_NO_FATAL_FAILURE(mMotionClassifier->classify(motionArgs));
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800215}
216
217/**
218 * Make sure nothing crashes when a videoFrame is sent.
219 */
220TEST_F(MotionClassifierTest, Classify_OneVideoFrame) {
221 NotifyMotionArgs motionArgs = generateBasicMotionArgs();
222
223 std::vector<int16_t> videoData = {1, 2, 3, 4};
224 timeval timestamp = { 1, 1};
225 TouchVideoFrame frame(2, 2, std::move(videoData), timestamp);
226 motionArgs.videoFrames = {frame};
227
228 // We are not checking the return value, because we can't be making assumptions
229 // about the HAL operation, since it will be highly hardware-dependent
Siarhei Vishniakou4bdbb6a2019-04-11 09:42:09 -0700230 ASSERT_NO_FATAL_FAILURE(mMotionClassifier->classify(motionArgs));
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800231}
232
233/**
234 * Make sure nothing crashes when 2 videoFrames are sent.
235 */
236TEST_F(MotionClassifierTest, Classify_TwoVideoFrames) {
237 NotifyMotionArgs motionArgs = generateBasicMotionArgs();
238
239 std::vector<int16_t> videoData1 = {1, 2, 3, 4};
240 timeval timestamp1 = { 1, 1};
241 TouchVideoFrame frame1(2, 2, std::move(videoData1), timestamp1);
242
243 std::vector<int16_t> videoData2 = {6, 6, 6, 6};
244 timeval timestamp2 = { 1, 2};
245 TouchVideoFrame frame2(2, 2, std::move(videoData2), timestamp2);
246
247 motionArgs.videoFrames = {frame1, frame2};
248
249 // We are not checking the return value, because we can't be making assumptions
250 // about the HAL operation, since it will be highly hardware-dependent
Siarhei Vishniakou4bdbb6a2019-04-11 09:42:09 -0700251 ASSERT_NO_FATAL_FAILURE(mMotionClassifier->classify(motionArgs));
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800252}
253
254/**
255 * Make sure MotionClassifier does not crash when it is reset.
256 */
257TEST_F(MotionClassifierTest, Reset_DoesNotCrash) {
Siarhei Vishniakou4bdbb6a2019-04-11 09:42:09 -0700258 ASSERT_NO_FATAL_FAILURE(mMotionClassifier->reset());
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800259}
260
261/**
262 * Make sure MotionClassifier does not crash when a device is reset.
263 */
264TEST_F(MotionClassifierTest, DeviceReset_DoesNotCrash) {
265 NotifyDeviceResetArgs args(1/*sequenceNum*/, 2/*eventTime*/, 3/*deviceId*/);
Siarhei Vishniakou4bdbb6a2019-04-11 09:42:09 -0700266 ASSERT_NO_FATAL_FAILURE(mMotionClassifier->reset(args));
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800267}
268
269} // namespace android