testfolderwatcher.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. #ifndef MIRALL_TESTFOLDERWATCHER_H
  8. #define MIRALL_TESTFOLDERWATCHER_H
  9. #include <QtTest>
  10. #include "mirall/folderwatcher_linux.h"
  11. #include "mirall/utility.h"
  12. using namespace Mirall;
  13. class TestFolderWatcher : public QObject
  14. {
  15. Q_OBJECT
  16. public slots:
  17. void slotFolderChanged( const QString& path ) {
  18. qDebug() << "COMPARE: " << path << _checkMark;
  19. QVERIFY(_checkMark == path);
  20. _checkMark.clear();
  21. }
  22. void slotEnd() { // in case something goes wrong...
  23. _loop.quit();
  24. QVERIFY2(1 == 0, "Loop hang!");
  25. }
  26. private:
  27. QString _root;
  28. FolderWatcher *_watcher;
  29. QEventLoop _loop;
  30. QTimer _timer;
  31. QString _checkMark;
  32. private slots:
  33. void initTestCase() {
  34. qsrand(QTime::currentTime().msec());
  35. _root = QDir::tempPath() + "/" + "test_" + QString::number(qrand());
  36. qDebug() << "creating test directory tree in " << _root;
  37. QDir rootDir(_root);
  38. rootDir.mkpath(_root + "/a1/b1/c1");
  39. rootDir.mkpath(_root + "/a1/b1/c2");
  40. rootDir.mkpath(_root + "/a1/b2/c1");
  41. rootDir.mkpath(_root + "/a1/b3/c3");
  42. rootDir.mkpath(_root + "/a2/b3/c3");
  43. Utility::writeRandomFile( _root+"/a1/random.bin");
  44. Utility::writeRandomFile( _root+"/a1/b2/todelete.bin");
  45. Utility::writeRandomFile( _root+"/a2/movefile");
  46. _watcher = new FolderWatcher(_root);
  47. QObject::connect(_watcher, SIGNAL(folderChanged(QString)), this, SLOT(slotFolderChanged(QString)));
  48. _timer.singleShot(3000, this, SLOT(slotEnd()));
  49. }
  50. void testACreate() { // create a new file
  51. QString cmd;
  52. _checkMark = _root;
  53. cmd = QString("echo \"xyz\" > %1/foo.txt").arg(_root);
  54. qDebug() << "Command: " << cmd;
  55. system(cmd.toLocal8Bit());
  56. _loop.processEvents();
  57. QVERIFY(_checkMark.isEmpty()); // the slot clears the checkmark.
  58. }
  59. void testATouch() { // touch an existing file.
  60. QString cmd;
  61. cmd = QString("/usr/bin/touch %1/a1/random.bin").arg(_root);
  62. _checkMark = _root+"/a1";
  63. qDebug() << "Command: " << cmd;
  64. system(cmd.toLocal8Bit());
  65. _loop.processEvents();
  66. QVERIFY(_checkMark.isEmpty()); // the slot clears the checkmark.
  67. }
  68. void testCreateADir() {
  69. _checkMark = _root+"/a1/b1";
  70. QDir dir;
  71. dir.mkdir( _root + "/a1/b1/new_dir");
  72. QVERIFY(QFile::exists(_root + "/a1/b1/new_dir"));
  73. _loop.processEvents();
  74. QVERIFY(_checkMark.isEmpty()); // the slot clears the checkmark.
  75. }
  76. void testRemoveADir() {
  77. _checkMark = _root+"/a1/b1";
  78. QDir dir;
  79. dir.rmdir(_root+"/a1/b1/c2");
  80. _loop.processEvents();
  81. }
  82. void testRemoveAFile() {
  83. _checkMark = _root+"/a1/b2";
  84. QVERIFY(QFile::exists(_root+"/a1/b2/todelete.bin"));
  85. QFile::remove(_root+"/a1/b2/todelete.bin");
  86. QVERIFY(!QFile::exists(_root+"/a1/b2/todelete.bin"));
  87. _loop.processEvents();
  88. QVERIFY(_checkMark.isEmpty()); // the slot clears the checkmark.
  89. }
  90. void testMoveAFile() {
  91. _checkMark = _root+"/a2";
  92. QVERIFY(QFile::exists(_root+"/a2/movefile"));
  93. QFile::rename(_root+"/a2/movefile", _root+"/a2/movefile.renamed" );
  94. QVERIFY(QFile::exists(_root+"/a2/movefile.renamed"));
  95. _loop.processEvents();
  96. QVERIFY(_checkMark.isEmpty()); // the slot clears the checkmark.
  97. }
  98. void cleanupTestCase() {
  99. if( _root.startsWith(QDir::tempPath() )) {
  100. system( QString("rm -rf %1").arg(_root).toLocal8Bit() );
  101. }
  102. delete _watcher;
  103. }
  104. };
  105. #endif