|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the tools applications of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include <qdeclarativetester.h> |
|
43 #include <QDebug> |
|
44 #include <QApplication> |
|
45 #include <qdeclarativeview.h> |
|
46 #include <QFile> |
|
47 #include <QDeclarativeComponent> |
|
48 #include <QDir> |
|
49 #include <QCryptographicHash> |
|
50 #include <private/qabstractanimation_p.h> |
|
51 #include <private/qdeclarativeitem_p.h> |
|
52 |
|
53 QT_BEGIN_NAMESPACE |
|
54 |
|
55 |
|
56 QDeclarativeTester::QDeclarativeTester(const QString &script, QDeclarativeViewer::ScriptOptions opts, |
|
57 QDeclarativeView *parent) |
|
58 : QAbstractAnimation(parent), m_script(script), m_view(parent), filterEvents(true), options(opts), |
|
59 testscript(0), hasCompleted(false), hasFailed(false) |
|
60 { |
|
61 parent->viewport()->installEventFilter(this); |
|
62 parent->installEventFilter(this); |
|
63 QUnifiedTimer::instance()->setConsistentTiming(true); |
|
64 if (options & QDeclarativeViewer::Play) |
|
65 this->run(); |
|
66 start(); |
|
67 } |
|
68 |
|
69 QDeclarativeTester::~QDeclarativeTester() |
|
70 { |
|
71 if (!hasFailed && |
|
72 options & QDeclarativeViewer::Record && |
|
73 options & QDeclarativeViewer::SaveOnExit) |
|
74 save(); |
|
75 } |
|
76 |
|
77 int QDeclarativeTester::duration() const |
|
78 { |
|
79 return -1; |
|
80 } |
|
81 |
|
82 void QDeclarativeTester::addMouseEvent(Destination dest, QMouseEvent *me) |
|
83 { |
|
84 MouseEvent e(me); |
|
85 e.destination = dest; |
|
86 m_mouseEvents << e; |
|
87 } |
|
88 |
|
89 void QDeclarativeTester::addKeyEvent(Destination dest, QKeyEvent *ke) |
|
90 { |
|
91 KeyEvent e(ke); |
|
92 e.destination = dest; |
|
93 m_keyEvents << e; |
|
94 } |
|
95 |
|
96 bool QDeclarativeTester::eventFilter(QObject *o, QEvent *e) |
|
97 { |
|
98 if (!filterEvents) |
|
99 return false; |
|
100 |
|
101 Destination destination; |
|
102 if (o == m_view) { |
|
103 destination = View; |
|
104 } else if (o == m_view->viewport()) { |
|
105 destination = ViewPort; |
|
106 } else { |
|
107 return false; |
|
108 } |
|
109 |
|
110 switch (e->type()) { |
|
111 case QEvent::KeyPress: |
|
112 case QEvent::KeyRelease: |
|
113 addKeyEvent(destination, (QKeyEvent *)e); |
|
114 return true; |
|
115 case QEvent::MouseButtonPress: |
|
116 case QEvent::MouseButtonRelease: |
|
117 case QEvent::MouseMove: |
|
118 case QEvent::MouseButtonDblClick: |
|
119 addMouseEvent(destination, (QMouseEvent *)e); |
|
120 return true; |
|
121 default: |
|
122 break; |
|
123 } |
|
124 return false; |
|
125 } |
|
126 |
|
127 void QDeclarativeTester::executefailure() |
|
128 { |
|
129 hasFailed = true; |
|
130 |
|
131 if (options & QDeclarativeViewer::ExitOnFailure) |
|
132 exit(-1); |
|
133 } |
|
134 |
|
135 void QDeclarativeTester::imagefailure() |
|
136 { |
|
137 hasFailed = true; |
|
138 |
|
139 if (options & QDeclarativeViewer::ExitOnFailure) |
|
140 exit(-1); |
|
141 } |
|
142 |
|
143 void QDeclarativeTester::complete() |
|
144 { |
|
145 if ((options & QDeclarativeViewer::TestErrorProperty) && !hasFailed) { |
|
146 QString e = m_view->rootObject()->property("error").toString(); |
|
147 if (!e.isEmpty()) { |
|
148 qWarning() << "Test failed:" << e; |
|
149 hasFailed = true; |
|
150 } |
|
151 } |
|
152 if (options & QDeclarativeViewer::ExitOnComplete) |
|
153 QApplication::exit(hasFailed?-1:0); |
|
154 |
|
155 if (hasCompleted) |
|
156 return; |
|
157 hasCompleted = true; |
|
158 |
|
159 if (options & QDeclarativeViewer::Play) |
|
160 qWarning("Script playback complete"); |
|
161 } |
|
162 |
|
163 void QDeclarativeTester::run() |
|
164 { |
|
165 QDeclarativeComponent c(m_view->engine(), m_script + QLatin1String(".qml")); |
|
166 |
|
167 testscript = qobject_cast<QDeclarativeVisualTest *>(c.create()); |
|
168 if (testscript) testscript->setParent(this); |
|
169 else { executefailure(); exit(-1); } |
|
170 testscriptidx = 0; |
|
171 } |
|
172 |
|
173 void QDeclarativeTester::save() |
|
174 { |
|
175 QString filename = m_script + QLatin1String(".qml"); |
|
176 QFileInfo filenameInfo(filename); |
|
177 QDir saveDir = filenameInfo.absoluteDir(); |
|
178 saveDir.mkpath("."); |
|
179 |
|
180 QFile file(filename); |
|
181 file.open(QIODevice::WriteOnly); |
|
182 QTextStream ts(&file); |
|
183 |
|
184 ts << "import Qt.VisualTest 4.7\n\n"; |
|
185 ts << "VisualTest {\n"; |
|
186 |
|
187 int imgCount = 0; |
|
188 QList<KeyEvent> keyevents = m_savedKeyEvents; |
|
189 QList<MouseEvent> mouseevents = m_savedMouseEvents; |
|
190 for (int ii = 0; ii < m_savedFrameEvents.count(); ++ii) { |
|
191 const FrameEvent &fe = m_savedFrameEvents.at(ii); |
|
192 ts << " Frame {\n"; |
|
193 ts << " msec: " << fe.msec << "\n"; |
|
194 if (!fe.hash.isEmpty()) { |
|
195 ts << " hash: \"" << fe.hash.toHex() << "\"\n"; |
|
196 } else if (!fe.image.isNull()) { |
|
197 QString filename = filenameInfo.baseName() + "." + QString::number(imgCount) + ".png"; |
|
198 fe.image.save(m_script + "." + QString::number(imgCount) + ".png"); |
|
199 imgCount++; |
|
200 ts << " image: \"" << filename << "\"\n"; |
|
201 } |
|
202 ts << " }\n"; |
|
203 |
|
204 while (!mouseevents.isEmpty() && |
|
205 mouseevents.first().msec == fe.msec) { |
|
206 MouseEvent me = mouseevents.takeFirst(); |
|
207 |
|
208 ts << " Mouse {\n"; |
|
209 ts << " type: " << me.type << "\n"; |
|
210 ts << " button: " << me.button << "\n"; |
|
211 ts << " buttons: " << me.buttons << "\n"; |
|
212 ts << " x: " << me.pos.x() << "; y: " << me.pos.y() << "\n"; |
|
213 ts << " modifiers: " << me.modifiers << "\n"; |
|
214 if (me.destination == ViewPort) |
|
215 ts << " sendToViewport: true\n"; |
|
216 ts << " }\n"; |
|
217 } |
|
218 |
|
219 while (!keyevents.isEmpty() && |
|
220 keyevents.first().msec == fe.msec) { |
|
221 KeyEvent ke = keyevents.takeFirst(); |
|
222 |
|
223 ts << " Key {\n"; |
|
224 ts << " type: " << ke.type << "\n"; |
|
225 ts << " key: " << ke.key << "\n"; |
|
226 ts << " modifiers: " << ke.modifiers << "\n"; |
|
227 ts << " text: \"" << ke.text.toUtf8().toHex() << "\"\n"; |
|
228 ts << " autorep: " << (ke.autorep?"true":"false") << "\n"; |
|
229 ts << " count: " << ke.count << "\n"; |
|
230 if (ke.destination == ViewPort) |
|
231 ts << " sendToViewport: true\n"; |
|
232 ts << " }\n"; |
|
233 } |
|
234 } |
|
235 |
|
236 ts << "}\n"; |
|
237 file.close(); |
|
238 } |
|
239 |
|
240 void QDeclarativeTester::updateCurrentTime(int msec) |
|
241 { |
|
242 QDeclarativeItemPrivate::setConsistentTime(msec); |
|
243 if (!testscript && msec > 16 && options & QDeclarativeViewer::Snapshot) |
|
244 return; |
|
245 |
|
246 QImage img(m_view->width(), m_view->height(), QImage::Format_RGB32); |
|
247 |
|
248 if (options & QDeclarativeViewer::TestImages) { |
|
249 img.fill(qRgb(255,255,255)); |
|
250 QPainter p(&img); |
|
251 m_view->render(&p); |
|
252 } |
|
253 |
|
254 bool snapshot = msec == 16 && (options & QDeclarativeViewer::Snapshot |
|
255 || (testscript && testscript->count() == 2)); |
|
256 |
|
257 FrameEvent fe; |
|
258 fe.msec = msec; |
|
259 if (msec == 0 || !(options & QDeclarativeViewer::TestImages)) { |
|
260 // Skip first frame, skip if not doing images |
|
261 } else if (0 == (m_savedFrameEvents.count() % 60) || snapshot) { |
|
262 fe.image = img; |
|
263 } else { |
|
264 QCryptographicHash hash(QCryptographicHash::Md5); |
|
265 hash.addData((const char *)img.bits(), img.bytesPerLine() * img.height()); |
|
266 fe.hash = hash.result(); |
|
267 } |
|
268 m_savedFrameEvents.append(fe); |
|
269 |
|
270 // Deliver mouse events |
|
271 filterEvents = false; |
|
272 |
|
273 if (!testscript) { |
|
274 for (int ii = 0; ii < m_mouseEvents.count(); ++ii) { |
|
275 MouseEvent &me = m_mouseEvents[ii]; |
|
276 me.msec = msec; |
|
277 QMouseEvent event(me.type, me.pos, me.button, me.buttons, me.modifiers); |
|
278 |
|
279 if (me.destination == View) { |
|
280 QCoreApplication::sendEvent(m_view, &event); |
|
281 } else { |
|
282 QCoreApplication::sendEvent(m_view->viewport(), &event); |
|
283 } |
|
284 } |
|
285 |
|
286 for (int ii = 0; ii < m_keyEvents.count(); ++ii) { |
|
287 KeyEvent &ke = m_keyEvents[ii]; |
|
288 ke.msec = msec; |
|
289 QKeyEvent event(ke.type, ke.key, ke.modifiers, ke.text, ke.autorep, ke.count); |
|
290 |
|
291 if (ke.destination == View) { |
|
292 QCoreApplication::sendEvent(m_view, &event); |
|
293 } else { |
|
294 QCoreApplication::sendEvent(m_view->viewport(), &event); |
|
295 } |
|
296 } |
|
297 m_savedMouseEvents.append(m_mouseEvents); |
|
298 m_savedKeyEvents.append(m_keyEvents); |
|
299 } |
|
300 |
|
301 m_mouseEvents.clear(); |
|
302 m_keyEvents.clear(); |
|
303 |
|
304 // Advance test script |
|
305 while (testscript && testscript->count() > testscriptidx) { |
|
306 |
|
307 QObject *event = testscript->event(testscriptidx); |
|
308 |
|
309 if (QDeclarativeVisualTestFrame *frame = qobject_cast<QDeclarativeVisualTestFrame *>(event)) { |
|
310 if (frame->msec() < msec) { |
|
311 if (options & QDeclarativeViewer::TestImages && !(options & QDeclarativeViewer::Record)) { |
|
312 qWarning() << "QDeclarativeTester: Extra frame. Seen:" |
|
313 << msec << "Expected:" << frame->msec(); |
|
314 imagefailure(); |
|
315 } |
|
316 } else if (frame->msec() == msec) { |
|
317 if (!frame->hash().isEmpty() && frame->hash().toUtf8() != fe.hash.toHex()) { |
|
318 if (options & QDeclarativeViewer::TestImages && !(options & QDeclarativeViewer::Record)) { |
|
319 qWarning() << "QDeclarativeTester: Mismatched frame hash at" << msec |
|
320 << ". Seen:" << fe.hash.toHex() |
|
321 << "Expected:" << frame->hash().toUtf8(); |
|
322 imagefailure(); |
|
323 } |
|
324 } |
|
325 } else if (frame->msec() > msec) { |
|
326 break; |
|
327 } |
|
328 |
|
329 if (options & QDeclarativeViewer::TestImages && !(options & QDeclarativeViewer::Record) && !frame->image().isEmpty()) { |
|
330 QImage goodImage(frame->image().toLocalFile()); |
|
331 if (goodImage != img) { |
|
332 QString reject(frame->image().toLocalFile() + ".reject.png"); |
|
333 qWarning() << "QDeclarativeTester: Image mismatch. Reject saved to:" |
|
334 << reject; |
|
335 img.save(reject); |
|
336 bool doDiff = (goodImage.size() == img.size()); |
|
337 if (doDiff) { |
|
338 QImage diffimg(m_view->width(), m_view->height(), QImage::Format_RGB32); |
|
339 diffimg.fill(qRgb(255,255,255)); |
|
340 QPainter p(&diffimg); |
|
341 int diffCount = 0; |
|
342 for (int x = 0; x < img.width(); ++x) { |
|
343 for (int y = 0; y < img.height(); ++y) { |
|
344 if (goodImage.pixel(x,y) != img.pixel(x,y)) { |
|
345 ++diffCount; |
|
346 p.drawPoint(x,y); |
|
347 } |
|
348 } |
|
349 } |
|
350 QString diff(frame->image().toLocalFile() + ".diff.png"); |
|
351 diffimg.save(diff); |
|
352 qWarning().nospace() << " Diff (" << diffCount << " pixels differed) saved to: " << diff; |
|
353 } |
|
354 imagefailure(); |
|
355 } |
|
356 } |
|
357 } else if (QDeclarativeVisualTestMouse *mouse = qobject_cast<QDeclarativeVisualTestMouse *>(event)) { |
|
358 QPoint pos(mouse->x(), mouse->y()); |
|
359 QPoint globalPos = m_view->mapToGlobal(QPoint(0, 0)) + pos; |
|
360 QMouseEvent event((QEvent::Type)mouse->type(), pos, globalPos, (Qt::MouseButton)mouse->button(), (Qt::MouseButtons)mouse->buttons(), (Qt::KeyboardModifiers)mouse->modifiers()); |
|
361 |
|
362 MouseEvent me(&event); |
|
363 me.msec = msec; |
|
364 if (!mouse->sendToViewport()) { |
|
365 QCoreApplication::sendEvent(m_view, &event); |
|
366 me.destination = View; |
|
367 } else { |
|
368 QCoreApplication::sendEvent(m_view->viewport(), &event); |
|
369 me.destination = ViewPort; |
|
370 } |
|
371 m_savedMouseEvents.append(me); |
|
372 } else if (QDeclarativeVisualTestKey *key = qobject_cast<QDeclarativeVisualTestKey *>(event)) { |
|
373 |
|
374 QKeyEvent event((QEvent::Type)key->type(), key->key(), (Qt::KeyboardModifiers)key->modifiers(), QString::fromUtf8(QByteArray::fromHex(key->text().toUtf8())), key->autorep(), key->count()); |
|
375 |
|
376 KeyEvent ke(&event); |
|
377 ke.msec = msec; |
|
378 if (!key->sendToViewport()) { |
|
379 QCoreApplication::sendEvent(m_view, &event); |
|
380 ke.destination = View; |
|
381 } else { |
|
382 QCoreApplication::sendEvent(m_view->viewport(), &event); |
|
383 ke.destination = ViewPort; |
|
384 } |
|
385 m_savedKeyEvents.append(ke); |
|
386 } |
|
387 testscriptidx++; |
|
388 } |
|
389 |
|
390 filterEvents = true; |
|
391 |
|
392 if (testscript && testscript->count() <= testscriptidx) { |
|
393 //if (msec == 16) //for a snapshot, leave it up long enough to see |
|
394 // (void)::sleep(1); |
|
395 complete(); |
|
396 } |
|
397 } |
|
398 |
|
399 void QDeclarativeTester::registerTypes() |
|
400 { |
|
401 qmlRegisterType<QDeclarativeVisualTest>("Qt.VisualTest", 4,7, "VisualTest"); |
|
402 qmlRegisterType<QDeclarativeVisualTestFrame>("Qt.VisualTest", 4,7, "Frame"); |
|
403 qmlRegisterType<QDeclarativeVisualTestMouse>("Qt.VisualTest", 4,7, "Mouse"); |
|
404 qmlRegisterType<QDeclarativeVisualTestKey>("Qt.VisualTest", 4,7, "Key"); |
|
405 } |
|
406 |
|
407 QT_END_NAMESPACE |