blob: ee422fe7d8c29e4fece33b4c50997bce68308029 [file] [log] [blame]
Jeff Brownb2d44352011-02-17 13:01:34 -08001/*
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 Brownf4a4ec22010-06-16 01:53:36 -070016
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 Brown6cdee982012-02-03 20:11:27 -080023#include <errno.h>
Jeff Brownf4a4ec22010-06-16 01:53:36 -070024
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
Jeff Brown6cdee982012-02-03 20:11:27 -080038 // file descriptor provided to it. One easy way is to provide it with one end
Jeff Brownf4a4ec22010-06-16 01:53:36 -070039 // of a pipe and to check for EPIPE on the other end after the channel is destroyed.
Jeff Brown6cdee982012-02-03 20:11:27 -080040 Pipe pipe;
Jeff Brownf4a4ec22010-06-16 01:53:36 -070041
Jeff Brown6cdee982012-02-03 20:11:27 -080042 sp<InputChannel> inputChannel = new InputChannel(String8("channel name"), pipe.sendFd);
Jeff Brownf4a4ec22010-06-16 01:53:36 -070043
44 EXPECT_STREQ("channel name", inputChannel->getName().string())
45 << "channel should have provided name";
Jeff Brown6cdee982012-02-03 20:11:27 -080046 EXPECT_EQ(pipe.sendFd, inputChannel->getFd())
47 << "channel should have provided fd";
Jeff Brownf4a4ec22010-06-16 01:53:36 -070048
49 inputChannel.clear(); // destroys input channel
50
Jeff Brown6cdee982012-02-03 20:11:27 -080051 EXPECT_EQ(-EPIPE, pipe.readSignal())
52 << "channel should have closed fd when destroyed";
Jeff Brownf4a4ec22010-06-16 01:53:36 -070053
54 // clean up fds of Pipe endpoints that were closed so we don't try to close them again
Jeff Brown6cdee982012-02-03 20:11:27 -080055 pipe.sendFd = -1;
Jeff Brownf4a4ec22010-06-16 01:53:36 -070056}
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
Jeff Brownf4a4ec22010-06-16 01:53:36 -070073 // Server->Client communication
Jeff Brown6cdee982012-02-03 20:11:27 -080074 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 Brownf4a4ec22010-06-16 01:53:36 -070088
89 // Client->Server communication
Jeff Brown6cdee982012-02-03 20:11:27 -080090 InputMessage clientReply;
91 memset(&clientReply, 0, sizeof(InputMessage));
92 clientReply.header.type = InputMessage::TYPE_FINISHED;
Jeff Brownf0490c92012-02-07 14:46:57 -080093 clientReply.body.finished.seq = 0x11223344;
Jeff Brown6cdee982012-02-03 20:11:27 -080094 clientReply.body.finished.handled = true;
95 EXPECT_EQ(OK, clientChannel->sendMessage(&clientReply))
96 << "client channel should be able to send message to server channel";
97
98 InputMessage serverReply;
99 EXPECT_EQ(OK, serverChannel->receiveMessage(&serverReply))
100 << "server channel should be able to receive message from client channel";
101 EXPECT_EQ(clientReply.header.type, serverReply.header.type)
102 << "server channel should receive the correct message from client channel";
Jeff Brownf0490c92012-02-07 14:46:57 -0800103 EXPECT_EQ(clientReply.body.finished.seq, serverReply.body.finished.seq)
104 << "server channel should receive the correct message from client channel";
Jeff Brown6cdee982012-02-03 20:11:27 -0800105 EXPECT_EQ(clientReply.body.finished.handled, serverReply.body.finished.handled)
106 << "server channel should receive the correct message from client channel";
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700107}
108
109TEST_F(InputChannelTest, ReceiveSignal_WhenNoSignalPresent_ReturnsAnError) {
110 sp<InputChannel> serverChannel, clientChannel;
111
112 status_t result = InputChannel::openInputChannelPair(String8("channel name"),
113 serverChannel, clientChannel);
114
115 ASSERT_EQ(OK, result)
116 << "should have successfully opened a channel pair";
117
Jeff Brown6cdee982012-02-03 20:11:27 -0800118 InputMessage msg;
119 EXPECT_EQ(WOULD_BLOCK, clientChannel->receiveMessage(&msg))
120 << "receiveMessage should have returned WOULD_BLOCK";
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700121}
122
123TEST_F(InputChannelTest, ReceiveSignal_WhenPeerClosed_ReturnsAnError) {
124 sp<InputChannel> serverChannel, clientChannel;
125
126 status_t result = InputChannel::openInputChannelPair(String8("channel name"),
127 serverChannel, clientChannel);
128
129 ASSERT_EQ(OK, result)
130 << "should have successfully opened a channel pair";
131
132 serverChannel.clear(); // close server channel
133
Jeff Brown6cdee982012-02-03 20:11:27 -0800134 InputMessage msg;
135 EXPECT_EQ(DEAD_OBJECT, clientChannel->receiveMessage(&msg))
136 << "receiveMessage should have returned DEAD_OBJECT";
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700137}
138
139TEST_F(InputChannelTest, SendSignal_WhenPeerClosed_ReturnsAnError) {
140 sp<InputChannel> serverChannel, clientChannel;
141
142 status_t result = InputChannel::openInputChannelPair(String8("channel name"),
143 serverChannel, clientChannel);
144
145 ASSERT_EQ(OK, result)
146 << "should have successfully opened a channel pair";
147
148 serverChannel.clear(); // close server channel
149
Jeff Brown6cdee982012-02-03 20:11:27 -0800150 InputMessage msg;
151 msg.header.type = InputMessage::TYPE_KEY;
152 EXPECT_EQ(DEAD_OBJECT, clientChannel->sendMessage(&msg))
153 << "sendMessage should have returned DEAD_OBJECT";
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700154}
155
156
157} // namespace android