1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
|
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include "debugutil_p.h"
#include "qqmldebugprocess_p.h"
#include <private/qqmldebugconnection_p.h>
#include <QtCore/qtimer.h>
#include <QtTest/qtest.h>
#include <QtTest/qsignalspy.h>
QQmlDebugTest::QQmlDebugTest(const char *qmlTestDataDir, FailOnWarningsPolicy failOnWarningsPolicy)
: QQmlDataTest(qmlTestDataDir, failOnWarningsPolicy)
{
}
bool QQmlDebugTest::waitForSignal(QObject *sender, const char *member, int timeout)
{
QSignalSpy spy(sender, member);
// Do not use spy.wait(). We want to avoid nested event loops.
if (QTest::qWaitFor([&]() { return spy.size() > 0; }, timeout))
return true;
qWarning("waitForSignal %s timed out after %d ms", member, timeout);
return false;
}
QList<QQmlDebugClient *> QQmlDebugTest::createOtherClients(QQmlDebugConnection *connection)
{
QList<QQmlDebugClient *> ret;
static const auto debuggerServices
= QStringList({"V8Debugger", "QmlDebugger", "DebugMessages"});
static const auto inspectorServices
= QStringList({"QmlInspector"});
static const auto profilerServices
= QStringList({"CanvasFrameRate", "EngineControl", "DebugMessages"});
for (const QString &service : debuggerServices) {
if (!connection->client(service))
ret << new QQmlDebugClient(service, connection);
}
for (const QString &service : inspectorServices) {
if (!connection->client(service))
ret << new QQmlDebugClient(service, connection);
}
for (const QString &service : profilerServices) {
if (!connection->client(service))
ret << new QQmlDebugClient(service, connection);
}
return ret;
}
QString QQmlDebugTest::clientStateString(const QQmlDebugClient *client)
{
if (!client)
return QLatin1String("null");
switch (client->state()) {
case QQmlDebugClient::NotConnected: return QLatin1String("Not connected");
case QQmlDebugClient::Unavailable: return QLatin1String("Unavailable");
case QQmlDebugClient::Enabled: return QLatin1String("Enabled");
default: return QLatin1String("Invalid");
}
}
QString QQmlDebugTest::connectionStateString(const QQmlDebugConnection *connection)
{
if (!connection)
return QLatin1String("null");
return connection->isConnected() ? QLatin1String("connected") : QLatin1String("not connected");
}
QQmlDebugTestClient::QQmlDebugTestClient(const QString &s, QQmlDebugConnection *c)
: QQmlDebugClient(s, c)
{
connect(this, &QQmlDebugClient::stateChanged, this, [this](QQmlDebugClient::State newState) {
QCOMPARE(newState, state());
});
}
QByteArray QQmlDebugTestClient::waitForResponse()
{
lastMsg.clear();
QQmlDebugTest::waitForSignal(this, SIGNAL(serverMessage(QByteArray)));
if (lastMsg.isEmpty()) {
qWarning() << "no response from server!";
return QByteArray();
}
return lastMsg;
}
void QQmlDebugTestClient::messageReceived(const QByteArray &ba)
{
lastMsg = ba;
emit serverMessage(ba);
}
QQmlDebugTest::ConnectResult QQmlDebugTest::connectTo(
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;
ClientStateHandler stateHandler(m_clients, createOtherClients(m_connection), services.isEmpty()
? QQmlDebugClient::Enabled : QQmlDebugClient::Unavailable);
QSignalSpy okSpy(&stateHandler, &ClientStateHandler::allOk);
QSignalSpy disconnectSpy(m_connection, &QQmlDebugConnection::disconnected);
m_connection->connectToHost(QLatin1String("127.0.0.1"), m_process->debugPort());
if (!QTest::qWaitFor([&](){ return okSpy.size() > 0 || disconnectSpy.size() > 0; }, 5000))
return ConnectionTimeout;
if (!stateHandler.allEnabled())
return EnableFailed;
if (!stateHandler.othersAsExpected())
return RestrictFailed;
return ConnectSuccess;
}
QList<QQmlDebugClient *> QQmlDebugTest::createClients()
{
return QList<QQmlDebugClient *>();
}
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->stateString() : 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;
}
}
ClientStateHandler::ClientStateHandler(const QList<QQmlDebugClient *> &clients,
const QList<QQmlDebugClient *> &others,
QQmlDebugClient::State expectedOthers) :
m_clients(clients), m_others(others), m_expectedOthers(expectedOthers)
{
for (QQmlDebugClient *client : m_clients) {
QObject::connect(client, &QQmlDebugClient::stateChanged,
this, &ClientStateHandler::checkStates);
}
for (QQmlDebugClient *client : m_others) {
QObject::connect(client, &QQmlDebugClient::stateChanged,
this, &ClientStateHandler::checkStates);
}
}
ClientStateHandler::~ClientStateHandler()
{
qDeleteAll(m_others);
}
void ClientStateHandler::checkStates()
{
for (QQmlDebugClient *client : m_clients) {
if (client->state() != QQmlDebugClient::Enabled)
return;
}
m_allEnabled = true;
for (QQmlDebugClient *other : m_others) {
if (other->state() != m_expectedOthers)
return;
}
m_othersAsExpected = true;
emit allOk();
}
QString debugJsServerPath(const QString &selfPath)
{
static const char *debugserver = "qqmldebugjsserver";
QString appPath = QCoreApplication::applicationDirPath();
const int position = appPath.lastIndexOf(selfPath);
return (position == -1 ? appPath : appPath.replace(position, selfPath.size(), debugserver))
+ "/" + debugserver;
}
#include <moc_debugutil_p.cpp>
|