From 88c0c030f8e4cc8583e7012fd32e5ed08dfc505f Mon Sep 17 00:00:00 2001 From: Povilas Kanapickas Date: Thu, 7 Jan 2021 23:43:54 +0200 Subject: [PATCH] gui: Add tests for KeySequence serialization --- CMakeLists.txt | 3 + src/gui/CMakeLists.txt | 31 +++++++- src/gui/test/KeySequenceTests.cpp | 126 ++++++++++++++++++++++++++++++ src/gui/test/main.cpp | 23 ++++++ 4 files changed, 181 insertions(+), 2 deletions(-) create mode 100644 src/gui/test/KeySequenceTests.cpp create mode 100644 src/gui/test/main.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 49f33471..c56b30be 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -430,4 +430,7 @@ endif() else() message (STATUS "NOT configuring the installer") endif() + +enable_testing() + add_subdirectory (src) diff --git a/src/gui/CMakeLists.txt b/src/gui/CMakeLists.txt index 8c725f3d..50920945 100644 --- a/src/gui/CMakeLists.txt +++ b/src/gui/CMakeLists.txt @@ -6,6 +6,15 @@ set (CMAKE_AUTORCC ON) set (CMAKE_AUTOUIC ON) set (CMAKE_INCLUDE_CURRENT_DIR ON) +# files that are used both in tests and the app +set(GUI_COMMON_SOURCE_FILES + src/KeySequence.cpp +) + +set(GUI_COMMON_HEADER_FILES + src/KeySequence.h +) + set(GUI_SOURCE_FILES src/AboutDialog.cpp src/Action.cpp @@ -23,7 +32,6 @@ set(GUI_SOURCE_FILES src/IpcClient.cpp src/Ipc.cpp src/IpcReader.cpp - src/KeySequence.cpp src/KeySequenceWidget.cpp src/LogWindow.cpp src/main.cpp @@ -67,7 +75,6 @@ set(GUI_HEADER_FILES src/IpcClient.h src/Ipc.h src/IpcReader.h - src/KeySequence.h src/KeySequenceWidget.h src/LogWindow.h src/MainWindow.h @@ -113,6 +120,8 @@ if (WIN32) endif() add_executable (barrier WIN32 + ${GUI_COMMON_SOURCE_FILES} + ${GUI_COMMON_HEADER_FILES} ${GUI_SOURCE_FILES} ${GUI_HEADER_FILES} ${GUI_UI_FILES} @@ -151,3 +160,21 @@ if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin") elseif (${CMAKE_SYSTEM_NAME} MATCHES "Linux" OR ${CMAKE_SYSTEM_NAME} MATCHES "BSD") install (TARGETS barrier DESTINATION bin) endif() + +if (BARRIER_BUILD_TESTS) + set(GUI_TEST_SOURCE_FILES + test/KeySequenceTests.cpp + test/main.cpp + ) + + add_executable(guiunittests + ${GUI_TEST_SOURCE_FILES} + ${GUI_COMMON_SOURCE_FILES} + ${GUI_COMMON_HEADER_FILES} + ) + + add_test(guiunittests guiunittests) + + target_include_directories(guiunittests PUBLIC ../../ext) + target_link_libraries(guiunittests gtest gmock Qt5::Core Qt5::Widgets Qt5::Network pthread) +endif() diff --git a/src/gui/test/KeySequenceTests.cpp b/src/gui/test/KeySequenceTests.cpp new file mode 100644 index 00000000..669d8674 --- /dev/null +++ b/src/gui/test/KeySequenceTests.cpp @@ -0,0 +1,126 @@ +/* barrier -- mouse and keyboard sharing utility + Copyright (C) 2021 Povilas Kanapickas + + 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. + + 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 . +*/ + +#include "../src/KeySequence.h" +#include +#include + +#include +#include +#include + +namespace { + + auto s_key_sequence_test_keys = { + Qt::Key_Space, + Qt::Key_Escape, + Qt::Key_Tab, + Qt::Key_Backtab, + Qt::Key_Backspace, + Qt::Key_Return, + Qt::Key_Insert, + Qt::Key_Delete, + Qt::Key_Pause, + Qt::Key_Print, + Qt::Key_SysReq, + Qt::Key_Home, + Qt::Key_End, + Qt::Key_Left, + Qt::Key_Up, + Qt::Key_Right, + Qt::Key_Down, + Qt::Key_Comma, + Qt::Key_Semicolon, + Qt::Key_PageUp, + Qt::Key_PageDown, + Qt::Key_CapsLock, + Qt::Key_NumLock, + Qt::Key_ScrollLock, + Qt::Key_Help, + Qt::Key_Enter, + Qt::Key_Clear, + Qt::Key_Back, + Qt::Key_Forward, + Qt::Key_Stop, + Qt::Key_Refresh, + Qt::Key_VolumeDown, + Qt::Key_VolumeMute, + Qt::Key_VolumeUp, + Qt::Key_MediaPlay, + Qt::Key_MediaStop, + Qt::Key_MediaPrevious, + Qt::Key_MediaNext, + Qt::Key_HomePage, + Qt::Key_Favorites, + Qt::Key_Search, + Qt::Key_Standby, + Qt::Key_LaunchMail, + Qt::Key_LaunchMedia, + Qt::Key_Launch0, + Qt::Key_Launch1, + Qt::Key_Select, + }; + + QString getTemporaryFilename() + { + QTemporaryFile temp_file; + temp_file.open(); + return temp_file.fileName(); + } +} + +class KeySequenceLoadSaveTestFixture : + public ::testing::TestWithParam> {}; + +TEST_P(KeySequenceLoadSaveTestFixture, SupportsSpecialSymbols) +{ + int key = std::tr1::get<0>(GetParam()); + QSettings::Format format = std::tr1::get<1>(GetParam()); + + auto filename = getTemporaryFilename(); + + { + QSettings settings(filename, format); + KeySequence sequence; + + sequence.appendKey(key, 0); + sequence.appendKey(key, 0); + settings.beginGroup("test"); + sequence.saveSettings(settings); + settings.endGroup(); + } + { + QSettings settings(filename, format); + KeySequence sequence; + + settings.beginGroup("test"); + sequence.loadSettings(settings); + settings.endGroup(); + + const auto& data = sequence.sequence(); + ASSERT_EQ(data.size(), 2); + ASSERT_EQ(data[0], key); + ASSERT_EQ(data[1], key); + } + + QFile::remove(filename); +} + +INSTANTIATE_TEST_CASE_P( + KeySequenceLoadSaveTests, + KeySequenceLoadSaveTestFixture, + ::testing::Combine(::testing::ValuesIn(s_key_sequence_test_keys), + ::testing::Values(QSettings::NativeFormat, QSettings::IniFormat))); diff --git a/src/gui/test/main.cpp b/src/gui/test/main.cpp new file mode 100644 index 00000000..b57dd767 --- /dev/null +++ b/src/gui/test/main.cpp @@ -0,0 +1,23 @@ +/* barrier -- mouse and keyboard sharing utility + Copyright (C) 2021 Povilas Kanapickas + + 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. + + 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 . +*/ + +#include + +int main(int argc, char **argv) +{ + testing::InitGoogleTest(&argc, argv); + return (RUN_ALL_TESTS() == 1) ? 1 : 0; +}