1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the test suite of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
21 | ** included in the packaging of this file. Please review the following |
22 | ** information to ensure the GNU General Public License requirements will |
23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
24 | ** |
25 | ** $QT_END_LICENSE$ |
26 | ** |
27 | ****************************************************************************/ |
28 | |
29 | |
30 | #include <QtTest/QtTest> |
31 | #include <QtGui> |
32 | #include <QtWidgets> |
33 | #include <QtCore> |
34 | #include <QtNetwork/QNetworkAccessManager> |
35 | #include <QtNetwork/QNetworkRequest> |
36 | #include <QtNetwork/QNetworkReply> |
37 | #include <qdebug.h> |
38 | |
39 | #include "../../network-settings.h" |
40 | |
41 | |
42 | class tst_QNetworkAccessManager_And_QProgressDialog : public QObject |
43 | { |
44 | Q_OBJECT |
45 | public: |
46 | tst_QNetworkAccessManager_And_QProgressDialog(); |
47 | private slots: |
48 | void initTestCase(); |
49 | void downloadCheck(); |
50 | void downloadCheck_data(); |
51 | }; |
52 | |
53 | class DownloadCheckWidget : public QWidget |
54 | { |
55 | Q_OBJECT |
56 | public: |
57 | DownloadCheckWidget(QWidget *parent = 0) : |
58 | QWidget(parent), lateReadyRead(true), zeroCopy(false), |
59 | progressDlg(this), netmanager(this) |
60 | { |
61 | progressDlg.setRange(minimum: 1, maximum: 100); |
62 | QMetaObject::invokeMethod(obj: this, member: "go" , type: Qt::QueuedConnection); |
63 | } |
64 | bool lateReadyRead; |
65 | bool zeroCopy; |
66 | public slots: |
67 | void go() |
68 | { |
69 | QNetworkRequest request(QUrl("http://" + QtNetworkSettings::serverName() + "/qtest/bigfile" )); |
70 | if (zeroCopy) |
71 | request.setAttribute(code: QNetworkRequest::MaximumDownloadBufferSizeAttribute, value: 10*1024*1024); |
72 | |
73 | QNetworkReply *reply = netmanager.get( |
74 | request: QNetworkRequest( |
75 | QUrl("http://" + QtNetworkSettings::serverName() + "/qtest/bigfile" ) |
76 | )); |
77 | connect(sender: reply, SIGNAL(downloadProgress(qint64,qint64)), |
78 | receiver: this, SLOT(dataReadProgress(qint64,qint64))); |
79 | connect(sender: reply, SIGNAL(readyRead()), |
80 | receiver: this, SLOT(dataReadyRead())); |
81 | connect(sender: reply, SIGNAL(finished()), receiver: this, SLOT(finishedFromReply())); |
82 | |
83 | progressDlg.exec(); |
84 | } |
85 | void dataReadProgress(qint64 done, qint64 total) |
86 | { |
87 | QNetworkReply *reply = qobject_cast<QNetworkReply *>(object: sender()); |
88 | Q_UNUSED(reply); |
89 | progressDlg.setMaximum(total); |
90 | progressDlg.setValue(done); |
91 | } |
92 | void dataReadyRead() |
93 | { |
94 | QNetworkReply *reply = qobject_cast<QNetworkReply *>(object: sender()); |
95 | Q_UNUSED(reply); |
96 | lateReadyRead = true; |
97 | } |
98 | void finishedFromReply() |
99 | { |
100 | QNetworkReply *reply = qobject_cast<QNetworkReply *>(object: sender()); |
101 | lateReadyRead = false; |
102 | reply->deleteLater(); |
103 | QTestEventLoop::instance().exitLoop(); |
104 | } |
105 | |
106 | private: |
107 | QProgressDialog progressDlg; |
108 | QNetworkAccessManager netmanager; |
109 | }; |
110 | |
111 | |
112 | tst_QNetworkAccessManager_And_QProgressDialog::tst_QNetworkAccessManager_And_QProgressDialog() |
113 | { |
114 | } |
115 | |
116 | void tst_QNetworkAccessManager_And_QProgressDialog::initTestCase() |
117 | { |
118 | if (!QtNetworkSettings::verifyTestNetworkSettings()) |
119 | QSKIP("No network test server available" ); |
120 | } |
121 | |
122 | void tst_QNetworkAccessManager_And_QProgressDialog::downloadCheck_data() |
123 | { |
124 | QTest::addColumn<bool>(name: "useZeroCopy" ); |
125 | QTest::newRow(dataTag: "with-zeroCopy" ) << true; |
126 | QTest::newRow(dataTag: "without-zeroCopy" ) << false; |
127 | } |
128 | |
129 | void tst_QNetworkAccessManager_And_QProgressDialog::downloadCheck() |
130 | { |
131 | QFETCH(bool, useZeroCopy); |
132 | |
133 | DownloadCheckWidget widget; |
134 | widget.zeroCopy = useZeroCopy; |
135 | widget.show(); |
136 | // run and exit on finished() |
137 | QTestEventLoop::instance().enterLoop(secs: 10); |
138 | QVERIFY(!QTestEventLoop::instance().timeout()); |
139 | // run some more to catch the late readyRead() (or: to not catch it) |
140 | QTestEventLoop::instance().enterLoop(secs: 1); |
141 | QVERIFY(QTestEventLoop::instance().timeout()); |
142 | // the following fails when a readyRead() was received after finished() |
143 | QVERIFY(!widget.lateReadyRead); |
144 | } |
145 | |
146 | |
147 | |
148 | QTEST_MAIN(tst_QNetworkAccessManager_And_QProgressDialog) |
149 | #include "tst_qnetworkaccessmanager_and_qprogressdialog.moc" |
150 | |