From dd9bdf5654a02c8bcafd7b26ceb652bd737d9cd5 Mon Sep 17 00:00:00 2001 From: Shane Kearns Date: Thu, 31 May 2012 18:27:23 +0100 Subject: Rewrite QNetworkReply downloadProgress autotest 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 --- .../access/qnetworkreply/tst_qnetworkreply.cpp | 110 +++++++++++---------- 1 file changed, 56 insertions(+), 54 deletions(-) diff --git a/tests/auto/network/access/qnetworkreply/tst_qnetworkreply.cpp b/tests/auto/network/access/qnetworkreply/tst_qnetworkreply.cpp index 2226f99432..ae95b18b7b 100644 --- a/tests/auto/network/access/qnetworkreply/tst_qnetworkreply.cpp +++ b/tests/auto/network/access/qnetworkreply/tst_qnetworkreply.cpp @@ -4745,71 +4745,73 @@ void tst_QNetworkReply::rateControl() void tst_QNetworkReply::downloadProgress_data() { - QTest::addColumn("loopCount"); + QTest::addColumn("url"); + QTest::addColumn("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 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 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 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() -- cgit v1.2.3