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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
|
#include <qtest.h>
#include <QmlView>
#include <QApplication>
#include <QLibraryInfo>
#include <QDir>
#include <QDebug>
#include <QProcess>
#include <QFile>
static QString testdir;
class tst_visual : public QObject
{
Q_OBJECT
public:
tst_visual();
static QString toTestScript(const QString &);
static QString viewer();
private slots:
void visual_data();
void visual();
private:
QString qmlviewer;
QStringList findQmlFiles(const QDir &d);
};
tst_visual::tst_visual()
{
qmlviewer = viewer();
}
QString tst_visual::viewer()
{
QString binaries = QLibraryInfo::location(QLibraryInfo::BinariesPath);
QString qmlviewer;
#if defined(Q_WS_MAC)
qmlviewer = QDir(binaries).absoluteFilePath("qmlviewer.app/Contents/MacOS/qmlviewer");
#elif defined(Q_WS_WIN)
qmlviewer = QDir(binaries).absoluteFilePath("qmlviewer.exe");
#else
qmlviewer = QDir(binaries).absoluteFilePath("qmlviewer");
#endif
return qmlviewer;
}
void tst_visual::visual_data()
{
QTest::addColumn<QString>("file");
QTest::addColumn<QString>("testdata");
QStringList files;
files << findQmlFiles(QDir(QT_TEST_SOURCE_DIR));
foreach (const QString &file, files) {
QString testdata = toTestScript(file);
if (testdata.isEmpty())
continue;
QTest::newRow(file.toLatin1().constData()) << file << testdata;
}
}
void tst_visual::visual()
{
QFETCH(QString, file);
QFETCH(QString, testdata);
QStringList arguments;
arguments << "-script" << testdata
<< "-scriptopts" << "play,testimages,exitoncomplete,exitonfailure"
<< file;
QProcess p;
p.start(qmlviewer, arguments);
QVERIFY(p.waitForFinished());
QCOMPARE(p.exitStatus(), QProcess::NormalExit);
QCOMPARE(p.exitCode(), 0);
}
QString tst_visual::toTestScript(const QString &file)
{
if (!file.endsWith(".qml"))
return QString();
int index = file.lastIndexOf(QDir::separator());
if (index == -1)
return QString();
QString testdata = file.left(index + 1) +
QString("data") + QDir::separator() +
file.mid(index + 1, file.length() - index - 5);
return testdata;
}
QStringList tst_visual::findQmlFiles(const QDir &d)
{
QStringList rv;
QStringList files = d.entryList(QStringList() << QLatin1String("*.qml"),
QDir::Files);
foreach (const QString &file, files) {
if (file.at(0).isLower()) {
rv << d.absoluteFilePath(file);
}
}
QStringList dirs = d.entryList(QDir::Dirs | QDir::NoDotAndDotDot |
QDir::NoSymLinks);
foreach (const QString &dir, dirs) {
if (dir == "data")
continue;
QDir sub = d;
sub.cd(dir);
rv << findQmlFiles(sub);
}
return rv;
}
enum Mode { Record, Play, TestVisuals, UpdateVisuals, Test };
void action(Mode mode, const QString &file)
{
Q_ASSERT(mode != Test);
QString testdata = tst_visual::toTestScript(file);
if (Record == mode) {
QStringList arguments;
arguments << "-script" << testdata
<< "-scriptopts" << "record,saveonexit"
<< file;
QProcess p;
p.setProcessChannelMode(QProcess::ForwardedChannels);
p.start(tst_visual::viewer(), arguments);
p.waitForFinished();
} else if (Play == mode) {
QStringList arguments;
arguments << "-script" << testdata
<< "-scriptopts" << "play,testimages,exitoncomplete"
<< file;
QProcess p;
p.setProcessChannelMode(QProcess::ForwardedChannels);
p.start(tst_visual::viewer(), arguments);
p.waitForFinished();
} else if (TestVisuals == mode) {
QStringList arguments;
arguments << "-script" << testdata
<< "-scriptopts" << "play"
<< file;
QProcess p;
p.setProcessChannelMode(QProcess::ForwardedChannels);
p.start(tst_visual::viewer(), arguments);
p.waitForFinished();
} else if (UpdateVisuals == mode) {
QStringList arguments;
arguments << "-script" << testdata
<< "-scriptopts" << "play,record,exitoncomplete,saveonexit"
<< file;
QProcess p;
p.setProcessChannelMode(QProcess::ForwardedChannels);
p.start(tst_visual::viewer(), arguments);
p.waitForFinished();
}
}
void usage()
{
fprintf(stderr, "\n");
fprintf(stderr, "QML related options\n");
fprintf(stderr, " -record file : record new test data for file\n");
fprintf(stderr, " -play file : playback test data for file, printing errors\n");
fprintf(stderr, " -testvisuals file : playback test data for file, without errors\n");
fprintf(stderr, " -updatevisuals file : playback test data for file, accept new visuals for file\n");
}
int main(int argc, char **argv)
{
QApplication app(argc, argv);
Mode mode = Test;
QString file;
bool showHelp = false;
int newArgc = 1;
char **newArgv = new char*[argc];
newArgv[0] = argv[0];
for (int ii = 1; ii < argc; ++ii) {
QString arg(argv[ii]);
if (arg == "-play" && (ii + 1) < argc) {
mode = Play;
file = argv[++ii];
} else if (arg == "-record" && (ii + 1) < argc) {
mode = Record;
file = argv[++ii];
} else if (arg == "-testvisuals" && (ii + 1) < argc) {
mode = TestVisuals;
file = argv[++ii];
} else if (arg == "-updatevisuals" && (ii + 1) < argc) {
mode = UpdateVisuals;
file = argv[++ii];
} else {
newArgv[newArgc++] = argv[ii];
}
if (arg == "-help") {
atexit(usage);
showHelp = true;
}
}
if (mode == Test || showHelp) {
tst_visual tc;
return QTest::qExec(&tc, newArgc, newArgv);
} else {
if (!file.endsWith(QLatin1String(".qml"))) {
qWarning() << "Error: Invalid file name (must end in .qml)";
return -1;
}
QDir d = QDir::current();
QString absFile = d.absoluteFilePath(file);
if (!QFile::exists(absFile)) {
qWarning() << "Error: File does not exist";
return -1;
}
action(mode, absFile);
return 0;
}
}
#include "tst_visual.moc"
|