From a6156d1aedaba720fe4618828609dbae931af313 Mon Sep 17 00:00:00 2001 From: Shane Kearns Date: Mon, 16 Apr 2012 14:02:02 +0100 Subject: [PATCH] Add test case for checking ConnectInBackground is set ConnectInBackground should be set when opening a network session due to a background request. This test checks that. Unfortunately, none of the bearer plugins currently in Qt Base support this attribute, so the test result is inconclusive. (testing with a debugger shows the attribute is set correctly, but it can't be read back as the set is discarded by the plugin implementation) Change-Id: Idcf777fe489a62d4ff5007ffd291a84ba052311b Reviewed-by: Martin Petersson --- .../access/qnetworkreply/tst_qnetworkreply.cpp | 65 +++++++++++++++++++++- 1 file changed, 64 insertions(+), 1 deletion(-) diff --git a/tests/auto/network/access/qnetworkreply/tst_qnetworkreply.cpp b/tests/auto/network/access/qnetworkreply/tst_qnetworkreply.cpp index aede0d2..726e986 100644 --- a/tests/auto/network/access/qnetworkreply/tst_qnetworkreply.cpp +++ b/tests/auto/network/access/qnetworkreply/tst_qnetworkreply.cpp @@ -407,7 +407,8 @@ private Q_SLOTS: void backgroundRequest(); void backgroundRequestInterruption_data(); void backgroundRequestInterruption(); - + void backgroundRequestConnectInBackground_data(); + void backgroundRequestConnectInBackground(); // NOTE: This test must be last! void parentingRepliesToTheApp(); @@ -6956,6 +6957,68 @@ void tst_QNetworkReply::backgroundRequestInterruption() #endif } +void tst_QNetworkReply::backgroundRequestConnectInBackground_data() +{ + QTest::addColumn("url"); + QTest::addColumn("background"); + + QUrl httpurl("http://" + QtNetworkSettings::serverName()); + QUrl ftpurl("ftp://" + QtNetworkSettings::serverName() + "/qtest/rfc3252.txt"); + + QTest::newRow("http, fg") << httpurl << false; + QTest::newRow("http, bg") << httpurl << true; + + QTest::newRow("ftp, fg") << ftpurl << false; + QTest::newRow("ftp, bg") << ftpurl << true; +} + +//test purpose: check that backgroundness is propagated to the network session +void tst_QNetworkReply::backgroundRequestConnectInBackground() +{ +#ifdef QT_BUILD_INTERNAL +#ifndef QT_NO_BEARERMANAGEMENT + QFETCH(QUrl, url); + QFETCH(bool, background); + + QNetworkRequest request(url); + + if (background) + request.setAttribute(QNetworkRequest::BackgroundRequestAttribute, QVariant::fromValue(true)); + + QWeakPointer session = QNetworkAccessManagerPrivate::getNetworkSession(&manager); + //force QNAM to reopen the session. + if (session && session.data()->isOpen()) { + const_cast(session.data())->close(); + QCoreApplication::processEvents(); //let signals propagate inside QNAM + } + + //this preconstructs the session so we can change policies in advance + manager.setConfiguration(networkConfiguration); + + session = QNetworkAccessManagerPrivate::getNetworkSession(&manager); + QVERIFY(session); + QNetworkSession::UsagePolicies original = session.data()->usagePolicies(); + QNetworkSessionPrivate::setUsagePolicies(*const_cast(session.data()), QNetworkSession::NoPolicy); + + QNetworkReplyPtr reply(manager.get(request)); + + QVERIFY(waitForFinish(reply) != Timeout); + session = QNetworkAccessManagerPrivate::getNetworkSession(&manager); + if (session) { + QVariant cib = session.data()->sessionProperty(QStringLiteral("ConnectInBackground")); + if (!cib.isValid()) + QSKIP("inconclusive - ConnectInBackground session property not supported by the bearer plugin"); + QCOMPARE(cib.toBool(), background); + QNetworkSessionPrivate::setUsagePolicies(*const_cast(session.data()), original); + } else { + QSKIP("inconclusive - network session has been destroyed"); + } + + QVERIFY(reply->isFinished()); +#endif +#endif +} + // NOTE: This test must be last testcase in tst_qnetworkreply! void tst_QNetworkReply::parentingRepliesToTheApp() { -- 2.7.4