deskflow/src/test/integtests/platform/XWindowsKeyStateTests.cpp

216 lines
5.8 KiB
C++
Raw Normal View History

2012-06-10 16:50:54 +00:00
/*
* synergy -- mouse and keyboard sharing utility
2016-09-07 14:24:00 +00:00
* Copyright (C) 2012-2016 Symless Ltd.
* Copyright (C) 2011 Nick Bolton
2012-06-10 16:50:54 +00:00
*
* This package is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* found in the file LICENSE that should have accompanied this file.
2012-06-10 16:50:54 +00:00
*
* This package is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define TEST_ENV
2014-02-28 15:36:51 +00:00
#include "test/mock/synergy/MockEventQueue.h"
#include "test/mock/synergy/MockKeyMap.h"
#include "base/Log.h"
#include "platform/XWindowsKeyState.h"
2012-06-10 16:50:54 +00:00
#define XK_LATIN1
#define XK_MISCELLANY
#include <X11/keysymdef.h>
2012-06-10 16:50:54 +00:00
#if HAVE_XKB_EXTENSION
#include <X11/XKBlib.h>
2012-06-10 16:50:54 +00:00
#endif
#include <errno.h>
Solve SonarCloud security hotspots and bugs (#7383) * Test: allocLockCursorToScreenInfo_withState_setsState * Merge remote-tracking branch 'origin/master' into S1-1717-sonarcloud-issues * Remove header wrappers for gmock/gtest * Convenience wrapper script for tests * Use `std::copy` instead of `strcpy` * Use `std::ranges::copy` instead of `stdcpy` * Delete dead code * Add guitests VS Code config and test wrapper * Revert "Delete dead code" This reverts commit aa40f5cd35a22b4b69acfd1876aceea053088060. * Build guitests with CMake * Run all tests * Don't use --gtest_filter for Qt tests * Undo skip for GUI tests * Coverage for `IpcClient::sendCommand` * Remove provider and proxy * Use lamda for StreamProvider to reduce boilerplate * Restore version checker tests * Remove activation souces * Tasks for current/all tests * Change command for tasks * Mock QNetworkAccessManager * Create core app to satisfy Qt assertations * Use `std::copy` instead of `std::ranges::copy` * Remove integtests * Merge guitests into unittests * Use std::string::length * Remove include (resolves to root) * Fix memory leaks * Disable sigsegv tests * Fixed formatting * Remove guitests from CI * New MainWindowTests * Passing test for MainWindowTests * Use alternative to strlen in MainWindow::checkSecureSocket * Passing test for Log::print * Fixed dtor call order * Fixed var name typo * Use proxy instead of `#define protected public` * Add args test for log * Add license ctor * Fixed log test for release * Add error log test * Init qt with -platform offscreen * Back-out initQt function * Use QT_QPA_PLATFORM * Try QT_QPA_PLATFORM=offscreen * Use more readable env node * Set QT_QPA_PLATFORM in CI and CodeQL * Remove env not needed * Modernize Log::print * Calculate the length of the format string fmt during the initial scan loop * More direct and efficient alterative to `strlen` * Fixed major maint issues in AppConfig * New clang and cmake rules * Set `max_pargs_hwrap` to 4 * Undo clang format for now * Fix missing `Setting::` * Fixed missing `m_S` on Windows * Re-add accidental dep resolution (fix later) * Fixed missing buffer position increment * Turn on errors * Fixed tests and improve error message * Fixed segfault on log refactor * Extract log time to function * Copyright date * Update ChangeLog
2024-07-16 08:30:35 +00:00
#include <gmock/gmock.h>
#include <gtest/gtest.h>
class XWindowsKeyStateTests : public ::testing::Test {
2012-06-10 16:50:54 +00:00
protected:
XWindowsKeyStateTests() : m_display(NULL) {}
~XWindowsKeyStateTests() {
if (m_display != NULL) {
LOG((CLOG_DEBUG "closing display"));
XCloseDisplay(m_display);
}
}
virtual void SetUp() {
// open the display only once for the entire test suite
if (this->m_display == NULL) {
LOG((CLOG_DEBUG "opening display"));
this->m_display = XOpenDisplay(NULL);
ASSERT_TRUE(this->m_display != NULL)
<< "unable to open display: " << errno;
}
}
virtual void TearDown() {}
Display *m_display;
2012-06-10 16:50:54 +00:00
};
TEST_F(XWindowsKeyStateTests, setActiveGroup_pollAndSet_groupIsZero) {
MockKeyMap keyMap;
MockEventQueue eventQueue;
XWindowsKeyState keyState(m_display, true, &eventQueue, keyMap);
2012-06-10 16:50:54 +00:00
keyState.setActiveGroup(XWindowsKeyState::kGroupPollAndSet);
2012-06-10 16:50:54 +00:00
ASSERT_EQ(0, keyState.group());
2012-06-10 16:50:54 +00:00
}
TEST_F(XWindowsKeyStateTests, setActiveGroup_poll_groupIsNotSet) {
MockKeyMap keyMap;
MockEventQueue eventQueue;
XWindowsKeyState keyState(m_display, true, &eventQueue, keyMap);
2012-06-10 16:50:54 +00:00
keyState.setActiveGroup(XWindowsKeyState::kGroupPoll);
2012-06-10 16:50:54 +00:00
ASSERT_LE(-1, keyState.group());
2012-06-10 16:50:54 +00:00
}
TEST_F(XWindowsKeyStateTests, setActiveGroup_customGroup_groupWasSet) {
MockKeyMap keyMap;
MockEventQueue eventQueue;
XWindowsKeyState keyState(m_display, true, &eventQueue, keyMap);
2012-06-10 16:50:54 +00:00
keyState.setActiveGroup(1);
2012-06-10 16:50:54 +00:00
ASSERT_EQ(1, keyState.group());
2012-06-10 16:50:54 +00:00
}
TEST_F(XWindowsKeyStateTests, mapModifiersFromX_zeroState_zeroMask) {
MockKeyMap keyMap;
MockEventQueue eventQueue;
XWindowsKeyState keyState(m_display, true, &eventQueue, keyMap);
2012-06-10 16:50:54 +00:00
int mask = keyState.mapModifiersFromX(0);
2012-06-10 16:50:54 +00:00
ASSERT_EQ(0, mask);
2012-06-10 16:50:54 +00:00
}
TEST_F(XWindowsKeyStateTests, mapModifiersToX_zeroMask_resultIsTrue) {
MockKeyMap keyMap;
MockEventQueue eventQueue;
XWindowsKeyState keyState(m_display, true, &eventQueue, keyMap);
2012-06-10 16:50:54 +00:00
unsigned int modifiers = 0;
bool result = keyState.mapModifiersToX(0, modifiers);
2012-06-10 16:50:54 +00:00
ASSERT_TRUE(result);
2012-06-10 16:50:54 +00:00
}
TEST_F(XWindowsKeyStateTests, fakeCtrlAltDel_default_returnsFalse) {
MockKeyMap keyMap;
MockEventQueue eventQueue;
XWindowsKeyState keyState(m_display, true, &eventQueue, keyMap);
2012-06-10 16:50:54 +00:00
bool result = keyState.fakeCtrlAltDel();
2012-06-10 16:50:54 +00:00
ASSERT_FALSE(result);
2012-06-10 16:50:54 +00:00
}
TEST_F(XWindowsKeyStateTests, pollActiveModifiers_defaultState_returnsZero) {
MockKeyMap keyMap;
MockEventQueue eventQueue;
XWindowsKeyState keyState(m_display, true, &eventQueue, keyMap);
2012-06-10 16:50:54 +00:00
KeyModifierMask actual = keyState.pollActiveModifiers();
2012-06-10 16:50:54 +00:00
ASSERT_EQ(0, actual);
2012-06-10 16:50:54 +00:00
}
#if 0 // TODO: fix, causes sigsegv
2014-11-12 11:28:41 +00:00
TEST_F(XWindowsKeyStateTests, pollActiveModifiers_shiftKeyDownThenUp_masksAreCorrect)
2012-06-10 16:50:54 +00:00
{
MockKeyMap keyMap;
MockEventQueue eventQueue;
XWindowsKeyState keyState(
m_display, true, &eventQueue, keyMap);
2012-06-10 16:50:54 +00:00
// set mock modifier mapping
std::fill(keyState.modifierFromX().begin(), keyState.modifierFromX().end(), 0);
keyState.modifierFromX()[ShiftMapIndex] = KeyModifierShift;
2012-06-10 16:50:54 +00:00
KeyCode key = XKeysymToKeycode(m_display, XK_Shift_L);
2012-06-10 16:50:54 +00:00
// fake shift key down (without using synergy)
XTestFakeKeyEvent(m_display, key, true, CurrentTime);
2012-06-10 16:50:54 +00:00
// function under test (1st call)
KeyModifierMask modDown = keyState.pollActiveModifiers();
2012-06-10 16:50:54 +00:00
// fake shift key up (without using synergy)
XTestFakeKeyEvent(m_display, key, false, CurrentTime);
2012-06-10 16:50:54 +00:00
// function under test (2nd call)
KeyModifierMask modUp = keyState.pollActiveModifiers();
2012-06-10 16:50:54 +00:00
EXPECT_TRUE((modDown & KeyModifierShift) == KeyModifierShift)
<< "shift key not in mask - key was not pressed";
2012-06-10 16:50:54 +00:00
EXPECT_TRUE((modUp & KeyModifierShift) == 0)
<< "shift key still in mask - make sure no keys are being held down";
2012-06-10 16:50:54 +00:00
}
#endif
2012-06-10 16:50:54 +00:00
TEST_F(XWindowsKeyStateTests, pollActiveGroup_defaultState_returnsZero) {
MockKeyMap keyMap;
MockEventQueue eventQueue;
XWindowsKeyState keyState(m_display, true, &eventQueue, keyMap);
2012-06-10 16:50:54 +00:00
SInt32 actual = keyState.pollActiveGroup();
2012-06-10 16:50:54 +00:00
ASSERT_EQ(0, actual);
2012-06-10 16:50:54 +00:00
}
TEST_F(XWindowsKeyStateTests, pollActiveGroup_positiveGroup_returnsGroup) {
MockKeyMap keyMap;
MockEventQueue eventQueue;
XWindowsKeyState keyState(m_display, true, &eventQueue, keyMap);
2012-06-10 16:50:54 +00:00
keyState.group(3);
2012-06-10 16:50:54 +00:00
SInt32 actual = keyState.pollActiveGroup();
2012-06-10 16:50:54 +00:00
ASSERT_EQ(3, actual);
2012-06-10 16:50:54 +00:00
}
TEST_F(XWindowsKeyStateTests, pollActiveGroup_xkb_areEqual) {
2012-06-10 16:50:54 +00:00
#if HAVE_XKB_EXTENSION
MockKeyMap keyMap;
MockEventQueue eventQueue;
XWindowsKeyState keyState(m_display, true, &eventQueue, keyMap);
2012-06-10 16:50:54 +00:00
// reset the group
keyState.group(-1);
2012-06-10 16:50:54 +00:00
XkbStateRec state;
2012-06-10 16:50:54 +00:00
// compare pollActiveGroup() with XkbGetState()
if (XkbGetState(m_display, XkbUseCoreKbd, &state) == Success) {
SInt32 actual = keyState.pollActiveGroup();
2012-06-10 16:50:54 +00:00
ASSERT_EQ(state.group, actual);
} else {
FAIL() << "XkbGetState() returned error " << errno;
}
2012-06-10 16:50:54 +00:00
#else
SUCCEED() << "Xkb extension not installed";
2012-06-10 16:50:54 +00:00
#endif
}