TDE core libraries
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

kdirwatchunittest.cpp 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. This file is or will be part of KDE desktop environment
  3. (c) 2006 Dirk Mueller <mueller@kde.org>
  4. It is licensed under GPL version 2.
  5. If it is part of KDE libraries than this file is licensed under
  6. LGPL version 2.
  7. */
  8. #include <unistd.h>
  9. #include <tqfile.h>
  10. #include <tqdir.h>
  11. #include <kdebug.h>
  12. #include "kdirwatchunittest.h"
  13. void KDirWatchTest::VERIFY_NOTHING()
  14. {
  15. unsigned m_s[3];
  16. for(int i = 0; i < 3; ++i)
  17. m_s[i] = m_signals[i];
  18. for (int i = 0; i < 20; ++i) {
  19. usleep(100*1000);
  20. tqApp->processEvents();
  21. }
  22. bool nothing_failed = true;
  23. for (int i = 0; i < 3; ++i)
  24. if (m_s[i] != m_signals[i])
  25. nothing_failed = false;
  26. KDirWatch::statistics();
  27. VERIFY (nothing_failed);
  28. }
  29. void KDirWatchTest::VERIFY_DIRTY(const TQString& alert)
  30. {
  31. unsigned m_s[3];
  32. for(int i = 0; i < 3; ++i)
  33. m_s[i] = m_signals[i];
  34. for (int i = 0; i < 20; ++i) {
  35. usleep(100*1000);
  36. tqApp->processEvents();
  37. }
  38. VERIFY (m_s[sigCreated] == m_signals[sigCreated] &&
  39. m_s[sigDeleted] == m_signals[sigDeleted] &&
  40. m_s[sigDirty] + 1 == m_signals[sigDirty] &&
  41. m_lastSignal == alert);
  42. }
  43. void KDirWatchTest::VERIFY_CREATED(const TQString& alert)
  44. {
  45. unsigned m_s[3];
  46. for(int i = 0; i < 3; ++i)
  47. m_s[i] = m_signals[i];
  48. for (int i = 0; i < 20; ++i) {
  49. usleep(100*1000);
  50. tqApp->processEvents();
  51. }
  52. VERIFY (m_s[sigDirty] == m_signals[sigDirty] &&
  53. m_s[sigDeleted] == m_signals[sigDeleted] &&
  54. m_s[sigCreated] + 1 == m_signals[sigCreated] &&
  55. m_lastSignal == alert);
  56. }
  57. void KDirWatchTest::VERIFY_DELETED(const TQString& alert)
  58. {
  59. unsigned m_s[3];
  60. for(int i = 0; i < 3; ++i)
  61. m_s[i] = m_signals[i];
  62. for (int i = 0; i < 20; ++i) {
  63. usleep(100*1000);
  64. tqApp->processEvents();
  65. }
  66. VERIFY (m_s[sigDirty] == m_signals[sigDirty] &&
  67. m_s[sigCreated] == m_signals[sigCreated] &&
  68. m_s[sigDeleted] + 1 == m_signals[sigDeleted] &&
  69. m_lastSignal == alert);
  70. }
  71. void KDirWatchTest::remove_file (const TQString& file)
  72. {
  73. ::unlink (TQFile::encodeName(file));
  74. }
  75. void KDirWatchTest::touch_file (const TQString& file)
  76. {
  77. TQFile f(file);
  78. f.open(IO_WriteOnly);
  79. }
  80. void KDirWatchTest::rename_file(const TQString& from, const TQString& to)
  81. {
  82. ::rename(TQFile::encodeName(from), TQFile::encodeName(to));
  83. }
  84. TDEUNITTEST_MODULE ( tdeunittest_kdirwatch, "KDirWatchTest" )
  85. TDEUNITTEST_MODULE_REGISTER_TESTER (KDirWatchTest)
  86. #define SLEEP() TQApplication::processEvents();
  87. void KDirWatchTest::allTests()
  88. {
  89. for(int loop=0; loop<3; ++loop) {
  90. d = new KDirWatch;
  91. VERIFY (d != 0);
  92. TQDir* dir = new TQDir(m_workingDir);
  93. VERIFY (dir != 0);
  94. connect(d, TQT_SIGNAL (dirty( const TQString &)), TQT_SLOT( slotDirty( const TQString &)) );
  95. connect(d, TQT_SIGNAL (created( const TQString &)), TQT_SLOT( slotCreated( const TQString &)) );
  96. connect(d, TQT_SIGNAL (deleted( const TQString &)), TQT_SLOT( slotDeleted( const TQString &)) );
  97. VERIFY (dir->mkdir (m_workingDir));
  98. d->addDir (m_workingDir);
  99. VERIFY_NOTHING();
  100. dir->mkdir ("does");
  101. VERIFY_DIRTY (m_workingDir);
  102. touch_file (m_workingDir + "/file");
  103. VERIFY_DIRTY (m_workingDir);
  104. VERIFY_NOTHING ();
  105. remove_file (m_workingDir + "/file");
  106. d->addDir (m_workingDir + "/does/not/exist");
  107. d->removeDir (m_workingDir);
  108. VERIFY_NOTHING();
  109. dir->mkdir ("does/not");
  110. VERIFY_NOTHING();
  111. dir->mkdir ("does/not/exist");
  112. VERIFY_CREATED (m_workingDir + "/does/not/exist");
  113. dir->rmdir ("does/not/exist");
  114. VERIFY_DELETED (m_workingDir + "/does/not/exist");
  115. dir->rmdir ("does/not");
  116. VERIFY_NOTHING();
  117. dir->rmdir ("does");
  118. VERIFY_NOTHING();
  119. d->addFile(m_workingDir + "/a");
  120. touch_file(m_workingDir + "/a");
  121. VERIFY_CREATED (m_workingDir + "/a");
  122. rename_file (m_workingDir + "/a", m_workingDir + "/b");
  123. VERIFY_DELETED (m_workingDir + "/a");
  124. VERIFY_NOTHING();
  125. touch_file (m_workingDir + "/a");
  126. VERIFY_CREATED (m_workingDir + "/a");
  127. VERIFY_NOTHING();
  128. touch_file (m_workingDir + "/a");
  129. VERIFY_DIRTY (m_workingDir + "/a");
  130. remove_file (m_workingDir + "/b");
  131. VERIFY_NOTHING();
  132. d->removeFile(m_workingDir + "/a");
  133. remove_file(m_workingDir + "/a");
  134. VERIFY (dir->rmdir (m_workingDir));
  135. delete d;
  136. }
  137. }
  138. #include "kdirwatchunittest.moc"