refactor: port X11LayoutParserTests to QtTests

This commit is contained in:
sithlord48 2025-04-05 14:33:29 -04:00 committed by Nick Bolton
parent baaa02cda2
commit 7fa326234e
4 changed files with 199 additions and 149 deletions

View file

@ -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 <fstream>
#include <gtest/gtest.h>
const std::string testDir = "tmp/test";
void createTestFiles()
{
std::ofstream correctEvdevFile(testDir + "/correctEvdev.xml");
if (!correctEvdevFile.is_open()) {
FAIL();
}
correctEvdevFile << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" << std::endl;
correctEvdevFile << "<xkbConfigRegistry version=\"1.1\">" << std::endl;
correctEvdevFile << " <layoutList>" << std::endl;
correctEvdevFile << " <layout>" << std::endl;
correctEvdevFile << " <configItem>" << std::endl;
correctEvdevFile << " <name>us</name>" << std::endl;
correctEvdevFile << " <!-- Keyboard indicator for English layouts -->" << std::endl;
correctEvdevFile << " <shortDescription>en</shortDescription>" << std::endl;
correctEvdevFile << " <description>English (US)</description>" << std::endl;
correctEvdevFile << " <languageList>" << std::endl;
correctEvdevFile << " <iso639Id>eng</iso639Id>" << std::endl;
correctEvdevFile << " </languageList>" << std::endl;
correctEvdevFile << " </configItem>" << std::endl;
correctEvdevFile << " <variantList>" << std::endl;
correctEvdevFile << " <variant>" << std::endl;
correctEvdevFile << " <configItem>" << std::endl;
correctEvdevFile << " <name>eng</name>" << std::endl;
correctEvdevFile << " <shortDescription>eng</shortDescription>" << std::endl;
correctEvdevFile << " <description>Cherokee</description>" << std::endl;
correctEvdevFile << " <languageList>" << std::endl;
correctEvdevFile << " <iso639Id>eng</iso639Id>" << std::endl;
correctEvdevFile << " </languageList>" << std::endl;
correctEvdevFile << " </configItem>" << std::endl;
correctEvdevFile << " </variant>" << std::endl;
correctEvdevFile << " </variantList>" << std::endl;
correctEvdevFile << " </layout>" << std::endl;
correctEvdevFile << " <layout>" << std::endl;
correctEvdevFile << " <configItem>" << std::endl;
correctEvdevFile << " <name>ru</name>" << std::endl;
correctEvdevFile << " <!-- Keyboard indicator for Russian layouts -->" << std::endl;
correctEvdevFile << " <shortDescription>ru</shortDescription>" << std::endl;
correctEvdevFile << " <description>Russian</description>" << std::endl;
correctEvdevFile << " <languageList>" << std::endl;
correctEvdevFile << " <iso639Id>rus</iso639Id>" << std::endl;
correctEvdevFile << " </languageList>" << std::endl;
correctEvdevFile << " </configItem>" << std::endl;
correctEvdevFile << " </layout>" << std::endl;
correctEvdevFile << " </layoutList>" << std::endl;
correctEvdevFile << "</xkbConfigRegistry>" << std::endl;
correctEvdevFile.close();
std::ofstream evdevFromFutureFile(testDir + "/evdevFromFuture.xml");
if (!evdevFromFutureFile.is_open()) {
FAIL();
}
evdevFromFutureFile << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" << std::endl;
evdevFromFutureFile << "<xkbConfigRegistry version=\"1.1\">" << std::endl;
evdevFromFutureFile << " <layoutList>" << std::endl;
evdevFromFutureFile << " <layout>" << std::endl;
evdevFromFutureFile << " <configItem>" << std::endl;
evdevFromFutureFile << " <name>futureLangName</name>" << std::endl;
evdevFromFutureFile << " <languageList>" << std::endl;
evdevFromFutureFile << " <iso639Id>fln</iso639Id>" << std::endl;
evdevFromFutureFile << " </languageList>" << std::endl;
evdevFromFutureFile << " </configItem>" << std::endl;
evdevFromFutureFile << " </layout>" << std::endl;
evdevFromFutureFile << " </layoutList>" << std::endl;
evdevFromFutureFile << "</xkbConfigRegistry>" << std::endl;
evdevFromFutureFile.close();
std::ofstream incorrectEvdevFile1(testDir + "/incorrectEvdev1.xml");
if (!incorrectEvdevFile1.is_open()) {
FAIL();
}
incorrectEvdevFile1 << "<incorrectRootTag></incorrectRootTag>" << std::endl;
incorrectEvdevFile1.close();
std::ofstream incorrectEvdevFile2(testDir + "/incorrectEvdev2.xml");
if (!incorrectEvdevFile2.is_open()) {
FAIL();
}
incorrectEvdevFile2 << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" << std::endl;
incorrectEvdevFile2 << "<xkbConfigRegistry version=\"1.1\">" << std::endl;
incorrectEvdevFile2 << "</xkbConfigRegistry>" << std::endl;
incorrectEvdevFile2.close();
std::ofstream incorrectEvdevFile3(testDir + "/incorrectEvdev3.xml");
if (!incorrectEvdevFile3.is_open()) {
FAIL();
}
incorrectEvdevFile3 << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" << std::endl;
incorrectEvdevFile3 << "<xkbConfigRegistry version=\"1.1\">" << std::endl;
incorrectEvdevFile3 << " <layoutList>" << std::endl;
incorrectEvdevFile3 << " <layout>" << std::endl;
incorrectEvdevFile3 << " </layout>" << std::endl;
incorrectEvdevFile3 << " </layoutList>" << std::endl;
incorrectEvdevFile3 << "</xkbConfigRegistry>" << std::endl;
incorrectEvdevFile3.close();
}
TEST(X11LayoutsParsingTests, xmlCorrectParsingTest)
{
createTestFiles();
std::vector<std::string> 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<std::string> 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

View file

@ -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()

View file

@ -0,0 +1,85 @@
/*
* Deskflow -- mouse and keyboard sharing utility
* SPDX-FileCopyrightText: (C) 2025 Chris Rizzitello <sithlord48@gmail.com>
* 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)

View file

@ -0,0 +1,105 @@
/*
* 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 "base/Log.h"
#include <QTest>
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(
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
"<xkbConfigRegistry version=\"1.1\">\n"
" <layoutList>\n"
" <layout>\n"
" <configItem>\n"
" <name>us</name>\n"
" <!-- Keyboard indicator for English layouts -->\n"
" <shortDescription>en</shortDescription>\n"
" <description>English (US)</description>\n"
" <languageList>\n"
" <iso639Id>eng</iso639Id>\n"
" </languageList>\n"
" </configItem>\n"
" <variantList>\n"
" <variant>\n"
" <configItem>\n"
" <name>eng</name>\n"
" <shortDescription>eng</shortDescription>\n"
" <description>Cherokee</description>\n"
" <languageList>\n"
" <iso639Id>eng</iso639Id>\n"
" </languageList>\n"
" </configItem>\n"
" </variant>\n"
" </variantList>\n"
" </layout>\n"
" <layout>\n"
" <configItem>\n"
" <name>ru</name>\n"
" <!-- Keyboard indicator for Russian layouts -->\n"
" <shortDescription>ru</shortDescription>\n"
" <description>Russian</description>\n"
" <languageList>\n"
" <iso639Id>rus</iso639Id>\n"
" </languageList>\n"
" </configItem>\n"
" </layout>\n"
" </layoutList>\n"
"</xkbConfigRegistry>\n"
);
const QString kFutureEvContents = QStringLiteral(
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
"<xkbConfigRegistry version=\"1.1\">\n"
" <layoutList>\n"
" <layout>\n"
" <configItem>\n"
" <name>futureLangName</name>\n"
" <languageList>\n"
" <iso639Id>fln</iso639Id>\n"
" </languageList>\n"
" </configItem>\n"
" </layout>\n"
" </layoutList>\n"
"</xkbConfigRegistry>\n"
);
const QString kBadEv1Contents = QStringLiteral("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
const QString kBadEv2Contents = QStringLiteral(
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
"<xkbConfigRegistry version=\"1.1\">\n"
"</xkbConfigRegistry>"
);
const QString kBadEv3Contents = QStringLiteral(
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
"<xkbConfigRegistry version=\"1.1\">\n"
" <layoutList>\n"
" <layout>\n"
" </layout>\n"
" </layoutList>\n"
"</xkbConfigRegistry>\n"
);
};