testfolderwatcher.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. */
  7. #include <QtTest>
  8. #include "folderwatcher.h"
  9. #include "utility.h"
  10. using namespace OCC;
  11. class TestFolderWatcher : public QObject
  12. {
  13. Q_OBJECT
  14. public slots:
  15. void slotFolderChanged( const QString& path ) {
  16. if (_skipNotifications.contains(path)) {
  17. return;
  18. }
  19. if (_requiredNotifications.contains(path)) {
  20. _receivedNotifications.insert(path);
  21. }
  22. }
  23. void slotEnd() { // in case something goes wrong...
  24. _loop.quit();
  25. QVERIFY2(1 == 0, "Loop hang!");
  26. }
  27. private:
  28. QString _root;
  29. FolderWatcher *_watcher;
  30. QEventLoop _loop;
  31. QTimer _timer;
  32. QSet<QString> _requiredNotifications;
  33. QSet<QString> _receivedNotifications;
  34. QSet<QString> _skipNotifications;
  35. void processAndWait()
  36. {
  37. _loop.processEvents();
  38. Utility::usleep(200000);
  39. _loop.processEvents();
  40. }
  41. private slots:
  42. void initTestCase() {
  43. qsrand(QTime::currentTime().msec());
  44. _root = QDir::tempPath() + "/" + "test_" + QString::number(qrand());
  45. qDebug() << "creating test directory tree in " << _root;
  46. QDir rootDir(_root);
  47. rootDir.mkpath(_root + "/a1/b1/c1");
  48. rootDir.mkpath(_root + "/a1/b1/c2");
  49. rootDir.mkpath(_root + "/a1/b2/c1");
  50. rootDir.mkpath(_root + "/a1/b3/c3");
  51. rootDir.mkpath(_root + "/a2/b3/c3");
  52. Utility::writeRandomFile( _root+"/a1/random.bin");
  53. Utility::writeRandomFile( _root+"/a1/b2/todelete.bin");
  54. Utility::writeRandomFile( _root+"/a2/renamefile");
  55. Utility::writeRandomFile( _root+"/a1/movefile");
  56. _watcher = new FolderWatcher(_root);
  57. QObject::connect(_watcher, SIGNAL(pathChanged(QString)), this, SLOT(slotFolderChanged(QString)));
  58. _timer.singleShot(5000, this, SLOT(slotEnd()));
  59. }
  60. void init()
  61. {
  62. _receivedNotifications.clear();
  63. _requiredNotifications.clear();
  64. _skipNotifications.clear();
  65. }
  66. void checkNotifications()
  67. {
  68. processAndWait();
  69. QCOMPARE(_receivedNotifications, _requiredNotifications);
  70. }
  71. void testACreate() { // create a new file
  72. QString file(_root + "/foo.txt");
  73. QString cmd;
  74. _requiredNotifications.insert(file);
  75. cmd = QString("echo \"xyz\" > %1").arg(file);
  76. qDebug() << "Command: " << cmd;
  77. system(cmd.toLocal8Bit());
  78. checkNotifications();
  79. }
  80. void testATouch() { // touch an existing file.
  81. QString file(_root + "/a1/random.bin");
  82. _requiredNotifications.insert(file);
  83. #ifdef Q_OS_WIN
  84. Utility::writeRandomFile(QString("%1/a1/random.bin").arg(_root));
  85. #else
  86. QString cmd;
  87. cmd = QString("touch %1").arg(file);
  88. qDebug() << "Command: " << cmd;
  89. system(cmd.toLocal8Bit());
  90. #endif
  91. checkNotifications();
  92. }
  93. void testCreateADir() {
  94. QString file(_root+"/a1/b1/new_dir");
  95. _requiredNotifications.insert(file);
  96. //_skipNotifications.insert(_root + "/a1/b1/new_dir");
  97. QDir dir;
  98. dir.mkdir(file);
  99. QVERIFY(QFile::exists(file));
  100. checkNotifications();
  101. }
  102. void testRemoveADir() {
  103. QString file(_root+"/a1/b3/c3");
  104. _requiredNotifications.insert(file);
  105. QDir dir;
  106. QVERIFY(dir.rmdir(file));
  107. checkNotifications();
  108. }
  109. void testRemoveAFile() {
  110. QString file(_root+"/a1/b2/todelete.bin");
  111. _requiredNotifications.insert(file);
  112. QVERIFY(QFile::exists(file));
  113. QFile::remove(file);
  114. QVERIFY(!QFile::exists(file));
  115. checkNotifications();
  116. }
  117. void testRenameAFile() {
  118. QString file1(_root+"/a2/renamefile");
  119. QString file2(_root+"/a2/renamefile.renamed");
  120. _requiredNotifications.insert(file1);
  121. _requiredNotifications.insert(file2);
  122. QVERIFY(QFile::exists(file1));
  123. QFile::rename(file1, file2);
  124. QVERIFY(QFile::exists(file2));
  125. checkNotifications();
  126. }
  127. void testMoveAFile() {
  128. QString old_file(_root+"/a1/movefile");
  129. QString new_file(_root+"/a2/movefile.renamed");
  130. _requiredNotifications.insert(old_file);
  131. _requiredNotifications.insert(new_file);
  132. QVERIFY(QFile::exists(old_file));
  133. QFile::rename(old_file, new_file);
  134. QVERIFY(QFile::exists(new_file));
  135. checkNotifications();
  136. }
  137. void cleanupTestCase() {
  138. if( _root.startsWith(QDir::tempPath() )) {
  139. system( QString("rm -rf %1").arg(_root).toLocal8Bit() );
  140. }
  141. delete _watcher;
  142. }
  143. };
  144. #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
  145. QTEST_MAIN(TestFolderWatcher)
  146. #else
  147. QTEST_GUILESS_MAIN(TestFolderWatcher)
  148. #endif
  149. #include "testfolderwatcher.moc"