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 | |
Siarhei Vishniakou | 34d6fef | 2022-02-01 19:03:45 -0800 | [diff] [blame^] | 23 | #include <aidl/android/hardware/input/processor/BnInputProcessor.h> |
| 24 | #include <aidl/android/hardware/input/processor/IInputProcessor.h> |
| 25 | #include <android/binder_manager.h> |
| 26 | #include <android/binder_process.h> |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 27 | |
Siarhei Vishniakou | 34d6fef | 2022-02-01 19:03:45 -0800 | [diff] [blame^] | 28 | using namespace aidl::android::hardware::input; |
| 29 | using aidl::android::hardware::input::common::Classification; |
| 30 | using aidl::android::hardware::input::processor::IInputProcessor; |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 31 | |
| 32 | namespace android { |
| 33 | |
| 34 | // --- InputClassifierTest --- |
| 35 | |
| 36 | static NotifyMotionArgs generateBasicMotionArgs() { |
| 37 | // Create a basic motion event for testing |
Siarhei Vishniakou | fd3718c | 2019-02-28 08:16:26 -0800 | [diff] [blame] | 38 | PointerProperties properties; |
| 39 | properties.id = 0; |
| 40 | properties.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER; |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 41 | |
Siarhei Vishniakou | fd3718c | 2019-02-28 08:16:26 -0800 | [diff] [blame] | 42 | PointerCoords coords; |
| 43 | coords.clear(); |
| 44 | coords.setAxisValue(AMOTION_EVENT_AXIS_X, 1); |
| 45 | coords.setAxisValue(AMOTION_EVENT_AXIS_Y, 1); |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 46 | static constexpr nsecs_t downTime = 2; |
Siarhei Vishniakou | 58ba3d1 | 2021-02-11 01:31:07 +0000 | [diff] [blame] | 47 | NotifyMotionArgs motionArgs(1 /*sequenceNum*/, downTime /*eventTime*/, 2 /*readTime*/, |
| 48 | 3 /*deviceId*/, AINPUT_SOURCE_ANY, ADISPLAY_ID_DEFAULT, |
| 49 | 4 /*policyFlags*/, AMOTION_EVENT_ACTION_DOWN, 0 /*actionButton*/, |
| 50 | 0 /*flags*/, AMETA_NONE, 0 /*buttonState*/, |
| 51 | MotionClassification::NONE, AMOTION_EVENT_EDGE_FLAG_NONE, |
| 52 | 1 /*pointerCount*/, &properties, &coords, 0 /*xPrecision*/, |
| 53 | 0 /*yPrecision*/, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 54 | AMOTION_EVENT_INVALID_CURSOR_POSITION, downTime, |
| 55 | {} /*videoFrames*/); |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 56 | return motionArgs; |
| 57 | } |
| 58 | |
| 59 | class InputClassifierTest : public testing::Test { |
| 60 | protected: |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 61 | TestInputListener mTestListener; |
| 62 | std::unique_ptr<InputClassifierInterface> mClassifier; |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 63 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 64 | void SetUp() override { mClassifier = std::make_unique<InputClassifier>(mTestListener); } |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 65 | }; |
| 66 | |
| 67 | /** |
| 68 | * Create a basic configuration change and send it to input classifier. |
| 69 | * Expect that the event is received by the next input stage, unmodified. |
| 70 | */ |
| 71 | TEST_F(InputClassifierTest, SendToNextStage_NotifyConfigurationChangedArgs) { |
| 72 | // Create a basic configuration change and send to classifier |
| 73 | NotifyConfigurationChangedArgs args(1/*sequenceNum*/, 2/*eventTime*/); |
| 74 | |
| 75 | mClassifier->notifyConfigurationChanged(&args); |
| 76 | NotifyConfigurationChangedArgs outArgs; |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 77 | ASSERT_NO_FATAL_FAILURE(mTestListener.assertNotifyConfigurationChangedWasCalled(&outArgs)); |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 78 | ASSERT_EQ(args, outArgs); |
| 79 | } |
| 80 | |
| 81 | TEST_F(InputClassifierTest, SendToNextStage_NotifyKeyArgs) { |
| 82 | // Create a basic key event and send to classifier |
Siarhei Vishniakou | 58ba3d1 | 2021-02-11 01:31:07 +0000 | [diff] [blame] | 83 | NotifyKeyArgs args(1 /*sequenceNum*/, 2 /*eventTime*/, 21 /*readTime*/, 3 /*deviceId*/, |
| 84 | AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_DEFAULT, 0 /*policyFlags*/, |
| 85 | AKEY_EVENT_ACTION_DOWN, 4 /*flags*/, AKEYCODE_HOME, 5 /*scanCode*/, |
| 86 | AMETA_NONE, 6 /*downTime*/); |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 87 | |
| 88 | mClassifier->notifyKey(&args); |
| 89 | NotifyKeyArgs outArgs; |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 90 | ASSERT_NO_FATAL_FAILURE(mTestListener.assertNotifyKeyWasCalled(&outArgs)); |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 91 | ASSERT_EQ(args, outArgs); |
| 92 | } |
| 93 | |
| 94 | |
| 95 | /** |
| 96 | * Create a basic motion event and send it to input classifier. |
| 97 | * Expect that the event is received by the next input stage, unmodified. |
| 98 | */ |
| 99 | TEST_F(InputClassifierTest, SendToNextStage_NotifyMotionArgs) { |
| 100 | NotifyMotionArgs motionArgs = generateBasicMotionArgs(); |
| 101 | mClassifier->notifyMotion(&motionArgs); |
| 102 | NotifyMotionArgs args; |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 103 | ASSERT_NO_FATAL_FAILURE(mTestListener.assertNotifyMotionWasCalled(&args)); |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 104 | ASSERT_EQ(motionArgs, args); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Create a basic switch event and send it to input classifier. |
| 109 | * Expect that the event is received by the next input stage, unmodified. |
| 110 | */ |
| 111 | TEST_F(InputClassifierTest, SendToNextStage_NotifySwitchArgs) { |
| 112 | NotifySwitchArgs args(1/*sequenceNum*/, 2/*eventTime*/, 3/*policyFlags*/, 4/*switchValues*/, |
| 113 | 5/*switchMask*/); |
| 114 | |
| 115 | mClassifier->notifySwitch(&args); |
| 116 | NotifySwitchArgs outArgs; |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 117 | ASSERT_NO_FATAL_FAILURE(mTestListener.assertNotifySwitchWasCalled(&outArgs)); |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 118 | ASSERT_EQ(args, outArgs); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Create a basic device reset event and send it to input classifier. |
| 123 | * Expect that the event is received by the next input stage, unmodified. |
| 124 | */ |
| 125 | TEST_F(InputClassifierTest, SendToNextStage_NotifyDeviceResetArgs) { |
| 126 | NotifyDeviceResetArgs args(1/*sequenceNum*/, 2/*eventTime*/, 3/*deviceId*/); |
| 127 | |
| 128 | mClassifier->notifyDeviceReset(&args); |
| 129 | NotifyDeviceResetArgs outArgs; |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 130 | ASSERT_NO_FATAL_FAILURE(mTestListener.assertNotifyDeviceResetWasCalled(&outArgs)); |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 131 | ASSERT_EQ(args, outArgs); |
| 132 | } |
| 133 | |
Siarhei Vishniakou | c9ac19e | 2020-03-19 11:55:01 -0700 | [diff] [blame] | 134 | TEST_F(InputClassifierTest, SetMotionClassifier_Enabled) { |
| 135 | mClassifier->setMotionClassifierEnabled(true); |
| 136 | } |
| 137 | |
| 138 | TEST_F(InputClassifierTest, SetMotionClassifier_Disabled) { |
| 139 | mClassifier->setMotionClassifierEnabled(false); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Try to break it by calling setMotionClassifierEnabled multiple times. |
| 144 | */ |
| 145 | TEST_F(InputClassifierTest, SetMotionClassifier_Multiple) { |
| 146 | mClassifier->setMotionClassifierEnabled(true); |
| 147 | mClassifier->setMotionClassifierEnabled(true); |
| 148 | mClassifier->setMotionClassifierEnabled(true); |
| 149 | mClassifier->setMotionClassifierEnabled(false); |
| 150 | mClassifier->setMotionClassifierEnabled(false); |
| 151 | mClassifier->setMotionClassifierEnabled(true); |
| 152 | mClassifier->setMotionClassifierEnabled(true); |
| 153 | mClassifier->setMotionClassifierEnabled(true); |
| 154 | } |
| 155 | |
Siarhei Vishniakou | 1652397 | 2020-03-04 17:48:39 -0800 | [diff] [blame] | 156 | /** |
| 157 | * A minimal implementation of IInputClassifier. |
| 158 | */ |
Siarhei Vishniakou | 34d6fef | 2022-02-01 19:03:45 -0800 | [diff] [blame^] | 159 | class TestHal : public aidl::android::hardware::input::processor::BnInputProcessor { |
| 160 | ::ndk::ScopedAStatus classify( |
| 161 | const ::aidl::android::hardware::input::common::MotionEvent& in_event, |
| 162 | ::aidl::android::hardware::input::common::Classification* _aidl_return) override { |
| 163 | *_aidl_return = Classification::NONE; |
| 164 | return ndk::ScopedAStatus::ok(); |
| 165 | } |
| 166 | ::ndk::ScopedAStatus reset() override { return ndk::ScopedAStatus::ok(); } |
| 167 | ::ndk::ScopedAStatus resetDevice(int32_t in_deviceId) override { |
| 168 | return ndk::ScopedAStatus::ok(); |
| 169 | } |
Siarhei Vishniakou | 1652397 | 2020-03-04 17:48:39 -0800 | [diff] [blame] | 170 | }; |
| 171 | |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 172 | // --- MotionClassifierTest --- |
| 173 | |
| 174 | class MotionClassifierTest : public testing::Test { |
| 175 | protected: |
| 176 | std::unique_ptr<MotionClassifierInterface> mMotionClassifier; |
| 177 | |
Siarhei Vishniakou | 34d6fef | 2022-02-01 19:03:45 -0800 | [diff] [blame^] | 178 | void SetUp() override { |
| 179 | std::shared_ptr<IInputProcessor> service = ndk::SharedRefBase::make<TestHal>(); |
| 180 | mMotionClassifier = MotionClassifier::create(std::move(service)); |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 181 | } |
| 182 | }; |
| 183 | |
| 184 | /** |
| 185 | * Since MotionClassifier creates a new thread to communicate with HAL, |
| 186 | * it's not really expected to ever exit. However, for testing purposes, |
| 187 | * we need to ensure that it is able to exit cleanly. |
| 188 | * If the thread is not properly cleaned up, it will generate SIGABRT. |
| 189 | * The logic for exiting the thread and cleaning up the resources is inside |
| 190 | * the destructor. Here, we just make sure the destructor does not crash. |
| 191 | */ |
| 192 | TEST_F(MotionClassifierTest, Destructor_DoesNotCrash) { |
| 193 | mMotionClassifier = nullptr; |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Make sure MotionClassifier can handle events that don't have any |
| 198 | * video frames. |
| 199 | */ |
| 200 | TEST_F(MotionClassifierTest, Classify_NoVideoFrames) { |
| 201 | NotifyMotionArgs motionArgs = generateBasicMotionArgs(); |
| 202 | |
| 203 | // We are not checking the return value, because we can't be making assumptions |
| 204 | // about the HAL operation, since it will be highly hardware-dependent |
Siarhei Vishniakou | 4bdbb6a | 2019-04-11 09:42:09 -0700 | [diff] [blame] | 205 | ASSERT_NO_FATAL_FAILURE(mMotionClassifier->classify(motionArgs)); |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Make sure nothing crashes when a videoFrame is sent. |
| 210 | */ |
| 211 | TEST_F(MotionClassifierTest, Classify_OneVideoFrame) { |
| 212 | NotifyMotionArgs motionArgs = generateBasicMotionArgs(); |
| 213 | |
| 214 | std::vector<int16_t> videoData = {1, 2, 3, 4}; |
| 215 | timeval timestamp = { 1, 1}; |
| 216 | TouchVideoFrame frame(2, 2, std::move(videoData), timestamp); |
| 217 | motionArgs.videoFrames = {frame}; |
| 218 | |
| 219 | // We are not checking the return value, because we can't be making assumptions |
| 220 | // about the HAL operation, since it will be highly hardware-dependent |
Siarhei Vishniakou | 4bdbb6a | 2019-04-11 09:42:09 -0700 | [diff] [blame] | 221 | ASSERT_NO_FATAL_FAILURE(mMotionClassifier->classify(motionArgs)); |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Make sure nothing crashes when 2 videoFrames are sent. |
| 226 | */ |
| 227 | TEST_F(MotionClassifierTest, Classify_TwoVideoFrames) { |
| 228 | NotifyMotionArgs motionArgs = generateBasicMotionArgs(); |
| 229 | |
| 230 | std::vector<int16_t> videoData1 = {1, 2, 3, 4}; |
| 231 | timeval timestamp1 = { 1, 1}; |
| 232 | TouchVideoFrame frame1(2, 2, std::move(videoData1), timestamp1); |
| 233 | |
| 234 | std::vector<int16_t> videoData2 = {6, 6, 6, 6}; |
| 235 | timeval timestamp2 = { 1, 2}; |
| 236 | TouchVideoFrame frame2(2, 2, std::move(videoData2), timestamp2); |
| 237 | |
| 238 | motionArgs.videoFrames = {frame1, frame2}; |
| 239 | |
| 240 | // We are not checking the return value, because we can't be making assumptions |
| 241 | // about the HAL operation, since it will be highly hardware-dependent |
Siarhei Vishniakou | 4bdbb6a | 2019-04-11 09:42:09 -0700 | [diff] [blame] | 242 | ASSERT_NO_FATAL_FAILURE(mMotionClassifier->classify(motionArgs)); |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | /** |
| 246 | * Make sure MotionClassifier does not crash when it is reset. |
| 247 | */ |
| 248 | TEST_F(MotionClassifierTest, Reset_DoesNotCrash) { |
Siarhei Vishniakou | 4bdbb6a | 2019-04-11 09:42:09 -0700 | [diff] [blame] | 249 | ASSERT_NO_FATAL_FAILURE(mMotionClassifier->reset()); |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | /** |
| 253 | * Make sure MotionClassifier does not crash when a device is reset. |
| 254 | */ |
| 255 | TEST_F(MotionClassifierTest, DeviceReset_DoesNotCrash) { |
| 256 | NotifyDeviceResetArgs args(1/*sequenceNum*/, 2/*eventTime*/, 3/*deviceId*/); |
Siarhei Vishniakou | 4bdbb6a | 2019-04-11 09:42:09 -0700 | [diff] [blame] | 257 | ASSERT_NO_FATAL_FAILURE(mMotionClassifier->reset(args)); |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | } // namespace android |