blob: af74edd65ddaa9e35b6bdffc6c73f37161ba1ddd [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
Josh Gao2ccbe3a2019-08-09 14:35:36 -070049 sp<InputChannel> inputChannel = InputChannel::create("channel name", std::move(sendFd));
50
51 EXPECT_NE(inputChannel, nullptr) << "channel should be successfully created";
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -080052 EXPECT_STREQ("channel name", inputChannel->getName().c_str())
Jeff Brown5912f952013-07-01 19:10:31 -070053 << "channel should have provided name";
Josh Gao2ccbe3a2019-08-09 14:35:36 -070054 EXPECT_NE(-1, inputChannel->getFd()) << "channel should have valid fd";
Jeff Brown5912f952013-07-01 19:10:31 -070055
Josh Gao2ccbe3a2019-08-09 14:35:36 -070056 // InputChannel should be the owner of the file descriptor now
57 ASSERT_FALSE(sendFd.ok());
58}
Jeff Brown5912f952013-07-01 19:10:31 -070059
Josh Gao2ccbe3a2019-08-09 14:35:36 -070060TEST_F(InputChannelTest, SetAndGetToken) {
61 Pipe pipe;
62 sp<InputChannel> channel =
63 InputChannel::create("test channel", android::base::unique_fd(pipe.sendFd));
64 EXPECT_EQ(channel->getToken(), nullptr);
Jeff Brown5912f952013-07-01 19:10:31 -070065
Josh Gao2ccbe3a2019-08-09 14:35:36 -070066 sp<IBinder> token = new BBinder();
67 channel->setToken(token);
68 EXPECT_EQ(token, channel->getToken());
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));
89 serverMsg.header.type = InputMessage::TYPE_KEY;
90 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));
105 clientReply.header.type = InputMessage::TYPE_FINISHED;
106 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;
164 msg.header.type = InputMessage::TYPE_KEY;
165 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;
183 serverMsg.header.type = InputMessage::TYPE_MOTION;
184 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
Jeff Brown5912f952013-07-01 19:10:31 -0700201
202} // namespace android