diff options
Diffstat (limited to 'tests/auto/corelib/io/qfilesystemwatcher/tst_qfilesystemwatcher.cpp')
-rw-r--r-- | tests/auto/corelib/io/qfilesystemwatcher/tst_qfilesystemwatcher.cpp | 163 |
1 files changed, 101 insertions, 62 deletions
diff --git a/tests/auto/corelib/io/qfilesystemwatcher/tst_qfilesystemwatcher.cpp b/tests/auto/corelib/io/qfilesystemwatcher/tst_qfilesystemwatcher.cpp index acf26039d1..184eef3f15 100644 --- a/tests/auto/corelib/io/qfilesystemwatcher/tst_qfilesystemwatcher.cpp +++ b/tests/auto/corelib/io/qfilesystemwatcher/tst_qfilesystemwatcher.cpp @@ -1,5 +1,6 @@ // Copyright (C) 2021 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only + #include <QTest> #include <QCoreApplication> @@ -22,6 +23,23 @@ #include <QStandardPaths> #endif +using namespace std::chrono_literals; + +#if defined(Q_OS_QNX) +constexpr bool isQNX = true; +#else +constexpr bool isQNX = false; +#endif + +#if defined(Q_OS_QNX) || defined(Q_OS_VXWORKS) +// Longer polling times on QNX and VxWorks, otherwise the tests fail on the CI +constexpr auto nativeEngineTimeout = 1s; +constexpr auto pollingEngineTimeout = 1s; +#else +constexpr auto nativeEngineTimeout = 0ms; +constexpr auto pollingEngineTimeout = 20ms; +#endif + /* All tests need to run in temporary directories not used * by the application to avoid non-deterministic failures on Windows * due to locked directories and left-overs from previous tests. */ @@ -129,26 +147,27 @@ void tst_QFileSystemWatcher::basicTest() watcher.setObjectName(QLatin1String("_qt_autotest_force_engine_") + backend); QVERIFY(watcher.addPath(testFile.fileName())); + const bool isPollerBackend = backend == u"poller"; + QSignalSpy changedSpy(&watcher, &QFileSystemWatcher::fileChanged); QVERIFY(changedSpy.isValid()); QEventLoop eventLoop; QTimer timer; + timer.setInterval(isPollerBackend ? pollingEngineTimeout : nativeEngineTimeout); connect(&timer, SIGNAL(timeout()), &eventLoop, SLOT(quit())); // modify the file, should get a signal from the watcher - // resolution of the modification time is system dependent, but it's at most 1 second when using - // the polling engine. I've heard rumors that FAT32 has a 2 second resolution. So, we have to - // wait a bit before we can modify the file (hrmph)... - QTest::qWait(2000); + if (isPollerBackend || isQNX) + QTest::qWait(pollingEngineTimeout); - testFile.open(QIODevice::WriteOnly | QIODevice::Append); + QVERIFY(testFile.open(QIODevice::WriteOnly | QIODevice::Append)); testFile.write(QByteArray("world")); testFile.close(); // waiting max 5 seconds for notification for file modification to trigger - QTRY_COMPARE(changedSpy.count(), 1); - QCOMPARE(changedSpy.at(0).count(), 1); + QTRY_COMPARE(changedSpy.size(), 1); + QCOMPARE(changedSpy.at(0).size(), 1); QString fileName = changedSpy.at(0).at(0).toString(); QCOMPARE(fileName, testFile.fileName()); @@ -157,25 +176,25 @@ void tst_QFileSystemWatcher::basicTest() // remove the watch and modify the file, should not get a signal from the watcher QVERIFY(watcher.removePath(testFile.fileName())); - testFile.open(QIODevice::WriteOnly | QIODevice::Truncate); + QVERIFY(testFile.open(QIODevice::WriteOnly | QIODevice::Truncate)); testFile.write(QByteArray("hello universe!")); testFile.close(); - // waiting max 5 seconds for notification for file modification to trigger - timer.start(5000); + // waiting for notification for file modification to trigger + timer.start(); eventLoop.exec(); - QCOMPARE(changedSpy.count(), 0); + QCOMPARE(changedSpy.size(), 0); // readd the file watch with a relative path const QString relativeTestFileName = QDir::current().relativeFilePath(testFile.fileName()); QVERIFY(!relativeTestFileName.isEmpty()); QVERIFY(watcher.addPath(relativeTestFileName)); - testFile.open(QIODevice::WriteOnly | QIODevice::Truncate); + QVERIFY(testFile.open(QIODevice::WriteOnly | QIODevice::Truncate)); testFile.write(QByteArray("hello multiverse!")); testFile.close(); - QTRY_VERIFY(changedSpy.count() > 0); + QTRY_VERIFY(changedSpy.size() > 0); QVERIFY(watcher.removePath(relativeTestFileName)); @@ -191,8 +210,8 @@ void tst_QFileSystemWatcher::basicTest() #if !defined(Q_OS_QNX) // waiting max 5 seconds for notification for file permission modification to trigger - QTRY_COMPARE(changedSpy.count(), 1); - QCOMPARE(changedSpy.at(0).count(), 1); + QTRY_COMPARE(changedSpy.size(), 1); + QCOMPARE(changedSpy.at(0).size(), 1); fileName = changedSpy.at(0).at(0).toString(); QCOMPARE(fileName, testFile.fileName()); @@ -205,11 +224,11 @@ void tst_QFileSystemWatcher::basicTest() QVERIFY(watcher.removePath(testFile.fileName())); testFile.setPermissions(QFile::ReadOwner | QFile::WriteOwner | QFile::ExeOther); - // waiting max 5 seconds for notification for file modification to trigger - timer.start(5000); + // waiting for notification for file modification to trigger + timer.start(); eventLoop.exec(); - QCOMPARE(changedSpy.count(), 0); + QCOMPARE(changedSpy.size(), 0); // readd the file watch QVERIFY(watcher.addPath(testFile.fileName())); @@ -220,8 +239,8 @@ void tst_QFileSystemWatcher::basicTest() // waiting max 5 seconds for notification for file removal to trigger // > 0 && < 3 because some platforms may emit two changes // XXX: which platforms? (QTBUG-23370) - QTRY_VERIFY(changedSpy.count() > 0 && changedSpy.count() < 3); - QCOMPARE(changedSpy.at(0).count(), 1); + QTRY_VERIFY(changedSpy.size() > 0 && changedSpy.size() < 3); + QCOMPARE(changedSpy.at(0).size(), 1); fileName = changedSpy.at(0).at(0).toString(); QCOMPARE(fileName, testFile.fileName()); @@ -233,11 +252,11 @@ void tst_QFileSystemWatcher::basicTest() testFile.write(QByteArray("hello")); testFile.close(); - // waiting max 5 seconds for notification for file recreation to trigger - timer.start(5000); + // waiting for notification for file recreation to trigger + timer.start(); eventLoop.exec(); - QCOMPARE(changedSpy.count(), 0); + QCOMPARE(changedSpy.size(), 0); QVERIFY(testFile.remove()); } @@ -248,7 +267,9 @@ void tst_QFileSystemWatcher::watchDirectory_data() QTest::addColumn<QStringList>("testDirNames"); const QStringList testDirNames = {QStringLiteral("testdir"), QStringLiteral("testdir2")}; +#if !defined(QT_NO_INOTIFY) QTest::newRow("native backend") << "native" << testDirNames; +#endif QTest::newRow("poller backend") << "poller" << testDirNames; } @@ -279,16 +300,18 @@ void tst_QFileSystemWatcher::watchDirectory() watcher.setObjectName(QLatin1String("_qt_autotest_force_engine_") + backend); QVERIFY(watcher.addPaths(testDirs).isEmpty()); + const bool isPollerBackend = backend == u"poller"; + QSignalSpy changedSpy(&watcher, &QFileSystemWatcher::directoryChanged); QVERIFY(changedSpy.isValid()); QEventLoop eventLoop; QTimer timer; + timer.setInterval(isPollerBackend ? pollingEngineTimeout : nativeEngineTimeout); 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); + if (isPollerBackend || isQNX) + QTest::qWait(pollingEngineTimeout); + // remove the watch, should not get notification of a new file QVERIFY(watcher.removePaths(testDirs).isEmpty()); for (const auto &testFileName : testFiles) { @@ -297,11 +320,11 @@ void tst_QFileSystemWatcher::watchDirectory() testFile.close(); } - // waiting max 5 seconds for notification for file recreationg to trigger - timer.start(5000); + // waiting for notification for file recreation to trigger + timer.start(); eventLoop.exec(); - QCOMPARE(changedSpy.count(), 0); + QCOMPARE(changedSpy.size(), 0); QVERIFY(watcher.addPaths(testDirs).isEmpty()); @@ -309,7 +332,7 @@ void tst_QFileSystemWatcher::watchDirectory() for (const auto &testFileName : testFiles) QVERIFY(QFile::remove(testFileName)); - timer.start(5000); + timer.start(); eventLoop.exec(); // remove the directory, should get a signal from the watcher @@ -321,10 +344,10 @@ void tst_QFileSystemWatcher::watchDirectory() signalCounter[testDirName] = 0; // waiting max 5 seconds for notification for directory removal to trigger - QTRY_COMPARE(changedSpy.count(), testDirs.size() * 2); - for (int i = 0; i < changedSpy.count(); i++) { + QTRY_COMPARE(changedSpy.size(), testDirs.size() * 2); + for (int i = 0; i < changedSpy.size(); i++) { const auto &signal = changedSpy.at(i); - QCOMPARE(signal.count(), 1); + QCOMPARE(signal.size(), 1); auto it = signalCounter.find(signal.at(0).toString()); QVERIFY(it != signalCounter.end()); @@ -336,7 +359,7 @@ void tst_QFileSystemWatcher::watchDirectory() QCOMPARE(count, 2); // flush pending signals (like the one from the rmdir above) - timer.start(5000); + timer.start(); eventLoop.exec(); changedSpy.clear(); @@ -348,11 +371,11 @@ void tst_QFileSystemWatcher::watchDirectory() } } - // waiting max 5 seconds for notification for dir recreation to trigger - timer.start(5000); + // waiting for notification for dir recreation to trigger + timer.start(); eventLoop.exec(); - QCOMPARE(changedSpy.count(), 0); + QCOMPARE(changedSpy.size(), 0); for (const auto &testDirName : testDirs) QVERIFY(temporaryDir.rmdir(testDirName)); @@ -364,12 +387,12 @@ void tst_QFileSystemWatcher::addPath() QFileSystemWatcher watcher; QString home = QDir::homePath(); QVERIFY(watcher.addPath(home)); - QCOMPARE(watcher.directories().count(), 1); + QCOMPARE(watcher.directories().size(), 1); QCOMPARE(watcher.directories().first(), home); // second watch on an already-watched path should fail QVERIFY(!watcher.addPath(home)); - QCOMPARE(watcher.directories().count(), 1); + QCOMPARE(watcher.directories().size(), 1); // With empty string QTest::ignoreMessage(QtWarningMsg, "QFileSystemWatcher::addPath: path is empty"); @@ -382,9 +405,9 @@ void tst_QFileSystemWatcher::removePath() QString home = QDir::homePath(); QVERIFY(watcher.addPath(home)); QVERIFY(watcher.removePath(home)); - QCOMPARE(watcher.directories().count(), 0); + QCOMPARE(watcher.directories().size(), 0); QVERIFY(!watcher.removePath(home)); - QCOMPARE(watcher.directories().count(), 0); + QCOMPARE(watcher.directories().size(), 0); // With empty string QTest::ignoreMessage(QtWarningMsg, "QFileSystemWatcher::removePath: path is empty"); @@ -396,8 +419,14 @@ void tst_QFileSystemWatcher::addPaths() QFileSystemWatcher watcher; QStringList paths; paths << QDir::homePath() << QDir::tempPath(); +#ifndef Q_OS_QNX + // Adding this makes QNX fail and we haven't investigated why + for (const QFileInfo &fi : QDir::drives()) + paths << fi.absoluteFilePath(); // on Unix, this will be just "/" +#endif + QCOMPARE(watcher.addPaths(paths), QStringList()); - QCOMPARE(watcher.directories().count(), 2); + QCOMPARE(watcher.directories().size(), paths.size()); // With empty list paths.clear(); @@ -464,10 +493,16 @@ void tst_QFileSystemWatcher::removePaths() QFileSystemWatcher watcher; QStringList paths; paths << QDir::homePath() << QDir::tempPath(); +#ifndef Q_OS_QNX + // Adding this makes QNX fail and we haven't investigated why + for (const QFileInfo &fi : QDir::drives()) + paths << fi.absoluteFilePath(); // on Unix, this will be just "/" +#endif + QCOMPARE(watcher.addPaths(paths), QStringList()); - QCOMPARE(watcher.directories().count(), 2); + QCOMPARE(watcher.directories().size(), paths.size()); QCOMPARE(watcher.removePaths(paths), QStringList()); - QCOMPARE(watcher.directories().count(), 0); + QCOMPARE(watcher.directories().size(), 0); //With empty list paths.clear(); @@ -535,28 +570,29 @@ void tst_QFileSystemWatcher::watchFileAndItsDirectory() QVERIFY(watcher.addPath(testDir.absolutePath())); QVERIFY(watcher.addPath(testFileName)); + const bool isPollerBackend = backend == u"poller"; + QSignalSpy fileChangedSpy(&watcher, &QFileSystemWatcher::fileChanged); FileSystemWatcherSpy dirChangedSpy(&watcher, FileSystemWatcherSpy::SpyOnDirectoryChanged); QVERIFY(fileChangedSpy.isValid()); QEventLoop eventLoop; QTimer timer; + timer.setInterval(isPollerBackend ? pollingEngineTimeout : nativeEngineTimeout); 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); + if (isPollerBackend || isQNX) + QTest::qWait(pollingEngineTimeout); QVERIFY2(testFile.open(QIODevice::WriteOnly | QIODevice::Truncate), msgFileOperationFailed("open", testFile)); QVERIFY2(testFile.write(QByteArrayLiteral("hello again")), msgFileOperationFailed("write", testFile)); testFile.close(); -#ifdef Q_OS_MAC +#ifdef Q_OS_DARWIN // wait again for the file's atime to be updated QTest::qWait(2000); #endif - QTRY_VERIFY(fileChangedSpy.count() > 0); + QTRY_VERIFY(fileChangedSpy.size() > 0); QVERIFY2(dirChangedSpy.count() == 0, dirChangedSpy.receivedFilesMessage()); fileChangedSpy.clear(); @@ -565,9 +601,9 @@ void tst_QFileSystemWatcher::watchFileAndItsDirectory() QVERIFY2(secondFile.write(QByteArrayLiteral("Foo")) > 0, msgFileOperationFailed("write", secondFile)); secondFile.close(); - timer.start(3000); + timer.start(); eventLoop.exec(); - int fileChangedSpyCount = fileChangedSpy.count(); + int fileChangedSpyCount = fileChangedSpy.size(); #ifdef Q_OS_WIN if (fileChangedSpyCount != 0) QEXPECT_FAIL("", "See QTBUG-30943", Continue); @@ -579,7 +615,7 @@ void tst_QFileSystemWatcher::watchFileAndItsDirectory() QVERIFY(QFile::remove(testFileName)); - QTRY_VERIFY(fileChangedSpy.count() > 0); + QTRY_VERIFY(fileChangedSpy.size() > 0); QTRY_COMPARE(dirChangedSpy.count(), 1); fileChangedSpy.clear(); @@ -589,9 +625,9 @@ void tst_QFileSystemWatcher::watchFileAndItsDirectory() QVERIFY(!watcher.removePath(testFileName)); QVERIFY(QFile::remove(secondFileName)); - timer.start(3000); + timer.start(); eventLoop.exec(); - QCOMPARE(fileChangedSpy.count(), 0); + QCOMPARE(fileChangedSpy.size(), 0); QCOMPARE(dirChangedSpy.count(), 1); // QTBUG-61792, removal should succeed (bug on Windows which uses one change @@ -613,9 +649,11 @@ void tst_QFileSystemWatcher::nonExistingFile() QStringList() << "../..//./does-not-exist"); // empty path is not actually a failure + QTest::ignoreMessage(QtWarningMsg, "QFileSystemWatcher::addPaths: list is empty"); QCOMPARE(watcher.addPaths(QStringList() << QString()), QStringList()); // empty path is not actually a failure + QTest::ignoreMessage(QtWarningMsg, "QFileSystemWatcher::removePaths: list is empty"); QCOMPARE(watcher.removePaths(QStringList() << QString()), QStringList()); } @@ -694,7 +732,7 @@ void tst_QFileSystemWatcher::QTBUG2331() // remove directory, we should get one change signal, and we should no longer // be watching the directory. QVERIFY(temporaryDirectory.remove()); - QTRY_COMPARE(changedSpy.count(), 1); + QTRY_COMPARE(changedSpy.size(), 1); QCOMPARE(watcher.directories(), QStringList()); } #endif // QT_BUILD_INTERNAL @@ -722,7 +760,8 @@ public slots: QCOMPARE(finfo.absolutePath(), moveSrcDir.absolutePath()); if (!added) { - foreach (const QFileInfo &fi, moveDestination.entryInfoList(QDir::Files | QDir::NoSymLinks)) + const auto entries = moveDestination.entryInfoList(QDir::Files | QDir::NoSymLinks); + for (const QFileInfo &fi : entries) watcher->addPath(fi.absoluteFilePath()); added = true; } @@ -762,9 +801,9 @@ void tst_QFileSystemWatcher::signalsEmittedAfterFileMoved() QVERIFY(watcher.addPath(movePath)); // add files to watcher - QFileInfoList files = testDir.entryInfoList(QDir::Files | QDir::NoSymLinks); + const QFileInfoList files = testDir.entryInfoList(QDir::Files | QDir::NoSymLinks); QCOMPARE(files.size(), fileCount); - foreach (const QFileInfo &finfo, files) + for (const QFileInfo &finfo : files) QVERIFY(watcher.addPath(finfo.absoluteFilePath())); // create the signal receiver @@ -776,7 +815,7 @@ void tst_QFileSystemWatcher::signalsEmittedAfterFileMoved() QCOMPARE(changedSpy.count(), 0); // move files to second directory - foreach (const QFileInfo &finfo, files) + for (const QFileInfo &finfo : files) QVERIFY(testDir.rename(finfo.fileName(), QString("movehere/%2").arg(finfo.fileName()))); QCoreApplication::processEvents(); |