|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the test suite of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 |
|
43 #include <QtTest/QtTest> |
|
44 #include <qdirmodel.h> |
|
45 #include <qapplication.h> |
|
46 #include <qtreeview.h> |
|
47 #include <qdir.h> |
|
48 #include <qdebug.h> |
|
49 |
|
50 //TESTED_CLASS= |
|
51 //TESTED_FILES= |
|
52 |
|
53 #if defined(Q_OS_SYMBIAN) |
|
54 # define STRINGIFY(x) #x |
|
55 # define TOSTRING(x) STRINGIFY(x) |
|
56 # define SRCDIR "C:/Private/" TOSTRING(SYMBIAN_SRCDIR_UID) "/" |
|
57 #endif |
|
58 |
|
59 class tst_QDirModel : public QObject |
|
60 { |
|
61 Q_OBJECT |
|
62 |
|
63 public: |
|
64 |
|
65 tst_QDirModel(); |
|
66 virtual ~tst_QDirModel(); |
|
67 |
|
68 |
|
69 public slots: |
|
70 void initTestCase(); |
|
71 void cleanupTestCase(); |
|
72 void init(); |
|
73 void cleanup(); |
|
74 private slots: |
|
75 void getSetCheck(); |
|
76 /* |
|
77 void construct(); |
|
78 void rowCount(); |
|
79 void columnCount(); |
|
80 void t_data(); |
|
81 void setData(); |
|
82 void hasChildren(); |
|
83 void isEditable(); |
|
84 void isDragEnabled(); |
|
85 void isDropEnabled(); |
|
86 void sort(); |
|
87 */ |
|
88 bool rowsAboutToBeRemoved_init(const QString &test_path, const QStringList &initial_files); |
|
89 bool rowsAboutToBeRemoved_cleanup(const QString &test_path); |
|
90 void rowsAboutToBeRemoved_data(); |
|
91 void rowsAboutToBeRemoved(); |
|
92 |
|
93 void mkdir_data(); |
|
94 void mkdir(); |
|
95 |
|
96 void rmdir_data(); |
|
97 void rmdir(); |
|
98 |
|
99 void filePath(); |
|
100 |
|
101 void hidden(); |
|
102 |
|
103 void fileName(); |
|
104 void fileName_data(); |
|
105 void task196768_sorting(); |
|
106 void filter(); |
|
107 |
|
108 void task244669_remove(); |
|
109 }; |
|
110 |
|
111 // Testing get/set functions |
|
112 void tst_QDirModel::getSetCheck() |
|
113 { |
|
114 QDirModel obj1; |
|
115 // QFileIconProvider * QDirModel::iconProvider() |
|
116 // void QDirModel::setIconProvider(QFileIconProvider *) |
|
117 QFileIconProvider *var1 = new QFileIconProvider; |
|
118 obj1.setIconProvider(var1); |
|
119 QCOMPARE(var1, obj1.iconProvider()); |
|
120 obj1.setIconProvider((QFileIconProvider *)0); |
|
121 QCOMPARE((QFileIconProvider *)0, obj1.iconProvider()); |
|
122 delete var1; |
|
123 |
|
124 // bool QDirModel::resolveSymlinks() |
|
125 // void QDirModel::setResolveSymlinks(bool) |
|
126 obj1.setResolveSymlinks(false); |
|
127 QCOMPARE(false, obj1.resolveSymlinks()); |
|
128 obj1.setResolveSymlinks(true); |
|
129 QCOMPARE(true, obj1.resolveSymlinks()); |
|
130 |
|
131 // bool QDirModel::lazyChildCount() |
|
132 // void QDirModel::setLazyChildCount(bool) |
|
133 obj1.setLazyChildCount(false); |
|
134 QCOMPARE(false, obj1.lazyChildCount()); |
|
135 obj1.setLazyChildCount(true); |
|
136 QCOMPARE(true, obj1.lazyChildCount()); |
|
137 } |
|
138 |
|
139 |
|
140 Q_DECLARE_METATYPE(QModelIndex) |
|
141 Q_DECLARE_METATYPE(QModelIndexList) |
|
142 |
|
143 tst_QDirModel::tst_QDirModel() |
|
144 |
|
145 { |
|
146 } |
|
147 |
|
148 tst_QDirModel::~tst_QDirModel() |
|
149 { |
|
150 } |
|
151 |
|
152 void tst_QDirModel::initTestCase() |
|
153 { |
|
154 } |
|
155 |
|
156 void tst_QDirModel::cleanupTestCase() |
|
157 { |
|
158 QDir current; |
|
159 current.rmdir(".qtest_hidden"); |
|
160 } |
|
161 |
|
162 void tst_QDirModel::init() |
|
163 { |
|
164 } |
|
165 |
|
166 void tst_QDirModel::cleanup() |
|
167 { |
|
168 } |
|
169 |
|
170 /* |
|
171 tests |
|
172 */ |
|
173 /* |
|
174 void tst_QDirModel::construct() |
|
175 { |
|
176 QDirModel model; |
|
177 QModelIndex index = model.index(QDir::currentPath() + "/test"); |
|
178 index = model.index(2, 0, index); |
|
179 QVERIFY(index.isValid()); |
|
180 QFileInfo info(QDir::currentPath() + "/test/file03.tst"); |
|
181 QCOMPARE(model.filePath(index), info.absoluteFilePath()); |
|
182 } |
|
183 |
|
184 void tst_QDirModel::rowCount() |
|
185 { |
|
186 QDirModel model; |
|
187 QModelIndex index = model.index(QDir::currentPath() + "/test"); |
|
188 QVERIFY(index.isValid()); |
|
189 QCOMPARE(model.rowCount(index), 4); |
|
190 } |
|
191 |
|
192 void tst_QDirModel::columnCount() |
|
193 { |
|
194 QDirModel model; |
|
195 QModelIndex index = model.index(QDir::currentPath() + "/test"); |
|
196 QVERIFY(index.isValid()); |
|
197 QCOMPARE(model.columnCount(index), 4); |
|
198 } |
|
199 |
|
200 void tst_QDirModel::t_data() |
|
201 { |
|
202 QDirModel model; |
|
203 QModelIndex index = model.index(QDir::currentPath() + "/test"); |
|
204 QVERIFY(index.isValid()); |
|
205 QCOMPARE(model.rowCount(index), 4); |
|
206 |
|
207 index = model.index(2, 0, index); |
|
208 QVERIFY(index.isValid()); |
|
209 QCOMPARE(model.data(index).toString(), QString::fromLatin1("file03.tst")); |
|
210 QCOMPARE(model.rowCount(index), 0); |
|
211 } |
|
212 |
|
213 void tst_QDirModel::setData() |
|
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.setData(index, "file0X.tst", Qt::EditRole)); |
|
222 } |
|
223 |
|
224 void tst_QDirModel::hasChildren() |
|
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.hasChildren(index)); |
|
233 } |
|
234 |
|
235 void tst_QDirModel::isEditable() |
|
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::ItemIsEditable)); |
|
244 } |
|
245 |
|
246 void tst_QDirModel::isDragEnabled() |
|
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(index.isValid()); |
|
254 QVERIFY(model.flags(index) & Qt::ItemIsDragEnabled); |
|
255 } |
|
256 |
|
257 void tst_QDirModel::isDropEnabled() |
|
258 { |
|
259 QDirModel model; |
|
260 QModelIndex index = model.index(QDir::currentPath() + "/test"); |
|
261 QVERIFY(index.isValid()); |
|
262 |
|
263 index = model.index(2, 0, index); |
|
264 QVERIFY(!(model.flags(index) & Qt::ItemIsDropEnabled)); |
|
265 } |
|
266 |
|
267 void tst_QDirModel::sort() |
|
268 { |
|
269 QDirModel model; |
|
270 QModelIndex parent = model.index(QDir::currentPath() + "/test"); |
|
271 QVERIFY(parent.isValid()); |
|
272 |
|
273 QModelIndex index = model.index(0, 0, parent); |
|
274 QCOMPARE(model.data(index).toString(), QString::fromLatin1("file01.tst")); |
|
275 |
|
276 index = model.index(3, 0, parent); |
|
277 QCOMPARE(model.data(index).toString(), QString::fromLatin1("file04.tst")); |
|
278 |
|
279 model.sort(0, Qt::DescendingOrder); |
|
280 parent = model.index(QDir::currentPath() + "/test"); |
|
281 |
|
282 index = model.index(0, 0, parent); |
|
283 QCOMPARE(model.data(index).toString(), QString::fromLatin1("file04.tst")); |
|
284 |
|
285 index = model.index(3, 0, parent); |
|
286 QCOMPARE(model.data(index).toString(), QString::fromLatin1("file01.tst")); |
|
287 } |
|
288 */ |
|
289 |
|
290 void tst_QDirModel::mkdir_data() |
|
291 { |
|
292 QTest::addColumn<QString>("dirName"); // the directory to be made under <currentpath>/dirtest |
|
293 QTest::addColumn<bool>("mkdirSuccess"); |
|
294 QTest::addColumn<int>("rowCount"); |
|
295 |
|
296 QTest::newRow("okDirName") << QString("test2") << true << 2; |
|
297 QTest::newRow("existingDirName") << QString("test1") << false << 1; |
|
298 QTest::newRow("nameWithSpace") << QString("ab cd") << true << 2; |
|
299 QTest::newRow("emptyDirName") << QString("") << false << 1; |
|
300 QTest::newRow("nullDirName") << QString() << false << 1; |
|
301 |
|
302 /* |
|
303 QTest::newRow("recursiveDirName") << QString("test2/test3") << false << false; |
|
304 QTest::newRow("singleDotDirName") << QString("./test3") << true << true; |
|
305 QTest::newRow("outOfTreeDirName") << QString("../test4") << false << false; |
|
306 QTest::newRow("insideTreeDirName") << QString("../dirtest/test4") << true << true; |
|
307 QTest::newRow("insideTreeDirName2") << QString("./././././../dirtest/./../dirtest/test4") << true << true; |
|
308 QTest::newRow("absoluteDirName") << QString(QDir::currentPath() + "/dirtest/test5") << true << true; |
|
309 QTest::newRow("outOfTreeDirName") << QString(QDir::currentPath() + "/test5") << false << false; |
|
310 |
|
311 // Directory names only illegal on Windows |
|
312 #ifdef Q_WS_WIN |
|
313 QTest::newRow("illegalDirName") << QString("*") << false << false; |
|
314 QTest::newRow("illegalDirName2") << QString("|") << false << false; |
|
315 QTest::newRow("onlySpace") << QString(" ") << false << false; |
|
316 #endif |
|
317 */ |
|
318 } |
|
319 |
|
320 void tst_QDirModel::mkdir() |
|
321 { |
|
322 QFETCH(QString, dirName); |
|
323 QFETCH(bool, mkdirSuccess); |
|
324 QFETCH(int, rowCount); |
|
325 |
|
326 QDirModel model; |
|
327 model.setReadOnly(false); |
|
328 |
|
329 QModelIndex parent = model.index(SRCDIR "dirtest"); |
|
330 QVERIFY(parent.isValid()); |
|
331 QCOMPARE(model.rowCount(parent), 1); // start out with only 'test1' - in's in the depot |
|
332 |
|
333 QModelIndex index = model.mkdir(parent, dirName); |
|
334 bool success = index.isValid(); |
|
335 int rows = model.rowCount(parent); |
|
336 |
|
337 if (success && !model.rmdir(index)) |
|
338 QVERIFY(QDir(SRCDIR "dirtests").rmdir(dirName)); |
|
339 |
|
340 QCOMPARE(rows, rowCount); |
|
341 QCOMPARE(success, mkdirSuccess); |
|
342 } |
|
343 |
|
344 void tst_QDirModel::rmdir_data() |
|
345 { |
|
346 QTest::addColumn<QString>("dirName"); // <currentpath>/dirtest/dirname |
|
347 QTest::addColumn<bool>("rmdirSuccess"); |
|
348 QTest::addColumn<int>("rowCount"); |
|
349 |
|
350 QTest::newRow("okDirName") << QString("test2") << true << 2; |
|
351 QTest::newRow("existingDirName") << QString("test1") << false << 1; |
|
352 QTest::newRow("nameWithSpace") << QString("ab cd") << true << 2; |
|
353 QTest::newRow("emptyDirName") << QString("") << false << 1; |
|
354 QTest::newRow("nullDirName") << QString() << false << 1; |
|
355 } |
|
356 |
|
357 void tst_QDirModel::rmdir() |
|
358 { |
|
359 QFETCH(QString, dirName); |
|
360 QFETCH(bool, rmdirSuccess); |
|
361 QFETCH(int, rowCount); |
|
362 |
|
363 QDirModel model; |
|
364 model.setReadOnly(false); |
|
365 |
|
366 QModelIndex parent = model.index(SRCDIR "/dirtest"); |
|
367 QVERIFY(parent.isValid()); |
|
368 QCOMPARE(model.rowCount(parent), 1); // start out with only 'test1' - in's in the depot |
|
369 |
|
370 QModelIndex index; |
|
371 if (rmdirSuccess) { |
|
372 index = model.mkdir(parent, dirName); |
|
373 QVERIFY(index.isValid()); |
|
374 } |
|
375 |
|
376 int rows = model.rowCount(parent); |
|
377 bool success = model.rmdir(index); |
|
378 |
|
379 if (!success) { // cleanup |
|
380 QDir dirtests(SRCDIR "/dirtests/"); |
|
381 dirtests.rmdir(dirName); |
|
382 } |
|
383 |
|
384 QCOMPARE(rows, rowCount); |
|
385 QCOMPARE(success, rmdirSuccess); |
|
386 } |
|
387 |
|
388 void tst_QDirModel::rowsAboutToBeRemoved_data() |
|
389 { |
|
390 QTest::addColumn<QString>("test_path"); |
|
391 QTest::addColumn<QStringList>("initial_files"); |
|
392 QTest::addColumn<int>("remove_row"); |
|
393 QTest::addColumn<QStringList>("remove_files"); |
|
394 QTest::addColumn<QStringList>("expected_files"); |
|
395 |
|
396 QString test_path = "test2"; |
|
397 QStringList initial_files = (QStringList() |
|
398 << "file1.tst" |
|
399 << "file2.tst" |
|
400 << "file3.tst" |
|
401 << "file4.tst"); |
|
402 |
|
403 QTest::newRow("removeFirstRow") |
|
404 << test_path |
|
405 << initial_files |
|
406 << 0 |
|
407 << (QStringList() << "file1.tst") |
|
408 << (QStringList() << "file2.tst" << "file3.tst" << "file4.tst"); |
|
409 |
|
410 QTest::newRow("removeMiddle") |
|
411 << test_path |
|
412 << initial_files |
|
413 << 1 |
|
414 << (QStringList() << "file2.tst") |
|
415 << (QStringList() << "file1.tst" << "file3.tst" << "file4.tst"); |
|
416 |
|
417 QTest::newRow("removeLastRow") |
|
418 << test_path |
|
419 << initial_files |
|
420 << 3 |
|
421 << (QStringList() << "file4.tst") |
|
422 << (QStringList() << "file1.tst" << "file2.tst" << "file3.tst"); |
|
423 |
|
424 } |
|
425 |
|
426 bool tst_QDirModel::rowsAboutToBeRemoved_init(const QString &test_path, const QStringList &initial_files) |
|
427 { |
|
428 QString path = QDir::currentPath() + "/" + test_path; |
|
429 if (!QDir::current().mkdir(test_path) && false) { // FIXME |
|
430 qDebug() << "failed to create dir" << path; |
|
431 return false; |
|
432 } |
|
433 |
|
434 for (int i = 0; i < initial_files.count(); ++i) { |
|
435 QFile file(path + "/" + initial_files.at(i)); |
|
436 if (!file.open(QIODevice::WriteOnly)) { |
|
437 qDebug() << "failed to open file" << initial_files.at(i); |
|
438 return false; |
|
439 } |
|
440 if (!file.resize(1024)) { |
|
441 qDebug() << "failed to resize file" << initial_files.at(i); |
|
442 return false; |
|
443 } |
|
444 if (!file.flush()) { |
|
445 qDebug() << "failed to flush file" << initial_files.at(i); |
|
446 return false; |
|
447 } |
|
448 } |
|
449 |
|
450 return true; |
|
451 } |
|
452 |
|
453 bool tst_QDirModel::rowsAboutToBeRemoved_cleanup(const QString &test_path) |
|
454 { |
|
455 QString path = QDir::currentPath() + "/" + test_path; |
|
456 QDir dir(path, "*", QDir::SortFlags(QDir::Name|QDir::IgnoreCase), QDir::Files); |
|
457 QStringList files = dir.entryList(); |
|
458 |
|
459 for (int i = 0; i < files.count(); ++i) { |
|
460 if (!dir.remove(files.at(i))) { |
|
461 qDebug() << "failed to remove file" << files.at(i); |
|
462 return false; |
|
463 } |
|
464 } |
|
465 |
|
466 if (!QDir::current().rmdir(test_path) && false) { // FIXME |
|
467 qDebug() << "failed to remove dir" << test_path; |
|
468 return false; |
|
469 } |
|
470 |
|
471 return true; |
|
472 } |
|
473 |
|
474 void tst_QDirModel::rowsAboutToBeRemoved() |
|
475 { |
|
476 QFETCH(QString, test_path); |
|
477 QFETCH(QStringList, initial_files); |
|
478 QFETCH(int, remove_row); |
|
479 QFETCH(QStringList, remove_files); |
|
480 QFETCH(QStringList, expected_files); |
|
481 |
|
482 rowsAboutToBeRemoved_cleanup(test_path); // clean up first |
|
483 QVERIFY(rowsAboutToBeRemoved_init(test_path, initial_files)); |
|
484 |
|
485 QDirModel model; |
|
486 model.setReadOnly(false); |
|
487 |
|
488 qRegisterMetaType<QModelIndex>("QModelIndex"); |
|
489 |
|
490 // NOTE: QDirModel will call refresh() when a file is removed. refresh() will reread the entire directory, |
|
491 // and emit layoutAboutToBeChanged and layoutChange. So, instead of checking for |
|
492 // rowsAboutToBeRemoved/rowsRemoved we check for layoutAboutToBeChanged/layoutChanged |
|
493 QSignalSpy spy(&model, SIGNAL(layoutAboutToBeChanged())); |
|
494 |
|
495 QModelIndex parent = model.index(test_path); |
|
496 QVERIFY(parent.isValid()); |
|
497 |
|
498 // remove the file |
|
499 { |
|
500 QModelIndex index = model.index(remove_row, 0, parent); |
|
501 QVERIFY(index.isValid()); |
|
502 QVERIFY(model.remove(index)); |
|
503 } |
|
504 |
|
505 QCOMPARE(spy.count(), 1); |
|
506 |
|
507 // Compare the result |
|
508 for (int row = 0; row < expected_files.count(); ++row) { |
|
509 QModelIndex index = model.index(row, 0, parent); |
|
510 QString str = index.data().toString(); |
|
511 QCOMPARE(str, expected_files.at(row)); |
|
512 } |
|
513 |
|
514 QVERIFY(rowsAboutToBeRemoved_cleanup(test_path)); |
|
515 } |
|
516 |
|
517 void tst_QDirModel::hidden() |
|
518 { |
|
519 #ifdef Q_OS_UNIX |
|
520 QDir current; |
|
521 current.mkdir(".qtest_hidden"); |
|
522 |
|
523 QDirModel model; |
|
524 QModelIndex index = model.index(QDir::currentPath() + "/.qtest_hidden"); |
|
525 //QVERIFY(!index.isValid()); // hidden items are not listed, but if you specify a valid path, it will give a valid index |
|
526 |
|
527 current.mkdir(".qtest_hidden/qtest_visible"); |
|
528 QModelIndex index2 = model.index(QDir::currentPath() + "/.qtest_hidden/qtest_visible"); |
|
529 QVERIFY(index2.isValid()); |
|
530 |
|
531 QDirModel model2; |
|
532 model2.setFilter(model2.filter() | QDir::Hidden); |
|
533 index = model2.index(QDir::currentPath() + "/.qtest_hidden"); |
|
534 QVERIFY(index.isValid()); |
|
535 #else |
|
536 QSKIP("Test not implemented on non-Unixes", SkipAll); |
|
537 #endif |
|
538 } |
|
539 |
|
540 void tst_QDirModel::fileName_data() |
|
541 { |
|
542 QTest::addColumn<QString>("path"); |
|
543 QTest::addColumn<QString>("result"); |
|
544 |
|
545 QTest::newRow("invalid") << "" << ""; |
|
546 //QTest::newRow("root") << "/" << "/"; |
|
547 //QTest::newRow("home") << "/home" << "home"; |
|
548 // TODO add symlink test too |
|
549 } |
|
550 |
|
551 void tst_QDirModel::fileName() |
|
552 { |
|
553 QDirModel model; |
|
554 |
|
555 QFETCH(QString, path); |
|
556 QFETCH(QString, result); |
|
557 QCOMPARE(model.fileName(model.index(path)), result); |
|
558 } |
|
559 |
|
560 #if 0 |
|
561 void tst_QDirModel::unreadable() |
|
562 { |
|
563 #ifdef Q_OS_UNIX |
|
564 //QFile current("qtest_unreadable"); |
|
565 //QVERIFY(current.setPermissions(QFile::WriteOwner)); |
|
566 |
|
567 QDirModel model; |
|
568 QModelIndex index = model.index(QDir::currentPath() + "/qtest_unreadable"); |
|
569 QVERIFY(!index.isValid()); |
|
570 |
|
571 QDirModel model2; |
|
572 model2.setFilter(model2.filter() | QDir::Hidden); |
|
573 index = model2.index(QDir::currentPath() + "/qtest_unreadable"); |
|
574 QVERIFY(index.isValid()); |
|
575 #else |
|
576 QSKIP("Test not implemented on non-Unixes", SkipAll); |
|
577 #endif |
|
578 } |
|
579 #endif |
|
580 |
|
581 void tst_QDirModel::filePath() |
|
582 { |
|
583 QFile::remove(SRCDIR "test.lnk"); |
|
584 QVERIFY(QFile(SRCDIR "tst_qdirmodel.cpp").link(SRCDIR "test.lnk")); |
|
585 QDirModel model; |
|
586 model.setResolveSymlinks(false); |
|
587 QModelIndex index = model.index(SRCDIR "test.lnk"); |
|
588 QVERIFY(index.isValid()); |
|
589 #if defined(Q_OS_SYMBIAN) |
|
590 // Since model will force lowercase path in Symbian, make case insensitive compare |
|
591 // Note: Windows should fail this, too, if test path has any uppercase letters. |
|
592 QCOMPARE(model.filePath(index).toLower(), QString(SRCDIR).toLower() + "test.lnk"); |
|
593 model.setResolveSymlinks(true); |
|
594 QCOMPARE(model.filePath(index).toLower(), QString(SRCDIR).toLower() + "tst_qdirmodel.cpp"); |
|
595 #else |
|
596 #ifndef Q_OS_WINCE |
|
597 QString path = SRCDIR; |
|
598 #else |
|
599 QString path = QFileInfo(SRCDIR).absoluteFilePath() + "/"; |
|
600 #endif |
|
601 QCOMPARE(model.filePath(index), path + QString( "test.lnk")); |
|
602 model.setResolveSymlinks(true); |
|
603 QCOMPARE(model.filePath(index), path + QString( "tst_qdirmodel.cpp")); |
|
604 #endif |
|
605 QFile::remove(SRCDIR "test.lnk"); |
|
606 } |
|
607 |
|
608 void tst_QDirModel::task196768_sorting() |
|
609 { |
|
610 //this task showed that the persistent model indexes got corrupted when sorting |
|
611 QString path = SRCDIR; |
|
612 |
|
613 QDirModel model; |
|
614 |
|
615 /* QDirModel has a bug if we show the content of the subdirectory inside a hidden directory |
|
616 and we don't add QDir::Hidden. But as QDirModel is deprecated, we decided not to fix it. */ |
|
617 model.setFilter(QDir::AllEntries | QDir::Hidden | QDir::AllDirs); |
|
618 |
|
619 QTreeView view; |
|
620 QPersistentModelIndex index = model.index(path); |
|
621 view.setModel(&model); |
|
622 QModelIndex index2 = model.index(path); |
|
623 QCOMPARE(index.data(), index2.data()); |
|
624 view.setRootIndex(index); |
|
625 index2 = model.index(path); |
|
626 QCOMPARE(index.data(), index2.data()); |
|
627 view.setCurrentIndex(index); |
|
628 index2 = model.index(path); |
|
629 QCOMPARE(index.data(), index2.data()); |
|
630 view.setSortingEnabled(true); |
|
631 index2 = model.index(path); |
|
632 QCOMPARE(index.data(), index2.data()); |
|
633 } |
|
634 |
|
635 void tst_QDirModel::filter() |
|
636 { |
|
637 QDirModel model; |
|
638 model.setNameFilters(QStringList() << "*.nada"); |
|
639 QModelIndex index = model.index(SRCDIR "test"); |
|
640 Q_ASSERT(model.rowCount(index) == 0); |
|
641 QModelIndex index2 = model.index(SRCDIR "test/file01.tst"); |
|
642 Q_ASSERT(!index2.isValid()); |
|
643 Q_ASSERT(model.rowCount(index) == 0); |
|
644 } |
|
645 |
|
646 void tst_QDirModel::task244669_remove() |
|
647 { |
|
648 QFile f1(SRCDIR "dirtest/f1.txt"); |
|
649 QVERIFY(f1.open(QIODevice::WriteOnly)); |
|
650 f1.close(); |
|
651 QFile f2(SRCDIR "dirtest/f2.txt"); |
|
652 QVERIFY(f2.open(QIODevice::WriteOnly)); |
|
653 f2.close(); |
|
654 |
|
655 QDirModel model; |
|
656 model.setReadOnly(false); |
|
657 QPersistentModelIndex parent = model.index(SRCDIR "dirtest"); |
|
658 QPersistentModelIndex index2 = model.index(SRCDIR "dirtest/f2.txt"); |
|
659 QPersistentModelIndex index1 = model.index(SRCDIR "dirtest/f1.txt"); |
|
660 |
|
661 QVERIFY(parent.isValid()); |
|
662 QVERIFY(index1.isValid()); |
|
663 QVERIFY(index2.isValid()); |
|
664 QCOMPARE(parent.data() , model.index(SRCDIR "dirtest").data()); |
|
665 QCOMPARE(index1.data() , model.index(SRCDIR "dirtest/f1.txt").data()); |
|
666 QCOMPARE(index2.data() , model.index(SRCDIR "dirtest/f2.txt").data()); |
|
667 |
|
668 QVERIFY(model.remove(index1)); |
|
669 |
|
670 QVERIFY(parent.isValid()); |
|
671 QVERIFY(!index1.isValid()); |
|
672 QVERIFY(index2.isValid()); |
|
673 QCOMPARE(parent.data() , model.index(SRCDIR "dirtest").data()); |
|
674 QCOMPARE(index2.data() , model.index(SRCDIR "dirtest/f2.txt").data()); |
|
675 |
|
676 QVERIFY(model.remove(index2)); |
|
677 |
|
678 QVERIFY(parent.isValid()); |
|
679 QVERIFY(!index2.isValid()); |
|
680 QVERIFY(!index1.isValid()); |
|
681 QCOMPARE(parent.data() , model.index(SRCDIR "dirtest").data()); |
|
682 } |
|
683 |
|
684 QTEST_MAIN(tst_QDirModel) |
|
685 #include "tst_qdirmodel.moc" |