InputPublisherAndConsumer_test.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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 <sys/mman.h>
  19. #include <time.h>
  20. #include <cutils/ashmem.h>
  21. #include <gtest/gtest.h>
  22. #include <input/InputTransport.h>
  23. #include <utils/Timers.h>
  24. #include <utils/StopWatch.h>
  25. namespace android {
  26. class InputPublisherAndConsumerTest : public testing::Test {
  27. protected:
  28. sp<InputChannel> serverChannel, clientChannel;
  29. InputPublisher* mPublisher;
  30. InputConsumer* mConsumer;
  31. PreallocatedInputEventFactory mEventFactory;
  32. virtual void SetUp() {
  33. status_t result = InputChannel::openInputChannelPair(String8("channel name"),
  34. serverChannel, clientChannel);
  35. mPublisher = new InputPublisher(serverChannel);
  36. mConsumer = new InputConsumer(clientChannel);
  37. }
  38. virtual void TearDown() {
  39. if (mPublisher) {
  40. delete mPublisher;
  41. mPublisher = NULL;
  42. }
  43. if (mConsumer) {
  44. delete mConsumer;
  45. mConsumer = NULL;
  46. }
  47. serverChannel.clear();
  48. clientChannel.clear();
  49. }
  50. void PublishAndConsumeKeyEvent();
  51. void PublishAndConsumeMotionEvent();
  52. };
  53. TEST_F(InputPublisherAndConsumerTest, GetChannel_ReturnsTheChannel) {
  54. EXPECT_EQ(serverChannel.get(), mPublisher->getChannel().get());
  55. EXPECT_EQ(clientChannel.get(), mConsumer->getChannel().get());
  56. }
  57. void InputPublisherAndConsumerTest::PublishAndConsumeKeyEvent() {
  58. status_t status;
  59. const uint32_t seq = 15;
  60. const int32_t deviceId = 1;
  61. const int32_t source = AINPUT_SOURCE_KEYBOARD;
  62. const int32_t action = AKEY_EVENT_ACTION_DOWN;
  63. const int32_t flags = AKEY_EVENT_FLAG_FROM_SYSTEM;
  64. const int32_t keyCode = AKEYCODE_ENTER;
  65. const int32_t scanCode = 13;
  66. const int32_t metaState = AMETA_ALT_LEFT_ON | AMETA_ALT_ON;
  67. const int32_t repeatCount = 1;
  68. const nsecs_t downTime = 3;
  69. const nsecs_t eventTime = 4;
  70. status = mPublisher->publishKeyEvent(seq, deviceId, source, action, flags,
  71. keyCode, scanCode, metaState, repeatCount, downTime, eventTime);
  72. ASSERT_EQ(OK, status)
  73. << "publisher publishKeyEvent should return OK";
  74. uint32_t consumeSeq;
  75. InputEvent* event;
  76. status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq, &event);
  77. ASSERT_EQ(OK, status)
  78. << "consumer consume should return OK";
  79. ASSERT_TRUE(event != NULL)
  80. << "consumer should have returned non-NULL event";
  81. ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, event->getType())
  82. << "consumer should have returned a key event";
  83. KeyEvent* keyEvent = static_cast<KeyEvent*>(event);
  84. EXPECT_EQ(seq, consumeSeq);
  85. EXPECT_EQ(deviceId, keyEvent->getDeviceId());
  86. EXPECT_EQ(source, keyEvent->getSource());
  87. EXPECT_EQ(action, keyEvent->getAction());
  88. EXPECT_EQ(flags, keyEvent->getFlags());
  89. EXPECT_EQ(keyCode, keyEvent->getKeyCode());
  90. EXPECT_EQ(scanCode, keyEvent->getScanCode());
  91. EXPECT_EQ(metaState, keyEvent->getMetaState());
  92. EXPECT_EQ(repeatCount, keyEvent->getRepeatCount());
  93. EXPECT_EQ(downTime, keyEvent->getDownTime());
  94. EXPECT_EQ(eventTime, keyEvent->getEventTime());
  95. status = mConsumer->sendFinishedSignal(seq, true);
  96. ASSERT_EQ(OK, status)
  97. << "consumer sendFinishedSignal should return OK";
  98. uint32_t finishedSeq = 0;
  99. bool handled = false;
  100. status = mPublisher->receiveFinishedSignal(&finishedSeq, &handled);
  101. ASSERT_EQ(OK, status)
  102. << "publisher receiveFinishedSignal should return OK";
  103. ASSERT_EQ(seq, finishedSeq)
  104. << "publisher receiveFinishedSignal should have returned the original sequence number";
  105. ASSERT_TRUE(handled)
  106. << "publisher receiveFinishedSignal should have set handled to consumer's reply";
  107. }
  108. void InputPublisherAndConsumerTest::PublishAndConsumeMotionEvent() {
  109. status_t status;
  110. const uint32_t seq = 15;
  111. const int32_t deviceId = 1;
  112. const int32_t source = AINPUT_SOURCE_TOUCHSCREEN;
  113. const int32_t action = AMOTION_EVENT_ACTION_MOVE;
  114. const int32_t actionButton = 0;
  115. const int32_t flags = AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED;
  116. const int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_TOP;
  117. const int32_t metaState = AMETA_ALT_LEFT_ON | AMETA_ALT_ON;
  118. const int32_t buttonState = AMOTION_EVENT_BUTTON_PRIMARY;
  119. const float xOffset = -10;
  120. const float yOffset = -20;
  121. const float xPrecision = 0.25;
  122. const float yPrecision = 0.5;
  123. const nsecs_t downTime = 3;
  124. const size_t pointerCount = 3;
  125. const nsecs_t eventTime = 4;
  126. PointerProperties pointerProperties[pointerCount];
  127. PointerCoords pointerCoords[pointerCount];
  128. for (size_t i = 0; i < pointerCount; i++) {
  129. pointerProperties[i].clear();
  130. pointerProperties[i].id = (i + 2) % pointerCount;
  131. pointerProperties[i].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
  132. pointerCoords[i].clear();
  133. pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, 100 * i);
  134. pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, 200 * i);
  135. pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 0.5 * i);
  136. pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_SIZE, 0.7 * i);
  137. pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, 1.5 * i);
  138. pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, 1.7 * i);
  139. pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, 2.5 * i);
  140. pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, 2.7 * i);
  141. pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, 3.5 * i);
  142. }
  143. status = mPublisher->publishMotionEvent(seq, deviceId, source, action, actionButton,
  144. flags, edgeFlags, metaState, buttonState, xOffset, yOffset, xPrecision, yPrecision,
  145. downTime, eventTime, pointerCount,
  146. pointerProperties, pointerCoords);
  147. ASSERT_EQ(OK, status)
  148. << "publisher publishMotionEvent should return OK";
  149. uint32_t consumeSeq;
  150. InputEvent* event;
  151. status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq, &event);
  152. ASSERT_EQ(OK, status)
  153. << "consumer consume should return OK";
  154. ASSERT_TRUE(event != NULL)
  155. << "consumer should have returned non-NULL event";
  156. ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType())
  157. << "consumer should have returned a motion event";
  158. MotionEvent* motionEvent = static_cast<MotionEvent*>(event);
  159. EXPECT_EQ(seq, consumeSeq);
  160. EXPECT_EQ(deviceId, motionEvent->getDeviceId());
  161. EXPECT_EQ(source, motionEvent->getSource());
  162. EXPECT_EQ(action, motionEvent->getAction());
  163. EXPECT_EQ(flags, motionEvent->getFlags());
  164. EXPECT_EQ(edgeFlags, motionEvent->getEdgeFlags());
  165. EXPECT_EQ(metaState, motionEvent->getMetaState());
  166. EXPECT_EQ(buttonState, motionEvent->getButtonState());
  167. EXPECT_EQ(xPrecision, motionEvent->getXPrecision());
  168. EXPECT_EQ(yPrecision, motionEvent->getYPrecision());
  169. EXPECT_EQ(downTime, motionEvent->getDownTime());
  170. EXPECT_EQ(eventTime, motionEvent->getEventTime());
  171. EXPECT_EQ(pointerCount, motionEvent->getPointerCount());
  172. EXPECT_EQ(0U, motionEvent->getHistorySize());
  173. for (size_t i = 0; i < pointerCount; i++) {
  174. SCOPED_TRACE(i);
  175. EXPECT_EQ(pointerProperties[i].id, motionEvent->getPointerId(i));
  176. EXPECT_EQ(pointerProperties[i].toolType, motionEvent->getToolType(i));
  177. EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X),
  178. motionEvent->getRawX(i));
  179. EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y),
  180. motionEvent->getRawY(i));
  181. EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X) + xOffset,
  182. motionEvent->getX(i));
  183. EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y) + yOffset,
  184. motionEvent->getY(i));
  185. EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE),
  186. motionEvent->getPressure(i));
  187. EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE),
  188. motionEvent->getSize(i));
  189. EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
  190. motionEvent->getTouchMajor(i));
  191. EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
  192. motionEvent->getTouchMinor(i));
  193. EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
  194. motionEvent->getToolMajor(i));
  195. EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
  196. motionEvent->getToolMinor(i));
  197. EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION),
  198. motionEvent->getOrientation(i));
  199. }
  200. status = mConsumer->sendFinishedSignal(seq, false);
  201. ASSERT_EQ(OK, status)
  202. << "consumer sendFinishedSignal should return OK";
  203. uint32_t finishedSeq = 0;
  204. bool handled = true;
  205. status = mPublisher->receiveFinishedSignal(&finishedSeq, &handled);
  206. ASSERT_EQ(OK, status)
  207. << "publisher receiveFinishedSignal should return OK";
  208. ASSERT_EQ(seq, finishedSeq)
  209. << "publisher receiveFinishedSignal should have returned the original sequence number";
  210. ASSERT_FALSE(handled)
  211. << "publisher receiveFinishedSignal should have set handled to consumer's reply";
  212. }
  213. TEST_F(InputPublisherAndConsumerTest, PublishKeyEvent_EndToEnd) {
  214. ASSERT_NO_FATAL_FAILURE(PublishAndConsumeKeyEvent());
  215. }
  216. TEST_F(InputPublisherAndConsumerTest, PublishMotionEvent_EndToEnd) {
  217. ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
  218. }
  219. TEST_F(InputPublisherAndConsumerTest, PublishMotionEvent_WhenPointerCountLessThan1_ReturnsError) {
  220. status_t status;
  221. const size_t pointerCount = 0;
  222. PointerProperties pointerProperties[pointerCount];
  223. PointerCoords pointerCoords[pointerCount];
  224. status = mPublisher->publishMotionEvent(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  225. pointerCount, pointerProperties, pointerCoords);
  226. ASSERT_EQ(BAD_VALUE, status)
  227. << "publisher publishMotionEvent should return BAD_VALUE";
  228. }
  229. TEST_F(InputPublisherAndConsumerTest, PublishMotionEvent_WhenPointerCountGreaterThanMax_ReturnsError) {
  230. status_t status;
  231. const size_t pointerCount = MAX_POINTERS + 1;
  232. PointerProperties pointerProperties[pointerCount];
  233. PointerCoords pointerCoords[pointerCount];
  234. for (size_t i = 0; i < pointerCount; i++) {
  235. pointerProperties[i].clear();
  236. pointerCoords[i].clear();
  237. }
  238. status = mPublisher->publishMotionEvent(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  239. pointerCount, pointerProperties, pointerCoords);
  240. ASSERT_EQ(BAD_VALUE, status)
  241. << "publisher publishMotionEvent should return BAD_VALUE";
  242. }
  243. TEST_F(InputPublisherAndConsumerTest, PublishMultipleEvents_EndToEnd) {
  244. ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
  245. ASSERT_NO_FATAL_FAILURE(PublishAndConsumeKeyEvent());
  246. ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
  247. ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
  248. ASSERT_NO_FATAL_FAILURE(PublishAndConsumeKeyEvent());
  249. }
  250. } // namespace android