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> |
| 19 | |
| 20 | #include "TestInputListener.h" |
| 21 | |
| 22 | #include <android/hardware/input/classifier/1.0/IInputClassifier.h> |
| 23 | |
| 24 | using namespace android::hardware::input; |
| 25 | |
| 26 | namespace android { |
| 27 | |
| 28 | // --- InputClassifierTest --- |
| 29 | |
| 30 | static NotifyMotionArgs generateBasicMotionArgs() { |
| 31 | // Create a basic motion event for testing |
Siarhei Vishniakou | fd3718c | 2019-02-28 08:16:26 -0800 | [diff] [blame] | 32 | PointerProperties properties; |
| 33 | properties.id = 0; |
| 34 | properties.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER; |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 35 | |
Siarhei Vishniakou | fd3718c | 2019-02-28 08:16:26 -0800 | [diff] [blame] | 36 | PointerCoords coords; |
| 37 | coords.clear(); |
| 38 | coords.setAxisValue(AMOTION_EVENT_AXIS_X, 1); |
| 39 | coords.setAxisValue(AMOTION_EVENT_AXIS_Y, 1); |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 40 | static constexpr nsecs_t downTime = 2; |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 41 | NotifyMotionArgs motionArgs(1 /*sequenceNum*/, downTime /*eventTime*/, 3 /*deviceId*/, |
| 42 | AINPUT_SOURCE_ANY, ADISPLAY_ID_DEFAULT, 4 /*policyFlags*/, |
| 43 | AMOTION_EVENT_ACTION_DOWN, 0 /*actionButton*/, 0 /*flags*/, |
| 44 | AMETA_NONE, 0 /*buttonState*/, MotionClassification::NONE, |
Atif Niyaz | 21da0ff | 2019-06-28 13:22:51 -0700 | [diff] [blame] | 45 | AMOTION_EVENT_EDGE_FLAG_NONE, 1 /*pointerCount*/, &properties, |
| 46 | &coords, 0 /*xPrecision*/, 0 /*yPrecision*/, |
| 47 | AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 48 | AMOTION_EVENT_INVALID_CURSOR_POSITION, downTime, |
| 49 | {} /*videoFrames*/); |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 50 | return motionArgs; |
| 51 | } |
| 52 | |
| 53 | class InputClassifierTest : public testing::Test { |
| 54 | protected: |
| 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 | */ |
| 73 | TEST_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 | |
| 83 | TEST_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 | */ |
| 100 | TEST_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 | */ |
| 112 | TEST_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 | */ |
| 126 | TEST_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 | |
| 135 | // --- MotionClassifierTest --- |
| 136 | |
| 137 | class MotionClassifierTest : public testing::Test { |
| 138 | protected: |
| 139 | std::unique_ptr<MotionClassifierInterface> mMotionClassifier; |
| 140 | |
| 141 | virtual void SetUp() override { |
Siarhei Vishniakou | 4bdbb6a | 2019-04-11 09:42:09 -0700 | [diff] [blame] | 142 | mMotionClassifier = std::make_unique<MotionClassifier>(); |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 143 | } |
| 144 | }; |
| 145 | |
| 146 | /** |
| 147 | * Since MotionClassifier creates a new thread to communicate with HAL, |
| 148 | * it's not really expected to ever exit. However, for testing purposes, |
| 149 | * we need to ensure that it is able to exit cleanly. |
| 150 | * If the thread is not properly cleaned up, it will generate SIGABRT. |
| 151 | * The logic for exiting the thread and cleaning up the resources is inside |
| 152 | * the destructor. Here, we just make sure the destructor does not crash. |
| 153 | */ |
| 154 | TEST_F(MotionClassifierTest, Destructor_DoesNotCrash) { |
| 155 | mMotionClassifier = nullptr; |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Make sure MotionClassifier can handle events that don't have any |
| 160 | * video frames. |
| 161 | */ |
| 162 | TEST_F(MotionClassifierTest, Classify_NoVideoFrames) { |
| 163 | NotifyMotionArgs motionArgs = generateBasicMotionArgs(); |
| 164 | |
| 165 | // We are not checking the return value, because we can't be making assumptions |
| 166 | // about the HAL operation, since it will be highly hardware-dependent |
Siarhei Vishniakou | 4bdbb6a | 2019-04-11 09:42:09 -0700 | [diff] [blame] | 167 | ASSERT_NO_FATAL_FAILURE(mMotionClassifier->classify(motionArgs)); |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Make sure nothing crashes when a videoFrame is sent. |
| 172 | */ |
| 173 | TEST_F(MotionClassifierTest, Classify_OneVideoFrame) { |
| 174 | NotifyMotionArgs motionArgs = generateBasicMotionArgs(); |
| 175 | |
| 176 | std::vector<int16_t> videoData = {1, 2, 3, 4}; |
| 177 | timeval timestamp = { 1, 1}; |
| 178 | TouchVideoFrame frame(2, 2, std::move(videoData), timestamp); |
| 179 | motionArgs.videoFrames = {frame}; |
| 180 | |
| 181 | // We are not checking the return value, because we can't be making assumptions |
| 182 | // about the HAL operation, since it will be highly hardware-dependent |
Siarhei Vishniakou | 4bdbb6a | 2019-04-11 09:42:09 -0700 | [diff] [blame] | 183 | ASSERT_NO_FATAL_FAILURE(mMotionClassifier->classify(motionArgs)); |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Make sure nothing crashes when 2 videoFrames are sent. |
| 188 | */ |
| 189 | TEST_F(MotionClassifierTest, Classify_TwoVideoFrames) { |
| 190 | NotifyMotionArgs motionArgs = generateBasicMotionArgs(); |
| 191 | |
| 192 | std::vector<int16_t> videoData1 = {1, 2, 3, 4}; |
| 193 | timeval timestamp1 = { 1, 1}; |
| 194 | TouchVideoFrame frame1(2, 2, std::move(videoData1), timestamp1); |
| 195 | |
| 196 | std::vector<int16_t> videoData2 = {6, 6, 6, 6}; |
| 197 | timeval timestamp2 = { 1, 2}; |
| 198 | TouchVideoFrame frame2(2, 2, std::move(videoData2), timestamp2); |
| 199 | |
| 200 | motionArgs.videoFrames = {frame1, frame2}; |
| 201 | |
| 202 | // We are not checking the return value, because we can't be making assumptions |
| 203 | // about the HAL operation, since it will be highly hardware-dependent |
Siarhei Vishniakou | 4bdbb6a | 2019-04-11 09:42:09 -0700 | [diff] [blame] | 204 | ASSERT_NO_FATAL_FAILURE(mMotionClassifier->classify(motionArgs)); |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Make sure MotionClassifier does not crash when it is reset. |
| 209 | */ |
| 210 | TEST_F(MotionClassifierTest, Reset_DoesNotCrash) { |
Siarhei Vishniakou | 4bdbb6a | 2019-04-11 09:42:09 -0700 | [diff] [blame] | 211 | ASSERT_NO_FATAL_FAILURE(mMotionClassifier->reset()); |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Make sure MotionClassifier does not crash when a device is reset. |
| 216 | */ |
| 217 | TEST_F(MotionClassifierTest, DeviceReset_DoesNotCrash) { |
| 218 | NotifyDeviceResetArgs args(1/*sequenceNum*/, 2/*eventTime*/, 3/*deviceId*/); |
Siarhei Vishniakou | 4bdbb6a | 2019-04-11 09:42:09 -0700 | [diff] [blame] | 219 | ASSERT_NO_FATAL_FAILURE(mMotionClassifier->reset(args)); |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | } // namespace android |