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