From 1424702918720576792da713cd3b8fee485775cf Mon Sep 17 00:00:00 2001 From: Shane Kearns Date: Tue, 10 Apr 2012 13:14:51 +0100 Subject: [PATCH] Add unit test for BackgroundRequestAttribute Change-Id: I807953cac3d23825461f9ae860badbd148835330 Reviewed-by: Martin Petersson --- .../qnetworkreply/tst_qnetworkreply.cpp | 48 ++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/tests/auto/network/access/qnetworkreply/tst_qnetworkreply.cpp b/tests/auto/network/access/qnetworkreply/tst_qnetworkreply.cpp index 185c3eedd8..2d784fcbbc 100644 --- a/tests/auto/network/access/qnetworkreply/tst_qnetworkreply.cpp +++ b/tests/auto/network/access/qnetworkreply/tst_qnetworkreply.cpp @@ -75,6 +75,7 @@ #include #include #include +#include #endif #ifdef QT_BUILD_INTERNAL #include @@ -402,6 +403,9 @@ private Q_SLOTS: void ftpAuthentication_data(); void ftpAuthentication(); + void backgroundRequest_data(); + void backgroundRequest(); + // NOTE: This test must be last! void parentingRepliesToTheApp(); private: @@ -6778,7 +6782,6 @@ void tst_QNetworkReply::closeDuringDownload() QTest::qWait(1000); //cancelling ftp takes some time, this avoids a warning caused by test's cleanup() destroying the connection cache before the abort is finished } - void tst_QNetworkReply::ftpAuthentication_data() { QTest::addColumn("referenceName"); @@ -6806,6 +6809,49 @@ void tst_QNetworkReply::ftpAuthentication() QCOMPARE(reply->error(), QNetworkReply::NetworkError(error)); } +void tst_QNetworkReply::backgroundRequest_data() +{ + QTest::addColumn("background"); + QTest::addColumn("policy"); + QTest::addColumn("error"); + + QTest::newRow("fg, normal") << false << 0 << QNetworkReply::NoError; + QTest::newRow("bg, normal") << true << 0 << QNetworkReply::NoError; + QTest::newRow("fg, nobg") << false << (int)QNetworkSession::NoBackgroundTrafficPolicy << QNetworkReply::NoError; + QTest::newRow("bg, nobg") << true << (int)QNetworkSession::NoBackgroundTrafficPolicy << QNetworkReply::BackgroundRequestNotAllowedError; + +} + +void tst_QNetworkReply::backgroundRequest() +{ +#ifdef QT_BUILD_INTERNAL + QFETCH(bool, background); + QFETCH(int, policy); + QFETCH(QNetworkReply::NetworkError, error); + + QNetworkRequest request(QUrl("http://" + QtNetworkSettings::serverName())); + + if (background) + request.setAttribute(QNetworkRequest::BackgroundRequestAttribute, QVariant::fromValue(true)); + + //this preconstructs the session so we can change policies in advance + manager.setConfiguration(networkConfiguration); + + const QWeakPointer session = QNetworkAccessManagerPrivate::getNetworkSession(&manager); + QVERIFY(session); + QNetworkSession::UsagePolicies original = session.data()->usagePolicies(); + QNetworkSessionPrivate::setUsagePolicies(*const_cast(session.data()), QNetworkSession::UsagePolicies(policy)); + + QNetworkReplyPtr reply(manager.get(request)); + + QVERIFY(waitForFinish(reply) != Timeout); + if (session) + QNetworkSessionPrivate::setUsagePolicies(*const_cast(session.data()), original); + + QVERIFY(reply->isFinished()); + QCOMPARE(reply->error(), error); +#endif +} // NOTE: This test must be last testcase in tst_qnetworkreply! void tst_QNetworkReply::parentingRepliesToTheApp()