chromium/remoting/client/input/normalizing_input_filter_win_unittest.cc

// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <stdint.h>

#include "remoting/client/input/normalizing_input_filter_win.h"
#include "remoting/proto/event.pb.h"
#include "remoting/protocol/protocol_mock_objects.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

using ::testing::InSequence;
InputStub;
KeyEvent;
MockInputStub;
MouseEvent;

namespace remoting {

namespace {

const unsigned int kUsbKeyA =;
const unsigned int kUsbLeftControl =;
const unsigned int kUsbRightAlt =;

// A hardcoded value used to verify |lock_states| is preserved.
static const uint32_t kTestLockStates = protocol::KeyEvent::LOCK_STATES_NUMLOCK;

MATCHER_P2(EqualsKeyEvent, usb_keycode, pressed, "") {}

KeyEvent MakeKeyEvent(uint32_t keycode, bool pressed) {}

void PressAndReleaseKey(InputStub* input_stub, uint32_t keycode) {}

MATCHER_P2(EqualsMouseMoveEvent, x, y, "") {}

static MouseEvent MakeMouseMoveEvent(int x, int y) {}

}  // namespace

// Test press/release of LeftControl, RightAlt, then LeftControl again.
TEST() {
  MockInputStub stub;
  NormalizingInputFilterWin processor(&stub);

  {
    InSequence s;

    EXPECT_CALL();
    EXPECT_CALL();
    EXPECT_CALL();
    EXPECT_CALL();
    EXPECT_CALL();
    EXPECT_CALL();
  }

  // Inject press & release events.
  PressAndReleaseKey(&processor, kUsbLeftControl);
  PressAndReleaseKey(&processor, kUsbRightAlt);
  PressAndReleaseKey(&processor, kUsbLeftControl);
}

// Test LeftControl key repeat causes it to be treated as LeftControl.
TEST() {
  MockInputStub stub;
  NormalizingInputFilterWin processor(&stub);

  {
    InSequence s;

    EXPECT_CALL();
    EXPECT_CALL();
    EXPECT_CALL();
  }

  // Inject a press and repeats for LeftControl, and verify that the repeats
  // result in press events.
  processor.InjectKeyEvent(MakeKeyEvent(kUsbLeftControl, true));
  processor.InjectKeyEvent(MakeKeyEvent(kUsbLeftControl, true));
  processor.InjectKeyEvent(MakeKeyEvent(kUsbLeftControl, false));
}

// Test LeftControl key pressed while pressing & releasing a printable key and
// then RightAlt treats it as LeftControl, not AltGr combo.
TEST() {
  MockInputStub stub;
  NormalizingInputFilterWin processor(&stub);

  {
    InSequence s;

    EXPECT_CALL();
    EXPECT_CALL();
    EXPECT_CALL();
    EXPECT_CALL();
    EXPECT_CALL();
    EXPECT_CALL();
  }

  // Inject a press for LeftControl, press and release a character key, then
  // press RightAlt, and finally release LeftControl.
  processor.InjectKeyEvent(MakeKeyEvent(kUsbLeftControl, true));
  PressAndReleaseKey(&processor, kUsbKeyA);
  PressAndReleaseKey(&processor, kUsbRightAlt);
  processor.InjectKeyEvent(MakeKeyEvent(kUsbLeftControl, false));
}

// Test LeftControl press followed by RightAlt press results in
// it being interpreted as AltGr, so only RightAlt events generated.
// Also press a printable key while AltGr is down to verify that doesn't
// confuse things.
TEST() {
  MockInputStub stub;
  NormalizingInputFilterWin processor(&stub);

  {
    InSequence s;

    EXPECT_CALL();
    EXPECT_CALL();
    EXPECT_CALL();
    EXPECT_CALL();
  }

  // Hold LeftControl and then RightAlt, then release.
  processor.InjectKeyEvent(MakeKeyEvent(kUsbLeftControl, true));
  processor.InjectKeyEvent(MakeKeyEvent(kUsbRightAlt, true));
  PressAndReleaseKey(&processor, kUsbKeyA);
  processor.InjectKeyEvent(MakeKeyEvent(kUsbRightAlt, false));
  processor.InjectKeyEvent(MakeKeyEvent(kUsbLeftControl, false));
}

// Test LeftControl press followed by RightAlt press, and repeats results in
// it being interpreted as AltGr, so only RightAlt events generated.
TEST() {
  MockInputStub stub;
  NormalizingInputFilterWin processor(&stub);

  {
    InSequence s;

    EXPECT_CALL();
    EXPECT_CALL();
    EXPECT_CALL();
  }

  // Hold LeftControl and then RightAlt, repeat, then release. Sequence
  // reflects the order of events generated by Windows.
  processor.InjectKeyEvent(MakeKeyEvent(kUsbLeftControl, true));
  processor.InjectKeyEvent(MakeKeyEvent(kUsbRightAlt, true));
  processor.InjectKeyEvent(MakeKeyEvent(kUsbLeftControl, true));
  processor.InjectKeyEvent(MakeKeyEvent(kUsbRightAlt, true));
  processor.InjectKeyEvent(MakeKeyEvent(kUsbRightAlt, false));
  processor.InjectKeyEvent(MakeKeyEvent(kUsbLeftControl, false));
}

// Test LeftControl held during mouse event treats it as LeftControl.
TEST() {
  MockInputStub stub;
  NormalizingInputFilterWin processor(&stub);

  {
    InSequence s;

    EXPECT_CALL();
    EXPECT_CALL();
    EXPECT_CALL();
    EXPECT_CALL();
    EXPECT_CALL();
  }

  // Hold the left Control while moving the mouse.
  processor.InjectKeyEvent(MakeKeyEvent(kUsbLeftControl, true));
  processor.InjectMouseEvent(MakeMouseMoveEvent(0, 0));
  PressAndReleaseKey(&processor, kUsbRightAlt);
  processor.InjectKeyEvent(MakeKeyEvent(kUsbLeftControl, false));
}

// Test LeftControl & RightAlt held during mouse event treats it as AltGr.
TEST() {
  MockInputStub stub;
  NormalizingInputFilterWin processor(&stub);

  {
    InSequence s;

    EXPECT_CALL();
    EXPECT_CALL();
    EXPECT_CALL();
  }

  // Hold the left Control while moving the mouse.
  processor.InjectKeyEvent(MakeKeyEvent(kUsbLeftControl, true));
  processor.InjectKeyEvent(MakeKeyEvent(kUsbRightAlt, true));
  processor.InjectMouseEvent(MakeMouseMoveEvent(0, 0));
  processor.InjectKeyEvent(MakeKeyEvent(kUsbRightAlt, false));
  processor.InjectKeyEvent(MakeKeyEvent(kUsbLeftControl, false));
}

}  // namespace remoting