| Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 1 | /* | 
|  | 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> | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 19 | #include <gui/constants.h> | 
| Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 20 |  | 
|  | 21 | #include "TestInputListener.h" | 
|  | 22 |  | 
|  | 23 | #include <android/hardware/input/classifier/1.0/IInputClassifier.h> | 
|  | 24 |  | 
|  | 25 | using namespace android::hardware::input; | 
| Siarhei Vishniakou | 1652397 | 2020-03-04 17:48:39 -0800 | [diff] [blame] | 26 | using android::hardware::Return; | 
|  | 27 | using android::hardware::Void; | 
|  | 28 | using android::hardware::input::common::V1_0::Classification; | 
| Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 29 |  | 
|  | 30 | namespace android { | 
|  | 31 |  | 
|  | 32 | // --- InputClassifierTest --- | 
|  | 33 |  | 
|  | 34 | static NotifyMotionArgs generateBasicMotionArgs() { | 
|  | 35 | // Create a basic motion event for testing | 
| Siarhei Vishniakou | fd3718c | 2019-02-28 08:16:26 -0800 | [diff] [blame] | 36 | PointerProperties properties; | 
|  | 37 | properties.id = 0; | 
|  | 38 | properties.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER; | 
| Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 39 |  | 
| Siarhei Vishniakou | fd3718c | 2019-02-28 08:16:26 -0800 | [diff] [blame] | 40 | PointerCoords coords; | 
|  | 41 | coords.clear(); | 
|  | 42 | coords.setAxisValue(AMOTION_EVENT_AXIS_X, 1); | 
|  | 43 | coords.setAxisValue(AMOTION_EVENT_AXIS_Y, 1); | 
| Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 44 | static constexpr nsecs_t downTime = 2; | 
| Siarhei Vishniakou | 58ba3d1 | 2021-02-11 01:31:07 +0000 | [diff] [blame] | 45 | 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 Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 52 | AMOTION_EVENT_INVALID_CURSOR_POSITION, downTime, | 
|  | 53 | {} /*videoFrames*/); | 
| Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 54 | return motionArgs; | 
|  | 55 | } | 
|  | 56 |  | 
|  | 57 | class InputClassifierTest : public testing::Test { | 
|  | 58 | protected: | 
| Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 59 | TestInputListener mTestListener; | 
|  | 60 | std::unique_ptr<InputClassifierInterface> mClassifier; | 
| Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 61 |  | 
| Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 62 | void SetUp() override { mClassifier = std::make_unique<InputClassifier>(mTestListener); } | 
| Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 63 | }; | 
|  | 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 | */ | 
|  | 69 | TEST_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 Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 75 | ASSERT_NO_FATAL_FAILURE(mTestListener.assertNotifyConfigurationChangedWasCalled(&outArgs)); | 
| Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 76 | ASSERT_EQ(args, outArgs); | 
|  | 77 | } | 
|  | 78 |  | 
|  | 79 | TEST_F(InputClassifierTest, SendToNextStage_NotifyKeyArgs) { | 
|  | 80 | // Create a basic key event and send to classifier | 
| Siarhei Vishniakou | 58ba3d1 | 2021-02-11 01:31:07 +0000 | [diff] [blame] | 81 | 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 Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 85 |  | 
|  | 86 | mClassifier->notifyKey(&args); | 
|  | 87 | NotifyKeyArgs outArgs; | 
| Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 88 | ASSERT_NO_FATAL_FAILURE(mTestListener.assertNotifyKeyWasCalled(&outArgs)); | 
| Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 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 | */ | 
|  | 97 | TEST_F(InputClassifierTest, SendToNextStage_NotifyMotionArgs) { | 
|  | 98 | NotifyMotionArgs motionArgs = generateBasicMotionArgs(); | 
|  | 99 | mClassifier->notifyMotion(&motionArgs); | 
|  | 100 | NotifyMotionArgs args; | 
| Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 101 | ASSERT_NO_FATAL_FAILURE(mTestListener.assertNotifyMotionWasCalled(&args)); | 
| Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 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 | */ | 
|  | 109 | TEST_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 Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 115 | ASSERT_NO_FATAL_FAILURE(mTestListener.assertNotifySwitchWasCalled(&outArgs)); | 
| Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 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 | */ | 
|  | 123 | TEST_F(InputClassifierTest, SendToNextStage_NotifyDeviceResetArgs) { | 
|  | 124 | NotifyDeviceResetArgs args(1/*sequenceNum*/, 2/*eventTime*/, 3/*deviceId*/); | 
|  | 125 |  | 
|  | 126 | mClassifier->notifyDeviceReset(&args); | 
|  | 127 | NotifyDeviceResetArgs outArgs; | 
| Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 128 | ASSERT_NO_FATAL_FAILURE(mTestListener.assertNotifyDeviceResetWasCalled(&outArgs)); | 
| Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 129 | ASSERT_EQ(args, outArgs); | 
|  | 130 | } | 
|  | 131 |  | 
| Siarhei Vishniakou | c9ac19e | 2020-03-19 11:55:01 -0700 | [diff] [blame] | 132 | TEST_F(InputClassifierTest, SetMotionClassifier_Enabled) { | 
|  | 133 | mClassifier->setMotionClassifierEnabled(true); | 
|  | 134 | } | 
|  | 135 |  | 
|  | 136 | TEST_F(InputClassifierTest, SetMotionClassifier_Disabled) { | 
|  | 137 | mClassifier->setMotionClassifierEnabled(false); | 
|  | 138 | } | 
|  | 139 |  | 
|  | 140 | /** | 
|  | 141 | * Try to break it by calling setMotionClassifierEnabled multiple times. | 
|  | 142 | */ | 
|  | 143 | TEST_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 Vishniakou | 1652397 | 2020-03-04 17:48:39 -0800 | [diff] [blame] | 154 | /** | 
|  | 155 | * A minimal implementation of IInputClassifier. | 
|  | 156 | */ | 
|  | 157 | struct 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 | */ | 
|  | 169 | class TestDeathRecipient : public android::hardware::hidl_death_recipient { | 
|  | 170 | public: | 
|  | 171 | virtual void serviceDied(uint64_t cookie, | 
|  | 172 | const wp<android::hidl::base::V1_0::IBase>& who) override{}; | 
|  | 173 | }; | 
|  | 174 |  | 
| Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 175 | // --- MotionClassifierTest --- | 
|  | 176 |  | 
|  | 177 | class MotionClassifierTest : public testing::Test { | 
|  | 178 | protected: | 
|  | 179 | std::unique_ptr<MotionClassifierInterface> mMotionClassifier; | 
|  | 180 |  | 
|  | 181 | virtual void SetUp() override { | 
| Siarhei Vishniakou | 1652397 | 2020-03-04 17:48:39 -0800 | [diff] [blame] | 182 | 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 Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 190 | } | 
|  | 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 | */ | 
|  | 201 | TEST_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 | */ | 
|  | 209 | TEST_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 Vishniakou | 4bdbb6a | 2019-04-11 09:42:09 -0700 | [diff] [blame] | 214 | ASSERT_NO_FATAL_FAILURE(mMotionClassifier->classify(motionArgs)); | 
| Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 215 | } | 
|  | 216 |  | 
|  | 217 | /** | 
|  | 218 | * Make sure nothing crashes when a videoFrame is sent. | 
|  | 219 | */ | 
|  | 220 | TEST_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 Vishniakou | 4bdbb6a | 2019-04-11 09:42:09 -0700 | [diff] [blame] | 230 | ASSERT_NO_FATAL_FAILURE(mMotionClassifier->classify(motionArgs)); | 
| Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 231 | } | 
|  | 232 |  | 
|  | 233 | /** | 
|  | 234 | * Make sure nothing crashes when 2 videoFrames are sent. | 
|  | 235 | */ | 
|  | 236 | TEST_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 Vishniakou | 4bdbb6a | 2019-04-11 09:42:09 -0700 | [diff] [blame] | 251 | ASSERT_NO_FATAL_FAILURE(mMotionClassifier->classify(motionArgs)); | 
| Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 252 | } | 
|  | 253 |  | 
|  | 254 | /** | 
|  | 255 | * Make sure MotionClassifier does not crash when it is reset. | 
|  | 256 | */ | 
|  | 257 | TEST_F(MotionClassifierTest, Reset_DoesNotCrash) { | 
| Siarhei Vishniakou | 4bdbb6a | 2019-04-11 09:42:09 -0700 | [diff] [blame] | 258 | ASSERT_NO_FATAL_FAILURE(mMotionClassifier->reset()); | 
| Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 259 | } | 
|  | 260 |  | 
|  | 261 | /** | 
|  | 262 | * Make sure MotionClassifier does not crash when a device is reset. | 
|  | 263 | */ | 
|  | 264 | TEST_F(MotionClassifierTest, DeviceReset_DoesNotCrash) { | 
|  | 265 | NotifyDeviceResetArgs args(1/*sequenceNum*/, 2/*eventTime*/, 3/*deviceId*/); | 
| Siarhei Vishniakou | 4bdbb6a | 2019-04-11 09:42:09 -0700 | [diff] [blame] | 266 | ASSERT_NO_FATAL_FAILURE(mMotionClassifier->reset(args)); | 
| Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 267 | } | 
|  | 268 |  | 
|  | 269 | } // namespace android |