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 <qabstractnetworkcache.h> |
32 | |
33 | #define EXAMPLE_URL "http://user:pass@www.example.com/#foo" |
34 | |
35 | class tst_QNetworkCacheMetaData : public QObject |
36 | { |
37 | Q_OBJECT |
38 | |
39 | private slots: |
40 | void qnetworkcachemetadata_data(); |
41 | void qnetworkcachemetadata(); |
42 | |
43 | void expirationDate_data(); |
44 | void expirationDate(); |
45 | void isValid_data(); |
46 | void isValid(); |
47 | void lastModified_data(); |
48 | void lastModified(); |
49 | void operatorEqual_data(); |
50 | void operatorEqual(); |
51 | void operatorEqualEqual_data(); |
52 | void operatorEqualEqual(); |
53 | void rawHeaders_data(); |
54 | void rawHeaders(); |
55 | void saveToDisk_data(); |
56 | void saveToDisk(); |
57 | void url_data(); |
58 | void url(); |
59 | |
60 | void stream(); |
61 | }; |
62 | |
63 | // Subclass that exposes the protected functions. |
64 | class SubQNetworkCacheMetaData : public QNetworkCacheMetaData |
65 | { |
66 | public:}; |
67 | |
68 | void tst_QNetworkCacheMetaData::qnetworkcachemetadata_data() |
69 | { |
70 | } |
71 | |
72 | void tst_QNetworkCacheMetaData::qnetworkcachemetadata() |
73 | { |
74 | QNetworkCacheMetaData data; |
75 | QCOMPARE(data.expirationDate(), QDateTime()); |
76 | QCOMPARE(data.isValid(), false); |
77 | QCOMPARE(data.lastModified(), QDateTime()); |
78 | QCOMPARE(data.operator!=(QNetworkCacheMetaData()), false); |
79 | QNetworkCacheMetaData metaData; |
80 | QCOMPARE(data.operator=(metaData), QNetworkCacheMetaData()); |
81 | QCOMPARE(data.operator==(QNetworkCacheMetaData()), true); |
82 | QCOMPARE(data.rawHeaders(), QNetworkCacheMetaData::RawHeaderList()); |
83 | QCOMPARE(data.saveToDisk(), true); |
84 | QCOMPARE(data.url(), QUrl()); |
85 | data.setExpirationDate(QDateTime()); |
86 | data.setLastModified(QDateTime()); |
87 | data.setRawHeaders(QNetworkCacheMetaData::RawHeaderList()); |
88 | data.setSaveToDisk(false); |
89 | data.setUrl(QUrl()); |
90 | } |
91 | |
92 | void tst_QNetworkCacheMetaData::expirationDate_data() |
93 | { |
94 | QTest::addColumn<QDateTime>(name: "expirationDate" ); |
95 | QTest::newRow(dataTag: "null" ) << QDateTime(); |
96 | QTest::newRow(dataTag: "now" ) << QDateTime::currentDateTime(); |
97 | } |
98 | |
99 | // public QDateTime expirationDate() const |
100 | void tst_QNetworkCacheMetaData::expirationDate() |
101 | { |
102 | QFETCH(QDateTime, expirationDate); |
103 | |
104 | SubQNetworkCacheMetaData data; |
105 | |
106 | data.setExpirationDate(expirationDate); |
107 | QCOMPARE(data.expirationDate(), expirationDate); |
108 | } |
109 | |
110 | Q_DECLARE_METATYPE(QNetworkCacheMetaData) |
111 | void tst_QNetworkCacheMetaData::isValid_data() |
112 | { |
113 | QTest::addColumn<QNetworkCacheMetaData>(name: "data" ); |
114 | QTest::addColumn<bool>(name: "isValid" ); |
115 | |
116 | QNetworkCacheMetaData metaData; |
117 | QTest::newRow(dataTag: "null" ) << metaData << false; |
118 | |
119 | QNetworkCacheMetaData data1; |
120 | data1.setUrl(QUrl(EXAMPLE_URL)); |
121 | QTest::newRow(dataTag: "valid-1" ) << data1 << true; |
122 | |
123 | QNetworkCacheMetaData data2; |
124 | QNetworkCacheMetaData::RawHeaderList ; |
125 | headers.append(t: QNetworkCacheMetaData::RawHeader("foo" , "Bar" )); |
126 | data2.setRawHeaders(headers); |
127 | QTest::newRow(dataTag: "valid-2" ) << data2 << true; |
128 | |
129 | QNetworkCacheMetaData data3; |
130 | data3.setLastModified(QDateTime::currentDateTime()); |
131 | QTest::newRow(dataTag: "valid-3" ) << data3 << true; |
132 | |
133 | QNetworkCacheMetaData data4; |
134 | data4.setExpirationDate(QDateTime::currentDateTime()); |
135 | QTest::newRow(dataTag: "valid-4" ) << data4 << true; |
136 | |
137 | QNetworkCacheMetaData data5; |
138 | data5.setSaveToDisk(false); |
139 | QTest::newRow(dataTag: "valid-5" ) << data5 << true; |
140 | } |
141 | |
142 | // public bool isValid() const |
143 | void tst_QNetworkCacheMetaData::isValid() |
144 | { |
145 | QFETCH(QNetworkCacheMetaData, data); |
146 | QFETCH(bool, isValid); |
147 | |
148 | QCOMPARE(data.isValid(), isValid); |
149 | } |
150 | |
151 | void tst_QNetworkCacheMetaData::lastModified_data() |
152 | { |
153 | QTest::addColumn<QDateTime>(name: "lastModified" ); |
154 | QTest::newRow(dataTag: "null" ) << QDateTime(); |
155 | QTest::newRow(dataTag: "now" ) << QDateTime::currentDateTime(); |
156 | } |
157 | |
158 | // public QDateTime lastModified() const |
159 | void tst_QNetworkCacheMetaData::lastModified() |
160 | { |
161 | QFETCH(QDateTime, lastModified); |
162 | |
163 | SubQNetworkCacheMetaData data; |
164 | |
165 | data.setLastModified(lastModified); |
166 | QCOMPARE(data.lastModified(), lastModified); |
167 | } |
168 | |
169 | void tst_QNetworkCacheMetaData::operatorEqual_data() |
170 | { |
171 | QTest::addColumn<QNetworkCacheMetaData>(name: "other" ); |
172 | QTest::newRow(dataTag: "null" ) << QNetworkCacheMetaData(); |
173 | |
174 | QNetworkCacheMetaData data; |
175 | data.setUrl(QUrl(EXAMPLE_URL)); |
176 | QNetworkCacheMetaData::RawHeaderList ; |
177 | headers.append(t: QNetworkCacheMetaData::RawHeader("foo" , "Bar" )); |
178 | data.setRawHeaders(headers); |
179 | data.setLastModified(QDateTime::currentDateTime()); |
180 | data.setExpirationDate(QDateTime::currentDateTime()); |
181 | data.setSaveToDisk(false); |
182 | QTest::newRow(dataTag: "valid" ) << data; |
183 | } |
184 | |
185 | // public QNetworkCacheMetaData& operator=(QNetworkCacheMetaData const& other) |
186 | void tst_QNetworkCacheMetaData::operatorEqual() |
187 | { |
188 | QFETCH(QNetworkCacheMetaData, other); |
189 | |
190 | QNetworkCacheMetaData data = other; |
191 | |
192 | QCOMPARE(data, other); |
193 | } |
194 | |
195 | void tst_QNetworkCacheMetaData::operatorEqualEqual_data() |
196 | { |
197 | QTest::addColumn<QNetworkCacheMetaData>(name: "a" ); |
198 | QTest::addColumn<QNetworkCacheMetaData>(name: "b" ); |
199 | QTest::addColumn<bool>(name: "operatorEqualEqual" ); |
200 | QTest::newRow(dataTag: "null" ) << QNetworkCacheMetaData() << QNetworkCacheMetaData() << true; |
201 | |
202 | QNetworkCacheMetaData data1; |
203 | data1.setUrl(QUrl(EXAMPLE_URL)); |
204 | QTest::newRow(dataTag: "valid-1-1" ) << data1 << QNetworkCacheMetaData() << false; |
205 | QTest::newRow(dataTag: "valid-1-2" ) << data1 << data1 << true; |
206 | |
207 | QNetworkCacheMetaData data2; |
208 | QNetworkCacheMetaData::RawHeaderList ; |
209 | headers.append(t: QNetworkCacheMetaData::RawHeader("foo" , "Bar" )); |
210 | data2.setRawHeaders(headers); |
211 | QTest::newRow(dataTag: "valid-2-1" ) << data2 << QNetworkCacheMetaData() << false; |
212 | QTest::newRow(dataTag: "valid-2-2" ) << data2 << data2 << true; |
213 | QTest::newRow(dataTag: "valid-2-3" ) << data2 << data1 << false; |
214 | |
215 | QNetworkCacheMetaData data3; |
216 | data3.setLastModified(QDateTime::currentDateTime()); |
217 | QTest::newRow(dataTag: "valid-3-1" ) << data3 << QNetworkCacheMetaData() << false; |
218 | QTest::newRow(dataTag: "valid-3-2" ) << data3 << data3 << true; |
219 | QTest::newRow(dataTag: "valid-3-3" ) << data3 << data1 << false; |
220 | QTest::newRow(dataTag: "valid-3-4" ) << data3 << data2 << false; |
221 | |
222 | QNetworkCacheMetaData data4; |
223 | data4.setExpirationDate(QDateTime::currentDateTime()); |
224 | QTest::newRow(dataTag: "valid-4-1" ) << data4 << QNetworkCacheMetaData() << false; |
225 | QTest::newRow(dataTag: "valid-4-2" ) << data4 << data4 << true; |
226 | QTest::newRow(dataTag: "valid-4-3" ) << data4 << data1 << false; |
227 | QTest::newRow(dataTag: "valid-4-4" ) << data4 << data2 << false; |
228 | QTest::newRow(dataTag: "valid-4-5" ) << data4 << data3 << false; |
229 | |
230 | QNetworkCacheMetaData data5; |
231 | data5.setSaveToDisk(false); |
232 | QTest::newRow(dataTag: "valid-5-1" ) << data5 << QNetworkCacheMetaData() << false; |
233 | QTest::newRow(dataTag: "valid-5-2" ) << data5 << data5 << true; |
234 | QTest::newRow(dataTag: "valid-5-3" ) << data5 << data1 << false; |
235 | QTest::newRow(dataTag: "valid-5-4" ) << data5 << data2 << false; |
236 | QTest::newRow(dataTag: "valid-5-5" ) << data5 << data3 << false; |
237 | QTest::newRow(dataTag: "valid-5-6" ) << data5 << data4 << false; |
238 | } |
239 | |
240 | // public bool operator==(QNetworkCacheMetaData const& other) const |
241 | void tst_QNetworkCacheMetaData::operatorEqualEqual() |
242 | { |
243 | QFETCH(QNetworkCacheMetaData, a); |
244 | QFETCH(QNetworkCacheMetaData, b); |
245 | QFETCH(bool, operatorEqualEqual); |
246 | |
247 | QCOMPARE(a == b, operatorEqualEqual); |
248 | } |
249 | |
250 | Q_DECLARE_METATYPE(QNetworkCacheMetaData::RawHeaderList) |
251 | void tst_QNetworkCacheMetaData::() |
252 | { |
253 | QTest::addColumn<QNetworkCacheMetaData::RawHeaderList>(name: "rawHeaders" ); |
254 | QTest::newRow(dataTag: "null" ) << QNetworkCacheMetaData::RawHeaderList(); |
255 | QNetworkCacheMetaData::RawHeaderList ; |
256 | headers.append(t: QNetworkCacheMetaData::RawHeader("foo" , "Bar" )); |
257 | QTest::newRow(dataTag: "valie" ) << headers; |
258 | } |
259 | |
260 | // public QNetworkCacheMetaData::RawHeaderList rawHeaders() const |
261 | void tst_QNetworkCacheMetaData::() |
262 | { |
263 | QFETCH(QNetworkCacheMetaData::RawHeaderList, ); |
264 | |
265 | SubQNetworkCacheMetaData data; |
266 | |
267 | data.setRawHeaders(rawHeaders); |
268 | QCOMPARE(data.rawHeaders(), rawHeaders); |
269 | } |
270 | |
271 | void tst_QNetworkCacheMetaData::saveToDisk_data() |
272 | { |
273 | QTest::addColumn<bool>(name: "saveToDisk" ); |
274 | QTest::newRow(dataTag: "false" ) << false; |
275 | QTest::newRow(dataTag: "true" ) << true; |
276 | } |
277 | |
278 | // public bool saveToDisk() const |
279 | void tst_QNetworkCacheMetaData::saveToDisk() |
280 | { |
281 | QFETCH(bool, saveToDisk); |
282 | |
283 | SubQNetworkCacheMetaData data; |
284 | |
285 | data.setSaveToDisk(saveToDisk); |
286 | QCOMPARE(data.saveToDisk(), saveToDisk); |
287 | } |
288 | |
289 | void tst_QNetworkCacheMetaData::url_data() |
290 | { |
291 | QTest::addColumn<QUrl>(name: "url" ); |
292 | QTest::addColumn<QUrl>(name: "expected" ); |
293 | QTest::newRow(dataTag: "null" ) << QUrl() << QUrl(); |
294 | QTest::newRow(dataTag: "valid" ) << QUrl(EXAMPLE_URL) << QUrl("http://user@www.example.com/" ); |
295 | } |
296 | |
297 | // public QUrl url() const |
298 | void tst_QNetworkCacheMetaData::url() |
299 | { |
300 | QFETCH(QUrl, url); |
301 | QFETCH(QUrl, expected); |
302 | |
303 | SubQNetworkCacheMetaData data; |
304 | data.setUrl(url); |
305 | QCOMPARE(data.url(), expected); |
306 | } |
307 | |
308 | void tst_QNetworkCacheMetaData::stream() |
309 | { |
310 | QNetworkCacheMetaData data; |
311 | data.setUrl(QUrl(EXAMPLE_URL)); |
312 | QNetworkCacheMetaData::RawHeaderList ; |
313 | headers.append(t: QNetworkCacheMetaData::RawHeader("foo" , "Bar" )); |
314 | data.setRawHeaders(headers); |
315 | data.setLastModified(QDateTime::currentDateTime()); |
316 | data.setExpirationDate(QDateTime::currentDateTime()); |
317 | data.setSaveToDisk(false); |
318 | |
319 | QBuffer buffer; |
320 | buffer.open(openMode: QIODevice::ReadWrite); |
321 | QDataStream stream(&buffer); |
322 | stream << data; |
323 | |
324 | buffer.seek(off: 0); |
325 | QNetworkCacheMetaData data2; |
326 | stream >> data2; |
327 | QCOMPARE(data2, data); |
328 | } |
329 | |
330 | QTEST_MAIN(tst_QNetworkCacheMetaData) |
331 | #include "tst_qnetworkcachemetadata.moc" |
332 | |
333 | |