From 4b731207dfb42a499d6a061adbbb13cd58d817a9 Mon Sep 17 00:00:00 2001 From: Ulf Hermann Date: Tue, 15 Aug 2017 17:23:35 +0200 Subject: Tests: Unify debugger tests' connection mechanisms All the tests do pretty much the same thing in their init() or connect() methods: Create a process, create a debug connection, create some clients and make sure they're all running. We can deduplicate the code by moving all this into a common base class. Furthermore, the QSKIP in the qqmlenginedebuginspectorintegration test was obviously done because the BLACKLIST mechanism doesn't cover failures from internally called methods. As we don't have that problem anymore now, we can use BLACKLIST instead. Change-Id: I6d45d3b4e9645558ecc783a81fd740b00235cdc1 Reviewed-by: Simon Hausmann --- tests/auto/qml/debugger/shared/debugutil.cpp | 112 +++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) (limited to 'tests/auto/qml/debugger/shared/debugutil.cpp') diff --git a/tests/auto/qml/debugger/shared/debugutil.cpp b/tests/auto/qml/debugger/shared/debugutil.cpp index 7b9e935678..d3bb25ee89 100644 --- a/tests/auto/qml/debugger/shared/debugutil.cpp +++ b/tests/auto/qml/debugger/shared/debugutil.cpp @@ -302,3 +302,115 @@ void QQmlDebugProcess::processError(QProcess::ProcessError error) m_eventLoop.exit(); } + +template +struct Finalizer { + F m_lambda; + Finalizer(F &&lambda) : m_lambda(std::forward(lambda)) {} + ~Finalizer() { m_lambda(); } +}; + +template +static Finalizer defer(F &&lambda) +{ + return Finalizer(std::forward(lambda)); +} + +QQmlDebugTest::ConnectResult QQmlDebugTest::connect( + const QString &executable, const QString &services, const QString &extraArgs, + bool block) +{ + QStringList arguments; + arguments << QString::fromLatin1("-qmljsdebugger=port:13773,13783%3%4") + .arg(block ? QStringLiteral(",block") : QString()) + .arg(services.isEmpty() ? services : (QStringLiteral(",services:") + services)) + << extraArgs; + + m_process = createProcess(executable); + if (!m_process) + return ProcessFailed; + + m_process->start(QStringList() << arguments); + if (!m_process->waitForSessionStart()) + return SessionFailed; + + m_connection = createConnection(); + if (!m_connection) + return ConnectionFailed; + + m_clients = createClients(); + if (m_clients.contains(nullptr)) + return ClientsFailed; + + auto allEnabled = [this]() { + for (QQmlDebugClient *client : m_clients) { + if (client->state() != QQmlDebugClient::Enabled) + return false; + } + return true; + }; + + QList others = createOtherClients(m_connection); + auto deleter = defer([&others]() { qDeleteAll(others); }); + Q_UNUSED(deleter); + + const int port = m_process->debugPort(); + m_connection->connectToHost(QLatin1String("127.0.0.1"), port); + for (int tries = 0; tries < 100 && !allEnabled(); ++tries) + QTest::qWait(50); + if (!allEnabled()) + return EnableFailed; + + const QQmlDebugClient::State expectedState = services.isEmpty() ? QQmlDebugClient::Enabled + : QQmlDebugClient::Unavailable; + for (QQmlDebugClient *other : others) { + if (other->state() != expectedState) + return RestrictFailed; + } + + return ConnectSuccess; +} + +QList QQmlDebugTest::createClients() +{ + return QList(); +} + +QQmlDebugProcess *QQmlDebugTest::createProcess(const QString &executable) +{ + return new QQmlDebugProcess(executable, this); +} + +QQmlDebugConnection *QQmlDebugTest::createConnection() +{ + return new QQmlDebugConnection(this); +} + +void QQmlDebugTest::cleanup() +{ + if (QTest::currentTestFailed()) { + const QString null = QStringLiteral("null"); + + qDebug() << "Process State:" << (m_process ? m_process->state() : null); + qDebug() << "Application Output:" << (m_process ? m_process->output() : null); + qDebug() << "Connection State:" << QQmlDebugTest::connectionStateString(m_connection); + for (QQmlDebugClient *client : m_clients) { + if (client) + qDebug() << client->name() << "State:" << QQmlDebugTest::clientStateString(client); + else + qDebug() << "Failed Client:" << null; + } + } + + qDeleteAll(m_clients); + m_clients.clear(); + + delete m_connection; + m_connection = nullptr; + + if (m_process) { + m_process->stop(); + delete m_process; + m_process = nullptr; + } +} -- cgit v1.2.3