Debugger: Move server into it's own thread
[profile/ivi/qtdeclarative.git] / tests / auto / declarative / debugger / qdeclarativedebugservice / tst_qdeclarativedebugservice.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the test suite of the Qt Toolkit.
8 **
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.
17 **
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.
21 **
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.
29 **
30 ** Other Usage
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.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 #include <qtest.h>
42 #include <QSignalSpy>
43 #include <QTimer>
44 #include <QHostAddress>
45 #include <QDebug>
46 #include <QThread>
47
48 #include <QtDeclarative/qdeclarativeengine.h>
49
50 #include <private/qdeclarativedebugclient_p.h>
51 #include <private/qdeclarativedebugservice_p.h>
52
53 #include "../../shared/util.h"
54 #include "../shared/debugutil_p.h"
55
56 #define PORT 13769
57 #define STR_PORT "13769"
58
59 class tst_QDeclarativeDebugService : public QObject
60 {
61     Q_OBJECT
62 private:
63     QDeclarativeDebugConnection *m_conn;
64
65 private slots:
66     void initTestCase();
67
68     void name();
69     void status();
70     void sendMessage();
71     void idForObject();
72     void objectForId();
73     void objectToString();
74 };
75
76 void tst_QDeclarativeDebugService::initTestCase()
77 {
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);
81
82     m_conn = new QDeclarativeDebugConnection(this);
83
84
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())
90             break;
91         QTest::qSleep(100);
92     }
93     QVERIFY(m_conn->isConnected());
94
95     QTRY_VERIFY(QDeclarativeDebugService::hasDebuggingClient());
96 }
97
98 void tst_QDeclarativeDebugService::name()
99 {
100     QString name = "tst_QDeclarativeDebugService::name()";
101
102     QDeclarativeDebugService service(name);
103     QCOMPARE(service.name(), name);
104 }
105
106 void tst_QDeclarativeDebugService::status()
107 {
108     QDeclarativeDebugTestService service("tst_QDeclarativeDebugService::status()");
109     QCOMPARE(service.status(), QDeclarativeDebugService::Unavailable);
110
111     {
112         QDeclarativeDebugTestClient client("tst_QDeclarativeDebugService::status()", m_conn);
113         QTRY_COMPARE(client.status(), QDeclarativeDebugClient::Enabled);
114         QTRY_COMPARE(service.status(), QDeclarativeDebugService::Enabled);
115     }
116
117
118     QTRY_COMPARE(service.status(), QDeclarativeDebugService::Unavailable);
119
120     QTest::ignoreMessage(QtWarningMsg, "QDeclarativeDebugService: Conflicting plugin name \"tst_QDeclarativeDebugService::status()\" ");
121
122     QDeclarativeDebugService duplicate("tst_QDeclarativeDebugService::status()");
123     QCOMPARE(duplicate.status(), QDeclarativeDebugService::NotConnected);
124 }
125
126 void tst_QDeclarativeDebugService::sendMessage()
127 {
128     QDeclarativeDebugTestService service("tst_QDeclarativeDebugService::sendMessage()");
129     QDeclarativeDebugTestClient client("tst_QDeclarativeDebugService::sendMessage()", m_conn);
130
131     QByteArray msg = "hello!";
132
133     QTRY_COMPARE(client.status(), QDeclarativeDebugClient::Enabled);
134     QTRY_COMPARE(service.status(), QDeclarativeDebugService::Enabled);
135
136     client.sendMessage(msg);
137     QByteArray resp = client.waitForResponse();
138     QCOMPARE(resp, msg);
139
140     QTest::ignoreMessage(QtWarningMsg, "QDeclarativeDebugService: Conflicting plugin name \"tst_QDeclarativeDebugService::sendMessage()\" ");
141     QDeclarativeDebugService duplicate("tst_QDeclarativeDebugService::sendMessage()");
142     duplicate.sendMessage("msg");
143 }
144
145 void tst_QDeclarativeDebugService::idForObject()
146 {
147     QCOMPARE(QDeclarativeDebugService::idForObject(0), -1);
148
149     QObject *objA = new QObject;
150
151     int idA = QDeclarativeDebugService::idForObject(objA);
152     QVERIFY(idA >= 0);
153     QCOMPARE(QDeclarativeDebugService::objectForId(idA), objA);
154
155     int idAA = QDeclarativeDebugService::idForObject(objA);
156     QCOMPARE(idAA, idA);
157
158     QObject *objB = new QObject;
159     int idB = QDeclarativeDebugService::idForObject(objB);
160     QVERIFY(idB != idA);
161     QCOMPARE(QDeclarativeDebugService::objectForId(idB), objB);
162
163     delete objA;
164     delete objB;
165 }
166
167 void tst_QDeclarativeDebugService::objectForId()
168 {
169     QCOMPARE(QDeclarativeDebugService::objectForId(-1), static_cast<QObject*>(0));
170     QCOMPARE(QDeclarativeDebugService::objectForId(1), static_cast<QObject*>(0));
171
172     QObject *obj = new QObject;
173     int id = QDeclarativeDebugService::idForObject(obj);
174     QCOMPARE(QDeclarativeDebugService::objectForId(id), obj);
175
176     delete obj;
177     QCOMPARE(QDeclarativeDebugService::objectForId(id), static_cast<QObject*>(0));
178 }
179
180 void tst_QDeclarativeDebugService::objectToString()
181 {
182     QCOMPARE(QDeclarativeDebugService::objectToString(0), QString("NULL"));
183
184     QObject *obj = new QObject;
185     QCOMPARE(QDeclarativeDebugService::objectToString(obj), QString("QObject: <unnamed>"));
186
187     obj->setObjectName("Hello");
188     QCOMPARE(QDeclarativeDebugService::objectToString(obj), QString("QObject: Hello"));
189     delete obj;
190 }
191
192
193 int main(int argc, char *argv[])
194 {
195     int _argc = argc + 1;
196     char **_argv = new char*[_argc];
197     for (int i = 0; i < argc; ++i)
198         _argv[i] = argv[i];
199     char arg[] = "-qmljsdebugger=port:" STR_PORT;
200     _argv[_argc - 1] = arg;
201
202     QGuiApplication app(_argc, _argv);
203     tst_QDeclarativeDebugService tc;
204     return QTest::qExec(&tc, _argc, _argv);
205     delete _argv;
206 }
207
208 #include "tst_qdeclarativedebugservice.moc"