blob: b4e755a595e80b32261e9163d1c9cc41a3adfcc9 [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 Vishniakou16523972020-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;
Garfield Tan00f511d2019-06-12 16:55:40 -070044 NotifyMotionArgs motionArgs(1 /*sequenceNum*/, downTime /*eventTime*/, 3 /*deviceId*/,
45 AINPUT_SOURCE_ANY, ADISPLAY_ID_DEFAULT, 4 /*policyFlags*/,
46 AMOTION_EVENT_ACTION_DOWN, 0 /*actionButton*/, 0 /*flags*/,
47 AMETA_NONE, 0 /*buttonState*/, MotionClassification::NONE,
Atif Niyaz21da0ff2019-06-28 13:22:51 -070048 AMOTION_EVENT_EDGE_FLAG_NONE, 1 /*pointerCount*/, &properties,
49 &coords, 0 /*xPrecision*/, 0 /*yPrecision*/,
50 AMOTION_EVENT_INVALID_CURSOR_POSITION,
Garfield Tan00f511d2019-06-12 16:55:40 -070051 AMOTION_EVENT_INVALID_CURSOR_POSITION, downTime,
52 {} /*videoFrames*/);
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080053 return motionArgs;
54}
55
56class InputClassifierTest : public testing::Test {
57protected:
58 sp<InputClassifierInterface> mClassifier;
59 sp<TestInputListener> mTestListener;
60
61 virtual void SetUp() override {
62 mTestListener = new TestInputListener();
63 mClassifier = new InputClassifier(mTestListener);
64 }
65
66 virtual void TearDown() override {
67 mClassifier.clear();
68 mTestListener.clear();
69 }
70};
71
72/**
73 * Create a basic configuration change and send it to input classifier.
74 * Expect that the event is received by the next input stage, unmodified.
75 */
76TEST_F(InputClassifierTest, SendToNextStage_NotifyConfigurationChangedArgs) {
77 // Create a basic configuration change and send to classifier
78 NotifyConfigurationChangedArgs args(1/*sequenceNum*/, 2/*eventTime*/);
79
80 mClassifier->notifyConfigurationChanged(&args);
81 NotifyConfigurationChangedArgs outArgs;
82 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled(&outArgs));
83 ASSERT_EQ(args, outArgs);
84}
85
86TEST_F(InputClassifierTest, SendToNextStage_NotifyKeyArgs) {
87 // Create a basic key event and send to classifier
88 NotifyKeyArgs args(1/*sequenceNum*/, 2/*eventTime*/, 3/*deviceId*/, AINPUT_SOURCE_KEYBOARD,
89 ADISPLAY_ID_DEFAULT, 0/*policyFlags*/, AKEY_EVENT_ACTION_DOWN, 4/*flags*/,
90 AKEYCODE_HOME, 5/*scanCode*/, AMETA_NONE, 6/*downTime*/);
91
92 mClassifier->notifyKey(&args);
93 NotifyKeyArgs outArgs;
94 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&outArgs));
95 ASSERT_EQ(args, outArgs);
96}
97
98
99/**
100 * Create a basic motion event and send it to input classifier.
101 * Expect that the event is received by the next input stage, unmodified.
102 */
103TEST_F(InputClassifierTest, SendToNextStage_NotifyMotionArgs) {
104 NotifyMotionArgs motionArgs = generateBasicMotionArgs();
105 mClassifier->notifyMotion(&motionArgs);
106 NotifyMotionArgs args;
107 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
108 ASSERT_EQ(motionArgs, args);
109}
110
111/**
112 * Create a basic switch event and send it to input classifier.
113 * Expect that the event is received by the next input stage, unmodified.
114 */
115TEST_F(InputClassifierTest, SendToNextStage_NotifySwitchArgs) {
116 NotifySwitchArgs args(1/*sequenceNum*/, 2/*eventTime*/, 3/*policyFlags*/, 4/*switchValues*/,
117 5/*switchMask*/);
118
119 mClassifier->notifySwitch(&args);
120 NotifySwitchArgs outArgs;
121 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifySwitchWasCalled(&outArgs));
122 ASSERT_EQ(args, outArgs);
123}
124
125/**
126 * Create a basic device reset event and send it to input classifier.
127 * Expect that the event is received by the next input stage, unmodified.
128 */
129TEST_F(InputClassifierTest, SendToNextStage_NotifyDeviceResetArgs) {
130 NotifyDeviceResetArgs args(1/*sequenceNum*/, 2/*eventTime*/, 3/*deviceId*/);
131
132 mClassifier->notifyDeviceReset(&args);
133 NotifyDeviceResetArgs outArgs;
134 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyDeviceResetWasCalled(&outArgs));
135 ASSERT_EQ(args, outArgs);
136}
137
Siarhei Vishniakou16523972020-03-04 17:48:39 -0800138/**
139 * A minimal implementation of IInputClassifier.
140 */
141struct TestHal : public android::hardware::input::classifier::V1_0::IInputClassifier {
142 Return<Classification> classify(
143 const android::hardware::input::common::V1_0::MotionEvent& event) override {
144 return Classification::NONE;
145 };
146 Return<void> reset() override { return Void(); };
147 Return<void> resetDevice(int32_t deviceId) override { return Void(); };
148};
149
150/**
151 * An entity that will be subscribed to the HAL death.
152 */
153class TestDeathRecipient : public android::hardware::hidl_death_recipient {
154public:
155 virtual void serviceDied(uint64_t cookie,
156 const wp<android::hidl::base::V1_0::IBase>& who) override{};
157};
158
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800159// --- MotionClassifierTest ---
160
161class MotionClassifierTest : public testing::Test {
162protected:
163 std::unique_ptr<MotionClassifierInterface> mMotionClassifier;
164
165 virtual void SetUp() override {
Siarhei Vishniakou16523972020-03-04 17:48:39 -0800166 mMotionClassifier = MotionClassifier::create(new TestDeathRecipient());
167 if (mMotionClassifier == nullptr) {
168 // If the device running this test does not have IInputClassifier service,
169 // use the test HAL instead.
170 // Using 'new' to access non-public constructor
171 mMotionClassifier =
172 std::unique_ptr<MotionClassifier>(new MotionClassifier(new TestHal()));
173 }
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800174 }
175};
176
177/**
178 * Since MotionClassifier creates a new thread to communicate with HAL,
179 * it's not really expected to ever exit. However, for testing purposes,
180 * we need to ensure that it is able to exit cleanly.
181 * If the thread is not properly cleaned up, it will generate SIGABRT.
182 * The logic for exiting the thread and cleaning up the resources is inside
183 * the destructor. Here, we just make sure the destructor does not crash.
184 */
185TEST_F(MotionClassifierTest, Destructor_DoesNotCrash) {
186 mMotionClassifier = nullptr;
187}
188
189/**
190 * Make sure MotionClassifier can handle events that don't have any
191 * video frames.
192 */
193TEST_F(MotionClassifierTest, Classify_NoVideoFrames) {
194 NotifyMotionArgs motionArgs = generateBasicMotionArgs();
195
196 // We are not checking the return value, because we can't be making assumptions
197 // about the HAL operation, since it will be highly hardware-dependent
Siarhei Vishniakou4bdbb6a2019-04-11 09:42:09 -0700198 ASSERT_NO_FATAL_FAILURE(mMotionClassifier->classify(motionArgs));
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800199}
200
201/**
202 * Make sure nothing crashes when a videoFrame is sent.
203 */
204TEST_F(MotionClassifierTest, Classify_OneVideoFrame) {
205 NotifyMotionArgs motionArgs = generateBasicMotionArgs();
206
207 std::vector<int16_t> videoData = {1, 2, 3, 4};
208 timeval timestamp = { 1, 1};
209 TouchVideoFrame frame(2, 2, std::move(videoData), timestamp);
210 motionArgs.videoFrames = {frame};
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 2 videoFrames are sent.
219 */
220TEST_F(MotionClassifierTest, Classify_TwoVideoFrames) {
221 NotifyMotionArgs motionArgs = generateBasicMotionArgs();
222
223 std::vector<int16_t> videoData1 = {1, 2, 3, 4};
224 timeval timestamp1 = { 1, 1};
225 TouchVideoFrame frame1(2, 2, std::move(videoData1), timestamp1);
226
227 std::vector<int16_t> videoData2 = {6, 6, 6, 6};
228 timeval timestamp2 = { 1, 2};
229 TouchVideoFrame frame2(2, 2, std::move(videoData2), timestamp2);
230
231 motionArgs.videoFrames = {frame1, frame2};
232
233 // We are not checking the return value, because we can't be making assumptions
234 // about the HAL operation, since it will be highly hardware-dependent
Siarhei Vishniakou4bdbb6a2019-04-11 09:42:09 -0700235 ASSERT_NO_FATAL_FAILURE(mMotionClassifier->classify(motionArgs));
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800236}
237
238/**
239 * Make sure MotionClassifier does not crash when it is reset.
240 */
241TEST_F(MotionClassifierTest, Reset_DoesNotCrash) {
Siarhei Vishniakou4bdbb6a2019-04-11 09:42:09 -0700242 ASSERT_NO_FATAL_FAILURE(mMotionClassifier->reset());
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800243}
244
245/**
246 * Make sure MotionClassifier does not crash when a device is reset.
247 */
248TEST_F(MotionClassifierTest, DeviceReset_DoesNotCrash) {
249 NotifyDeviceResetArgs args(1/*sequenceNum*/, 2/*eventTime*/, 3/*deviceId*/);
Siarhei Vishniakou4bdbb6a2019-04-11 09:42:09 -0700250 ASSERT_NO_FATAL_FAILURE(mMotionClassifier->reset(args));
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800251}
252
253} // namespace android