diff --git a/src/unittests/common/CMakeLists.txt b/src/unittests/common/CMakeLists.txt index 8bc9e4ef7..321493cfd 100644 --- a/src/unittests/common/CMakeLists.txt +++ b/src/unittests/common/CMakeLists.txt @@ -7,3 +7,10 @@ create_test( SOURCE SettingsTests.cpp WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/src/lib/common" ) + +create_test( + NAME I18NTests + DEPENDS common + SOURCE I18NTests.cpp + WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/src/lib/common" +) diff --git a/src/unittests/common/I18NTests.cpp b/src/unittests/common/I18NTests.cpp new file mode 100644 index 000000000..22aead362 --- /dev/null +++ b/src/unittests/common/I18NTests.cpp @@ -0,0 +1,86 @@ +/* + * Deskflow -- mouse and keyboard sharing utility + * SPDX-FileCopyrightText: (C) 2025 Chris Rizzitello + * SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception + */ + +#include "I18NTests.h" + +#include "common/I18N.h" +#include "common/Settings.h" +#include +#include +#include + +void I18NTests::initTestCase() +{ + QFile oldSettings(m_settingsFile); + if (oldSettings.exists()) + oldSettings.remove(); + Settings::setSettingsFile(m_settingsFile); + Settings::setStateFile(m_stateFile); + + m_myTDir = QStringLiteral("%1/translations").arg(QCoreApplication::applicationDirPath()); + const auto srcTDir = QStringLiteral("%1/../../../translations").arg(QCoreApplication::applicationDirPath()); + + QDir dir; + if (dir.exists(m_myTDir)) { + dir.setPath(m_myTDir); + dir.removeRecursively(); + } + + dir.mkdir(m_myTDir); + dir.setPath(srcTDir); + for (const auto &file : dir.entryList({"deskflow_*.qm"}, QDir::Files, QDir::Name)) { + QFile::copy(QStringLiteral("%1/%2").arg(srcTDir, file), QStringLiteral("%1/%2").arg(m_myTDir, file)); + QVERIFY(QFile::exists(QStringLiteral("%1/%2").arg(m_myTDir, file))); + } +} + +void I18NTests::creationTest() +{ + QVERIFY(I18N::instance()); +} + +void I18NTests::detectedLangTest() +{ + for (const auto &lang : I18N::detectedLanguages()) + QVERIFY(m_langMap.contains(lang)); +} + +void I18NTests::check639NameTest() +{ + for (const auto &lang : m_langMap.keys()) + QCOMPARE(I18N::nativeTo639Name(lang), m_langMap.value(lang)); +} + +void I18NTests::toNativeNameTest() +{ + for (const auto &lang : m_langMap.values()) + QCOMPARE(I18N::toNativeName(lang), m_langMap.key(lang)); +} + +void I18NTests::setLangTest() +{ + QSignalSpy spy(I18N::instance(), &I18N::languageChanged); + for (const auto &lang : m_langMap.values()) { + I18N::setLanguage(lang); + QCOMPARE(I18N::currentLanguage(), lang); + } + QCOMPARE(spy.count(), m_langMap.count()); +} + +void I18NTests::reDetectTest() +{ + QSignalSpy spy(I18N::instance(), &I18N::langaugesChanged); + + I18N::reDetectLanguages(); + QCOMPARE(spy.count(), 0); + + QFile::remove(QStringLiteral("%1/deskflow_en.qm").arg(m_myTDir)); + + I18N::reDetectLanguages(); + QCOMPARE(spy.count(), 1); +} + +QTEST_MAIN(I18NTests) diff --git a/src/unittests/common/I18NTests.h b/src/unittests/common/I18NTests.h new file mode 100644 index 000000000..56f86d1a1 --- /dev/null +++ b/src/unittests/common/I18NTests.h @@ -0,0 +1,31 @@ +/* + * Deskflow -- mouse and keyboard sharing utility + * SPDX-FileCopyrightText: (C) 2025 Chris Rizzitello + * SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception + */ + +#include + +class I18NTests : public QObject +{ + Q_OBJECT +private Q_SLOTS: + void initTestCase(); + void creationTest(); + void detectedLangTest(); + void check639NameTest(); + void toNativeNameTest(); + void setLangTest(); + void reDetectTest(); + +private: + QString m_myTDir; + inline static const QString m_settingsPathTemp = QStringLiteral("tmp/test"); + inline static const QString m_settingsFile = QStringLiteral("%1/Deskflow.conf").arg(m_settingsPathTemp); + inline static const QString m_stateFile = QStringLiteral("%1/Deskflow.state").arg(m_settingsPathTemp); + inline static const QMap m_langMap = { + {QStringLiteral("English"), QStringLiteral("en")}, {QStringLiteral("Español"), QStringLiteral("es")}, + {QStringLiteral("Italiano"), QStringLiteral("it")}, {QStringLiteral("日本語"), QStringLiteral("ja")}, + {QStringLiteral("Русский"), QStringLiteral("ru")}, {QStringLiteral("简体中文"), QStringLiteral("zh_CN")} + }; +};