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 | |
Siarhei Vishniakou | 16a2e30 | 2019-01-14 19:21:45 -0800 | [diff] [blame] | 17 | #include <array> |
| 18 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 19 | #include <unistd.h> |
| 20 | #include <time.h> |
| 21 | #include <errno.h> |
| 22 | |
Josh Gao | 2ccbe3a | 2019-08-09 14:35:36 -0700 | [diff] [blame] | 23 | #include <binder/Binder.h> |
Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 24 | #include <binder/Parcel.h> |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 25 | #include <gtest/gtest.h> |
| 26 | #include <input/InputTransport.h> |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 27 | #include <utils/StopWatch.h> |
| 28 | #include <utils/StrongPointer.h> |
Josh Gao | 2ccbe3a | 2019-08-09 14:35:36 -0700 | [diff] [blame] | 29 | #include <utils/Timers.h> |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 30 | |
| 31 | namespace android { |
| 32 | |
Siarhei Vishniakou | 8d66013 | 2024-01-11 16:48:44 -0800 | [diff] [blame] | 33 | namespace { |
| 34 | bool operator==(const InputChannel& left, const InputChannel& right) { |
| 35 | struct stat lhs, rhs; |
| 36 | if (fstat(left.getFd(), &lhs) != 0) { |
| 37 | return false; |
| 38 | } |
| 39 | if (fstat(right.getFd(), &rhs) != 0) { |
| 40 | return false; |
| 41 | } |
| 42 | // If file descriptors are pointing to same inode they are duplicated fds. |
| 43 | return left.getName() == right.getName() && |
| 44 | left.getConnectionToken() == right.getConnectionToken() && lhs.st_ino == rhs.st_ino; |
| 45 | } |
| 46 | } // namespace |
| 47 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 48 | class InputChannelTest : public testing::Test { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 49 | }; |
| 50 | |
Siarhei Vishniakou | 8d66013 | 2024-01-11 16:48:44 -0800 | [diff] [blame] | 51 | TEST_F(InputChannelTest, ClientAndServerTokensMatch) { |
| 52 | std::unique_ptr<InputChannel> serverChannel, clientChannel; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 53 | |
Siarhei Vishniakou | 8d66013 | 2024-01-11 16:48:44 -0800 | [diff] [blame] | 54 | status_t result = |
| 55 | InputChannel::openInputChannelPair("channel name", serverChannel, clientChannel); |
| 56 | ASSERT_EQ(OK, result) << "should have successfully opened a channel pair"; |
| 57 | EXPECT_EQ(serverChannel->getConnectionToken(), clientChannel->getConnectionToken()); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | TEST_F(InputChannelTest, OpenInputChannelPair_ReturnsAPairOfConnectedChannels) { |
Siarhei Vishniakou | d258827 | 2020-07-10 11:15:40 -0500 | [diff] [blame] | 61 | std::unique_ptr<InputChannel> serverChannel, clientChannel; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 62 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 63 | status_t result = InputChannel::openInputChannelPair("channel name", |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 64 | serverChannel, clientChannel); |
| 65 | |
Siarhei Vishniakou | 8d66013 | 2024-01-11 16:48:44 -0800 | [diff] [blame] | 66 | ASSERT_EQ(OK, result) << "should have successfully opened a channel pair"; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 67 | |
| 68 | // Name |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 69 | EXPECT_STREQ("channel name (server)", serverChannel->getName().c_str()) |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 70 | << "server channel should have suffixed name"; |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 71 | EXPECT_STREQ("channel name (client)", clientChannel->getName().c_str()) |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 72 | << "client channel should have suffixed name"; |
| 73 | |
| 74 | // Server->Client communication |
Egor Pasko | a0d32af | 2023-12-14 17:45:41 +0100 | [diff] [blame] | 75 | InputMessage serverMsg = {}; |
Siarhei Vishniakou | 5240277 | 2019-10-22 09:32:30 -0700 | [diff] [blame] | 76 | serverMsg.header.type = InputMessage::Type::KEY; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 77 | serverMsg.body.key.action = AKEY_EVENT_ACTION_DOWN; |
| 78 | EXPECT_EQ(OK, serverChannel->sendMessage(&serverMsg)) |
| 79 | << "server channel should be able to send message to client channel"; |
| 80 | |
| 81 | InputMessage clientMsg; |
| 82 | EXPECT_EQ(OK, clientChannel->receiveMessage(&clientMsg)) |
| 83 | << "client channel should be able to receive message from server channel"; |
| 84 | EXPECT_EQ(serverMsg.header.type, clientMsg.header.type) |
| 85 | << "client channel should receive the correct message from server channel"; |
| 86 | EXPECT_EQ(serverMsg.body.key.action, clientMsg.body.key.action) |
| 87 | << "client channel should receive the correct message from server channel"; |
| 88 | |
| 89 | // Client->Server communication |
Egor Pasko | a0d32af | 2023-12-14 17:45:41 +0100 | [diff] [blame] | 90 | InputMessage clientReply = {}; |
Siarhei Vishniakou | 5240277 | 2019-10-22 09:32:30 -0700 | [diff] [blame] | 91 | clientReply.header.type = InputMessage::Type::FINISHED; |
Siarhei Vishniakou | a64c159 | 2020-06-22 12:02:29 -0500 | [diff] [blame] | 92 | clientReply.header.seq = 0x11223344; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 93 | clientReply.body.finished.handled = true; |
| 94 | EXPECT_EQ(OK, clientChannel->sendMessage(&clientReply)) |
| 95 | << "client channel should be able to send message to server channel"; |
| 96 | |
| 97 | InputMessage serverReply; |
| 98 | EXPECT_EQ(OK, serverChannel->receiveMessage(&serverReply)) |
| 99 | << "server channel should be able to receive message from client channel"; |
| 100 | EXPECT_EQ(clientReply.header.type, serverReply.header.type) |
| 101 | << "server channel should receive the correct message from client channel"; |
Siarhei Vishniakou | a64c159 | 2020-06-22 12:02:29 -0500 | [diff] [blame] | 102 | EXPECT_EQ(clientReply.header.seq, serverReply.header.seq) |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 103 | << "server channel should receive the correct message from client channel"; |
| 104 | EXPECT_EQ(clientReply.body.finished.handled, serverReply.body.finished.handled) |
| 105 | << "server channel should receive the correct message from client channel"; |
| 106 | } |
| 107 | |
Egor Pasko | a0d32af | 2023-12-14 17:45:41 +0100 | [diff] [blame] | 108 | TEST_F(InputChannelTest, ProbablyHasInput) { |
| 109 | std::unique_ptr<InputChannel> senderChannel, receiverChannel; |
| 110 | |
| 111 | // Open a pair of channels. |
| 112 | status_t result = |
| 113 | InputChannel::openInputChannelPair("channel name", senderChannel, receiverChannel); |
| 114 | ASSERT_EQ(OK, result) << "should have successfully opened a channel pair"; |
| 115 | |
| 116 | ASSERT_FALSE(receiverChannel->probablyHasInput()); |
| 117 | |
| 118 | // Send one message. |
| 119 | InputMessage serverMsg = {}; |
| 120 | serverMsg.header.type = InputMessage::Type::KEY; |
| 121 | serverMsg.body.key.action = AKEY_EVENT_ACTION_DOWN; |
| 122 | EXPECT_EQ(OK, senderChannel->sendMessage(&serverMsg)) |
| 123 | << "server channel should be able to send message to client channel"; |
| 124 | |
| 125 | // Verify input is available. |
| 126 | bool hasInput = false; |
| 127 | do { |
| 128 | // The probablyHasInput() can return false positive under rare circumstances uncontrollable |
| 129 | // by the tests. Re-request the availability in this case. Returning |false| for a long |
| 130 | // time is not intended, and would cause a test timeout. |
| 131 | hasInput = receiverChannel->probablyHasInput(); |
| 132 | } while (!hasInput); |
| 133 | EXPECT_TRUE(hasInput) |
| 134 | << "client channel should observe that message is available before receiving it"; |
| 135 | |
| 136 | // Receive (consume) the message. |
| 137 | InputMessage clientMsg; |
| 138 | EXPECT_EQ(OK, receiverChannel->receiveMessage(&clientMsg)) |
| 139 | << "client channel should be able to receive message from server channel"; |
| 140 | EXPECT_EQ(serverMsg.header.type, clientMsg.header.type) |
| 141 | << "client channel should receive the correct message from server channel"; |
| 142 | EXPECT_EQ(serverMsg.body.key.action, clientMsg.body.key.action) |
| 143 | << "client channel should receive the correct message from server channel"; |
| 144 | |
| 145 | // Verify input is not available. |
| 146 | EXPECT_FALSE(receiverChannel->probablyHasInput()) |
| 147 | << "client should not observe any more messages after receiving the single one"; |
| 148 | } |
| 149 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 150 | TEST_F(InputChannelTest, ReceiveSignal_WhenNoSignalPresent_ReturnsAnError) { |
Siarhei Vishniakou | d258827 | 2020-07-10 11:15:40 -0500 | [diff] [blame] | 151 | std::unique_ptr<InputChannel> serverChannel, clientChannel; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 152 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 153 | status_t result = InputChannel::openInputChannelPair("channel name", |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 154 | serverChannel, clientChannel); |
| 155 | |
| 156 | ASSERT_EQ(OK, result) |
| 157 | << "should have successfully opened a channel pair"; |
| 158 | |
| 159 | InputMessage msg; |
| 160 | EXPECT_EQ(WOULD_BLOCK, clientChannel->receiveMessage(&msg)) |
| 161 | << "receiveMessage should have returned WOULD_BLOCK"; |
| 162 | } |
| 163 | |
| 164 | TEST_F(InputChannelTest, ReceiveSignal_WhenPeerClosed_ReturnsAnError) { |
Siarhei Vishniakou | d258827 | 2020-07-10 11:15:40 -0500 | [diff] [blame] | 165 | std::unique_ptr<InputChannel> serverChannel, clientChannel; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 166 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 167 | status_t result = InputChannel::openInputChannelPair("channel name", |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 168 | serverChannel, clientChannel); |
| 169 | |
| 170 | ASSERT_EQ(OK, result) |
| 171 | << "should have successfully opened a channel pair"; |
| 172 | |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 173 | serverChannel.reset(); // close server channel |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 174 | |
| 175 | InputMessage msg; |
| 176 | EXPECT_EQ(DEAD_OBJECT, clientChannel->receiveMessage(&msg)) |
| 177 | << "receiveMessage should have returned DEAD_OBJECT"; |
| 178 | } |
| 179 | |
| 180 | TEST_F(InputChannelTest, SendSignal_WhenPeerClosed_ReturnsAnError) { |
Siarhei Vishniakou | d258827 | 2020-07-10 11:15:40 -0500 | [diff] [blame] | 181 | std::unique_ptr<InputChannel> serverChannel, clientChannel; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 182 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 183 | status_t result = InputChannel::openInputChannelPair("channel name", |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 184 | serverChannel, clientChannel); |
| 185 | |
| 186 | ASSERT_EQ(OK, result) |
| 187 | << "should have successfully opened a channel pair"; |
| 188 | |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 189 | serverChannel.reset(); // close server channel |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 190 | |
| 191 | InputMessage msg; |
Siarhei Vishniakou | 5240277 | 2019-10-22 09:32:30 -0700 | [diff] [blame] | 192 | msg.header.type = InputMessage::Type::KEY; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 193 | EXPECT_EQ(DEAD_OBJECT, clientChannel->sendMessage(&msg)) |
| 194 | << "sendMessage should have returned DEAD_OBJECT"; |
| 195 | } |
| 196 | |
Siarhei Vishniakou | 16a2e30 | 2019-01-14 19:21:45 -0800 | [diff] [blame] | 197 | TEST_F(InputChannelTest, SendAndReceive_MotionClassification) { |
Siarhei Vishniakou | d258827 | 2020-07-10 11:15:40 -0500 | [diff] [blame] | 198 | std::unique_ptr<InputChannel> serverChannel, clientChannel; |
Siarhei Vishniakou | 16a2e30 | 2019-01-14 19:21:45 -0800 | [diff] [blame] | 199 | status_t result = InputChannel::openInputChannelPair("channel name", |
| 200 | serverChannel, clientChannel); |
| 201 | ASSERT_EQ(OK, result) |
| 202 | << "should have successfully opened a channel pair"; |
| 203 | |
| 204 | std::array<MotionClassification, 3> classifications = { |
| 205 | MotionClassification::NONE, |
| 206 | MotionClassification::AMBIGUOUS_GESTURE, |
| 207 | MotionClassification::DEEP_PRESS, |
| 208 | }; |
| 209 | |
| 210 | InputMessage serverMsg = {}, clientMsg; |
Siarhei Vishniakou | 5240277 | 2019-10-22 09:32:30 -0700 | [diff] [blame] | 211 | serverMsg.header.type = InputMessage::Type::MOTION; |
Siarhei Vishniakou | a64c159 | 2020-06-22 12:02:29 -0500 | [diff] [blame] | 212 | serverMsg.header.seq = 1; |
Siarhei Vishniakou | 16a2e30 | 2019-01-14 19:21:45 -0800 | [diff] [blame] | 213 | serverMsg.body.motion.pointerCount = 1; |
| 214 | |
| 215 | for (MotionClassification classification : classifications) { |
| 216 | // Send and receive a message with classification |
| 217 | serverMsg.body.motion.classification = classification; |
| 218 | EXPECT_EQ(OK, serverChannel->sendMessage(&serverMsg)) |
| 219 | << "server channel should be able to send message to client channel"; |
| 220 | |
| 221 | EXPECT_EQ(OK, clientChannel->receiveMessage(&clientMsg)) |
| 222 | << "client channel should be able to receive message from server channel"; |
| 223 | EXPECT_EQ(serverMsg.header.type, clientMsg.header.type); |
| 224 | EXPECT_EQ(classification, clientMsg.body.motion.classification) << |
| 225 | "Expected to receive " << motionClassificationToString(classification); |
| 226 | } |
| 227 | } |
| 228 | |
Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 229 | TEST_F(InputChannelTest, DuplicateChannelAndAssertEqual) { |
Siarhei Vishniakou | d258827 | 2020-07-10 11:15:40 -0500 | [diff] [blame] | 230 | std::unique_ptr<InputChannel> serverChannel, clientChannel; |
Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 231 | |
| 232 | status_t result = |
| 233 | InputChannel::openInputChannelPair("channel dup", serverChannel, clientChannel); |
| 234 | |
| 235 | ASSERT_EQ(OK, result) << "should have successfully opened a channel pair"; |
| 236 | |
Siarhei Vishniakou | d258827 | 2020-07-10 11:15:40 -0500 | [diff] [blame] | 237 | std::unique_ptr<InputChannel> dupChan = serverChannel->dup(); |
Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 238 | |
| 239 | EXPECT_EQ(*serverChannel == *dupChan, true) << "inputchannel should be equal after duplication"; |
| 240 | } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 241 | |
| 242 | } // namespace android |