blob: 7f4bd6eac23cc35af8741641668e775b80f880bb [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 {
36protected:
37 virtual void SetUp() { }
38 virtual void TearDown() { }
39};
40
41
42TEST_F(InputChannelTest, ConstructorAndDestructor_TakesOwnershipOfFileDescriptors) {
43 // Our purpose here is to verify that the input channel destructor closes the
44 // file descriptor provided to it. One easy way is to provide it with one end
45 // of a pipe and to check for EPIPE on the other end after the channel is destroyed.
46 Pipe pipe;
47
Josh Gao2ccbe3a2019-08-09 14:35:36 -070048 android::base::unique_fd sendFd(pipe.sendFd);
Jeff Brown5912f952013-07-01 19:10:31 -070049
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -070050 sp<InputChannel> inputChannel =
51 InputChannel::create("channel name", std::move(sendFd), new BBinder());
Josh Gao2ccbe3a2019-08-09 14:35:36 -070052
53 EXPECT_NE(inputChannel, nullptr) << "channel should be successfully created";
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -080054 EXPECT_STREQ("channel name", inputChannel->getName().c_str())
Jeff Brown5912f952013-07-01 19:10:31 -070055 << "channel should have provided name";
Josh Gao2ccbe3a2019-08-09 14:35:36 -070056 EXPECT_NE(-1, inputChannel->getFd()) << "channel should have valid fd";
Jeff Brown5912f952013-07-01 19:10:31 -070057
Josh Gao2ccbe3a2019-08-09 14:35:36 -070058 // InputChannel should be the owner of the file descriptor now
59 ASSERT_FALSE(sendFd.ok());
60}
Jeff Brown5912f952013-07-01 19:10:31 -070061
Josh Gao2ccbe3a2019-08-09 14:35:36 -070062TEST_F(InputChannelTest, SetAndGetToken) {
63 Pipe pipe;
Josh Gao2ccbe3a2019-08-09 14:35:36 -070064 sp<IBinder> token = new BBinder();
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -070065 sp<InputChannel> channel =
66 InputChannel::create("test channel", android::base::unique_fd(pipe.sendFd), token);
67
68 EXPECT_EQ(token, channel->getConnectionToken());
Jeff Brown5912f952013-07-01 19:10:31 -070069}
70
71TEST_F(InputChannelTest, OpenInputChannelPair_ReturnsAPairOfConnectedChannels) {
72 sp<InputChannel> serverChannel, clientChannel;
73
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -080074 status_t result = InputChannel::openInputChannelPair("channel name",
Jeff Brown5912f952013-07-01 19:10:31 -070075 serverChannel, clientChannel);
76
77 ASSERT_EQ(OK, result)
78 << "should have successfully opened a channel pair";
79
80 // Name
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -080081 EXPECT_STREQ("channel name (server)", serverChannel->getName().c_str())
Jeff Brown5912f952013-07-01 19:10:31 -070082 << "server channel should have suffixed name";
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -080083 EXPECT_STREQ("channel name (client)", clientChannel->getName().c_str())
Jeff Brown5912f952013-07-01 19:10:31 -070084 << "client channel should have suffixed name";
85
86 // Server->Client communication
87 InputMessage serverMsg;
88 memset(&serverMsg, 0, sizeof(InputMessage));
Siarhei Vishniakou52402772019-10-22 09:32:30 -070089 serverMsg.header.type = InputMessage::Type::KEY;
Jeff Brown5912f952013-07-01 19:10:31 -070090 serverMsg.body.key.action = AKEY_EVENT_ACTION_DOWN;
91 EXPECT_EQ(OK, serverChannel->sendMessage(&serverMsg))
92 << "server channel should be able to send message to client channel";
93
94 InputMessage clientMsg;
95 EXPECT_EQ(OK, clientChannel->receiveMessage(&clientMsg))
96 << "client channel should be able to receive message from server channel";
97 EXPECT_EQ(serverMsg.header.type, clientMsg.header.type)
98 << "client channel should receive the correct message from server channel";
99 EXPECT_EQ(serverMsg.body.key.action, clientMsg.body.key.action)
100 << "client channel should receive the correct message from server channel";
101
102 // Client->Server communication
103 InputMessage clientReply;
104 memset(&clientReply, 0, sizeof(InputMessage));
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700105 clientReply.header.type = InputMessage::Type::FINISHED;
Jeff Brown5912f952013-07-01 19:10:31 -0700106 clientReply.body.finished.seq = 0x11223344;
107 clientReply.body.finished.handled = true;
108 EXPECT_EQ(OK, clientChannel->sendMessage(&clientReply))
109 << "client channel should be able to send message to server channel";
110
111 InputMessage serverReply;
112 EXPECT_EQ(OK, serverChannel->receiveMessage(&serverReply))
113 << "server channel should be able to receive message from client channel";
114 EXPECT_EQ(clientReply.header.type, serverReply.header.type)
115 << "server channel should receive the correct message from client channel";
116 EXPECT_EQ(clientReply.body.finished.seq, serverReply.body.finished.seq)
117 << "server channel should receive the correct message from client channel";
118 EXPECT_EQ(clientReply.body.finished.handled, serverReply.body.finished.handled)
119 << "server channel should receive the correct message from client channel";
120}
121
122TEST_F(InputChannelTest, ReceiveSignal_WhenNoSignalPresent_ReturnsAnError) {
123 sp<InputChannel> serverChannel, clientChannel;
124
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800125 status_t result = InputChannel::openInputChannelPair("channel name",
Jeff Brown5912f952013-07-01 19:10:31 -0700126 serverChannel, clientChannel);
127
128 ASSERT_EQ(OK, result)
129 << "should have successfully opened a channel pair";
130
131 InputMessage msg;
132 EXPECT_EQ(WOULD_BLOCK, clientChannel->receiveMessage(&msg))
133 << "receiveMessage should have returned WOULD_BLOCK";
134}
135
136TEST_F(InputChannelTest, ReceiveSignal_WhenPeerClosed_ReturnsAnError) {
137 sp<InputChannel> serverChannel, clientChannel;
138
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800139 status_t result = InputChannel::openInputChannelPair("channel name",
Jeff Brown5912f952013-07-01 19:10:31 -0700140 serverChannel, clientChannel);
141
142 ASSERT_EQ(OK, result)
143 << "should have successfully opened a channel pair";
144
145 serverChannel.clear(); // close server channel
146
147 InputMessage msg;
148 EXPECT_EQ(DEAD_OBJECT, clientChannel->receiveMessage(&msg))
149 << "receiveMessage should have returned DEAD_OBJECT";
150}
151
152TEST_F(InputChannelTest, SendSignal_WhenPeerClosed_ReturnsAnError) {
153 sp<InputChannel> serverChannel, clientChannel;
154
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800155 status_t result = InputChannel::openInputChannelPair("channel name",
Jeff Brown5912f952013-07-01 19:10:31 -0700156 serverChannel, clientChannel);
157
158 ASSERT_EQ(OK, result)
159 << "should have successfully opened a channel pair";
160
161 serverChannel.clear(); // close server channel
162
163 InputMessage msg;
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700164 msg.header.type = InputMessage::Type::KEY;
Jeff Brown5912f952013-07-01 19:10:31 -0700165 EXPECT_EQ(DEAD_OBJECT, clientChannel->sendMessage(&msg))
166 << "sendMessage should have returned DEAD_OBJECT";
167}
168
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800169TEST_F(InputChannelTest, SendAndReceive_MotionClassification) {
170 sp<InputChannel> serverChannel, clientChannel;
171 status_t result = InputChannel::openInputChannelPair("channel name",
172 serverChannel, clientChannel);
173 ASSERT_EQ(OK, result)
174 << "should have successfully opened a channel pair";
175
176 std::array<MotionClassification, 3> classifications = {
177 MotionClassification::NONE,
178 MotionClassification::AMBIGUOUS_GESTURE,
179 MotionClassification::DEEP_PRESS,
180 };
181
182 InputMessage serverMsg = {}, clientMsg;
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700183 serverMsg.header.type = InputMessage::Type::MOTION;
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800184 serverMsg.body.motion.seq = 1;
185 serverMsg.body.motion.pointerCount = 1;
186
187 for (MotionClassification classification : classifications) {
188 // Send and receive a message with classification
189 serverMsg.body.motion.classification = classification;
190 EXPECT_EQ(OK, serverChannel->sendMessage(&serverMsg))
191 << "server channel should be able to send message to client channel";
192
193 EXPECT_EQ(OK, clientChannel->receiveMessage(&clientMsg))
194 << "client channel should be able to receive message from server channel";
195 EXPECT_EQ(serverMsg.header.type, clientMsg.header.type);
196 EXPECT_EQ(classification, clientMsg.body.motion.classification) <<
197 "Expected to receive " << motionClassificationToString(classification);
198 }
199}
200
Chris Ye0783e992020-06-02 21:34:49 -0700201TEST_F(InputChannelTest, InputChannelParcelAndUnparcel) {
202 sp<InputChannel> serverChannel, clientChannel;
203
204 status_t result =
205 InputChannel::openInputChannelPair("channel parceling", serverChannel, clientChannel);
206
207 ASSERT_EQ(OK, result) << "should have successfully opened a channel pair";
208
209 InputChannel chan;
210 Parcel parcel;
211 ASSERT_EQ(OK, serverChannel->writeToParcel(&parcel));
212 parcel.setDataPosition(0);
213 chan.readFromParcel(&parcel);
214
215 EXPECT_EQ(chan == *serverChannel, true)
216 << "inputchannel should be equal after parceling and unparceling.\n"
217 << "name " << chan.getName() << " name " << serverChannel->getName();
218}
219
220TEST_F(InputChannelTest, DuplicateChannelAndAssertEqual) {
221 sp<InputChannel> serverChannel, clientChannel;
222
223 status_t result =
224 InputChannel::openInputChannelPair("channel dup", serverChannel, clientChannel);
225
226 ASSERT_EQ(OK, result) << "should have successfully opened a channel pair";
227
228 sp<InputChannel> dupChan = serverChannel->dup();
229
230 EXPECT_EQ(*serverChannel == *dupChan, true) << "inputchannel should be equal after duplication";
231}
Jeff Brown5912f952013-07-01 19:10:31 -0700232
233} // namespace android