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 "debugutil_p.h" |
30 | #include "qqmldebugtestservice.h" |
31 | |
32 | #include <private/qqmldebugconnector_p.h> |
33 | #include <private/qqmldebugconnection_p.h> |
34 | |
35 | #include <QtTest/qtest.h> |
36 | #include <QtTest/qsignalspy.h> |
37 | #include <QtCore/qtimer.h> |
38 | #include <QtCore/qdebug.h> |
39 | #include <QtCore/qthread.h> |
40 | #include <QtNetwork/qhostaddress.h> |
41 | #include <QtQml/qqmlengine.h> |
42 | |
43 | #define PORT 13770 |
44 | #define STR_PORT "13770" |
45 | |
46 | class tst_QQmlDebugClient : public QObject |
47 | { |
48 | Q_OBJECT |
49 | |
50 | private: |
51 | QQmlDebugConnection *m_conn; |
52 | QQmlDebugTestService *m_service; |
53 | |
54 | private slots: |
55 | void initTestCase(); |
56 | |
57 | void name(); |
58 | void state(); |
59 | void sendMessage(); |
60 | void parallelConnect(); |
61 | void sequentialConnect(); |
62 | }; |
63 | |
64 | void tst_QQmlDebugClient::initTestCase() |
65 | { |
66 | QQmlDebugConnector::setPluginKey(QLatin1String("QQmlDebugServer" )); |
67 | QQmlDebugConnector::setServices(QStringList() |
68 | << QStringLiteral("tst_QQmlDebugClient::handshake()" )); |
69 | |
70 | m_service = new QQmlDebugTestService("tst_QQmlDebugClient::handshake()" ); |
71 | |
72 | foreach (const QString &service, QQmlDebuggingEnabler::debuggerServices()) |
73 | QCOMPARE(QQmlDebugConnector::instance()->service(service), (QQmlDebugService *)nullptr); |
74 | foreach (const QString &service, QQmlDebuggingEnabler::inspectorServices()) |
75 | QCOMPARE(QQmlDebugConnector::instance()->service(service), (QQmlDebugService *)nullptr); |
76 | foreach (const QString &service, QQmlDebuggingEnabler::profilerServices()) |
77 | QCOMPARE(QQmlDebugConnector::instance()->service(service), (QQmlDebugService *)nullptr); |
78 | |
79 | const QString waitingMsg = QString("QML Debugger: Waiting for connection on port %1..." ).arg(PORT); |
80 | QTest::ignoreMessage(type: QtDebugMsg, message: waitingMsg.toLatin1().constData()); |
81 | QQmlDebuggingEnabler::startTcpDebugServer(PORT); |
82 | |
83 | new QQmlEngine(this); |
84 | |
85 | m_conn = new QQmlDebugConnection(this); |
86 | |
87 | QQmlDebugTestClient client("tst_QQmlDebugClient::handshake()" , m_conn); |
88 | |
89 | |
90 | for (int i = 0; i < 50; ++i) { |
91 | // try for 5 seconds ... |
92 | m_conn->connectToHost(hostName: "127.0.0.1" , PORT); |
93 | if (m_conn->waitForConnected(msecs: 100)) |
94 | break; |
95 | } |
96 | |
97 | QVERIFY(m_conn->isConnected()); |
98 | |
99 | QTRY_COMPARE(client.state(), QQmlDebugClient::Enabled); |
100 | } |
101 | |
102 | void tst_QQmlDebugClient::name() |
103 | { |
104 | QString name = "tst_QQmlDebugClient::name()" ; |
105 | |
106 | QQmlDebugClient client(name, m_conn); |
107 | QCOMPARE(client.name(), name); |
108 | } |
109 | |
110 | void tst_QQmlDebugClient::state() |
111 | { |
112 | { |
113 | QQmlDebugConnection dummyConn; |
114 | QQmlDebugClient client("tst_QQmlDebugClient::state()" , &dummyConn); |
115 | QCOMPARE(client.state(), QQmlDebugClient::NotConnected); |
116 | QCOMPARE(client.serviceVersion(), -1.0f); |
117 | } |
118 | |
119 | QQmlDebugTestClient client("tst_QQmlDebugClient::state()" , m_conn); |
120 | QCOMPARE(client.state(), QQmlDebugClient::Unavailable); |
121 | |
122 | // duplicate plugin name |
123 | QTest::ignoreMessage(type: QtWarningMsg, message: "QQmlDebugClient: Conflicting plugin name \"tst_QQmlDebugClient::state()\"" ); |
124 | QQmlDebugClient client2("tst_QQmlDebugClient::state()" , m_conn); |
125 | QCOMPARE(client2.state(), QQmlDebugClient::NotConnected); |
126 | |
127 | QQmlDebugClient client3("tst_QQmlDebugClient::state3()" , nullptr); |
128 | QCOMPARE(client3.state(), QQmlDebugClient::NotConnected); |
129 | } |
130 | |
131 | void tst_QQmlDebugClient::sendMessage() |
132 | { |
133 | QQmlDebugTestClient client("tst_QQmlDebugClient::handshake()" , m_conn); |
134 | |
135 | QByteArray msg = "hello!" ; |
136 | |
137 | QTRY_COMPARE(client.state(), QQmlDebugClient::Enabled); |
138 | |
139 | client.sendMessage(message: msg); |
140 | QByteArray resp = client.waitForResponse(); |
141 | QCOMPARE(resp, msg); |
142 | } |
143 | |
144 | void tst_QQmlDebugClient::parallelConnect() |
145 | { |
146 | QQmlDebugConnection connection2; |
147 | |
148 | QTest::ignoreMessage(type: QtWarningMsg, message: "QML Debugger: Another client is already connected." ); |
149 | // will connect & immediately disconnect |
150 | connection2.connectToHost(hostName: "127.0.0.1" , PORT); |
151 | QTest::ignoreMessage(type: QtWarningMsg, message: "QQmlDebugConnection: Did not get handshake answer in time" ); |
152 | QVERIFY(!connection2.waitForConnected(1000)); |
153 | QVERIFY(!connection2.isConnected()); |
154 | QVERIFY(m_conn->isConnected()); |
155 | } |
156 | |
157 | void tst_QQmlDebugClient::sequentialConnect() |
158 | { |
159 | QQmlDebugConnection connection2; |
160 | QQmlDebugTestClient client2("tst_QQmlDebugClient::handshake()" , &connection2); |
161 | |
162 | m_conn->close(); |
163 | QVERIFY(!m_conn->isConnected()); |
164 | |
165 | // Make sure that the disconnect is actually delivered to the server |
166 | QTest::qWait(ms: 100); |
167 | |
168 | connection2.connectToHost(hostName: "127.0.0.1" , PORT); |
169 | QVERIFY(connection2.waitForConnected()); |
170 | QVERIFY(connection2.isConnected()); |
171 | QTRY_COMPARE(client2.state(), QQmlDebugClient::Enabled); |
172 | } |
173 | |
174 | QTEST_MAIN(tst_QQmlDebugClient) |
175 | |
176 | #include "tst_qqmldebugclient.moc" |
177 | |
178 | |