author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Mon, 04 Oct 2010 01:19:32 +0300 | |
changeset 37 | 758a864f9613 |
parent 30 | 5dc02b23752f |
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 |
#include <QtTest/QtTest> |
|
42 |
||
43 |
#include <QCoreApplication> |
|
44 |
||
45 |
#include <QFileSystemWatcher> |
|
46 |
||
47 |
#ifdef Q_OS_LINUX |
|
48 |
# ifdef QT_NO_INOTIFY |
|
49 |
# include <linux/version.h> |
|
50 |
# else |
|
51 |
# include <sys/inotify.h> |
|
52 |
# endif |
|
53 |
#endif |
|
54 |
||
55 |
//TESTED_CLASS= |
|
56 |
//TESTED_FILES= |
|
57 |
||
58 |
class tst_QFileSystemWatcher : public QObject |
|
59 |
{ |
|
60 |
Q_OBJECT |
|
61 |
||
62 |
public: |
|
63 |
tst_QFileSystemWatcher(); |
|
64 |
||
65 |
private slots: |
|
66 |
void basicTest_data(); |
|
67 |
void basicTest(); |
|
68 |
||
69 |
void watchDirectory_data() { basicTest_data(); } |
|
70 |
void watchDirectory(); |
|
71 |
||
72 |
void addPath(); |
|
73 |
void removePath(); |
|
74 |
void addPaths(); |
|
75 |
void removePaths(); |
|
76 |
||
77 |
void watchFileAndItsDirectory_data() { basicTest_data(); } |
|
78 |
void watchFileAndItsDirectory(); |
|
79 |
||
80 |
void nonExistingFile(); |
|
81 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
82 |
void removeFileAndUnWatch(); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
83 |
|
0 | 84 |
void cleanup(); |
85 |
private: |
|
86 |
QStringList do_force_engines; |
|
87 |
bool do_force_native; |
|
88 |
}; |
|
89 |
||
90 |
tst_QFileSystemWatcher::tst_QFileSystemWatcher() |
|
91 |
: do_force_native(false) |
|
92 |
{ |
|
93 |
#ifdef Q_OS_LINUX |
|
94 |
// the inotify implementation in the kernel is known to be buggy in certain versions of the linux kernel |
|
95 |
do_force_engines << "native"; |
|
96 |
do_force_engines << "dnotify"; |
|
97 |
||
98 |
#ifdef QT_NO_INOTIFY |
|
99 |
if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,13)) |
|
100 |
do_force_engines << "inotify"; |
|
101 |
#else |
|
102 |
if (inotify_init() != -1) |
|
103 |
do_force_engines << "inotify"; |
|
104 |
#endif |
|
105 |
#elif defined(Q_OS_WIN) || defined(Q_OS_DARWIN) || defined(Q_OS_FREEBSD) || defined(Q_OS_SYMBIAN) |
|
106 |
// we have native engines for win32, macosx and freebsd |
|
107 |
do_force_engines << "native"; |
|
108 |
#endif |
|
109 |
} |
|
110 |
||
111 |
void tst_QFileSystemWatcher::basicTest_data() |
|
112 |
{ |
|
113 |
QTest::addColumn<QString>("backend"); |
|
114 |
foreach(QString engine, do_force_engines) |
|
115 |
QTest::newRow(engine.toLatin1().constData()) << engine; |
|
116 |
QTest::newRow("poller") << "poller"; |
|
117 |
} |
|
118 |
||
119 |
void tst_QFileSystemWatcher::basicTest() |
|
120 |
{ |
|
121 |
QFETCH(QString, backend); |
|
122 |
qDebug() << "Testing" << backend << "engine"; |
|
123 |
||
124 |
// create test file |
|
125 |
QFile testFile("testfile.txt"); |
|
126 |
testFile.setPermissions(QFile::ReadOwner | QFile::WriteOwner); |
|
127 |
testFile.remove(); |
|
128 |
QVERIFY(testFile.open(QIODevice::WriteOnly | QIODevice::Truncate)); |
|
129 |
testFile.write(QByteArray("hello")); |
|
130 |
testFile.close(); |
|
131 |
||
132 |
// set some file permissions |
|
133 |
testFile.setPermissions(QFile::ReadOwner | QFile::WriteOwner); |
|
134 |
||
135 |
// create watcher, forcing it to use a specific backend |
|
136 |
QFileSystemWatcher watcher; |
|
137 |
watcher.setObjectName(QLatin1String("_qt_autotest_force_engine_") + backend); |
|
138 |
watcher.addPath(testFile.fileName()); |
|
139 |
||
140 |
QSignalSpy changedSpy(&watcher, SIGNAL(fileChanged(const QString &))); |
|
141 |
QEventLoop eventLoop; |
|
142 |
connect(&watcher, |
|
143 |
SIGNAL(fileChanged(const QString &)), |
|
144 |
&eventLoop, |
|
145 |
SLOT(quit())); |
|
146 |
QTimer timer; |
|
147 |
connect(&timer, SIGNAL(timeout()), &eventLoop, SLOT(quit())); |
|
148 |
||
149 |
// modify the file, should get a signal from the watcher |
|
150 |
||
151 |
// resolution of the modification time is system dependent, but it's at most 1 second when using |
|
152 |
// the polling engine. I've heard rumors that FAT32 has a 2 second resolution. So, we have to |
|
153 |
// wait a bit before we can modify the file (hrmph)... |
|
154 |
#ifndef Q_OS_WINCE |
|
155 |
QTest::qWait(2000); |
|
156 |
#else |
|
157 |
// WinCE is always a little bit slower. Give it a little bit more time |
|
158 |
QTest::qWait(5000); |
|
159 |
#endif |
|
160 |
||
161 |
testFile.open(QIODevice::WriteOnly | QIODevice::Append); |
|
162 |
testFile.write(QByteArray("world")); |
|
163 |
testFile.close(); |
|
164 |
||
165 |
// qDebug() << "waiting max 5 seconds for notification for file modification to trigger(1)"; |
|
166 |
timer.start(5000); |
|
167 |
eventLoop.exec(); |
|
168 |
||
169 |
QCOMPARE(changedSpy.count(), 1); |
|
170 |
QCOMPARE(changedSpy.at(0).count(), 1); |
|
171 |
||
172 |
QString fileName = changedSpy.at(0).at(0).toString(); |
|
173 |
QCOMPARE(fileName, testFile.fileName()); |
|
174 |
||
175 |
changedSpy.clear(); |
|
176 |
||
177 |
// remove the watch and modify the file, should not get a signal from the watcher |
|
178 |
watcher.removePath(testFile.fileName()); |
|
179 |
testFile.open(QIODevice::WriteOnly | QIODevice::Truncate); |
|
180 |
testFile.write(QByteArray("hello universe!")); |
|
181 |
testFile.close(); |
|
182 |
||
183 |
// qDebug() << "waiting max 5 seconds for notification for file modification to trigger (2)"; |
|
184 |
timer.start(5000); |
|
185 |
eventLoop.exec(); |
|
186 |
||
187 |
QCOMPARE(changedSpy.count(), 0); |
|
188 |
||
189 |
// readd the file watch with a relative path |
|
190 |
watcher.addPath(testFile.fileName().prepend("./")); |
|
191 |
testFile.open(QIODevice::WriteOnly | QIODevice::Truncate); |
|
192 |
testFile.write(QByteArray("hello multiverse!")); |
|
193 |
testFile.close(); |
|
194 |
||
195 |
timer.start(5000); |
|
196 |
eventLoop.exec(); |
|
197 |
||
198 |
QVERIFY(changedSpy.count() > 0); |
|
199 |
||
200 |
watcher.removePath(testFile.fileName().prepend("./")); |
|
201 |
||
202 |
changedSpy.clear(); |
|
203 |
||
204 |
// readd the file watch |
|
205 |
watcher.addPath(testFile.fileName()); |
|
206 |
||
207 |
// change the permissions, should get a signal from the watcher |
|
208 |
testFile.setPermissions(QFile::ReadOwner); |
|
209 |
||
210 |
// qDebug() << "waiting max 5 seconds for notification for file permission modification to trigger(1)"; |
|
211 |
timer.start(5000); |
|
212 |
eventLoop.exec(); |
|
213 |
||
214 |
QCOMPARE(changedSpy.count(), 1); |
|
215 |
QCOMPARE(changedSpy.at(0).count(), 1); |
|
216 |
||
217 |
fileName = changedSpy.at(0).at(0).toString(); |
|
218 |
QCOMPARE(fileName, testFile.fileName()); |
|
219 |
||
220 |
changedSpy.clear(); |
|
221 |
||
222 |
// remove the watch and modify file permissions, should not get a signal from the watcher |
|
223 |
watcher.removePath(testFile.fileName()); |
|
224 |
testFile.setPermissions(QFile::ReadOwner | QFile::WriteOwner | QFile::ExeOther); |
|
225 |
||
226 |
// qDebug() << "waiting max 5 seconds for notification for file modification to trigger (2)"; |
|
227 |
timer.start(5000); |
|
228 |
eventLoop.exec(); |
|
229 |
||
230 |
QCOMPARE(changedSpy.count(), 0); |
|
231 |
||
232 |
// readd the file watch |
|
233 |
watcher.addPath(testFile.fileName()); |
|
234 |
||
235 |
// remove the file, should get a signal from the watcher |
|
236 |
QVERIFY(testFile.remove()); |
|
237 |
||
238 |
// qDebug() << "waiting max 5 seconds for notification for file removal to trigger"; |
|
239 |
timer.start(5000); |
|
240 |
eventLoop.exec(); |
|
241 |
||
242 |
QVERIFY(changedSpy.count() == 1 || changedSpy.count() == 2); // removing a file on some filesystems seems to deliver 2 notifications |
|
243 |
QCOMPARE(changedSpy.at(0).count(), 1); |
|
244 |
||
245 |
fileName = changedSpy.at(0).at(0).toString(); |
|
246 |
QCOMPARE(fileName, testFile.fileName()); |
|
247 |
||
248 |
changedSpy.clear(); |
|
249 |
||
250 |
// recreate the file, we should not get any notification |
|
251 |
QVERIFY(testFile.open(QIODevice::WriteOnly | QIODevice::Truncate)); |
|
252 |
testFile.write(QByteArray("hello")); |
|
253 |
testFile.close(); |
|
254 |
||
255 |
// qDebug() << "waiting max 5 seconds for notification for file recreation to trigger"; |
|
256 |
timer.start(5000); |
|
257 |
eventLoop.exec(); |
|
258 |
||
259 |
QCOMPARE(changedSpy.count(), 0); |
|
260 |
||
261 |
QVERIFY(testFile.remove()); |
|
262 |
} |
|
263 |
||
264 |
void tst_QFileSystemWatcher::watchDirectory() |
|
265 |
{ |
|
266 |
QFETCH(QString, backend); |
|
267 |
qDebug() << "Testing" << backend << "engine"; |
|
268 |
||
269 |
QDir().mkdir("testDir"); |
|
270 |
QDir testDir("testDir"); |
|
271 |
||
272 |
QString testFileName = testDir.filePath("testFile.txt"); |
|
273 |
QFile::remove(testFileName); |
|
274 |
||
275 |
QFileSystemWatcher watcher; |
|
276 |
watcher.setObjectName(QLatin1String("_qt_autotest_force_engine_") + backend); |
|
277 |
watcher.addPath(testDir.dirName()); |
|
278 |
||
279 |
QSignalSpy changedSpy(&watcher, SIGNAL(directoryChanged(const QString &))); |
|
280 |
QEventLoop eventLoop; |
|
281 |
connect(&watcher, |
|
282 |
SIGNAL(directoryChanged(const QString &)), |
|
283 |
&eventLoop, |
|
284 |
SLOT(quit())); |
|
285 |
QTimer timer; |
|
286 |
connect(&timer, SIGNAL(timeout()), &eventLoop, SLOT(quit())); |
|
287 |
||
288 |
// resolution of the modification time is system dependent, but it's at most 1 second when using |
|
289 |
// the polling engine. From what I know, FAT32 has a 2 second resolution. So we have to |
|
290 |
// wait before modifying the directory... |
|
291 |
QTest::qWait(2000); |
|
292 |
QFile testFile(testFileName); |
|
293 |
QString fileName; |
|
294 |
||
295 |
// remove the watch, should not get notification of a new file |
|
296 |
watcher.removePath(testDir.dirName()); |
|
297 |
QVERIFY(testFile.open(QIODevice::WriteOnly | QIODevice::Truncate)); |
|
298 |
testFile.close(); |
|
299 |
||
300 |
// qDebug() << "waiting max 5 seconds for notification for file recreationg to trigger"; |
|
301 |
timer.start(5000); |
|
302 |
eventLoop.exec(); |
|
303 |
||
304 |
QCOMPARE(changedSpy.count(), 0); |
|
305 |
||
306 |
watcher.addPath(testDir.dirName()); |
|
307 |
||
308 |
// remove the file again, should get a signal from the watcher |
|
309 |
QVERIFY(testFile.remove()); |
|
310 |
||
311 |
timer.start(5000); |
|
312 |
eventLoop.exec(); |
|
313 |
||
314 |
// remove the directory, should get a signal from the watcher |
|
315 |
QVERIFY(QDir().rmdir("testDir")); |
|
316 |
||
317 |
// qDebug() << "waiting max 5 seconds for notification for directory removal to trigger"; |
|
318 |
timer.start(5000); |
|
319 |
eventLoop.exec(); |
|
320 |
||
321 |
#ifdef Q_OS_WINCE |
|
322 |
QEXPECT_FAIL("poller", "Directory does not get updated on file removal(See #137910)", Abort); |
|
323 |
#endif |
|
324 |
QCOMPARE(changedSpy.count(), 2); |
|
325 |
QCOMPARE(changedSpy.at(0).count(), 1); |
|
326 |
QCOMPARE(changedSpy.at(1).count(), 1); |
|
327 |
||
328 |
fileName = changedSpy.at(0).at(0).toString(); |
|
329 |
QCOMPARE(fileName, testDir.dirName()); |
|
330 |
fileName = changedSpy.at(1).at(0).toString(); |
|
331 |
QCOMPARE(fileName, testDir.dirName()); |
|
332 |
||
333 |
changedSpy.clear(); |
|
334 |
||
335 |
// recreate the file, we should not get any notification |
|
336 |
if (!QDir().mkdir("testDir")) |
|
337 |
QSKIP("Failed to recreate directory, skipping final test.", SkipSingle); |
|
338 |
||
339 |
// qDebug() << "waiting max 5 seconds for notification for dir recreation to trigger"; |
|
340 |
timer.start(5000); |
|
341 |
eventLoop.exec(); |
|
342 |
||
343 |
QCOMPARE(changedSpy.count(), 0); |
|
344 |
||
345 |
QVERIFY(QDir().rmdir("testDir")); |
|
346 |
} |
|
347 |
||
348 |
void tst_QFileSystemWatcher::addPath() |
|
349 |
{ |
|
350 |
QFileSystemWatcher watcher; |
|
351 |
QString home = QDir::homePath(); |
|
352 |
watcher.addPath(home); |
|
353 |
QCOMPARE(watcher.directories().count(), 1); |
|
354 |
QCOMPARE(watcher.directories().first(), home); |
|
355 |
watcher.addPath(home); |
|
356 |
QCOMPARE(watcher.directories().count(), 1); |
|
357 |
||
358 |
// With empty string |
|
359 |
QTest::ignoreMessage(QtWarningMsg, "QFileSystemWatcher::addPath: path is empty"); |
|
360 |
watcher.addPath(QString()); |
|
361 |
} |
|
362 |
||
363 |
void tst_QFileSystemWatcher::removePath() |
|
364 |
{ |
|
365 |
QFileSystemWatcher watcher; |
|
366 |
QString home = QDir::homePath(); |
|
367 |
watcher.addPath(home); |
|
368 |
watcher.removePath(home); |
|
369 |
QCOMPARE(watcher.directories().count(), 0); |
|
370 |
watcher.removePath(home); |
|
371 |
QCOMPARE(watcher.directories().count(), 0); |
|
372 |
||
373 |
// With empty string |
|
374 |
QTest::ignoreMessage(QtWarningMsg, "QFileSystemWatcher::removePath: path is empty"); |
|
375 |
watcher.removePath(QString()); |
|
376 |
} |
|
377 |
||
378 |
void tst_QFileSystemWatcher::addPaths() |
|
379 |
{ |
|
380 |
QFileSystemWatcher watcher; |
|
381 |
QStringList paths; |
|
382 |
paths << QDir::homePath() << QDir::currentPath(); |
|
383 |
watcher.addPaths(paths); |
|
384 |
QCOMPARE(watcher.directories().count(), 2); |
|
385 |
||
386 |
// With empty list |
|
387 |
paths.clear(); |
|
388 |
QTest::ignoreMessage(QtWarningMsg, "QFileSystemWatcher::addPaths: list is empty"); |
|
389 |
watcher.addPaths(paths); |
|
390 |
} |
|
391 |
||
392 |
void tst_QFileSystemWatcher::removePaths() |
|
393 |
{ |
|
394 |
QFileSystemWatcher watcher; |
|
395 |
QStringList paths; |
|
396 |
paths << QDir::homePath() << QDir::currentPath(); |
|
397 |
watcher.addPaths(paths); |
|
398 |
QCOMPARE(watcher.directories().count(), 2); |
|
399 |
watcher.removePaths(paths); |
|
400 |
QCOMPARE(watcher.directories().count(), 0); |
|
401 |
||
402 |
//With empty list |
|
403 |
paths.clear(); |
|
404 |
QTest::ignoreMessage(QtWarningMsg, "QFileSystemWatcher::removePaths: list is empty"); |
|
405 |
watcher.removePaths(paths); |
|
406 |
} |
|
407 |
||
408 |
#if 0 |
|
409 |
class SignalTest : public QObject { |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
410 |
Q_OBJECT |
0 | 411 |
public slots: |
412 |
void fileSlot(const QString &file) { qDebug() << "file " << file;} |
|
413 |
void dirSlot(const QString &dir) { qDebug() << "dir" << dir;} |
|
414 |
}; |
|
415 |
#endif |
|
416 |
||
417 |
void tst_QFileSystemWatcher::watchFileAndItsDirectory() |
|
418 |
{ |
|
419 |
QFETCH(QString, backend); |
|
420 |
QDir().mkdir("testDir"); |
|
421 |
QDir testDir("testDir"); |
|
422 |
||
423 |
QString testFileName = testDir.filePath("testFile.txt"); |
|
424 |
QString secondFileName = testDir.filePath("testFile2.txt"); |
|
425 |
QFile::remove(secondFileName); |
|
426 |
||
427 |
QFile testFile(testFileName); |
|
428 |
testFile.setPermissions(QFile::ReadOwner | QFile::WriteOwner); |
|
429 |
testFile.remove(); |
|
430 |
||
431 |
QVERIFY(testFile.open(QIODevice::WriteOnly | QIODevice::Truncate)); |
|
432 |
testFile.write(QByteArray("hello")); |
|
433 |
testFile.close(); |
|
434 |
||
435 |
QFileSystemWatcher watcher; |
|
436 |
watcher.setObjectName(QLatin1String("_qt_autotest_force_engine_") + backend); |
|
437 |
||
438 |
watcher.addPath(testDir.dirName()); |
|
439 |
watcher.addPath(testFileName); |
|
440 |
||
441 |
/* |
|
442 |
SignalTest signalTest; |
|
443 |
QObject::connect(&watcher, SIGNAL(fileChanged(const QString &)), &signalTest, SLOT(fileSlot(const QString &))); |
|
444 |
QObject::connect(&watcher, SIGNAL(directoryChanged(const QString &)), &signalTest, SLOT(dirSlot(const QString &))); |
|
445 |
*/ |
|
446 |
||
447 |
QSignalSpy fileChangedSpy(&watcher, SIGNAL(fileChanged(const QString &))); |
|
448 |
QSignalSpy dirChangedSpy(&watcher, SIGNAL(directoryChanged(const QString &))); |
|
449 |
QEventLoop eventLoop; |
|
450 |
QTimer timer; |
|
451 |
connect(&timer, SIGNAL(timeout()), &eventLoop, SLOT(quit())); |
|
452 |
||
453 |
// resolution of the modification time is system dependent, but it's at most 1 second when using |
|
454 |
// the polling engine. From what I know, FAT32 has a 2 second resolution. So we have to |
|
455 |
// wait before modifying the directory... |
|
456 |
QTest::qWait(2000); |
|
457 |
||
458 |
QVERIFY(testFile.open(QIODevice::WriteOnly | QIODevice::Truncate)); |
|
459 |
testFile.write(QByteArray("hello again")); |
|
460 |
testFile.close(); |
|
461 |
||
462 |
timer.start(3000); |
|
463 |
eventLoop.exec(); |
|
464 |
QVERIFY(fileChangedSpy.count() > 0); |
|
465 |
//according to Qt 4 documentation: |
|
466 |
//void QFileSystemWatcher::directoryChanged ( const QString & path ) [signal] |
|
467 |
//This signal is emitted when the directory at a specified path, is modified |
|
468 |
//(e.g., when a file is added, -->modified<-- or deleted) or removed from disk. |
|
469 |
//Note that if there are several changes during a short period of time, some |
|
470 |
//of the changes might not emit this signal. However, the last change in the |
|
471 |
//sequence of changes will always generate this signal. |
|
472 |
//Symbian behaves as documented (and can't be filtered), but the other platforms don't |
|
473 |
//so test should not assert this |
|
474 |
QVERIFY(dirChangedSpy.count() < 2); |
|
475 |
||
476 |
if (backend == "dnotify") |
|
477 |
QSKIP("dnotify is broken, skipping the rest of the test.", SkipSingle); |
|
478 |
||
479 |
fileChangedSpy.clear(); |
|
480 |
dirChangedSpy.clear(); |
|
481 |
QFile secondFile(secondFileName); |
|
482 |
secondFile.open(QIODevice::WriteOnly | QIODevice::Truncate); |
|
483 |
secondFile.write("Foo"); |
|
484 |
secondFile.close(); |
|
485 |
||
486 |
timer.start(3000); |
|
487 |
eventLoop.exec(); |
|
488 |
QCOMPARE(fileChangedSpy.count(), 0); |
|
489 |
#ifdef Q_OS_WINCE |
|
490 |
QEXPECT_FAIL("poller", "Directory does not get updated on file removal(See #137910)", Abort); |
|
491 |
#endif |
|
492 |
QCOMPARE(dirChangedSpy.count(), 1); |
|
493 |
||
494 |
dirChangedSpy.clear(); |
|
495 |
||
496 |
QFile::remove(testFileName); |
|
497 |
||
498 |
timer.start(3000); |
|
499 |
eventLoop.exec(); |
|
500 |
QVERIFY(fileChangedSpy.count() > 0); |
|
501 |
QCOMPARE(dirChangedSpy.count(), 1); |
|
502 |
||
503 |
fileChangedSpy.clear(); |
|
504 |
dirChangedSpy.clear(); |
|
505 |
||
506 |
watcher.removePath(testFileName); |
|
507 |
QFile::remove(secondFileName); |
|
508 |
||
509 |
timer.start(3000); |
|
510 |
eventLoop.exec(); |
|
511 |
QCOMPARE(fileChangedSpy.count(), 0); |
|
512 |
// polling watcher has generated separate events for content and time change |
|
513 |
// on Symbian emulator, so allow possibility of 2 events |
|
514 |
QVERIFY(dirChangedSpy.count() == 1 || dirChangedSpy.count() == 2); |
|
515 |
||
516 |
QVERIFY(QDir().rmdir("testDir")); |
|
517 |
} |
|
518 |
||
519 |
void tst_QFileSystemWatcher::cleanup() |
|
520 |
{ |
|
521 |
QDir testDir("testDir"); |
|
522 |
QString testFileName = testDir.filePath("testFile.txt"); |
|
523 |
QString secondFileName = testDir.filePath("testFile2.txt"); |
|
524 |
QFile::remove(testFileName); |
|
525 |
QFile::remove(secondFileName); |
|
526 |
QDir().rmdir("testDir"); |
|
527 |
} |
|
528 |
||
529 |
void tst_QFileSystemWatcher::nonExistingFile() |
|
530 |
{ |
|
531 |
// Don't crash... |
|
532 |
QFileSystemWatcher watcher; |
|
533 |
watcher.addPath("file_that_does_not_exist.txt"); |
|
534 |
QVERIFY(true); |
|
535 |
} |
|
536 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
537 |
void tst_QFileSystemWatcher::removeFileAndUnWatch() |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
538 |
{ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
539 |
static const char * const filename = "foo.txt"; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
540 |
QFileSystemWatcher watcher; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
541 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
542 |
{ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
543 |
QFile testFile(filename); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
544 |
testFile.open(QIODevice::WriteOnly); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
545 |
testFile.close(); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
546 |
} |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
547 |
watcher.addPath(filename); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
548 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
549 |
QFile::remove(filename); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
550 |
watcher.removePath(filename); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
551 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
552 |
{ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
553 |
QFile testFile(filename); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
554 |
testFile.open(QIODevice::WriteOnly); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
555 |
testFile.close(); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
556 |
} |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
557 |
watcher.addPath(filename); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
558 |
} |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
559 |
|
0 | 560 |
QTEST_MAIN(tst_QFileSystemWatcher) |
561 |
#include "tst_qfilesystemwatcher.moc" |