Initial import from the monolithic Qt.
[profile/ivi/qtdeclarative.git] / tests / auto / declarative / qdeclarativedebugclient / tst_qdeclarativedebugclient.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
50 #include <private/qdeclarativedebug_p.h>
51 #include <private/qdeclarativeenginedebug_p.h>
52 #include <private/qdeclarativedebugclient_p.h>
53 #include <private/qdeclarativedebugservice_p.h>
54 #include <private/qdeclarativedebughelper_p.h>
55
56 #include "../../../shared/util.h"
57 #include "../shared/debugutil_p.h"
58
59 class tst_QDeclarativeDebugClient : public QObject
60 {
61     Q_OBJECT
62
63 private:
64     QDeclarativeDebugConnection *m_conn;
65
66 private slots:
67     void initTestCase();
68
69     void name();
70     void status();
71     void sendMessage();
72 };
73
74 void tst_QDeclarativeDebugClient::initTestCase()
75 {
76     QTest::ignoreMessage(QtWarningMsg, "Qml debugging is enabled. Only use this in a safe environment!");
77     QDeclarativeDebugHelper::enableDebugging();
78
79     QTest::ignoreMessage(QtWarningMsg, "QDeclarativeDebugServer: Waiting for connection on port 13770...");
80     new QDeclarativeEngine(this);
81
82     m_conn = new QDeclarativeDebugConnection(this);
83
84     QDeclarativeDebugTestClient client("tst_QDeclarativeDebugClient::handshake()", m_conn);
85     QDeclarativeDebugTestService service("tst_QDeclarativeDebugClient::handshake()");
86
87     m_conn->connectToHost("127.0.0.1", 13770);
88
89     QTest::ignoreMessage(QtWarningMsg, "QDeclarativeDebugServer: Connection established");
90     bool ok = m_conn->waitForConnected();
91     Q_ASSERT(ok);
92
93     QTRY_VERIFY(QDeclarativeDebugService::hasDebuggingClient());
94     QTRY_COMPARE(client.status(), QDeclarativeDebugClient::Enabled);
95 }
96
97 void tst_QDeclarativeDebugClient::name()
98 {
99     QString name = "tst_QDeclarativeDebugClient::name()";
100
101     QDeclarativeDebugClient client(name, m_conn);
102     QCOMPARE(client.name(), name);
103 }
104
105 void tst_QDeclarativeDebugClient::status()
106 {
107     {
108         QDeclarativeDebugConnection dummyConn;
109         QDeclarativeDebugClient client("tst_QDeclarativeDebugClient::status()", &dummyConn);
110         QCOMPARE(client.status(), QDeclarativeDebugClient::NotConnected);
111     }
112
113     QDeclarativeDebugTestClient client("tst_QDeclarativeDebugClient::status()", m_conn);
114     QCOMPARE(client.status(), QDeclarativeDebugClient::Unavailable);
115
116     {
117         QDeclarativeDebugTestService service("tst_QDeclarativeDebugClient::status()");
118         QTRY_COMPARE(client.status(), QDeclarativeDebugClient::Enabled);
119     }
120
121     QTRY_COMPARE(client.status(), QDeclarativeDebugClient::Unavailable);
122
123     // duplicate plugin name
124     QTest::ignoreMessage(QtWarningMsg, "QDeclarativeDebugClient: Conflicting plugin name \"tst_QDeclarativeDebugClient::status()\" ");
125     QDeclarativeDebugClient client2("tst_QDeclarativeDebugClient::status()", m_conn);
126     QCOMPARE(client2.status(), QDeclarativeDebugClient::NotConnected);
127
128     QDeclarativeDebugClient client3("tst_QDeclarativeDebugClient::status3()", 0);
129     QCOMPARE(client3.status(), QDeclarativeDebugClient::NotConnected);
130 }
131
132 void tst_QDeclarativeDebugClient::sendMessage()
133 {
134     QDeclarativeDebugTestService service("tst_QDeclarativeDebugClient::sendMessage()");
135     QDeclarativeDebugTestClient client("tst_QDeclarativeDebugClient::sendMessage()", m_conn);
136
137     QByteArray msg = "hello!";
138
139     QTRY_COMPARE(client.status(), QDeclarativeDebugClient::Enabled);
140
141     client.sendMessage(msg);
142     QByteArray resp = client.waitForResponse();
143     QCOMPARE(resp, msg);
144 }
145
146 int main(int argc, char *argv[])
147 {
148     int _argc = argc + 1;
149     char **_argv = new char*[_argc];
150     for (int i = 0; i < argc; ++i)
151         _argv[i] = argv[i];
152     _argv[_argc - 1] = "-qmljsdebugger=port:13770";
153
154     QApplication app(_argc, _argv);
155     tst_QDeclarativeDebugClient tc;
156     return QTest::qExec(&tc, _argc, _argv);
157     delete _argv;
158 }
159
160 #include "tst_qdeclarativedebugclient.moc"
161