2014-01-23 16:16:08 +04:00
|
|
|
/*
|
|
|
|
* This software is in the public domain, furnished "as is", without technical
|
|
|
|
* support, and with no warranty, express or implied, as to its usefulness for
|
|
|
|
* any purpose.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef MIRALL_TESTFOLDERWATCHER_H
|
|
|
|
#define MIRALL_TESTFOLDERWATCHER_H
|
|
|
|
|
|
|
|
#include <QtTest>
|
|
|
|
|
2014-11-19 16:45:25 +03:00
|
|
|
#include "folderwatcher.h"
|
2014-07-11 02:31:24 +04:00
|
|
|
#include "utility.h"
|
2014-01-23 16:16:08 +04:00
|
|
|
|
2014-11-10 00:36:49 +03:00
|
|
|
using namespace OCC;
|
2014-01-23 16:16:08 +04:00
|
|
|
|
|
|
|
class TestFolderWatcher : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public slots:
|
|
|
|
void slotFolderChanged( const QString& path ) {
|
2014-11-06 02:36:04 +03:00
|
|
|
if (_skipNotifications.contains(path)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (_requiredNotifications.contains(path)) {
|
|
|
|
_receivedNotifications.insert(path);
|
|
|
|
}
|
2014-01-23 16:16:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void slotEnd() { // in case something goes wrong...
|
|
|
|
_loop.quit();
|
|
|
|
QVERIFY2(1 == 0, "Loop hang!");
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
QString _root;
|
|
|
|
FolderWatcher *_watcher;
|
|
|
|
QEventLoop _loop;
|
|
|
|
QTimer _timer;
|
2014-11-06 02:36:04 +03:00
|
|
|
QSet<QString> _requiredNotifications;
|
|
|
|
QSet<QString> _receivedNotifications;
|
|
|
|
QSet<QString> _skipNotifications;
|
|
|
|
|
|
|
|
void processAndWait()
|
|
|
|
{
|
|
|
|
_loop.processEvents();
|
2014-11-21 10:20:54 +03:00
|
|
|
Utility::usleep(200000);
|
2014-11-06 02:36:04 +03:00
|
|
|
_loop.processEvents();
|
|
|
|
}
|
2014-01-23 16:16:08 +04:00
|
|
|
|
|
|
|
private slots:
|
|
|
|
void initTestCase() {
|
|
|
|
qsrand(QTime::currentTime().msec());
|
|
|
|
_root = QDir::tempPath() + "/" + "test_" + QString::number(qrand());
|
|
|
|
qDebug() << "creating test directory tree in " << _root;
|
|
|
|
QDir rootDir(_root);
|
|
|
|
|
|
|
|
rootDir.mkpath(_root + "/a1/b1/c1");
|
|
|
|
rootDir.mkpath(_root + "/a1/b1/c2");
|
|
|
|
rootDir.mkpath(_root + "/a1/b2/c1");
|
|
|
|
rootDir.mkpath(_root + "/a1/b3/c3");
|
|
|
|
rootDir.mkpath(_root + "/a2/b3/c3");
|
|
|
|
Utility::writeRandomFile( _root+"/a1/random.bin");
|
|
|
|
Utility::writeRandomFile( _root+"/a1/b2/todelete.bin");
|
2014-11-06 02:36:04 +03:00
|
|
|
Utility::writeRandomFile( _root+"/a2/renamefile");
|
|
|
|
Utility::writeRandomFile( _root+"/a1/movefile");
|
2014-01-23 16:16:08 +04:00
|
|
|
|
|
|
|
_watcher = new FolderWatcher(_root);
|
2014-11-07 12:53:41 +03:00
|
|
|
QObject::connect(_watcher, SIGNAL(pathChanged(QString)), this, SLOT(slotFolderChanged(QString)));
|
2014-11-06 02:36:04 +03:00
|
|
|
_timer.singleShot(5000, this, SLOT(slotEnd()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void init()
|
|
|
|
{
|
|
|
|
_receivedNotifications.clear();
|
|
|
|
_requiredNotifications.clear();
|
|
|
|
_skipNotifications.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void checkNotifications()
|
|
|
|
{
|
|
|
|
processAndWait();
|
|
|
|
QCOMPARE(_receivedNotifications, _requiredNotifications);
|
2014-01-23 16:16:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void testACreate() { // create a new file
|
2014-11-07 12:53:41 +03:00
|
|
|
QString file(_root + "/foo.txt");
|
2014-01-23 16:16:08 +04:00
|
|
|
QString cmd;
|
2014-11-07 12:53:41 +03:00
|
|
|
_requiredNotifications.insert(file);
|
|
|
|
cmd = QString("echo \"xyz\" > %1").arg(file);
|
2014-01-23 16:16:08 +04:00
|
|
|
qDebug() << "Command: " << cmd;
|
|
|
|
system(cmd.toLocal8Bit());
|
|
|
|
|
2014-11-06 02:36:04 +03:00
|
|
|
checkNotifications();
|
2014-01-23 16:16:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void testATouch() { // touch an existing file.
|
2014-11-07 12:53:41 +03:00
|
|
|
QString file(_root + "/a1/random.bin");
|
|
|
|
_requiredNotifications.insert(file);
|
2014-11-06 02:36:04 +03:00
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
Utility::writeRandomFile(QString("%1/a1/random.bin").arg(_root));
|
|
|
|
#else
|
2014-01-23 16:16:08 +04:00
|
|
|
QString cmd;
|
2015-02-08 02:27:46 +03:00
|
|
|
cmd = QString("touch %1").arg(file);
|
2014-01-23 16:16:08 +04:00
|
|
|
qDebug() << "Command: " << cmd;
|
|
|
|
system(cmd.toLocal8Bit());
|
2014-11-06 02:36:04 +03:00
|
|
|
#endif
|
2014-01-23 16:16:08 +04:00
|
|
|
|
2014-11-06 02:36:04 +03:00
|
|
|
checkNotifications();
|
2014-01-23 16:16:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void testCreateADir() {
|
2014-11-07 12:53:41 +03:00
|
|
|
QString file(_root+"/a1/b1/new_dir");
|
|
|
|
_requiredNotifications.insert(file);
|
|
|
|
//_skipNotifications.insert(_root + "/a1/b1/new_dir");
|
2014-01-23 16:16:08 +04:00
|
|
|
QDir dir;
|
2014-11-07 12:53:41 +03:00
|
|
|
dir.mkdir(file);
|
|
|
|
QVERIFY(QFile::exists(file));
|
2014-11-06 02:36:04 +03:00
|
|
|
|
|
|
|
checkNotifications();
|
2014-01-23 16:16:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void testRemoveADir() {
|
2014-11-07 12:53:41 +03:00
|
|
|
QString file(_root+"/a1/b3/c3");
|
|
|
|
_requiredNotifications.insert(file);
|
2014-01-23 16:16:08 +04:00
|
|
|
QDir dir;
|
2014-11-07 12:53:41 +03:00
|
|
|
QVERIFY(dir.rmdir(file));
|
2014-11-06 02:36:04 +03:00
|
|
|
|
|
|
|
checkNotifications();
|
2014-01-23 16:16:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void testRemoveAFile() {
|
2014-11-07 12:53:41 +03:00
|
|
|
QString file(_root+"/a1/b2/todelete.bin");
|
|
|
|
_requiredNotifications.insert(file);
|
|
|
|
QVERIFY(QFile::exists(file));
|
|
|
|
QFile::remove(file);
|
|
|
|
QVERIFY(!QFile::exists(file));
|
2014-11-06 02:36:04 +03:00
|
|
|
|
|
|
|
checkNotifications();
|
|
|
|
}
|
|
|
|
|
|
|
|
void testRenameAFile() {
|
2014-11-07 12:53:41 +03:00
|
|
|
QString file1(_root+"/a2/renamefile");
|
|
|
|
QString file2(_root+"/a2/renamefile.renamed");
|
|
|
|
_requiredNotifications.insert(file1);
|
|
|
|
_requiredNotifications.insert(file2);
|
|
|
|
QVERIFY(QFile::exists(file1));
|
|
|
|
QFile::rename(file1, file2);
|
|
|
|
QVERIFY(QFile::exists(file2));
|
2014-11-06 02:36:04 +03:00
|
|
|
|
|
|
|
checkNotifications();
|
2014-01-23 16:16:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void testMoveAFile() {
|
2014-11-07 12:53:41 +03:00
|
|
|
QString old_file(_root+"/a1/movefile");
|
|
|
|
QString new_file(_root+"/a2/movefile.renamed");
|
|
|
|
_requiredNotifications.insert(old_file);
|
|
|
|
_requiredNotifications.insert(new_file);
|
|
|
|
QVERIFY(QFile::exists(old_file));
|
|
|
|
QFile::rename(old_file, new_file);
|
|
|
|
QVERIFY(QFile::exists(new_file));
|
2014-11-06 02:36:04 +03:00
|
|
|
|
|
|
|
checkNotifications();
|
2014-01-23 16:16:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void cleanupTestCase() {
|
|
|
|
if( _root.startsWith(QDir::tempPath() )) {
|
|
|
|
system( QString("rm -rf %1").arg(_root).toLocal8Bit() );
|
|
|
|
}
|
|
|
|
delete _watcher;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|