feat: add Initial Qt Tests items

create a new SettingsTests Test
This commit is contained in:
sithlord48
2025-03-28 15:00:43 -04:00
committed by Nick Bolton
parent 5365e34f08
commit 807f32975a
5 changed files with 191 additions and 0 deletions

View File

@ -12,4 +12,6 @@ add_subdirectory(apps)
option(BUILD_TESTS "Build tests" ON)
if(BUILD_TESTS)
add_subdirectory(test)
add_subdirectory(unittests)
endif()

View File

@ -0,0 +1,65 @@
# SPDX-FileCopyrightText: 2025 Deskflow Developers
# SPDX-License-Identifier: MIT
## Use To create tests
function(create_test)
set(options)
set(oneValueArgs
NAME #NAME of new test
WORKING_DIRECTORY #Working Dir
DEPENDS #Library being tested
SOURCE #Single Source File
)
set(multiValueArgs
LIBS #Any Additional libs that are not Qt::Test or the DEPENDS lib
)
cmake_parse_arguments(m "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
if("${m_NAME}" STREQUAL "")
message(FATAL_ERROR "create_test, tests require a NAME")
endif()
if("${m_WORKING_DIRECTORY}" STREQUAL "")
message(FATAL_ERROR "create_test, tests require a WORKING_DIRECTORY")
endif()
if("${m_SOURCE}" STREQUAL "")
message(FATAL_ERROR "create_test, tests require a SOURCE")
endif()
if("${m_DEPENDS}" STREQUAL "")
message(FATAL_ERROR "create_test, tests require a DEPENDS")
endif()
add_executable(${m_NAME} ${m_SOURCE})
target_link_libraries(${m_NAME} ${m_DEPENDS} ${m_LIBS} Qt::Test)
add_test(NAME ${m_NAME} COMMAND $<TARGET_FILE:${m_NAME}> WORKING_DIRECTORY ${m_WORKING_DIRECTORY})
set_tests_properties(${m_NAME} PROPERTIES DEPENDS ${m_DEPENDS})
set_property(GLOBAL APPEND PROPERTY ${CMAKE_PROJECT_NAME}_tests ${m_NAME})
endfunction()
enable_testing()
find_package(Qt6 ${REQUIRED_QT_VERSION} REQUIRED COMPONENTS Test)
add_subdirectory(common)
#make sure to use CI only plugin on Unix
if (UNIX AND NOT APPLE)
set(qPA_Platform "QT_QPA_PLATFORM=minimal")
endif()
#Store all test that should be run post build in the property
get_property(PROJECT_TESTS GLOBAL PROPERTY ${CMAKE_PROJECT_NAME}_tests)
if(APPLE)
set(guiApp Deskflow)
else()
set(guiApp deskflow)
endif()
add_custom_target(run_tests ALL DEPENDS ${PROJECT_TESTS} ${guiApp})
add_custom_command (
TARGET run_tests
POST_BUILD
COMMAND ${qPA_Platform} ${CMAKE_CTEST_COMMAND} --test-dir "${CMAKE_BINARY_DIR}/src/unittests" --output-on-failure
)

View File

@ -0,0 +1,9 @@
# SPDX-FileCopyrightText: 2025 Deskflow Developers
# SPDX-License-Identifier: MIT
create_test(
NAME SettingsTests
DEPENDS common
SOURCE SettingsTests.cpp
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/src/lib/common"
)

View File

@ -0,0 +1,79 @@
/*
* Deskflow -- mouse and keyboard sharing utility
* SPDX-FileCopyrightText: (C) 2025 Chris Rizzitello <sithlord48@gmail.com>
* SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
*/
#include "SettingsTests.h"
#include <QFile>
#include <QSignalSpy>
void SettingsTests::initTestCase()
{
QFile oldSettings(m_settingsFile);
if (oldSettings.exists())
oldSettings.remove();
}
void SettingsTests::setSettingsFile()
{
Settings::setSettingFile(m_settingsFile);
}
void SettingsTests::settingsFile()
{
QVERIFY(Settings::settingsFile().endsWith(m_settingsFile));
}
void SettingsTests::settingsPath()
{
QVERIFY(Settings::settingsPath().endsWith(m_settingsPath));
}
void SettingsTests::tlsDir()
{
QVERIFY(Settings::tlsDir().endsWith(m_expectedTlsDir));
}
void SettingsTests::tlsLocalDb()
{
QVERIFY(Settings::tlsLocalDb().endsWith(m_expectedTlsLocalDB));
}
void SettingsTests::tlsTrustedServersDb()
{
QVERIFY(Settings::tlsTrustedServersDb().endsWith(m_expectedTlsServerDB));
}
void SettingsTests::tlsTrustedClientsDb()
{
QVERIFY(Settings::tlsTrustedClientsDb().endsWith(m_expectedTlsClientDB));
}
void SettingsTests::checkValidSettings()
{
QSignalSpy spy(Settings::instance(), &Settings::settingsChanged);
QVERIFY(spy.isValid());
const auto validKeys = Settings::validKeys();
for (const auto &setting : validKeys) {
const auto value = Settings::value(setting).toString();
QCOMPARE(Settings::defaultValue(setting).toString(), value);
Settings::setValue(setting, "NEW_VALUE");
QCOMPARE(spy.count(), 1);
QCOMPARE(qvariant_cast<QString>(spy.first().at(0)), setting);
QCOMPARE(Settings::value(setting).toString(), "NEW_VALUE");
Settings::setValue(setting, QVariant());
QCOMPARE(spy.count(), 2);
QCOMPARE(Settings::value(setting).toString(), value);
// Reset the spy for the next loop
spy.clear();
QCOMPARE(spy.count(), 0);
}
}
QTEST_MAIN(SettingsTests)

View File

@ -0,0 +1,36 @@
/*
* Deskflow -- mouse and keyboard sharing utility
* SPDX-FileCopyrightText: (C) 2025 Chris Rizzitello <sithlord48@gmail.com>
* SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
*/
#include "../../lib/common/Settings.h"
#include <QTest>
class SettingsTests : public QObject
{
Q_OBJECT
private slots:
void initTestCase();
// Test are run in order top to bottom
void setSettingsFile();
void settingsFile();
void settingsPath();
void tlsDir();
void tlsLocalDb();
void tlsTrustedServersDb();
void tlsTrustedClientsDb();
void checkValidSettings();
private:
inline static const QString m_settingsPath = QStringLiteral("tmp/test");
inline static const QString m_settingsFile = QStringLiteral("%1/Deskflow.conf").arg(m_settingsPath);
inline static const QString m_expectedTlsDir = QStringLiteral("tmp/test/%1").arg(kTlsDirName);
inline static const QString m_expectedTlsLocalDB =
QStringLiteral("%1/%2").arg(m_expectedTlsDir, kTlsFingerprintLocalFilename);
inline static const QString m_expectedTlsServerDB =
QStringLiteral("%1/%2").arg(m_expectedTlsDir, kTlsFingerprintTrustedServersFilename);
inline static const QString m_expectedTlsClientDB =
QStringLiteral("%1/%2").arg(m_expectedTlsDir, kTlsFingerprintTrustedClientsFilename);
};