testfolderwatcher.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. void touch(const QString &file)
  11. {
  12. #ifdef Q_OS_WIN
  13. OCC::Utility::writeRandomFile(file);
  14. #else
  15. QString cmd;
  16. cmd = QString("touch %1").arg(file);
  17. qDebug() << "Command: " << cmd;
  18. system(cmd.toLocal8Bit());
  19. #endif
  20. }
  21. void mkdir(const QString &file)
  22. {
  23. #ifdef Q_OS_WIN
  24. QDir dir;
  25. dir.mkdir(file);
  26. #else
  27. QString cmd = QString("mkdir %1").arg(file);
  28. qDebug() << "Command: " << cmd;
  29. system(cmd.toLocal8Bit());
  30. #endif
  31. }
  32. void rmdir(const QString &file)
  33. {
  34. #ifdef Q_OS_WIN
  35. QDir dir;
  36. dir.rmdir(file);
  37. #else
  38. QString cmd = QString("rmdir %1").arg(file);
  39. qDebug() << "Command: " << cmd;
  40. system(cmd.toLocal8Bit());
  41. #endif
  42. }
  43. void rm(const QString &file)
  44. {
  45. #ifdef Q_OS_WIN
  46. QFile::remove(file);
  47. #else
  48. QString cmd = QString("rm %1").arg(file);
  49. qDebug() << "Command: " << cmd;
  50. system(cmd.toLocal8Bit());
  51. #endif
  52. }
  53. void mv(const QString &file1, const QString &file2)
  54. {
  55. #ifdef Q_OS_WIN
  56. QFile::rename(file1, file2);
  57. #else
  58. QString cmd = QString("mv %1 %2").arg(file1, file2);
  59. qDebug() << "Command: " << cmd;
  60. system(cmd.toLocal8Bit());
  61. #endif
  62. }
  63. using namespace OCC;
  64. class TestFolderWatcher : public QObject
  65. {
  66. Q_OBJECT
  67. QTemporaryDir _root;
  68. QString _rootPath;
  69. QScopedPointer<FolderWatcher> _watcher;
  70. QScopedPointer<QSignalSpy> _pathChangedSpy;
  71. bool waitForPathChanged(const QString &path)
  72. {
  73. QElapsedTimer t;
  74. t.start();
  75. while (t.elapsed() < 5000) {
  76. // Check if it was already reported as changed by the watcher
  77. for (int i = 0; i < _pathChangedSpy->size(); ++i) {
  78. const auto &args = _pathChangedSpy->at(i);
  79. if (args.first().toString() == path)
  80. return true;
  81. }
  82. // Wait a bit and test again (don't bother checking if we timed out or not)
  83. _pathChangedSpy->wait(200);
  84. }
  85. return false;
  86. }
  87. public:
  88. TestFolderWatcher() {
  89. qsrand(QTime::currentTime().msec());
  90. QDir rootDir(_root.path());
  91. _rootPath = rootDir.canonicalPath();
  92. qDebug() << "creating test directory tree in " << _rootPath;
  93. rootDir.mkpath("a1/b1/c1");
  94. rootDir.mkpath("a1/b1/c2");
  95. rootDir.mkpath("a1/b2/c1");
  96. rootDir.mkpath("a1/b3/c3");
  97. rootDir.mkpath("a2/b3/c3");
  98. Utility::writeRandomFile( _rootPath+"/a1/random.bin");
  99. Utility::writeRandomFile( _rootPath+"/a1/b2/todelete.bin");
  100. Utility::writeRandomFile( _rootPath+"/a2/renamefile");
  101. Utility::writeRandomFile( _rootPath+"/a1/movefile");
  102. _watcher.reset(new FolderWatcher(_rootPath));
  103. _pathChangedSpy.reset(new QSignalSpy(_watcher.data(), SIGNAL(pathChanged(QString))));
  104. }
  105. private slots:
  106. void init()
  107. {
  108. _pathChangedSpy->clear();
  109. }
  110. void testACreate() { // create a new file
  111. QString file(_rootPath + "/foo.txt");
  112. QString cmd;
  113. cmd = QString("echo \"xyz\" > %1").arg(file);
  114. qDebug() << "Command: " << cmd;
  115. system(cmd.toLocal8Bit());
  116. QVERIFY(waitForPathChanged(file));
  117. }
  118. void testATouch() { // touch an existing file.
  119. QString file(_rootPath + "/a1/random.bin");
  120. touch(file);
  121. QVERIFY(waitForPathChanged(file));
  122. }
  123. void testCreateADir() {
  124. QString file(_rootPath+"/a1/b1/new_dir");
  125. mkdir(file);
  126. QVERIFY(waitForPathChanged(file));
  127. }
  128. void testRemoveADir() {
  129. QString file(_rootPath+"/a1/b3/c3");
  130. rmdir(file);
  131. QVERIFY(waitForPathChanged(file));
  132. }
  133. void testRemoveAFile() {
  134. QString file(_rootPath+"/a1/b2/todelete.bin");
  135. QVERIFY(QFile::exists(file));
  136. rm(file);
  137. QVERIFY(!QFile::exists(file));
  138. QVERIFY(waitForPathChanged(file));
  139. }
  140. void testRenameAFile() {
  141. QString file1(_rootPath+"/a2/renamefile");
  142. QString file2(_rootPath+"/a2/renamefile.renamed");
  143. QVERIFY(QFile::exists(file1));
  144. mv(file1, file2);
  145. QVERIFY(QFile::exists(file2));
  146. QVERIFY(waitForPathChanged(file1));
  147. QVERIFY(waitForPathChanged(file2));
  148. }
  149. void testMoveAFile() {
  150. QString old_file(_rootPath+"/a1/movefile");
  151. QString new_file(_rootPath+"/a2/movefile.renamed");
  152. QVERIFY(QFile::exists(old_file));
  153. mv(old_file, new_file);
  154. QVERIFY(QFile::exists(new_file));
  155. QVERIFY(waitForPathChanged(old_file));
  156. QVERIFY(waitForPathChanged(new_file));
  157. }
  158. };
  159. #ifdef Q_OS_MAC
  160. QTEST_MAIN(TestFolderWatcher)
  161. #else
  162. QTEST_GUILESS_MAIN(TestFolderWatcher)
  163. #endif
  164. #include "testfolderwatcher.moc"