1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the tools applications of the Qt Toolkit.
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
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.
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.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
40 ****************************************************************************/
42 #include <qdeclarativetester.h>
43 #include <QtDeclarative/qdeclarativeview.h>
44 #include <QtDeclarative/QDeclarativeComponent>
45 #include <QtGui/QGraphicsObject>
46 #include <QtGui/QApplication>
47 #include <QtCore/QDebug>
48 #include <QtCore/QFile>
49 #include <QtCore/QDir>
50 #include <QtCore/QCryptographicHash>
52 #ifndef NO_PRIVATE_HEADERS
53 #include <private/qabstractanimation_p.h>
54 #include <private/qdeclarativeitem_p.h>
59 extern Q_GUI_EXPORT bool qt_applefontsmoothing_enabled;
61 QDeclarativeTester::QDeclarativeTester(const QString &script, QDeclarativeViewer::ScriptOptions opts,
62 QDeclarativeView *parent)
63 : QAbstractAnimation(parent), m_script(script), m_view(parent), filterEvents(true), options(opts),
64 testscript(0), hasCompleted(false), hasFailed(false)
66 parent->viewport()->installEventFilter(this);
67 parent->installEventFilter(this);
68 #ifndef NO_PRIVATE_HEADERS
69 QUnifiedTimer::instance()->setConsistentTiming(true);
72 //Font antialiasing makes tests system-specific, so disable it
73 QFont noAA = QApplication::font();
74 noAA.setStyleStrategy(QFont::NoAntialias);
75 QApplication::setFont(noAA);
77 if (options & QDeclarativeViewer::Play)
82 QDeclarativeTester::~QDeclarativeTester()
85 options & QDeclarativeViewer::Record &&
86 options & QDeclarativeViewer::SaveOnExit)
90 int QDeclarativeTester::duration() const
95 void QDeclarativeTester::addMouseEvent(Destination dest, QMouseEvent *me)
102 void QDeclarativeTester::addKeyEvent(Destination dest, QKeyEvent *ke)
105 e.destination = dest;
109 bool QDeclarativeTester::eventFilter(QObject *o, QEvent *e)
114 Destination destination;
117 } else if (o == m_view->viewport()) {
118 destination = ViewPort;
124 case QEvent::KeyPress:
125 case QEvent::KeyRelease:
126 addKeyEvent(destination, (QKeyEvent *)e);
128 case QEvent::MouseButtonPress:
129 case QEvent::MouseButtonRelease:
130 case QEvent::MouseMove:
131 case QEvent::MouseButtonDblClick:
132 addMouseEvent(destination, (QMouseEvent *)e);
140 void QDeclarativeTester::executefailure()
144 if (options & QDeclarativeViewer::ExitOnFailure)
148 void QDeclarativeTester::imagefailure()
152 if (options & QDeclarativeViewer::ExitOnFailure){
154 exit(hasFailed?-1:0);
158 void QDeclarativeTester::testSkip()
160 if (options & QDeclarativeViewer::TestSkipProperty){
161 QString e = m_view->rootObject()->property("skip").toString();
164 qWarning() << "Test failed, but skipping it: " << e;
166 qWarning() << "Test skipped: " << e;
173 void QDeclarativeTester::complete()
175 if ((options & QDeclarativeViewer::TestErrorProperty) && !hasFailed) {
176 QString e = m_view->rootObject()->property("error").toString();
178 qWarning() << "Test failed:" << e;
185 if (options & QDeclarativeViewer::ExitOnComplete)
186 QApplication::exit(hasFailed?-1:0);
192 if (options & QDeclarativeViewer::Play)
193 qWarning("Script playback complete");
196 void QDeclarativeTester::run()
198 QDeclarativeComponent c(m_view->engine(), m_script + QLatin1String(".qml"));
200 testscript = qobject_cast<QDeclarativeVisualTest *>(c.create());
201 if (testscript) testscript->setParent(this);
202 else { executefailure(); exit(-1); }
206 void QDeclarativeTester::save()
208 QString filename = m_script + QLatin1String(".qml");
209 QFileInfo filenameInfo(filename);
210 QDir saveDir = filenameInfo.absoluteDir();
213 QFile file(filename);
214 file.open(QIODevice::WriteOnly);
215 QTextStream ts(&file);
217 ts << "import Qt.VisualTest 4.7\n\n";
218 ts << "VisualTest {\n";
221 QList<KeyEvent> keyevents = m_savedKeyEvents;
222 QList<MouseEvent> mouseevents = m_savedMouseEvents;
223 for (int ii = 0; ii < m_savedFrameEvents.count(); ++ii) {
224 const FrameEvent &fe = m_savedFrameEvents.at(ii);
226 ts << " msec: " << fe.msec << "\n";
227 if (!fe.hash.isEmpty()) {
228 ts << " hash: \"" << fe.hash.toHex() << "\"\n";
229 } else if (!fe.image.isNull()) {
230 QString filename = filenameInfo.baseName() + "." + QString::number(imgCount) + ".png";
231 fe.image.save(m_script + "." + QString::number(imgCount) + ".png");
233 ts << " image: \"" << filename << "\"\n";
237 while (!mouseevents.isEmpty() &&
238 mouseevents.first().msec == fe.msec) {
239 MouseEvent me = mouseevents.takeFirst();
242 ts << " type: " << me.type << "\n";
243 ts << " button: " << me.button << "\n";
244 ts << " buttons: " << me.buttons << "\n";
245 ts << " x: " << me.pos.x() << "; y: " << me.pos.y() << "\n";
246 ts << " modifiers: " << me.modifiers << "\n";
247 if (me.destination == ViewPort)
248 ts << " sendToViewport: true\n";
252 while (!keyevents.isEmpty() &&
253 keyevents.first().msec == fe.msec) {
254 KeyEvent ke = keyevents.takeFirst();
257 ts << " type: " << ke.type << "\n";
258 ts << " key: " << ke.key << "\n";
259 ts << " modifiers: " << ke.modifiers << "\n";
260 ts << " text: \"" << ke.text.toUtf8().toHex() << "\"\n";
261 ts << " autorep: " << (ke.autorep?"true":"false") << "\n";
262 ts << " count: " << ke.count << "\n";
263 if (ke.destination == ViewPort)
264 ts << " sendToViewport: true\n";
273 void QDeclarativeTester::updateCurrentTime(int msec)
275 #ifndef NO_PRIVATE_HEADERS
276 QDeclarativeItemPrivate::setConsistentTime(msec);
278 if (!testscript && msec > 16 && options & QDeclarativeViewer::Snapshot)
281 QImage img(m_view->width(), m_view->height(), QImage::Format_RGB32);
283 if (options & QDeclarativeViewer::TestImages) {
284 img.fill(qRgb(255,255,255));
287 bool oldSmooth = qt_applefontsmoothing_enabled;
288 qt_applefontsmoothing_enabled = false;
292 qt_applefontsmoothing_enabled = oldSmooth;
298 bool snapshot = msec == 16 && (options & QDeclarativeViewer::Snapshot
299 || (testscript && testscript->count() == 2));
303 if (msec == 0 || !(options & QDeclarativeViewer::TestImages)) {
304 // Skip first frame, skip if not doing images
305 } else if (0 == ((m_savedFrameEvents.count()-1) % 60) || snapshot) {
308 QCryptographicHash hash(QCryptographicHash::Md5);
309 hash.addData((const char *)img.bits(), img.bytesPerLine() * img.height());
310 fe.hash = hash.result();
312 m_savedFrameEvents.append(fe);
314 // Deliver mouse events
315 filterEvents = false;
318 for (int ii = 0; ii < m_mouseEvents.count(); ++ii) {
319 MouseEvent &me = m_mouseEvents[ii];
321 QMouseEvent event(me.type, me.pos, me.button, me.buttons, me.modifiers);
323 if (me.destination == View) {
324 QCoreApplication::sendEvent(m_view, &event);
326 QCoreApplication::sendEvent(m_view->viewport(), &event);
330 for (int ii = 0; ii < m_keyEvents.count(); ++ii) {
331 KeyEvent &ke = m_keyEvents[ii];
333 QKeyEvent event(ke.type, ke.key, ke.modifiers, ke.text, ke.autorep, ke.count);
335 if (ke.destination == View) {
336 QCoreApplication::sendEvent(m_view, &event);
338 QCoreApplication::sendEvent(m_view->viewport(), &event);
341 m_savedMouseEvents.append(m_mouseEvents);
342 m_savedKeyEvents.append(m_keyEvents);
345 m_mouseEvents.clear();
348 // Advance test script
349 while (testscript && testscript->count() > testscriptidx) {
351 QObject *event = testscript->event(testscriptidx);
353 if (QDeclarativeVisualTestFrame *frame = qobject_cast<QDeclarativeVisualTestFrame *>(event)) {
354 if (frame->msec() < msec) {
355 if (options & QDeclarativeViewer::TestImages && !(options & QDeclarativeViewer::Record)) {
356 qWarning() << "QDeclarativeTester(" << m_script << "): Extra frame. Seen:"
357 << msec << "Expected:" << frame->msec();
360 } else if (frame->msec() == msec) {
361 if (!frame->hash().isEmpty() && frame->hash().toUtf8() != fe.hash.toHex()) {
362 if (options & QDeclarativeViewer::TestImages && !(options & QDeclarativeViewer::Record)) {
363 qWarning() << "QDeclarativeTester(" << m_script << "): Mismatched frame hash at" << msec
364 << ". Seen:" << fe.hash.toHex()
365 << "Expected:" << frame->hash().toUtf8();
369 } else if (frame->msec() > msec) {
373 if (options & QDeclarativeViewer::TestImages && !(options & QDeclarativeViewer::Record) && !frame->image().isEmpty()) {
374 QImage goodImage(frame->image().toLocalFile());
375 if (frame->msec() == 16 && goodImage.size() != img.size()){
376 //Also an image mismatch, but this warning is more informative. Only checked at start though.
377 qWarning() << "QDeclarativeTester(" << m_script << "): Size mismatch. This test must be run at " << goodImage.size();
380 if (goodImage != img) {
381 QString reject(frame->image().toLocalFile() + ".reject.png");
382 qWarning() << "QDeclarativeTester(" << m_script << "): Image mismatch. Reject saved to:"
385 bool doDiff = (goodImage.size() == img.size());
387 QImage diffimg(m_view->width(), m_view->height(), QImage::Format_RGB32);
388 diffimg.fill(qRgb(255,255,255));
389 QPainter p(&diffimg);
391 for (int x = 0; x < img.width(); ++x) {
392 for (int y = 0; y < img.height(); ++y) {
393 if (goodImage.pixel(x,y) != img.pixel(x,y)) {
399 QString diff(frame->image().toLocalFile() + ".diff.png");
401 qWarning().nospace() << " Diff (" << diffCount << " pixels differed) saved to: " << diff;
406 } else if (QDeclarativeVisualTestMouse *mouse = qobject_cast<QDeclarativeVisualTestMouse *>(event)) {
407 QPoint pos(mouse->x(), mouse->y());
408 QPoint globalPos = m_view->mapToGlobal(QPoint(0, 0)) + pos;
409 QMouseEvent event((QEvent::Type)mouse->type(), pos, globalPos, (Qt::MouseButton)mouse->button(), (Qt::MouseButtons)mouse->buttons(), (Qt::KeyboardModifiers)mouse->modifiers());
411 MouseEvent me(&event);
413 if (!mouse->sendToViewport()) {
414 QCoreApplication::sendEvent(m_view, &event);
415 me.destination = View;
417 QCoreApplication::sendEvent(m_view->viewport(), &event);
418 me.destination = ViewPort;
420 m_savedMouseEvents.append(me);
421 } else if (QDeclarativeVisualTestKey *key = qobject_cast<QDeclarativeVisualTestKey *>(event)) {
423 QKeyEvent event((QEvent::Type)key->type(), key->key(), (Qt::KeyboardModifiers)key->modifiers(), QString::fromUtf8(QByteArray::fromHex(key->text().toUtf8())), key->autorep(), key->count());
427 if (!key->sendToViewport()) {
428 QCoreApplication::sendEvent(m_view, &event);
429 ke.destination = View;
431 QCoreApplication::sendEvent(m_view->viewport(), &event);
432 ke.destination = ViewPort;
434 m_savedKeyEvents.append(ke);
441 if (testscript && testscript->count() <= testscriptidx) {
442 //if (msec == 16) //for a snapshot, leave it up long enough to see
448 void QDeclarativeTester::registerTypes()
450 qmlRegisterType<QDeclarativeVisualTest>("Qt.VisualTest", 4,7, "VisualTest");
451 qmlRegisterType<QDeclarativeVisualTestFrame>("Qt.VisualTest", 4,7, "Frame");
452 qmlRegisterType<QDeclarativeVisualTestMouse>("Qt.VisualTest", 4,7, "Mouse");
453 qmlRegisterType<QDeclarativeVisualTestKey>("Qt.VisualTest", 4,7, "Key");