remote_input_filter_unittest.cc 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "remoting/host/remote_input_filter.h"
  5. #include <stdint.h>
  6. #include "remoting/proto/event.pb.h"
  7. #include "remoting/protocol/input_event_tracker.h"
  8. #include "remoting/protocol/protocol_mock_objects.h"
  9. #include "remoting/protocol/test_event_matchers.h"
  10. #include "testing/gmock/include/gmock/gmock.h"
  11. #include "testing/gtest/include/gtest/gtest.h"
  12. #include "ui/events/event.h"
  13. using ::testing::_;
  14. using ::testing::ExpectationSet;
  15. using ::testing::InSequence;
  16. namespace remoting {
  17. using protocol::InputEventTracker;
  18. using protocol::MockInputStub;
  19. using protocol::test::EqualsKeyEvent;
  20. using protocol::test::EqualsTouchEventTypeAndId;
  21. namespace {
  22. static protocol::MouseEvent MouseMoveEvent(int x, int y) {
  23. protocol::MouseEvent event;
  24. event.set_x(x);
  25. event.set_y(y);
  26. return event;
  27. }
  28. static protocol::KeyEvent UsbKeyEvent(int usb_keycode, bool pressed) {
  29. protocol::KeyEvent event;
  30. event.set_usb_keycode(usb_keycode);
  31. event.set_pressed(pressed);
  32. return event;
  33. }
  34. protocol::TouchEvent TouchStartEvent(uint32_t id) {
  35. protocol::TouchEvent event;
  36. event.set_event_type(protocol::TouchEvent::TOUCH_POINT_START);
  37. protocol::TouchEventPoint* point = event.add_touch_points();
  38. point->set_id(id);
  39. point->set_x(0.0f);
  40. point->set_y(0.0f);
  41. point->set_radius_x(0.0f);
  42. point->set_radius_y(0.0f);
  43. point->set_angle(0.0f);
  44. point->set_pressure(0.0f);
  45. return event;
  46. }
  47. } // namespace
  48. // Verify that events get through if there is no local activity.
  49. TEST(RemoteInputFilterTest, NoLocalActivity) {
  50. MockInputStub mock_stub;
  51. InputEventTracker input_tracker(&mock_stub);
  52. RemoteInputFilter input_filter(&input_tracker);
  53. EXPECT_CALL(mock_stub, InjectMouseEvent(_)).Times(10);
  54. for (int i = 0; i < 10; ++i)
  55. input_filter.InjectMouseEvent(MouseMoveEvent(0, 0));
  56. }
  57. // Verify that events get through until there is local activity.
  58. TEST(RemoteInputFilterTest, MismatchedLocalActivity) {
  59. MockInputStub mock_stub;
  60. InputEventTracker input_tracker(&mock_stub);
  61. RemoteInputFilter input_filter(&input_tracker);
  62. EXPECT_CALL(mock_stub, InjectMouseEvent(_)).Times(5);
  63. for (int i = 0; i < 10; ++i) {
  64. input_filter.InjectMouseEvent(MouseMoveEvent(0, 0));
  65. if (i == 4)
  66. input_filter.LocalPointerMoved(webrtc::DesktopVector(1, 1),
  67. ui::ET_MOUSE_MOVED);
  68. }
  69. }
  70. // Verify that touch events are not considered as echoes.
  71. TEST(RemoteInputFilterTest, TouchEventsAreNotCheckedForEcho) {
  72. MockInputStub mock_stub;
  73. InputEventTracker input_tracker(&mock_stub);
  74. RemoteInputFilter input_filter(&input_tracker);
  75. EXPECT_CALL(mock_stub, InjectMouseEvent(_));
  76. input_filter.InjectMouseEvent(MouseMoveEvent(0, 0));
  77. input_filter.LocalPointerMoved(webrtc::DesktopVector(0, 0),
  78. ui::ET_TOUCH_MOVED);
  79. input_filter.InjectMouseEvent(MouseMoveEvent(1, 1));
  80. }
  81. // Verify that echos of injected mouse events don't block activity.
  82. TEST(RemoteInputFilterTest, LocalEchoesOfRemoteActivity) {
  83. MockInputStub mock_stub;
  84. InputEventTracker input_tracker(&mock_stub);
  85. RemoteInputFilter input_filter(&input_tracker);
  86. EXPECT_CALL(mock_stub, InjectMouseEvent(_)).Times(10);
  87. for (int i = 0; i < 10; ++i) {
  88. input_filter.InjectMouseEvent(MouseMoveEvent(0, 0));
  89. input_filter.LocalPointerMoved(webrtc::DesktopVector(0, 0),
  90. ui::ET_MOUSE_MOVED);
  91. }
  92. }
  93. // Verify that echos followed by a mismatch blocks activity.
  94. TEST(RemoteInputFilterTest, LocalEchosAndLocalActivity) {
  95. MockInputStub mock_stub;
  96. InputEventTracker input_tracker(&mock_stub);
  97. RemoteInputFilter input_filter(&input_tracker);
  98. EXPECT_CALL(mock_stub, InjectMouseEvent(_)).Times(5);
  99. for (int i = 0; i < 10; ++i) {
  100. input_filter.InjectMouseEvent(MouseMoveEvent(0, 0));
  101. input_filter.LocalPointerMoved(webrtc::DesktopVector(0, 0),
  102. ui::ET_MOUSE_MOVED);
  103. if (i == 4)
  104. input_filter.LocalPointerMoved(webrtc::DesktopVector(1, 1),
  105. ui::ET_MOUSE_MOVED);
  106. }
  107. }
  108. // Verify that local keyboard input blocks activity.
  109. TEST(RemoteInputFilterTest, LocalKeyPressEventBlocksInput) {
  110. MockInputStub mock_stub;
  111. InputEventTracker input_tracker(&mock_stub);
  112. RemoteInputFilter input_filter(&input_tracker);
  113. input_filter.LocalKeyPressed(0);
  114. input_filter.InjectKeyEvent(UsbKeyEvent(1, true));
  115. }
  116. // Verify that local echoes of remote keyboard activity does not block input
  117. TEST(RemoteInputFilterTest, LocalEchoOfKeyPressEventDoesNotBlockInput) {
  118. MockInputStub mock_stub;
  119. InputEventTracker input_tracker(&mock_stub);
  120. RemoteInputFilter input_filter(&input_tracker);
  121. EXPECT_CALL(mock_stub, InjectKeyEvent(_)).Times(4);
  122. input_filter.InjectKeyEvent(UsbKeyEvent(1, true));
  123. input_filter.InjectKeyEvent(UsbKeyEvent(1, false));
  124. input_filter.LocalKeyPressed(1);
  125. input_filter.InjectKeyEvent(UsbKeyEvent(2, true));
  126. input_filter.InjectKeyEvent(UsbKeyEvent(2, false));
  127. }
  128. // Verify that local input matching remote keyboard activity that has already
  129. // been discarded as an echo blocks input.
  130. TEST(RemoteInputFilterTest, LocalKeyPressEventMatchingPreviousEchoBlocksInput) {
  131. MockInputStub mock_stub;
  132. InputEventTracker input_tracker(&mock_stub);
  133. RemoteInputFilter input_filter(&input_tracker);
  134. EXPECT_CALL(mock_stub, InjectKeyEvent(_)).Times(2);
  135. input_filter.InjectKeyEvent(UsbKeyEvent(1, true));
  136. input_filter.InjectKeyEvent(UsbKeyEvent(1, false));
  137. input_filter.LocalKeyPressed(1);
  138. input_filter.LocalKeyPressed(1);
  139. input_filter.InjectKeyEvent(UsbKeyEvent(2, true));
  140. input_filter.InjectKeyEvent(UsbKeyEvent(2, false));
  141. }
  142. // Verify that local input matching remote keyboard activity blocks input if
  143. // local echo is not expected
  144. TEST(RemoteInputFilterTest,
  145. LocalDuplicateKeyPressEventBlocksInputIfEchoDisabled) {
  146. MockInputStub mock_stub;
  147. InputEventTracker input_tracker(&mock_stub);
  148. RemoteInputFilter input_filter(&input_tracker);
  149. input_filter.SetExpectLocalEcho(false);
  150. EXPECT_CALL(mock_stub, InjectKeyEvent(_)).Times(2);
  151. input_filter.InjectKeyEvent(UsbKeyEvent(1, true));
  152. input_filter.InjectKeyEvent(UsbKeyEvent(1, false));
  153. input_filter.LocalKeyPressed(1);
  154. input_filter.InjectKeyEvent(UsbKeyEvent(2, true));
  155. input_filter.InjectKeyEvent(UsbKeyEvent(2, false));
  156. }
  157. // Verify that local activity also causes buttons, keys, and touches to be
  158. // released.
  159. TEST(RemoteInputFilterTest, LocalActivityReleasesAll) {
  160. MockInputStub mock_stub;
  161. InputEventTracker input_tracker(&mock_stub);
  162. RemoteInputFilter input_filter(&input_tracker);
  163. EXPECT_CALL(mock_stub, InjectMouseEvent(_)).Times(5);
  164. // Use release of a key as a proxy for InputEventTracker::ReleaseAll()
  165. // having been called, rather than mocking it.
  166. EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsKeyEvent(0, true)));
  167. EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsKeyEvent(0, false)));
  168. input_filter.InjectKeyEvent(UsbKeyEvent(0, true));
  169. // Touch points that are down should be canceled.
  170. EXPECT_CALL(mock_stub, InjectTouchEvent(EqualsTouchEventTypeAndId(
  171. protocol::TouchEvent::TOUCH_POINT_START, 0u)));
  172. EXPECT_CALL(mock_stub, InjectTouchEvent(EqualsTouchEventTypeAndId(
  173. protocol::TouchEvent::TOUCH_POINT_CANCEL, 0u)));
  174. input_filter.InjectTouchEvent(TouchStartEvent(0u));
  175. for (int i = 0; i < 10; ++i) {
  176. input_filter.InjectMouseEvent(MouseMoveEvent(0, 0));
  177. input_filter.LocalPointerMoved(webrtc::DesktopVector(0, 0),
  178. ui::ET_MOUSE_MOVED);
  179. if (i == 4)
  180. input_filter.LocalPointerMoved(webrtc::DesktopVector(1, 1),
  181. ui::ET_MOUSE_MOVED);
  182. }
  183. }
  184. } // namespace remoting