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 | #include <QtTest/QtTest> |
30 | #include <qdirmodel.h> |
31 | #include <qapplication.h> |
32 | #include <qtreeview.h> |
33 | #include <qdir.h> |
34 | #include <qdebug.h> |
35 | #include "emulationdetector.h" |
36 | |
37 | QT_WARNING_DISABLE_DEPRECATED |
38 | |
39 | class tst_QDirModel : public QObject |
40 | { |
41 | Q_OBJECT |
42 | public slots: |
43 | void initTestCase(); |
44 | void cleanupTestCase(); |
45 | void init(); |
46 | private slots: |
47 | void getSetCheck(); |
48 | void unreadable(); |
49 | /* |
50 | void construct(); |
51 | void rowCount(); |
52 | void columnCount(); |
53 | void t_data(); |
54 | void setData(); |
55 | void hasChildren(); |
56 | void isEditable(); |
57 | void isDragEnabled(); |
58 | void isDropEnabled(); |
59 | void sort(); |
60 | */ |
61 | bool rowsAboutToBeRemoved_init(const QString &test_path, const QStringList &initial_files); |
62 | bool rowsAboutToBeRemoved_cleanup(const QString &test_path); |
63 | void rowsAboutToBeRemoved_data(); |
64 | void rowsAboutToBeRemoved(); |
65 | |
66 | void mkdir_data(); |
67 | void mkdir(); |
68 | |
69 | void rmdir_data(); |
70 | void rmdir(); |
71 | |
72 | void filePath(); |
73 | |
74 | void hidden(); |
75 | |
76 | void fileName(); |
77 | void fileName_data(); |
78 | void task196768_sorting(); |
79 | void filter(); |
80 | |
81 | void task244669_remove(); |
82 | |
83 | void roleNames_data(); |
84 | void roleNames(); |
85 | }; |
86 | |
87 | // Testing get/set functions |
88 | void tst_QDirModel::getSetCheck() |
89 | { |
90 | QDirModel obj1; |
91 | // QFileIconProvider * QDirModel::iconProvider() |
92 | // void QDirModel::setIconProvider(QFileIconProvider *) |
93 | QFileIconProvider *var1 = new QFileIconProvider; |
94 | obj1.setIconProvider(var1); |
95 | QCOMPARE(var1, obj1.iconProvider()); |
96 | obj1.setIconProvider((QFileIconProvider *)0); |
97 | QCOMPARE((QFileIconProvider *)0, obj1.iconProvider()); |
98 | delete var1; |
99 | |
100 | // bool QDirModel::resolveSymlinks() |
101 | // void QDirModel::setResolveSymlinks(bool) |
102 | obj1.setResolveSymlinks(false); |
103 | QCOMPARE(false, obj1.resolveSymlinks()); |
104 | obj1.setResolveSymlinks(true); |
105 | QCOMPARE(true, obj1.resolveSymlinks()); |
106 | |
107 | // bool QDirModel::lazyChildCount() |
108 | // void QDirModel::setLazyChildCount(bool) |
109 | obj1.setLazyChildCount(false); |
110 | QCOMPARE(false, obj1.lazyChildCount()); |
111 | obj1.setLazyChildCount(true); |
112 | QCOMPARE(true, obj1.lazyChildCount()); |
113 | } |
114 | |
115 | void tst_QDirModel::initTestCase() |
116 | { |
117 | #if defined(Q_OS_ANDROID) && !defined(Q_OS_ANDROID_EMBEDDED) |
118 | QString dataPath = SRCDIR; |
119 | QString resourceSourcePath = QStringLiteral(":/android_testdata" ); |
120 | QDirIterator it(resourceSourcePath, QDirIterator::Subdirectories); |
121 | while (it.hasNext()) { |
122 | it.next(); |
123 | |
124 | QFileInfo fileInfo = it.fileInfo(); |
125 | if (!fileInfo.isDir()) { |
126 | QString destination = dataPath + QLatin1Char('/') + fileInfo.filePath().mid(resourceSourcePath.length()); |
127 | QFileInfo destinationFileInfo(destination); |
128 | if (!destinationFileInfo.exists()) { |
129 | QDir().mkpath(destinationFileInfo.path()); |
130 | if (!QFile::copy(fileInfo.filePath(), destination)) |
131 | qWarning("Failed to copy %s" , qPrintable(fileInfo.filePath())); |
132 | } |
133 | } |
134 | } |
135 | #endif |
136 | } |
137 | |
138 | void tst_QDirModel::cleanupTestCase() |
139 | { |
140 | QDir current; |
141 | current.rmdir(dirName: ".qtest_hidden" ); |
142 | } |
143 | |
144 | void tst_QDirModel::init() |
145 | { |
146 | #ifdef Q_OS_UNIX |
147 | if (QTest::currentTestFunction() == QLatin1String( "unreadable" )) { |
148 | // Make sure that the unreadable file created by the unreadable() |
149 | // test function doesn't already exist. |
150 | QFile unreadableFile(QDir::currentPath() + "qtest_unreadable" ); |
151 | if (unreadableFile.exists()) { |
152 | unreadableFile.remove(); |
153 | QVERIFY(!unreadableFile.exists()); |
154 | } |
155 | } |
156 | #endif |
157 | } |
158 | |
159 | /* |
160 | tests |
161 | */ |
162 | /* |
163 | void tst_QDirModel::construct() |
164 | { |
165 | QDirModel model; |
166 | QModelIndex index = model.index(QDir::currentPath() + "/test"); |
167 | index = model.index(2, 0, index); |
168 | QVERIFY(index.isValid()); |
169 | QFileInfo info(QDir::currentPath() + "/test/file03.tst"); |
170 | QCOMPARE(model.filePath(index), info.absoluteFilePath()); |
171 | } |
172 | |
173 | void tst_QDirModel::rowCount() |
174 | { |
175 | QDirModel model; |
176 | QModelIndex index = model.index(QDir::currentPath() + "/test"); |
177 | QVERIFY(index.isValid()); |
178 | QCOMPARE(model.rowCount(index), 4); |
179 | } |
180 | |
181 | void tst_QDirModel::columnCount() |
182 | { |
183 | QDirModel model; |
184 | QModelIndex index = model.index(QDir::currentPath() + "/test"); |
185 | QVERIFY(index.isValid()); |
186 | QCOMPARE(model.columnCount(index), 4); |
187 | } |
188 | |
189 | void tst_QDirModel::t_data() |
190 | { |
191 | QDirModel model; |
192 | QModelIndex index = model.index(QDir::currentPath() + "/test"); |
193 | QVERIFY(index.isValid()); |
194 | QCOMPARE(model.rowCount(index), 4); |
195 | |
196 | index = model.index(2, 0, index); |
197 | QVERIFY(index.isValid()); |
198 | QCOMPARE(model.data(index).toString(), QString::fromLatin1("file03.tst")); |
199 | QCOMPARE(model.rowCount(index), 0); |
200 | } |
201 | |
202 | void tst_QDirModel::setData() |
203 | { |
204 | QDirModel model; |
205 | QModelIndex index = model.index(QDir::currentPath() + "/test"); |
206 | QVERIFY(index.isValid()); |
207 | |
208 | index = model.index(2, 0, index); |
209 | QVERIFY(index.isValid()); |
210 | QVERIFY(!model.setData(index, "file0X.tst", Qt::EditRole)); |
211 | } |
212 | |
213 | void tst_QDirModel::hasChildren() |
214 | { |
215 | QDirModel model; |
216 | QModelIndex index = model.index(QDir::currentPath() + "/test"); |
217 | QVERIFY(index.isValid()); |
218 | |
219 | index = model.index(2, 0, index); |
220 | QVERIFY(index.isValid()); |
221 | QVERIFY(!model.hasChildren(index)); |
222 | } |
223 | |
224 | void tst_QDirModel::isEditable() |
225 | { |
226 | QDirModel model; |
227 | QModelIndex index = model.index(QDir::currentPath() + "/test"); |
228 | QVERIFY(index.isValid()); |
229 | |
230 | index = model.index(2, 0, index); |
231 | QVERIFY(index.isValid()); |
232 | QVERIFY(!(model.flags(index) & Qt::ItemIsEditable)); |
233 | } |
234 | |
235 | void tst_QDirModel::isDragEnabled() |
236 | { |
237 | QDirModel model; |
238 | QModelIndex index = model.index(QDir::currentPath() + "/test"); |
239 | QVERIFY(index.isValid()); |
240 | |
241 | index = model.index(2, 0, index); |
242 | QVERIFY(index.isValid()); |
243 | QVERIFY(model.flags(index) & Qt::ItemIsDragEnabled); |
244 | } |
245 | |
246 | void tst_QDirModel::isDropEnabled() |
247 | { |
248 | QDirModel model; |
249 | QModelIndex index = model.index(QDir::currentPath() + "/test"); |
250 | QVERIFY(index.isValid()); |
251 | |
252 | index = model.index(2, 0, index); |
253 | QVERIFY(!(model.flags(index) & Qt::ItemIsDropEnabled)); |
254 | } |
255 | |
256 | void tst_QDirModel::sort() |
257 | { |
258 | QDirModel model; |
259 | QModelIndex parent = model.index(QDir::currentPath() + "/test"); |
260 | QVERIFY(parent.isValid()); |
261 | |
262 | QModelIndex index = model.index(0, 0, parent); |
263 | QCOMPARE(model.data(index).toString(), QString::fromLatin1("file01.tst")); |
264 | |
265 | index = model.index(3, 0, parent); |
266 | QCOMPARE(model.data(index).toString(), QString::fromLatin1("file04.tst")); |
267 | |
268 | model.sort(0, Qt::DescendingOrder); |
269 | parent = model.index(QDir::currentPath() + "/test"); |
270 | |
271 | index = model.index(0, 0, parent); |
272 | QCOMPARE(model.data(index).toString(), QString::fromLatin1("file04.tst")); |
273 | |
274 | index = model.index(3, 0, parent); |
275 | QCOMPARE(model.data(index).toString(), QString::fromLatin1("file01.tst")); |
276 | } |
277 | */ |
278 | |
279 | void tst_QDirModel::mkdir_data() |
280 | { |
281 | QTest::addColumn<QString>(name: "dirName" ); // the directory to be made under <currentpath>/dirtest |
282 | QTest::addColumn<bool>(name: "mkdirSuccess" ); |
283 | QTest::addColumn<int>(name: "rowCount" ); |
284 | |
285 | QTest::newRow(dataTag: "okDirName" ) << QString("test2" ) << true << 2; |
286 | QTest::newRow(dataTag: "existingDirName" ) << QString("test1" ) << false << 1; |
287 | QTest::newRow(dataTag: "nameWithSpace" ) << QString("ab cd" ) << true << 2; |
288 | QTest::newRow(dataTag: "emptyDirName" ) << QString("" ) << false << 1; |
289 | QTest::newRow(dataTag: "nullDirName" ) << QString() << false << 1; |
290 | |
291 | /* |
292 | QTest::newRow("recursiveDirName") << QString("test2/test3") << false << false; |
293 | QTest::newRow("singleDotDirName") << QString("./test3") << true << true; |
294 | QTest::newRow("outOfTreeDirName") << QString("../test4") << false << false; |
295 | QTest::newRow("insideTreeDirName") << QString("../dirtest/test4") << true << true; |
296 | QTest::newRow("insideTreeDirName2") << QString("./././././../dirtest/./../dirtest/test4") << true << true; |
297 | QTest::newRow("absoluteDirName") << QString(QDir::currentPath() + "/dirtest/test5") << true << true; |
298 | QTest::newRow("outOfTreeDirName") << QString(QDir::currentPath() + "/test5") << false << false; |
299 | |
300 | // Directory names only illegal on Windows |
301 | #ifdef Q_OS_WIN |
302 | QTest::newRow("illegalDirName") << QString("*") << false << false; |
303 | QTest::newRow("illegalDirName2") << QString("|") << false << false; |
304 | QTest::newRow("onlySpace") << QString(" ") << false << false; |
305 | #endif |
306 | */ |
307 | } |
308 | |
309 | void tst_QDirModel::mkdir() |
310 | { |
311 | QFETCH(QString, dirName); |
312 | QFETCH(bool, mkdirSuccess); |
313 | QFETCH(int, rowCount); |
314 | |
315 | QDirModel model; |
316 | model.setReadOnly(false); |
317 | |
318 | QModelIndex parent = model.index(SRCDIR "dirtest" ); |
319 | #ifdef Q_OS_WINRT |
320 | QEXPECT_FAIL("" , "Sandboxed applications cannot access SRCDIR - QTBUG-68297" , Abort); |
321 | #endif |
322 | QVERIFY(parent.isValid()); |
323 | QCOMPARE(model.rowCount(parent), 1); // start out with only 'test1' - in's in the depot |
324 | |
325 | QModelIndex index = model.mkdir(parent, name: dirName); |
326 | bool success = index.isValid(); |
327 | int rows = model.rowCount(parent); |
328 | |
329 | if (success && !model.rmdir(index)) |
330 | QVERIFY(QDir(SRCDIR "dirtests" ).rmdir(dirName)); |
331 | |
332 | QCOMPARE(rows, rowCount); |
333 | QCOMPARE(success, mkdirSuccess); |
334 | } |
335 | |
336 | void tst_QDirModel::rmdir_data() |
337 | { |
338 | QTest::addColumn<QString>(name: "dirName" ); // <currentpath>/dirtest/dirname |
339 | QTest::addColumn<bool>(name: "rmdirSuccess" ); |
340 | QTest::addColumn<int>(name: "rowCount" ); |
341 | |
342 | QTest::newRow(dataTag: "okDirName" ) << QString("test2" ) << true << 2; |
343 | QTest::newRow(dataTag: "existingDirName" ) << QString("test1" ) << false << 1; |
344 | QTest::newRow(dataTag: "nameWithSpace" ) << QString("ab cd" ) << true << 2; |
345 | QTest::newRow(dataTag: "emptyDirName" ) << QString("" ) << false << 1; |
346 | QTest::newRow(dataTag: "nullDirName" ) << QString() << false << 1; |
347 | } |
348 | |
349 | void tst_QDirModel::rmdir() |
350 | { |
351 | QFETCH(QString, dirName); |
352 | QFETCH(bool, rmdirSuccess); |
353 | QFETCH(int, rowCount); |
354 | |
355 | QDirModel model; |
356 | model.setReadOnly(false); |
357 | |
358 | QModelIndex parent = model.index(SRCDIR "/dirtest" ); |
359 | #ifdef Q_OS_WINRT |
360 | QEXPECT_FAIL("" , "Sandboxed applications cannot access SRCDIR - QTBUG-68297" , Abort); |
361 | #endif |
362 | QVERIFY(parent.isValid()); |
363 | QCOMPARE(model.rowCount(parent), 1); // start out with only 'test1' - in's in the depot |
364 | |
365 | QModelIndex index; |
366 | if (rmdirSuccess) { |
367 | index = model.mkdir(parent, name: dirName); |
368 | QVERIFY(index.isValid()); |
369 | } |
370 | |
371 | int rows = model.rowCount(parent); |
372 | bool success = model.rmdir(index); |
373 | |
374 | if (!success) { // cleanup |
375 | QDir dirtests(SRCDIR "/dirtests/" ); |
376 | dirtests.rmdir(dirName); |
377 | } |
378 | |
379 | QCOMPARE(rows, rowCount); |
380 | QCOMPARE(success, rmdirSuccess); |
381 | } |
382 | |
383 | void tst_QDirModel::rowsAboutToBeRemoved_data() |
384 | { |
385 | QTest::addColumn<QString>(name: "test_path" ); |
386 | QTest::addColumn<QStringList>(name: "initial_files" ); |
387 | QTest::addColumn<int>(name: "remove_row" ); |
388 | QTest::addColumn<QStringList>(name: "remove_files" ); |
389 | QTest::addColumn<QStringList>(name: "expected_files" ); |
390 | |
391 | QString test_path = "test2" ; |
392 | QStringList initial_files = (QStringList() |
393 | << "file1.tst" |
394 | << "file2.tst" |
395 | << "file3.tst" |
396 | << "file4.tst" ); |
397 | |
398 | QTest::newRow(dataTag: "removeFirstRow" ) |
399 | << test_path |
400 | << initial_files |
401 | << 0 |
402 | << (QStringList() << "file1.tst" ) |
403 | << (QStringList() << "file2.tst" << "file3.tst" << "file4.tst" ); |
404 | |
405 | QTest::newRow(dataTag: "removeMiddle" ) |
406 | << test_path |
407 | << initial_files |
408 | << 1 |
409 | << (QStringList() << "file2.tst" ) |
410 | << (QStringList() << "file1.tst" << "file3.tst" << "file4.tst" ); |
411 | |
412 | QTest::newRow(dataTag: "removeLastRow" ) |
413 | << test_path |
414 | << initial_files |
415 | << 3 |
416 | << (QStringList() << "file4.tst" ) |
417 | << (QStringList() << "file1.tst" << "file2.tst" << "file3.tst" ); |
418 | |
419 | } |
420 | |
421 | bool tst_QDirModel::rowsAboutToBeRemoved_init(const QString &test_path, const QStringList &initial_files) |
422 | { |
423 | QString path = QDir::currentPath() + QLatin1Char('/') + test_path; |
424 | if (!QDir::current().mkdir(dirName: test_path) && false) { // FIXME |
425 | qDebug() << "failed to create dir" << path; |
426 | return false; |
427 | } |
428 | |
429 | for (int i = 0; i < initial_files.count(); ++i) { |
430 | QFile file(path + QLatin1Char('/') + initial_files.at(i)); |
431 | if (!file.open(flags: QIODevice::WriteOnly)) { |
432 | qDebug() << "failed to open file" << initial_files.at(i); |
433 | return false; |
434 | } |
435 | if (!file.resize(sz: 1024)) { |
436 | qDebug() << "failed to resize file" << initial_files.at(i); |
437 | return false; |
438 | } |
439 | if (!file.flush()) { |
440 | qDebug() << "failed to flush file" << initial_files.at(i); |
441 | return false; |
442 | } |
443 | } |
444 | |
445 | return true; |
446 | } |
447 | |
448 | bool tst_QDirModel::rowsAboutToBeRemoved_cleanup(const QString &test_path) |
449 | { |
450 | QString path = QDir::currentPath() + QLatin1Char('/') + test_path; |
451 | QDir dir(path, "*" , QDir::SortFlags(QDir::Name|QDir::IgnoreCase), QDir::Files); |
452 | QStringList files = dir.entryList(); |
453 | |
454 | for (int i = 0; i < files.count(); ++i) { |
455 | if (!dir.remove(fileName: files.at(i))) { |
456 | qDebug() << "failed to remove file" << files.at(i); |
457 | return false; |
458 | } |
459 | } |
460 | |
461 | if (!QDir::current().rmdir(dirName: test_path) && false) { // FIXME |
462 | qDebug() << "failed to remove dir" << test_path; |
463 | return false; |
464 | } |
465 | |
466 | return true; |
467 | } |
468 | |
469 | void tst_QDirModel::rowsAboutToBeRemoved() |
470 | { |
471 | #ifdef Q_OS_WINRT |
472 | QSKIP("Test fails on WinRT - QTBUG-68297" ); |
473 | #endif |
474 | QFETCH(QString, test_path); |
475 | QFETCH(QStringList, initial_files); |
476 | QFETCH(int, remove_row); |
477 | QFETCH(QStringList, remove_files); |
478 | QFETCH(QStringList, expected_files); |
479 | |
480 | rowsAboutToBeRemoved_cleanup(test_path); // clean up first |
481 | QVERIFY(rowsAboutToBeRemoved_init(test_path, initial_files)); |
482 | |
483 | QDirModel model; |
484 | model.setReadOnly(false); |
485 | |
486 | |
487 | // NOTE: QDirModel will call refresh() when a file is removed. refresh() will reread the entire directory, |
488 | // and emit layoutAboutToBeChanged and layoutChange. So, instead of checking for |
489 | // rowsAboutToBeRemoved/rowsRemoved we check for layoutAboutToBeChanged/layoutChanged |
490 | QSignalSpy spy(&model, SIGNAL(layoutAboutToBeChanged())); |
491 | |
492 | QModelIndex parent = model.index(path: test_path); |
493 | QVERIFY(parent.isValid()); |
494 | |
495 | // remove the file |
496 | { |
497 | QModelIndex index = model.index(row: remove_row, column: 0, parent); |
498 | QVERIFY(index.isValid()); |
499 | QVERIFY(model.remove(index)); |
500 | } |
501 | |
502 | QCOMPARE(spy.count(), 1); |
503 | |
504 | // Compare the result |
505 | for (int row = 0; row < expected_files.count(); ++row) { |
506 | QModelIndex index = model.index(row, column: 0, parent); |
507 | QString str = index.data().toString(); |
508 | QCOMPARE(str, expected_files.at(row)); |
509 | } |
510 | |
511 | QVERIFY(rowsAboutToBeRemoved_cleanup(test_path)); |
512 | } |
513 | |
514 | void tst_QDirModel::hidden() |
515 | { |
516 | #ifndef Q_OS_UNIX |
517 | QSKIP("Test not implemented on non-Unixes" ); |
518 | #else |
519 | QDir current; |
520 | current.mkdir(dirName: ".qtest_hidden" ); |
521 | |
522 | QDirModel model; |
523 | QModelIndex index = model.index(path: QDir::currentPath() + "/.qtest_hidden" ); |
524 | //QVERIFY(!index.isValid()); // hidden items are not listed, but if you specify a valid path, it will give a valid index |
525 | |
526 | current.mkdir(dirName: ".qtest_hidden/qtest_visible" ); |
527 | QModelIndex index2 = model.index(path: QDir::currentPath() + "/.qtest_hidden/qtest_visible" ); |
528 | QVERIFY(index2.isValid()); |
529 | |
530 | QDirModel model2; |
531 | model2.setFilter(model2.filter() | QDir::Hidden); |
532 | index = model2.index(path: QDir::currentPath() + "/.qtest_hidden" ); |
533 | QVERIFY(index.isValid()); |
534 | #endif |
535 | } |
536 | |
537 | void tst_QDirModel::fileName_data() |
538 | { |
539 | QTest::addColumn<QString>(name: "path" ); |
540 | QTest::addColumn<QString>(name: "result" ); |
541 | |
542 | QTest::newRow(dataTag: "invalid" ) << "" << "" ; |
543 | //QTest::newRow("root") << "/" << "/"; |
544 | //QTest::newRow("home") << "/home" << "home"; |
545 | // TODO add symlink test too |
546 | } |
547 | |
548 | void tst_QDirModel::fileName() |
549 | { |
550 | QDirModel model; |
551 | |
552 | QFETCH(QString, path); |
553 | QFETCH(QString, result); |
554 | QCOMPARE(model.fileName(model.index(path)), result); |
555 | } |
556 | |
557 | void tst_QDirModel::unreadable() |
558 | { |
559 | #ifndef Q_OS_UNIX |
560 | QSKIP("Test not implemented on non-Unixes" ); |
561 | #else |
562 | // Create an empty file which has no read permissions (file will be removed by cleanup()). |
563 | QFile unreadableFile(QDir::currentPath() + "qtest_unreadable" ); |
564 | QVERIFY2(unreadableFile.open(QIODevice::WriteOnly | QIODevice::Text), qPrintable(unreadableFile.errorString())); |
565 | unreadableFile.close(); |
566 | QVERIFY(unreadableFile.exists()); |
567 | QVERIFY2(unreadableFile.setPermissions(QFile::WriteOwner), qPrintable(unreadableFile.errorString())); |
568 | |
569 | // Check that we can't make a valid model index from an unreadable file. |
570 | QDirModel model; |
571 | QModelIndex index = model.index(path: QDir::currentPath() + "/qtest_unreadable" ); |
572 | QVERIFY(!index.isValid()); |
573 | |
574 | // Check that unreadable files are not treated like hidden files. |
575 | QDirModel model2; |
576 | model2.setFilter(model2.filter() | QDir::Hidden); |
577 | index = model2.index(path: QDir::currentPath() + "/qtest_unreadable" ); |
578 | QVERIFY(!index.isValid()); |
579 | #endif |
580 | } |
581 | |
582 | void tst_QDirModel::filePath() |
583 | { |
584 | QFile::remove(SRCDIR "test.lnk" ); |
585 | #ifdef Q_OS_WINRT |
586 | QEXPECT_FAIL("" , "Sandboxed applications cannot access SRCDIR - QTBUG-68297" , Abort); |
587 | #endif |
588 | QVERIFY(QFile(SRCDIR "tst_qdirmodel.cpp" ).link(SRCDIR "test.lnk" )); |
589 | QDirModel model; |
590 | model.setResolveSymlinks(false); |
591 | QModelIndex index = model.index(SRCDIR "test.lnk" ); |
592 | QVERIFY(index.isValid()); |
593 | #if !defined(Q_OS_ANDROID) |
594 | QString path = SRCDIR; |
595 | #else |
596 | QString path = QFileInfo(SRCDIR).absoluteFilePath(); |
597 | if (!path.endsWith(QLatin1Char('/'))) |
598 | path += QLatin1Char('/'); |
599 | #endif |
600 | QCOMPARE(model.filePath(index), path + QString( "test.lnk" )); |
601 | model.setResolveSymlinks(true); |
602 | QCOMPARE(model.filePath(index), path + QString( "tst_qdirmodel.cpp" )); |
603 | QFile::remove(SRCDIR "test.lnk" ); |
604 | } |
605 | |
606 | void tst_QDirModel::task196768_sorting() |
607 | { |
608 | //this task showed that the persistent model indexes got corrupted when sorting |
609 | QString path = SRCDIR; |
610 | |
611 | QDirModel model; |
612 | |
613 | /* QDirModel has a bug if we show the content of the subdirectory inside a hidden directory |
614 | and we don't add QDir::Hidden. But as QDirModel is deprecated, we decided not to fix it. */ |
615 | model.setFilter(QDir::AllEntries | QDir::Hidden | QDir::AllDirs); |
616 | |
617 | QTreeView view; |
618 | QPersistentModelIndex index = model.index(path); |
619 | view.setModel(&model); |
620 | QModelIndex index2 = model.index(path); |
621 | QCOMPARE(index.data(), index2.data()); |
622 | view.setRootIndex(index); |
623 | index2 = model.index(path); |
624 | QCOMPARE(index.data(), index2.data()); |
625 | view.setCurrentIndex(index); |
626 | index2 = model.index(path); |
627 | QCOMPARE(index.data(), index2.data()); |
628 | view.setSortingEnabled(true); |
629 | index2 = model.index(path); |
630 | |
631 | #if defined(Q_OS_ANDROID) && !defined(Q_OS_ANDROID_EMBEDDED) |
632 | QEXPECT_FAIL("" , "QTBUG-43818" , Continue); |
633 | #else |
634 | if (EmulationDetector::isRunningArmOnX86()) |
635 | QEXPECT_FAIL("" , "QTBUG-43818" , Continue); |
636 | #endif |
637 | |
638 | QCOMPARE(index.data(), index2.data()); |
639 | } |
640 | |
641 | void tst_QDirModel::filter() |
642 | { |
643 | QDirModel model; |
644 | model.setNameFilters(QStringList() << "*.nada" ); |
645 | QModelIndex index = model.index(SRCDIR "test" ); |
646 | #ifdef Q_OS_WINRT |
647 | QEXPECT_FAIL("" , "Sandboxed applications cannot access SRCDIR - QTBUG-68297" , Abort); |
648 | #endif |
649 | QCOMPARE(model.rowCount(index), 0); |
650 | QModelIndex index2 = model.index(SRCDIR "test/file01.tst" ); |
651 | QVERIFY(!index2.isValid()); |
652 | QCOMPARE(model.rowCount(index), 0); |
653 | } |
654 | |
655 | void tst_QDirModel::task244669_remove() |
656 | { |
657 | QFile f1(SRCDIR "dirtest/f1.txt" ); |
658 | #ifdef Q_OS_WINRT |
659 | QEXPECT_FAIL("" , "Sandboxed applications cannot access SRCDIR - QTBUG-68297" , Abort); |
660 | #endif |
661 | QVERIFY(f1.open(QIODevice::WriteOnly)); |
662 | f1.close(); |
663 | QFile f2(SRCDIR "dirtest/f2.txt" ); |
664 | QVERIFY(f2.open(QIODevice::WriteOnly)); |
665 | f2.close(); |
666 | |
667 | QDirModel model; |
668 | model.setReadOnly(false); |
669 | QPersistentModelIndex parent = model.index(SRCDIR "dirtest" ); |
670 | QPersistentModelIndex index2 = model.index(SRCDIR "dirtest/f2.txt" ); |
671 | QPersistentModelIndex index1 = model.index(SRCDIR "dirtest/f1.txt" ); |
672 | |
673 | QVERIFY(parent.isValid()); |
674 | QVERIFY(index1.isValid()); |
675 | QVERIFY(index2.isValid()); |
676 | QCOMPARE(parent.data() , model.index(SRCDIR "dirtest" ).data()); |
677 | QCOMPARE(index1.data() , model.index(SRCDIR "dirtest/f1.txt" ).data()); |
678 | QCOMPARE(index2.data() , model.index(SRCDIR "dirtest/f2.txt" ).data()); |
679 | |
680 | QVERIFY(model.remove(index1)); |
681 | |
682 | QVERIFY(parent.isValid()); |
683 | QVERIFY(!index1.isValid()); |
684 | QVERIFY(index2.isValid()); |
685 | QCOMPARE(parent.data() , model.index(SRCDIR "dirtest" ).data()); |
686 | QCOMPARE(index2.data() , model.index(SRCDIR "dirtest/f2.txt" ).data()); |
687 | |
688 | QVERIFY(model.remove(index2)); |
689 | |
690 | QVERIFY(parent.isValid()); |
691 | QVERIFY(!index2.isValid()); |
692 | QVERIFY(!index1.isValid()); |
693 | QCOMPARE(parent.data() , model.index(SRCDIR "dirtest" ).data()); |
694 | } |
695 | |
696 | void tst_QDirModel::roleNames_data() |
697 | { |
698 | QTest::addColumn<int>(name: "role" ); |
699 | QTest::addColumn<QByteArray>(name: "roleName" ); |
700 | QTest::newRow(dataTag: "decoration" ) << int(Qt::DecorationRole) << QByteArray("fileIcon" ); |
701 | QTest::newRow(dataTag: "display" ) << int(Qt::DisplayRole) << QByteArray("display" ); |
702 | QTest::newRow(dataTag: "fileIcon" ) << int(QDirModel::FileIconRole) << QByteArray("fileIcon" ); |
703 | QTest::newRow(dataTag: "filePath" ) << int(QDirModel::FilePathRole) << QByteArray("filePath" ); |
704 | QTest::newRow(dataTag: "fileName" ) << int(QDirModel::FileNameRole) << QByteArray("fileName" ); |
705 | } |
706 | |
707 | void tst_QDirModel::roleNames() |
708 | { |
709 | QDirModel model; |
710 | QHash<int, QByteArray> roles = model.roleNames(); |
711 | |
712 | QFETCH(int, role); |
713 | QVERIFY(roles.contains(role)); |
714 | |
715 | QFETCH(QByteArray, roleName); |
716 | QCOMPARE(roles.values(role).count(), 1); |
717 | QCOMPARE(roles.value(role), roleName); |
718 | } |
719 | |
720 | |
721 | QTEST_MAIN(tst_QDirModel) |
722 | #include "tst_qdirmodel.moc" |
723 | |