blob: 20699c9288572b0b31b017defc6862577ec8f6ca [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;
25
26namespace android {
27
28// --- InputClassifierTest ---
29
30static NotifyMotionArgs generateBasicMotionArgs() {
31 // Create a basic motion event for testing
32 constexpr size_t pointerCount = 1;
33 PointerProperties properties[pointerCount];
34 properties[0].id = 0;
35 properties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
36
37 PointerCoords coords[pointerCount];
38 coords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 1);
39 coords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 1);
40 static constexpr nsecs_t downTime = 2;
41 NotifyMotionArgs motionArgs(1/*sequenceNum*/, downTime/*eventTime*/, 3/*deviceId*/,
42 AINPUT_SOURCE_ANY, ADISPLAY_ID_DEFAULT, 4/*policyFlags*/, AMOTION_EVENT_ACTION_DOWN,
43 0/*actionButton*/, 0/*flags*/, AMETA_NONE, 0/*buttonState*/, MotionClassification::NONE,
44 AMOTION_EVENT_EDGE_FLAG_NONE, 5/*deviceTimestamp*/,
45 0/*pointerCount*/, properties, coords, 0/*xPrecision*/, 0/*yPrecision*/,
46 downTime, {}/*videoFrames*/);
47 return motionArgs;
48}
49
50class InputClassifierTest : public testing::Test {
51protected:
52 sp<InputClassifierInterface> mClassifier;
53 sp<TestInputListener> mTestListener;
54
55 virtual void SetUp() override {
56 mTestListener = new TestInputListener();
57 mClassifier = new InputClassifier(mTestListener);
58 }
59
60 virtual void TearDown() override {
61 mClassifier.clear();
62 mTestListener.clear();
63 }
64};
65
66/**
67 * Create a basic configuration change and send it to input classifier.
68 * Expect that the event is received by the next input stage, unmodified.
69 */
70TEST_F(InputClassifierTest, SendToNextStage_NotifyConfigurationChangedArgs) {
71 // Create a basic configuration change and send to classifier
72 NotifyConfigurationChangedArgs args(1/*sequenceNum*/, 2/*eventTime*/);
73
74 mClassifier->notifyConfigurationChanged(&args);
75 NotifyConfigurationChangedArgs outArgs;
76 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled(&outArgs));
77 ASSERT_EQ(args, outArgs);
78}
79
80TEST_F(InputClassifierTest, SendToNextStage_NotifyKeyArgs) {
81 // Create a basic key event and send to classifier
82 NotifyKeyArgs args(1/*sequenceNum*/, 2/*eventTime*/, 3/*deviceId*/, AINPUT_SOURCE_KEYBOARD,
83 ADISPLAY_ID_DEFAULT, 0/*policyFlags*/, AKEY_EVENT_ACTION_DOWN, 4/*flags*/,
84 AKEYCODE_HOME, 5/*scanCode*/, AMETA_NONE, 6/*downTime*/);
85
86 mClassifier->notifyKey(&args);
87 NotifyKeyArgs outArgs;
88 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&outArgs));
89 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;
101 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
102 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;
115 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifySwitchWasCalled(&outArgs));
116 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;
128 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyDeviceResetWasCalled(&outArgs));
129 ASSERT_EQ(args, outArgs);
130}
131
132// --- MotionClassifierTest ---
133
134class MotionClassifierTest : public testing::Test {
135protected:
136 std::unique_ptr<MotionClassifierInterface> mMotionClassifier;
137
138 virtual void SetUp() override {
139 sp<android::hardware::input::classifier::V1_0::IInputClassifier> service =
140 classifier::V1_0::IInputClassifier::getService();
141 if (service) {
142 mMotionClassifier = std::make_unique<MotionClassifier>(service);
143 }
144 }
145};
146
147/**
148 * Since MotionClassifier creates a new thread to communicate with HAL,
149 * it's not really expected to ever exit. However, for testing purposes,
150 * we need to ensure that it is able to exit cleanly.
151 * If the thread is not properly cleaned up, it will generate SIGABRT.
152 * The logic for exiting the thread and cleaning up the resources is inside
153 * the destructor. Here, we just make sure the destructor does not crash.
154 */
155TEST_F(MotionClassifierTest, Destructor_DoesNotCrash) {
156 mMotionClassifier = nullptr;
157}
158
159/**
160 * Make sure MotionClassifier can handle events that don't have any
161 * video frames.
162 */
163TEST_F(MotionClassifierTest, Classify_NoVideoFrames) {
164 NotifyMotionArgs motionArgs = generateBasicMotionArgs();
165
166 // We are not checking the return value, because we can't be making assumptions
167 // about the HAL operation, since it will be highly hardware-dependent
168 if (mMotionClassifier) {
169 ASSERT_NO_FATAL_FAILURE(mMotionClassifier->classify(motionArgs));
170 }
171}
172
173/**
174 * Make sure nothing crashes when a videoFrame is sent.
175 */
176TEST_F(MotionClassifierTest, Classify_OneVideoFrame) {
177 NotifyMotionArgs motionArgs = generateBasicMotionArgs();
178
179 std::vector<int16_t> videoData = {1, 2, 3, 4};
180 timeval timestamp = { 1, 1};
181 TouchVideoFrame frame(2, 2, std::move(videoData), timestamp);
182 motionArgs.videoFrames = {frame};
183
184 // We are not checking the return value, because we can't be making assumptions
185 // about the HAL operation, since it will be highly hardware-dependent
186 if (mMotionClassifier) {
187 ASSERT_NO_FATAL_FAILURE(mMotionClassifier->classify(motionArgs));
188 }
189}
190
191/**
192 * Make sure nothing crashes when 2 videoFrames are sent.
193 */
194TEST_F(MotionClassifierTest, Classify_TwoVideoFrames) {
195 NotifyMotionArgs motionArgs = generateBasicMotionArgs();
196
197 std::vector<int16_t> videoData1 = {1, 2, 3, 4};
198 timeval timestamp1 = { 1, 1};
199 TouchVideoFrame frame1(2, 2, std::move(videoData1), timestamp1);
200
201 std::vector<int16_t> videoData2 = {6, 6, 6, 6};
202 timeval timestamp2 = { 1, 2};
203 TouchVideoFrame frame2(2, 2, std::move(videoData2), timestamp2);
204
205 motionArgs.videoFrames = {frame1, frame2};
206
207 // We are not checking the return value, because we can't be making assumptions
208 // about the HAL operation, since it will be highly hardware-dependent
209 if (mMotionClassifier) {
210 ASSERT_NO_FATAL_FAILURE(mMotionClassifier->classify(motionArgs));
211 }
212}
213
214/**
215 * Make sure MotionClassifier does not crash when it is reset.
216 */
217TEST_F(MotionClassifierTest, Reset_DoesNotCrash) {
218 if (mMotionClassifier) {
219 ASSERT_NO_FATAL_FAILURE(mMotionClassifier->reset());
220 }
221}
222
223/**
224 * Make sure MotionClassifier does not crash when a device is reset.
225 */
226TEST_F(MotionClassifierTest, DeviceReset_DoesNotCrash) {
227 NotifyDeviceResetArgs args(1/*sequenceNum*/, 2/*eventTime*/, 3/*deviceId*/);
228 if (mMotionClassifier) {
229 ASSERT_NO_FATAL_FAILURE(mMotionClassifier->reset(args));
230 }
231}
232
233} // namespace android