Initial import from the monolithic Qt.
[profile/ivi/qtdeclarative.git] / tests / auto / declarative / qpacketprotocol / tst_qpacketprotocol.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 <QTcpSocket>
45 #include <QTcpServer>
46 #include <QDebug>
47 #include <QBuffer>
48
49 #include <private/qpacketprotocol_p.h>
50
51 #include "../shared/debugutil_p.h"
52
53 class tst_QPacketProtocol : public QObject
54 {
55     Q_OBJECT
56
57 private:
58     QTcpServer *m_server;
59     QTcpSocket *m_client;
60     QTcpSocket *m_serverConn;
61
62 private slots:
63     void init();
64     void cleanup();
65
66     void maximumPacketSize();
67     void setMaximumPacketSize();
68     void setMaximumPacketSize_data();
69     void send();
70     void send_data();
71     void packetsAvailable();
72     void packetsAvailable_data();
73     void clear();
74     void read();
75     void device();
76
77     void tst_QPacket_clear();
78 };
79
80 void tst_QPacketProtocol::init()
81 {
82     m_server = new QTcpServer(this);
83     m_serverConn = 0;
84     QVERIFY(m_server->listen(QHostAddress("127.0.0.1")));
85         
86     m_client = new QTcpSocket(this);
87     m_client->connectToHost(m_server->serverAddress(), m_server->serverPort());
88         
89     QVERIFY(m_client->waitForConnected());
90     QVERIFY(m_server->waitForNewConnection());
91     m_serverConn = m_server->nextPendingConnection();
92 }
93
94 void tst_QPacketProtocol::cleanup()
95 {
96     delete m_client;
97     delete m_serverConn;
98     delete m_server;
99 }
100
101 void tst_QPacketProtocol::maximumPacketSize()
102 {
103     QPacketProtocol p(m_client);
104     QCOMPARE(p.maximumPacketSize(), 0x7FFFFFFF);
105 }
106
107 void tst_QPacketProtocol::setMaximumPacketSize()
108 {
109     QFETCH(qint32, size);
110     QFETCH(qint32, expected);
111
112     QPacketProtocol out(m_serverConn);
113     QCOMPARE(out.setMaximumPacketSize(size), expected);
114 }
115
116 void tst_QPacketProtocol::setMaximumPacketSize_data()
117 {
118     QTest::addColumn<int>("size");
119     QTest::addColumn<int>("expected");
120
121     QTest::newRow("invalid") << qint32(sizeof(qint32) - 1) << qint32(0x7FFFFFFF);
122     QTest::newRow("still invalid") << qint32(sizeof(qint32)) << qint32(0x7FFFFFFF);
123     QTest::newRow("valid") << qint32(sizeof(qint32) + 1) << qint32(sizeof(qint32) + 1);
124 }
125
126 void tst_QPacketProtocol::send()
127 {
128     QFETCH(bool, useAutoSend);
129
130     QPacketProtocol in(m_client);
131     QPacketProtocol out(m_serverConn);
132
133     QByteArray ba;
134     int num;
135
136     if (useAutoSend) {
137         out.send() << "Hello world" << 123;
138     } else {
139         QPacket packet;
140         packet << "Hello world" << 123;
141         out.send(packet);
142     }
143
144     QVERIFY(QDeclarativeDebugTest::waitForSignal(&in, SIGNAL(readyRead())));
145
146     QPacket p = in.read();
147     p >> ba >> num;
148     QCOMPARE(ba, QByteArray("Hello world") + '\0');
149     QCOMPARE(num, 123);
150 }
151
152 void tst_QPacketProtocol::send_data()
153 {
154     QTest::addColumn<bool>("useAutoSend");
155
156     QTest::newRow("auto send") << true;
157     QTest::newRow("no auto send") << false;
158 }
159
160 void tst_QPacketProtocol::packetsAvailable()
161 {
162     QFETCH(int, packetCount);
163
164     QPacketProtocol out(m_client);
165     QPacketProtocol in(m_serverConn);
166
167     QCOMPARE(out.packetsAvailable(), qint64(0));
168     QCOMPARE(in.packetsAvailable(), qint64(0));
169
170     for (int i=0; i<packetCount; i++)
171         out.send() << "Hello";
172
173     QVERIFY(QDeclarativeDebugTest::waitForSignal(&in, SIGNAL(readyRead())));
174     QCOMPARE(in.packetsAvailable(), qint64(packetCount));
175 }
176
177 void tst_QPacketProtocol::packetsAvailable_data()
178 {
179     QTest::addColumn<int>("packetCount");
180
181     QTest::newRow("1") << 1;
182     QTest::newRow("2") << 2;
183     QTest::newRow("10") << 10;
184 }
185
186 void tst_QPacketProtocol::clear()
187 {
188     QPacketProtocol in(m_client);
189     QPacketProtocol out(m_serverConn);
190
191     out.send() << 123;
192     out.send() << 456;
193     QVERIFY(QDeclarativeDebugTest::waitForSignal(&in, SIGNAL(readyRead())));
194
195     in.clear();
196     QVERIFY(in.read().isEmpty());
197 }
198
199 void tst_QPacketProtocol::read()
200 {
201     QPacketProtocol in(m_client);
202     QPacketProtocol out(m_serverConn);
203
204     QVERIFY(in.read().isEmpty());
205
206     out.send() << 123;
207     out.send() << 456;
208     QVERIFY(QDeclarativeDebugTest::waitForSignal(&in, SIGNAL(readyRead())));
209
210     int num;
211
212     QPacket p1 = in.read();
213     QVERIFY(!p1.isEmpty());
214     p1 >> num;
215     QCOMPARE(num, 123);
216
217     QPacket p2 = in.read();
218     QVERIFY(!p2.isEmpty());
219     p2 >> num;
220     QCOMPARE(num, 456);
221
222     QVERIFY(in.read().isEmpty());
223 }
224
225 void tst_QPacketProtocol::device()
226 {
227     QPacketProtocol p(m_client);
228     QVERIFY(p.device() == m_client);
229 }
230
231 void tst_QPacketProtocol::tst_QPacket_clear()
232 {
233     QPacketProtocol protocol(m_client);
234
235     QPacket packet;
236
237     packet << "Hello world!" << 123;
238     protocol.send(packet);
239
240     packet.clear();
241     QVERIFY(packet.isEmpty());
242     packet << "Goodbyte world!" << 789;
243     protocol.send(packet);
244
245     QByteArray ba;
246     int num;
247     QPacketProtocol in(m_serverConn);
248     QVERIFY(QDeclarativeDebugTest::waitForSignal(&in, SIGNAL(readyRead())));
249
250     QPacket p1 = in.read();
251     p1 >> ba >> num;
252     QCOMPARE(ba, QByteArray("Hello world!") + '\0');
253     QCOMPARE(num, 123);
254
255     QPacket p2 = in.read();
256     p2 >> ba >> num;
257     QCOMPARE(ba, QByteArray("Goodbyte world!") + '\0');
258     QCOMPARE(num, 789);
259 }
260
261 QTEST_MAIN(tst_QPacketProtocol)
262
263 #include "tst_qpacketprotocol.moc"