Skip to content

Commit

Permalink
Add name length test
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaski authored and nicolasfella committed Dec 28, 2024
1 parent 6c7f05d commit 20103dd
Show file tree
Hide file tree
Showing 6 changed files with 207 additions and 0 deletions.
1 change: 1 addition & 0 deletions tests/auto/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
#
add_subdirectory(simpletest)
add_subdirectory(stresstest)
add_subdirectory(namelengthtest)
10 changes: 10 additions & 0 deletions tests/auto/namelengthtest/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# This file is part of KDSingleApplication.
#
# SPDX-FileCopyrightText: 2020 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]>
#
# SPDX-License-Identifier: MIT
#
# Contact KDAB at <[email protected]> for commercial licensing options.
#
add_subdirectory(namelengthtest)
add_subdirectory(test)
16 changes: 16 additions & 0 deletions tests/auto/namelengthtest/namelengthtest/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# This file is part of KDSingleApplication.
#
# SPDX-FileCopyrightText: 2020 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]>
#
# SPDX-License-Identifier: MIT
#
# Contact KDAB at <[email protected]> for commercial licensing options.
#
set(namelengthtest_SRCS main.cpp)
add_executable(
namelengthtest
${namelengthtest_SRCS}
)
target_link_libraries(
namelengthtest Qt::Widgets kdsingleapplication
)
59 changes: 59 additions & 0 deletions tests/auto/namelengthtest/namelengthtest/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
This file is part of KDSingleApplication.
SPDX-FileCopyrightText: 2019 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]>
SPDX-License-Identifier: MIT
Contact KDAB at <[email protected]> for commercial licensing options.
*/

#include <QtCore/QCoreApplication>
#include <QtCore/QStringList>
#include <QtCore/QString>
#include <QtCore/QTimer>

#include <kdsingleapplication.h>

#include <iostream>

#if defined(Q_OS_WIN)
#include <io.h>
#include <fcntl.h>
#endif

int main(int argc, char **argv)
{
#if defined(Q_OS_WIN)
_setmode(_fileno(stdout), _O_BINARY);
#endif

QCoreApplication app(argc, argv);

const QString appName = QLatin1String("namelengthtest-") + app.arguments().value(1);

KDSingleApplication kdsa(QStringLiteral("a").repeated(9000));

if (kdsa.isPrimaryInstance()) {
std::cout << "Primary" << std::endl;

QObject::connect(&kdsa, &KDSingleApplication::messageReceived, qApp,
[](const QByteArray &message) {
std::cout << "MESSAGE: >" << message.constData() << '<' << std::endl;
qApp->quit();
});

QTimer::singleShot(5000, qApp, []() { qApp->exit(1); });

return app.exec();
} else {
std::cout << "Secondary" << std::endl;

if (!kdsa.sendMessage(app.arguments().value(2).toUtf8())) {
std::cerr << "Unable to send message to the primary!" << std::endl;
return 1;
}
}

return 0;
}
19 changes: 19 additions & 0 deletions tests/auto/namelengthtest/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# This file is part of KDSingleApplication.
#
# SPDX-FileCopyrightText: 2020 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]>
#
# SPDX-License-Identifier: MIT
#
# Contact KDAB at <[email protected]> for commercial licensing options.
#
include_directories(${CMAKE_SOURCE_DIR}/src)
include_directories(${CMAKE_CURRENT_BINARY_DIR})

add_executable(
tst_namelengthtest
../tst_namelengthtest.cpp
)
target_link_libraries(
tst_namelengthtest Qt::Test
)
add_test(NAME tst_namelengthtest COMMAND tst_namelengthtest)
102 changes: 102 additions & 0 deletions tests/auto/namelengthtest/tst_namelengthtest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
This file is part of KDSingleApplication.
SPDX-FileCopyrightText: 2019 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]>
SPDX-License-Identifier: MIT
Contact KDAB at <[email protected]> for commercial licensing options.
*/

#include <QtCore/QProcess>
#include <QtCore/QRandomGenerator>
#include <QtTest/QTest>


#include <vector>
#include <memory>

// runs a primary, runs a secondary, checks that a message is received.

class tst_NameLengthTest : public QObject
{
Q_OBJECT
private Q_SLOTS:
void testNameLength_data();
void testNameLength();
};

void tst_NameLengthTest::testNameLength_data()
{
QTest::addColumn<QString>("message");

QTest::addRow("message-1") << QStringLiteral("Hello");
QTest::addRow("message-2") << QStringLiteral("Hello World");
QTest::addRow("message-3") << QStringLiteral("Hello World 123456789");
// QTest::addRow("message empty") << QString();

for (int i = 1; i <= 2048; i *= 2) {
QTest::addRow("message-x-%d", i) << QString(i, QLatin1Char('x'));
}
}

void tst_NameLengthTest::testNameLength()
{
QFETCH(QString, message);
#ifdef KDSINGLEAPPLICATION_BINARY_DIR
const QString executable = QStringLiteral(KDSINGLEAPPLICATION_BINARY_DIR "namelengthtest");
#else
const QString executable = QStringLiteral("namelengthtest/namelengthtest");
#endif
QByteArray output;
bool ok;

const QString testId = QString::number(QRandomGenerator::global()->generate());

QProcess primary;
primary.setProcessChannelMode(QProcess::ForwardedErrorChannel);
primary.start(executable, { testId });
QVERIFY(primary.waitForStarted());
QCOMPARE(primary.state(), QProcess::Running);
output.clear();
ok = QTest::qWaitFor([&]() {
output += primary.readAllStandardOutput();
return output == "Primary\n";
});
QVERIFY(ok);

QProcess secondary;
secondary.setProcessChannelMode(QProcess::ForwardedErrorChannel);
secondary.start(executable, { testId, message });
QVERIFY(secondary.waitForStarted());
QCOMPARE(secondary.state(), QProcess::Running);

output.clear();
ok = QTest::qWaitFor([&]() {
output += secondary.readAllStandardOutput();
return output == "Secondary\n";
});
QVERIFY(ok);

if (secondary.state() != QProcess::NotRunning)
QVERIFY(secondary.waitForFinished());
QCOMPARE(secondary.exitCode(), 0);
QCOMPARE(secondary.exitStatus(), QProcess::NormalExit);

output.clear();
const QByteArray expected = "MESSAGE: >" + message.toUtf8() + "<\n";
ok = QTest::qWaitFor([&]() {
output += primary.readAllStandardOutput();
return output == expected;
});
QVERIFY(ok);

if (primary.state() != QProcess::NotRunning)
QVERIFY(primary.waitForFinished());
QCOMPARE(primary.exitCode(), 0);
QCOMPARE(primary.exitStatus(), QProcess::NormalExit);
}

QTEST_MAIN(tst_NameLengthTest)

#include "tst_namelengthtest.moc"

0 comments on commit 20103dd

Please sign in to comment.