Home | History | Annotate | Download | only in protocol
      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/protocol/input_filter.h"
      6 
      7 #include "remoting/proto/event.pb.h"
      8 #include "remoting/protocol/protocol_mock_objects.h"
      9 #include "testing/gmock/include/gmock/gmock.h"
     10 #include "testing/gtest/include/gtest/gtest.h"
     11 
     12 using ::testing::_;
     13 
     14 namespace remoting {
     15 namespace protocol {
     16 
     17 MATCHER_P2(EqualsKeyEvent, usb_keycode, pressed, "") {
     18   return arg.usb_keycode() == static_cast<uint32>(usb_keycode) &&
     19          arg.pressed() == pressed;
     20 }
     21 
     22 MATCHER_P(EqualsTextEvent, text, "") {
     23   return arg.text() == text;
     24 }
     25 
     26 MATCHER_P2(EqualsMouseMoveEvent, x, y, "") {
     27   return arg.x() == x && arg.y() == y;
     28 }
     29 
     30 static KeyEvent NewKeyEvent(uint32 usb_keycode, bool pressed) {
     31   KeyEvent event;
     32   event.set_usb_keycode(usb_keycode);
     33   event.set_pressed(pressed);
     34   return event;
     35 }
     36 
     37 static TextEvent NewTextEvent(const std::string& text) {
     38   TextEvent event;
     39   event.set_text(text);
     40   return event;
     41 }
     42 
     43 static MouseEvent MouseMoveEvent(int x, int y) {
     44   MouseEvent event;
     45   event.set_x(x);
     46   event.set_y(y);
     47   return event;
     48 }
     49 
     50 static void InjectTestSequence(protocol::InputStub* input_stub) {
     51   // Inject a key event.
     52   input_stub->InjectKeyEvent(NewKeyEvent(0, true));
     53   input_stub->InjectKeyEvent(NewKeyEvent(0, false));
     54 
     55   // Inject a text event
     56   input_stub->InjectTextEvent(NewTextEvent("test"));
     57 
     58   // Inject mouse movemement.
     59   input_stub->InjectMouseEvent(MouseMoveEvent(10, 20));
     60 }
     61 
     62 // Verify that the filter passes events on correctly to a configured stub.
     63 TEST(InputFilterTest, EventsPassThroughFilter) {
     64   testing::StrictMock<MockInputStub> input_stub;
     65   InputFilter input_filter(&input_stub);
     66 
     67   EXPECT_CALL(input_stub, InjectKeyEvent(EqualsKeyEvent(0, true)));
     68   EXPECT_CALL(input_stub, InjectKeyEvent(EqualsKeyEvent(0, false)));
     69   EXPECT_CALL(input_stub, InjectTextEvent(EqualsTextEvent("test")));
     70   EXPECT_CALL(input_stub, InjectMouseEvent(EqualsMouseMoveEvent(10, 20)));
     71 
     72   InjectTestSequence(&input_filter);
     73 }
     74 
     75 // Verify that the filter ignores events if disabled.
     76 TEST(InputFilterTest, IgnoreEventsIfDisabled) {
     77   testing::StrictMock<MockInputStub> input_stub;
     78   InputFilter input_filter(&input_stub);
     79 
     80   input_filter.set_enabled(false);
     81   InjectTestSequence(&input_filter);
     82 }
     83 
     84 // Verify that the filter ignores events if not configured.
     85 TEST(InputFilterTest, IgnoreEventsIfNotConfigured) {
     86   InputFilter input_filter;
     87 
     88   InjectTestSequence(&input_filter);
     89 }
     90 
     91 } // namespace protocol
     92 } // namespace remoting
     93