InputChannel_test.cpp revision cbee6d6ede0499fb4a2c00bfc00d5db8d9ed5139
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 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> 23#include <errno.h> 24 25#include "../../utils/tests/TestHelpers.h" 26 27namespace android { 28 29class InputChannelTest : public testing::Test { 30protected: 31 virtual void SetUp() { } 32 virtual void TearDown() { } 33}; 34 35 36TEST_F(InputChannelTest, ConstructorAndDestructor_TakesOwnershipOfFileDescriptors) { 37 // Our purpose here is to verify that the input channel destructor closes the 38 // file descriptor provided to it. One easy way is to provide it with one end 39 // of a pipe and to check for EPIPE on the other end after the channel is destroyed. 40 Pipe pipe; 41 42 sp<InputChannel> inputChannel = new InputChannel(String8("channel name"), pipe.sendFd); 43 44 EXPECT_STREQ("channel name", inputChannel->getName().string()) 45 << "channel should have provided name"; 46 EXPECT_EQ(pipe.sendFd, inputChannel->getFd()) 47 << "channel should have provided fd"; 48 49 inputChannel.clear(); // destroys input channel 50 51 EXPECT_EQ(-EPIPE, pipe.readSignal()) 52 << "channel should have closed fd when destroyed"; 53 54 // clean up fds of Pipe endpoints that were closed so we don't try to close them again 55 pipe.sendFd = -1; 56} 57 58TEST_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 73 // Server->Client communication 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"; 88 89 // Client->Server communication 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"; 104} 105 106TEST_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 115 InputMessage msg; 116 EXPECT_EQ(WOULD_BLOCK, clientChannel->receiveMessage(&msg)) 117 << "receiveMessage should have returned WOULD_BLOCK"; 118} 119 120TEST_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 131 InputMessage msg; 132 EXPECT_EQ(DEAD_OBJECT, clientChannel->receiveMessage(&msg)) 133 << "receiveMessage should have returned DEAD_OBJECT"; 134} 135 136TEST_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 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"; 151} 152 153 154} // namespace android 155