Remove copy of tests/shared/util.h.
[profile/ivi/qtdeclarative.git] / tests / auto / declarative / debugger / 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 ** 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 "../shared/debugutil_p.h"
51
52 #define PORT 13770
53 #define STR_PORT "13770"
54
55 class tst_QDeclarativeDebugClient : public QObject
56 {
57     Q_OBJECT
58
59 private:
60     QDeclarativeDebugConnection *m_conn;
61
62 private slots:
63     void initTestCase();
64
65     void name();
66     void status();
67     void sendMessage();
68     void parallelConnect();
69     void sequentialConnect();
70 };
71
72 void tst_QDeclarativeDebugClient::initTestCase()
73 {
74     const QString waitingMsg = QString("QDeclarativeDebugServer: Waiting for connection on port %1...").arg(PORT);
75     QTest::ignoreMessage(QtWarningMsg, waitingMsg.toAscii().constData());
76     new QDeclarativeEngine(this);
77
78     m_conn = new QDeclarativeDebugConnection(this);
79
80     QDeclarativeDebugTestClient client("tst_QDeclarativeDebugClient::handshake()", m_conn);
81     QDeclarativeDebugTestService service("tst_QDeclarativeDebugClient::handshake()");
82
83     m_conn->connectToHost("127.0.0.1", PORT);
84
85     QTest::ignoreMessage(QtWarningMsg, "QDeclarativeDebugServer: Connection established");
86     bool ok = m_conn->waitForConnected();
87     QVERIFY(ok);
88
89     QTRY_VERIFY(QDeclarativeDebugService::hasDebuggingClient());
90     QTRY_COMPARE(client.status(), QDeclarativeDebugClient::Enabled);
91 }
92
93 void tst_QDeclarativeDebugClient::name()
94 {
95     QString name = "tst_QDeclarativeDebugClient::name()";
96
97     QDeclarativeDebugClient client(name, m_conn);
98     QCOMPARE(client.name(), name);
99 }
100
101 void tst_QDeclarativeDebugClient::status()
102 {
103     {
104         QDeclarativeDebugConnection dummyConn;
105         QDeclarativeDebugClient client("tst_QDeclarativeDebugClient::status()", &dummyConn);
106         QCOMPARE(client.status(), QDeclarativeDebugClient::NotConnected);
107     }
108
109     QDeclarativeDebugTestClient client("tst_QDeclarativeDebugClient::status()", m_conn);
110     QCOMPARE(client.status(), QDeclarativeDebugClient::Unavailable);
111
112     {
113         QDeclarativeDebugTestService service("tst_QDeclarativeDebugClient::status()");
114         QTRY_COMPARE(client.status(), QDeclarativeDebugClient::Enabled);
115     }
116
117     QTRY_COMPARE(client.status(), QDeclarativeDebugClient::Unavailable);
118
119     // duplicate plugin name
120     QTest::ignoreMessage(QtWarningMsg, "QDeclarativeDebugClient: Conflicting plugin name \"tst_QDeclarativeDebugClient::status()\" ");
121     QDeclarativeDebugClient client2("tst_QDeclarativeDebugClient::status()", m_conn);
122     QCOMPARE(client2.status(), QDeclarativeDebugClient::NotConnected);
123
124     QDeclarativeDebugClient client3("tst_QDeclarativeDebugClient::status3()", 0);
125     QCOMPARE(client3.status(), QDeclarativeDebugClient::NotConnected);
126 }
127
128 void tst_QDeclarativeDebugClient::sendMessage()
129 {
130     QDeclarativeDebugTestService service("tst_QDeclarativeDebugClient::sendMessage()");
131     QDeclarativeDebugTestClient client("tst_QDeclarativeDebugClient::sendMessage()", m_conn);
132
133     QByteArray msg = "hello!";
134
135     QTRY_COMPARE(client.status(), QDeclarativeDebugClient::Enabled);
136
137     client.sendMessage(msg);
138     QByteArray resp = client.waitForResponse();
139     QCOMPARE(resp, msg);
140 }
141
142 void tst_QDeclarativeDebugClient::parallelConnect()
143 {
144     QDeclarativeDebugConnection connection2;
145
146     connection2.connectToHost("127.0.0.1", PORT);
147     QTest::ignoreMessage(QtWarningMsg, "QDeclarativeDebugServer: Another client is already connected");
148     // will connect & immediately disconnect
149     QVERIFY(connection2.waitForConnected());
150     QTRY_COMPARE(connection2.state(), QAbstractSocket::UnconnectedState);
151     QVERIFY(m_conn->isConnected());
152 }
153
154 void tst_QDeclarativeDebugClient::sequentialConnect()
155 {
156     QDeclarativeDebugConnection connection2;
157     QDeclarativeDebugTestClient client2("tst_QDeclarativeDebugClient::handshake()", &connection2);
158     QDeclarativeDebugTestService service("tst_QDeclarativeDebugClient::handshake()");
159
160     m_conn->close();
161     QVERIFY(!m_conn->isConnected());
162     QCOMPARE(m_conn->state(), QAbstractSocket::UnconnectedState);
163
164     // Make sure that the disconnect is actually delivered to the server
165     QGuiApplication::processEvents();
166
167     connection2.connectToHost("127.0.0.1", PORT);
168     QTest::ignoreMessage(QtWarningMsg, "QDeclarativeDebugServer: Connection established");
169     QVERIFY(connection2.waitForConnected());
170     QVERIFY(connection2.isConnected());
171     QTRY_VERIFY(client2.status() == QDeclarativeDebugClient::Enabled);
172 }
173
174 int main(int argc, char *argv[])
175 {
176     int _argc = argc + 1;
177     char **_argv = new char*[_argc];
178     for (int i = 0; i < argc; ++i)
179         _argv[i] = argv[i];
180
181     _argv[_argc - 1] = "-qmljsdebugger=port:" STR_PORT;
182
183     QGuiApplication app(_argc, _argv);
184     tst_QDeclarativeDebugClient tc;
185     return QTest::qExec(&tc, _argc, _argv);
186     delete _argv;
187 }
188
189 #include "tst_qdeclarativedebugclient.moc"
190