InputChannel_test.cpp 6.0 KB

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