blob: 9121d7ebbad8ebfb32281a582373eac2edcea92c [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 Vishniakou958c4d02020-03-04 17:48:39 -0800135/**
136 * A minimal implementation of IInputClassifier.
137 */
138struct TestHal : public android::hardware::input::classifier::V1_0::IInputClassifier {
139 Return<Classification> classify(
140 const android::hardware::input::common::V1_0::MotionEvent& event) override {
141 return Classification::NONE;
142 };
143 Return<void> reset() override { return Void(); };
144 Return<void> resetDevice(int32_t deviceId) override { return Void(); };
145};
146
147/**
148 * An entity that will be subscribed to the HAL death.
149 */
150class TestDeathRecipient : public android::hardware::hidl_death_recipient {
151public:
152 virtual void serviceDied(uint64_t cookie,
153 const wp<android::hidl::base::V1_0::IBase>& who) override{};
154};
155
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800156// --- MotionClassifierTest ---
157
158class MotionClassifierTest : public testing::Test {
159protected:
160 std::unique_ptr<MotionClassifierInterface> mMotionClassifier;
161
162 virtual void SetUp() override {
Siarhei Vishniakou958c4d02020-03-04 17:48:39 -0800163 mMotionClassifier = MotionClassifier::create(new TestDeathRecipient());
164 if (mMotionClassifier == nullptr) {
165 // If the device running this test does not have IInputClassifier service,
166 // use the test HAL instead.
167 // Using 'new' to access non-public constructor
168 mMotionClassifier =
169 std::unique_ptr<MotionClassifier>(new MotionClassifier(new TestHal()));
170 }
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800171 }
172};
173
174/**
175 * Since MotionClassifier creates a new thread to communicate with HAL,
176 * it's not really expected to ever exit. However, for testing purposes,
177 * we need to ensure that it is able to exit cleanly.
178 * If the thread is not properly cleaned up, it will generate SIGABRT.
179 * The logic for exiting the thread and cleaning up the resources is inside
180 * the destructor. Here, we just make sure the destructor does not crash.
181 */
182TEST_F(MotionClassifierTest, Destructor_DoesNotCrash) {
183 mMotionClassifier = nullptr;
184}
185
186/**
187 * Make sure MotionClassifier can handle events that don't have any
188 * video frames.
189 */
190TEST_F(MotionClassifierTest, Classify_NoVideoFrames) {
191 NotifyMotionArgs motionArgs = generateBasicMotionArgs();
192
193 // We are not checking the return value, because we can't be making assumptions
194 // about the HAL operation, since it will be highly hardware-dependent
Siarhei Vishniakou4bdbb6a2019-04-11 09:42:09 -0700195 ASSERT_NO_FATAL_FAILURE(mMotionClassifier->classify(motionArgs));
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800196}
197
198/**
199 * Make sure nothing crashes when a videoFrame is sent.
200 */
201TEST_F(MotionClassifierTest, Classify_OneVideoFrame) {
202 NotifyMotionArgs motionArgs = generateBasicMotionArgs();
203
204 std::vector<int16_t> videoData = {1, 2, 3, 4};
205 timeval timestamp = { 1, 1};
206 TouchVideoFrame frame(2, 2, std::move(videoData), timestamp);
207 motionArgs.videoFrames = {frame};
208
209 // We are not checking the return value, because we can't be making assumptions
210 // about the HAL operation, since it will be highly hardware-dependent
Siarhei Vishniakou4bdbb6a2019-04-11 09:42:09 -0700211 ASSERT_NO_FATAL_FAILURE(mMotionClassifier->classify(motionArgs));
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800212}
213
214/**
215 * Make sure nothing crashes when 2 videoFrames are sent.
216 */
217TEST_F(MotionClassifierTest, Classify_TwoVideoFrames) {
218 NotifyMotionArgs motionArgs = generateBasicMotionArgs();
219
220 std::vector<int16_t> videoData1 = {1, 2, 3, 4};
221 timeval timestamp1 = { 1, 1};
222 TouchVideoFrame frame1(2, 2, std::move(videoData1), timestamp1);
223
224 std::vector<int16_t> videoData2 = {6, 6, 6, 6};
225 timeval timestamp2 = { 1, 2};
226 TouchVideoFrame frame2(2, 2, std::move(videoData2), timestamp2);
227
228 motionArgs.videoFrames = {frame1, frame2};
229
230 // We are not checking the return value, because we can't be making assumptions
231 // about the HAL operation, since it will be highly hardware-dependent
Siarhei Vishniakou4bdbb6a2019-04-11 09:42:09 -0700232 ASSERT_NO_FATAL_FAILURE(mMotionClassifier->classify(motionArgs));
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800233}
234
235/**
236 * Make sure MotionClassifier does not crash when it is reset.
237 */
238TEST_F(MotionClassifierTest, Reset_DoesNotCrash) {
Siarhei Vishniakou4bdbb6a2019-04-11 09:42:09 -0700239 ASSERT_NO_FATAL_FAILURE(mMotionClassifier->reset());
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800240}
241
242/**
243 * Make sure MotionClassifier does not crash when a device is reset.
244 */
245TEST_F(MotionClassifierTest, DeviceReset_DoesNotCrash) {
246 NotifyDeviceResetArgs args(1/*sequenceNum*/, 2/*eventTime*/, 3/*deviceId*/);
Siarhei Vishniakou4bdbb6a2019-04-11 09:42:09 -0700247 ASSERT_NO_FATAL_FAILURE(mMotionClassifier->reset(args));
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800248}
249
250} // namespace android