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> |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 26 | #include <gtest/gtest.h> |
| 27 | #include <input/InputTransport.h> |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 28 | #include <utils/StopWatch.h> |
| 29 | #include <utils/StrongPointer.h> |
Josh Gao | 2ccbe3a | 2019-08-09 14:35:36 -0700 | [diff] [blame] | 30 | #include <utils/Timers.h> |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 31 | |
| 32 | namespace android { |
| 33 | |
| 34 | class InputChannelTest : public testing::Test { |
| 35 | protected: |
| 36 | virtual void SetUp() { } |
| 37 | virtual void TearDown() { } |
| 38 | }; |
| 39 | |
| 40 | |
| 41 | TEST_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 Gao | 2ccbe3a | 2019-08-09 14:35:36 -0700 | [diff] [blame] | 47 | android::base::unique_fd sendFd(pipe.sendFd); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 48 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame^] | 49 | sp<InputChannel> inputChannel = |
| 50 | InputChannel::create("channel name", std::move(sendFd), new BBinder()); |
Josh Gao | 2ccbe3a | 2019-08-09 14:35:36 -0700 | [diff] [blame] | 51 | |
| 52 | EXPECT_NE(inputChannel, nullptr) << "channel should be successfully created"; |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 53 | EXPECT_STREQ("channel name", inputChannel->getName().c_str()) |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 54 | << "channel should have provided name"; |
Josh Gao | 2ccbe3a | 2019-08-09 14:35:36 -0700 | [diff] [blame] | 55 | EXPECT_NE(-1, inputChannel->getFd()) << "channel should have valid fd"; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 56 | |
Josh Gao | 2ccbe3a | 2019-08-09 14:35:36 -0700 | [diff] [blame] | 57 | // InputChannel should be the owner of the file descriptor now |
| 58 | ASSERT_FALSE(sendFd.ok()); |
| 59 | } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 60 | |
Josh Gao | 2ccbe3a | 2019-08-09 14:35:36 -0700 | [diff] [blame] | 61 | TEST_F(InputChannelTest, SetAndGetToken) { |
| 62 | Pipe pipe; |
Josh Gao | 2ccbe3a | 2019-08-09 14:35:36 -0700 | [diff] [blame] | 63 | sp<IBinder> token = new BBinder(); |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame^] | 64 | sp<InputChannel> channel = |
| 65 | InputChannel::create("test channel", android::base::unique_fd(pipe.sendFd), token); |
| 66 | |
| 67 | EXPECT_EQ(token, channel->getConnectionToken()); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | TEST_F(InputChannelTest, OpenInputChannelPair_ReturnsAPairOfConnectedChannels) { |
| 71 | sp<InputChannel> serverChannel, clientChannel; |
| 72 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 73 | status_t result = InputChannel::openInputChannelPair("channel name", |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 74 | serverChannel, clientChannel); |
| 75 | |
| 76 | ASSERT_EQ(OK, result) |
| 77 | << "should have successfully opened a channel pair"; |
| 78 | |
| 79 | // Name |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 80 | EXPECT_STREQ("channel name (server)", serverChannel->getName().c_str()) |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 81 | << "server channel should have suffixed name"; |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 82 | EXPECT_STREQ("channel name (client)", clientChannel->getName().c_str()) |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 83 | << "client channel should have suffixed name"; |
| 84 | |
| 85 | // Server->Client communication |
| 86 | InputMessage serverMsg; |
| 87 | memset(&serverMsg, 0, sizeof(InputMessage)); |
Siarhei Vishniakou | 5240277 | 2019-10-22 09:32:30 -0700 | [diff] [blame] | 88 | serverMsg.header.type = InputMessage::Type::KEY; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 89 | 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 Vishniakou | 5240277 | 2019-10-22 09:32:30 -0700 | [diff] [blame] | 104 | clientReply.header.type = InputMessage::Type::FINISHED; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 105 | 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 | |
| 121 | TEST_F(InputChannelTest, ReceiveSignal_WhenNoSignalPresent_ReturnsAnError) { |
| 122 | sp<InputChannel> serverChannel, clientChannel; |
| 123 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 124 | status_t result = InputChannel::openInputChannelPair("channel name", |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 125 | 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 | |
| 135 | TEST_F(InputChannelTest, ReceiveSignal_WhenPeerClosed_ReturnsAnError) { |
| 136 | sp<InputChannel> serverChannel, clientChannel; |
| 137 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 138 | status_t result = InputChannel::openInputChannelPair("channel name", |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 139 | 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 | |
| 151 | TEST_F(InputChannelTest, SendSignal_WhenPeerClosed_ReturnsAnError) { |
| 152 | sp<InputChannel> serverChannel, clientChannel; |
| 153 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 154 | status_t result = InputChannel::openInputChannelPair("channel name", |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 155 | 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 Vishniakou | 5240277 | 2019-10-22 09:32:30 -0700 | [diff] [blame] | 163 | msg.header.type = InputMessage::Type::KEY; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 164 | EXPECT_EQ(DEAD_OBJECT, clientChannel->sendMessage(&msg)) |
| 165 | << "sendMessage should have returned DEAD_OBJECT"; |
| 166 | } |
| 167 | |
Siarhei Vishniakou | 16a2e30 | 2019-01-14 19:21:45 -0800 | [diff] [blame] | 168 | TEST_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 Vishniakou | 5240277 | 2019-10-22 09:32:30 -0700 | [diff] [blame] | 182 | serverMsg.header.type = InputMessage::Type::MOTION; |
Siarhei Vishniakou | 16a2e30 | 2019-01-14 19:21:45 -0800 | [diff] [blame] | 183 | 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 Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 200 | |
| 201 | } // namespace android |