diff options
Diffstat (limited to 'tests/auto/qfilesystemwatcher/tst_qfilesystemwatcher.cpp')
-rw-r--r-- | tests/auto/qfilesystemwatcher/tst_qfilesystemwatcher.cpp | 130 |
1 files changed, 129 insertions, 1 deletions
diff --git a/tests/auto/qfilesystemwatcher/tst_qfilesystemwatcher.cpp b/tests/auto/qfilesystemwatcher/tst_qfilesystemwatcher.cpp index 1c103bb..748af2d 100644 --- a/tests/auto/qfilesystemwatcher/tst_qfilesystemwatcher.cpp +++ b/tests/auto/qfilesystemwatcher/tst_qfilesystemwatcher.cpp @@ -74,6 +74,10 @@ private slots: void addPaths(); void removePaths(); + void watchFileAndItsDirectory_data() { basicTest_data(); } + void watchFileAndItsDirectory(); + + void cleanup(); private: QStringList do_force_engines; bool do_force_native; @@ -94,7 +98,7 @@ tst_QFileSystemWatcher::tst_QFileSystemWatcher() if (inotify_init() != -1) do_force_engines << "inotify"; #endif -#elif defined(Q_OS_WIN) || defined(Q_OS_DARWIN) || defined(Q_OS_FREEBSD) +#elif defined(Q_OS_WIN) || defined(Q_OS_DARWIN) || defined(Q_OS_FREEBSD) || defined(Q_OS_SYMBIAN) // we have native engines for win32, macosx and freebsd do_force_engines << "native"; #endif @@ -312,6 +316,9 @@ void tst_QFileSystemWatcher::watchDirectory() #ifdef Q_OS_WINCE QEXPECT_FAIL("poller", "Directory does not get updated on file removal(See #137910)", Abort); +#elif defined(Q_OS_SYMBIAN) && defined(Q_CC_RVCT) + // Since native watcher is always used in real devices, this poller issue is irrelevant + QEXPECT_FAIL("poller", "Poller doesn't detect directory removal in RVCT builds", Abort); #endif QCOMPARE(changedSpy.count(), 2); QCOMPARE(changedSpy.at(0).count(), 1); @@ -397,5 +404,126 @@ void tst_QFileSystemWatcher::removePaths() watcher.removePaths(paths); } +#if 0 +class SignalTest : public QObject { + Q_OBJECT; + public slots: + void fileSlot(const QString &file) { qDebug() << "file " << file;} + void dirSlot(const QString &dir) { qDebug() << "dir" << dir;} +}; +#endif + +void tst_QFileSystemWatcher::watchFileAndItsDirectory() +{ + QFETCH(QString, backend); + QDir().mkdir("testDir"); + QDir testDir("testDir"); + + QString testFileName = testDir.filePath("testFile.txt"); + QString secondFileName = testDir.filePath("testFile2.txt"); + QFile::remove(secondFileName); + + QFile testFile(testFileName); + testFile.setPermissions(QFile::ReadOwner | QFile::WriteOwner); + testFile.remove(); + + QVERIFY(testFile.open(QIODevice::WriteOnly | QIODevice::Truncate)); + testFile.write(QByteArray("hello")); + testFile.close(); + + QFileSystemWatcher watcher; + watcher.setObjectName(QLatin1String("_qt_autotest_force_engine_") + backend); + + watcher.addPath(testDir.dirName()); + watcher.addPath(testFileName); + + /* + SignalTest signalTest; + QObject::connect(&watcher, SIGNAL(fileChanged(const QString &)), &signalTest, SLOT(fileSlot(const QString &))); + QObject::connect(&watcher, SIGNAL(directoryChanged(const QString &)), &signalTest, SLOT(dirSlot(const QString &))); + */ + + QSignalSpy fileChangedSpy(&watcher, SIGNAL(fileChanged(const QString &))); + QSignalSpy dirChangedSpy(&watcher, SIGNAL(directoryChanged(const QString &))); + QEventLoop eventLoop; + QTimer timer; + connect(&timer, SIGNAL(timeout()), &eventLoop, SLOT(quit())); + + // resolution of the modification time is system dependent, but it's at most 1 second when using + // the polling engine. From what I know, FAT32 has a 2 second resolution. So we have to + // wait before modifying the directory... + QTest::qWait(2000); + + QVERIFY(testFile.open(QIODevice::WriteOnly | QIODevice::Truncate)); + testFile.write(QByteArray("hello again")); + testFile.close(); + + timer.start(3000); + eventLoop.exec(); + QVERIFY(fileChangedSpy.count() > 0); + //according to Qt 4 documentation: + //void QFileSystemWatcher::directoryChanged ( const QString & path ) [signal] + //This signal is emitted when the directory at a specified path, is modified + //(e.g., when a file is added, -->modified<-- or deleted) or removed from disk. + //Note that if there are several changes during a short period of time, some + //of the changes might not emit this signal. However, the last change in the + //sequence of changes will always generate this signal. + //Symbian behaves as documented (and can't be filtered), but the other platforms don't + //so test should not assert this + QVERIFY(dirChangedSpy.count() < 2); + + if (backend == "dnotify") + QSKIP("dnotify is broken, skipping the rest of the test.", SkipSingle); + + fileChangedSpy.clear(); + dirChangedSpy.clear(); + QFile secondFile(secondFileName); + secondFile.open(QIODevice::WriteOnly | QIODevice::Truncate); + secondFile.write("Foo"); + secondFile.close(); + + timer.start(3000); + eventLoop.exec(); + QCOMPARE(fileChangedSpy.count(), 0); +#if defined(Q_OS_SYMBIAN) && defined(Q_CC_RVCT) + // Since native watcher is always used in real devices, this poller issue is irrelevant + // Symbian file system does not change modification time on a directory when a file inside is changed + QEXPECT_FAIL("poller", "Poller doesn't detect directory changes in RVCT builds", Abort); +#endif + QCOMPARE(dirChangedSpy.count(), 1); + + dirChangedSpy.clear(); + + QFile::remove(testFileName); + + timer.start(3000); + eventLoop.exec(); + QVERIFY(fileChangedSpy.count() > 0); + QCOMPARE(dirChangedSpy.count(), 1); + + fileChangedSpy.clear(); + dirChangedSpy.clear(); + + watcher.removePath(testFileName); + QFile::remove(secondFileName); + + timer.start(3000); + eventLoop.exec(); + QCOMPARE(fileChangedSpy.count(), 0); + QCOMPARE(dirChangedSpy.count(), 1); + + QVERIFY(QDir().rmdir("testDir")); +} + +void tst_QFileSystemWatcher::cleanup() +{ + QDir testDir("testDir"); + QString testFileName = testDir.filePath("testFile.txt"); + QString secondFileName = testDir.filePath("testFile2.txt"); + QFile::remove(testFileName); + QFile::remove(secondFileName); + QDir().rmdir("testDir"); +} + QTEST_MAIN(tst_QFileSystemWatcher) #include "tst_qfilesystemwatcher.moc" |