1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2018 The Qt Company Ltd. |
4 | ** Copyright (C) 2019 Intel Corporation. |
5 | ** Contact: https://www.qt.io/licensing/ |
6 | ** |
7 | ** This file is part of the test suite of the Qt Toolkit. |
8 | ** |
9 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
10 | ** Commercial License Usage |
11 | ** Licensees holding valid commercial Qt licenses may use this file in |
12 | ** accordance with the commercial license agreement provided with the |
13 | ** Software or, alternatively, in accordance with the terms contained in |
14 | ** a written agreement between you and The Qt Company. For licensing terms |
15 | ** and conditions see https://www.qt.io/terms-conditions. For further |
16 | ** information use the contact form at https://www.qt.io/contact-us. |
17 | ** |
18 | ** GNU General Public License Usage |
19 | ** Alternatively, this file may be used under the terms of the GNU |
20 | ** General Public License version 3 as published by the Free Software |
21 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
22 | ** included in the packaging of this file. Please review the following |
23 | ** information to ensure the GNU General Public License requirements will |
24 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
25 | ** |
26 | ** $QT_END_LICENSE$ |
27 | ** |
28 | ****************************************************************************/ |
29 | |
30 | #include <QtTest/QtTest> |
31 | #include <QtCore/QCoreApplication> |
32 | #include <QtCore/QScopeGuard> |
33 | #include <QtCore/private/qglobal_p.h> |
34 | |
35 | class tst_QResourceEngine: public QObject |
36 | { |
37 | Q_OBJECT |
38 | |
39 | public: |
40 | tst_QResourceEngine() |
41 | #if defined(Q_OS_ANDROID) && !defined(Q_OS_ANDROID_EMBEDDED) |
42 | : m_runtimeResourceRcc(QFileInfo(QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + QStringLiteral("/runtime_resource.rcc" )).absoluteFilePath()) |
43 | #else |
44 | : m_runtimeResourceRcc(QFINDTESTDATA("runtime_resource.rcc" )) |
45 | #endif |
46 | {} |
47 | |
48 | private slots: |
49 | void initTestCase(); |
50 | void cleanupTestCase(); |
51 | |
52 | void checkUnregisterResource_data(); |
53 | void checkUnregisterResource(); |
54 | void compressedResource_data(); |
55 | void compressedResource(); |
56 | void checkStructure_data(); |
57 | void checkStructure(); |
58 | void searchPath_data(); |
59 | void searchPath(); |
60 | #if QT_DEPRECATED_SINCE(5, 13) |
61 | void searchPath_deprecated_data(); |
62 | void searchPath_deprecated(); |
63 | #endif |
64 | void doubleSlashInRoot(); |
65 | void setLocale(); |
66 | void lastModified(); |
67 | void resourcesInStaticPlugins(); |
68 | |
69 | private: |
70 | const QString m_runtimeResourceRcc; |
71 | }; |
72 | |
73 | |
74 | void tst_QResourceEngine::initTestCase() |
75 | { |
76 | #if defined(Q_OS_ANDROID) && !defined(Q_OS_ANDROID_EMBEDDED) |
77 | QString sourcePath(QStringLiteral(":/android_testdata/" )); |
78 | QString dataPath(QStandardPaths::writableLocation(QStandardPaths::CacheLocation)); |
79 | |
80 | QDirIterator it(sourcePath, QDirIterator::Subdirectories); |
81 | while (it.hasNext()) { |
82 | it.next(); |
83 | |
84 | QFileInfo fileInfo = it.fileInfo(); |
85 | if (!fileInfo.isDir()) { |
86 | QString destination(dataPath + QLatin1Char('/') + fileInfo.filePath().mid(sourcePath.length())); |
87 | QFileInfo destinationFileInfo(destination); |
88 | if (!destinationFileInfo.exists()) { |
89 | QVERIFY(QDir().mkpath(destinationFileInfo.path())); |
90 | QVERIFY(QFile::copy(fileInfo.filePath(), destination)); |
91 | QVERIFY(QFileInfo(destination).exists()); |
92 | } |
93 | } |
94 | } |
95 | |
96 | QVERIFY(QDir::setCurrent(dataPath)); |
97 | #endif |
98 | |
99 | QVERIFY(!m_runtimeResourceRcc.isEmpty()); |
100 | QVERIFY(QResource::registerResource(m_runtimeResourceRcc)); |
101 | QVERIFY(QResource::registerResource(m_runtimeResourceRcc, "/secondary_root/" )); |
102 | } |
103 | |
104 | void tst_QResourceEngine::cleanupTestCase() |
105 | { |
106 | // make sure we don't leak memory |
107 | QVERIFY(QResource::unregisterResource(m_runtimeResourceRcc)); |
108 | QVERIFY(QResource::unregisterResource(m_runtimeResourceRcc, "/secondary_root/" )); |
109 | } |
110 | |
111 | void tst_QResourceEngine::compressedResource_data() |
112 | { |
113 | QTest::addColumn<QString>(name: "fileName" ); |
114 | QTest::addColumn<int>(name: "compressionAlgo" ); |
115 | QTest::addColumn<bool>(name: "supported" ); |
116 | |
117 | QTest::newRow(dataTag: "uncompressed" ) |
118 | << QFINDTESTDATA("uncompressed.rcc" ) << int(QResource::NoCompression) << true; |
119 | QTest::newRow(dataTag: "zlib" ) |
120 | << QFINDTESTDATA("zlib.rcc" ) << int(QResource::ZlibCompression) << true; |
121 | QTest::newRow(dataTag: "zstd" ) |
122 | << QFINDTESTDATA("zstd.rcc" ) << int(QResource::ZstdCompression) << QT_CONFIG(zstd); |
123 | } |
124 | |
125 | // Note: generateResource.sh parses this line. Make sure it's a simple number. |
126 | #define ZERO_FILE_LEN 16384 |
127 | // End note |
128 | void tst_QResourceEngine::compressedResource() |
129 | { |
130 | QFETCH(QString, fileName); |
131 | QFETCH(int, compressionAlgo); |
132 | QFETCH(bool, supported); |
133 | const QByteArray expectedData(ZERO_FILE_LEN, '\0'); |
134 | |
135 | QVERIFY(!QResource("zero.txt" ).isValid()); |
136 | QCOMPARE(QResource::registerResource(fileName), supported); |
137 | if (!supported) |
138 | return; |
139 | |
140 | auto unregister = qScopeGuard(f: [=] { QResource::unregisterResource(rccFilename: fileName); }); |
141 | |
142 | QResource resource("zero.txt" ); |
143 | QVERIFY(resource.isValid()); |
144 | QVERIFY(resource.size() > 0); |
145 | QVERIFY(resource.data()); |
146 | QCOMPARE(resource.compressionAlgorithm(), QResource::Compression(compressionAlgo)); |
147 | |
148 | if (compressionAlgo == QResource::NoCompression) { |
149 | QCOMPARE(resource.size(), ZERO_FILE_LEN); |
150 | QCOMPARE(memcmp(resource.data(), expectedData.data(), ZERO_FILE_LEN), 0); |
151 | |
152 | // API guarantees it will be QByteArray::fromRawData: |
153 | QCOMPARE(static_cast<const void *>(resource.uncompressedData().constData()), |
154 | static_cast<const void *>(resource.data())); |
155 | } else { |
156 | // reasonable expectation: |
157 | QVERIFY(resource.size() < ZERO_FILE_LEN); |
158 | } |
159 | |
160 | // using the engine |
161 | QFile f(":/zero.txt" ); |
162 | QVERIFY(f.exists()); |
163 | QVERIFY(f.open(QIODevice::ReadOnly)); |
164 | |
165 | // verify that we can decompress correctly |
166 | QCOMPARE(resource.uncompressedSize(), ZERO_FILE_LEN); |
167 | QCOMPARE(f.size(), ZERO_FILE_LEN); |
168 | |
169 | QByteArray data = resource.uncompressedData(); |
170 | QCOMPARE(data.size(), expectedData.size()); |
171 | QCOMPARE(data, expectedData); |
172 | |
173 | // decompression through the engine |
174 | data = f.readAll(); |
175 | QCOMPARE(data.size(), expectedData.size()); |
176 | QCOMPARE(data, expectedData); |
177 | } |
178 | |
179 | |
180 | void tst_QResourceEngine::checkStructure_data() |
181 | { |
182 | QTest::addColumn<QString>(name: "pathName" ); |
183 | QTest::addColumn<QByteArray>(name: "contents" ); |
184 | QTest::addColumn<QStringList>(name: "containedFiles" ); |
185 | QTest::addColumn<QStringList>(name: "containedDirs" ); |
186 | QTest::addColumn<QLocale>(name: "locale" ); |
187 | QTest::addColumn<qlonglong>(name: "contentsSize" ); |
188 | |
189 | QFileInfo info; |
190 | |
191 | QStringList rootContents; |
192 | rootContents << QLatin1String("aliasdir" ) |
193 | #if defined(Q_OS_ANDROID) && !defined(Q_OS_ANDROID_EMBEDDED) |
194 | << QLatin1String("android_testdata" ) |
195 | #endif |
196 | << QLatin1String("otherdir" ) |
197 | << QLatin1String("runtime_resource" ) |
198 | << QLatin1String("searchpath1" ) |
199 | << QLatin1String("searchpath2" ) |
200 | << QLatin1String("secondary_root" ) |
201 | << QLatin1String("staticplugin" ) |
202 | << QLatin1String("test" ) |
203 | #if defined(BUILTIN_TESTDATA) |
204 | << QLatin1String("testqrc" ) |
205 | #endif |
206 | << QLatin1String("withoutslashes" ); |
207 | |
208 | QTest::newRow(dataTag: "root dir" ) << QString(":/" ) |
209 | << QByteArray() |
210 | << (QStringList() |
211 | #if defined(BUILTIN_TESTDATA) |
212 | << "parentdir.txt" |
213 | << "runtime_resource.rcc" |
214 | #endif |
215 | << "search_file.txt" |
216 | #if defined(BUILTIN_TESTDATA) |
217 | << "uncompressed.rcc" |
218 | << "zlib.rcc" |
219 | << "zstd.rcc" |
220 | #endif |
221 | ) |
222 | << rootContents |
223 | << QLocale::c() |
224 | << qlonglong(0); |
225 | |
226 | QTest::newRow(dataTag: "secondary root" ) << QString(":/secondary_root/" ) |
227 | << QByteArray() |
228 | << QStringList() |
229 | << (QStringList() << QLatin1String("runtime_resource" )) |
230 | << QLocale::c() |
231 | << qlonglong(0); |
232 | |
233 | QStringList roots; |
234 | roots << QString(":/" ) << QString(":/runtime_resource/" ) << QString(":/secondary_root/runtime_resource/" ); |
235 | for(int i = 0; i < roots.size(); ++i) { |
236 | const QString root = roots.at(i); |
237 | |
238 | QTest::addRow(format: "%s prefix dir" , qPrintable(root)) << QString(root + "test/abc/123/+++" ) |
239 | << QByteArray() |
240 | << (QStringList() << QLatin1String("currentdir.txt" ) << QLatin1String("currentdir2.txt" ) << QLatin1String("parentdir.txt" )) |
241 | << (QStringList() << QLatin1String("subdir" )) |
242 | << QLocale::c() |
243 | << qlonglong(0); |
244 | |
245 | QTest::addRow(format: "%s parent to prefix" , qPrintable(root)) << QString(root + "test/abc/123" ) |
246 | << QByteArray() |
247 | << QStringList() |
248 | << (QStringList() << QLatin1String("+++" )) |
249 | << QLocale::c() |
250 | << qlonglong(0); |
251 | |
252 | QTest::addRow(format: "%s two parents prefix" , qPrintable(root)) << QString(root + "test/abc" ) |
253 | << QByteArray() |
254 | << QStringList() |
255 | << QStringList(QLatin1String("123" )) |
256 | << QLocale::c() |
257 | << qlonglong(0); |
258 | |
259 | QTest::addRow(format: "%s test dir " , qPrintable(root)) << QString(root + "test" ) |
260 | << QByteArray() |
261 | << (QStringList() << QLatin1String("testdir.txt" )) |
262 | << (QStringList() << QLatin1String("abc" ) << QLatin1String("test" )) |
263 | << QLocale::c() |
264 | << qlonglong(0); |
265 | |
266 | QTest::addRow(format: "%s prefix no slashes" , qPrintable(root)) << QString(root + "withoutslashes" ) |
267 | << QByteArray() |
268 | << QStringList("blahblah.txt" ) |
269 | << QStringList() |
270 | << QLocale::c() |
271 | << qlonglong(0); |
272 | |
273 | QTest::addRow(format: "%s other dir" , qPrintable(root)) << QString(root + "otherdir" ) |
274 | << QByteArray() |
275 | << QStringList(QLatin1String("otherdir.txt" )) |
276 | << QStringList() |
277 | << QLocale::c() |
278 | << qlonglong(0); |
279 | |
280 | QTest::addRow(format: "%s alias dir" , qPrintable(root)) << QString(root + "aliasdir" ) |
281 | << QByteArray() |
282 | << QStringList(QLatin1String("aliasdir.txt" )) |
283 | << QStringList() |
284 | << QLocale::c() |
285 | << qlonglong(0); |
286 | |
287 | QTest::addRow(format: "%s second test dir" , qPrintable(root)) << QString(root + "test/test" ) |
288 | << QByteArray() |
289 | << (QStringList() << QLatin1String("test1.txt" ) << QLatin1String("test2.txt" )) |
290 | << QStringList() |
291 | << QLocale::c() |
292 | << qlonglong(0); |
293 | |
294 | info = QFileInfo(QFINDTESTDATA("testqrc/test/test/test1.txt" )); |
295 | QTest::addRow(format: "%s test1 text" , qPrintable(root)) << QString(root + "test/test/test1.txt" ) |
296 | << QByteArray("abc\n" ) |
297 | << QStringList() |
298 | << QStringList() |
299 | << QLocale::c() |
300 | << qlonglong(info.size()); |
301 | |
302 | info = QFileInfo(QFINDTESTDATA("testqrc/blahblah.txt" )); |
303 | QTest::addRow(format: "%s text no slashes" , qPrintable(root)) << QString(root + "withoutslashes/blahblah.txt" ) |
304 | << QByteArray("qwerty\n" ) |
305 | << QStringList() |
306 | << QStringList() |
307 | << QLocale::c() |
308 | << qlonglong(info.size()); |
309 | |
310 | |
311 | info = QFileInfo(QFINDTESTDATA("testqrc/test/test/test2.txt" )); |
312 | QTest::addRow(format: "%s test1 text" , qPrintable(root)) << QString(root + "test/test/test2.txt" ) |
313 | << QByteArray("def\n" ) |
314 | << QStringList() |
315 | << QStringList() |
316 | << QLocale::c() |
317 | << qlonglong(info.size()); |
318 | |
319 | info = QFileInfo(QFINDTESTDATA("testqrc/currentdir.txt" )); |
320 | QTest::addRow(format: "%s currentdir text" , qPrintable(root)) << QString(root + "test/abc/123/+++/currentdir.txt" ) |
321 | << QByteArray("\"This is the current dir\"\n" ) |
322 | << QStringList() |
323 | << QStringList() |
324 | << QLocale::c() |
325 | << qlonglong(info.size()); |
326 | |
327 | info = QFileInfo(QFINDTESTDATA("testqrc/currentdir2.txt" )); |
328 | QTest::addRow(format: "%s currentdir text2" , qPrintable(root)) << QString(root + "test/abc/123/+++/currentdir2.txt" ) |
329 | << QByteArray("\"This is also the current dir\"\n" ) |
330 | << QStringList() |
331 | << QStringList() |
332 | << QLocale::c() |
333 | << qlonglong(info.size()); |
334 | |
335 | info = QFileInfo(QFINDTESTDATA("parentdir.txt" )); |
336 | QTest::addRow(format: "%s parentdir text" , qPrintable(root)) << QString(root + "test/abc/123/+++/parentdir.txt" ) |
337 | << QByteArray("abcdefgihklmnopqrstuvwxyz \n" ) |
338 | << QStringList() |
339 | << QStringList() |
340 | << QLocale::c() |
341 | << qlonglong(info.size()); |
342 | |
343 | info = QFileInfo(QFINDTESTDATA("testqrc/subdir/subdir.txt" )); |
344 | QTest::addRow(format: "%s subdir text" , qPrintable(root)) << QString(root + "test/abc/123/+++/subdir/subdir.txt" ) |
345 | << QByteArray("\"This is in the sub directory\"\n" ) |
346 | << QStringList() |
347 | << QStringList() |
348 | << QLocale::c() |
349 | << qlonglong(info.size()); |
350 | |
351 | info = QFileInfo(QFINDTESTDATA("testqrc/test/testdir.txt" )); |
352 | QTest::addRow(format: "%s testdir text" , qPrintable(root)) << QString(root + "test/testdir.txt" ) |
353 | << QByteArray("\"This is in the test directory\"\n" ) |
354 | << QStringList() |
355 | << QStringList() |
356 | << QLocale::c() |
357 | << qlonglong(info.size()); |
358 | |
359 | info = QFileInfo(QFINDTESTDATA("testqrc/otherdir/otherdir.txt" )); |
360 | QTest::addRow(format: "%s otherdir text" , qPrintable(root)) << QString(root + "otherdir/otherdir.txt" ) |
361 | << QByteArray("\"This is the other dir\"\n" ) |
362 | << QStringList() |
363 | << QStringList() |
364 | << QLocale::c() |
365 | << qlonglong(info.size()); |
366 | |
367 | info = QFileInfo(QFINDTESTDATA("testqrc/test/testdir2.txt" )); |
368 | QTest::addRow(format: "%s alias text" , qPrintable(root)) << QString(root + "aliasdir/aliasdir.txt" ) |
369 | << QByteArray("\"This is another file in this directory\"\n" ) |
370 | << QStringList() |
371 | << QStringList() |
372 | << QLocale::c() |
373 | << qlonglong(info.size()); |
374 | |
375 | info = QFileInfo(QFINDTESTDATA("testqrc/aliasdir/aliasdir.txt" )); |
376 | QTest::addRow(format: "%s korean text" , qPrintable(root)) << QString(root + "aliasdir/aliasdir.txt" ) |
377 | << QByteArray("\"This is a korean text file\"\n" ) |
378 | << QStringList() |
379 | << QStringList() |
380 | << QLocale("ko" ) |
381 | << qlonglong(info.size()); |
382 | |
383 | info = QFileInfo(QFINDTESTDATA("testqrc/aliasdir/aliasdir.txt" )); |
384 | QTest::addRow(format: "%s korean text 2" , qPrintable(root)) << QString(root + "aliasdir/aliasdir.txt" ) |
385 | << QByteArray("\"This is a korean text file\"\n" ) |
386 | << QStringList() |
387 | << QStringList() |
388 | << QLocale("ko_KR" ) |
389 | << qlonglong(info.size()); |
390 | |
391 | info = QFileInfo(QFINDTESTDATA("testqrc/test/german.txt" )); |
392 | QTest::addRow(format: "%s german text" , qPrintable(root)) << QString(root + "aliasdir/aliasdir.txt" ) |
393 | << QByteArray("Deutsch\n" ) |
394 | << QStringList() |
395 | << QStringList() |
396 | << QLocale("de" ) |
397 | << qlonglong(info.size()); |
398 | |
399 | info = QFileInfo(QFINDTESTDATA("testqrc/test/german.txt" )); |
400 | QTest::addRow(format: "%s german text 2" , qPrintable(root)) << QString(root + "aliasdir/aliasdir.txt" ) |
401 | << QByteArray("Deutsch\n" ) |
402 | << QStringList() |
403 | << QStringList() |
404 | << QLocale("de_DE" ) |
405 | << qlonglong(info.size()); |
406 | |
407 | QFile file(QFINDTESTDATA("testqrc/aliasdir/compressme.txt" )); |
408 | file.open(flags: QFile::ReadOnly); |
409 | info = QFileInfo(QFINDTESTDATA("testqrc/aliasdir/compressme.txt" )); |
410 | QTest::addRow(format: "%s compressed text" , qPrintable(root)) << QString(root + "aliasdir/aliasdir.txt" ) |
411 | << file.readAll() |
412 | << QStringList() |
413 | << QStringList() |
414 | << QLocale("de_CH" ) |
415 | << qlonglong(info.size()); |
416 | } |
417 | } |
418 | |
419 | void tst_QResourceEngine::checkStructure() |
420 | { |
421 | QFETCH(QString, pathName); |
422 | QFETCH(QByteArray, contents); |
423 | QFETCH(QStringList, containedFiles); |
424 | QFETCH(QStringList, containedDirs); |
425 | QFETCH(QLocale, locale); |
426 | QFETCH(qlonglong, contentsSize); |
427 | |
428 | bool directory = (containedDirs.size() + containedFiles.size() > 0); |
429 | QLocale::setDefault(locale); |
430 | |
431 | QFileInfo fileInfo(pathName); |
432 | |
433 | QVERIFY(fileInfo.exists()); |
434 | QCOMPARE(fileInfo.isDir(), directory); |
435 | QCOMPARE(fileInfo.size(), contentsSize); |
436 | QVERIFY(fileInfo.isReadable()); |
437 | QVERIFY(!fileInfo.isWritable()); |
438 | QVERIFY(!fileInfo.isExecutable()); |
439 | |
440 | if (directory) { |
441 | QDir dir(pathName); |
442 | |
443 | // Test the Dir filter |
444 | QFileInfoList list = dir.entryInfoList(filters: QDir::Dirs, sort: QDir::Name); |
445 | QCOMPARE(list.size(), containedDirs.size()); |
446 | |
447 | int i; |
448 | for (i=0; i<list.size(); ++i) { |
449 | QVERIFY(list.at(i).isDir()); |
450 | QCOMPARE(list.at(i).fileName(), containedDirs.at(i)); |
451 | } |
452 | |
453 | list = dir.entryInfoList(filters: QDir::Files, sort: QDir::Name); |
454 | QCOMPARE(list.size(), containedFiles.size()); |
455 | |
456 | for (i=0; i<list.size(); ++i) { |
457 | QVERIFY(!list.at(i).isDir()); |
458 | QCOMPARE(list.at(i).fileName(), containedFiles.at(i)); |
459 | } |
460 | |
461 | list = dir.entryInfoList(filters: QDir::NoFilter, sort: QDir::SortFlags(QDir::Name | QDir::DirsFirst)); |
462 | QCOMPARE(list.size(), containedFiles.size() + containedDirs.size()); |
463 | |
464 | for (i=0; i<list.size(); ++i) { |
465 | QString expectedName; |
466 | if (i < containedDirs.size()) |
467 | expectedName = containedDirs.at(i); |
468 | else |
469 | expectedName = containedFiles.at(i: i - containedDirs.size()); |
470 | |
471 | QCOMPARE(list.at(i).fileName(), expectedName); |
472 | } |
473 | } else { |
474 | QFile file(pathName); |
475 | QVERIFY(file.open(QFile::ReadOnly)); |
476 | |
477 | // check contents |
478 | QCOMPARE(file.readAll(), contents); |
479 | |
480 | // check memory map too |
481 | uchar *ptr = file.map(offset: 0, size: file.size(), flags: QFile::MapPrivateOption); |
482 | QVERIFY2(ptr, qPrintable(file.errorString())); |
483 | QByteArray ba = QByteArray::fromRawData(reinterpret_cast<const char *>(ptr), size: file.size()); |
484 | QCOMPARE(ba, contents); |
485 | |
486 | // check that it is still valid after closing the file |
487 | file.close(); |
488 | QCOMPARE(ba, contents); |
489 | } |
490 | QLocale::setDefault(QLocale::system()); |
491 | } |
492 | |
493 | void tst_QResourceEngine::searchPath_data() |
494 | { |
495 | auto searchPath = QFileInfo(QFINDTESTDATA("testqrc" )).canonicalFilePath(); |
496 | |
497 | QTest::addColumn<QString>(name: "searchPathPrefix" ); |
498 | QTest::addColumn<QString>(name: "searchPath" ); |
499 | QTest::addColumn<QString>(name: "file" ); |
500 | QTest::addColumn<QByteArray>(name: "expected" ); |
501 | |
502 | QTest::newRow(dataTag: "no_search_path" ) |
503 | << QString() |
504 | << QString() |
505 | << ":search_file.txt" |
506 | << QByteArray("root\n" ); |
507 | QTest::newRow(dataTag: "path1" ) |
508 | << "searchpath1" |
509 | << searchPath |
510 | << "searchpath1:searchpath1/search_file.txt" |
511 | << QByteArray("path1\n" ); |
512 | QTest::newRow(dataTag: "no_search_path2" ) |
513 | << QString() |
514 | << QString() |
515 | << ":/search_file.txt" |
516 | << QByteArray("root\n" ); |
517 | QTest::newRow(dataTag: "path2" ) |
518 | << "searchpath2" |
519 | << searchPath + "/searchpath2" |
520 | << "searchpath2:search_file.txt" |
521 | << QByteArray("path2\n" ); |
522 | } |
523 | |
524 | void tst_QResourceEngine::searchPath() |
525 | { |
526 | QFETCH(QString, searchPathPrefix); |
527 | QFETCH(QString, searchPath); |
528 | QFETCH(QString, file); |
529 | QFETCH(QByteArray, expected); |
530 | |
531 | if (!searchPath.isEmpty()) |
532 | QDir::addSearchPath(prefix: searchPathPrefix, path: searchPath); |
533 | QFile qf(file); |
534 | QVERIFY(qf.open(QFile::ReadOnly)); |
535 | QByteArray actual = qf.readAll(); |
536 | |
537 | actual.replace(before: '\r', c: "" ); |
538 | |
539 | QCOMPARE(actual, expected); |
540 | qf.close(); |
541 | } |
542 | |
543 | #if QT_DEPRECATED_SINCE(5, 13) |
544 | |
545 | void tst_QResourceEngine::searchPath_deprecated_data() |
546 | { |
547 | QTest::addColumn<QString>(name: "searchPath" ); |
548 | QTest::addColumn<QString>(name: "file" ); |
549 | QTest::addColumn<QByteArray>(name: "expected" ); |
550 | |
551 | QTest::newRow(dataTag: "no_search_path" ) << QString() |
552 | << ":search_file.txt" |
553 | << QByteArray("root\n" ); |
554 | QTest::newRow(dataTag: "path1" ) << "/searchpath1" |
555 | << ":search_file.txt" |
556 | << QByteArray("path1\n" ); |
557 | QTest::newRow(dataTag: "no_search_path2" ) << QString() |
558 | << ":/search_file.txt" |
559 | << QByteArray("root\n" ); |
560 | QTest::newRow(dataTag: "path2" ) << "/searchpath2" |
561 | << ":search_file.txt" |
562 | << QByteArray("path2\n" ); |
563 | } |
564 | |
565 | void tst_QResourceEngine::searchPath_deprecated() |
566 | { |
567 | QFETCH(QString, searchPath); |
568 | QFETCH(QString, file); |
569 | QFETCH(QByteArray, expected); |
570 | |
571 | if(!searchPath.isEmpty()) |
572 | QDir::addResourceSearchPath(path: searchPath); |
573 | QFile qf(file); |
574 | QVERIFY(qf.open(QFile::ReadOnly)); |
575 | QByteArray actual = qf.readAll(); |
576 | |
577 | actual.replace(before: '\r', c: "" ); |
578 | |
579 | QCOMPARE(actual, expected); |
580 | qf.close(); |
581 | } |
582 | |
583 | #endif |
584 | |
585 | void tst_QResourceEngine::checkUnregisterResource_data() |
586 | { |
587 | QTest::addColumn<QString>(name: "rcc_file" ); |
588 | QTest::addColumn<QString>(name: "root" ); |
589 | QTest::addColumn<QString>(name: "file_check" ); |
590 | QTest::addColumn<int>(name: "size" ); |
591 | |
592 | QTest::newRow(dataTag: "currentdir.txt" ) << QFINDTESTDATA("runtime_resource.rcc" ) << QString("/check_unregister/" ) |
593 | << QString(":/check_unregister/runtime_resource/test/abc/123/+++/currentdir.txt" ) |
594 | << (int)QFileInfo(QFINDTESTDATA("testqrc/currentdir.txt" )).size(); |
595 | } |
596 | |
597 | void tst_QResourceEngine::checkUnregisterResource() |
598 | { |
599 | QFETCH(QString, rcc_file); |
600 | QFETCH(QString, root); |
601 | QFETCH(QString, file_check); |
602 | QFETCH(int, size); |
603 | |
604 | |
605 | |
606 | QVERIFY(!QFile::exists(file_check)); |
607 | QVERIFY(QResource::registerResource(rcc_file, root)); |
608 | QVERIFY(QFile::exists(file_check)); |
609 | QVERIFY(QResource::unregisterResource(rcc_file, root)); |
610 | QVERIFY(!QFile::exists(file_check)); |
611 | QVERIFY(QResource::registerResource(rcc_file, root)); |
612 | QVERIFY(QFile::exists(file_check)); |
613 | QFileInfo fileInfo(file_check); |
614 | fileInfo.setCaching(false); |
615 | QVERIFY(fileInfo.exists()); |
616 | QVERIFY(!QResource::unregisterResource(rcc_file, root)); |
617 | QVERIFY(!QFile::exists(file_check)); |
618 | QCOMPARE((int)fileInfo.size(), size); |
619 | } |
620 | |
621 | void tst_QResourceEngine::doubleSlashInRoot() |
622 | { |
623 | QVERIFY(QFile::exists(":/secondary_root/runtime_resource/search_file.txt" )); |
624 | QVERIFY(QFile::exists("://secondary_root/runtime_resource/search_file.txt" )); |
625 | } |
626 | |
627 | void tst_QResourceEngine::setLocale() |
628 | { |
629 | QLocale::setDefault(QLocale::c()); |
630 | |
631 | // default constructed QResource gets the default locale |
632 | QResource resource; |
633 | resource.setFileName("aliasdir/aliasdir.txt" ); |
634 | QCOMPARE(resource.compressionAlgorithm(), QResource::NoCompression); |
635 | |
636 | // change the default locale and make sure it doesn't affect the resource |
637 | QLocale::setDefault(QLocale("de_CH" )); |
638 | QCOMPARE(resource.compressionAlgorithm(), QResource::NoCompression); |
639 | |
640 | // then explicitly set the locale on qresource |
641 | resource.setLocale(QLocale("de_CH" )); |
642 | QVERIFY(resource.compressionAlgorithm() != QResource::NoCompression); |
643 | |
644 | // the reset the default locale back |
645 | QLocale::setDefault(QLocale::system()); |
646 | } |
647 | |
648 | void tst_QResourceEngine::lastModified() |
649 | { |
650 | { |
651 | QFileInfo fi(":/" ); |
652 | QVERIFY(fi.exists()); |
653 | QVERIFY2(!fi.lastModified().isValid(), qPrintable(fi.lastModified().toString())); |
654 | } |
655 | { |
656 | QFileInfo fi(":/search_file.txt" ); |
657 | QVERIFY(fi.exists()); |
658 | QVERIFY(fi.lastModified().isValid()); |
659 | } |
660 | } |
661 | |
662 | Q_IMPORT_PLUGIN(PluginClass) |
663 | void tst_QResourceEngine::resourcesInStaticPlugins() |
664 | { |
665 | // We built a separate static plugin and attempted linking against |
666 | // it. That should successfully register the resources linked into |
667 | // the plugin via moc generated Q_INIT_RESOURCE calls in a |
668 | // Q_CONSTRUCTOR_FUNCTION. |
669 | QVERIFY(QFile::exists(":/staticplugin/main.cpp" )); |
670 | } |
671 | |
672 | QTEST_MAIN(tst_QResourceEngine) |
673 | |
674 | #include "tst_qresourceengine.moc" |
675 | |
676 | |