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 | |
Josh Gao | 2ccbe3a | 2019-08-09 14:35:36 -0700 | [diff] [blame] | 49 | sp<InputChannel> inputChannel = InputChannel::create("channel name", std::move(sendFd)); |
| 50 | |
| 51 | EXPECT_NE(inputChannel, nullptr) << "channel should be successfully created"; |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 52 | EXPECT_STREQ("channel name", inputChannel->getName().c_str()) |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 53 | << "channel should have provided name"; |
Josh Gao | 2ccbe3a | 2019-08-09 14:35:36 -0700 | [diff] [blame] | 54 | EXPECT_NE(-1, inputChannel->getFd()) << "channel should have valid fd"; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 55 | |
Josh Gao | 2ccbe3a | 2019-08-09 14:35:36 -0700 | [diff] [blame] | 56 | // InputChannel should be the owner of the file descriptor now |
| 57 | ASSERT_FALSE(sendFd.ok()); |
| 58 | } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 59 | |
Josh Gao | 2ccbe3a | 2019-08-09 14:35:36 -0700 | [diff] [blame] | 60 | TEST_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 Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 65 | |
Josh Gao | 2ccbe3a | 2019-08-09 14:35:36 -0700 | [diff] [blame] | 66 | sp<IBinder> token = new BBinder(); |
| 67 | channel->setToken(token); |
| 68 | EXPECT_EQ(token, channel->getToken()); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | TEST_F(InputChannelTest, OpenInputChannelPair_ReturnsAPairOfConnectedChannels) { |
| 72 | sp<InputChannel> serverChannel, clientChannel; |
| 73 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 74 | status_t result = InputChannel::openInputChannelPair("channel name", |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 75 | serverChannel, clientChannel); |
| 76 | |
| 77 | ASSERT_EQ(OK, result) |
| 78 | << "should have successfully opened a channel pair"; |
| 79 | |
| 80 | // Name |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 81 | EXPECT_STREQ("channel name (server)", serverChannel->getName().c_str()) |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 82 | << "server channel should have suffixed name"; |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 83 | EXPECT_STREQ("channel name (client)", clientChannel->getName().c_str()) |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 84 | << "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 | |
| 122 | TEST_F(InputChannelTest, ReceiveSignal_WhenNoSignalPresent_ReturnsAnError) { |
| 123 | sp<InputChannel> serverChannel, clientChannel; |
| 124 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 125 | status_t result = InputChannel::openInputChannelPair("channel name", |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 126 | 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 | |
| 136 | TEST_F(InputChannelTest, ReceiveSignal_WhenPeerClosed_ReturnsAnError) { |
| 137 | sp<InputChannel> serverChannel, clientChannel; |
| 138 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 139 | status_t result = InputChannel::openInputChannelPair("channel name", |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 140 | 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 | |
| 152 | TEST_F(InputChannelTest, SendSignal_WhenPeerClosed_ReturnsAnError) { |
| 153 | sp<InputChannel> serverChannel, clientChannel; |
| 154 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 155 | status_t result = InputChannel::openInputChannelPair("channel name", |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 156 | 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 Vishniakou | 16a2e30 | 2019-01-14 19:21:45 -0800 | [diff] [blame] | 169 | TEST_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 Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 201 | |
| 202 | } // namespace android |