Home | History | Annotate | Download | only in host
      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 
      5 #include "remoting/host/remote_input_filter.h"
      6 
      7 #include "remoting/proto/event.pb.h"
      8 #include "remoting/protocol/input_event_tracker.h"
      9 #include "remoting/protocol/protocol_mock_objects.h"
     10 #include "testing/gmock/include/gmock/gmock.h"
     11 #include "testing/gtest/include/gtest/gtest.h"
     12 
     13 using ::testing::_;
     14 using ::testing::ExpectationSet;
     15 using ::testing::InSequence;
     16 
     17 namespace remoting {
     18 
     19 using protocol::MockInputStub;
     20 using protocol::InputEventTracker;
     21 
     22 namespace {
     23 
     24 MATCHER_P2(EqualsUsbEvent, usb_keycode, pressed, "") {
     25   return arg.usb_keycode() == (unsigned int)usb_keycode &&
     26          arg.pressed() == pressed;
     27 }
     28 
     29 static protocol::MouseEvent MouseMoveEvent(int x, int y) {
     30   protocol::MouseEvent event;
     31   event.set_x(x);
     32   event.set_y(y);
     33   return event;
     34 }
     35 
     36 static protocol::KeyEvent UsbKeyEvent(int usb_keycode, bool pressed) {
     37   protocol::KeyEvent event;
     38   event.set_usb_keycode(usb_keycode);
     39   event.set_pressed(pressed);
     40   return event;
     41 }
     42 
     43 }
     44 
     45 // Verify that events get through if there is no local activity.
     46 TEST(RemoteInputFilterTest, NoLocalActivity) {
     47   MockInputStub mock_stub;
     48   InputEventTracker input_tracker(&mock_stub);
     49   RemoteInputFilter input_filter(&input_tracker);
     50 
     51   EXPECT_CALL(mock_stub, InjectMouseEvent(_))
     52         .Times(10);
     53 
     54   for (int i = 0; i < 10; ++i)
     55     input_filter.InjectMouseEvent(MouseMoveEvent(0, 0));
     56 }
     57 
     58 // Verify that events get through until there is local activity.
     59 TEST(RemoteInputFilterTest, MismatchedLocalActivity) {
     60   MockInputStub mock_stub;
     61   InputEventTracker input_tracker(&mock_stub);
     62   RemoteInputFilter input_filter(&input_tracker);
     63 
     64   EXPECT_CALL(mock_stub, InjectMouseEvent(_))
     65       .Times(5);
     66 
     67   for (int i = 0; i < 10; ++i) {
     68     input_filter.InjectMouseEvent(MouseMoveEvent(0, 0));
     69     if (i == 4)
     70       input_filter.LocalMouseMoved(webrtc::DesktopVector(1, 1));
     71   }
     72 }
     73 
     74 // Verify that echos of injected events don't block activity.
     75 TEST(RemoteInputFilterTest, LocalEchoesOfRemoteActivity) {
     76   MockInputStub mock_stub;
     77   InputEventTracker input_tracker(&mock_stub);
     78   RemoteInputFilter input_filter(&input_tracker);
     79 
     80   EXPECT_CALL(mock_stub, InjectMouseEvent(_))
     81       .Times(10);
     82 
     83   for (int i = 0; i < 10; ++i) {
     84     input_filter.InjectMouseEvent(MouseMoveEvent(0, 0));
     85     input_filter.LocalMouseMoved(webrtc::DesktopVector(0, 0));
     86   }
     87 }
     88 
     89 // Verify that echos followed by a mismatch blocks activity.
     90 TEST(RemoteInputFilterTest, LocalEchosAndLocalActivity) {
     91   MockInputStub mock_stub;
     92   InputEventTracker input_tracker(&mock_stub);
     93   RemoteInputFilter input_filter(&input_tracker);
     94 
     95   EXPECT_CALL(mock_stub, InjectMouseEvent(_))
     96       .Times(5);
     97 
     98   for (int i = 0; i < 10; ++i) {
     99     input_filter.InjectMouseEvent(MouseMoveEvent(0, 0));
    100     input_filter.LocalMouseMoved(webrtc::DesktopVector(0, 0));
    101     if (i == 4)
    102       input_filter.LocalMouseMoved(webrtc::DesktopVector(1, 1));
    103   }
    104 }
    105 
    106 // Verify that local activity also causes buttons & keys to be released.
    107 TEST(RemoteInputFilterTest, LocalActivityReleasesAll) {
    108   MockInputStub mock_stub;
    109   InputEventTracker input_tracker(&mock_stub);
    110   RemoteInputFilter input_filter(&input_tracker);
    111 
    112   EXPECT_CALL(mock_stub, InjectMouseEvent(_))
    113       .Times(5);
    114 
    115   // Use release of a key as a proxy for InputEventTracker::ReleaseAll()
    116   // having been called, rather than mocking it.
    117   EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(0, true)));
    118   EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(0, false)));
    119   input_filter.InjectKeyEvent(UsbKeyEvent(0, true));
    120 
    121   for (int i = 0; i < 10; ++i) {
    122     input_filter.InjectMouseEvent(MouseMoveEvent(0, 0));
    123     input_filter.LocalMouseMoved(webrtc::DesktopVector(0, 0));
    124     if (i == 4)
    125       input_filter.LocalMouseMoved(webrtc::DesktopVector(1, 1));
    126   }
    127 }
    128 
    129 }  // namespace remoting
    130