1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the test suite of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
44 #include <QHostAddress>
48 #include <QtDeclarative/qdeclarativeengine.h>
50 #include "../shared/debugutil_p.h"
53 #define STR_PORT "13770"
55 class tst_QDeclarativeDebugClient : public QObject
60 QDeclarativeDebugConnection *m_conn;
68 void parallelConnect();
69 void sequentialConnect();
72 void tst_QDeclarativeDebugClient::initTestCase()
74 const QString waitingMsg = QString("QDeclarativeDebugServer: Waiting for connection on port %1...").arg(PORT);
75 QTest::ignoreMessage(QtWarningMsg, waitingMsg.toAscii().constData());
76 new QDeclarativeEngine(this);
78 m_conn = new QDeclarativeDebugConnection(this);
80 QDeclarativeDebugTestClient client("tst_QDeclarativeDebugClient::handshake()", m_conn);
81 QDeclarativeDebugTestService service("tst_QDeclarativeDebugClient::handshake()");
83 QTest::ignoreMessage(QtWarningMsg, "QDeclarativeDebugServer: Connection established");
84 for (int i = 0; i < 50; ++i) {
85 // try for 5 seconds ...
86 m_conn->connectToHost("127.0.0.1", PORT);
87 if (m_conn->waitForConnected())
92 QVERIFY(m_conn->isConnected());
94 QTRY_VERIFY(QDeclarativeDebugService::hasDebuggingClient());
95 QTRY_COMPARE(client.status(), QDeclarativeDebugClient::Enabled);
98 void tst_QDeclarativeDebugClient::name()
100 QString name = "tst_QDeclarativeDebugClient::name()";
102 QDeclarativeDebugClient client(name, m_conn);
103 QCOMPARE(client.name(), name);
106 void tst_QDeclarativeDebugClient::status()
109 QDeclarativeDebugConnection dummyConn;
110 QDeclarativeDebugClient client("tst_QDeclarativeDebugClient::status()", &dummyConn);
111 QCOMPARE(client.status(), QDeclarativeDebugClient::NotConnected);
114 QDeclarativeDebugTestClient client("tst_QDeclarativeDebugClient::status()", m_conn);
115 QCOMPARE(client.status(), QDeclarativeDebugClient::Unavailable);
118 QDeclarativeDebugTestService service("tst_QDeclarativeDebugClient::status()");
119 QTRY_COMPARE(client.status(), QDeclarativeDebugClient::Enabled);
122 QTRY_COMPARE(client.status(), QDeclarativeDebugClient::Unavailable);
124 // duplicate plugin name
125 QTest::ignoreMessage(QtWarningMsg, "QDeclarativeDebugClient: Conflicting plugin name \"tst_QDeclarativeDebugClient::status()\" ");
126 QDeclarativeDebugClient client2("tst_QDeclarativeDebugClient::status()", m_conn);
127 QCOMPARE(client2.status(), QDeclarativeDebugClient::NotConnected);
129 QDeclarativeDebugClient client3("tst_QDeclarativeDebugClient::status3()", 0);
130 QCOMPARE(client3.status(), QDeclarativeDebugClient::NotConnected);
133 void tst_QDeclarativeDebugClient::sendMessage()
135 QDeclarativeDebugTestService service("tst_QDeclarativeDebugClient::sendMessage()");
136 QDeclarativeDebugTestClient client("tst_QDeclarativeDebugClient::sendMessage()", m_conn);
138 QByteArray msg = "hello!";
140 QTRY_COMPARE(client.status(), QDeclarativeDebugClient::Enabled);
142 client.sendMessage(msg);
143 QByteArray resp = client.waitForResponse();
147 void tst_QDeclarativeDebugClient::parallelConnect()
149 QDeclarativeDebugConnection connection2;
151 connection2.connectToHost("127.0.0.1", PORT);
152 QTest::ignoreMessage(QtWarningMsg, "QDeclarativeDebugServer: Another client is already connected");
153 // will connect & immediately disconnect
154 QVERIFY(connection2.waitForConnected());
155 QTRY_COMPARE(connection2.state(), QAbstractSocket::UnconnectedState);
156 QVERIFY(m_conn->isConnected());
159 void tst_QDeclarativeDebugClient::sequentialConnect()
161 QDeclarativeDebugConnection connection2;
162 QDeclarativeDebugTestClient client2("tst_QDeclarativeDebugClient::handshake()", &connection2);
163 QDeclarativeDebugTestService service("tst_QDeclarativeDebugClient::handshake()");
166 QVERIFY(!m_conn->isConnected());
167 QCOMPARE(m_conn->state(), QAbstractSocket::UnconnectedState);
169 // Make sure that the disconnect is actually delivered to the server
172 connection2.connectToHost("127.0.0.1", PORT);
173 QTest::ignoreMessage(QtWarningMsg, "QDeclarativeDebugServer: Connection established");
174 QVERIFY(connection2.waitForConnected());
175 QVERIFY(connection2.isConnected());
176 QTRY_VERIFY(client2.status() == QDeclarativeDebugClient::Enabled);
179 int main(int argc, char *argv[])
181 int _argc = argc + 1;
182 char **_argv = new char*[_argc];
183 for (int i = 0; i < argc; ++i)
186 _argv[_argc - 1] = "-qmljsdebugger=port:" STR_PORT;
188 QGuiApplication app(_argc, _argv);
189 tst_QDeclarativeDebugClient tc;
190 return QTest::qExec(&tc, _argc, _argv);
194 #include "tst_qdeclarativedebugclient.moc"