Initial import from the monolithic Qt.
[profile/ivi/qtdeclarative.git] / tests / auto / declarative / 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 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file.  Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23 **
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **
32 **
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 #include <private/qdeclarativedebughelper_p.h>
50
51 #include <private/qdeclarativedebug_p.h>
52 #include <private/qdeclarativeenginedebug_p.h>
53 #include <private/qdeclarativedebugclient_p.h>
54 #include <private/qdeclarativedebugservice_p.h>
55
56 #include "../../../shared/util.h"
57 #include "../shared/debugutil_p.h"
58
59
60 class tst_QDeclarativeDebugService : public QObject
61 {
62     Q_OBJECT
63 private:
64     QDeclarativeDebugConnection *m_conn;
65
66 private slots:
67     void initTestCase();
68
69     void name();
70     void status();
71     void sendMessage();
72     void idForObject();
73     void objectForId();
74     void objectToString();
75 };
76
77 void tst_QDeclarativeDebugService::initTestCase()
78 {
79     QTest::ignoreMessage(QtWarningMsg, "Qml debugging is enabled. Only use this in a safe environment!");
80     QDeclarativeDebugHelper::enableDebugging();
81
82     QTest::ignoreMessage(QtWarningMsg, "QDeclarativeDebugServer: Waiting for connection on port 13769...");
83     new QDeclarativeEngine(this);
84
85     m_conn = new QDeclarativeDebugConnection(this);
86     m_conn->connectToHost("127.0.0.1", 13769);
87
88     QTest::ignoreMessage(QtWarningMsg, "QDeclarativeDebugServer: Connection established");
89     bool ok = m_conn->waitForConnected();
90     Q_ASSERT(ok);
91
92     QTRY_VERIFY(QDeclarativeDebugService::hasDebuggingClient());
93 }
94
95 void tst_QDeclarativeDebugService::name()
96 {
97     QString name = "tst_QDeclarativeDebugService::name()";
98
99     QDeclarativeDebugService service(name);
100     QCOMPARE(service.name(), name);
101 }
102
103 void tst_QDeclarativeDebugService::status()
104 {
105     QDeclarativeDebugTestService service("tst_QDeclarativeDebugService::status()");
106     QCOMPARE(service.status(), QDeclarativeDebugService::Unavailable);
107
108     {
109         QDeclarativeDebugTestClient client("tst_QDeclarativeDebugService::status()", m_conn);
110         QTRY_COMPARE(client.status(), QDeclarativeDebugClient::Enabled);
111         QTRY_COMPARE(service.status(), QDeclarativeDebugService::Enabled);
112     }
113
114
115     QTRY_COMPARE(service.status(), QDeclarativeDebugService::Unavailable);
116
117     QTest::ignoreMessage(QtWarningMsg, "QDeclarativeDebugService: Conflicting plugin name \"tst_QDeclarativeDebugService::status()\" ");
118
119     QDeclarativeDebugService duplicate("tst_QDeclarativeDebugService::status()");
120     QCOMPARE(duplicate.status(), QDeclarativeDebugService::NotConnected);
121 }
122
123 void tst_QDeclarativeDebugService::sendMessage()
124 {
125     QDeclarativeDebugTestService service("tst_QDeclarativeDebugService::sendMessage()");
126     QDeclarativeDebugTestClient client("tst_QDeclarativeDebugService::sendMessage()", m_conn);
127
128     QByteArray msg = "hello!";
129
130     QTRY_COMPARE(client.status(), QDeclarativeDebugClient::Enabled);
131     QTRY_COMPARE(service.status(), QDeclarativeDebugService::Enabled);
132
133     client.sendMessage(msg);
134     QByteArray resp = client.waitForResponse();
135     QCOMPARE(resp, msg);
136
137     QTest::ignoreMessage(QtWarningMsg, "QDeclarativeDebugService: Conflicting plugin name \"tst_QDeclarativeDebugService::sendMessage()\" ");
138     QDeclarativeDebugService duplicate("tst_QDeclarativeDebugService::sendMessage()");
139     duplicate.sendMessage("msg");
140 }
141
142 void tst_QDeclarativeDebugService::idForObject()
143 {
144     QCOMPARE(QDeclarativeDebugService::idForObject(0), -1);
145
146     QObject *objA = new QObject;
147
148     int idA = QDeclarativeDebugService::idForObject(objA);
149     QVERIFY(idA >= 0);
150     QCOMPARE(QDeclarativeDebugService::objectForId(idA), objA);
151
152     int idAA = QDeclarativeDebugService::idForObject(objA);
153     QCOMPARE(idAA, idA);
154
155     QObject *objB = new QObject;
156     int idB = QDeclarativeDebugService::idForObject(objB);
157     QVERIFY(idB != idA);
158     QCOMPARE(QDeclarativeDebugService::objectForId(idB), objB);
159
160     delete objA;
161     delete objB;
162 }
163
164 void tst_QDeclarativeDebugService::objectForId()
165 {
166     QCOMPARE(QDeclarativeDebugService::objectForId(-1), static_cast<QObject*>(0));
167     QCOMPARE(QDeclarativeDebugService::objectForId(1), static_cast<QObject*>(0));
168
169     QObject *obj = new QObject;
170     int id = QDeclarativeDebugService::idForObject(obj);
171     QCOMPARE(QDeclarativeDebugService::objectForId(id), obj);
172
173     delete obj;
174     QCOMPARE(QDeclarativeDebugService::objectForId(id), static_cast<QObject*>(0));
175 }
176
177 void tst_QDeclarativeDebugService::objectToString()
178 {
179     QCOMPARE(QDeclarativeDebugService::objectToString(0), QString("NULL"));
180
181     QObject *obj = new QObject;
182     QCOMPARE(QDeclarativeDebugService::objectToString(obj), QString("QObject: <unnamed>"));
183
184     obj->setObjectName("Hello");
185     QCOMPARE(QDeclarativeDebugService::objectToString(obj), QString("QObject: Hello"));
186     delete obj;
187 }
188
189
190 int main(int argc, char *argv[])
191 {
192     int _argc = argc + 1;
193     char **_argv = new char*[_argc];
194     for (int i = 0; i < argc; ++i)
195         _argv[i] = argv[i];
196     _argv[_argc - 1] = "-qmljsdebugger=port:13769";
197
198     QApplication app(_argc, _argv);
199     tst_QDeclarativeDebugService tc;
200     return QTest::qExec(&tc, _argc, _argv);
201     delete _argv;
202 }
203
204 #include "tst_qdeclarativedebugservice.moc"