From 7fa326234e541c0f32792804a8a086910716ce9c Mon Sep 17 00:00:00 2001 From: sithlord48 Date: Sat, 5 Apr 2025 14:33:29 -0400 Subject: [PATCH] refactor: port X11LayoutParserTests to QtTests --- .../deskflow/X11LayoutParserTests.cpp | 149 ------------------ src/unittests/deskflow/CMakeLists.txt | 9 ++ .../deskflow/X11LayoutParserTests.cpp | 85 ++++++++++ src/unittests/deskflow/X11LayoutParserTests.h | 105 ++++++++++++ 4 files changed, 199 insertions(+), 149 deletions(-) delete mode 100644 src/test/unittests/deskflow/X11LayoutParserTests.cpp create mode 100644 src/unittests/deskflow/X11LayoutParserTests.cpp create mode 100644 src/unittests/deskflow/X11LayoutParserTests.h diff --git a/src/test/unittests/deskflow/X11LayoutParserTests.cpp b/src/test/unittests/deskflow/X11LayoutParserTests.cpp deleted file mode 100644 index 0eaaf0656..000000000 --- a/src/test/unittests/deskflow/X11LayoutParserTests.cpp +++ /dev/null @@ -1,149 +0,0 @@ -/* - * Deskflow -- mouse and keyboard sharing utility - * SPDX-FileCopyrightText: (C) 2014 - 2016 Symless Ltd. - * SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception - */ - -#if WINAPI_XWINDOWS -#include "deskflow/unix/X11LayoutsParser.h" - -#include -#include - -const std::string testDir = "tmp/test"; - -void createTestFiles() -{ - std::ofstream correctEvdevFile(testDir + "/correctEvdev.xml"); - if (!correctEvdevFile.is_open()) { - FAIL(); - } - - correctEvdevFile << "" << std::endl; - correctEvdevFile << "" << std::endl; - correctEvdevFile << " " << std::endl; - correctEvdevFile << " " << std::endl; - correctEvdevFile << " " << std::endl; - correctEvdevFile << " us" << std::endl; - correctEvdevFile << " " << std::endl; - correctEvdevFile << " en" << std::endl; - correctEvdevFile << " English (US)" << std::endl; - correctEvdevFile << " " << std::endl; - correctEvdevFile << " eng" << std::endl; - correctEvdevFile << " " << std::endl; - correctEvdevFile << " " << std::endl; - correctEvdevFile << " " << std::endl; - correctEvdevFile << " " << std::endl; - correctEvdevFile << " " << std::endl; - correctEvdevFile << " eng" << std::endl; - correctEvdevFile << " eng" << std::endl; - correctEvdevFile << " Cherokee" << std::endl; - correctEvdevFile << " " << std::endl; - correctEvdevFile << " eng" << std::endl; - correctEvdevFile << " " << std::endl; - correctEvdevFile << " " << std::endl; - correctEvdevFile << " " << std::endl; - correctEvdevFile << " " << std::endl; - correctEvdevFile << " " << std::endl; - correctEvdevFile << " " << std::endl; - correctEvdevFile << " " << std::endl; - correctEvdevFile << " ru" << std::endl; - correctEvdevFile << " " << std::endl; - correctEvdevFile << " ru" << std::endl; - correctEvdevFile << " Russian" << std::endl; - correctEvdevFile << " " << std::endl; - correctEvdevFile << " rus" << std::endl; - correctEvdevFile << " " << std::endl; - correctEvdevFile << " " << std::endl; - correctEvdevFile << " " << std::endl; - correctEvdevFile << " " << std::endl; - correctEvdevFile << "" << std::endl; - correctEvdevFile.close(); - - std::ofstream evdevFromFutureFile(testDir + "/evdevFromFuture.xml"); - if (!evdevFromFutureFile.is_open()) { - FAIL(); - } - - evdevFromFutureFile << "" << std::endl; - evdevFromFutureFile << "" << std::endl; - evdevFromFutureFile << " " << std::endl; - evdevFromFutureFile << " " << std::endl; - evdevFromFutureFile << " " << std::endl; - evdevFromFutureFile << " futureLangName" << std::endl; - evdevFromFutureFile << " " << std::endl; - evdevFromFutureFile << " fln" << std::endl; - evdevFromFutureFile << " " << std::endl; - evdevFromFutureFile << " " << std::endl; - evdevFromFutureFile << " " << std::endl; - evdevFromFutureFile << " " << std::endl; - evdevFromFutureFile << "" << std::endl; - evdevFromFutureFile.close(); - - std::ofstream incorrectEvdevFile1(testDir + "/incorrectEvdev1.xml"); - if (!incorrectEvdevFile1.is_open()) { - FAIL(); - } - - incorrectEvdevFile1 << "" << std::endl; - incorrectEvdevFile1.close(); - - std::ofstream incorrectEvdevFile2(testDir + "/incorrectEvdev2.xml"); - if (!incorrectEvdevFile2.is_open()) { - FAIL(); - } - - incorrectEvdevFile2 << "" << std::endl; - incorrectEvdevFile2 << "" << std::endl; - incorrectEvdevFile2 << "" << std::endl; - incorrectEvdevFile2.close(); - - std::ofstream incorrectEvdevFile3(testDir + "/incorrectEvdev3.xml"); - if (!incorrectEvdevFile3.is_open()) { - FAIL(); - } - - incorrectEvdevFile3 << "" << std::endl; - incorrectEvdevFile3 << "" << std::endl; - incorrectEvdevFile3 << " " << std::endl; - incorrectEvdevFile3 << " " << std::endl; - incorrectEvdevFile3 << " " << std::endl; - incorrectEvdevFile3 << " " << std::endl; - incorrectEvdevFile3 << "" << std::endl; - incorrectEvdevFile3.close(); -} - -TEST(X11LayoutsParsingTests, xmlCorrectParsingTest) -{ - createTestFiles(); - std::vector expectedResult = {"en", "ru"}; - auto parsedResult = X11LayoutsParser::getX11LanguageList(testDir + "/correctEvdev.xml"); - - EXPECT_EQ(parsedResult, parsedResult); -} - -TEST(X11LayoutsParsingTests, xmlParsingMissedEvdevFileTest) -{ - auto parsedResult = X11LayoutsParser::getX11LanguageList(testDir + "/missedFile"); - EXPECT_TRUE(parsedResult.empty()); -} - -TEST(X11LayoutsParsingTests, xmlParsingIncorrectEvdevFileTest) -{ - std::vector parsedResult; - parsedResult = X11LayoutsParser::getX11LanguageList(testDir + "/incorrectEvdev1.xml"); - EXPECT_TRUE(parsedResult.empty()); - parsedResult = X11LayoutsParser::getX11LanguageList(testDir + "/incorrectEvdev2.xml"); - EXPECT_TRUE(parsedResult.empty()); - parsedResult = X11LayoutsParser::getX11LanguageList(testDir + "/incorrectEvdev3.xml"); - EXPECT_TRUE(parsedResult.empty()); -} - -TEST(X11LayoutsParsingTests, layoutConvertTest) -{ - EXPECT_EQ(X11LayoutsParser::convertLayotToISO(testDir + "/correctEvdev.xml", "us", true), "en"); - EXPECT_EQ(X11LayoutsParser::convertLayotToISO(testDir + "/incorrectEvdev1.xml", "us", true), ""); - EXPECT_EQ(X11LayoutsParser::convertLayotToISO(testDir + "/evdevFromFuture.xml", "us", true), ""); -} - -#endif diff --git a/src/unittests/deskflow/CMakeLists.txt b/src/unittests/deskflow/CMakeLists.txt index d89c6ff8e..613530825 100644 --- a/src/unittests/deskflow/CMakeLists.txt +++ b/src/unittests/deskflow/CMakeLists.txt @@ -21,4 +21,13 @@ create_test( WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/src/lib/deskflow" ) +if(UNIX AND NOT APPLE) + create_test( + NAME X11LayoutParserTests + DEPENDS app + LIBS arch base ${extra_libs} + SOURCE X11LayoutParserTests.cpp + WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/src/lib/deskflow" + ) +endif() diff --git a/src/unittests/deskflow/X11LayoutParserTests.cpp b/src/unittests/deskflow/X11LayoutParserTests.cpp new file mode 100644 index 000000000..cc66087a6 --- /dev/null +++ b/src/unittests/deskflow/X11LayoutParserTests.cpp @@ -0,0 +1,85 @@ +/* + * Deskflow -- mouse and keyboard sharing utility + * SPDX-FileCopyrightText: (C) 2025 Chris Rizzitello + * SPDX-FileCopyrightText: (C) 2014 - 2016 Symless Ltd. + * SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception + */ + +#include "X11LayoutParserTests.h" + +#include "../../lib/deskflow/unix/X11LayoutsParser.h" + +void X11LayoutParserTests::initTestCase() +{ + QDir dir; + QVERIFY(dir.mkpath(kTestDir)); + + QFile correctEvdevFile(kTestCorrectFile); + QVERIFY(correctEvdevFile.open(QIODevice::WriteOnly)); + correctEvdevFile.write(kCorrectEvContents.toUtf8()); + correctEvdevFile.close(); + + QVERIFY(correctEvdevFile.open(QIODevice::ReadOnly)); + QCOMPARE(correctEvdevFile.readAll(), kCorrectEvContents); + correctEvdevFile.close(); + + QFile futureEvdevFile(kTestFutureFile); + QVERIFY(futureEvdevFile.open(QIODevice::WriteOnly)); + futureEvdevFile.write(kFutureEvContents.toUtf8()); + futureEvdevFile.close(); + + QVERIFY(futureEvdevFile.open(QIODevice::ReadOnly)); + QCOMPARE(futureEvdevFile.readAll(), kFutureEvContents); + futureEvdevFile.close(); + + QFile badEvdevFile1(kTestBadFile1); + QVERIFY(badEvdevFile1.open(QIODevice::WriteOnly)); + badEvdevFile1.write(kBadEv1Contents.toUtf8()); + badEvdevFile1.close(); + + QVERIFY(badEvdevFile1.open(QIODevice::ReadOnly)); + QCOMPARE(badEvdevFile1.readAll(), kBadEv1Contents); + badEvdevFile1.close(); + + QFile badEvdevFile2(kTestBadFile2); + QVERIFY(badEvdevFile2.open(QIODevice::WriteOnly)); + badEvdevFile2.write(kBadEv2Contents.toUtf8()); + badEvdevFile2.close(); + + QVERIFY(badEvdevFile2.open(QIODevice::ReadOnly)); + QCOMPARE(badEvdevFile2.readAll(), kBadEv2Contents); + badEvdevFile2.close(); + + QFile badEvdevFile3(kTestBadFile3); + QVERIFY(badEvdevFile3.open(QIODevice::WriteOnly)); + badEvdevFile3.write(kBadEv3Contents.toUtf8()); + badEvdevFile3.close(); + + QVERIFY(badEvdevFile3.open(QIODevice::ReadOnly)); + QCOMPARE(badEvdevFile3.readAll(), kBadEv3Contents); + badEvdevFile3.close(); +} + +void X11LayoutParserTests::xmlParse() +{ + const QString badPath = QStringLiteral("%1/%2").arg(kTestDir, "notafile"); + QVERIFY(X11LayoutsParser::getX11LanguageList(badPath.toStdString()).empty()); + + // we would like to check the method + // X11LayoutsParser::getAllLanguageData(kTestCorrectFile.toStdString()); + // however it's logic is flawed making as the return list size is based on number of languges host has installed + // this is correct on a real system but makes it hard to test the method + + QVERIFY(X11LayoutsParser::getX11LanguageList(kTestBadFile1.toStdString()).empty()); + QVERIFY(X11LayoutsParser::getX11LanguageList(kTestBadFile2.toStdString()).empty()); + QVERIFY(X11LayoutsParser::getX11LanguageList(kTestBadFile3.toStdString()).empty()); +} + +void X11LayoutParserTests::convertLayouts() +{ + QCOMPARE(X11LayoutsParser::convertLayotToISO(kTestCorrectFile.toStdString(), "us", true), "en"); + QCOMPARE(X11LayoutsParser::convertLayotToISO(kTestBadFile1.toStdString(), "us", true), ""); + QCOMPARE(X11LayoutsParser::convertLayotToISO(kTestFutureFile.toStdString(), "us", true), ""); +} + +QTEST_MAIN(X11LayoutParserTests) diff --git a/src/unittests/deskflow/X11LayoutParserTests.h b/src/unittests/deskflow/X11LayoutParserTests.h new file mode 100644 index 000000000..801718025 --- /dev/null +++ b/src/unittests/deskflow/X11LayoutParserTests.h @@ -0,0 +1,105 @@ +/* + * Deskflow -- mouse and keyboard sharing utility + * SPDX-FileCopyrightText: (C) 2025 Chris Rizzitello + * SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception + */ + +#include "base/Log.h" + +#include + +class X11LayoutParserTests : public QObject +{ + Q_OBJECT +private slots: + // Test are run in order top to bottom + void initTestCase(); + void xmlParse(); + void convertLayouts(); + +private: + Arch m_arch; + Log m_log; + const QString kTestDir = "tmp/test"; + const QString kTestCorrectFile = "tmp/test/correctEvdev.xml"; + const QString kTestFutureFile = "tmp/test/evdevFromFuture.xml"; + const QString kTestBadFile1 = "tmp/test/evdevBad1.xml"; + const QString kTestBadFile2 = "tmp/test/evdevBad2.xml"; + const QString kTestBadFile3 = "tmp/test/evdevBad3.xml"; + + const QString kCorrectEvContents = QStringLiteral( + "\n" + "\n" + " \n" + " \n" + " \n" + " us\n" + " \n" + " en\n" + " English (US)\n" + " \n" + " eng\n" + " \n" + " \n" + " \n" + " \n" + " \n" + " eng\n" + " eng\n" + " Cherokee\n" + " \n" + " eng\n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " ru\n" + " \n" + " ru\n" + " Russian\n" + " \n" + " rus\n" + " \n" + " \n" + " \n" + " \n" + "\n" + ); + + const QString kFutureEvContents = QStringLiteral( + "\n" + "\n" + " \n" + " \n" + " \n" + " futureLangName\n" + " \n" + " fln\n" + " \n" + " \n" + " \n" + " \n" + "\n" + ); + + const QString kBadEv1Contents = QStringLiteral("\n"); + + const QString kBadEv2Contents = QStringLiteral( + "\n" + "\n" + "" + ); + + const QString kBadEv3Contents = QStringLiteral( + "\n" + "\n" + " \n" + " \n" + " \n" + " \n" + "\n" + ); +};