blob: 25356cfcf05cecfaf2fada3891cb641d0ba9b94b [file] [log] [blame]
Jeff Brown5912f952013-07-01 19:10:31 -07001/*
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 Vishniakou16a2e302019-01-14 19:21:45 -080017#include <array>
18
Jeff Brown5912f952013-07-01 19:10:31 -070019#include <unistd.h>
20#include <time.h>
21#include <errno.h>
22
Josh Gao2ccbe3a2019-08-09 14:35:36 -070023#include <binder/Binder.h>
Chris Ye0783e992020-06-02 21:34:49 -070024#include <binder/Parcel.h>
Jeff Brown5912f952013-07-01 19:10:31 -070025#include <gtest/gtest.h>
26#include <input/InputTransport.h>
Jeff Brown5912f952013-07-01 19:10:31 -070027#include <utils/StopWatch.h>
28#include <utils/StrongPointer.h>
Josh Gao2ccbe3a2019-08-09 14:35:36 -070029#include <utils/Timers.h>
Jeff Brown5912f952013-07-01 19:10:31 -070030
31namespace android {
32
Siarhei Vishniakou8d660132024-01-11 16:48:44 -080033namespace {
34bool 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 Brown5912f952013-07-01 19:10:31 -070048class InputChannelTest : public testing::Test {
Jeff Brown5912f952013-07-01 19:10:31 -070049};
50
Siarhei Vishniakou8d660132024-01-11 16:48:44 -080051TEST_F(InputChannelTest, ClientAndServerTokensMatch) {
52 std::unique_ptr<InputChannel> serverChannel, clientChannel;
Jeff Brown5912f952013-07-01 19:10:31 -070053
Siarhei Vishniakou8d660132024-01-11 16:48:44 -080054 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 Brown5912f952013-07-01 19:10:31 -070058}
59
60TEST_F(InputChannelTest, OpenInputChannelPair_ReturnsAPairOfConnectedChannels) {
Siarhei Vishniakoud2588272020-07-10 11:15:40 -050061 std::unique_ptr<InputChannel> serverChannel, clientChannel;
Jeff Brown5912f952013-07-01 19:10:31 -070062
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -080063 status_t result = InputChannel::openInputChannelPair("channel name",
Jeff Brown5912f952013-07-01 19:10:31 -070064 serverChannel, clientChannel);
65
Siarhei Vishniakou8d660132024-01-11 16:48:44 -080066 ASSERT_EQ(OK, result) << "should have successfully opened a channel pair";
Jeff Brown5912f952013-07-01 19:10:31 -070067
Siarhei Vishniakou4125ea42024-07-01 16:38:17 -070068 EXPECT_EQ(serverChannel->getName(), clientChannel->getName());
Jeff Brown5912f952013-07-01 19:10:31 -070069
70 // Server->Client communication
Egor Paskoa0d32af2023-12-14 17:45:41 +010071 InputMessage serverMsg = {};
Siarhei Vishniakou52402772019-10-22 09:32:30 -070072 serverMsg.header.type = InputMessage::Type::KEY;
Jeff Brown5912f952013-07-01 19:10:31 -070073 serverMsg.body.key.action = AKEY_EVENT_ACTION_DOWN;
74 EXPECT_EQ(OK, serverChannel->sendMessage(&serverMsg))
75 << "server channel should be able to send message to client channel";
76
Paul Ramirez0c25e862024-06-18 21:33:33 +000077 android::base::Result<InputMessage> clientMsgResult = clientChannel->receiveMessage();
78 ASSERT_TRUE(clientMsgResult.ok())
Jeff Brown5912f952013-07-01 19:10:31 -070079 << "client channel should be able to receive message from server channel";
Paul Ramirez0c25e862024-06-18 21:33:33 +000080 const InputMessage& clientMsg = *clientMsgResult;
Jeff Brown5912f952013-07-01 19:10:31 -070081 EXPECT_EQ(serverMsg.header.type, clientMsg.header.type)
82 << "client channel should receive the correct message from server channel";
83 EXPECT_EQ(serverMsg.body.key.action, clientMsg.body.key.action)
84 << "client channel should receive the correct message from server channel";
85
86 // Client->Server communication
Egor Paskoa0d32af2023-12-14 17:45:41 +010087 InputMessage clientReply = {};
Siarhei Vishniakou52402772019-10-22 09:32:30 -070088 clientReply.header.type = InputMessage::Type::FINISHED;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -050089 clientReply.header.seq = 0x11223344;
Jeff Brown5912f952013-07-01 19:10:31 -070090 clientReply.body.finished.handled = true;
91 EXPECT_EQ(OK, clientChannel->sendMessage(&clientReply))
92 << "client channel should be able to send message to server channel";
93
Paul Ramirez0c25e862024-06-18 21:33:33 +000094 android::base::Result<InputMessage> serverReplyResult = serverChannel->receiveMessage();
95 ASSERT_TRUE(serverReplyResult.ok())
Jeff Brown5912f952013-07-01 19:10:31 -070096 << "server channel should be able to receive message from client channel";
Paul Ramirez0c25e862024-06-18 21:33:33 +000097 const InputMessage& serverReply = *serverReplyResult;
Jeff Brown5912f952013-07-01 19:10:31 -070098 EXPECT_EQ(clientReply.header.type, serverReply.header.type)
99 << "server channel should receive the correct message from client channel";
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500100 EXPECT_EQ(clientReply.header.seq, serverReply.header.seq)
Jeff Brown5912f952013-07-01 19:10:31 -0700101 << "server channel should receive the correct message from client channel";
102 EXPECT_EQ(clientReply.body.finished.handled, serverReply.body.finished.handled)
103 << "server channel should receive the correct message from client channel";
104}
105
Egor Paskoa0d32af2023-12-14 17:45:41 +0100106TEST_F(InputChannelTest, ProbablyHasInput) {
107 std::unique_ptr<InputChannel> senderChannel, receiverChannel;
108
109 // Open a pair of channels.
110 status_t result =
111 InputChannel::openInputChannelPair("channel name", senderChannel, receiverChannel);
112 ASSERT_EQ(OK, result) << "should have successfully opened a channel pair";
113
114 ASSERT_FALSE(receiverChannel->probablyHasInput());
115
116 // Send one message.
117 InputMessage serverMsg = {};
118 serverMsg.header.type = InputMessage::Type::KEY;
119 serverMsg.body.key.action = AKEY_EVENT_ACTION_DOWN;
120 EXPECT_EQ(OK, senderChannel->sendMessage(&serverMsg))
121 << "server channel should be able to send message to client channel";
122
123 // Verify input is available.
124 bool hasInput = false;
125 do {
126 // The probablyHasInput() can return false positive under rare circumstances uncontrollable
127 // by the tests. Re-request the availability in this case. Returning |false| for a long
128 // time is not intended, and would cause a test timeout.
129 hasInput = receiverChannel->probablyHasInput();
130 } while (!hasInput);
131 EXPECT_TRUE(hasInput)
132 << "client channel should observe that message is available before receiving it";
133
134 // Receive (consume) the message.
Paul Ramirez0c25e862024-06-18 21:33:33 +0000135 android::base::Result<InputMessage> clientMsgResult = receiverChannel->receiveMessage();
136 ASSERT_TRUE(clientMsgResult.ok())
Egor Paskoa0d32af2023-12-14 17:45:41 +0100137 << "client channel should be able to receive message from server channel";
Paul Ramirez0c25e862024-06-18 21:33:33 +0000138 const InputMessage& clientMsg = *clientMsgResult;
Egor Paskoa0d32af2023-12-14 17:45:41 +0100139 EXPECT_EQ(serverMsg.header.type, clientMsg.header.type)
140 << "client channel should receive the correct message from server channel";
141 EXPECT_EQ(serverMsg.body.key.action, clientMsg.body.key.action)
142 << "client channel should receive the correct message from server channel";
143
144 // Verify input is not available.
145 EXPECT_FALSE(receiverChannel->probablyHasInput())
146 << "client should not observe any more messages after receiving the single one";
147}
148
Jeff Brown5912f952013-07-01 19:10:31 -0700149TEST_F(InputChannelTest, ReceiveSignal_WhenNoSignalPresent_ReturnsAnError) {
Siarhei Vishniakoud2588272020-07-10 11:15:40 -0500150 std::unique_ptr<InputChannel> serverChannel, clientChannel;
Jeff Brown5912f952013-07-01 19:10:31 -0700151
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800152 status_t result = InputChannel::openInputChannelPair("channel name",
Jeff Brown5912f952013-07-01 19:10:31 -0700153 serverChannel, clientChannel);
154
155 ASSERT_EQ(OK, result)
156 << "should have successfully opened a channel pair";
157
Paul Ramirez0c25e862024-06-18 21:33:33 +0000158 android::base::Result<InputMessage> msgResult = clientChannel->receiveMessage();
159 EXPECT_EQ(WOULD_BLOCK, msgResult.error().code())
Jeff Brown5912f952013-07-01 19:10:31 -0700160 << "receiveMessage should have returned WOULD_BLOCK";
161}
162
163TEST_F(InputChannelTest, ReceiveSignal_WhenPeerClosed_ReturnsAnError) {
Siarhei Vishniakoud2588272020-07-10 11:15:40 -0500164 std::unique_ptr<InputChannel> serverChannel, clientChannel;
Jeff Brown5912f952013-07-01 19:10:31 -0700165
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800166 status_t result = InputChannel::openInputChannelPair("channel name",
Jeff Brown5912f952013-07-01 19:10:31 -0700167 serverChannel, clientChannel);
168
169 ASSERT_EQ(OK, result)
170 << "should have successfully opened a channel pair";
171
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500172 serverChannel.reset(); // close server channel
Jeff Brown5912f952013-07-01 19:10:31 -0700173
Paul Ramirez0c25e862024-06-18 21:33:33 +0000174 android::base::Result<InputMessage> msgResult = clientChannel->receiveMessage();
175 EXPECT_EQ(DEAD_OBJECT, msgResult.error().code())
Jeff Brown5912f952013-07-01 19:10:31 -0700176 << "receiveMessage should have returned DEAD_OBJECT";
177}
178
179TEST_F(InputChannelTest, SendSignal_WhenPeerClosed_ReturnsAnError) {
Siarhei Vishniakoud2588272020-07-10 11:15:40 -0500180 std::unique_ptr<InputChannel> serverChannel, clientChannel;
Jeff Brown5912f952013-07-01 19:10:31 -0700181
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800182 status_t result = InputChannel::openInputChannelPair("channel name",
Jeff Brown5912f952013-07-01 19:10:31 -0700183 serverChannel, clientChannel);
184
185 ASSERT_EQ(OK, result)
186 << "should have successfully opened a channel pair";
187
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500188 serverChannel.reset(); // close server channel
Jeff Brown5912f952013-07-01 19:10:31 -0700189
190 InputMessage msg;
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700191 msg.header.type = InputMessage::Type::KEY;
Jeff Brown5912f952013-07-01 19:10:31 -0700192 EXPECT_EQ(DEAD_OBJECT, clientChannel->sendMessage(&msg))
193 << "sendMessage should have returned DEAD_OBJECT";
194}
195
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800196TEST_F(InputChannelTest, SendAndReceive_MotionClassification) {
Siarhei Vishniakoud2588272020-07-10 11:15:40 -0500197 std::unique_ptr<InputChannel> serverChannel, clientChannel;
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800198 status_t result = InputChannel::openInputChannelPair("channel name",
199 serverChannel, clientChannel);
200 ASSERT_EQ(OK, result)
201 << "should have successfully opened a channel pair";
202
203 std::array<MotionClassification, 3> classifications = {
204 MotionClassification::NONE,
205 MotionClassification::AMBIGUOUS_GESTURE,
206 MotionClassification::DEEP_PRESS,
207 };
208
Paul Ramirez0c25e862024-06-18 21:33:33 +0000209 InputMessage serverMsg = {};
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700210 serverMsg.header.type = InputMessage::Type::MOTION;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500211 serverMsg.header.seq = 1;
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800212 serverMsg.body.motion.pointerCount = 1;
213
214 for (MotionClassification classification : classifications) {
215 // Send and receive a message with classification
216 serverMsg.body.motion.classification = classification;
217 EXPECT_EQ(OK, serverChannel->sendMessage(&serverMsg))
218 << "server channel should be able to send message to client channel";
219
Paul Ramirez0c25e862024-06-18 21:33:33 +0000220 android::base::Result<InputMessage> clientMsgResult = clientChannel->receiveMessage();
221 ASSERT_TRUE(clientMsgResult.ok())
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800222 << "client channel should be able to receive message from server channel";
Paul Ramirez0c25e862024-06-18 21:33:33 +0000223 const InputMessage& clientMsg = *clientMsgResult;
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800224 EXPECT_EQ(serverMsg.header.type, clientMsg.header.type);
Paul Ramirez0c25e862024-06-18 21:33:33 +0000225 EXPECT_EQ(classification, clientMsg.body.motion.classification)
226 << "Expected to receive " << motionClassificationToString(classification);
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800227 }
228}
229
Chris Ye0783e992020-06-02 21:34:49 -0700230TEST_F(InputChannelTest, DuplicateChannelAndAssertEqual) {
Siarhei Vishniakoud2588272020-07-10 11:15:40 -0500231 std::unique_ptr<InputChannel> serverChannel, clientChannel;
Chris Ye0783e992020-06-02 21:34:49 -0700232
233 status_t result =
234 InputChannel::openInputChannelPair("channel dup", serverChannel, clientChannel);
235
236 ASSERT_EQ(OK, result) << "should have successfully opened a channel pair";
237
Siarhei Vishniakoud2588272020-07-10 11:15:40 -0500238 std::unique_ptr<InputChannel> dupChan = serverChannel->dup();
Chris Ye0783e992020-06-02 21:34:49 -0700239
240 EXPECT_EQ(*serverChannel == *dupChan, true) << "inputchannel should be equal after duplication";
241}
Jeff Brown5912f952013-07-01 19:10:31 -0700242
243} // namespace android