-
Notifications
You must be signed in to change notification settings - Fork 9
/
qmlunittestrunner.cpp
82 lines (65 loc) · 2.21 KB
/
qmlunittestrunner.cpp
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
#include "qmlunittestrunner.h"
#include "qmllogger.h"
#include <QDeclarativeContext>
#include <QDebug>
#include <QDir>
QmlUnitTestRunner::QmlUnitTestRunner(QApplication *app) :
QObject(app)
{
this->app = app;
QStringList args = app->arguments();
// Skips executable
args.removeAt(0);
if (args.count() == 0) {
qDebug() << "No arguments passed, failing back to qmlunit Test Suite (" << (app->applicationDirPath() + "/test") << ")\n";
args = QStringList(app->applicationDirPath() + "/test");
}
QStringListIterator i(args);
while(i.hasNext())
findTests(i.next());
i = QStringListIterator(tests);
qDebug() << "Tests files found:";
while(i.hasNext()) {
QString currentArg = i.next();
qDebug() << "\t" << currentArg;
}
}
void QmlUnitTestRunner::setup(){
QmlLogger *logger = new QmlLogger(app->applicationDirPath(), this);
QDeclarativeEngine *engine = logger->engine();
engine->setOfflineStoragePath(QDir::currentPath() + "/storage");
engine->addImportPath(QDir::currentPath() + "/QmlUnit");
engine->rootContext()->setContextProperty("testsInput", tests);
engine->rootContext()->setContextProperty("currentPath", QDir::currentPath());
logger->setup();
}
int QmlUnitTestRunner::exec() {
setup();
return app->exec();
}
void QmlUnitTestRunner::findTests(QString path) {
if (isTest(path)) {
tests << QDir(path).absolutePath();
return;
}
QStringList filters; filters << "*";
QDir dir = QDir(QDir(path).absolutePath());
QListIterator<QFileInfo> files(dir.entryInfoList(filters, QDir::AllEntries | QDir::NoDotAndDotDot));
while(files.hasNext()) {
QFileInfo file = files.next();
if (file.fileName() == "." || file.fileName() == "..") continue;
if (isTest(file))
tests << file.absoluteFilePath();
else if (isDir(file))
findTests(file.absoluteFilePath());
}
}
bool QmlUnitTestRunner::isTest(QFileInfo file){
return isTest(file.fileName());
}
bool QmlUnitTestRunner::isTest(QString filePath){
return filePath.endsWith("Test.qml");
}
bool QmlUnitTestRunner::isDir(QFileInfo file){
return QDir(file.absoluteFilePath()).exists();
}