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 <private/qdeclarativedebugclient_p.h>
51 #include <private/qdeclarativedebugservice_p.h>
53 #include "../../shared/util.h"
54 #include "../shared/debugutil_p.h"
57 #define STR_PORT "13769"
59 class tst_QDeclarativeDebugService : public QObject
63 QDeclarativeDebugConnection *m_conn;
73 void objectToString();
76 void tst_QDeclarativeDebugService::initTestCase()
78 const QString waitingMsg = QString("QDeclarativeDebugServer: Waiting for connection on port %1...").arg(PORT);
79 QTest::ignoreMessage(QtWarningMsg, waitingMsg.toAscii().constData());
80 new QDeclarativeEngine(this);
82 m_conn = new QDeclarativeDebugConnection(this);
85 QTest::ignoreMessage(QtWarningMsg, "QDeclarativeDebugServer: Connection established");
86 for (int i = 0; i < 50; ++i) {
87 // try for 5 seconds ...
88 m_conn->connectToHost("127.0.0.1", PORT);
89 if (m_conn->waitForConnected())
93 QVERIFY(m_conn->isConnected());
95 QTRY_VERIFY(QDeclarativeDebugService::hasDebuggingClient());
98 void tst_QDeclarativeDebugService::name()
100 QString name = "tst_QDeclarativeDebugService::name()";
102 QDeclarativeDebugService service(name);
103 QCOMPARE(service.name(), name);
106 void tst_QDeclarativeDebugService::status()
108 QDeclarativeDebugTestService service("tst_QDeclarativeDebugService::status()");
109 QCOMPARE(service.status(), QDeclarativeDebugService::Unavailable);
112 QDeclarativeDebugTestClient client("tst_QDeclarativeDebugService::status()", m_conn);
113 QTRY_COMPARE(client.status(), QDeclarativeDebugClient::Enabled);
114 QTRY_COMPARE(service.status(), QDeclarativeDebugService::Enabled);
118 QTRY_COMPARE(service.status(), QDeclarativeDebugService::Unavailable);
120 QTest::ignoreMessage(QtWarningMsg, "QDeclarativeDebugService: Conflicting plugin name \"tst_QDeclarativeDebugService::status()\" ");
122 QDeclarativeDebugService duplicate("tst_QDeclarativeDebugService::status()");
123 QCOMPARE(duplicate.status(), QDeclarativeDebugService::NotConnected);
126 void tst_QDeclarativeDebugService::sendMessage()
128 QDeclarativeDebugTestService service("tst_QDeclarativeDebugService::sendMessage()");
129 QDeclarativeDebugTestClient client("tst_QDeclarativeDebugService::sendMessage()", m_conn);
131 QByteArray msg = "hello!";
133 QTRY_COMPARE(client.status(), QDeclarativeDebugClient::Enabled);
134 QTRY_COMPARE(service.status(), QDeclarativeDebugService::Enabled);
136 client.sendMessage(msg);
137 QByteArray resp = client.waitForResponse();
140 QTest::ignoreMessage(QtWarningMsg, "QDeclarativeDebugService: Conflicting plugin name \"tst_QDeclarativeDebugService::sendMessage()\" ");
141 QDeclarativeDebugService duplicate("tst_QDeclarativeDebugService::sendMessage()");
142 duplicate.sendMessage("msg");
145 void tst_QDeclarativeDebugService::idForObject()
147 QCOMPARE(QDeclarativeDebugService::idForObject(0), -1);
149 QObject *objA = new QObject;
151 int idA = QDeclarativeDebugService::idForObject(objA);
153 QCOMPARE(QDeclarativeDebugService::objectForId(idA), objA);
155 int idAA = QDeclarativeDebugService::idForObject(objA);
158 QObject *objB = new QObject;
159 int idB = QDeclarativeDebugService::idForObject(objB);
161 QCOMPARE(QDeclarativeDebugService::objectForId(idB), objB);
167 void tst_QDeclarativeDebugService::objectForId()
169 QCOMPARE(QDeclarativeDebugService::objectForId(-1), static_cast<QObject*>(0));
170 QCOMPARE(QDeclarativeDebugService::objectForId(1), static_cast<QObject*>(0));
172 QObject *obj = new QObject;
173 int id = QDeclarativeDebugService::idForObject(obj);
174 QCOMPARE(QDeclarativeDebugService::objectForId(id), obj);
177 QCOMPARE(QDeclarativeDebugService::objectForId(id), static_cast<QObject*>(0));
180 void tst_QDeclarativeDebugService::objectToString()
182 QCOMPARE(QDeclarativeDebugService::objectToString(0), QString("NULL"));
184 QObject *obj = new QObject;
185 QCOMPARE(QDeclarativeDebugService::objectToString(obj), QString("QObject: <unnamed>"));
187 obj->setObjectName("Hello");
188 QCOMPARE(QDeclarativeDebugService::objectToString(obj), QString("QObject: Hello"));
193 int main(int argc, char *argv[])
195 int _argc = argc + 1;
196 char **_argv = new char*[_argc];
197 for (int i = 0; i < argc; ++i)
199 char arg[] = "-qmljsdebugger=port:" STR_PORT;
200 _argv[_argc - 1] = arg;
202 QGuiApplication app(_argc, _argv);
203 tst_QDeclarativeDebugService tc;
204 return QTest::qExec(&tc, _argc, _argv);
208 #include "tst_qdeclarativedebugservice.moc"