Remove "All rights reserved" line from license headers.
[profile/ivi/qtdeclarative.git] / tests / auto / declarative / debugger / qdeclarativedebugservice / tst_qdeclarativedebugservice.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
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.
16 **
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.
20 **
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.
28 **
29 ** Other Usage
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.
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
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 version();
70     void status();
71     void sendMessage();
72     void idForObject();
73     void objectForId();
74     void objectToString();
75 };
76
77 void tst_QDeclarativeDebugService::initTestCase()
78 {
79     const QString waitingMsg = QString("QDeclarativeDebugServer: Waiting for connection on port %1...").arg(PORT);
80     QTest::ignoreMessage(QtWarningMsg, waitingMsg.toAscii().constData());
81     new QDeclarativeEngine(this);
82
83     m_conn = new QDeclarativeDebugConnection(this);
84
85
86     QTest::ignoreMessage(QtWarningMsg, "QDeclarativeDebugServer: 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())
91             break;
92         QTest::qSleep(100);
93     }
94     QVERIFY(m_conn->isConnected());
95
96     QTRY_VERIFY(QDeclarativeDebugService::hasDebuggingClient());
97 }
98
99 void tst_QDeclarativeDebugService::name()
100 {
101     QString name = "tst_QDeclarativeDebugService::name()";
102
103     QDeclarativeDebugService service(name, 1);
104     QCOMPARE(service.name(), name);
105 }
106
107 void tst_QDeclarativeDebugService::version()
108 {
109     QString name = "tst_QDeclarativeDebugService::name()";
110
111     QDeclarativeDebugService service(name, 2);
112     QCOMPARE(service.version(), 2.0f);
113 }
114
115 void tst_QDeclarativeDebugService::status()
116 {
117     QDeclarativeDebugTestService service("tst_QDeclarativeDebugService::status()");
118     QCOMPARE(service.status(), QDeclarativeDebugService::Unavailable);
119
120     {
121         QDeclarativeDebugTestClient client("tst_QDeclarativeDebugService::status()", m_conn);
122         QTRY_COMPARE(client.status(), QDeclarativeDebugClient::Enabled);
123         QTRY_COMPARE(service.status(), QDeclarativeDebugService::Enabled);
124     }
125
126
127     QTRY_COMPARE(service.status(), QDeclarativeDebugService::Unavailable);
128
129     QTest::ignoreMessage(QtWarningMsg, "QDeclarativeDebugService: Conflicting plugin name \"tst_QDeclarativeDebugService::status()\" ");
130     QDeclarativeDebugTestService duplicate("tst_QDeclarativeDebugService::status()");
131     QCOMPARE(duplicate.status(), QDeclarativeDebugService::NotConnected);
132 }
133
134 void tst_QDeclarativeDebugService::sendMessage()
135 {
136     QDeclarativeDebugTestService service("tst_QDeclarativeDebugService::sendMessage()");
137     QDeclarativeDebugTestClient client("tst_QDeclarativeDebugService::sendMessage()", m_conn);
138
139     QByteArray msg = "hello!";
140
141     QTRY_COMPARE(client.status(), QDeclarativeDebugClient::Enabled);
142     QTRY_COMPARE(service.status(), QDeclarativeDebugService::Enabled);
143
144     client.sendMessage(msg);
145     QByteArray resp = client.waitForResponse();
146     QCOMPARE(resp, msg);
147
148     QTest::ignoreMessage(QtWarningMsg, "QDeclarativeDebugService: Conflicting plugin name \"tst_QDeclarativeDebugService::sendMessage()\" ");
149     QDeclarativeDebugTestService duplicate("tst_QDeclarativeDebugService::sendMessage()");
150     duplicate.sendMessage("msg");
151 }
152
153 void tst_QDeclarativeDebugService::idForObject()
154 {
155     QCOMPARE(QDeclarativeDebugService::idForObject(0), -1);
156
157     QObject *objA = new QObject;
158
159     int idA = QDeclarativeDebugService::idForObject(objA);
160     QVERIFY(idA >= 0);
161     QCOMPARE(QDeclarativeDebugService::objectForId(idA), objA);
162
163     int idAA = QDeclarativeDebugService::idForObject(objA);
164     QCOMPARE(idAA, idA);
165
166     QObject *objB = new QObject;
167     int idB = QDeclarativeDebugService::idForObject(objB);
168     QVERIFY(idB != idA);
169     QCOMPARE(QDeclarativeDebugService::objectForId(idB), objB);
170
171     delete objA;
172     delete objB;
173 }
174
175 void tst_QDeclarativeDebugService::objectForId()
176 {
177     QCOMPARE(QDeclarativeDebugService::objectForId(-1), static_cast<QObject*>(0));
178     QCOMPARE(QDeclarativeDebugService::objectForId(1), static_cast<QObject*>(0));
179
180     QObject *obj = new QObject;
181     int id = QDeclarativeDebugService::idForObject(obj);
182     QCOMPARE(QDeclarativeDebugService::objectForId(id), obj);
183
184     delete obj;
185     QCOMPARE(QDeclarativeDebugService::objectForId(id), static_cast<QObject*>(0));
186 }
187
188 void tst_QDeclarativeDebugService::objectToString()
189 {
190     QCOMPARE(QDeclarativeDebugService::objectToString(0), QString("NULL"));
191
192     QObject *obj = new QObject;
193     QCOMPARE(QDeclarativeDebugService::objectToString(obj), QString("QObject: <unnamed>"));
194
195     obj->setObjectName("Hello");
196     QCOMPARE(QDeclarativeDebugService::objectToString(obj), QString("QObject: Hello"));
197     delete obj;
198 }
199
200
201 int main(int argc, char *argv[])
202 {
203     int _argc = argc + 1;
204     char **_argv = new char*[_argc];
205     for (int i = 0; i < argc; ++i)
206         _argv[i] = argv[i];
207     char arg[] = "-qmljsdebugger=port:" STR_PORT;
208     _argv[_argc - 1] = arg;
209
210     QGuiApplication app(_argc, _argv);
211     tst_QDeclarativeDebugService tc;
212     return QTest::qExec(&tc, _argc, _argv);
213     delete _argv;
214 }
215
216 #include "tst_qdeclarativedebugservice.moc"