1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
/*
* 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.
*
*/
#include <QtTest>
#include "folderwatcher.h"
#include "utility.h"
using namespace OCC;
class TestFolderWatcher : public QObject
{
Q_OBJECT
public slots:
void slotFolderChanged( const QString& path ) {
if (_skipNotifications.contains(path)) {
return;
}
if (_requiredNotifications.contains(path)) {
_receivedNotifications.insert(path);
}
}
void slotEnd() { // in case something goes wrong...
_loop.quit();
QVERIFY2(1 == 0, "Loop hang!");
}
private:
QString _root;
FolderWatcher *_watcher;
QEventLoop _loop;
QTimer _timer;
QSet<QString> _requiredNotifications;
QSet<QString> _receivedNotifications;
QSet<QString> _skipNotifications;
void processAndWait()
{
_loop.processEvents();
Utility::usleep(200000);
_loop.processEvents();
}
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");
Utility::writeRandomFile( _root+"/a2/renamefile");
Utility::writeRandomFile( _root+"/a1/movefile");
_watcher = new FolderWatcher(_root);
QObject::connect(_watcher, SIGNAL(pathChanged(QString)), this, SLOT(slotFolderChanged(QString)));
_timer.singleShot(5000, this, SLOT(slotEnd()));
}
void init()
{
_receivedNotifications.clear();
_requiredNotifications.clear();
_skipNotifications.clear();
}
void checkNotifications()
{
processAndWait();
QCOMPARE(_receivedNotifications, _requiredNotifications);
}
void testACreate() { // create a new file
QString file(_root + "/foo.txt");
QString cmd;
_requiredNotifications.insert(file);
cmd = QString("echo \"xyz\" > %1").arg(file);
qDebug() << "Command: " << cmd;
system(cmd.toLocal8Bit());
checkNotifications();
}
void testATouch() { // touch an existing file.
QString file(_root + "/a1/random.bin");
_requiredNotifications.insert(file);
#ifdef Q_OS_WIN
Utility::writeRandomFile(QString("%1/a1/random.bin").arg(_root));
#else
QString cmd;
cmd = QString("touch %1").arg(file);
qDebug() << "Command: " << cmd;
system(cmd.toLocal8Bit());
#endif
checkNotifications();
}
void testCreateADir() {
QString file(_root+"/a1/b1/new_dir");
_requiredNotifications.insert(file);
//_skipNotifications.insert(_root + "/a1/b1/new_dir");
QDir dir;
dir.mkdir(file);
QVERIFY(QFile::exists(file));
checkNotifications();
}
void testRemoveADir() {
QString file(_root+"/a1/b3/c3");
_requiredNotifications.insert(file);
QDir dir;
QVERIFY(dir.rmdir(file));
checkNotifications();
}
void testRemoveAFile() {
QString file(_root+"/a1/b2/todelete.bin");
_requiredNotifications.insert(file);
QVERIFY(QFile::exists(file));
QFile::remove(file);
QVERIFY(!QFile::exists(file));
checkNotifications();
}
void testRenameAFile() {
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));
checkNotifications();
}
void testMoveAFile() {
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));
checkNotifications();
}
void cleanupTestCase() {
if( _root.startsWith(QDir::tempPath() )) {
system( QString("rm -rf %1").arg(_root).toLocal8Bit() );
}
delete _watcher;
}
};
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
// Qt4 does not have QTEST_GUILESS_MAIN, so we simulate it.
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
TestFolderWatcher tc;
return QTest::qExec(&tc, argc, argv);
}
#else
QTEST_GUILESS_MAIN(TestFolderWatcher)
#endif
#include "testfolderwatcher.moc"
|