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