Rewrite QNetworkReply downloadProgress autotest
authorShane Kearns <ext-shane.2.kearns@nokia.com>
Thu, 31 May 2012 17:27:23 +0000 (18:27 +0100)
committerQt by Nokia <qt-info@nokia.com>
Thu, 14 Jun 2012 03:26:39 +0000 (05:26 +0200)
The existing autotest was made invalid by the downloadProgress
signal choking patch.
Rewrote the autotest to download files from the test server
with some rate limiting applied to ensure more than one signal
is emitted.

Change-Id: I6026bacdf356b4e1796b80f6983e5bdce0d1bfce
Reviewed-by: Martin Petersson <Martin.Petersson@nokia.com>
tests/auto/network/access/qnetworkreply/tst_qnetworkreply.cpp

index 2226f99..ae95b18 100644 (file)
@@ -4745,71 +4745,73 @@ void tst_QNetworkReply::rateControl()
 
 void tst_QNetworkReply::downloadProgress_data()
 {
-    QTest::addColumn<int>("loopCount");
+    QTest::addColumn<QUrl>("url");
+    QTest::addColumn<int>("expectedSize");
 
-    QTest::newRow("empty") << 0;
-    QTest::newRow("small") << 4;
-    QTest::newRow("big") << 4096;
+    QTest::newRow("empty") << QUrl::fromLocalFile(QFINDTESTDATA("empty")) << 0;
+    QTest::newRow("http:small") << QUrl("http://" + QtNetworkSettings::serverName() + "/qtest/rfc3252.txt") << 25962;
+    QTest::newRow("http:big") << QUrl("http://" + QtNetworkSettings::serverName() + "/qtest/bigfile") << 519240;
+    QTest::newRow("http:no-length") << QUrl("http://" + QtNetworkSettings::serverName() + "/qtest/deflate/rfc2616.html") << -1;
+    QTest::newRow("ftp:small") << QUrl("http://" + QtNetworkSettings::serverName() + "/qtest/rfc3252.txt") << 25962;
+    QTest::newRow("ftp:big") << QUrl("http://" + QtNetworkSettings::serverName() + "/qtest/bigfile") << 519240;
 }
 
-void tst_QNetworkReply::downloadProgress()
+class SlowReader : public QObject
 {
-#if !defined(QT_BUILD_INTERNAL)
-    QSKIP("backend for testing not available!");
-#endif
-    QTcpServer server;
-    QVERIFY(server.listen());
-
-    QNetworkRequest request("debugpipe://127.0.0.1:" + QString::number(server.serverPort()) + "/?bare=1");
-    QNetworkReplyPtr reply(manager.get(request));
-    QSignalSpy spy(reply.data(), SIGNAL(downloadProgress(qint64,qint64)));
-    connect(reply, SIGNAL(downloadProgress(qint64,qint64)),
-            &QTestEventLoop::instance(), SLOT(exitLoop()));
-    QVERIFY(spy.isValid());
-    QVERIFY(!reply->isFinished());
-    QVERIFY(reply->isRunning());
-
-    QCoreApplication::instance()->processEvents();
-    if (!server.hasPendingConnections())
-        server.waitForNewConnection(1000);
-    QVERIFY(server.hasPendingConnections());
-    QCOMPARE(spy.count(), 0);
-
-    QByteArray data(128, 'a');
-    QTcpSocket *sender = server.nextPendingConnection();
-    QVERIFY(sender);
-
-    QFETCH(int, loopCount);
-    for (int i = 1; i <= loopCount; ++i) {
-        sender->write(data);
-        QVERIFY2(sender->waitForBytesWritten(2000), "Network timeout");
-
-        spy.clear();
-        QTestEventLoop::instance().enterLoop(2);
-        QVERIFY(!QTestEventLoop::instance().timeout());
-        QVERIFY(spy.count() > 0);
-        QVERIFY(!reply->isFinished());
-        QVERIFY(reply->isRunning());
+    Q_OBJECT
+public:
+    SlowReader(QIODevice *dev)
+        : device(dev)
+    {
+        connect(device, SIGNAL(readyRead()), this, SLOT(deviceReady()));
+    }
+private slots:
+    void deviceReady()
+    {
+        QTimer::singleShot(100, this, SLOT(doRead()));
+    }
 
-        QList<QVariant> args = spy.last();
-        QCOMPARE(args.at(0).toInt(), i*data.size());
-        QCOMPARE(args.at(1).toInt(), -1);
+    void doRead()
+    {
+        device->readAll();
     }
+private:
+    QIODevice *device;
+};
 
-    // close the connection:
-    delete sender;
+void tst_QNetworkReply::downloadProgress()
+{
+    QFETCH(QUrl, url);
+    QFETCH(int, expectedSize);
 
-    spy.clear();
-    QTestEventLoop::instance().enterLoop(2);
-    QCOMPARE(reply->error(), QNetworkReply::NoError);
+    QNetworkRequest request(url);
+    QNetworkReplyPtr reply(manager.get(request));
+    //artificially slow down the test, otherwise only the final signal is emitted
+    reply->setReadBufferSize(qMax(20000, expectedSize / 4));
+    SlowReader reader(reply.data());
+    QSignalSpy spy(reply.data(), SIGNAL(downloadProgress(qint64,qint64)));
+    connect(reply, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop()));
+    QTestEventLoop::instance().enterLoop(30);
     QVERIFY(!QTestEventLoop::instance().timeout());
-    QVERIFY(spy.count() > 0);
-    QVERIFY(!reply->isRunning());
     QVERIFY(reply->isFinished());
 
-    QList<QVariant> args = spy.last();
-    QCOMPARE(args.at(0).toInt(), loopCount * data.size());
-    QCOMPARE(args.at(1).toInt(), loopCount * data.size());
+    QVERIFY(spy.count() > 0);
+
+    //final progress should have equal current & total
+    QList<QVariant> args = spy.takeLast();
+    QCOMPARE(args.at(0).toInt(), args.at(1).toInt());
+
+    qint64 current = 0;
+    //intermediate progress ascending and has expected total
+    while (!spy.isEmpty()) {
+        args = spy.takeFirst();
+        QVERIFY(args.at(0).toInt() >= current);
+        if (expectedSize >=0)
+            QCOMPARE(args.at(1).toInt(), expectedSize);
+        else
+            QVERIFY(args.at(1).toInt() == expectedSize || args.at(1).toInt() == args.at(0).toInt());
+        current = args.at(0).toInt();
+    }
 }
 
 void tst_QNetworkReply::uploadProgress_data()