testinotifywatcher.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * This software is in the public domain, furnished "as is", without technical
  3. * support, and with no warranty, express or implied, as to its usefulness for
  4. * any purpose.
  5. * */
  6. #include <QtTest>
  7. #include "folderwatcher_linux.h"
  8. #include "utility.h"
  9. using namespace OCC;
  10. class TestInotifyWatcher: public FolderWatcherPrivate
  11. {
  12. Q_OBJECT
  13. private:
  14. QString _root;
  15. private slots:
  16. void initTestCase() {
  17. qsrand(QTime::currentTime().msec());
  18. _root = QDir::tempPath() + "/" + "test_" + QString::number(qrand());
  19. qDebug() << "creating test directory tree in " << _root;
  20. QDir rootDir(_root);
  21. rootDir.mkpath(_root + "/a1/b1/c1");
  22. rootDir.mkpath(_root + "/a1/b1/c2");
  23. rootDir.mkpath(_root + "/a1/b2/c1");
  24. rootDir.mkpath(_root + "/a1/b3/c3");
  25. rootDir.mkpath(_root + "/a2/b3/c3");
  26. }
  27. // Test the recursive path listing function findFoldersBelow
  28. void testDirsBelowPath() {
  29. QStringList dirs;
  30. bool ok = findFoldersBelow(QDir(_root), dirs);
  31. QVERIFY( dirs.indexOf(_root + "/a1")>-1);
  32. QVERIFY( dirs.indexOf(_root + "/a1/b1")>-1);
  33. QVERIFY( dirs.indexOf(_root + "/a1/b1/c1")>-1);
  34. QVERIFY( dirs.indexOf(_root + "/a1/b1/c2")>-1);
  35. QVERIFY(Utility::writeRandomFile(_root+"/a1/rand1.dat"));
  36. QVERIFY(Utility::writeRandomFile(_root+"/a1/b1/rand2.dat"));
  37. QVERIFY(Utility::writeRandomFile(_root+"/a1/b1/c1/rand3.dat"));
  38. QVERIFY( dirs.indexOf(_root + "/a1/b2")>-1);
  39. QVERIFY( dirs.indexOf(_root + "/a1/b2/c1")>-1);
  40. QVERIFY( dirs.indexOf(_root + "/a1/b3")>-1);
  41. QVERIFY( dirs.indexOf(_root + "/a1/b3/c3")>-1);
  42. QVERIFY( dirs.indexOf(_root + "/a2"));
  43. QVERIFY( dirs.indexOf(_root + "/a2/b3"));
  44. QVERIFY( dirs.indexOf(_root + "/a2/b3/c3"));
  45. QVERIFY2(dirs.count() == 11, "Directory count wrong.");
  46. QVERIFY2(ok, "findFoldersBelow failed.");
  47. }
  48. void cleanupTestCase() {
  49. if( _root.startsWith(QDir::tempPath() )) {
  50. system( QString("rm -rf %1").arg(_root).toLocal8Bit() );
  51. }
  52. }
  53. };
  54. QTEST_APPLESS_MAIN(TestInotifyWatcher)
  55. #include "testinotifywatcher.moc"