1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the test suite of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
44 #include <QHostAddress>
48 #include <QtQml/qqmlengine.h>
50 #include <private/qqmldebugclient_p.h>
51 #include <private/qqmldebugservice_p.h>
53 #include "../../../shared/util.h"
54 #include "../shared/debugutil_p.h"
57 #define STR_PORT "13769"
59 class tst_QQmlDebugService : public QObject
63 QQmlDebugConnection *m_conn;
74 void objectToString();
77 void tst_QQmlDebugService::initTestCase()
79 const QString waitingMsg = QString("QML Debugger: Waiting for connection on port %1...").arg(PORT);
80 QTest::ignoreMessage(QtWarningMsg, waitingMsg.toAscii().constData());
83 m_conn = new QQmlDebugConnection(this);
86 QTest::ignoreMessage(QtWarningMsg, "QML Debugger: Connection established.");
87 for (int i = 0; i < 50; ++i) {
88 // try for 5 seconds ...
89 m_conn->connectToHost("127.0.0.1", PORT);
90 if (m_conn->waitForConnected())
94 QVERIFY(m_conn->isConnected());
96 QTRY_VERIFY(QQmlDebugService::hasDebuggingClient());
99 void tst_QQmlDebugService::name()
101 QString name = "tst_QQmlDebugService::name()";
103 QQmlDebugService service(name, 1);
104 QCOMPARE(service.name(), name);
107 void tst_QQmlDebugService::version()
109 QString name = "tst_QQmlDebugService::name()";
111 QQmlDebugService service(name, 2);
112 QCOMPARE(service.version(), 2.0f);
115 void tst_QQmlDebugService::state()
117 QQmlDebugTestService service("tst_QQmlDebugService::state()");
118 QCOMPARE(service.state(), QQmlDebugService::Unavailable);
121 QQmlDebugTestClient client("tst_QQmlDebugService::state()", m_conn);
122 QTRY_COMPARE(client.state(), QQmlDebugClient::Enabled);
123 QTRY_COMPARE(service.state(), QQmlDebugService::Enabled);
127 QTRY_COMPARE(service.state(), QQmlDebugService::Unavailable);
129 QTest::ignoreMessage(QtWarningMsg, "QQmlDebugService: Conflicting plugin name \"tst_QQmlDebugService::state()\" ");
130 QQmlDebugTestService duplicate("tst_QQmlDebugService::state()");
131 QCOMPARE(duplicate.state(), QQmlDebugService::NotConnected);
134 void tst_QQmlDebugService::sendMessage()
136 QQmlDebugTestService service("tst_QQmlDebugService::sendMessage()");
137 QQmlDebugTestClient client("tst_QQmlDebugService::sendMessage()", m_conn);
139 QByteArray msg = "hello!";
141 QTRY_COMPARE(client.state(), QQmlDebugClient::Enabled);
142 QTRY_COMPARE(service.state(), QQmlDebugService::Enabled);
144 client.sendMessage(msg);
145 QByteArray resp = client.waitForResponse();
148 QTest::ignoreMessage(QtWarningMsg, "QQmlDebugService: Conflicting plugin name \"tst_QQmlDebugService::sendMessage()\" ");
149 QQmlDebugTestService duplicate("tst_QQmlDebugService::sendMessage()");
150 duplicate.sendMessage("msg");
153 void tst_QQmlDebugService::idForObject()
155 QCOMPARE(QQmlDebugService::idForObject(0), -1);
157 QObject *objA = new QObject;
159 int idA = QQmlDebugService::idForObject(objA);
161 QCOMPARE(QQmlDebugService::objectForId(idA), objA);
163 int idAA = QQmlDebugService::idForObject(objA);
166 QObject *objB = new QObject;
167 int idB = QQmlDebugService::idForObject(objB);
169 QCOMPARE(QQmlDebugService::objectForId(idB), objB);
175 void tst_QQmlDebugService::objectForId()
177 QCOMPARE(QQmlDebugService::objectForId(-1), static_cast<QObject*>(0));
178 QCOMPARE(QQmlDebugService::objectForId(1), static_cast<QObject*>(0));
180 QObject *obj = new QObject;
181 int id = QQmlDebugService::idForObject(obj);
182 QCOMPARE(QQmlDebugService::objectForId(id), obj);
185 QCOMPARE(QQmlDebugService::objectForId(id), static_cast<QObject*>(0));
188 void tst_QQmlDebugService::objectToString()
190 QCOMPARE(QQmlDebugService::objectToString(0), QString("NULL"));
192 QObject *obj = new QObject;
193 QCOMPARE(QQmlDebugService::objectToString(obj), QString("QObject: <unnamed>"));
195 obj->setObjectName("Hello");
196 QCOMPARE(QQmlDebugService::objectToString(obj), QString("QObject: Hello"));
201 int main(int argc, char *argv[])
203 int _argc = argc + 1;
204 char **_argv = new char*[_argc];
205 for (int i = 0; i < argc; ++i)
207 char arg[] = "-qmljsdebugger=port:" STR_PORT;
208 _argv[_argc - 1] = arg;
210 QGuiApplication app(_argc, _argv);
211 tst_QQmlDebugService tc;
212 return QTest::qExec(&tc, _argc, _argv);
216 #include "tst_qqmldebugservice.moc"