| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the test suite of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
| 21 | ** included in the packaging of this file. Please review the following |
| 22 | ** information to ensure the GNU General Public License requirements will |
| 23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
| 24 | ** |
| 25 | ** $QT_END_LICENSE$ |
| 26 | ** |
| 27 | ****************************************************************************/ |
| 28 | |
| 29 | #include "qqmldebugtestservice.h" |
| 30 | #include "debugutil_p.h" |
| 31 | |
| 32 | #include <private/qqmldebugconnector_p.h> |
| 33 | #include <private/qqmldebugconnection_p.h> |
| 34 | |
| 35 | #include <QtQml/qqmlengine.h> |
| 36 | |
| 37 | #include <QtTest/qtest.h> |
| 38 | #include <QtTest/qsignalspy.h> |
| 39 | #include <QtNetwork/qhostaddress.h> |
| 40 | #include <QtCore/qtimer.h> |
| 41 | #include <QtCore/qdebug.h> |
| 42 | #include <QtCore/qthread.h> |
| 43 | |
| 44 | #include <ctime> |
| 45 | |
| 46 | QString fileName; |
| 47 | |
| 48 | class tst_QQmlDebugLocal : public QObject |
| 49 | { |
| 50 | Q_OBJECT |
| 51 | |
| 52 | private: |
| 53 | QQmlDebugConnection *m_conn; |
| 54 | QQmlDebugTestService *m_service; |
| 55 | |
| 56 | private slots: |
| 57 | void initTestCase(); |
| 58 | |
| 59 | void name(); |
| 60 | void state(); |
| 61 | void sendMessage(); |
| 62 | }; |
| 63 | |
| 64 | void tst_QQmlDebugLocal::initTestCase() |
| 65 | { |
| 66 | fileName = QString::fromLatin1(str: "tst_QQmlDebugLocal%1" ).arg(a: std::time(timer: nullptr)); |
| 67 | QQmlDebugConnector::setPluginKey("QQmlDebugServer" ); |
| 68 | m_service = new QQmlDebugTestService("tst_QQmlDebugLocal::handshake()" ); |
| 69 | |
| 70 | const QString waitingMsg = QString("QML Debugger: Connecting to socket %1..." ).arg(a: fileName); |
| 71 | QTest::ignoreMessage(type: QtDebugMsg, message: waitingMsg.toLatin1().constData()); |
| 72 | QQmlDebuggingEnabler::connectToLocalDebugger(socketFileName: fileName); |
| 73 | |
| 74 | QTest::qWait(ms: 1000); |
| 75 | |
| 76 | m_conn = new QQmlDebugConnection(this); |
| 77 | m_conn->startLocalServer(fileName); |
| 78 | |
| 79 | new QQmlEngine(this); |
| 80 | |
| 81 | QQmlDebugTestClient client("tst_QQmlDebugLocal::handshake()" , m_conn); |
| 82 | |
| 83 | for (int i = 0; i < 50; ++i) { |
| 84 | // try for 5 seconds ... |
| 85 | if (m_conn->waitForConnected(msecs: 100)) |
| 86 | break; |
| 87 | } |
| 88 | |
| 89 | QVERIFY(m_conn->isConnected()); |
| 90 | |
| 91 | QTRY_COMPARE(client.state(), QQmlDebugClient::Enabled); |
| 92 | } |
| 93 | |
| 94 | void tst_QQmlDebugLocal::name() |
| 95 | { |
| 96 | QString name = "tst_QQmlDebugLocal::name()" ; |
| 97 | |
| 98 | QQmlDebugClient client(name, m_conn); |
| 99 | QCOMPARE(client.name(), name); |
| 100 | } |
| 101 | |
| 102 | void tst_QQmlDebugLocal::state() |
| 103 | { |
| 104 | { |
| 105 | QQmlDebugConnection dummyConn; |
| 106 | QQmlDebugClient client("tst_QQmlDebugLocal::state()" , &dummyConn); |
| 107 | QCOMPARE(client.state(), QQmlDebugClient::NotConnected); |
| 108 | QCOMPARE(client.serviceVersion(), -1.0f); |
| 109 | } |
| 110 | |
| 111 | QQmlDebugTestClient client("tst_QQmlDebugLocal::state()" , m_conn); |
| 112 | QCOMPARE(client.state(), QQmlDebugClient::Unavailable); |
| 113 | |
| 114 | // duplicate plugin name |
| 115 | QTest::ignoreMessage(type: QtWarningMsg, message: "QQmlDebugClient: Conflicting plugin name \"tst_QQmlDebugLocal::state()\"" ); |
| 116 | QQmlDebugClient client2("tst_QQmlDebugLocal::state()" , m_conn); |
| 117 | QCOMPARE(client2.state(), QQmlDebugClient::NotConnected); |
| 118 | |
| 119 | QQmlDebugClient client3("tst_QQmlDebugLocal::state3()" , nullptr); |
| 120 | QCOMPARE(client3.state(), QQmlDebugClient::NotConnected); |
| 121 | } |
| 122 | |
| 123 | void tst_QQmlDebugLocal::sendMessage() |
| 124 | { |
| 125 | QQmlDebugTestClient client("tst_QQmlDebugLocal::handshake()" , m_conn); |
| 126 | |
| 127 | QByteArray msg = "hello!" ; |
| 128 | |
| 129 | QTRY_COMPARE(client.state(), QQmlDebugClient::Enabled); |
| 130 | |
| 131 | client.sendMessage(message: msg); |
| 132 | QByteArray resp = client.waitForResponse(); |
| 133 | QCOMPARE(resp, msg); |
| 134 | } |
| 135 | |
| 136 | QTEST_MAIN(tst_QQmlDebugLocal) |
| 137 | |
| 138 | #include "tst_qqmldebuglocal.moc" |
| 139 | |