diff options
author | Ulf Hermann <[email protected]> | 2014-12-02 15:57:06 +0100 |
---|---|---|
committer | Simon Hausmann <[email protected]> | 2015-06-26 14:13:46 +0000 |
commit | f23d4fafbff44bcb6fb1e259ca1021a4c4326084 (patch) | |
tree | 8fd6c26460d3dd9f268e64a92ad6e6ec4b375c56 | |
parent | 49d3cbfa171902ae2dc61bcdadfdac1305c50a2a (diff) |
Add option to use a local socket for QML debugging
Using a TCP debug server comes with a number of drawbacks. It has a
larger overhead than other connection types, the application has to
be able to access the network and there has to be an open port we can
find somehow.
Change-Id: Ia7fb24006b89419988c6504797303d84c3aa1bbc
Reviewed-by: Simon Hausmann <[email protected]>
18 files changed, 647 insertions, 43 deletions
diff --git a/src/plugins/qmltooling/qmldbg_local/qlocalclientconnection.cpp b/src/plugins/qmltooling/qmldbg_local/qlocalclientconnection.cpp new file mode 100644 index 0000000000..344700b8fd --- /dev/null +++ b/src/plugins/qmltooling/qmldbg_local/qlocalclientconnection.cpp @@ -0,0 +1,188 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the QtQml module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL21$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 or version 3 as published by the Free +** Software Foundation and appearing in the file LICENSE.LGPLv21 and +** LICENSE.LGPLv3 included in the packaging of this file. Please review the +** following information to ensure the GNU Lesser General Public License +** requirements will be met: https://www.gnu.org/licenses/lgpl.html and +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** As a special exception, The Qt Company gives you certain additional +** rights. These rights are described in The Qt Company LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qlocalclientconnection.h" +#include "qpacketprotocol.h" + +#include <QtCore/qplugin.h> +#include <QtNetwork/qlocalsocket.h> + +#include <private/qqmldebugserver_p.h> + +QT_BEGIN_NAMESPACE + +class QLocalClientConnectionPrivate { +public: + QLocalClientConnectionPrivate(); + + bool block; + QString filename; + QLocalSocket *socket; + QPacketProtocol *protocol; + QQmlDebugServer *debugServer; +}; + +QLocalClientConnectionPrivate::QLocalClientConnectionPrivate() : + block(false), + socket(0), + protocol(0), + debugServer(0) +{ +} + +QLocalClientConnection::QLocalClientConnection() : + d_ptr(new QLocalClientConnectionPrivate) +{ +} + +QLocalClientConnection::~QLocalClientConnection() +{ + if (isConnected()) + disconnect(); + delete d_ptr; +} + +void QLocalClientConnection::setServer(QQmlDebugServer *server) +{ + Q_D(QLocalClientConnection); + d->debugServer = server; +} + +bool QLocalClientConnection::isConnected() const +{ + Q_D(const QLocalClientConnection); + return d->socket && d->socket->state() == QLocalSocket::ConnectedState; +} + +void QLocalClientConnection::send(const QList<QByteArray> &messages) +{ + Q_D(QLocalClientConnection); + + if (!isConnected() || !d->protocol || !d->socket) + return; + + foreach (const QByteArray &message, messages) { + QPacket pack; + pack.writeRawData(message.data(), message.length()); + d->protocol->send(pack); + } + d->socket->flush(); +} + +void QLocalClientConnection::disconnect() +{ + Q_D(QLocalClientConnection); + + while (d->socket && d->socket->bytesToWrite() > 0) + d->socket->waitForBytesWritten(); + + // protocol might still be processing packages at this point + d->protocol->deleteLater(); + d->protocol = 0; + d->socket->deleteLater(); + d->socket = 0; +} + +bool QLocalClientConnection::waitForMessage() +{ + Q_D(QLocalClientConnection); + return d->protocol->waitForReadyRead(-1); +} + +bool QLocalClientConnection::setPortRange(int portFrom, int portTo, bool block, + const QString &hostaddress) +{ + Q_UNUSED(portFrom); + Q_UNUSED(portTo); + Q_UNUSED(block); + Q_UNUSED(hostaddress); + return false; +} + +bool QLocalClientConnection::setFileName(const QString &filename, bool block) +{ + Q_D(QLocalClientConnection); + d->filename = filename; + d->block = block; + return connect(); +} + +void QLocalClientConnection::waitForConnection() +{ + Q_D(QLocalClientConnection); + d->socket->waitForConnected(-1); +} + +bool QLocalClientConnection::connect() +{ + Q_D(QLocalClientConnection); + + d->socket = new QLocalSocket; + d->socket->setParent(this); + QObject::connect(d->socket, SIGNAL(connected()), this, SLOT(connectionEstablished())); + d->socket->connectToServer(d->filename); + qDebug("QML Debugger: Connecting to socket %s...", + d->filename.toLatin1().constData()); + return true; +} + +void QLocalClientConnection::readyRead() +{ + Q_D(QLocalClientConnection); + if (!d->protocol) + return; + + QPacket packet = d->protocol->read(); + + QByteArray content = packet.data(); + d->debugServer->receiveMessage(content); +} + +void QLocalClientConnection::connectionEstablished() +{ + Q_D(QLocalClientConnection); + + d->protocol = new QPacketProtocol(d->socket, this); + QObject::connect(d->protocol, SIGNAL(readyRead()), this, SLOT(readyRead())); + QObject::connect(d->protocol, SIGNAL(invalidPacket()), this, SLOT(invalidPacket())); + + if (d->block) + d->protocol->waitForReadyRead(-1); +} + +void QLocalClientConnection::invalidPacket() +{ + qWarning("QML Debugger: Received a corrupted packet! Giving up ..."); +} + +QT_END_NAMESPACE diff --git a/src/plugins/qmltooling/qmldbg_local/qlocalclientconnection.h b/src/plugins/qmltooling/qmldbg_local/qlocalclientconnection.h new file mode 100644 index 0000000000..424337900b --- /dev/null +++ b/src/plugins/qmltooling/qmldbg_local/qlocalclientconnection.h @@ -0,0 +1,78 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the QtQml module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL21$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 or version 3 as published by the Free +** Software Foundation and appearing in the file LICENSE.LGPLv21 and +** LICENSE.LGPLv3 included in the packaging of this file. Please review the +** following information to ensure the GNU Lesser General Public License +** requirements will be met: https://www.gnu.org/licenses/lgpl.html and +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** As a special exception, The Qt Company gives you certain additional +** rights. These rights are described in The Qt Company LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QLOCALCLIENTCONNECTION_H +#define QLOCALCLIENTCONNECTION_H + +#include <private/qqmldebugserverconnection_p.h> + +QT_BEGIN_NAMESPACE + +class QQmlDebugServer; +class QLocalClientConnectionPrivate; +class QLocalClientConnection : public QObject, public QQmlDebugServerConnection +{ + Q_OBJECT + Q_DECLARE_PRIVATE(QLocalClientConnection) + Q_DISABLE_COPY(QLocalClientConnection) + Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QQmlDebugServerConnection") + Q_INTERFACES(QQmlDebugServerConnection) + +public: + QLocalClientConnection(); + ~QLocalClientConnection(); + + void setServer(QQmlDebugServer *server); + bool setPortRange(int portFrom, int portTo, bool bock, const QString &hostaddress); + bool setFileName(const QString &filename, bool block); + + bool isConnected() const; + void send(const QList<QByteArray> &messages); + void disconnect(); + bool waitForMessage(); + + void waitForConnection(); + bool connect(); + +private Q_SLOTS: + void readyRead(); + void connectionEstablished(); + void invalidPacket(); + +private: + QLocalClientConnectionPrivate *d_ptr; +}; + +QT_END_NAMESPACE + +#endif // QLOCALCLIENTCONNECTION_H diff --git a/src/plugins/qmltooling/qmldbg_local/qmldbg_local.pri b/src/plugins/qmltooling/qmldbg_local/qmldbg_local.pri new file mode 100644 index 0000000000..7635523e9b --- /dev/null +++ b/src/plugins/qmltooling/qmldbg_local/qmldbg_local.pri @@ -0,0 +1,14 @@ +QT += core-private + +SOURCES += \ + $$PWD/qlocalclientconnection.cpp \ + $$PWD/../shared/qpacketprotocol.cpp + +HEADERS += \ + $$PWD/qlocalclientconnection.h \ + $$PWD/../shared/qpacketprotocol.h + +INCLUDEPATH += $$PWD \ + $$PWD/../shared + +OTHER_FILES += $$PWD/qlocalclientconnection.json diff --git a/src/plugins/qmltooling/qmldbg_local/qmldbg_local.pro b/src/plugins/qmltooling/qmldbg_local/qmldbg_local.pro new file mode 100644 index 0000000000..b060154933 --- /dev/null +++ b/src/plugins/qmltooling/qmldbg_local/qmldbg_local.pro @@ -0,0 +1,8 @@ +TARGET = qmldbg_local +QT = qml-private core-private + +PLUGIN_TYPE = qmltooling +PLUGIN_CLASS_NAME = QLocalClientConnection +load(qt_plugin) + +include(qmldbg_local.pri) diff --git a/src/plugins/qmltooling/qmldbg_tcp/qtcpserverconnection.cpp b/src/plugins/qmltooling/qmldbg_tcp/qtcpserverconnection.cpp index 2ae4edfce4..f7c0673c19 100644 --- a/src/plugins/qmltooling/qmldbg_tcp/qtcpserverconnection.cpp +++ b/src/plugins/qmltooling/qmldbg_tcp/qtcpserverconnection.cpp @@ -146,6 +146,13 @@ bool QTcpServerConnection::setPortRange(int portFrom, int portTo, bool block, return listen(); } +bool QTcpServerConnection::setFileName(const QString &fileName, bool block) +{ + Q_UNUSED(fileName); + Q_UNUSED(block); + return false; +} + void QTcpServerConnection::waitForConnection() { Q_D(QTcpServerConnection); diff --git a/src/plugins/qmltooling/qmldbg_tcp/qtcpserverconnection.h b/src/plugins/qmltooling/qmldbg_tcp/qtcpserverconnection.h index 942fb6e12c..496d328a11 100644 --- a/src/plugins/qmltooling/qmldbg_tcp/qtcpserverconnection.h +++ b/src/plugins/qmltooling/qmldbg_tcp/qtcpserverconnection.h @@ -54,6 +54,7 @@ public: void setServer(QQmlDebugServer *server); bool setPortRange(int portFrom, int portTo, bool bock, const QString &hostaddress); + bool setFileName(const QString &fileName, bool block); bool isConnected() const; void send(const QList<QByteArray> &messages); diff --git a/src/plugins/qmltooling/qmltooling.pro b/src/plugins/qmltooling/qmltooling.pro index 3bc48a6b33..585f80138b 100644 --- a/src/plugins/qmltooling/qmltooling.pro +++ b/src/plugins/qmltooling/qmltooling.pro @@ -1,4 +1,4 @@ TEMPLATE = subdirs -SUBDIRS = qmldbg_tcp +SUBDIRS = qmldbg_tcp qmldbg_local qtHaveModule(quick): SUBDIRS += qmldbg_qtquick2 diff --git a/src/qml/debugger/qqmldebug.h b/src/qml/debugger/qqmldebug.h index bb90c4f1b8..421c6968b9 100644 --- a/src/qml/debugger/qqmldebug.h +++ b/src/qml/debugger/qqmldebug.h @@ -45,6 +45,7 @@ struct Q_QML_EXPORT QQmlDebuggingEnabler QQmlDebuggingEnabler(bool printWarning = true); static bool startTcpDebugServer(int port, bool block = false, const QString &hostName = QString()); + static bool connectToLocalDebugger(const QString &socketFileName, bool block = false); }; // Execute code in constructor before first QQmlEngine is instantiated diff --git a/src/qml/debugger/qqmldebugserver.cpp b/src/qml/debugger/qqmldebugserver.cpp index 9985efcf12..7323d3bf46 100644 --- a/src/qml/debugger/qqmldebugserver.cpp +++ b/src/qml/debugger/qqmldebugserver.cpp @@ -106,8 +106,11 @@ class QQmlDebugServerPrivate : public QObjectPrivate public: QQmlDebugServerPrivate(); - bool start(int portFrom, int portTo, bool block, const QString &hostAddress, - const QString &pluginName); + bool init(const QString &pluginName, bool block); + + template<class Action> + static bool enable(Action action); + void advertisePlugins(); void cleanup(); QQmlDebugServerConnection *loadConnectionPlugin(const QString &pluginName); @@ -168,6 +171,12 @@ public: m_hostAddress = hostAddress; } + void setFileName(const QString &fileName, bool block) + { + m_fileName = fileName; + m_block = block; + } + void run(); private: @@ -176,6 +185,7 @@ private: int m_portTo; bool m_block; QString m_hostAddress; + QString m_fileName; }; QQmlDebugServerPrivate::QQmlDebugServerPrivate() : @@ -310,10 +320,19 @@ void QQmlDebugServerThread::run() #endif if (connection) { connection->setServer(server); - if (!connection->setPortRange(m_portFrom, m_portTo, m_block, m_hostAddress)) { - delete connection; - return; + + if (m_fileName.isEmpty()) { + if (!connection->setPortRange(m_portFrom, m_portTo, m_block, m_hostAddress)) { + delete connection; + return; + } + } else { + if (!connection->setFileName(m_fileName, m_block)) { + delete connection; + return; + } } + server->d_func()->connection = connection; if (m_block) connection->waitForConnection(); @@ -377,8 +396,7 @@ static void cleanupOnShutdown() wrapper->cleanup(); } -bool QQmlDebugServerPrivate::start(int portFrom, int portTo, bool block, const QString &hostAddress, - const QString &pluginName) +bool QQmlDebugServerPrivate::init(const QString &pluginName, bool block) { if (!QQmlEnginePrivate::qml_debugging_enabled) return false; @@ -399,9 +417,7 @@ bool QQmlDebugServerPrivate::start(int portFrom, int portTo, bool block, const Q thread->setObjectName(QStringLiteral("QQmlDebugServerThread")); thread->setPluginName(pluginName); - thread->setPortRange(portFrom, portTo == -1 ? portFrom : portTo, block, hostAddress); blockingMode = block; - thread->start(); return true; } @@ -418,6 +434,7 @@ QQmlDebugServer::QQmlDebugServer() bool block = false; bool ok = false; QString hostAddress; + QString fileName; // format: qmljsdebugger=port:<port_from>[,port_to],host:<ip address>][,block] if (!appD->qmljsDebugArgumentsString().isEmpty()) { @@ -452,6 +469,10 @@ QQmlDebugServer::QQmlDebugServer() hostAddress = strArgument.mid(5); } else if (strArgument == QLatin1String("block")) { block = true; + } else if (strArgument.startsWith(QLatin1String("file:"))) { + pluginName = QLatin1String("qmldbg_local"); + fileName = strArgument.mid(5); + ok = !fileName.isEmpty(); } else { qWarning() << QString::fromLatin1("QML Debugger: Invalid argument '%1' " "detected. Ignoring the same.") @@ -461,7 +482,13 @@ QQmlDebugServer::QQmlDebugServer() if (ok) { Q_D(QQmlDebugServer); - d->start(portFrom, portTo, block, hostAddress, pluginName); + if (d->init(pluginName, block)) { + if (!fileName.isEmpty()) + d->thread->setFileName(fileName, block); + else + d->thread->setPortRange(portFrom, portTo, block, hostAddress); + d->thread->start(); + } } else { qWarning() << QString(QLatin1String( "QML Debugger: Ignoring \"-qmljsdebugger=%1\". " @@ -745,7 +772,44 @@ void QQmlDebugServer::sendMessages(QQmlDebugService *service, Q_ARG(QList<QByteArray>, prefixedMessages)); } -bool QQmlDebugServer::enable(int portFrom, int portTo, bool block, const QString &hostAddress) +struct StartTcpServerAction { + int portFrom; + int portTo; + bool block; + QString hostAddress; + + StartTcpServerAction(int portFrom, int portTo, bool block, const QString &hostAddress) : + portFrom(portFrom), portTo(portTo), block(block), hostAddress(hostAddress) {} + + bool operator()(QQmlDebugServerPrivate *d) + { + if (!d->init(QLatin1String("qmldbg_tcp"), block)) + return false; + d->thread->setPortRange(portFrom, portTo == -1 ? portFrom : portTo, block, hostAddress); + d->thread->start(); + return true; + } +}; + +struct ConnectToLocalAction { + QString fileName; + bool block; + + ConnectToLocalAction(const QString &fileName, bool block) : + fileName(fileName), block(block) {} + + bool operator()(QQmlDebugServerPrivate *d) + { + if (!d->init(QLatin1String("qmldbg_local"), block)) + return false; + d->thread->setFileName(fileName, block); + d->thread->start(); + return true; + } +}; + +template<class Action> +bool QQmlDebugServerPrivate::enable(Action action) { #ifndef QT_NO_QML_DEBUGGER QQmlDebugServerInstanceWrapper *wrapper = debugServerInstance(); @@ -754,7 +818,7 @@ bool QQmlDebugServer::enable(int portFrom, int portTo, bool block, const QString QQmlDebugServerPrivate *d = wrapper->m_instance.d_func(); if (d->thread) return false; - if (!d->start(portFrom, portTo, block, hostAddress, QLatin1String("qmldbg_tcp"))) + if (!action(d)) return false; while (!wrapper->m_instance.hasConnection()) { if (!wrapper->m_instance.hasThread()) @@ -770,6 +834,46 @@ bool QQmlDebugServer::enable(int portFrom, int portTo, bool block, const QString #endif } +/*! + * Enables debugging for QML engines created after calling this function. The debug server will + * listen on \a port at \a hostName and block the QML engine until it receives a connection if + * \a block is true. If \a block is not specified it won't block and if \a hostName isn't specified + * it will listen on all available interfaces. You can only start one debug server at a time. A + * debug server may have already been started if the -qmljsdebugger= command line argument was + * given. This method returns \c true if a new debug server was successfully started, or \c false + * otherwise. + */ +bool QQmlDebuggingEnabler::startTcpDebugServer(int port, bool block, const QString &hostName) +{ +#ifndef QQML_NO_DEBUG_PROTOCOL + return QQmlDebugServerPrivate::enable(StartTcpServerAction(port, port, block, hostName)); +#else + Q_UNUSED(port); + Q_UNUSED(block); + Q_UNUSED(hostName); + return false; +#endif +} + +/*! + * Enables debugging for QML engines created after calling this function. The debug server will + * connect to a debugger waiting on a local socket at the given \a socketFileName and block the QML + * engine until the connection is established if \a block is true. If \a block is not specified it + * won't block. You can only start one debug server at a time. A debug server may have already been + * started if the -qmljsdebugger= command line argument was given. This method returns \c true if a + * new debug server was successfully started, or \c false otherwise. + */ +bool QQmlDebuggingEnabler::connectToLocalDebugger(const QString &socketFileName, bool block) +{ +#ifndef QQML_NO_DEBUG_PROTOCOL + return QQmlDebugServerPrivate::enable(ConnectToLocalAction(socketFileName, block)); +#else + Q_UNUSED(fileName); + Q_UNUSED(block); + return false; +#endif +} + void QQmlDebugServer::wakeEngine(QQmlEngine *engine) { // to be executed in debugger thread diff --git a/src/qml/debugger/qqmldebugserver_p.h b/src/qml/debugger/qqmldebugserver_p.h index b2bd0b2463..f4026ca85f 100644 --- a/src/qml/debugger/qqmldebugserver_p.h +++ b/src/qml/debugger/qqmldebugserver_p.h @@ -79,7 +79,6 @@ public: void receiveMessage(const QByteArray &message); void sendMessages(QQmlDebugService *service, const QList<QByteArray> &messages); - static bool enable(int portFrom, int portTo, bool block, const QString &hostAddress); private slots: void wakeEngine(QQmlEngine *engine); diff --git a/src/qml/debugger/qqmldebugserverconnection_p.h b/src/qml/debugger/qqmldebugserverconnection_p.h index 9170238b46..cfdf85a9b6 100644 --- a/src/qml/debugger/qqmldebugserverconnection_p.h +++ b/src/qml/debugger/qqmldebugserverconnection_p.h @@ -60,6 +60,7 @@ public: virtual void setServer(QQmlDebugServer *server) = 0; virtual bool setPortRange(int portFrom, int portTo, bool bock, const QString &hostaddress) = 0; + virtual bool setFileName(const QString &fileName, bool block) = 0; virtual bool isConnected() const = 0; virtual void send(const QList<QByteArray> &messages) = 0; virtual void disconnect() = 0; diff --git a/src/qml/qml/qqmlengine.cpp b/src/qml/qml/qqmlengine.cpp index a866a33787..fdce590873 100644 --- a/src/qml/qml/qqmlengine.cpp +++ b/src/qml/qml/qqmlengine.cpp @@ -1501,27 +1501,6 @@ QQmlDebuggingEnabler::QQmlDebuggingEnabler(bool printWarning) #endif } -/*! - * Enables debugging for QML engines created after calling this function. The debug server will - * listen on \a port at \a hostName and block the QML engine until it receives a connection if - * \a block is true. If \a block is not specified it won't block and if \a hostName isn't specified - * it will listen on all available interfaces. You can only start one debug server at a time. A - * debug server may have already been started if the -qmljsdebugger= command line argument was - * given. This method returns \c true if a new debug server was successfully started, or \c false - * otherwise. - */ -bool QQmlDebuggingEnabler::startTcpDebugServer(int port, bool block, const QString &hostName) -{ -#ifndef QQML_NO_DEBUG_PROTOCOL - return QQmlDebugServer::enable(port, port, block, hostName); -#else - Q_UNUSED(port); - Q_UNUSED(block); - Q_UNUSED(hostName); - return false; -#endif -} - class QQmlDataExtended { public: QQmlDataExtended(); diff --git a/tests/auto/qml/debugger/debugger.pro b/tests/auto/qml/debugger/debugger.pro index fc1c0b537b..5a08418be1 100644 --- a/tests/auto/qml/debugger/debugger.pro +++ b/tests/auto/qml/debugger/debugger.pro @@ -12,6 +12,7 @@ PUBLICTESTS += \ PRIVATETESTS += \ qqmldebugclient \ + qqmldebuglocal \ qqmldebugservice SUBDIRS += $$PUBLICTESTS diff --git a/tests/auto/qml/debugger/qqmldebuglocal/qqmldebuglocal.pro b/tests/auto/qml/debugger/qqmldebuglocal/qqmldebuglocal.pro new file mode 100644 index 0000000000..b612da11de --- /dev/null +++ b/tests/auto/qml/debugger/qqmldebuglocal/qqmldebuglocal.pro @@ -0,0 +1,16 @@ +CONFIG += testcase +TARGET = tst_qqmldebuglocal +osx:CONFIG -= app_bundle + +HEADERS += ../shared/qqmldebugtestservice.h + +SOURCES += tst_qqmldebuglocal.cpp \ + ../shared/qqmldebugtestservice.cpp + +INCLUDEPATH += ../shared +include(../shared/debugutil.pri) + +CONFIG += parallel_test +QT += qml-private testlib gui-private core-private + +DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0 QT_QML_DEBUG_NO_WARNING diff --git a/tests/auto/qml/debugger/qqmldebuglocal/tst_qqmldebuglocal.cpp b/tests/auto/qml/debugger/qqmldebuglocal/tst_qqmldebuglocal.cpp new file mode 100644 index 0000000000..b1219eabe4 --- /dev/null +++ b/tests/auto/qml/debugger/qqmldebuglocal/tst_qqmldebuglocal.cpp @@ -0,0 +1,161 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL21$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 or version 3 as published by the Free +** Software Foundation and appearing in the file LICENSE.LGPLv21 and +** LICENSE.LGPLv3 included in the packaging of this file. Please review the +** following information to ensure the GNU Lesser General Public License +** requirements will be met: https://www.gnu.org/licenses/lgpl.html and +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** As a special exception, The Qt Company gives you certain additional +** rights. These rights are described in The Qt Company LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <qtest.h> +#include <QSignalSpy> +#include <QTimer> +#include <QHostAddress> +#include <QDebug> +#include <QThread> +#include <ctime> + +#include "debugutil_p.h" +#include "qqmldebugtestservice.h" + +QString fileName; + +class tst_QQmlDebugLocal : public QObject +{ + Q_OBJECT + +private: + QQmlDebugConnection *m_conn; + + bool connect(); + +signals: + void waiting(); + void parallel(); + +private slots: + void initTestCase(); + + void name(); + void state(); + void sendMessage(); +}; + +void tst_QQmlDebugLocal::initTestCase() +{ + const QString waitingMsg = QString("QML Debugger: Connecting to socket %1...").arg(fileName); + QTest::ignoreMessage(QtDebugMsg, waitingMsg.toLatin1().constData()); + + m_conn = new QQmlDebugConnection(this); + m_conn->startLocalServer(fileName); + + new QQmlEngine(this); + + QQmlDebugTestClient client("tst_QQmlDebugLocal::handshake()", m_conn); + QQmlDebugTestService service("tst_QQmlDebugLocal::handshake()"); + + for (int i = 0; i < 50; ++i) { + // try for 5 seconds ... + if (m_conn->waitForConnected(100)) + break; + } + + QVERIFY(m_conn->isConnected()); + + QTRY_VERIFY(QQmlDebugService::hasDebuggingClient()); + QTRY_COMPARE(client.state(), QQmlDebugClient::Enabled); +} + +void tst_QQmlDebugLocal::name() +{ + QString name = "tst_QQmlDebugLocal::name()"; + + QQmlDebugClient client(name, m_conn); + QCOMPARE(client.name(), name); +} + +void tst_QQmlDebugLocal::state() +{ + { + QQmlDebugConnection dummyConn; + QQmlDebugClient client("tst_QQmlDebugLocal::state()", &dummyConn); + QCOMPARE(client.state(), QQmlDebugClient::NotConnected); + QCOMPARE(client.serviceVersion(), -1.0f); + } + + QQmlDebugTestClient client("tst_QQmlDebugLocal::state()", m_conn); + QCOMPARE(client.state(), QQmlDebugClient::Unavailable); + + { + QQmlDebugTestService service("tst_QQmlDebugLocal::state()", 2); + QTRY_COMPARE(client.state(), QQmlDebugClient::Enabled); + QCOMPARE(client.serviceVersion(), 2.0f); + } + + QTRY_COMPARE(client.state(), QQmlDebugClient::Unavailable); + + // duplicate plugin name + QTest::ignoreMessage(QtWarningMsg, "QQmlDebugClient: Conflicting plugin name \"tst_QQmlDebugLocal::state()\""); + QQmlDebugClient client2("tst_QQmlDebugLocal::state()", m_conn); + QCOMPARE(client2.state(), QQmlDebugClient::NotConnected); + + QQmlDebugClient client3("tst_QQmlDebugLocal::state3()", 0); + QCOMPARE(client3.state(), QQmlDebugClient::NotConnected); +} + +void tst_QQmlDebugLocal::sendMessage() +{ + QQmlDebugTestService service("tst_QQmlDebugLocal::sendMessage()"); + QQmlDebugTestClient client("tst_QQmlDebugLocal::sendMessage()", m_conn); + + QByteArray msg = "hello!"; + + QTRY_COMPARE(client.state(), QQmlDebugClient::Enabled); + + client.sendMessage(msg); + QByteArray resp = client.waitForResponse(); + QCOMPARE(resp, msg); +} + +int main(int argc, char *argv[]) +{ + fileName = QString::fromLatin1("tst_QQmlDebugLocal%1").arg(time(0)); + + int _argc = argc + 1; + char **_argv = new char*[_argc]; + for (int i = 0; i < argc; ++i) + _argv[i] = argv[i]; + char *arg = strdup((QString::fromLatin1("-qmljsdebugger=file:") + fileName).toLatin1().data()); + _argv[_argc - 1] = arg; + + QGuiApplication app(_argc, _argv); + tst_QQmlDebugLocal tc; + return QTest::qExec(&tc, _argc, _argv); + delete _argv; +} + +#include "tst_qqmldebuglocal.moc" diff --git a/tests/auto/qml/debugger/qqmldebuglocal/tst_qqmldebuglocal.pro b/tests/auto/qml/debugger/qqmldebuglocal/tst_qqmldebuglocal.pro new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/tests/auto/qml/debugger/qqmldebuglocal/tst_qqmldebuglocal.pro diff --git a/tests/auto/qml/debugger/shared/qqmldebugclient.cpp b/tests/auto/qml/debugger/shared/qqmldebugclient.cpp index c7281dec69..0f7e572e02 100644 --- a/tests/auto/qml/debugger/shared/qqmldebugclient.cpp +++ b/tests/auto/qml/debugger/shared/qqmldebugclient.cpp @@ -39,6 +39,8 @@ #include <QtCore/qstringlist.h> #include <QtCore/qtimer.h> #include <QtNetwork/qnetworkproxy.h> +#include <QtNetwork/qlocalserver.h> +#include <QtNetwork/qlocalsocket.h> const int protocolVersion = 1; const QString serverId = QLatin1String("QDeclarativeDebugServer"); @@ -61,6 +63,7 @@ public: QQmlDebugConnection *q; QPacketProtocol *protocol; QIODevice *device; + QLocalServer *server; QEventLoop handshakeEventLoop; QTimer handshakeTimer; @@ -72,6 +75,10 @@ public: void connectDeviceSignals(); public Q_SLOTS: + void forwardStateChange(QLocalSocket::LocalSocketState state); + void forwardError(QLocalSocket::LocalSocketError error); + + void newConnection(); void connected(); void readyRead(); void deviceAboutToClose(); @@ -79,7 +86,7 @@ public Q_SLOTS: }; QQmlDebugConnectionPrivate::QQmlDebugConnectionPrivate(QQmlDebugConnection *c) - : QObject(c), q(c), protocol(0), device(0), gotHello(false) + : QObject(c), q(c), protocol(0), device(0), server(0), gotHello(false) { protocol = new QPacketProtocol(q, this); QObject::connect(c, SIGNAL(connected()), this, SLOT(connected())); @@ -307,10 +314,13 @@ void QQmlDebugConnection::close() bool QQmlDebugConnection::waitForConnected(int msecs) { QAbstractSocket *socket = qobject_cast<QAbstractSocket*>(d->device); - if (!socket) - return false; - if (!socket->waitForConnected(msecs)) + if (!socket) { + if (!d->server || (!d->server->hasPendingConnections() && + !d->server->waitForNewConnection(msecs))) + return false; + } else if (!socket->waitForConnected(msecs)) { return false; + } // wait for handshake d->handshakeTimer.start(); d->handshakeEventLoop.exec(); @@ -336,9 +346,13 @@ QString QQmlDebugConnection::stateString() const QAbstractSocket::SocketState QQmlDebugConnection::state() const { - QAbstractSocket *socket = qobject_cast<QAbstractSocket*>(d->device); - if (socket) - return socket->state(); + QAbstractSocket *abstractSocket = qobject_cast<QAbstractSocket*>(d->device); + if (abstractSocket) + return abstractSocket->state(); + + QLocalSocket *localSocket = qobject_cast<QLocalSocket*>(d->device); + if (localSocket) + return static_cast<QAbstractSocket::SocketState>(localSocket->state()); return QAbstractSocket::UnconnectedState; } @@ -366,6 +380,29 @@ void QQmlDebugConnection::connectToHost(const QString &hostName, quint16 port) QIODevice::open(ReadWrite | Unbuffered); } +void QQmlDebugConnection::startLocalServer(const QString &fileName) +{ + d->gotHello = false; + d->server = new QLocalServer(d); + // QueuedConnection so that waitForNewConnection() returns true. + connect(d->server, SIGNAL(newConnection()), d, SLOT(newConnection()), Qt::QueuedConnection); + d->server->listen(fileName); + QIODevice::open(ReadWrite | Unbuffered); +} + +void QQmlDebugConnectionPrivate::newConnection() +{ + QLocalSocket *socket = server->nextPendingConnection(); + server->close(); + device = socket; + connectDeviceSignals(); + connect(socket, SIGNAL(stateChanged(QLocalSocket::LocalSocketState)), + this, SLOT(forwardStateChange(QLocalSocket::LocalSocketState))); + connect(socket, SIGNAL(error(QLocalSocket::LocalSocketError)), + this, SLOT(forwardError(QLocalSocket::LocalSocketError))); + emit q->connected(); +} + void QQmlDebugConnectionPrivate::connectDeviceSignals() { connect(device, SIGNAL(bytesWritten(qint64)), q, SIGNAL(bytesWritten(qint64))); @@ -373,7 +410,15 @@ void QQmlDebugConnectionPrivate::connectDeviceSignals() connect(device, SIGNAL(aboutToClose()), this, SLOT(deviceAboutToClose())); } -// +void QQmlDebugConnectionPrivate::forwardStateChange(QLocalSocket::LocalSocketState state) +{ + emit q->stateChanged(static_cast<QAbstractSocket::SocketState>(state)); +} + +void QQmlDebugConnectionPrivate::forwardError(QLocalSocket::LocalSocketError error) +{ + emit q->error(static_cast<QAbstractSocket::SocketError>(error)); +} QQmlDebugClientPrivate::QQmlDebugClientPrivate() : connection(0) diff --git a/tests/auto/qml/debugger/shared/qqmldebugclient.h b/tests/auto/qml/debugger/shared/qqmldebugclient.h index 52f428cca7..fe9da693c8 100644 --- a/tests/auto/qml/debugger/shared/qqmldebugclient.h +++ b/tests/auto/qml/debugger/shared/qqmldebugclient.h @@ -46,6 +46,7 @@ public: ~QQmlDebugConnection(); void connectToHost(const QString &hostName, quint16 port); + void startLocalServer(const QString &fileName); void setDataStreamVersion(int dataStreamVersion); int dataStreamVersion(); |