| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2010 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 "TestHelpers.h" | 
|  | 18 |  | 
| chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 19 | #include <attestation/HmacKeyManager.h> | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 20 | #include <gtest/gtest.h> | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 21 | #include <gui/constants.h> | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 22 | #include <input/InputTransport.h> | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 23 |  | 
| Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 24 | using android::base::Result; | 
|  | 25 |  | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 26 | namespace android { | 
|  | 27 |  | 
| Siarhei Vishniakou | 6252af7 | 2023-09-20 09:00:38 -0700 | [diff] [blame] | 28 | namespace { | 
|  | 29 |  | 
|  | 30 | static constexpr float EPSILON = MotionEvent::ROUNDING_PRECISION; | 
|  | 31 | static constexpr int32_t POINTER_1_DOWN = | 
|  | 32 | AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); | 
|  | 33 | static constexpr int32_t POINTER_2_DOWN = | 
|  | 34 | AMOTION_EVENT_ACTION_POINTER_DOWN | (2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); | 
|  | 35 |  | 
|  | 36 | struct Pointer { | 
|  | 37 | int32_t id; | 
|  | 38 | float x; | 
|  | 39 | float y; | 
|  | 40 | bool isResampled = false; | 
|  | 41 | }; | 
|  | 42 |  | 
|  | 43 | } // namespace | 
| Prabir Pradhan | 00e029d | 2023-03-09 20:11:09 +0000 | [diff] [blame] | 44 |  | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 45 | class InputPublisherAndConsumerTest : public testing::Test { | 
|  | 46 | protected: | 
| Siarhei Vishniakou | d258827 | 2020-07-10 11:15:40 -0500 | [diff] [blame] | 47 | std::shared_ptr<InputChannel> mServerChannel, mClientChannel; | 
|  | 48 | std::unique_ptr<InputPublisher> mPublisher; | 
|  | 49 | std::unique_ptr<InputConsumer> mConsumer; | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 50 | PreallocatedInputEventFactory mEventFactory; | 
|  | 51 |  | 
| Siarhei Vishniakou | d258827 | 2020-07-10 11:15:40 -0500 | [diff] [blame] | 52 | void SetUp() override { | 
|  | 53 | std::unique_ptr<InputChannel> serverChannel, clientChannel; | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 54 | status_t result = InputChannel::openInputChannelPair("channel name", | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 55 | serverChannel, clientChannel); | 
| Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 56 | ASSERT_EQ(OK, result); | 
| Siarhei Vishniakou | d258827 | 2020-07-10 11:15:40 -0500 | [diff] [blame] | 57 | mServerChannel = std::move(serverChannel); | 
|  | 58 | mClientChannel = std::move(clientChannel); | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 59 |  | 
| Siarhei Vishniakou | d258827 | 2020-07-10 11:15:40 -0500 | [diff] [blame] | 60 | mPublisher = std::make_unique<InputPublisher>(mServerChannel); | 
|  | 61 | mConsumer = std::make_unique<InputConsumer>(mClientChannel); | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 62 | } | 
|  | 63 |  | 
| Siarhei Vishniakou | 6252af7 | 2023-09-20 09:00:38 -0700 | [diff] [blame] | 64 | void publishAndConsumeKeyEvent(); | 
|  | 65 | void publishAndConsumeMotionStream(); | 
|  | 66 | void publishAndConsumeFocusEvent(); | 
|  | 67 | void publishAndConsumeCaptureEvent(); | 
|  | 68 | void publishAndConsumeDragEvent(); | 
|  | 69 | void publishAndConsumeTouchModeEvent(); | 
|  | 70 | void publishAndConsumeMotionEvent(int32_t action, nsecs_t downTime, | 
|  | 71 | const std::vector<Pointer>& pointers); | 
|  | 72 |  | 
|  | 73 | private: | 
|  | 74 | // The sequence number to use when publishing the next event | 
|  | 75 | uint32_t mSeq = 1; | 
|  | 76 |  | 
|  | 77 | void publishAndConsumeMotionEvent( | 
|  | 78 | int32_t deviceId, uint32_t source, int32_t displayId, std::array<uint8_t, 32> hmac, | 
|  | 79 | int32_t action, int32_t actionButton, int32_t flags, int32_t edgeFlags, | 
|  | 80 | int32_t metaState, int32_t buttonState, MotionClassification classification, | 
|  | 81 | float xScale, float yScale, float xOffset, float yOffset, float xPrecision, | 
|  | 82 | float yPrecision, float xCursorPosition, float yCursorPosition, float rawXScale, | 
|  | 83 | float rawYScale, float rawXOffset, float rawYOffset, nsecs_t downTime, | 
|  | 84 | nsecs_t eventTime, const std::vector<PointerProperties>& pointerProperties, | 
|  | 85 | const std::vector<PointerCoords>& pointerCoords); | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 86 | }; | 
|  | 87 |  | 
|  | 88 | TEST_F(InputPublisherAndConsumerTest, GetChannel_ReturnsTheChannel) { | 
| Siarhei Vishniakou | a1188f5 | 2020-10-20 20:14:52 -0500 | [diff] [blame] | 89 | ASSERT_NE(nullptr, mPublisher->getChannel()); | 
|  | 90 | ASSERT_NE(nullptr, mConsumer->getChannel()); | 
| Siarhei Vishniakou | d258827 | 2020-07-10 11:15:40 -0500 | [diff] [blame] | 91 | EXPECT_EQ(mServerChannel.get(), mPublisher->getChannel().get()); | 
|  | 92 | EXPECT_EQ(mClientChannel.get(), mConsumer->getChannel().get()); | 
| Siarhei Vishniakou | a1188f5 | 2020-10-20 20:14:52 -0500 | [diff] [blame] | 93 | ASSERT_EQ(mPublisher->getChannel()->getConnectionToken(), | 
|  | 94 | mConsumer->getChannel()->getConnectionToken()); | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 95 | } | 
|  | 96 |  | 
| Siarhei Vishniakou | 6252af7 | 2023-09-20 09:00:38 -0700 | [diff] [blame] | 97 | void InputPublisherAndConsumerTest::publishAndConsumeKeyEvent() { | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 98 | status_t status; | 
|  | 99 |  | 
| Siarhei Vishniakou | 6252af7 | 2023-09-20 09:00:38 -0700 | [diff] [blame] | 100 | const uint32_t seq = mSeq++; | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 101 | int32_t eventId = InputEvent::nextId(); | 
| Siarhei Vishniakou | a62a8dd | 2018-06-08 21:17:33 +0100 | [diff] [blame] | 102 | constexpr int32_t deviceId = 1; | 
| Siarhei Vishniakou | 3826d47 | 2020-01-27 10:44:40 -0600 | [diff] [blame] | 103 | constexpr uint32_t source = AINPUT_SOURCE_KEYBOARD; | 
| Siarhei Vishniakou | a62a8dd | 2018-06-08 21:17:33 +0100 | [diff] [blame] | 104 | constexpr int32_t displayId = ADISPLAY_ID_DEFAULT; | 
| Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 105 | constexpr std::array<uint8_t, 32> hmac = {31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, | 
|  | 106 | 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, | 
|  | 107 | 9,  8,  7,  6,  5,  4,  3,  2,  1,  0}; | 
| Siarhei Vishniakou | a62a8dd | 2018-06-08 21:17:33 +0100 | [diff] [blame] | 108 | constexpr int32_t action = AKEY_EVENT_ACTION_DOWN; | 
|  | 109 | constexpr int32_t flags = AKEY_EVENT_FLAG_FROM_SYSTEM; | 
|  | 110 | constexpr int32_t keyCode = AKEYCODE_ENTER; | 
|  | 111 | constexpr int32_t scanCode = 13; | 
|  | 112 | constexpr int32_t metaState = AMETA_ALT_LEFT_ON | AMETA_ALT_ON; | 
|  | 113 | constexpr int32_t repeatCount = 1; | 
|  | 114 | constexpr nsecs_t downTime = 3; | 
|  | 115 | constexpr nsecs_t eventTime = 4; | 
| Siarhei Vishniakou | 3531ae7 | 2021-02-02 12:12:27 -1000 | [diff] [blame] | 116 | const nsecs_t publishTime = systemTime(SYSTEM_TIME_MONOTONIC); | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 117 |  | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 118 | status = mPublisher->publishKeyEvent(seq, eventId, deviceId, source, displayId, hmac, action, | 
|  | 119 | flags, keyCode, scanCode, metaState, repeatCount, downTime, | 
| Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 120 | eventTime); | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 121 | ASSERT_EQ(OK, status) | 
|  | 122 | << "publisher publishKeyEvent should return OK"; | 
|  | 123 |  | 
|  | 124 | uint32_t consumeSeq; | 
|  | 125 | InputEvent* event; | 
| Harry Cutts | 82c791c | 2023-03-10 17:15:07 +0000 | [diff] [blame] | 126 | status = mConsumer->consume(&mEventFactory, /*consumeBatches=*/true, -1, &consumeSeq, &event); | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 127 | ASSERT_EQ(OK, status) | 
|  | 128 | << "consumer consume should return OK"; | 
|  | 129 |  | 
| Yi Kong | 5bed83b | 2018-07-17 12:53:47 -0700 | [diff] [blame] | 130 | ASSERT_TRUE(event != nullptr) | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 131 | << "consumer should have returned non-NULL event"; | 
| Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 132 | ASSERT_EQ(InputEventType::KEY, event->getType()) << "consumer should have returned a key event"; | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 133 |  | 
|  | 134 | KeyEvent* keyEvent = static_cast<KeyEvent*>(event); | 
|  | 135 | EXPECT_EQ(seq, consumeSeq); | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 136 | EXPECT_EQ(eventId, keyEvent->getId()); | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 137 | EXPECT_EQ(deviceId, keyEvent->getDeviceId()); | 
|  | 138 | EXPECT_EQ(source, keyEvent->getSource()); | 
| Siarhei Vishniakou | a62a8dd | 2018-06-08 21:17:33 +0100 | [diff] [blame] | 139 | EXPECT_EQ(displayId, keyEvent->getDisplayId()); | 
| Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 140 | EXPECT_EQ(hmac, keyEvent->getHmac()); | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 141 | EXPECT_EQ(action, keyEvent->getAction()); | 
|  | 142 | EXPECT_EQ(flags, keyEvent->getFlags()); | 
|  | 143 | EXPECT_EQ(keyCode, keyEvent->getKeyCode()); | 
|  | 144 | EXPECT_EQ(scanCode, keyEvent->getScanCode()); | 
|  | 145 | EXPECT_EQ(metaState, keyEvent->getMetaState()); | 
|  | 146 | EXPECT_EQ(repeatCount, keyEvent->getRepeatCount()); | 
|  | 147 | EXPECT_EQ(downTime, keyEvent->getDownTime()); | 
|  | 148 | EXPECT_EQ(eventTime, keyEvent->getEventTime()); | 
|  | 149 |  | 
|  | 150 | status = mConsumer->sendFinishedSignal(seq, true); | 
|  | 151 | ASSERT_EQ(OK, status) | 
|  | 152 | << "consumer sendFinishedSignal should return OK"; | 
|  | 153 |  | 
| Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 154 | Result<InputPublisher::ConsumerResponse> result = mPublisher->receiveConsumerResponse(); | 
|  | 155 | ASSERT_TRUE(result.ok()) << "receiveConsumerResponse should return OK"; | 
|  | 156 | ASSERT_TRUE(std::holds_alternative<InputPublisher::Finished>(*result)); | 
|  | 157 | const InputPublisher::Finished& finish = std::get<InputPublisher::Finished>(*result); | 
|  | 158 | ASSERT_EQ(seq, finish.seq) | 
|  | 159 | << "receiveConsumerResponse should have returned the original sequence number"; | 
|  | 160 | ASSERT_TRUE(finish.handled) | 
|  | 161 | << "receiveConsumerResponse should have set handled to consumer's reply"; | 
|  | 162 | ASSERT_GE(finish.consumeTime, publishTime) | 
| Siarhei Vishniakou | 3531ae7 | 2021-02-02 12:12:27 -1000 | [diff] [blame] | 163 | << "finished signal's consume time should be greater than publish time"; | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 164 | } | 
|  | 165 |  | 
| Siarhei Vishniakou | 6252af7 | 2023-09-20 09:00:38 -0700 | [diff] [blame] | 166 | void InputPublisherAndConsumerTest::publishAndConsumeMotionStream() { | 
|  | 167 | const nsecs_t downTime = systemTime(SYSTEM_TIME_MONOTONIC); | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 168 |  | 
| Siarhei Vishniakou | 6252af7 | 2023-09-20 09:00:38 -0700 | [diff] [blame] | 169 | publishAndConsumeMotionEvent(AMOTION_EVENT_ACTION_DOWN, downTime, | 
|  | 170 | {Pointer{.id = 0, .x = 20, .y = 30}}); | 
|  | 171 |  | 
|  | 172 | publishAndConsumeMotionEvent(POINTER_1_DOWN, downTime, | 
|  | 173 | {Pointer{.id = 0, .x = 20, .y = 30}, | 
|  | 174 | Pointer{.id = 1, .x = 200, .y = 300}}); | 
|  | 175 |  | 
|  | 176 | publishAndConsumeMotionEvent(POINTER_2_DOWN, downTime, | 
|  | 177 | {Pointer{.id = 0, .x = 20, .y = 30}, | 
|  | 178 | Pointer{.id = 1, .x = 200, .y = 300}, | 
|  | 179 | Pointer{.id = 2, .x = 300, .y = 400}}); | 
|  | 180 |  | 
|  | 181 | // Provide a consistent input stream - cancel the gesture that was started above | 
|  | 182 | publishAndConsumeMotionEvent(AMOTION_EVENT_ACTION_CANCEL, downTime, | 
|  | 183 | {Pointer{.id = 0, .x = 20, .y = 30}, | 
|  | 184 | Pointer{.id = 1, .x = 200, .y = 300}, | 
|  | 185 | Pointer{.id = 2, .x = 300, .y = 400}}); | 
|  | 186 | } | 
|  | 187 |  | 
|  | 188 | void InputPublisherAndConsumerTest::publishAndConsumeMotionEvent( | 
|  | 189 | int32_t action, nsecs_t downTime, const std::vector<Pointer>& pointers) { | 
| Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 190 | constexpr int32_t deviceId = 1; | 
| Siarhei Vishniakou | 3826d47 | 2020-01-27 10:44:40 -0600 | [diff] [blame] | 191 | constexpr uint32_t source = AINPUT_SOURCE_TOUCHSCREEN; | 
| Siarhei Vishniakou | a62a8dd | 2018-06-08 21:17:33 +0100 | [diff] [blame] | 192 | constexpr int32_t displayId = ADISPLAY_ID_DEFAULT; | 
| Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 193 | constexpr std::array<uint8_t, 32> hmac = {0,  1,  2,  3,  4,  5,  6,  7,  8,  9,  10, | 
|  | 194 | 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, | 
|  | 195 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}; | 
| Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 196 | constexpr int32_t actionButton = 0; | 
| Siarhei Vishniakou | 6252af7 | 2023-09-20 09:00:38 -0700 | [diff] [blame] | 197 | int32_t flags = AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED; | 
|  | 198 |  | 
|  | 199 | if (action == AMOTION_EVENT_ACTION_CANCEL) { | 
|  | 200 | flags |= AMOTION_EVENT_FLAG_CANCELED; | 
|  | 201 | } | 
|  | 202 | const size_t pointerCount = pointers.size(); | 
| Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 203 | constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_TOP; | 
|  | 204 | constexpr int32_t metaState = AMETA_ALT_LEFT_ON | AMETA_ALT_ON; | 
|  | 205 | constexpr int32_t buttonState = AMOTION_EVENT_BUTTON_PRIMARY; | 
| Siarhei Vishniakou | 16a2e30 | 2019-01-14 19:21:45 -0800 | [diff] [blame] | 206 | constexpr MotionClassification classification = MotionClassification::AMBIGUOUS_GESTURE; | 
| Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 207 | constexpr float xScale = 2; | 
|  | 208 | constexpr float yScale = 3; | 
| Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 209 | constexpr float xOffset = -10; | 
|  | 210 | constexpr float yOffset = -20; | 
| Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 211 | constexpr float rawXScale = 4; | 
|  | 212 | constexpr float rawYScale = -5; | 
|  | 213 | constexpr float rawXOffset = -11; | 
|  | 214 | constexpr float rawYOffset = 42; | 
| Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 215 | constexpr float xPrecision = 0.25; | 
|  | 216 | constexpr float yPrecision = 0.5; | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 217 | constexpr float xCursorPosition = 1.3; | 
|  | 218 | constexpr float yCursorPosition = 50.6; | 
| Siarhei Vishniakou | 6252af7 | 2023-09-20 09:00:38 -0700 | [diff] [blame] | 219 |  | 
|  | 220 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC); | 
|  | 221 | std::vector<PointerProperties> pointerProperties; | 
|  | 222 | std::vector<PointerCoords> pointerCoords; | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 223 | for (size_t i = 0; i < pointerCount; i++) { | 
| Siarhei Vishniakou | 6252af7 | 2023-09-20 09:00:38 -0700 | [diff] [blame] | 224 | pointerProperties.push_back({}); | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 225 | pointerProperties[i].clear(); | 
| Siarhei Vishniakou | 6252af7 | 2023-09-20 09:00:38 -0700 | [diff] [blame] | 226 | pointerProperties[i].id = pointers[i].id; | 
| Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 227 | pointerProperties[i].toolType = ToolType::FINGER; | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 228 |  | 
| Siarhei Vishniakou | 6252af7 | 2023-09-20 09:00:38 -0700 | [diff] [blame] | 229 | pointerCoords.push_back({}); | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 230 | pointerCoords[i].clear(); | 
| Siarhei Vishniakou | 6252af7 | 2023-09-20 09:00:38 -0700 | [diff] [blame] | 231 | pointerCoords[i].isResampled = pointers[i].isResampled; | 
|  | 232 | pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, pointers[i].x); | 
|  | 233 | pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, pointers[i].y); | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 234 | pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 0.5 * i); | 
|  | 235 | pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_SIZE, 0.7 * i); | 
|  | 236 | pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, 1.5 * i); | 
|  | 237 | pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, 1.7 * i); | 
|  | 238 | pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, 2.5 * i); | 
|  | 239 | pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, 2.7 * i); | 
|  | 240 | pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, 3.5 * i); | 
|  | 241 | } | 
|  | 242 |  | 
| Siarhei Vishniakou | 6252af7 | 2023-09-20 09:00:38 -0700 | [diff] [blame] | 243 | publishAndConsumeMotionEvent(deviceId, source, displayId, hmac, action, actionButton, flags, | 
|  | 244 | edgeFlags, metaState, buttonState, classification, xScale, yScale, | 
|  | 245 | xOffset, yOffset, xPrecision, yPrecision, xCursorPosition, | 
|  | 246 | yCursorPosition, rawXScale, rawYScale, rawXOffset, rawYOffset, | 
|  | 247 | downTime, eventTime, pointerProperties, pointerCoords); | 
|  | 248 | } | 
|  | 249 |  | 
|  | 250 | void InputPublisherAndConsumerTest::publishAndConsumeMotionEvent( | 
|  | 251 | int32_t deviceId, uint32_t source, int32_t displayId, std::array<uint8_t, 32> hmac, | 
|  | 252 | int32_t action, int32_t actionButton, int32_t flags, int32_t edgeFlags, int32_t metaState, | 
|  | 253 | int32_t buttonState, MotionClassification classification, float xScale, float yScale, | 
|  | 254 | float xOffset, float yOffset, float xPrecision, float yPrecision, float xCursorPosition, | 
|  | 255 | float yCursorPosition, float rawXScale, float rawYScale, float rawXOffset, float rawYOffset, | 
|  | 256 | nsecs_t downTime, nsecs_t eventTime, | 
|  | 257 | const std::vector<PointerProperties>& pointerProperties, | 
|  | 258 | const std::vector<PointerCoords>& pointerCoords) { | 
|  | 259 | const uint32_t seq = mSeq++; | 
|  | 260 | const int32_t eventId = InputEvent::nextId(); | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 261 | ui::Transform transform; | 
|  | 262 | transform.set({xScale, 0, xOffset, 0, yScale, yOffset, 0, 0, 1}); | 
| Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 263 | ui::Transform rawTransform; | 
|  | 264 | rawTransform.set({rawXScale, 0, rawXOffset, 0, rawYScale, rawYOffset, 0, 0, 1}); | 
| Siarhei Vishniakou | 6252af7 | 2023-09-20 09:00:38 -0700 | [diff] [blame] | 265 |  | 
|  | 266 | status_t status; | 
|  | 267 | ASSERT_EQ(pointerProperties.size(), pointerCoords.size()); | 
|  | 268 | const size_t pointerCount = pointerProperties.size(); | 
|  | 269 | const nsecs_t publishTime = systemTime(SYSTEM_TIME_MONOTONIC); | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 270 | status = mPublisher->publishMotionEvent(seq, eventId, deviceId, source, displayId, hmac, action, | 
| Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 271 | actionButton, flags, edgeFlags, metaState, buttonState, | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 272 | classification, transform, xPrecision, yPrecision, | 
| Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 273 | xCursorPosition, yCursorPosition, rawTransform, | 
| Siarhei Vishniakou | 6252af7 | 2023-09-20 09:00:38 -0700 | [diff] [blame] | 274 | downTime, eventTime, pointerCount, | 
|  | 275 | pointerProperties.data(), pointerCoords.data()); | 
|  | 276 | ASSERT_EQ(OK, status) << "publisher publishMotionEvent should return OK"; | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 277 |  | 
|  | 278 | uint32_t consumeSeq; | 
|  | 279 | InputEvent* event; | 
| Harry Cutts | 82c791c | 2023-03-10 17:15:07 +0000 | [diff] [blame] | 280 | status = mConsumer->consume(&mEventFactory, /*consumeBatches=*/true, -1, &consumeSeq, &event); | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 281 | ASSERT_EQ(OK, status) | 
|  | 282 | << "consumer consume should return OK"; | 
|  | 283 |  | 
| Yi Kong | 5bed83b | 2018-07-17 12:53:47 -0700 | [diff] [blame] | 284 | ASSERT_TRUE(event != nullptr) | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 285 | << "consumer should have returned non-NULL event"; | 
| Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 286 | ASSERT_EQ(InputEventType::MOTION, event->getType()) | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 287 | << "consumer should have returned a motion event"; | 
|  | 288 |  | 
|  | 289 | MotionEvent* motionEvent = static_cast<MotionEvent*>(event); | 
|  | 290 | EXPECT_EQ(seq, consumeSeq); | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 291 | EXPECT_EQ(eventId, motionEvent->getId()); | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 292 | EXPECT_EQ(deviceId, motionEvent->getDeviceId()); | 
|  | 293 | EXPECT_EQ(source, motionEvent->getSource()); | 
| Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 294 | EXPECT_EQ(displayId, motionEvent->getDisplayId()); | 
| Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 295 | EXPECT_EQ(hmac, motionEvent->getHmac()); | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 296 | EXPECT_EQ(action, motionEvent->getAction()); | 
|  | 297 | EXPECT_EQ(flags, motionEvent->getFlags()); | 
|  | 298 | EXPECT_EQ(edgeFlags, motionEvent->getEdgeFlags()); | 
|  | 299 | EXPECT_EQ(metaState, motionEvent->getMetaState()); | 
|  | 300 | EXPECT_EQ(buttonState, motionEvent->getButtonState()); | 
| Siarhei Vishniakou | 16a2e30 | 2019-01-14 19:21:45 -0800 | [diff] [blame] | 301 | EXPECT_EQ(classification, motionEvent->getClassification()); | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 302 | EXPECT_EQ(transform, motionEvent->getTransform()); | 
| Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 303 | EXPECT_EQ(xOffset, motionEvent->getXOffset()); | 
|  | 304 | EXPECT_EQ(yOffset, motionEvent->getYOffset()); | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 305 | EXPECT_EQ(xPrecision, motionEvent->getXPrecision()); | 
|  | 306 | EXPECT_EQ(yPrecision, motionEvent->getYPrecision()); | 
| Prabir Pradhan | 00e029d | 2023-03-09 20:11:09 +0000 | [diff] [blame] | 307 | EXPECT_NEAR(xCursorPosition, motionEvent->getRawXCursorPosition(), EPSILON); | 
|  | 308 | EXPECT_NEAR(yCursorPosition, motionEvent->getRawYCursorPosition(), EPSILON); | 
|  | 309 | EXPECT_NEAR(xCursorPosition * xScale + xOffset, motionEvent->getXCursorPosition(), EPSILON); | 
|  | 310 | EXPECT_NEAR(yCursorPosition * yScale + yOffset, motionEvent->getYCursorPosition(), EPSILON); | 
| Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 311 | EXPECT_EQ(rawTransform, motionEvent->getRawTransform()); | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 312 | EXPECT_EQ(downTime, motionEvent->getDownTime()); | 
|  | 313 | EXPECT_EQ(eventTime, motionEvent->getEventTime()); | 
|  | 314 | EXPECT_EQ(pointerCount, motionEvent->getPointerCount()); | 
|  | 315 | EXPECT_EQ(0U, motionEvent->getHistorySize()); | 
|  | 316 |  | 
|  | 317 | for (size_t i = 0; i < pointerCount; i++) { | 
|  | 318 | SCOPED_TRACE(i); | 
|  | 319 | EXPECT_EQ(pointerProperties[i].id, motionEvent->getPointerId(i)); | 
|  | 320 | EXPECT_EQ(pointerProperties[i].toolType, motionEvent->getToolType(i)); | 
|  | 321 |  | 
| Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 322 | const auto& pc = pointerCoords[i]; | 
| Prabir Pradhan | 00e029d | 2023-03-09 20:11:09 +0000 | [diff] [blame] | 323 | EXPECT_EQ(pc, motionEvent->getSamplePointerCoords()[i]); | 
|  | 324 |  | 
|  | 325 | EXPECT_NEAR(pc.getX() * rawXScale + rawXOffset, motionEvent->getRawX(i), EPSILON); | 
|  | 326 | EXPECT_NEAR(pc.getY() * rawYScale + rawYOffset, motionEvent->getRawY(i), EPSILON); | 
|  | 327 | EXPECT_NEAR(pc.getX() * xScale + xOffset, motionEvent->getX(i), EPSILON); | 
|  | 328 | EXPECT_NEAR(pc.getY() * yScale + yOffset, motionEvent->getY(i), EPSILON); | 
| Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 329 | EXPECT_EQ(pc.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), motionEvent->getPressure(i)); | 
|  | 330 | EXPECT_EQ(pc.getAxisValue(AMOTION_EVENT_AXIS_SIZE), motionEvent->getSize(i)); | 
|  | 331 | EXPECT_EQ(pc.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), motionEvent->getTouchMajor(i)); | 
|  | 332 | EXPECT_EQ(pc.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), motionEvent->getTouchMinor(i)); | 
|  | 333 | EXPECT_EQ(pc.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), motionEvent->getToolMajor(i)); | 
|  | 334 | EXPECT_EQ(pc.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), motionEvent->getToolMinor(i)); | 
| Prabir Pradhan | 9eb02c0 | 2021-10-19 14:02:20 -0700 | [diff] [blame] | 335 |  | 
|  | 336 | // Calculate the orientation after scaling, keeping in mind that an orientation of 0 is | 
|  | 337 | // "up", and the positive y direction is "down". | 
|  | 338 | const float unscaledOrientation = pc.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION); | 
|  | 339 | const float x = sinf(unscaledOrientation) * xScale; | 
|  | 340 | const float y = -cosf(unscaledOrientation) * yScale; | 
|  | 341 | EXPECT_EQ(atan2f(x, -y), motionEvent->getOrientation(i)); | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 342 | } | 
|  | 343 |  | 
|  | 344 | status = mConsumer->sendFinishedSignal(seq, false); | 
|  | 345 | ASSERT_EQ(OK, status) | 
|  | 346 | << "consumer sendFinishedSignal should return OK"; | 
|  | 347 |  | 
| Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 348 | Result<InputPublisher::ConsumerResponse> result = mPublisher->receiveConsumerResponse(); | 
|  | 349 | ASSERT_TRUE(result.ok()) << "receiveConsumerResponse should return OK"; | 
|  | 350 | ASSERT_TRUE(std::holds_alternative<InputPublisher::Finished>(*result)); | 
|  | 351 | const InputPublisher::Finished& finish = std::get<InputPublisher::Finished>(*result); | 
|  | 352 | ASSERT_EQ(seq, finish.seq) | 
|  | 353 | << "receiveConsumerResponse should have returned the original sequence number"; | 
|  | 354 | ASSERT_FALSE(finish.handled) | 
|  | 355 | << "receiveConsumerResponse should have set handled to consumer's reply"; | 
|  | 356 | ASSERT_GE(finish.consumeTime, publishTime) | 
| Siarhei Vishniakou | 3531ae7 | 2021-02-02 12:12:27 -1000 | [diff] [blame] | 357 | << "finished signal's consume time should be greater than publish time"; | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 358 | } | 
|  | 359 |  | 
| Siarhei Vishniakou | 6252af7 | 2023-09-20 09:00:38 -0700 | [diff] [blame] | 360 | void InputPublisherAndConsumerTest::publishAndConsumeFocusEvent() { | 
| Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 361 | status_t status; | 
|  | 362 |  | 
|  | 363 | constexpr uint32_t seq = 15; | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 364 | int32_t eventId = InputEvent::nextId(); | 
| Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 365 | constexpr bool hasFocus = true; | 
| Siarhei Vishniakou | 3531ae7 | 2021-02-02 12:12:27 -1000 | [diff] [blame] | 366 | const nsecs_t publishTime = systemTime(SYSTEM_TIME_MONOTONIC); | 
| Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 367 |  | 
| Antonio Kantek | 3cfec7b | 2021-11-05 18:26:17 -0700 | [diff] [blame] | 368 | status = mPublisher->publishFocusEvent(seq, eventId, hasFocus); | 
| arthurhung | 7632c33 | 2020-12-30 16:58:01 +0800 | [diff] [blame] | 369 | ASSERT_EQ(OK, status) << "publisher publishFocusEvent should return OK"; | 
| Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 370 |  | 
|  | 371 | uint32_t consumeSeq; | 
|  | 372 | InputEvent* event; | 
| Harry Cutts | 82c791c | 2023-03-10 17:15:07 +0000 | [diff] [blame] | 373 | status = mConsumer->consume(&mEventFactory, /*consumeBatches=*/true, -1, &consumeSeq, &event); | 
| Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 374 | ASSERT_EQ(OK, status) << "consumer consume should return OK"; | 
|  | 375 |  | 
|  | 376 | ASSERT_TRUE(event != nullptr) << "consumer should have returned non-NULL event"; | 
| Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 377 | ASSERT_EQ(InputEventType::FOCUS, event->getType()) | 
| Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 378 | << "consumer should have returned a focus event"; | 
|  | 379 |  | 
|  | 380 | FocusEvent* focusEvent = static_cast<FocusEvent*>(event); | 
|  | 381 | EXPECT_EQ(seq, consumeSeq); | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 382 | EXPECT_EQ(eventId, focusEvent->getId()); | 
| Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 383 | EXPECT_EQ(hasFocus, focusEvent->getHasFocus()); | 
| Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 384 |  | 
|  | 385 | status = mConsumer->sendFinishedSignal(seq, true); | 
|  | 386 | ASSERT_EQ(OK, status) << "consumer sendFinishedSignal should return OK"; | 
|  | 387 |  | 
| Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 388 | Result<InputPublisher::ConsumerResponse> result = mPublisher->receiveConsumerResponse(); | 
|  | 389 | ASSERT_TRUE(result.ok()) << "receiveConsumerResponse should return OK"; | 
|  | 390 | ASSERT_TRUE(std::holds_alternative<InputPublisher::Finished>(*result)); | 
|  | 391 | const InputPublisher::Finished& finish = std::get<InputPublisher::Finished>(*result); | 
| Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 392 |  | 
| Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 393 | ASSERT_EQ(seq, finish.seq) | 
|  | 394 | << "receiveConsumerResponse should have returned the original sequence number"; | 
|  | 395 | ASSERT_TRUE(finish.handled) | 
|  | 396 | << "receiveConsumerResponse should have set handled to consumer's reply"; | 
|  | 397 | ASSERT_GE(finish.consumeTime, publishTime) | 
| Siarhei Vishniakou | 3531ae7 | 2021-02-02 12:12:27 -1000 | [diff] [blame] | 398 | << "finished signal's consume time should be greater than publish time"; | 
| Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 399 | } | 
|  | 400 |  | 
| Siarhei Vishniakou | 6252af7 | 2023-09-20 09:00:38 -0700 | [diff] [blame] | 401 | void InputPublisherAndConsumerTest::publishAndConsumeCaptureEvent() { | 
| Prabir Pradhan | 3f37b7b | 2020-11-10 16:50:18 -0800 | [diff] [blame] | 402 | status_t status; | 
|  | 403 |  | 
|  | 404 | constexpr uint32_t seq = 42; | 
|  | 405 | int32_t eventId = InputEvent::nextId(); | 
|  | 406 | constexpr bool captureEnabled = true; | 
| Siarhei Vishniakou | 3531ae7 | 2021-02-02 12:12:27 -1000 | [diff] [blame] | 407 | const nsecs_t publishTime = systemTime(SYSTEM_TIME_MONOTONIC); | 
| Prabir Pradhan | 3f37b7b | 2020-11-10 16:50:18 -0800 | [diff] [blame] | 408 |  | 
|  | 409 | status = mPublisher->publishCaptureEvent(seq, eventId, captureEnabled); | 
| arthurhung | 7632c33 | 2020-12-30 16:58:01 +0800 | [diff] [blame] | 410 | ASSERT_EQ(OK, status) << "publisher publishCaptureEvent should return OK"; | 
| Prabir Pradhan | 3f37b7b | 2020-11-10 16:50:18 -0800 | [diff] [blame] | 411 |  | 
|  | 412 | uint32_t consumeSeq; | 
|  | 413 | InputEvent* event; | 
| Harry Cutts | 82c791c | 2023-03-10 17:15:07 +0000 | [diff] [blame] | 414 | status = mConsumer->consume(&mEventFactory, /*consumeBatches=*/true, -1, &consumeSeq, &event); | 
| Prabir Pradhan | 3f37b7b | 2020-11-10 16:50:18 -0800 | [diff] [blame] | 415 | ASSERT_EQ(OK, status) << "consumer consume should return OK"; | 
|  | 416 |  | 
|  | 417 | ASSERT_TRUE(event != nullptr) << "consumer should have returned non-NULL event"; | 
| Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 418 | ASSERT_EQ(InputEventType::CAPTURE, event->getType()) | 
| Prabir Pradhan | 3f37b7b | 2020-11-10 16:50:18 -0800 | [diff] [blame] | 419 | << "consumer should have returned a capture event"; | 
|  | 420 |  | 
|  | 421 | const CaptureEvent* captureEvent = static_cast<CaptureEvent*>(event); | 
|  | 422 | EXPECT_EQ(seq, consumeSeq); | 
|  | 423 | EXPECT_EQ(eventId, captureEvent->getId()); | 
|  | 424 | EXPECT_EQ(captureEnabled, captureEvent->getPointerCaptureEnabled()); | 
|  | 425 |  | 
|  | 426 | status = mConsumer->sendFinishedSignal(seq, true); | 
|  | 427 | ASSERT_EQ(OK, status) << "consumer sendFinishedSignal should return OK"; | 
|  | 428 |  | 
| Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 429 | Result<InputPublisher::ConsumerResponse> result = mPublisher->receiveConsumerResponse(); | 
|  | 430 | ASSERT_TRUE(result.ok()) << "receiveConsumerResponse should return OK"; | 
|  | 431 | ASSERT_TRUE(std::holds_alternative<InputPublisher::Finished>(*result)); | 
|  | 432 | const InputPublisher::Finished& finish = std::get<InputPublisher::Finished>(*result); | 
|  | 433 | ASSERT_EQ(seq, finish.seq) | 
|  | 434 | << "receiveConsumerResponse should have returned the original sequence number"; | 
|  | 435 | ASSERT_TRUE(finish.handled) | 
|  | 436 | << "receiveConsumerResponse should have set handled to consumer's reply"; | 
|  | 437 | ASSERT_GE(finish.consumeTime, publishTime) | 
| Siarhei Vishniakou | 3531ae7 | 2021-02-02 12:12:27 -1000 | [diff] [blame] | 438 | << "finished signal's consume time should be greater than publish time"; | 
| Prabir Pradhan | 3f37b7b | 2020-11-10 16:50:18 -0800 | [diff] [blame] | 439 | } | 
|  | 440 |  | 
| Siarhei Vishniakou | 6252af7 | 2023-09-20 09:00:38 -0700 | [diff] [blame] | 441 | void InputPublisherAndConsumerTest::publishAndConsumeDragEvent() { | 
| arthurhung | 7632c33 | 2020-12-30 16:58:01 +0800 | [diff] [blame] | 442 | status_t status; | 
|  | 443 |  | 
|  | 444 | constexpr uint32_t seq = 15; | 
|  | 445 | int32_t eventId = InputEvent::nextId(); | 
|  | 446 | constexpr bool isExiting = false; | 
|  | 447 | constexpr float x = 10; | 
|  | 448 | constexpr float y = 15; | 
|  | 449 | const nsecs_t publishTime = systemTime(SYSTEM_TIME_MONOTONIC); | 
|  | 450 |  | 
|  | 451 | status = mPublisher->publishDragEvent(seq, eventId, x, y, isExiting); | 
|  | 452 | ASSERT_EQ(OK, status) << "publisher publishDragEvent should return OK"; | 
|  | 453 |  | 
|  | 454 | uint32_t consumeSeq; | 
|  | 455 | InputEvent* event; | 
| Harry Cutts | 82c791c | 2023-03-10 17:15:07 +0000 | [diff] [blame] | 456 | status = mConsumer->consume(&mEventFactory, /*consumeBatches=*/true, -1, &consumeSeq, &event); | 
| arthurhung | 7632c33 | 2020-12-30 16:58:01 +0800 | [diff] [blame] | 457 | ASSERT_EQ(OK, status) << "consumer consume should return OK"; | 
|  | 458 |  | 
|  | 459 | ASSERT_TRUE(event != nullptr) << "consumer should have returned non-NULL event"; | 
| Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 460 | ASSERT_EQ(InputEventType::DRAG, event->getType()) | 
| arthurhung | 7632c33 | 2020-12-30 16:58:01 +0800 | [diff] [blame] | 461 | << "consumer should have returned a drag event"; | 
|  | 462 |  | 
| Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 463 | const DragEvent& dragEvent = static_cast<const DragEvent&>(*event); | 
| arthurhung | 7632c33 | 2020-12-30 16:58:01 +0800 | [diff] [blame] | 464 | EXPECT_EQ(seq, consumeSeq); | 
| Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 465 | EXPECT_EQ(eventId, dragEvent.getId()); | 
|  | 466 | EXPECT_EQ(isExiting, dragEvent.isExiting()); | 
|  | 467 | EXPECT_EQ(x, dragEvent.getX()); | 
|  | 468 | EXPECT_EQ(y, dragEvent.getY()); | 
| arthurhung | 7632c33 | 2020-12-30 16:58:01 +0800 | [diff] [blame] | 469 |  | 
|  | 470 | status = mConsumer->sendFinishedSignal(seq, true); | 
|  | 471 | ASSERT_EQ(OK, status) << "consumer sendFinishedSignal should return OK"; | 
|  | 472 |  | 
| Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 473 | Result<InputPublisher::ConsumerResponse> result = mPublisher->receiveConsumerResponse(); | 
|  | 474 | ASSERT_TRUE(result.ok()) << "receiveConsumerResponse should return OK"; | 
|  | 475 | ASSERT_TRUE(std::holds_alternative<InputPublisher::Finished>(*result)); | 
|  | 476 | const InputPublisher::Finished& finish = std::get<InputPublisher::Finished>(*result); | 
|  | 477 | ASSERT_EQ(seq, finish.seq) | 
|  | 478 | << "receiveConsumerResponse should have returned the original sequence number"; | 
|  | 479 | ASSERT_TRUE(finish.handled) | 
|  | 480 | << "receiveConsumerResponse should have set handled to consumer's reply"; | 
|  | 481 | ASSERT_GE(finish.consumeTime, publishTime) | 
| arthurhung | 7632c33 | 2020-12-30 16:58:01 +0800 | [diff] [blame] | 482 | << "finished signal's consume time should be greater than publish time"; | 
|  | 483 | } | 
|  | 484 |  | 
| Siarhei Vishniakou | 6252af7 | 2023-09-20 09:00:38 -0700 | [diff] [blame] | 485 | void InputPublisherAndConsumerTest::publishAndConsumeTouchModeEvent() { | 
| Antonio Kantek | 7cdf8ef | 2021-07-13 18:04:53 -0700 | [diff] [blame] | 486 | status_t status; | 
|  | 487 |  | 
|  | 488 | constexpr uint32_t seq = 15; | 
|  | 489 | int32_t eventId = InputEvent::nextId(); | 
|  | 490 | constexpr bool touchModeEnabled = true; | 
|  | 491 | const nsecs_t publishTime = systemTime(SYSTEM_TIME_MONOTONIC); | 
|  | 492 |  | 
|  | 493 | status = mPublisher->publishTouchModeEvent(seq, eventId, touchModeEnabled); | 
|  | 494 | ASSERT_EQ(OK, status) << "publisher publishTouchModeEvent should return OK"; | 
|  | 495 |  | 
|  | 496 | uint32_t consumeSeq; | 
|  | 497 | InputEvent* event; | 
| Harry Cutts | 82c791c | 2023-03-10 17:15:07 +0000 | [diff] [blame] | 498 | status = mConsumer->consume(&mEventFactory, /*consumeBatches=*/true, -1, &consumeSeq, &event); | 
| Antonio Kantek | 7cdf8ef | 2021-07-13 18:04:53 -0700 | [diff] [blame] | 499 | ASSERT_EQ(OK, status) << "consumer consume should return OK"; | 
|  | 500 |  | 
|  | 501 | ASSERT_TRUE(event != nullptr) << "consumer should have returned non-NULL event"; | 
| Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 502 | ASSERT_EQ(InputEventType::TOUCH_MODE, event->getType()) | 
| Antonio Kantek | 7cdf8ef | 2021-07-13 18:04:53 -0700 | [diff] [blame] | 503 | << "consumer should have returned a touch mode event"; | 
|  | 504 |  | 
|  | 505 | const TouchModeEvent& touchModeEvent = static_cast<const TouchModeEvent&>(*event); | 
|  | 506 | EXPECT_EQ(seq, consumeSeq); | 
|  | 507 | EXPECT_EQ(eventId, touchModeEvent.getId()); | 
|  | 508 | EXPECT_EQ(touchModeEnabled, touchModeEvent.isInTouchMode()); | 
|  | 509 |  | 
|  | 510 | status = mConsumer->sendFinishedSignal(seq, true); | 
|  | 511 | ASSERT_EQ(OK, status) << "consumer sendFinishedSignal should return OK"; | 
|  | 512 |  | 
|  | 513 | Result<InputPublisher::ConsumerResponse> result = mPublisher->receiveConsumerResponse(); | 
|  | 514 | ASSERT_TRUE(result.ok()) << "receiveConsumerResponse should return OK"; | 
|  | 515 | ASSERT_TRUE(std::holds_alternative<InputPublisher::Finished>(*result)); | 
|  | 516 | const InputPublisher::Finished& finish = std::get<InputPublisher::Finished>(*result); | 
|  | 517 | ASSERT_EQ(seq, finish.seq) | 
|  | 518 | << "receiveConsumerResponse should have returned the original sequence number"; | 
|  | 519 | ASSERT_TRUE(finish.handled) | 
|  | 520 | << "receiveConsumerResponse should have set handled to consumer's reply"; | 
|  | 521 | ASSERT_GE(finish.consumeTime, publishTime) | 
|  | 522 | << "finished signal's consume time should be greater than publish time"; | 
|  | 523 | } | 
|  | 524 |  | 
| Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 525 | TEST_F(InputPublisherAndConsumerTest, SendTimeline) { | 
|  | 526 | const int32_t inputEventId = 20; | 
|  | 527 | std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline; | 
|  | 528 | graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 30; | 
|  | 529 | graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 40; | 
|  | 530 | status_t status = mConsumer->sendTimeline(inputEventId, graphicsTimeline); | 
|  | 531 | ASSERT_EQ(OK, status); | 
|  | 532 |  | 
|  | 533 | Result<InputPublisher::ConsumerResponse> result = mPublisher->receiveConsumerResponse(); | 
|  | 534 | ASSERT_TRUE(result.ok()) << "receiveConsumerResponse should return OK"; | 
|  | 535 | ASSERT_TRUE(std::holds_alternative<InputPublisher::Timeline>(*result)); | 
|  | 536 | const InputPublisher::Timeline& timeline = std::get<InputPublisher::Timeline>(*result); | 
|  | 537 | ASSERT_EQ(inputEventId, timeline.inputEventId); | 
|  | 538 | ASSERT_EQ(graphicsTimeline, timeline.graphicsTimeline); | 
|  | 539 | } | 
|  | 540 |  | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 541 | TEST_F(InputPublisherAndConsumerTest, PublishKeyEvent_EndToEnd) { | 
| Siarhei Vishniakou | 6252af7 | 2023-09-20 09:00:38 -0700 | [diff] [blame] | 542 | ASSERT_NO_FATAL_FAILURE(publishAndConsumeKeyEvent()); | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 543 | } | 
|  | 544 |  | 
|  | 545 | TEST_F(InputPublisherAndConsumerTest, PublishMotionEvent_EndToEnd) { | 
| Siarhei Vishniakou | 6252af7 | 2023-09-20 09:00:38 -0700 | [diff] [blame] | 546 | ASSERT_NO_FATAL_FAILURE(publishAndConsumeMotionStream()); | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 547 | } | 
|  | 548 |  | 
| Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 549 | TEST_F(InputPublisherAndConsumerTest, PublishFocusEvent_EndToEnd) { | 
| Siarhei Vishniakou | 6252af7 | 2023-09-20 09:00:38 -0700 | [diff] [blame] | 550 | ASSERT_NO_FATAL_FAILURE(publishAndConsumeFocusEvent()); | 
| Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 551 | } | 
|  | 552 |  | 
| Prabir Pradhan | 3f37b7b | 2020-11-10 16:50:18 -0800 | [diff] [blame] | 553 | TEST_F(InputPublisherAndConsumerTest, PublishCaptureEvent_EndToEnd) { | 
| Siarhei Vishniakou | 6252af7 | 2023-09-20 09:00:38 -0700 | [diff] [blame] | 554 | ASSERT_NO_FATAL_FAILURE(publishAndConsumeCaptureEvent()); | 
| Prabir Pradhan | 3f37b7b | 2020-11-10 16:50:18 -0800 | [diff] [blame] | 555 | } | 
|  | 556 |  | 
| arthurhung | 7632c33 | 2020-12-30 16:58:01 +0800 | [diff] [blame] | 557 | TEST_F(InputPublisherAndConsumerTest, PublishDragEvent_EndToEnd) { | 
| Siarhei Vishniakou | 6252af7 | 2023-09-20 09:00:38 -0700 | [diff] [blame] | 558 | ASSERT_NO_FATAL_FAILURE(publishAndConsumeDragEvent()); | 
| arthurhung | 7632c33 | 2020-12-30 16:58:01 +0800 | [diff] [blame] | 559 | } | 
|  | 560 |  | 
| Antonio Kantek | 7cdf8ef | 2021-07-13 18:04:53 -0700 | [diff] [blame] | 561 | TEST_F(InputPublisherAndConsumerTest, PublishTouchModeEvent_EndToEnd) { | 
| Siarhei Vishniakou | 6252af7 | 2023-09-20 09:00:38 -0700 | [diff] [blame] | 562 | ASSERT_NO_FATAL_FAILURE(publishAndConsumeTouchModeEvent()); | 
| Antonio Kantek | 7cdf8ef | 2021-07-13 18:04:53 -0700 | [diff] [blame] | 563 | } | 
|  | 564 |  | 
| Siarhei Vishniakou | b0fffdd | 2017-11-10 20:16:56 -0800 | [diff] [blame] | 565 | TEST_F(InputPublisherAndConsumerTest, PublishMotionEvent_WhenSequenceNumberIsZero_ReturnsError) { | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 566 | status_t status; | 
| Siarhei Vishniakou | b0fffdd | 2017-11-10 20:16:56 -0800 | [diff] [blame] | 567 | const size_t pointerCount = 1; | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 568 | PointerProperties pointerProperties[pointerCount]; | 
|  | 569 | PointerCoords pointerCoords[pointerCount]; | 
| Siarhei Vishniakou | b0fffdd | 2017-11-10 20:16:56 -0800 | [diff] [blame] | 570 | for (size_t i = 0; i < pointerCount; i++) { | 
|  | 571 | pointerProperties[i].clear(); | 
|  | 572 | pointerCoords[i].clear(); | 
|  | 573 | } | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 574 |  | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 575 | ui::Transform identityTransform; | 
| Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 576 | status = | 
|  | 577 | mPublisher->publishMotionEvent(0, InputEvent::nextId(), 0, 0, 0, INVALID_HMAC, 0, 0, 0, | 
|  | 578 | 0, 0, 0, MotionClassification::NONE, identityTransform, | 
|  | 579 | 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
|  | 580 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, | 
|  | 581 | 0, 0, pointerCount, pointerProperties, pointerCoords); | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 582 | ASSERT_EQ(BAD_VALUE, status) | 
|  | 583 | << "publisher publishMotionEvent should return BAD_VALUE"; | 
|  | 584 | } | 
|  | 585 |  | 
| Siarhei Vishniakou | b0fffdd | 2017-11-10 20:16:56 -0800 | [diff] [blame] | 586 | TEST_F(InputPublisherAndConsumerTest, PublishMotionEvent_WhenPointerCountLessThan1_ReturnsError) { | 
|  | 587 | status_t status; | 
|  | 588 | const size_t pointerCount = 0; | 
|  | 589 | PointerProperties pointerProperties[pointerCount]; | 
|  | 590 | PointerCoords pointerCoords[pointerCount]; | 
|  | 591 |  | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 592 | ui::Transform identityTransform; | 
| Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 593 | status = | 
|  | 594 | mPublisher->publishMotionEvent(1, InputEvent::nextId(), 0, 0, 0, INVALID_HMAC, 0, 0, 0, | 
|  | 595 | 0, 0, 0, MotionClassification::NONE, identityTransform, | 
|  | 596 | 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
|  | 597 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, | 
|  | 598 | 0, 0, pointerCount, pointerProperties, pointerCoords); | 
| Siarhei Vishniakou | b0fffdd | 2017-11-10 20:16:56 -0800 | [diff] [blame] | 599 | ASSERT_EQ(BAD_VALUE, status) | 
|  | 600 | << "publisher publishMotionEvent should return BAD_VALUE"; | 
|  | 601 | } | 
|  | 602 |  | 
|  | 603 | TEST_F(InputPublisherAndConsumerTest, | 
|  | 604 | PublishMotionEvent_WhenPointerCountGreaterThanMax_ReturnsError) { | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 605 | status_t status; | 
|  | 606 | const size_t pointerCount = MAX_POINTERS + 1; | 
|  | 607 | PointerProperties pointerProperties[pointerCount]; | 
|  | 608 | PointerCoords pointerCoords[pointerCount]; | 
|  | 609 | for (size_t i = 0; i < pointerCount; i++) { | 
|  | 610 | pointerProperties[i].clear(); | 
|  | 611 | pointerCoords[i].clear(); | 
|  | 612 | } | 
|  | 613 |  | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 614 | ui::Transform identityTransform; | 
| Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 615 | status = | 
|  | 616 | mPublisher->publishMotionEvent(1, InputEvent::nextId(), 0, 0, 0, INVALID_HMAC, 0, 0, 0, | 
|  | 617 | 0, 0, 0, MotionClassification::NONE, identityTransform, | 
|  | 618 | 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
|  | 619 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, | 
|  | 620 | 0, 0, pointerCount, pointerProperties, pointerCoords); | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 621 | ASSERT_EQ(BAD_VALUE, status) | 
|  | 622 | << "publisher publishMotionEvent should return BAD_VALUE"; | 
|  | 623 | } | 
|  | 624 |  | 
|  | 625 | TEST_F(InputPublisherAndConsumerTest, PublishMultipleEvents_EndToEnd) { | 
| Siarhei Vishniakou | 6252af7 | 2023-09-20 09:00:38 -0700 | [diff] [blame] | 626 | const nsecs_t downTime = systemTime(SYSTEM_TIME_MONOTONIC); | 
|  | 627 |  | 
|  | 628 | publishAndConsumeMotionEvent(AMOTION_EVENT_ACTION_DOWN, downTime, | 
|  | 629 | {Pointer{.id = 0, .x = 20, .y = 30}}); | 
|  | 630 | ASSERT_NO_FATAL_FAILURE(publishAndConsumeKeyEvent()); | 
|  | 631 | publishAndConsumeMotionEvent(POINTER_1_DOWN, downTime, | 
|  | 632 | {Pointer{.id = 0, .x = 20, .y = 30}, | 
|  | 633 | Pointer{.id = 1, .x = 200, .y = 300}}); | 
|  | 634 | ASSERT_NO_FATAL_FAILURE(publishAndConsumeFocusEvent()); | 
|  | 635 | publishAndConsumeMotionEvent(POINTER_2_DOWN, downTime, | 
|  | 636 | {Pointer{.id = 0, .x = 20, .y = 30}, | 
|  | 637 | Pointer{.id = 1, .x = 200, .y = 300}, | 
|  | 638 | Pointer{.id = 2, .x = 200, .y = 300}}); | 
|  | 639 | ASSERT_NO_FATAL_FAILURE(publishAndConsumeKeyEvent()); | 
|  | 640 | ASSERT_NO_FATAL_FAILURE(publishAndConsumeCaptureEvent()); | 
|  | 641 | ASSERT_NO_FATAL_FAILURE(publishAndConsumeDragEvent()); | 
|  | 642 | // Provide a consistent input stream - cancel the gesture that was started above | 
|  | 643 | publishAndConsumeMotionEvent(AMOTION_EVENT_ACTION_CANCEL, downTime, | 
|  | 644 | {Pointer{.id = 0, .x = 20, .y = 30}, | 
|  | 645 | Pointer{.id = 1, .x = 200, .y = 300}, | 
|  | 646 | Pointer{.id = 2, .x = 200, .y = 300}}); | 
|  | 647 | ASSERT_NO_FATAL_FAILURE(publishAndConsumeKeyEvent()); | 
|  | 648 | ASSERT_NO_FATAL_FAILURE(publishAndConsumeTouchModeEvent()); | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 649 | } | 
|  | 650 |  | 
|  | 651 | } // namespace android |