Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1 | /* |
| 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 Vishniakou | 16a2e30 | 2019-01-14 19:21:45 -0800 | [diff] [blame] | 17 | #include <array> |
| 18 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 19 | #include "TestHelpers.h" |
| 20 | |
| 21 | #include <unistd.h> |
| 22 | #include <time.h> |
| 23 | #include <errno.h> |
| 24 | |
Josh Gao | 2ccbe3a | 2019-08-09 14:35:36 -0700 | [diff] [blame] | 25 | #include <binder/Binder.h> |
Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 26 | #include <binder/Parcel.h> |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 27 | #include <gtest/gtest.h> |
| 28 | #include <input/InputTransport.h> |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 29 | #include <utils/StopWatch.h> |
| 30 | #include <utils/StrongPointer.h> |
Josh Gao | 2ccbe3a | 2019-08-09 14:35:36 -0700 | [diff] [blame] | 31 | #include <utils/Timers.h> |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 32 | |
| 33 | namespace android { |
| 34 | |
| 35 | class InputChannelTest : public testing::Test { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 36 | }; |
| 37 | |
| 38 | |
| 39 | TEST_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 Gao | 2ccbe3a | 2019-08-09 14:35:36 -0700 | [diff] [blame] | 45 | android::base::unique_fd sendFd(pipe.sendFd); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 46 | |
Siarhei Vishniakou | d258827 | 2020-07-10 11:15:40 -0500 | [diff] [blame] | 47 | std::unique_ptr<InputChannel> inputChannel = |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 48 | InputChannel::create("channel name", std::move(sendFd), new BBinder()); |
Josh Gao | 2ccbe3a | 2019-08-09 14:35:36 -0700 | [diff] [blame] | 49 | |
| 50 | EXPECT_NE(inputChannel, nullptr) << "channel should be successfully created"; |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 51 | EXPECT_STREQ("channel name", inputChannel->getName().c_str()) |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 52 | << "channel should have provided name"; |
Josh Gao | 2ccbe3a | 2019-08-09 14:35:36 -0700 | [diff] [blame] | 53 | EXPECT_NE(-1, inputChannel->getFd()) << "channel should have valid fd"; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 54 | |
Josh Gao | 2ccbe3a | 2019-08-09 14:35:36 -0700 | [diff] [blame] | 55 | // InputChannel should be the owner of the file descriptor now |
| 56 | ASSERT_FALSE(sendFd.ok()); |
| 57 | } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 58 | |
Josh Gao | 2ccbe3a | 2019-08-09 14:35:36 -0700 | [diff] [blame] | 59 | TEST_F(InputChannelTest, SetAndGetToken) { |
| 60 | Pipe pipe; |
Josh Gao | 2ccbe3a | 2019-08-09 14:35:36 -0700 | [diff] [blame] | 61 | sp<IBinder> token = new BBinder(); |
Siarhei Vishniakou | d258827 | 2020-07-10 11:15:40 -0500 | [diff] [blame] | 62 | std::unique_ptr<InputChannel> channel = |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 63 | InputChannel::create("test channel", android::base::unique_fd(pipe.sendFd), token); |
| 64 | |
| 65 | EXPECT_EQ(token, channel->getConnectionToken()); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | TEST_F(InputChannelTest, OpenInputChannelPair_ReturnsAPairOfConnectedChannels) { |
Siarhei Vishniakou | d258827 | 2020-07-10 11:15:40 -0500 | [diff] [blame] | 69 | std::unique_ptr<InputChannel> serverChannel, clientChannel; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 70 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 71 | status_t result = InputChannel::openInputChannelPair("channel name", |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 72 | serverChannel, clientChannel); |
| 73 | |
| 74 | ASSERT_EQ(OK, result) |
| 75 | << "should have successfully opened a channel pair"; |
| 76 | |
| 77 | // Name |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 78 | EXPECT_STREQ("channel name (server)", serverChannel->getName().c_str()) |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 79 | << "server channel should have suffixed name"; |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 80 | EXPECT_STREQ("channel name (client)", clientChannel->getName().c_str()) |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 81 | << "client channel should have suffixed name"; |
| 82 | |
| 83 | // Server->Client communication |
| 84 | InputMessage serverMsg; |
| 85 | memset(&serverMsg, 0, sizeof(InputMessage)); |
Siarhei Vishniakou | 5240277 | 2019-10-22 09:32:30 -0700 | [diff] [blame] | 86 | serverMsg.header.type = InputMessage::Type::KEY; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 87 | 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 Vishniakou | 5240277 | 2019-10-22 09:32:30 -0700 | [diff] [blame] | 102 | clientReply.header.type = InputMessage::Type::FINISHED; |
Siarhei Vishniakou | a64c159 | 2020-06-22 12:02:29 -0500 | [diff] [blame] | 103 | clientReply.header.seq = 0x11223344; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 104 | 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 Vishniakou | a64c159 | 2020-06-22 12:02:29 -0500 | [diff] [blame] | 113 | EXPECT_EQ(clientReply.header.seq, serverReply.header.seq) |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 114 | << "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 | |
| 119 | TEST_F(InputChannelTest, ReceiveSignal_WhenNoSignalPresent_ReturnsAnError) { |
Siarhei Vishniakou | d258827 | 2020-07-10 11:15:40 -0500 | [diff] [blame] | 120 | std::unique_ptr<InputChannel> serverChannel, clientChannel; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 121 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 122 | status_t result = InputChannel::openInputChannelPair("channel name", |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 123 | 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 | |
| 133 | TEST_F(InputChannelTest, ReceiveSignal_WhenPeerClosed_ReturnsAnError) { |
Siarhei Vishniakou | d258827 | 2020-07-10 11:15:40 -0500 | [diff] [blame] | 134 | std::unique_ptr<InputChannel> serverChannel, clientChannel; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 135 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 136 | status_t result = InputChannel::openInputChannelPair("channel name", |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 137 | serverChannel, clientChannel); |
| 138 | |
| 139 | ASSERT_EQ(OK, result) |
| 140 | << "should have successfully opened a channel pair"; |
| 141 | |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 142 | serverChannel.reset(); // close server channel |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 143 | |
| 144 | InputMessage msg; |
| 145 | EXPECT_EQ(DEAD_OBJECT, clientChannel->receiveMessage(&msg)) |
| 146 | << "receiveMessage should have returned DEAD_OBJECT"; |
| 147 | } |
| 148 | |
| 149 | TEST_F(InputChannelTest, SendSignal_WhenPeerClosed_ReturnsAnError) { |
Siarhei Vishniakou | d258827 | 2020-07-10 11:15:40 -0500 | [diff] [blame] | 150 | std::unique_ptr<InputChannel> serverChannel, clientChannel; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 151 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 152 | status_t result = InputChannel::openInputChannelPair("channel name", |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 153 | serverChannel, clientChannel); |
| 154 | |
| 155 | ASSERT_EQ(OK, result) |
| 156 | << "should have successfully opened a channel pair"; |
| 157 | |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 158 | serverChannel.reset(); // close server channel |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 159 | |
| 160 | InputMessage msg; |
Siarhei Vishniakou | 5240277 | 2019-10-22 09:32:30 -0700 | [diff] [blame] | 161 | msg.header.type = InputMessage::Type::KEY; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 162 | EXPECT_EQ(DEAD_OBJECT, clientChannel->sendMessage(&msg)) |
| 163 | << "sendMessage should have returned DEAD_OBJECT"; |
| 164 | } |
| 165 | |
Siarhei Vishniakou | 16a2e30 | 2019-01-14 19:21:45 -0800 | [diff] [blame] | 166 | TEST_F(InputChannelTest, SendAndReceive_MotionClassification) { |
Siarhei Vishniakou | d258827 | 2020-07-10 11:15:40 -0500 | [diff] [blame] | 167 | std::unique_ptr<InputChannel> serverChannel, clientChannel; |
Siarhei Vishniakou | 16a2e30 | 2019-01-14 19:21:45 -0800 | [diff] [blame] | 168 | 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 Vishniakou | 5240277 | 2019-10-22 09:32:30 -0700 | [diff] [blame] | 180 | serverMsg.header.type = InputMessage::Type::MOTION; |
Siarhei Vishniakou | a64c159 | 2020-06-22 12:02:29 -0500 | [diff] [blame] | 181 | serverMsg.header.seq = 1; |
Siarhei Vishniakou | 16a2e30 | 2019-01-14 19:21:45 -0800 | [diff] [blame] | 182 | 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 Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 198 | TEST_F(InputChannelTest, InputChannelParcelAndUnparcel) { |
Siarhei Vishniakou | d258827 | 2020-07-10 11:15:40 -0500 | [diff] [blame] | 199 | std::unique_ptr<InputChannel> serverChannel, clientChannel; |
Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 200 | |
| 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 | |
| 217 | TEST_F(InputChannelTest, DuplicateChannelAndAssertEqual) { |
Siarhei Vishniakou | d258827 | 2020-07-10 11:15:40 -0500 | [diff] [blame] | 218 | std::unique_ptr<InputChannel> serverChannel, clientChannel; |
Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 219 | |
| 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 Vishniakou | d258827 | 2020-07-10 11:15:40 -0500 | [diff] [blame] | 225 | std::unique_ptr<InputChannel> dupChan = serverChannel->dup(); |
Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 226 | |
| 227 | EXPECT_EQ(*serverChannel == *dupChan, true) << "inputchannel should be equal after duplication"; |
| 228 | } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 229 | |
| 230 | } // namespace android |