blob: ada275d014232418c64867af494d7a7f633bd375 [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>
Jeff Brown5912f952013-07-01 19:10:31 -070026#include <gtest/gtest.h>
27#include <input/InputTransport.h>
Jeff Brown5912f952013-07-01 19:10:31 -070028#include <utils/StopWatch.h>
29#include <utils/StrongPointer.h>
Josh Gao2ccbe3a2019-08-09 14:35:36 -070030#include <utils/Timers.h>
Jeff Brown5912f952013-07-01 19:10:31 -070031
32namespace android {
33
34class InputChannelTest : public testing::Test {
35protected:
36 virtual void SetUp() { }
37 virtual void TearDown() { }
38};
39
40
41TEST_F(InputChannelTest, ConstructorAndDestructor_TakesOwnershipOfFileDescriptors) {
42 // Our purpose here is to verify that the input channel destructor closes the
43 // file descriptor provided to it. One easy way is to provide it with one end
44 // of a pipe and to check for EPIPE on the other end after the channel is destroyed.
45 Pipe pipe;
46
Josh Gao2ccbe3a2019-08-09 14:35:36 -070047 android::base::unique_fd sendFd(pipe.sendFd);
Jeff Brown5912f952013-07-01 19:10:31 -070048
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -070049 sp<InputChannel> inputChannel =
50 InputChannel::create("channel name", std::move(sendFd), new BBinder());
Josh Gao2ccbe3a2019-08-09 14:35:36 -070051
52 EXPECT_NE(inputChannel, nullptr) << "channel should be successfully created";
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -080053 EXPECT_STREQ("channel name", inputChannel->getName().c_str())
Jeff Brown5912f952013-07-01 19:10:31 -070054 << "channel should have provided name";
Josh Gao2ccbe3a2019-08-09 14:35:36 -070055 EXPECT_NE(-1, inputChannel->getFd()) << "channel should have valid fd";
Jeff Brown5912f952013-07-01 19:10:31 -070056
Josh Gao2ccbe3a2019-08-09 14:35:36 -070057 // InputChannel should be the owner of the file descriptor now
58 ASSERT_FALSE(sendFd.ok());
59}
Jeff Brown5912f952013-07-01 19:10:31 -070060
Josh Gao2ccbe3a2019-08-09 14:35:36 -070061TEST_F(InputChannelTest, SetAndGetToken) {
62 Pipe pipe;
Josh Gao2ccbe3a2019-08-09 14:35:36 -070063 sp<IBinder> token = new BBinder();
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -070064 sp<InputChannel> channel =
65 InputChannel::create("test channel", android::base::unique_fd(pipe.sendFd), token);
66
67 EXPECT_EQ(token, channel->getConnectionToken());
Jeff Brown5912f952013-07-01 19:10:31 -070068}
69
70TEST_F(InputChannelTest, OpenInputChannelPair_ReturnsAPairOfConnectedChannels) {
71 sp<InputChannel> serverChannel, clientChannel;
72
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -080073 status_t result = InputChannel::openInputChannelPair("channel name",
Jeff Brown5912f952013-07-01 19:10:31 -070074 serverChannel, clientChannel);
75
76 ASSERT_EQ(OK, result)
77 << "should have successfully opened a channel pair";
78
79 // Name
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -080080 EXPECT_STREQ("channel name (server)", serverChannel->getName().c_str())
Jeff Brown5912f952013-07-01 19:10:31 -070081 << "server channel should have suffixed name";
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -080082 EXPECT_STREQ("channel name (client)", clientChannel->getName().c_str())
Jeff Brown5912f952013-07-01 19:10:31 -070083 << "client channel should have suffixed name";
84
85 // Server->Client communication
86 InputMessage serverMsg;
87 memset(&serverMsg, 0, sizeof(InputMessage));
Siarhei Vishniakou52402772019-10-22 09:32:30 -070088 serverMsg.header.type = InputMessage::Type::KEY;
Jeff Brown5912f952013-07-01 19:10:31 -070089 serverMsg.body.key.action = AKEY_EVENT_ACTION_DOWN;
90 EXPECT_EQ(OK, serverChannel->sendMessage(&serverMsg))
91 << "server channel should be able to send message to client channel";
92
93 InputMessage clientMsg;
94 EXPECT_EQ(OK, clientChannel->receiveMessage(&clientMsg))
95 << "client channel should be able to receive message from server channel";
96 EXPECT_EQ(serverMsg.header.type, clientMsg.header.type)
97 << "client channel should receive the correct message from server channel";
98 EXPECT_EQ(serverMsg.body.key.action, clientMsg.body.key.action)
99 << "client channel should receive the correct message from server channel";
100
101 // Client->Server communication
102 InputMessage clientReply;
103 memset(&clientReply, 0, sizeof(InputMessage));
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700104 clientReply.header.type = InputMessage::Type::FINISHED;
Jeff Brown5912f952013-07-01 19:10:31 -0700105 clientReply.body.finished.seq = 0x11223344;
106 clientReply.body.finished.handled = true;
107 EXPECT_EQ(OK, clientChannel->sendMessage(&clientReply))
108 << "client channel should be able to send message to server channel";
109
110 InputMessage serverReply;
111 EXPECT_EQ(OK, serverChannel->receiveMessage(&serverReply))
112 << "server channel should be able to receive message from client channel";
113 EXPECT_EQ(clientReply.header.type, serverReply.header.type)
114 << "server channel should receive the correct message from client channel";
115 EXPECT_EQ(clientReply.body.finished.seq, serverReply.body.finished.seq)
116 << "server channel should receive the correct message from client channel";
117 EXPECT_EQ(clientReply.body.finished.handled, serverReply.body.finished.handled)
118 << "server channel should receive the correct message from client channel";
119}
120
121TEST_F(InputChannelTest, ReceiveSignal_WhenNoSignalPresent_ReturnsAnError) {
122 sp<InputChannel> serverChannel, clientChannel;
123
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800124 status_t result = InputChannel::openInputChannelPair("channel name",
Jeff Brown5912f952013-07-01 19:10:31 -0700125 serverChannel, clientChannel);
126
127 ASSERT_EQ(OK, result)
128 << "should have successfully opened a channel pair";
129
130 InputMessage msg;
131 EXPECT_EQ(WOULD_BLOCK, clientChannel->receiveMessage(&msg))
132 << "receiveMessage should have returned WOULD_BLOCK";
133}
134
135TEST_F(InputChannelTest, ReceiveSignal_WhenPeerClosed_ReturnsAnError) {
136 sp<InputChannel> serverChannel, clientChannel;
137
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800138 status_t result = InputChannel::openInputChannelPair("channel name",
Jeff Brown5912f952013-07-01 19:10:31 -0700139 serverChannel, clientChannel);
140
141 ASSERT_EQ(OK, result)
142 << "should have successfully opened a channel pair";
143
144 serverChannel.clear(); // close server channel
145
146 InputMessage msg;
147 EXPECT_EQ(DEAD_OBJECT, clientChannel->receiveMessage(&msg))
148 << "receiveMessage should have returned DEAD_OBJECT";
149}
150
151TEST_F(InputChannelTest, SendSignal_WhenPeerClosed_ReturnsAnError) {
152 sp<InputChannel> serverChannel, clientChannel;
153
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800154 status_t result = InputChannel::openInputChannelPair("channel name",
Jeff Brown5912f952013-07-01 19:10:31 -0700155 serverChannel, clientChannel);
156
157 ASSERT_EQ(OK, result)
158 << "should have successfully opened a channel pair";
159
160 serverChannel.clear(); // close server channel
161
162 InputMessage msg;
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700163 msg.header.type = InputMessage::Type::KEY;
Jeff Brown5912f952013-07-01 19:10:31 -0700164 EXPECT_EQ(DEAD_OBJECT, clientChannel->sendMessage(&msg))
165 << "sendMessage should have returned DEAD_OBJECT";
166}
167
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800168TEST_F(InputChannelTest, SendAndReceive_MotionClassification) {
169 sp<InputChannel> serverChannel, clientChannel;
170 status_t result = InputChannel::openInputChannelPair("channel name",
171 serverChannel, clientChannel);
172 ASSERT_EQ(OK, result)
173 << "should have successfully opened a channel pair";
174
175 std::array<MotionClassification, 3> classifications = {
176 MotionClassification::NONE,
177 MotionClassification::AMBIGUOUS_GESTURE,
178 MotionClassification::DEEP_PRESS,
179 };
180
181 InputMessage serverMsg = {}, clientMsg;
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700182 serverMsg.header.type = InputMessage::Type::MOTION;
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800183 serverMsg.body.motion.seq = 1;
184 serverMsg.body.motion.pointerCount = 1;
185
186 for (MotionClassification classification : classifications) {
187 // Send and receive a message with classification
188 serverMsg.body.motion.classification = classification;
189 EXPECT_EQ(OK, serverChannel->sendMessage(&serverMsg))
190 << "server channel should be able to send message to client channel";
191
192 EXPECT_EQ(OK, clientChannel->receiveMessage(&clientMsg))
193 << "client channel should be able to receive message from server channel";
194 EXPECT_EQ(serverMsg.header.type, clientMsg.header.type);
195 EXPECT_EQ(classification, clientMsg.body.motion.classification) <<
196 "Expected to receive " << motionClassificationToString(classification);
197 }
198}
199
Jeff Brown5912f952013-07-01 19:10:31 -0700200
201} // namespace android