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