Jeff Brown | ebbd5d1 | 2011-02-17 13:01:34 -0800 | [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 | */ |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 16 | |
Mathias Agopian | b93a03f8 | 2012-02-17 15:34:57 -0800 | [diff] [blame] | 17 | #include <androidfw/InputTransport.h> |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 18 | #include <utils/Timers.h> |
| 19 | #include <utils/StopWatch.h> |
Mathias Agopian | b93a03f8 | 2012-02-17 15:34:57 -0800 | [diff] [blame] | 20 | #include <utils/StrongPointer.h> |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 21 | #include <gtest/gtest.h> |
| 22 | #include <unistd.h> |
| 23 | #include <time.h> |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 24 | #include <errno.h> |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 25 | |
Mathias Agopian | 08965ec | 2012-03-05 16:16:58 -0800 | [diff] [blame] | 26 | #include "TestHelpers.h" |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 27 | |
| 28 | namespace android { |
| 29 | |
| 30 | class InputChannelTest : public testing::Test { |
| 31 | protected: |
| 32 | virtual void SetUp() { } |
| 33 | virtual void TearDown() { } |
| 34 | }; |
| 35 | |
| 36 | |
| 37 | TEST_F(InputChannelTest, ConstructorAndDestructor_TakesOwnershipOfFileDescriptors) { |
| 38 | // Our purpose here is to verify that the input channel destructor closes the |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 39 | // file descriptor provided to it. One easy way is to provide it with one end |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 40 | // of a pipe and to check for EPIPE on the other end after the channel is destroyed. |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 41 | Pipe pipe; |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 42 | |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 43 | sp<InputChannel> inputChannel = new InputChannel(String8("channel name"), pipe.sendFd); |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 44 | |
| 45 | EXPECT_STREQ("channel name", inputChannel->getName().string()) |
| 46 | << "channel should have provided name"; |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 47 | EXPECT_EQ(pipe.sendFd, inputChannel->getFd()) |
| 48 | << "channel should have provided fd"; |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 49 | |
| 50 | inputChannel.clear(); // destroys input channel |
| 51 | |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 52 | EXPECT_EQ(-EPIPE, pipe.readSignal()) |
| 53 | << "channel should have closed fd when destroyed"; |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 54 | |
| 55 | // clean up fds of Pipe endpoints that were closed so we don't try to close them again |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 56 | pipe.sendFd = -1; |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | TEST_F(InputChannelTest, OpenInputChannelPair_ReturnsAPairOfConnectedChannels) { |
| 60 | sp<InputChannel> serverChannel, clientChannel; |
| 61 | |
| 62 | status_t result = InputChannel::openInputChannelPair(String8("channel name"), |
| 63 | serverChannel, clientChannel); |
| 64 | |
| 65 | ASSERT_EQ(OK, result) |
| 66 | << "should have successfully opened a channel pair"; |
| 67 | |
| 68 | // Name |
| 69 | EXPECT_STREQ("channel name (server)", serverChannel->getName().string()) |
| 70 | << "server channel should have suffixed name"; |
| 71 | EXPECT_STREQ("channel name (client)", clientChannel->getName().string()) |
| 72 | << "client channel should have suffixed name"; |
| 73 | |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 74 | // Server->Client communication |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 75 | InputMessage serverMsg; |
| 76 | memset(&serverMsg, 0, sizeof(InputMessage)); |
| 77 | serverMsg.header.type = InputMessage::TYPE_KEY; |
| 78 | serverMsg.body.key.action = AKEY_EVENT_ACTION_DOWN; |
| 79 | EXPECT_EQ(OK, serverChannel->sendMessage(&serverMsg)) |
| 80 | << "server channel should be able to send message to client channel"; |
| 81 | |
| 82 | InputMessage clientMsg; |
| 83 | EXPECT_EQ(OK, clientChannel->receiveMessage(&clientMsg)) |
| 84 | << "client channel should be able to receive message from server channel"; |
| 85 | EXPECT_EQ(serverMsg.header.type, clientMsg.header.type) |
| 86 | << "client channel should receive the correct message from server channel"; |
| 87 | EXPECT_EQ(serverMsg.body.key.action, clientMsg.body.key.action) |
| 88 | << "client channel should receive the correct message from server channel"; |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 89 | |
| 90 | // Client->Server communication |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 91 | InputMessage clientReply; |
| 92 | memset(&clientReply, 0, sizeof(InputMessage)); |
| 93 | clientReply.header.type = InputMessage::TYPE_FINISHED; |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 94 | clientReply.body.finished.seq = 0x11223344; |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 95 | clientReply.body.finished.handled = true; |
| 96 | EXPECT_EQ(OK, clientChannel->sendMessage(&clientReply)) |
| 97 | << "client channel should be able to send message to server channel"; |
| 98 | |
| 99 | InputMessage serverReply; |
| 100 | EXPECT_EQ(OK, serverChannel->receiveMessage(&serverReply)) |
| 101 | << "server channel should be able to receive message from client channel"; |
| 102 | EXPECT_EQ(clientReply.header.type, serverReply.header.type) |
| 103 | << "server channel should receive the correct message from client channel"; |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 104 | EXPECT_EQ(clientReply.body.finished.seq, serverReply.body.finished.seq) |
| 105 | << "server channel should receive the correct message from client channel"; |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 106 | EXPECT_EQ(clientReply.body.finished.handled, serverReply.body.finished.handled) |
| 107 | << "server channel should receive the correct message from client channel"; |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | TEST_F(InputChannelTest, ReceiveSignal_WhenNoSignalPresent_ReturnsAnError) { |
| 111 | sp<InputChannel> serverChannel, clientChannel; |
| 112 | |
| 113 | status_t result = InputChannel::openInputChannelPair(String8("channel name"), |
| 114 | serverChannel, clientChannel); |
| 115 | |
| 116 | ASSERT_EQ(OK, result) |
| 117 | << "should have successfully opened a channel pair"; |
| 118 | |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 119 | InputMessage msg; |
| 120 | EXPECT_EQ(WOULD_BLOCK, clientChannel->receiveMessage(&msg)) |
| 121 | << "receiveMessage should have returned WOULD_BLOCK"; |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | TEST_F(InputChannelTest, ReceiveSignal_WhenPeerClosed_ReturnsAnError) { |
| 125 | sp<InputChannel> serverChannel, clientChannel; |
| 126 | |
| 127 | status_t result = InputChannel::openInputChannelPair(String8("channel name"), |
| 128 | serverChannel, clientChannel); |
| 129 | |
| 130 | ASSERT_EQ(OK, result) |
| 131 | << "should have successfully opened a channel pair"; |
| 132 | |
| 133 | serverChannel.clear(); // close server channel |
| 134 | |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 135 | InputMessage msg; |
| 136 | EXPECT_EQ(DEAD_OBJECT, clientChannel->receiveMessage(&msg)) |
| 137 | << "receiveMessage should have returned DEAD_OBJECT"; |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | TEST_F(InputChannelTest, SendSignal_WhenPeerClosed_ReturnsAnError) { |
| 141 | sp<InputChannel> serverChannel, clientChannel; |
| 142 | |
| 143 | status_t result = InputChannel::openInputChannelPair(String8("channel name"), |
| 144 | serverChannel, clientChannel); |
| 145 | |
| 146 | ASSERT_EQ(OK, result) |
| 147 | << "should have successfully opened a channel pair"; |
| 148 | |
| 149 | serverChannel.clear(); // close server channel |
| 150 | |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 151 | InputMessage msg; |
| 152 | msg.header.type = InputMessage::TYPE_KEY; |
| 153 | EXPECT_EQ(DEAD_OBJECT, clientChannel->sendMessage(&msg)) |
| 154 | << "sendMessage should have returned DEAD_OBJECT"; |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | |
| 158 | } // namespace android |