blob: 650c93034c31691a8fb633859e3324645f05b1da [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 "TestHelpers.h"
20
21#include <unistd.h>
22#include <time.h>
23#include <errno.h>
24
Josh Gao2ccbe3a2019-08-09 14:35:36 -070025#include <binder/Binder.h>
Chris Ye0783e992020-06-02 21:34:49 -070026#include <binder/Parcel.h>
Jeff Brown5912f952013-07-01 19:10:31 -070027#include <gtest/gtest.h>
28#include <input/InputTransport.h>
Jeff Brown5912f952013-07-01 19:10:31 -070029#include <utils/StopWatch.h>
30#include <utils/StrongPointer.h>
Josh Gao2ccbe3a2019-08-09 14:35:36 -070031#include <utils/Timers.h>
Jeff Brown5912f952013-07-01 19:10:31 -070032
33namespace android {
34
35class InputChannelTest : public testing::Test {
Jeff Brown5912f952013-07-01 19:10:31 -070036};
37
38
39TEST_F(InputChannelTest, ConstructorAndDestructor_TakesOwnershipOfFileDescriptors) {
40 // Our purpose here is to verify that the input channel destructor closes the
41 // file descriptor provided to it. One easy way is to provide it with one end
42 // of a pipe and to check for EPIPE on the other end after the channel is destroyed.
43 Pipe pipe;
44
Josh Gao2ccbe3a2019-08-09 14:35:36 -070045 android::base::unique_fd sendFd(pipe.sendFd);
Jeff Brown5912f952013-07-01 19:10:31 -070046
Siarhei Vishniakoud2588272020-07-10 11:15:40 -050047 std::unique_ptr<InputChannel> inputChannel =
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -070048 InputChannel::create("channel name", std::move(sendFd), new BBinder());
Josh Gao2ccbe3a2019-08-09 14:35:36 -070049
50 EXPECT_NE(inputChannel, nullptr) << "channel should be successfully created";
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -080051 EXPECT_STREQ("channel name", inputChannel->getName().c_str())
Jeff Brown5912f952013-07-01 19:10:31 -070052 << "channel should have provided name";
Josh Gao2ccbe3a2019-08-09 14:35:36 -070053 EXPECT_NE(-1, inputChannel->getFd()) << "channel should have valid fd";
Jeff Brown5912f952013-07-01 19:10:31 -070054
Josh Gao2ccbe3a2019-08-09 14:35:36 -070055 // InputChannel should be the owner of the file descriptor now
56 ASSERT_FALSE(sendFd.ok());
57}
Jeff Brown5912f952013-07-01 19:10:31 -070058
Josh Gao2ccbe3a2019-08-09 14:35:36 -070059TEST_F(InputChannelTest, SetAndGetToken) {
60 Pipe pipe;
Josh Gao2ccbe3a2019-08-09 14:35:36 -070061 sp<IBinder> token = new BBinder();
Siarhei Vishniakoud2588272020-07-10 11:15:40 -050062 std::unique_ptr<InputChannel> channel =
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -070063 InputChannel::create("test channel", android::base::unique_fd(pipe.sendFd), token);
64
65 EXPECT_EQ(token, channel->getConnectionToken());
Jeff Brown5912f952013-07-01 19:10:31 -070066}
67
68TEST_F(InputChannelTest, OpenInputChannelPair_ReturnsAPairOfConnectedChannels) {
Siarhei Vishniakoud2588272020-07-10 11:15:40 -050069 std::unique_ptr<InputChannel> serverChannel, clientChannel;
Jeff Brown5912f952013-07-01 19:10:31 -070070
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -080071 status_t result = InputChannel::openInputChannelPair("channel name",
Jeff Brown5912f952013-07-01 19:10:31 -070072 serverChannel, clientChannel);
73
74 ASSERT_EQ(OK, result)
75 << "should have successfully opened a channel pair";
76
77 // Name
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -080078 EXPECT_STREQ("channel name (server)", serverChannel->getName().c_str())
Jeff Brown5912f952013-07-01 19:10:31 -070079 << "server channel should have suffixed name";
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -080080 EXPECT_STREQ("channel name (client)", clientChannel->getName().c_str())
Jeff Brown5912f952013-07-01 19:10:31 -070081 << "client channel should have suffixed name";
82
83 // Server->Client communication
Egor Paskoa0d32af2023-12-14 17:45:41 +010084 InputMessage serverMsg = {};
Siarhei Vishniakou52402772019-10-22 09:32:30 -070085 serverMsg.header.type = InputMessage::Type::KEY;
Jeff Brown5912f952013-07-01 19:10:31 -070086 serverMsg.body.key.action = AKEY_EVENT_ACTION_DOWN;
87 EXPECT_EQ(OK, serverChannel->sendMessage(&serverMsg))
88 << "server channel should be able to send message to client channel";
89
90 InputMessage clientMsg;
91 EXPECT_EQ(OK, clientChannel->receiveMessage(&clientMsg))
92 << "client channel should be able to receive message from server channel";
93 EXPECT_EQ(serverMsg.header.type, clientMsg.header.type)
94 << "client channel should receive the correct message from server channel";
95 EXPECT_EQ(serverMsg.body.key.action, clientMsg.body.key.action)
96 << "client channel should receive the correct message from server channel";
97
98 // Client->Server communication
Egor Paskoa0d32af2023-12-14 17:45:41 +010099 InputMessage clientReply = {};
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700100 clientReply.header.type = InputMessage::Type::FINISHED;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500101 clientReply.header.seq = 0x11223344;
Jeff Brown5912f952013-07-01 19:10:31 -0700102 clientReply.body.finished.handled = true;
103 EXPECT_EQ(OK, clientChannel->sendMessage(&clientReply))
104 << "client channel should be able to send message to server channel";
105
106 InputMessage serverReply;
107 EXPECT_EQ(OK, serverChannel->receiveMessage(&serverReply))
108 << "server channel should be able to receive message from client channel";
109 EXPECT_EQ(clientReply.header.type, serverReply.header.type)
110 << "server channel should receive the correct message from client channel";
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500111 EXPECT_EQ(clientReply.header.seq, serverReply.header.seq)
Jeff Brown5912f952013-07-01 19:10:31 -0700112 << "server channel should receive the correct message from client channel";
113 EXPECT_EQ(clientReply.body.finished.handled, serverReply.body.finished.handled)
114 << "server channel should receive the correct message from client channel";
115}
116
Egor Paskoa0d32af2023-12-14 17:45:41 +0100117TEST_F(InputChannelTest, ProbablyHasInput) {
118 std::unique_ptr<InputChannel> senderChannel, receiverChannel;
119
120 // Open a pair of channels.
121 status_t result =
122 InputChannel::openInputChannelPair("channel name", senderChannel, receiverChannel);
123 ASSERT_EQ(OK, result) << "should have successfully opened a channel pair";
124
125 ASSERT_FALSE(receiverChannel->probablyHasInput());
126
127 // Send one message.
128 InputMessage serverMsg = {};
129 serverMsg.header.type = InputMessage::Type::KEY;
130 serverMsg.body.key.action = AKEY_EVENT_ACTION_DOWN;
131 EXPECT_EQ(OK, senderChannel->sendMessage(&serverMsg))
132 << "server channel should be able to send message to client channel";
133
134 // Verify input is available.
135 bool hasInput = false;
136 do {
137 // The probablyHasInput() can return false positive under rare circumstances uncontrollable
138 // by the tests. Re-request the availability in this case. Returning |false| for a long
139 // time is not intended, and would cause a test timeout.
140 hasInput = receiverChannel->probablyHasInput();
141 } while (!hasInput);
142 EXPECT_TRUE(hasInput)
143 << "client channel should observe that message is available before receiving it";
144
145 // Receive (consume) the message.
146 InputMessage clientMsg;
147 EXPECT_EQ(OK, receiverChannel->receiveMessage(&clientMsg))
148 << "client channel should be able to receive message from server channel";
149 EXPECT_EQ(serverMsg.header.type, clientMsg.header.type)
150 << "client channel should receive the correct message from server channel";
151 EXPECT_EQ(serverMsg.body.key.action, clientMsg.body.key.action)
152 << "client channel should receive the correct message from server channel";
153
154 // Verify input is not available.
155 EXPECT_FALSE(receiverChannel->probablyHasInput())
156 << "client should not observe any more messages after receiving the single one";
157}
158
Jeff Brown5912f952013-07-01 19:10:31 -0700159TEST_F(InputChannelTest, ReceiveSignal_WhenNoSignalPresent_ReturnsAnError) {
Siarhei Vishniakoud2588272020-07-10 11:15:40 -0500160 std::unique_ptr<InputChannel> serverChannel, clientChannel;
Jeff Brown5912f952013-07-01 19:10:31 -0700161
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800162 status_t result = InputChannel::openInputChannelPair("channel name",
Jeff Brown5912f952013-07-01 19:10:31 -0700163 serverChannel, clientChannel);
164
165 ASSERT_EQ(OK, result)
166 << "should have successfully opened a channel pair";
167
168 InputMessage msg;
169 EXPECT_EQ(WOULD_BLOCK, clientChannel->receiveMessage(&msg))
170 << "receiveMessage should have returned WOULD_BLOCK";
171}
172
173TEST_F(InputChannelTest, ReceiveSignal_WhenPeerClosed_ReturnsAnError) {
Siarhei Vishniakoud2588272020-07-10 11:15:40 -0500174 std::unique_ptr<InputChannel> serverChannel, clientChannel;
Jeff Brown5912f952013-07-01 19:10:31 -0700175
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800176 status_t result = InputChannel::openInputChannelPair("channel name",
Jeff Brown5912f952013-07-01 19:10:31 -0700177 serverChannel, clientChannel);
178
179 ASSERT_EQ(OK, result)
180 << "should have successfully opened a channel pair";
181
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500182 serverChannel.reset(); // close server channel
Jeff Brown5912f952013-07-01 19:10:31 -0700183
184 InputMessage msg;
185 EXPECT_EQ(DEAD_OBJECT, clientChannel->receiveMessage(&msg))
186 << "receiveMessage should have returned DEAD_OBJECT";
187}
188
189TEST_F(InputChannelTest, SendSignal_WhenPeerClosed_ReturnsAnError) {
Siarhei Vishniakoud2588272020-07-10 11:15:40 -0500190 std::unique_ptr<InputChannel> serverChannel, clientChannel;
Jeff Brown5912f952013-07-01 19:10:31 -0700191
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800192 status_t result = InputChannel::openInputChannelPair("channel name",
Jeff Brown5912f952013-07-01 19:10:31 -0700193 serverChannel, clientChannel);
194
195 ASSERT_EQ(OK, result)
196 << "should have successfully opened a channel pair";
197
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500198 serverChannel.reset(); // close server channel
Jeff Brown5912f952013-07-01 19:10:31 -0700199
200 InputMessage msg;
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700201 msg.header.type = InputMessage::Type::KEY;
Jeff Brown5912f952013-07-01 19:10:31 -0700202 EXPECT_EQ(DEAD_OBJECT, clientChannel->sendMessage(&msg))
203 << "sendMessage should have returned DEAD_OBJECT";
204}
205
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800206TEST_F(InputChannelTest, SendAndReceive_MotionClassification) {
Siarhei Vishniakoud2588272020-07-10 11:15:40 -0500207 std::unique_ptr<InputChannel> serverChannel, clientChannel;
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800208 status_t result = InputChannel::openInputChannelPair("channel name",
209 serverChannel, clientChannel);
210 ASSERT_EQ(OK, result)
211 << "should have successfully opened a channel pair";
212
213 std::array<MotionClassification, 3> classifications = {
214 MotionClassification::NONE,
215 MotionClassification::AMBIGUOUS_GESTURE,
216 MotionClassification::DEEP_PRESS,
217 };
218
219 InputMessage serverMsg = {}, clientMsg;
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700220 serverMsg.header.type = InputMessage::Type::MOTION;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500221 serverMsg.header.seq = 1;
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800222 serverMsg.body.motion.pointerCount = 1;
223
224 for (MotionClassification classification : classifications) {
225 // Send and receive a message with classification
226 serverMsg.body.motion.classification = classification;
227 EXPECT_EQ(OK, serverChannel->sendMessage(&serverMsg))
228 << "server channel should be able to send message to client channel";
229
230 EXPECT_EQ(OK, clientChannel->receiveMessage(&clientMsg))
231 << "client channel should be able to receive message from server channel";
232 EXPECT_EQ(serverMsg.header.type, clientMsg.header.type);
233 EXPECT_EQ(classification, clientMsg.body.motion.classification) <<
234 "Expected to receive " << motionClassificationToString(classification);
235 }
236}
237
Chris Ye0783e992020-06-02 21:34:49 -0700238TEST_F(InputChannelTest, InputChannelParcelAndUnparcel) {
Siarhei Vishniakoud2588272020-07-10 11:15:40 -0500239 std::unique_ptr<InputChannel> serverChannel, clientChannel;
Chris Ye0783e992020-06-02 21:34:49 -0700240
241 status_t result =
242 InputChannel::openInputChannelPair("channel parceling", serverChannel, clientChannel);
243
244 ASSERT_EQ(OK, result) << "should have successfully opened a channel pair";
245
246 InputChannel chan;
247 Parcel parcel;
248 ASSERT_EQ(OK, serverChannel->writeToParcel(&parcel));
249 parcel.setDataPosition(0);
250 chan.readFromParcel(&parcel);
251
252 EXPECT_EQ(chan == *serverChannel, true)
253 << "inputchannel should be equal after parceling and unparceling.\n"
254 << "name " << chan.getName() << " name " << serverChannel->getName();
255}
256
257TEST_F(InputChannelTest, DuplicateChannelAndAssertEqual) {
Siarhei Vishniakoud2588272020-07-10 11:15:40 -0500258 std::unique_ptr<InputChannel> serverChannel, clientChannel;
Chris Ye0783e992020-06-02 21:34:49 -0700259
260 status_t result =
261 InputChannel::openInputChannelPair("channel dup", serverChannel, clientChannel);
262
263 ASSERT_EQ(OK, result) << "should have successfully opened a channel pair";
264
Siarhei Vishniakoud2588272020-07-10 11:15:40 -0500265 std::unique_ptr<InputChannel> dupChan = serverChannel->dup();
Chris Ye0783e992020-06-02 21:34:49 -0700266
267 EXPECT_EQ(*serverChannel == *dupChan, true) << "inputchannel should be equal after duplication";
268}
Jeff Brown5912f952013-07-01 19:10:31 -0700269
270} // namespace android