author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Tue, 06 Jul 2010 15:10:48 +0300 | |
changeset 30 | 5dc02b23752f |
parent 18 | 2f34d5167611 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 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 "../../../src/gui/dialogs/qfilesystemmodel_p.h" |
|
45 |
#include <QFileIconProvider> |
|
46 |
#include <QTreeView> |
|
47 |
#include <QHeaderView> |
|
48 |
#include "../../shared/util.h" |
|
49 |
#include <QTime> |
|
50 |
#include <QStyle> |
|
51 |
#include <QtGlobal> |
|
52 |
#if defined(Q_OS_SYMBIAN) |
|
53 |
# include <f32file.h> |
|
54 |
#endif |
|
55 |
//TESTED_CLASS= |
|
56 |
//TESTED_FILES= |
|
57 |
||
58 |
#define WAITTIME 1000 |
|
59 |
||
60 |
// Will try to wait for the condition while allowing event processing |
|
61 |
// for a maximum of 5 seconds. |
|
62 |
#define TRY_WAIT(expr) \ |
|
63 |
do { \ |
|
64 |
const int step = 50; \ |
|
65 |
for (int __i = 0; __i < 5000 && !(expr); __i+=step) { \ |
|
66 |
QTest::qWait(step); \ |
|
67 |
} \ |
|
68 |
} while(0) |
|
69 |
||
70 |
#if defined(Q_OS_SYMBIAN) |
|
71 |
static HBufC* qt_QString2HBufCNewL(const QString& aString) |
|
72 |
{ |
|
73 |
HBufC *buffer; |
|
74 |
#ifdef QT_NO_UNICODE |
|
75 |
TPtrC8 ptr(reinterpret_cast<const TUint8*>(aString.toLocal8Bit().constData())); |
|
76 |
buffer = HBufC8::NewL(ptr.Length()); |
|
77 |
buffer->Des().Copy(ptr); |
|
78 |
#else |
|
79 |
TPtrC16 ptr(reinterpret_cast<const TUint16*>(aString.utf16())); |
|
80 |
buffer = HBufC16::NewL(ptr.Length()); |
|
81 |
buffer->Des().Copy(ptr); |
|
82 |
#endif |
|
83 |
return buffer; |
|
84 |
} |
|
85 |
#endif |
|
86 |
||
87 |
class tst_QFileSystemModel : public QObject { |
|
88 |
Q_OBJECT |
|
89 |
||
90 |
public: |
|
91 |
tst_QFileSystemModel(); |
|
92 |
virtual ~tst_QFileSystemModel(); |
|
93 |
||
94 |
public Q_SLOTS: |
|
95 |
void init(); |
|
96 |
void cleanup(); |
|
97 |
||
98 |
private slots: |
|
99 |
void indexPath(); |
|
100 |
||
101 |
void rootPath(); |
|
102 |
void naturalCompare_data(); |
|
103 |
void naturalCompare(); |
|
104 |
void readOnly(); |
|
105 |
void iconProvider(); |
|
106 |
||
107 |
void rowCount(); |
|
108 |
||
109 |
void rowsInserted_data(); |
|
110 |
void rowsInserted(); |
|
111 |
||
112 |
void rowsRemoved_data(); |
|
113 |
void rowsRemoved(); |
|
114 |
||
115 |
void dataChanged_data(); |
|
116 |
void dataChanged(); |
|
117 |
||
118 |
void filters_data(); |
|
119 |
void filters(); |
|
120 |
||
121 |
void nameFilters(); |
|
122 |
||
123 |
void setData_data(); |
|
124 |
void setData(); |
|
125 |
||
126 |
void sort_data(); |
|
127 |
void sort(); |
|
128 |
||
129 |
void mkdir(); |
|
130 |
||
131 |
void caseSensitivity(); |
|
132 |
||
133 |
#if defined(Q_OS_WIN) && !defined(Q_OS_WINCE) |
|
134 |
void Win32LongFileName(); |
|
135 |
#endif |
|
136 |
||
137 |
void drives_data(); |
|
138 |
void drives(); |
|
139 |
void dirsBeforeFiles(); |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
140 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
141 |
void roleNames_data(); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
142 |
void roleNames(); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
143 |
|
0 | 144 |
protected: |
145 |
bool createFiles(const QString &test_path, const QStringList &initial_files, int existingFileCount = 0, const QStringList &intial_dirs = QStringList(), const QString &baseDir = QDir::temp().absolutePath()); |
|
146 |
||
147 |
private: |
|
148 |
QFileSystemModel *model; |
|
149 |
QString flatDirTestPath; |
|
150 |
#if defined(Q_OS_SYMBIAN) |
|
151 |
RFs rfs; |
|
152 |
#endif |
|
153 |
}; |
|
154 |
||
155 |
tst_QFileSystemModel::tst_QFileSystemModel() : model(0) |
|
156 |
{ |
|
157 |
qRegisterMetaType<QModelIndex>("QModelIndex"); |
|
158 |
||
159 |
QTime midnight(0, 0, 0); |
|
160 |
qsrand(midnight.secsTo(QTime::currentTime())); |
|
161 |
// generating unique temporary directory name |
|
162 |
flatDirTestPath = QDir::temp().path() + '/' + QString("flatdirtest.") + QString::number(qrand()); |
|
163 |
#if defined(Q_OS_SYMBIAN) |
|
164 |
rfs.Connect(); |
|
165 |
#endif |
|
166 |
} |
|
167 |
||
168 |
tst_QFileSystemModel::~tst_QFileSystemModel() |
|
169 |
{ |
|
170 |
#if defined(Q_OS_SYMBIAN) |
|
171 |
rfs.Close(); |
|
172 |
#endif |
|
173 |
QString tmp = flatDirTestPath; |
|
174 |
QDir dir(tmp); |
|
175 |
if (dir.exists() && !dir.rmdir(tmp)) |
|
176 |
qWarning("failed to remove tmp dir %s", dir.dirName().toAscii().data()); |
|
177 |
} |
|
178 |
||
179 |
void tst_QFileSystemModel::init() |
|
180 |
{ |
|
181 |
cleanup(); |
|
182 |
QCOMPARE(model, (QFileSystemModel*)0); |
|
183 |
model = new QFileSystemModel; |
|
184 |
} |
|
185 |
||
186 |
void tst_QFileSystemModel::cleanup() |
|
187 |
{ |
|
188 |
delete model; |
|
189 |
model = 0; |
|
190 |
QString tmp = flatDirTestPath; |
|
191 |
QDir dir(tmp); |
|
192 |
if (dir.exists(tmp)) { |
|
193 |
QStringList list = dir.entryList(QDir::AllEntries | QDir::System | QDir::Hidden | QDir::NoDotAndDotDot); |
|
194 |
for (int i = 0; i < list.count(); ++i) { |
|
195 |
QFileInfo fi(dir.path() + '/' + list.at(i)); |
|
196 |
if (fi.exists() && fi.isFile()) { |
|
197 |
QFile p(fi.absoluteFilePath()); |
|
198 |
p.setPermissions(QFile::ReadUser | QFile::ReadOwner | QFile::ExeOwner | QFile::ExeUser | QFile::WriteUser | QFile::WriteOwner | QFile::WriteOther); |
|
199 |
QFile dead(dir.path() + '/' + list.at(i)); |
|
200 |
dead.remove(); |
|
201 |
} |
|
202 |
if (fi.exists() && fi.isDir()) |
|
203 |
QVERIFY(dir.rmdir(list.at(i))); |
|
204 |
} |
|
205 |
list = dir.entryList(QDir::AllEntries | QDir::System | QDir::Hidden | QDir::NoDotAndDotDot); |
|
206 |
QVERIFY(list.count() == 0); |
|
207 |
} |
|
208 |
} |
|
209 |
||
210 |
void tst_QFileSystemModel::indexPath() |
|
211 |
{ |
|
212 |
#if !defined(Q_OS_WIN) && !defined(Q_OS_SYMBIAN) |
|
213 |
int depth = QDir::currentPath().count('/'); |
|
214 |
model->setRootPath(QDir::currentPath()); |
|
215 |
QTest::qWait(WAITTIME); |
|
216 |
QString backPath; |
|
217 |
for (int i = 0; i <= depth * 2 + 1; ++i) { |
|
218 |
backPath += "../"; |
|
219 |
QModelIndex idx = model->index(backPath); |
|
220 |
QVERIFY(i != depth - 1 ? idx.isValid() : !idx.isValid()); |
|
221 |
} |
|
222 |
QTest::qWait(WAITTIME * 3); |
|
223 |
qApp->processEvents(); |
|
224 |
#endif |
|
225 |
} |
|
226 |
||
227 |
void tst_QFileSystemModel::rootPath() |
|
228 |
{ |
|
229 |
QCOMPARE(model->rootPath(), QString(QDir().path())); |
|
230 |
||
231 |
QSignalSpy rootChanged(model, SIGNAL(rootPathChanged(const QString &))); |
|
232 |
QModelIndex root = model->setRootPath(model->rootPath()); |
|
233 |
root = model->setRootPath("this directory shouldn't exist"); |
|
234 |
QCOMPARE(rootChanged.count(), 0); |
|
235 |
||
236 |
QString oldRootPath = model->rootPath(); |
|
237 |
root = model->setRootPath(QDir::homePath()); |
|
238 |
||
239 |
QTRY_VERIFY(model->rowCount(root) >= 0); |
|
240 |
QCOMPARE(model->rootPath(), QString(QDir::homePath())); |
|
241 |
QCOMPARE(rootChanged.count(), oldRootPath == model->rootPath() ? 0 : 1); |
|
242 |
QCOMPARE(model->rootDirectory().absolutePath(), QDir::homePath()); |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
243 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
244 |
model->setRootPath(QDir::rootPath()); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
245 |
int oldCount = rootChanged.count(); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
246 |
oldRootPath = model->rootPath(); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
247 |
root = model->setRootPath(QDir::homePath() + QLatin1String("/.")); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
248 |
QTRY_VERIFY(model->rowCount(root) >= 0); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
249 |
QCOMPARE(model->rootPath(), QDir::homePath()); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
250 |
QCOMPARE(rootChanged.count(), oldRootPath == model->rootPath() ? oldCount : oldCount + 1); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
251 |
QCOMPARE(model->rootDirectory().absolutePath(), QDir::homePath()); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
252 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
253 |
QDir newdir = QDir::home(); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
254 |
if (newdir.cdUp()) { |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
255 |
oldCount = rootChanged.count(); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
256 |
oldRootPath = model->rootPath(); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
257 |
root = model->setRootPath(QDir::homePath() + QLatin1String("/..")); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
258 |
QTRY_VERIFY(model->rowCount(root) >= 0); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
259 |
QCOMPARE(model->rootPath(), newdir.path()); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
260 |
QCOMPARE(rootChanged.count(), oldCount + 1); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
261 |
QCOMPARE(model->rootDirectory().absolutePath(), newdir.path()); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
262 |
} |
0 | 263 |
} |
264 |
||
265 |
void tst_QFileSystemModel::naturalCompare_data() |
|
266 |
{ |
|
267 |
QTest::addColumn<QString>("s1"); |
|
268 |
QTest::addColumn<QString>("s2"); |
|
269 |
QTest::addColumn<int>("caseSensitive"); |
|
270 |
QTest::addColumn<int>("result"); |
|
271 |
QTest::addColumn<int>("swap"); |
|
272 |
for (int j = 0; j < 4; ++j) { // <- set a prefix and a postfix string (not numbers) |
|
273 |
QString prefix = (j == 0 || j == 1) ? "b" : ""; |
|
274 |
QString postfix = (j == 1 || j == 2) ? "y" : ""; |
|
275 |
||
276 |
for (int k = 0; k < 3; ++k) { // <- make 0 not a special case |
|
277 |
QString num = QString("%1").arg(k); |
|
278 |
QString nump = QString("%1").arg(k + 1); |
|
279 |
for (int i = 10; i < 12; ++i) { // <- swap s1 and s2 and reverse the result |
|
280 |
QTest::newRow("basic") << prefix + "0" + postfix << prefix + "0" + postfix << int(Qt::CaseInsensitive) << 0; |
|
281 |
||
282 |
// s1 should always be less then s2 |
|
283 |
QTest::newRow("just text") << prefix + "fred" + postfix << prefix + "jane" + postfix << int(Qt::CaseInsensitive) << i; |
|
284 |
QTest::newRow("just numbers") << prefix + num + postfix << prefix + "9" + postfix << int(Qt::CaseInsensitive) << i; |
|
285 |
QTest::newRow("zero") << prefix + num + postfix << prefix + "0" + nump + postfix << int(Qt::CaseInsensitive) << i; |
|
286 |
QTest::newRow("space b") << prefix + num + postfix << prefix + " " + nump + postfix << int(Qt::CaseInsensitive) << i; |
|
287 |
QTest::newRow("space a") << prefix + num + postfix << prefix + nump + " " + postfix << int(Qt::CaseInsensitive) << i; |
|
288 |
QTest::newRow("tab b") << prefix + num + postfix << prefix + " " + nump + postfix << int(Qt::CaseInsensitive) << i; |
|
289 |
QTest::newRow("tab a") << prefix + num + postfix << prefix + nump + " " + postfix << int(Qt::CaseInsensitive) << i; |
|
290 |
QTest::newRow("10 vs 2") << prefix + num + postfix << prefix + "10" + postfix << int(Qt::CaseInsensitive) << i; |
|
291 |
QTest::newRow("diff len") << prefix + num + postfix << prefix + nump + postfix + "x" << int(Qt::CaseInsensitive) << i; |
|
292 |
QTest::newRow("01 before 1") << prefix + "0" + num + postfix << prefix + nump + postfix << int(Qt::CaseInsensitive) << i; |
|
293 |
QTest::newRow("mul nums 2nd") << prefix + "1-" + num + postfix << prefix + "1-" + nump + postfix << int(Qt::CaseInsensitive) << i; |
|
294 |
QTest::newRow("mul nums 2nd") << prefix + "10-" + num + postfix<< prefix + "10-10" + postfix << int(Qt::CaseInsensitive) << i; |
|
295 |
QTest::newRow("mul nums 2nd") << prefix + "10-0"+ num + postfix<< prefix + "10-10" + postfix << int(Qt::CaseInsensitive) << i; |
|
296 |
QTest::newRow("mul nums 2nd") << prefix + "10-" + num + postfix<< prefix + "10-010" + postfix << int(Qt::CaseInsensitive) << i; |
|
297 |
QTest::newRow("mul nums big") << prefix + "10-" + num + postfix<< prefix + "20-0" + postfix << int(Qt::CaseInsensitive) << i; |
|
298 |
QTest::newRow("mul nums big") << prefix + "2-" + num + postfix << prefix + "10-0" + postfix << int(Qt::CaseInsensitive) << i; |
|
299 |
QTest::newRow("mul alphabet") << prefix + num + "-a" + postfix << prefix + num + "-c" + postfix << int(Qt::CaseInsensitive) << i; |
|
300 |
QTest::newRow("mul alphabet2")<< prefix + num + "-a9" + postfix<< prefix + num + "-c0" + postfix << int(Qt::CaseInsensitive) << i; |
|
301 |
QTest::newRow("mul nums w\\0")<< prefix + num + "-"+ num + postfix<< prefix + num+"-0"+nump + postfix << int(Qt::CaseInsensitive) << i; |
|
302 |
QTest::newRow("num first") << prefix + num + postfix << prefix + "a" + postfix << int(Qt::CaseInsensitive) << i; |
|
303 |
} |
|
304 |
} |
|
305 |
} |
|
306 |
} |
|
307 |
||
308 |
void tst_QFileSystemModel::naturalCompare() |
|
309 |
{ |
|
310 |
#ifdef QT_BUILD_INTERNAL |
|
311 |
QFETCH(QString, s1); |
|
312 |
QFETCH(QString, s2); |
|
313 |
QFETCH(int, caseSensitive); |
|
314 |
QFETCH(int, result); |
|
315 |
||
316 |
if (result == 10) |
|
317 |
QCOMPARE(QFileSystemModelPrivate::naturalCompare(s1, s2, Qt::CaseSensitivity(caseSensitive)), -1); |
|
318 |
else |
|
319 |
if (result == 11) |
|
320 |
QCOMPARE(QFileSystemModelPrivate::naturalCompare(s2, s1, Qt::CaseSensitivity(caseSensitive)), 1); |
|
321 |
else |
|
322 |
QCOMPARE(QFileSystemModelPrivate::naturalCompare(s2, s1, Qt::CaseSensitivity(caseSensitive)), result); |
|
323 |
#if defined(Q_OS_WINCE) |
|
324 |
// On Windows CE we need to wait after each test, otherwise no new threads can be |
|
325 |
// created. The scheduler takes its time to recognize ended threads. |
|
326 |
QTest::qWait(300); |
|
327 |
#endif |
|
328 |
#endif |
|
329 |
} |
|
330 |
||
331 |
void tst_QFileSystemModel::readOnly() |
|
332 |
{ |
|
333 |
QCOMPARE(model->isReadOnly(), true); |
|
334 |
QTemporaryFile file; |
|
335 |
file.open(); |
|
336 |
QModelIndex root = model->setRootPath(QDir::tempPath()); |
|
337 |
||
338 |
QTRY_VERIFY(model->rowCount(root) > 0); |
|
339 |
QVERIFY(!(model->flags(model->index(file.fileName())) & Qt::ItemIsEditable)); |
|
340 |
model->setReadOnly(false); |
|
341 |
QCOMPARE(model->isReadOnly(), false); |
|
342 |
QVERIFY(model->flags(model->index(file.fileName())) & Qt::ItemIsEditable); |
|
343 |
} |
|
344 |
||
345 |
class CustomFileIconProvider : public QFileIconProvider |
|
346 |
{ |
|
347 |
public: |
|
348 |
CustomFileIconProvider() : QFileIconProvider() { |
|
349 |
mb = qApp->style()->standardIcon(QStyle::SP_MessageBoxCritical); |
|
350 |
dvd = qApp->style()->standardIcon(QStyle::SP_DriveDVDIcon); |
|
351 |
} |
|
352 |
||
353 |
virtual QIcon icon(const QFileInfo &info) const |
|
354 |
{ |
|
355 |
if (info.isDir()) |
|
356 |
return mb; |
|
357 |
||
358 |
return QFileIconProvider::icon(info); |
|
359 |
} |
|
360 |
virtual QIcon icon(IconType type) const |
|
361 |
{ |
|
362 |
if (type == QFileIconProvider::Folder) |
|
363 |
return dvd; |
|
364 |
||
365 |
return QFileIconProvider::icon(type); |
|
366 |
} |
|
367 |
private: |
|
368 |
QIcon mb; |
|
369 |
QIcon dvd; |
|
370 |
}; |
|
371 |
||
372 |
void tst_QFileSystemModel::iconProvider() |
|
373 |
{ |
|
374 |
QVERIFY(model->iconProvider()); |
|
375 |
QFileIconProvider *p = new QFileIconProvider(); |
|
376 |
model->setIconProvider(p); |
|
377 |
QCOMPARE(model->iconProvider(), p); |
|
378 |
model->setIconProvider(0); |
|
379 |
delete p; |
|
380 |
||
381 |
QFileSystemModel *myModel = new QFileSystemModel(); |
|
382 |
myModel->setRootPath(QDir::homePath()); |
|
383 |
//Let's wait to populate the model |
|
384 |
QTest::qWait(250); |
|
385 |
//We change the provider, icons must me updated |
|
386 |
CustomFileIconProvider *custom = new CustomFileIconProvider(); |
|
387 |
myModel->setIconProvider(custom); |
|
388 |
||
389 |
QPixmap mb = qApp->style()->standardIcon(QStyle::SP_MessageBoxCritical).pixmap(50, 50); |
|
390 |
QCOMPARE(myModel->fileIcon(myModel->index(QDir::homePath())).pixmap(50, 50), mb); |
|
391 |
delete myModel; |
|
392 |
delete custom; |
|
393 |
} |
|
394 |
||
395 |
bool tst_QFileSystemModel::createFiles(const QString &test_path, const QStringList &initial_files, int existingFileCount, const QStringList &initial_dirs, const QString &dir) |
|
396 |
{ |
|
397 |
QDir baseDir(dir); |
|
398 |
if (!baseDir.exists(test_path)) { |
|
399 |
if (!baseDir.mkdir(test_path) && false) { |
|
400 |
qDebug() << "failed to create dir" << test_path; |
|
401 |
return false; |
|
402 |
} |
|
403 |
} |
|
404 |
//qDebug() << (model->rowCount(model->index(test_path))) << existingFileCount << initial_files; |
|
405 |
TRY_WAIT((model->rowCount(model->index(test_path)) == existingFileCount)); |
|
406 |
for (int i = 0; i < initial_dirs.count(); ++i) { |
|
407 |
QDir dir(test_path); |
|
408 |
if (!dir.exists()) { |
|
409 |
qWarning() << "error" << test_path << "doesn't exists"; |
|
410 |
return false; |
|
411 |
} |
|
412 |
if(!dir.mkdir(initial_dirs.at(i))) { |
|
413 |
qWarning() << "error" << "failed to make" << initial_dirs.at(i); |
|
414 |
return false; |
|
415 |
} |
|
416 |
//qDebug() << test_path + '/' + initial_dirs.at(i) << (QFile::exists(test_path + '/' + initial_dirs.at(i))); |
|
417 |
} |
|
418 |
for (int i = 0; i < initial_files.count(); ++i) { |
|
419 |
QFile file(test_path + '/' + initial_files.at(i)); |
|
420 |
if (!file.open(QIODevice::WriteOnly | QIODevice::Append)) { |
|
421 |
qDebug() << "failed to open file" << initial_files.at(i); |
|
422 |
return false; |
|
423 |
} |
|
424 |
if (!file.resize(1024 + file.size())) { |
|
425 |
qDebug() << "failed to resize file" << initial_files.at(i); |
|
426 |
return false; |
|
427 |
} |
|
428 |
if (!file.flush()) { |
|
429 |
qDebug() << "failed to flush file" << initial_files.at(i); |
|
430 |
return false; |
|
431 |
} |
|
432 |
file.close(); |
|
433 |
#if defined(Q_OS_WIN) && !defined(Q_OS_WINCE) |
|
434 |
if (initial_files.at(i)[0] == '.') |
|
435 |
QProcess::execute(QString("attrib +h %1").arg(file.fileName())); |
|
436 |
#elif defined(Q_OS_SYMBIAN) |
|
437 |
if (initial_files.at(i)[0] == '.') { |
|
438 |
HBufC* buffer = qt_QString2HBufCNewL(QDir::toNativeSeparators(file.fileName())); |
|
439 |
rfs.SetAtt(*buffer, KEntryAttHidden, 0); |
|
440 |
delete buffer; |
|
441 |
} |
|
442 |
#endif |
|
443 |
//qDebug() << test_path + '/' + initial_files.at(i) << (QFile::exists(test_path + '/' + initial_files.at(i))); |
|
444 |
} |
|
445 |
return true; |
|
446 |
} |
|
447 |
||
448 |
void tst_QFileSystemModel::rowCount() |
|
449 |
{ |
|
450 |
QString tmp = flatDirTestPath; |
|
451 |
QVERIFY(createFiles(tmp, QStringList())); |
|
452 |
||
453 |
QSignalSpy spy2(model, SIGNAL(rowsInserted(const QModelIndex &, int, int))); |
|
454 |
QSignalSpy spy3(model, SIGNAL(rowsAboutToBeInserted(const QModelIndex &, int, int))); |
|
455 |
||
456 |
#if !defined(Q_OS_WINCE) |
|
457 |
QStringList files = QStringList() << "b" << "d" << "f" << "h" << "j" << ".a" << ".c" << ".e" << ".g"; |
|
458 |
QString l = "b,d,f,h,j,.a,.c,.e,.g"; |
|
459 |
#else |
|
460 |
// Cannot hide them on CE |
|
461 |
QStringList files = QStringList() << "b" << "d" << "f" << "h" << "j"; |
|
462 |
QString l = "b,d,f,h,j"; |
|
463 |
#endif |
|
464 |
QVERIFY(createFiles(tmp, files)); |
|
465 |
||
466 |
QModelIndex root = model->setRootPath(tmp); |
|
467 |
QTRY_COMPARE(model->rowCount(root), 5); |
|
468 |
QVERIFY(spy2.count() > 0); |
|
469 |
QVERIFY(spy3.count() > 0); |
|
470 |
} |
|
471 |
||
472 |
void tst_QFileSystemModel::rowsInserted_data() |
|
473 |
{ |
|
474 |
QTest::addColumn<int>("count"); |
|
475 |
QTest::addColumn<int>("assending"); |
|
476 |
for (int i = 0; i < 4; ++i) { |
|
477 |
QTest::newRow(QString("Qt::AscendingOrder %1").arg(i).toLocal8Bit().constData()) << i << (int)Qt::AscendingOrder; |
|
478 |
QTest::newRow(QString("Qt::DescendingOrder %1").arg(i).toLocal8Bit().constData()) << i << (int)Qt::DescendingOrder; |
|
479 |
} |
|
480 |
} |
|
481 |
||
482 |
void tst_QFileSystemModel::rowsInserted() |
|
483 |
{ |
|
484 |
#if defined(Q_OS_WINCE) |
|
485 |
QSKIP("Watching directories does not work on CE(see #137910)", SkipAll); |
|
486 |
#endif |
|
487 |
QString tmp = flatDirTestPath; |
|
488 |
rowCount(); |
|
489 |
QModelIndex root = model->index(model->rootPath()); |
|
490 |
||
491 |
QFETCH(int, assending); |
|
492 |
QFETCH(int, count); |
|
493 |
model->sort(0, (Qt::SortOrder)assending); |
|
494 |
||
495 |
QSignalSpy spy0(model, SIGNAL(rowsInserted(const QModelIndex &, int, int))); |
|
496 |
QSignalSpy spy1(model, SIGNAL(rowsAboutToBeInserted(const QModelIndex &, int, int))); |
|
497 |
int oldCount = model->rowCount(root); |
|
498 |
QStringList files; |
|
499 |
for (int i = 0; i < count; ++i) |
|
500 |
files.append(QString("c%1").arg(i)); |
|
501 |
QVERIFY(createFiles(tmp, files, 5)); |
|
502 |
TRY_WAIT(model->rowCount(root) == oldCount + count); |
|
503 |
QTRY_COMPARE(model->rowCount(root), oldCount + count); |
|
504 |
QTest::qWait(100); // Let the sort settle. |
|
505 |
int totalRowsInserted = 0; |
|
506 |
for (int i = 0; i < spy0.count(); ++i) { |
|
507 |
int start = spy0[i].value(1).toInt(); |
|
508 |
int end = spy0[i].value(2).toInt(); |
|
509 |
totalRowsInserted += end - start + 1; |
|
510 |
} |
|
511 |
QCOMPARE(totalRowsInserted, count); |
|
512 |
if (assending == (Qt::SortOrder)Qt::AscendingOrder) { |
|
513 |
QString letter = model->index(model->rowCount(root) - 1, 0, root).data().toString(); |
|
514 |
QCOMPARE(letter, QString("j")); |
|
515 |
} else { |
|
516 |
QCOMPARE(model->index(model->rowCount(root) - 1, 0, root).data().toString(), QString("b")); |
|
517 |
} |
|
518 |
if (spy0.count() > 0) { |
|
519 |
if (count == 0) |
|
520 |
QCOMPARE(spy0.count(), 0); |
|
521 |
else |
|
522 |
QVERIFY(spy0.count() >= 1); |
|
523 |
} |
|
524 |
if (count == 0) QCOMPARE(spy1.count(), 0); else QVERIFY(spy1.count() >= 1); |
|
525 |
||
526 |
QVERIFY(createFiles(tmp, QStringList(".hidden_file"), 5 + count)); |
|
527 |
||
528 |
if (count != 0) QTRY_VERIFY(spy0.count() >= 1); else QTRY_VERIFY(spy0.count() == 0); |
|
529 |
if (count != 0) QTRY_VERIFY(spy1.count() >= 1); else QTRY_VERIFY(spy1.count() == 0); |
|
530 |
} |
|
531 |
||
532 |
void tst_QFileSystemModel::rowsRemoved_data() |
|
533 |
{ |
|
534 |
rowsInserted_data(); |
|
535 |
} |
|
536 |
||
537 |
void tst_QFileSystemModel::rowsRemoved() |
|
538 |
{ |
|
539 |
#if defined(Q_OS_WINCE) |
|
540 |
QSKIP("Watching directories does not work on CE(see #137910)", SkipAll); |
|
541 |
#endif |
|
542 |
QString tmp = flatDirTestPath; |
|
543 |
rowCount(); |
|
544 |
QModelIndex root = model->index(model->rootPath()); |
|
545 |
||
546 |
QFETCH(int, count); |
|
547 |
QFETCH(int, assending); |
|
548 |
model->sort(0, (Qt::SortOrder)assending); |
|
549 |
QTest::qWait(WAITTIME); |
|
550 |
||
551 |
QSignalSpy spy0(model, SIGNAL(rowsRemoved(const QModelIndex &, int, int))); |
|
552 |
QSignalSpy spy1(model, SIGNAL(rowsAboutToBeRemoved(const QModelIndex &, int, int))); |
|
553 |
int oldCount = model->rowCount(root); |
|
554 |
for (int i = count - 1; i >= 0; --i) { |
|
555 |
//qDebug() << "removing" << model->index(i, 0, root).data().toString(); |
|
556 |
QVERIFY(QFile::remove(tmp + '/' + model->index(i, 0, root).data().toString())); |
|
557 |
} |
|
558 |
for (int i = 0 ; i < 10; ++i) { |
|
559 |
QTest::qWait(WAITTIME); |
|
560 |
qApp->processEvents(); |
|
561 |
if (count != 0) { |
|
562 |
if (i == 10 || spy0.count() != 0) { |
|
563 |
QVERIFY(spy0.count() >= 1); |
|
564 |
QVERIFY(spy1.count() >= 1); |
|
565 |
} |
|
566 |
} else { |
|
567 |
if (i == 10 || spy0.count() == 0) { |
|
568 |
QVERIFY(spy0.count() == 0); |
|
569 |
QVERIFY(spy1.count() == 0); |
|
570 |
} |
|
571 |
} |
|
572 |
QStringList lst; |
|
573 |
for (int i = 0; i < model->rowCount(root); ++i) |
|
574 |
lst.append(model->index(i, 0, root).data().toString()); |
|
575 |
if (model->rowCount(root) == oldCount - count) |
|
576 |
break; |
|
577 |
qDebug() << "still have:" << lst << QFile::exists(tmp + '/' + QString(".a")); |
|
578 |
QDir tmpLister(tmp); |
|
579 |
qDebug() << tmpLister.entryList(); |
|
580 |
} |
|
581 |
QTRY_COMPARE(model->rowCount(root), oldCount - count); |
|
582 |
||
583 |
QVERIFY(QFile::exists(tmp + '/' + QString(".a"))); |
|
584 |
QVERIFY(QFile::remove(tmp + '/' + QString(".a"))); |
|
585 |
QVERIFY(QFile::remove(tmp + '/' + QString(".c"))); |
|
586 |
QTest::qWait(WAITTIME); |
|
587 |
||
588 |
if (count != 0) QVERIFY(spy0.count() >= 1); else QVERIFY(spy0.count() == 0); |
|
589 |
if (count != 0) QVERIFY(spy1.count() >= 1); else QVERIFY(spy1.count() == 0); |
|
590 |
} |
|
591 |
||
592 |
void tst_QFileSystemModel::dataChanged_data() |
|
593 |
{ |
|
594 |
rowsInserted_data(); |
|
595 |
} |
|
596 |
||
597 |
void tst_QFileSystemModel::dataChanged() |
|
598 |
{ |
|
599 |
// This can't be tested right now sense we don't watch files, only directories |
|
600 |
return; |
|
601 |
||
602 |
/* |
|
603 |
QString tmp = flatDirTestPath; |
|
604 |
rowCount(); |
|
605 |
QModelIndex root = model->index(model->rootPath()); |
|
606 |
||
607 |
QFETCH(int, count); |
|
608 |
QFETCH(int, assending); |
|
609 |
model->sort(0, (Qt::SortOrder)assending); |
|
610 |
||
611 |
QSignalSpy spy(model, SIGNAL(dataChanged (const QModelIndex &, const QModelIndex &))); |
|
612 |
QStringList files; |
|
613 |
for (int i = 0; i < count; ++i) |
|
614 |
files.append(model->index(i, 0, root).data().toString()); |
|
615 |
createFiles(tmp, files); |
|
616 |
||
617 |
QTest::qWait(WAITTIME); |
|
618 |
||
619 |
if (count != 0) QVERIFY(spy.count() >= 1); else QVERIFY(spy.count() == 0); |
|
620 |
*/ |
|
621 |
} |
|
622 |
||
623 |
void tst_QFileSystemModel::filters_data() |
|
624 |
{ |
|
625 |
QTest::addColumn<QStringList>("files"); |
|
626 |
QTest::addColumn<QStringList>("dirs"); |
|
627 |
QTest::addColumn<int>("dirFilters"); |
|
628 |
QTest::addColumn<QStringList>("nameFilters"); |
|
629 |
QTest::addColumn<int>("rowCount"); |
|
630 |
#if !defined(Q_OS_WINCE) && !defined(Q_OS_SYMBIAN) |
|
631 |
QTest::newRow("no dirs") << (QStringList() << "a" << "b" << "c") << QStringList() << (int)(QDir::Dirs) << QStringList() << 2; |
|
632 |
QTest::newRow("one dir - dotdot") << (QStringList() << "a" << "b" << "c") << (QStringList() << "Z") << (int)(QDir::Dirs | QDir::NoDotAndDotDot) << QStringList() << 1; |
|
633 |
QTest::newRow("one dir") << (QStringList() << "a" << "b" << "c") << (QStringList() << "Z") << (int)(QDir::Dirs) << QStringList() << 3; |
|
634 |
QTest::newRow("no dir + hidden") << (QStringList() << "a" << "b" << "c") << QStringList() << (int)(QDir::Dirs | QDir::Hidden) << QStringList() << 2; |
|
635 |
QTest::newRow("dir+hid+files") << (QStringList() << "a" << "b" << "c") << QStringList() << |
|
636 |
(int)(QDir::Dirs | QDir::Files | QDir::Hidden) << QStringList() << 5; |
|
637 |
QTest::newRow("dir+file+hid-dot .A") << (QStringList() << "a" << "b" << "c") << (QStringList() << ".A") << |
|
638 |
(int)(QDir::Dirs | QDir::Files | QDir::Hidden | QDir::NoDotAndDotDot) << QStringList() << 4; |
|
639 |
QTest::newRow("dir+files+hid+dot A") << (QStringList() << "a" << "b" << "c") << (QStringList() << "AFolder") << |
|
640 |
(int)(QDir::Dirs | QDir::Files | QDir::Hidden | QDir::NoDotAndDotDot) << (QStringList() << "A*") << 2; |
|
641 |
QTest::newRow("dir+files+hid+dot+cas1") << (QStringList() << "a" << "b" << "c") << (QStringList() << "Z") << |
|
642 |
(int)(QDir::Dirs | QDir::Files | QDir::Hidden | QDir::NoDotAndDotDot | QDir::CaseSensitive) << (QStringList() << "Z") << 1; |
|
643 |
QTest::newRow("dir+files+hid+dot+cas2") << (QStringList() << "a" << "b" << "c") << (QStringList() << "Z") << |
|
644 |
(int)(QDir::Dirs | QDir::Files | QDir::Hidden | QDir::NoDotAndDotDot | QDir::CaseSensitive) << (QStringList() << "a") << 1; |
|
645 |
QTest::newRow("dir+files+hid+dot+cas+alldir") << (QStringList() << "a" << "b" << "c") << (QStringList() << "Z") << |
|
646 |
(int)(QDir::Dirs | QDir::Files | QDir::Hidden | QDir::NoDotAndDotDot | QDir::CaseSensitive | QDir::AllDirs) << (QStringList() << "Z") << 1; |
|
647 |
#else |
|
648 |
QTest::qWait(3000); // We need to calm down a bit... |
|
649 |
QTest::newRow("no dirs") << (QStringList() << "a" << "b" << "c") << QStringList() << (int)(QDir::Dirs) << QStringList() << 0; |
|
650 |
QTest::newRow("one dir - dotdot") << (QStringList() << "a" << "b" << "c") << (QStringList() << "Z") << (int)(QDir::Dirs | QDir::NoDotAndDotDot) << QStringList() << 1; |
|
651 |
QTest::newRow("one dir") << (QStringList() << "a" << "b" << "c") << (QStringList() << "Z") << (int)(QDir::Dirs) << QStringList() << 1; |
|
652 |
QTest::newRow("no dir + hidden") << (QStringList() << "a" << "b" << "c") << QStringList() << (int)(QDir::Dirs | QDir::Hidden) << QStringList() << 0; |
|
653 |
QTest::newRow("dir+hid+files") << (QStringList() << "a" << "b" << "c") << QStringList() << |
|
654 |
(int)(QDir::Dirs | QDir::Files | QDir::Hidden) << QStringList() << 3; |
|
655 |
#if defined(Q_OS_SYMBIAN) |
|
656 |
// Some symbian envs have a bug that causes "A" and ".A" to be considered same name in file system. |
|
657 |
QTest::newRow("dir+file+hid-dot .D") << (QStringList() << "a" << "b" << "c") << (QStringList() << ".D") << |
|
658 |
#else |
|
659 |
QTest::newRow("dir+file+hid-dot .A") << (QStringList() << "a" << "b" << "c") << (QStringList() << ".A") << |
|
660 |
#endif |
|
661 |
(int)(QDir::Dirs | QDir::Files | QDir::Hidden | QDir::NoDotAndDotDot) << QStringList() << 4; |
|
662 |
QTest::newRow("dir+files+hid+dot A") << (QStringList() << "a" << "b" << "c") << (QStringList() << "AFolder") << |
|
663 |
(int)(QDir::Dirs | QDir::Files | QDir::Hidden | QDir::NoDotAndDotDot) << (QStringList() << "A*") << 2; |
|
664 |
QTest::newRow("dir+files+hid+dot+cas1") << (QStringList() << "a" << "b" << "c") << (QStringList() << "Z") << |
|
665 |
(int)(QDir::Dirs | QDir::Files | QDir::Hidden | QDir::NoDotAndDotDot | QDir::CaseSensitive) << (QStringList() << "Z") << 1; |
|
666 |
QTest::newRow("dir+files+hid+dot+cas2") << (QStringList() << "a" << "b" << "c") << (QStringList() << "Z") << |
|
667 |
(int)(QDir::Dirs | QDir::Files | QDir::Hidden | QDir::NoDotAndDotDot | QDir::CaseSensitive) << (QStringList() << "a") << 1; |
|
668 |
QTest::newRow("dir+files+hid+dot+cas+alldir") << (QStringList() << "a" << "b" << "c") << (QStringList() << "Z") << |
|
669 |
(int)(QDir::Dirs | QDir::Files | QDir::Hidden | QDir::NoDotAndDotDot | QDir::CaseSensitive | QDir::AllDirs) << (QStringList() << "Z") << 1; |
|
670 |
#endif |
|
671 |
||
672 |
QTest::newRow("case sensitive") << (QStringList() << "Antiguagdb" << "Antiguamtd" |
|
673 |
<< "Antiguamtp" << "afghanistangdb" << "afghanistanmtd") |
|
674 |
<< QStringList() << (int)(QDir::Files) << QStringList() << 5; |
|
675 |
} |
|
676 |
||
677 |
void tst_QFileSystemModel::filters() |
|
678 |
{ |
|
679 |
QString tmp = flatDirTestPath; |
|
680 |
QVERIFY(createFiles(tmp, QStringList())); |
|
681 |
QModelIndex root = model->setRootPath(tmp); |
|
682 |
QFETCH(QStringList, files); |
|
683 |
QFETCH(QStringList, dirs); |
|
684 |
QFETCH(int, dirFilters); |
|
685 |
QFETCH(QStringList, nameFilters); |
|
686 |
QFETCH(int, rowCount); |
|
687 |
||
688 |
if (nameFilters.count() > 0) |
|
689 |
model->setNameFilters(nameFilters); |
|
690 |
model->setNameFilterDisables(false); |
|
691 |
model->setFilter((QDir::Filters)dirFilters); |
|
692 |
||
693 |
QVERIFY(createFiles(tmp, files, 0, dirs)); |
|
694 |
QTRY_COMPARE(model->rowCount(root), rowCount); |
|
695 |
||
696 |
// Make sure that we do what QDir does |
|
697 |
QDir xFactor(tmp); |
|
698 |
QDir::Filters filters = (QDir::Filters)dirFilters; |
|
699 |
if (nameFilters.count() > 0) |
|
700 |
QCOMPARE(xFactor.entryList(nameFilters, filters).count(), rowCount); |
|
701 |
else |
|
702 |
QVERIFY(xFactor.entryList(filters).count() == rowCount); |
|
703 |
||
704 |
#ifdef Q_OS_LINUX |
|
705 |
if (files.count() >= 3 && rowCount >= 3 && rowCount != 5) { |
|
706 |
QString fileName1 = (tmp + '/' + files.at(0)); |
|
707 |
QString fileName2 = (tmp + '/' + files.at(1)); |
|
708 |
QString fileName3 = (tmp + '/' + files.at(2)); |
|
709 |
QFile::Permissions originalPermissions = QFile::permissions(fileName1); |
|
710 |
QVERIFY(QFile::setPermissions(fileName1, QFile::WriteOwner)); |
|
711 |
QVERIFY(QFile::setPermissions(fileName2, QFile::ReadOwner)); |
|
712 |
QVERIFY(QFile::setPermissions(fileName3, QFile::ExeOwner)); |
|
713 |
||
714 |
model->setFilter((QDir::Files | QDir::Readable)); |
|
715 |
QTRY_COMPARE(model->rowCount(root), 1); |
|
716 |
||
717 |
model->setFilter((QDir::Files | QDir::Writable)); |
|
718 |
QTRY_COMPARE(model->rowCount(root), 1); |
|
719 |
||
720 |
model->setFilter((QDir::Files | QDir::Executable)); |
|
721 |
QTRY_COMPARE(model->rowCount(root), 1); |
|
722 |
||
723 |
// reset permissions |
|
724 |
QVERIFY(QFile::setPermissions(fileName1, originalPermissions)); |
|
725 |
QVERIFY(QFile::setPermissions(fileName2, originalPermissions)); |
|
726 |
QVERIFY(QFile::setPermissions(fileName3, originalPermissions)); |
|
727 |
} |
|
728 |
#endif |
|
729 |
} |
|
730 |
||
731 |
void tst_QFileSystemModel::nameFilters() |
|
732 |
{ |
|
733 |
QStringList list; |
|
734 |
list << "a" << "b" << "c"; |
|
735 |
model->setNameFilters(list); |
|
736 |
model->setNameFilterDisables(false); |
|
737 |
QCOMPARE(model->nameFilters(), list); |
|
738 |
||
739 |
QString tmp = flatDirTestPath; |
|
740 |
QVERIFY(createFiles(tmp, list)); |
|
741 |
QModelIndex root = model->setRootPath(tmp); |
|
742 |
QTRY_COMPARE(model->rowCount(root), 3); |
|
743 |
||
744 |
QStringList filters; |
|
745 |
filters << "a" << "b"; |
|
746 |
model->setNameFilters(filters); |
|
747 |
QTRY_COMPARE(model->rowCount(root), 2); |
|
748 |
} |
|
749 |
void tst_QFileSystemModel::setData_data() |
|
750 |
{ |
|
751 |
QTest::addColumn<QStringList>("files"); |
|
752 |
QTest::addColumn<QString>("oldFileName"); |
|
753 |
QTest::addColumn<QString>("newFileName"); |
|
754 |
QTest::addColumn<bool>("success"); |
|
755 |
/*QTest::newRow("outside current dir") << (QStringList() << "a" << "b" << "c") |
|
756 |
<< flatDirTestPath + '/' + "a" |
|
757 |
<< QDir::temp().absolutePath() + '/' + "a" |
|
758 |
<< false; |
|
759 |
*/ |
|
760 |
QTest::newRow("in current dir") << (QStringList() << "a" << "b" << "c") |
|
761 |
<< "a" |
|
762 |
<< "d" |
|
763 |
<< true; |
|
764 |
} |
|
765 |
||
766 |
void tst_QFileSystemModel::setData() |
|
767 |
{ |
|
768 |
QSignalSpy spy(model, SIGNAL(fileRenamed(const QString&, const QString&, const QString&))); |
|
769 |
QString tmp = flatDirTestPath; |
|
770 |
QFETCH(QStringList, files); |
|
771 |
QFETCH(QString, oldFileName); |
|
772 |
QFETCH(QString, newFileName); |
|
773 |
QFETCH(bool, success); |
|
774 |
||
775 |
QVERIFY(createFiles(tmp, files)); |
|
776 |
QModelIndex root = model->setRootPath(tmp); |
|
777 |
QTRY_COMPARE(model->rowCount(root), files.count()); |
|
778 |
||
779 |
QModelIndex idx = model->index(tmp + '/' + oldFileName); |
|
780 |
QCOMPARE(idx.isValid(), true); |
|
781 |
QCOMPARE(model->setData(idx, newFileName), false); |
|
782 |
||
783 |
model->setReadOnly(false); |
|
784 |
QCOMPARE(model->setData(idx, newFileName), success); |
|
785 |
if (success) { |
|
786 |
QCOMPARE(spy.count(), 1); |
|
787 |
QList<QVariant> arguments = spy.takeFirst(); |
|
788 |
QCOMPARE(model->data(idx, QFileSystemModel::FileNameRole).toString(), newFileName); |
|
789 |
QCOMPARE(model->index(arguments.at(0).toString()), model->index(tmp)); |
|
790 |
QCOMPARE(arguments.at(1).toString(), oldFileName); |
|
791 |
QCOMPARE(arguments.at(2).toString(), newFileName); |
|
792 |
QCOMPARE(QFile::rename(tmp + '/' + newFileName, tmp + '/' + oldFileName), true); |
|
793 |
} |
|
794 |
QTRY_COMPARE(model->rowCount(root), files.count()); |
|
795 |
} |
|
796 |
||
797 |
class MyFriendFileSystemModel : public QFileSystemModel |
|
798 |
{ |
|
799 |
friend class tst_QFileSystemModel; |
|
800 |
Q_DECLARE_PRIVATE(QFileSystemModel) |
|
801 |
}; |
|
802 |
||
803 |
void tst_QFileSystemModel::sort_data() |
|
804 |
{ |
|
805 |
QTest::addColumn<bool>("fileDialogMode"); |
|
806 |
QTest::newRow("standard usage") << false; |
|
807 |
QTest::newRow("QFileDialog usage") << true; |
|
808 |
} |
|
809 |
||
810 |
void tst_QFileSystemModel::sort() |
|
811 |
{ |
|
812 |
QTemporaryFile file; |
|
813 |
file.open(); |
|
814 |
QModelIndex root = model->setRootPath(QDir::tempPath()); |
|
815 |
QTRY_VERIFY(model->rowCount(root) > 0); |
|
816 |
||
817 |
QPersistentModelIndex idx = model->index(0, 1, root); |
|
818 |
model->sort(0, Qt::AscendingOrder); |
|
819 |
model->sort(0, Qt::DescendingOrder); |
|
820 |
QVERIFY(idx.column() != 0); |
|
821 |
||
822 |
model->setRootPath(QDir::homePath()); |
|
823 |
||
824 |
QFETCH(bool, fileDialogMode); |
|
825 |
||
826 |
MyFriendFileSystemModel *myModel = new MyFriendFileSystemModel(); |
|
827 |
QTreeView *tree = new QTreeView(); |
|
828 |
||
829 |
if (fileDialogMode) |
|
830 |
myModel->d_func()->disableRecursiveSort = true; |
|
831 |
||
832 |
QDir dir(QDir::tempPath()); |
|
833 |
//initialize the randomness |
|
834 |
qsrand(QDateTime::currentDateTime().toTime_t()); |
|
835 |
QString tempName = QLatin1String("sortTemp.") + QString::number(qrand()); |
|
836 |
dir.mkdir(tempName); |
|
837 |
dir.cd(tempName); |
|
838 |
QTRY_VERIFY(dir.exists()); |
|
839 |
||
840 |
const QString dirPath = dir.absolutePath(); |
|
841 |
QVERIFY(dir.exists()); |
|
842 |
||
843 |
//Create a file that will be at the end when sorting by name (For Mac, the default) |
|
844 |
//but if we sort by size descending it will be the first |
|
845 |
QFile tempFile(dirPath + "/plop2.txt"); |
|
846 |
tempFile.open(QIODevice::WriteOnly | QIODevice::Text); |
|
847 |
QTextStream out(&tempFile); |
|
848 |
out << "The magic number is: " << 49 << "\n"; |
|
849 |
tempFile.close(); |
|
850 |
||
851 |
QFile tempFile2(dirPath + "/plop.txt"); |
|
852 |
tempFile2.open(QIODevice::WriteOnly | QIODevice::Text); |
|
853 |
QTextStream out2(&tempFile2); |
|
854 |
out2 << "The magic number is : " << 49 << " but i write some stuff in the file \n"; |
|
855 |
tempFile2.close(); |
|
856 |
||
857 |
myModel->setRootPath(""); |
|
858 |
myModel->setFilter(QDir::AllEntries | QDir::System | QDir::Hidden); |
|
859 |
tree->setSortingEnabled(true); |
|
860 |
tree->setModel(myModel); |
|
861 |
tree->show(); |
|
862 |
tree->resize(800, 800); |
|
863 |
QTest::qWait(500); |
|
864 |
tree->header()->setSortIndicator(1,Qt::DescendingOrder); |
|
865 |
tree->header()->setResizeMode(0, QHeaderView::ResizeToContents); |
|
866 |
QStringList dirsToOpen; |
|
867 |
do |
|
868 |
{ |
|
869 |
dirsToOpen<<dir.absolutePath(); |
|
870 |
} while (dir.cdUp()); |
|
871 |
||
872 |
for (int i = dirsToOpen.size() -1 ; i > 0 ; --i) { |
|
873 |
QString path = dirsToOpen[i]; |
|
874 |
QTest::qWait(500); |
|
875 |
tree->expand(myModel->index(path, 0)); |
|
876 |
} |
|
877 |
tree->expand(myModel->index(dirPath, 0)); |
|
878 |
QTest::qWait(500); |
|
879 |
QModelIndex parent = myModel->index(dirPath, 0); |
|
880 |
QList<QString> expectedOrder; |
|
881 |
expectedOrder << tempFile2.fileName() << tempFile.fileName() << dirPath + QChar('/') + "." << dirPath + QChar('/') + ".."; |
|
882 |
//File dialog Mode means sub trees are not sorted, only the current root |
|
883 |
if (fileDialogMode) { |
|
884 |
QList<QString> actualRows; |
|
885 |
for(int i = 0; i < myModel->rowCount(parent); ++i) |
|
886 |
{ |
|
887 |
actualRows << dirPath + QChar('/') + myModel->index(i, 1, parent).data(QFileSystemModel::FileNameRole).toString(); |
|
888 |
} |
|
889 |
QVERIFY(actualRows != expectedOrder); |
|
890 |
} else { |
|
891 |
for(int i = 0; i < myModel->rowCount(parent); ++i) |
|
892 |
{ |
|
893 |
QTRY_COMPARE(dirPath + QChar('/') + myModel->index(i, 1, parent).data(QFileSystemModel::FileNameRole).toString(), expectedOrder.at(i)); |
|
894 |
} |
|
895 |
} |
|
896 |
||
897 |
delete tree; |
|
898 |
delete myModel; |
|
899 |
||
900 |
dir.setPath(QDir::tempPath()); |
|
901 |
dir.cd(tempName); |
|
902 |
tempFile.remove(); |
|
903 |
tempFile2.remove(); |
|
904 |
dir.cdUp(); |
|
905 |
dir.rmdir(tempName); |
|
906 |
||
907 |
} |
|
908 |
||
909 |
void tst_QFileSystemModel::mkdir() |
|
910 |
{ |
|
911 |
QString tmp = QDir::tempPath(); |
|
912 |
QString newFolderPath = QDir::toNativeSeparators(tmp + '/' + "NewFoldermkdirtest4"); |
|
913 |
QModelIndex tmpDir = model->index(tmp); |
|
914 |
QVERIFY(tmpDir.isValid()); |
|
915 |
QDir bestatic(newFolderPath); |
|
916 |
if (bestatic.exists()) { |
|
917 |
if (!bestatic.rmdir(newFolderPath)) |
|
918 |
qWarning() << "unable to remove" << newFolderPath; |
|
919 |
QTest::qWait(WAITTIME); |
|
920 |
} |
|
921 |
model->mkdir(tmpDir, "NewFoldermkdirtest3"); |
|
922 |
model->mkdir(tmpDir, "NewFoldermkdirtest5"); |
|
923 |
QModelIndex idx = model->mkdir(tmpDir, "NewFoldermkdirtest4"); |
|
924 |
QVERIFY(idx.isValid()); |
|
925 |
int oldRow = idx.row(); |
|
926 |
QTest::qWait(WAITTIME); |
|
927 |
idx = model->index(newFolderPath); |
|
928 |
QDir cleanup(tmp); |
|
929 |
QVERIFY(cleanup.rmdir(QLatin1String("NewFoldermkdirtest3"))); |
|
930 |
QVERIFY(cleanup.rmdir(QLatin1String("NewFoldermkdirtest5"))); |
|
931 |
bestatic.rmdir(newFolderPath); |
|
932 |
QVERIFY(0 != idx.row()); |
|
933 |
QCOMPARE(oldRow, idx.row()); |
|
934 |
} |
|
935 |
||
936 |
void tst_QFileSystemModel::caseSensitivity() |
|
937 |
{ |
|
938 |
QString tmp = flatDirTestPath; |
|
939 |
QStringList files; |
|
940 |
files << "a" << "c" << "C"; |
|
941 |
QVERIFY(createFiles(tmp, files)); |
|
942 |
QModelIndex root = model->index(tmp); |
|
943 |
QCOMPARE(model->rowCount(root), 0); |
|
944 |
for (int i = 0; i < files.count(); ++i) { |
|
945 |
QVERIFY(model->index(tmp + '/' + files.at(i)).isValid()); |
|
946 |
} |
|
947 |
} |
|
948 |
||
949 |
#if defined(Q_OS_WIN) && !defined(Q_OS_WINCE) |
|
950 |
void tst_QFileSystemModel::Win32LongFileName() |
|
951 |
{ |
|
952 |
QString tmp = flatDirTestPath; |
|
953 |
QStringList files; |
|
954 |
files << "aaaaaaaaaa" << "bbbbbbbbbb" << "cccccccccc"; |
|
955 |
QVERIFY(createFiles(tmp, files)); |
|
956 |
QModelIndex root = model->setRootPath(tmp); |
|
957 |
QTRY_VERIFY(model->index(tmp + QLatin1String("/aaaaaa~1")).isValid()); |
|
958 |
QTRY_VERIFY(model->index(tmp + QLatin1String("/bbbbbb~1")).isValid()); |
|
959 |
QTRY_VERIFY(model->index(tmp + QLatin1String("/cccccc~1")).isValid()); |
|
960 |
} |
|
961 |
#endif |
|
962 |
||
963 |
void tst_QFileSystemModel::drives_data() |
|
964 |
{ |
|
965 |
QTest::addColumn<QString>("path"); |
|
966 |
QTest::newRow("current") << QDir::currentPath(); |
|
967 |
QTest::newRow("slash") << "/"; |
|
968 |
QTest::newRow("My Computer") << "My Computer"; |
|
969 |
} |
|
970 |
||
971 |
void tst_QFileSystemModel::drives() |
|
972 |
{ |
|
973 |
QFETCH(QString, path); |
|
974 |
QFileSystemModel model; |
|
975 |
model.setRootPath(path); |
|
976 |
model.fetchMore(QModelIndex()); |
|
977 |
QFileInfoList drives = QDir::drives(); |
|
978 |
QTest::qWait(5000); |
|
979 |
QTRY_COMPARE(model.rowCount(), drives.count()); |
|
980 |
} |
|
981 |
||
982 |
void tst_QFileSystemModel::dirsBeforeFiles() |
|
983 |
{ |
|
984 |
const QString dirPath = QString("%1/task221717_sortedOrder_test_dir").arg(QDir::tempPath()); |
|
985 |
QDir dir(dirPath); |
|
986 |
// clean up from last time |
|
987 |
if (dir.exists()) { |
|
988 |
for (int i = 0; i < 3; ++i) { |
|
989 |
QLatin1Char c('a' + i); |
|
990 |
dir.rmdir(QString("%1-dir").arg(c)); |
|
991 |
QFile::remove(dirPath + QString("/%1-file").arg(c)); |
|
992 |
} |
|
993 |
dir.rmdir(dirPath); |
|
994 |
} |
|
995 |
dir.mkpath(dirPath); |
|
996 |
QVERIFY(dir.exists()); |
|
997 |
||
998 |
for (int i = 0; i < 3; ++i) { |
|
999 |
QLatin1Char c('a' + i); |
|
1000 |
dir.mkdir(QString("%1-dir").arg(c)); |
|
1001 |
QFile file(dirPath + QString("/%1-file").arg(c)); |
|
1002 |
file.open(QIODevice::ReadWrite); |
|
1003 |
file.close(); |
|
1004 |
} |
|
1005 |
||
1006 |
QModelIndex root = model->setRootPath(dirPath); |
|
1007 |
QTest::qWait(1000); // allow model to be notified by the file system watcher |
|
1008 |
||
1009 |
// ensure that no file occurs before a directory |
|
1010 |
for (int i = 0; i < model->rowCount(root); ++i) { |
|
1011 |
#ifndef Q_OS_MAC |
|
1012 |
QVERIFY(i == 0 || |
|
1013 |
!(model->fileInfo(model->index(i - 1, 0, root)).isFile() |
|
1014 |
&& model->fileInfo(model->index(i, 0, root)).isDir())); |
|
1015 |
#else |
|
1016 |
QVERIFY(i == 0 || |
|
1017 |
model->fileInfo(model->index(i - 1, 0, root)).fileName() < |
|
1018 |
model->fileInfo(model->index(i, 0, root)).fileName()); |
|
1019 |
#endif |
|
1020 |
} |
|
1021 |
} |
|
1022 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
1023 |
void tst_QFileSystemModel::roleNames_data() |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
1024 |
{ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
1025 |
QTest::addColumn<int>("role"); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
1026 |
QTest::addColumn<QByteArray>("roleName"); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
1027 |
QTest::newRow("decoration") << int(Qt::DecorationRole) << QByteArray("decoration"); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
1028 |
QTest::newRow("display") << int(Qt::DisplayRole) << QByteArray("display"); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
1029 |
QTest::newRow("fileIcon") << int(QFileSystemModel::FileIconRole) << QByteArray("fileIcon"); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
1030 |
QTest::newRow("filePath") << int(QFileSystemModel::FilePathRole) << QByteArray("filePath"); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
1031 |
QTest::newRow("fileName") << int(QFileSystemModel::FileNameRole) << QByteArray("fileName"); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
1032 |
QTest::newRow("filePermissions") << int(QFileSystemModel::FilePermissions) << QByteArray("filePermissions"); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
1033 |
} |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
1034 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
1035 |
void tst_QFileSystemModel::roleNames() |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
1036 |
{ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
1037 |
QFileSystemModel model; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
1038 |
QHash<int, QByteArray> roles = model.roleNames(); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
1039 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
1040 |
QFETCH(int, role); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
1041 |
QVERIFY(roles.contains(role)); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
1042 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
1043 |
QFETCH(QByteArray, roleName); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
1044 |
QList<QByteArray> values = roles.values(role); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
1045 |
QVERIFY(values.contains(roleName)); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
1046 |
} |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
1047 |
|
0 | 1048 |
QTEST_MAIN(tst_QFileSystemModel) |
1049 |
#include "tst_qfilesystemmodel.moc" |
|
1050 |