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