diff options
author | Mårten Nordheim <marten.nordheim@qt.io> | 2019-04-26 12:56:23 +0200 |
---|---|---|
committer | Mårten Nordheim <marten.nordheim@qt.io> | 2019-05-15 12:48:07 +0000 |
commit | cd816d4b6ac4358a92dbda906288ba6d969fc1cd (patch) | |
tree | e310a7225d2b7e8353f902b46ce8965e376e8f58 /tests/auto | |
parent | 927eba344c2dbabc7275a4f561dd97f0e30760a2 (diff) |
Add setAutoDeleteReplies to QNetworkAccessManager
Following the introduction of AutoDeleteReplyOnFinishAttribute to
QNetworkRequest it seems natural to make it easy to enable for all
replies created with the current QNetworkAccessManager.
[ChangeLog][QtNetwork][QNetworkAccessManager] Added setAutoDeleteReplies
to QNetworkAccessManager to enable the AutoDeleteReplyOnFinishAttribute
attribute for all QNetworkRequests that are passed to
QNetworkAccessManager.
Change-Id: I7f96dd1fc9a899328e89732be17780b4e710c2a2
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Reviewed-by: Markus Goetz (Woboq GmbH) <markus@woboq.com>
Reviewed-by: Timur Pocheptsov <timur.pocheptsov@qt.io>
Diffstat (limited to 'tests/auto')
-rw-r--r-- | tests/auto/network/access/qnetworkreply/tst_qnetworkreply.cpp | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/tests/auto/network/access/qnetworkreply/tst_qnetworkreply.cpp b/tests/auto/network/access/qnetworkreply/tst_qnetworkreply.cpp index 5e3018366c..43739be4a3 100644 --- a/tests/auto/network/access/qnetworkreply/tst_qnetworkreply.cpp +++ b/tests/auto/network/access/qnetworkreply/tst_qnetworkreply.cpp @@ -505,6 +505,8 @@ private Q_SLOTS: void autoDeleteRepliesAttribute_data(); void autoDeleteRepliesAttribute(); + void autoDeleteReplies_data(); + void autoDeleteReplies(); // NOTE: This test must be last! void parentingRepliesToTheApp(); @@ -9241,6 +9243,95 @@ void tst_QNetworkReply::autoDeleteRepliesAttribute() } } +void tst_QNetworkReply::autoDeleteReplies_data() +{ + autoDeleteRepliesAttribute_data(); +} + +void tst_QNetworkReply::autoDeleteReplies() +{ + QFETCH(QUrl, destination); + manager.setAutoDeleteReplies(true); + auto cleanup = qScopeGuard([this] { manager.setAutoDeleteReplies(false); }); + { + // Get + QNetworkRequest request(destination); + QNetworkReply *reply = manager.get(request); + QSignalSpy finishedSpy(reply, &QNetworkReply::finished); + QSignalSpy destroyedSpy(reply, &QObject::destroyed); + QVERIFY(finishedSpy.wait()); + QCOMPARE(destroyedSpy.count(), 0); + QVERIFY(destroyedSpy.wait()); + } + { + // Post + QNetworkRequest request(destination); + QNetworkReply *reply = manager.post(request, QByteArrayLiteral("datastring")); + QSignalSpy finishedSpy(reply, &QNetworkReply::finished); + QSignalSpy destroyedSpy(reply, &QObject::destroyed); + QVERIFY(finishedSpy.wait()); + QCOMPARE(destroyedSpy.count(), 0); + QVERIFY(destroyedSpy.wait()); + } + // Here we repeat the test, but override the auto-deletion in the QNetworkRequest + // We need two calls to processEvents to test that the QNetworkReply doesn't get deleted. + // The first call executes a metacall event which adds the deleteLater meta event which + // would be executed in the second call. But that shouldn't happen in this case. + { + // Get + QNetworkRequest request(destination); + request.setAttribute(QNetworkRequest::AutoDeleteReplyOnFinishAttribute, false); + QScopedPointer<QNetworkReply> reply(manager.get(request)); + QSignalSpy finishedSpy(reply.data(), &QNetworkReply::finished); + QSignalSpy destroyedSpy(reply.data(), &QObject::destroyed); + QVERIFY(finishedSpy.wait()); + QCOMPARE(destroyedSpy.count(), 0); + QCoreApplication::processEvents(); + QCoreApplication::processEvents(); + QCOMPARE(destroyedSpy.count(), 0); + } + { + // Post + QNetworkRequest request(destination); + request.setAttribute(QNetworkRequest::AutoDeleteReplyOnFinishAttribute, false); + QScopedPointer<QNetworkReply> reply(manager.post(request, QByteArrayLiteral("datastring"))); + QSignalSpy finishedSpy(reply.data(), &QNetworkReply::finished); + QSignalSpy destroyedSpy(reply.data(), &QObject::destroyed); + QVERIFY(finishedSpy.wait()); + QCOMPARE(destroyedSpy.count(), 0); + QCoreApplication::processEvents(); + QCoreApplication::processEvents(); + QCOMPARE(destroyedSpy.count(), 0); + } + // Now we repeat the test with autoDeleteReplies set to false + cleanup.dismiss(); + manager.setAutoDeleteReplies(false); + { + // Get + QNetworkRequest request(destination); + QScopedPointer<QNetworkReply> reply(manager.get(request)); + QSignalSpy finishedSpy(reply.data(), &QNetworkReply::finished); + QSignalSpy destroyedSpy(reply.data(), &QObject::destroyed); + QVERIFY(finishedSpy.wait()); + QCOMPARE(destroyedSpy.count(), 0); + QCoreApplication::processEvents(); + QCoreApplication::processEvents(); + QCOMPARE(destroyedSpy.count(), 0); + } + { + // Post + QNetworkRequest request(destination); + QScopedPointer<QNetworkReply> reply(manager.post(request, QByteArrayLiteral("datastring"))); + QSignalSpy finishedSpy(reply.data(), &QNetworkReply::finished); + QSignalSpy destroyedSpy(reply.data(), &QObject::destroyed); + QVERIFY(finishedSpy.wait()); + QCOMPARE(destroyedSpy.count(), 0); + QCoreApplication::processEvents(); + QCoreApplication::processEvents(); + QCOMPARE(destroyedSpy.count(), 0); + } +} + // NOTE: This test must be last testcase in tst_qnetworkreply! void tst_QNetworkReply::parentingRepliesToTheApp() { |