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