blob: 0661261003e4a9905bdd58a44fc47f028f520174 [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
84 InputMessage serverMsg;
85 memset(&serverMsg, 0, sizeof(InputMessage));
Siarhei Vishniakou52402772019-10-22 09:32:30 -070086 serverMsg.header.type = InputMessage::Type::KEY;
Jeff Brown5912f952013-07-01 19:10:31 -070087 serverMsg.body.key.action = AKEY_EVENT_ACTION_DOWN;
88 EXPECT_EQ(OK, serverChannel->sendMessage(&serverMsg))
89 << "server channel should be able to send message to client channel";
90
91 InputMessage clientMsg;
92 EXPECT_EQ(OK, clientChannel->receiveMessage(&clientMsg))
93 << "client channel should be able to receive message from server channel";
94 EXPECT_EQ(serverMsg.header.type, clientMsg.header.type)
95 << "client channel should receive the correct message from server channel";
96 EXPECT_EQ(serverMsg.body.key.action, clientMsg.body.key.action)
97 << "client channel should receive the correct message from server channel";
98
99 // Client->Server communication
100 InputMessage clientReply;
101 memset(&clientReply, 0, sizeof(InputMessage));
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700102 clientReply.header.type = InputMessage::Type::FINISHED;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500103 clientReply.header.seq = 0x11223344;
Jeff Brown5912f952013-07-01 19:10:31 -0700104 clientReply.body.finished.handled = true;
105 EXPECT_EQ(OK, clientChannel->sendMessage(&clientReply))
106 << "client channel should be able to send message to server channel";
107
108 InputMessage serverReply;
109 EXPECT_EQ(OK, serverChannel->receiveMessage(&serverReply))
110 << "server channel should be able to receive message from client channel";
111 EXPECT_EQ(clientReply.header.type, serverReply.header.type)
112 << "server channel should receive the correct message from client channel";
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500113 EXPECT_EQ(clientReply.header.seq, serverReply.header.seq)
Jeff Brown5912f952013-07-01 19:10:31 -0700114 << "server channel should receive the correct message from client channel";
115 EXPECT_EQ(clientReply.body.finished.handled, serverReply.body.finished.handled)
116 << "server channel should receive the correct message from client channel";
117}
118
119TEST_F(InputChannelTest, ReceiveSignal_WhenNoSignalPresent_ReturnsAnError) {
Siarhei Vishniakoud2588272020-07-10 11:15:40 -0500120 std::unique_ptr<InputChannel> serverChannel, clientChannel;
Jeff Brown5912f952013-07-01 19:10:31 -0700121
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800122 status_t result = InputChannel::openInputChannelPair("channel name",
Jeff Brown5912f952013-07-01 19:10:31 -0700123 serverChannel, clientChannel);
124
125 ASSERT_EQ(OK, result)
126 << "should have successfully opened a channel pair";
127
128 InputMessage msg;
129 EXPECT_EQ(WOULD_BLOCK, clientChannel->receiveMessage(&msg))
130 << "receiveMessage should have returned WOULD_BLOCK";
131}
132
133TEST_F(InputChannelTest, ReceiveSignal_WhenPeerClosed_ReturnsAnError) {
Siarhei Vishniakoud2588272020-07-10 11:15:40 -0500134 std::unique_ptr<InputChannel> serverChannel, clientChannel;
Jeff Brown5912f952013-07-01 19:10:31 -0700135
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800136 status_t result = InputChannel::openInputChannelPair("channel name",
Jeff Brown5912f952013-07-01 19:10:31 -0700137 serverChannel, clientChannel);
138
139 ASSERT_EQ(OK, result)
140 << "should have successfully opened a channel pair";
141
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500142 serverChannel.reset(); // close server channel
Jeff Brown5912f952013-07-01 19:10:31 -0700143
144 InputMessage msg;
145 EXPECT_EQ(DEAD_OBJECT, clientChannel->receiveMessage(&msg))
146 << "receiveMessage should have returned DEAD_OBJECT";
147}
148
149TEST_F(InputChannelTest, SendSignal_WhenPeerClosed_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
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500158 serverChannel.reset(); // close server channel
Jeff Brown5912f952013-07-01 19:10:31 -0700159
160 InputMessage msg;
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700161 msg.header.type = InputMessage::Type::KEY;
Jeff Brown5912f952013-07-01 19:10:31 -0700162 EXPECT_EQ(DEAD_OBJECT, clientChannel->sendMessage(&msg))
163 << "sendMessage should have returned DEAD_OBJECT";
164}
165
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800166TEST_F(InputChannelTest, SendAndReceive_MotionClassification) {
Siarhei Vishniakoud2588272020-07-10 11:15:40 -0500167 std::unique_ptr<InputChannel> serverChannel, clientChannel;
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800168 status_t result = InputChannel::openInputChannelPair("channel name",
169 serverChannel, clientChannel);
170 ASSERT_EQ(OK, result)
171 << "should have successfully opened a channel pair";
172
173 std::array<MotionClassification, 3> classifications = {
174 MotionClassification::NONE,
175 MotionClassification::AMBIGUOUS_GESTURE,
176 MotionClassification::DEEP_PRESS,
177 };
178
179 InputMessage serverMsg = {}, clientMsg;
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700180 serverMsg.header.type = InputMessage::Type::MOTION;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500181 serverMsg.header.seq = 1;
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800182 serverMsg.body.motion.pointerCount = 1;
183
184 for (MotionClassification classification : classifications) {
185 // Send and receive a message with classification
186 serverMsg.body.motion.classification = classification;
187 EXPECT_EQ(OK, serverChannel->sendMessage(&serverMsg))
188 << "server channel should be able to send message to client channel";
189
190 EXPECT_EQ(OK, clientChannel->receiveMessage(&clientMsg))
191 << "client channel should be able to receive message from server channel";
192 EXPECT_EQ(serverMsg.header.type, clientMsg.header.type);
193 EXPECT_EQ(classification, clientMsg.body.motion.classification) <<
194 "Expected to receive " << motionClassificationToString(classification);
195 }
196}
197
Chris Ye0783e992020-06-02 21:34:49 -0700198TEST_F(InputChannelTest, InputChannelParcelAndUnparcel) {
Siarhei Vishniakoud2588272020-07-10 11:15:40 -0500199 std::unique_ptr<InputChannel> serverChannel, clientChannel;
Chris Ye0783e992020-06-02 21:34:49 -0700200
201 status_t result =
202 InputChannel::openInputChannelPair("channel parceling", serverChannel, clientChannel);
203
204 ASSERT_EQ(OK, result) << "should have successfully opened a channel pair";
205
206 InputChannel chan;
207 Parcel parcel;
208 ASSERT_EQ(OK, serverChannel->writeToParcel(&parcel));
209 parcel.setDataPosition(0);
210 chan.readFromParcel(&parcel);
211
212 EXPECT_EQ(chan == *serverChannel, true)
213 << "inputchannel should be equal after parceling and unparceling.\n"
214 << "name " << chan.getName() << " name " << serverChannel->getName();
215}
216
217TEST_F(InputChannelTest, DuplicateChannelAndAssertEqual) {
Siarhei Vishniakoud2588272020-07-10 11:15:40 -0500218 std::unique_ptr<InputChannel> serverChannel, clientChannel;
Chris Ye0783e992020-06-02 21:34:49 -0700219
220 status_t result =
221 InputChannel::openInputChannelPair("channel dup", serverChannel, clientChannel);
222
223 ASSERT_EQ(OK, result) << "should have successfully opened a channel pair";
224
Siarhei Vishniakoud2588272020-07-10 11:15:40 -0500225 std::unique_ptr<InputChannel> dupChan = serverChannel->dup();
Chris Ye0783e992020-06-02 21:34:49 -0700226
227 EXPECT_EQ(*serverChannel == *dupChan, true) << "inputchannel should be equal after duplication";
228}
Jeff Brown5912f952013-07-01 19:10:31 -0700229
230} // namespace android