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