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 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include <qdeclarativetester.h>
44 #include <QApplication>
45 #include <QtQuick1/qdeclarativeview.h>
47 #include <QDeclarativeComponent>
49 #include <QCryptographicHash>
50 #include <private/qabstractanimation_p.h>
51 #include <QGraphicsObject>
52 #include <QtQuick1/private/qdeclarativeitem_p.h>
56 extern Q_GUI_EXPORT bool qt_applefontsmoothing_enabled;
58 QDeclarativeTester::QDeclarativeTester(const QString &script, QDeclarativeViewer::ScriptOptions opts,
59 QDeclarativeView *parent)
60 : QAbstractAnimation(parent), m_script(script), m_view(parent), filterEvents(true), options(opts),
61 testscript(0), hasCompleted(false), hasFailed(false)
63 parent->viewport()->installEventFilter(this);
64 parent->installEventFilter(this);
65 QUnifiedTimer::instance()->setConsistentTiming(true);
67 //Font antialiasing makes tests system-specific, so disable it
68 QFont noAA = QApplication::font();
69 noAA.setStyleStrategy(QFont::NoAntialias);
70 QApplication::setFont(noAA);
72 if (options & QDeclarativeViewer::Play)
77 QDeclarativeTester::~QDeclarativeTester()
80 options & QDeclarativeViewer::Record &&
81 options & QDeclarativeViewer::SaveOnExit)
85 int QDeclarativeTester::duration() const
90 void QDeclarativeTester::addMouseEvent(Destination dest, QMouseEvent *me)
97 void QDeclarativeTester::addKeyEvent(Destination dest, QKeyEvent *ke)
100 e.destination = dest;
104 bool QDeclarativeTester::eventFilter(QObject *o, QEvent *e)
109 Destination destination;
112 } else if (o == m_view->viewport()) {
113 destination = ViewPort;
119 case QEvent::KeyPress:
120 case QEvent::KeyRelease:
121 addKeyEvent(destination, (QKeyEvent *)e);
123 case QEvent::MouseButtonPress:
124 case QEvent::MouseButtonRelease:
125 case QEvent::MouseMove:
126 case QEvent::MouseButtonDblClick:
127 addMouseEvent(destination, (QMouseEvent *)e);
135 void QDeclarativeTester::executefailure()
139 if (options & QDeclarativeViewer::ExitOnFailure)
143 void QDeclarativeTester::imagefailure()
147 if (options & QDeclarativeViewer::ExitOnFailure){
149 exit(hasFailed?-1:0);
153 void QDeclarativeTester::testSkip()
155 if (options & QDeclarativeViewer::TestSkipProperty){
156 QString e = m_view->rootObject()->property("skip").toString();
159 qWarning() << "Test failed, but skipping it: " << e;
161 qWarning() << "Test skipped: " << e;
168 void QDeclarativeTester::complete()
170 if ((options & QDeclarativeViewer::TestErrorProperty) && !hasFailed) {
171 QString e = m_view->rootObject()->property("error").toString();
173 qWarning() << "Test failed:" << e;
180 if (options & QDeclarativeViewer::ExitOnComplete)
181 QApplication::exit(hasFailed?-1:0);
187 if (options & QDeclarativeViewer::Play)
188 qWarning("Script playback complete");
191 void QDeclarativeTester::run()
193 QDeclarativeComponent c(m_view->engine(), m_script + QLatin1String(".qml"));
195 testscript = qobject_cast<QDeclarativeVisualTest *>(c.create());
196 if (testscript) testscript->setParent(this);
197 else { executefailure(); exit(-1); }
201 void QDeclarativeTester::save()
203 QString filename = m_script + QLatin1String(".qml");
204 QFileInfo filenameInfo(filename);
205 QDir saveDir = filenameInfo.absoluteDir();
206 saveDir.mkpath(QLatin1String("."));
208 QFile file(filename);
209 file.open(QIODevice::WriteOnly);
210 QTextStream ts(&file);
212 ts << "import Qt.VisualTest 4.7\n\n";
213 ts << "VisualTest {\n";
216 QList<KeyEvent> keyevents = m_savedKeyEvents;
217 QList<MouseEvent> mouseevents = m_savedMouseEvents;
218 for (int ii = 0; ii < m_savedFrameEvents.count(); ++ii) {
219 const FrameEvent &fe = m_savedFrameEvents.at(ii);
221 ts << " msec: " << fe.msec << "\n";
222 if (!fe.hash.isEmpty()) {
223 ts << " hash: \"" << fe.hash.toHex() << "\"\n";
224 } else if (!fe.image.isNull()) {
225 QString filename = filenameInfo.baseName() + QLatin1String(".") + QString::number(imgCount) + QLatin1String(".png");
226 fe.image.save(m_script + QLatin1String(".") + QString::number(imgCount) + QLatin1String(".png"));
228 ts << " image: \"" << filename << "\"\n";
232 while (!mouseevents.isEmpty() &&
233 mouseevents.first().msec == fe.msec) {
234 MouseEvent me = mouseevents.takeFirst();
237 ts << " type: " << me.type << "\n";
238 ts << " button: " << me.button << "\n";
239 ts << " buttons: " << me.buttons << "\n";
240 ts << " x: " << me.pos.x() << "; y: " << me.pos.y() << "\n";
241 ts << " modifiers: " << me.modifiers << "\n";
242 if (me.destination == ViewPort)
243 ts << " sendToViewport: true\n";
247 while (!keyevents.isEmpty() &&
248 keyevents.first().msec == fe.msec) {
249 KeyEvent ke = keyevents.takeFirst();
252 ts << " type: " << ke.type << "\n";
253 ts << " key: " << ke.key << "\n";
254 ts << " modifiers: " << ke.modifiers << "\n";
255 ts << " text: \"" << ke.text.toUtf8().toHex() << "\"\n";
256 ts << " autorep: " << (ke.autorep?"true":"false") << "\n";
257 ts << " count: " << ke.count << "\n";
258 if (ke.destination == ViewPort)
259 ts << " sendToViewport: true\n";
268 void QDeclarativeTester::updateCurrentTime(int msec)
270 QDeclarativeItemPrivate::setConsistentTime(msec);
272 if (!testscript && msec > 16 && options & QDeclarativeViewer::Snapshot)
275 QImage img(m_view->width(), m_view->height(), QImage::Format_RGB32);
277 if (options & QDeclarativeViewer::TestImages) {
278 img.fill(qRgb(255,255,255));
280 /*#ifdef Q_OS_MAC //Compile failure on Cocoa x64 QTBUG-22304
281 bool oldSmooth = qt_applefontsmoothing_enabled;
282 qt_applefontsmoothing_enabled = false;
285 /*#ifdef Q_OS_MAC //Compile failure on Cocoa x64 QTBUG-22304
286 qt_applefontsmoothing_enabled = oldSmooth;
292 bool snapshot = msec == 16 && (options & QDeclarativeViewer::Snapshot
293 || (testscript && testscript->count() == 2));
297 if (msec == 0 || !(options & QDeclarativeViewer::TestImages)) {
298 // Skip first frame, skip if not doing images
299 } else if (0 == ((m_savedFrameEvents.count()-1) % 60) || snapshot) {
302 QCryptographicHash hash(QCryptographicHash::Md5);
303 hash.addData((const char *)img.bits(), img.bytesPerLine() * img.height());
304 fe.hash = hash.result();
306 m_savedFrameEvents.append(fe);
308 // Deliver mouse events
309 filterEvents = false;
312 for (int ii = 0; ii < m_mouseEvents.count(); ++ii) {
313 MouseEvent &me = m_mouseEvents[ii];
315 QMouseEvent event(me.type, me.pos, me.button, me.buttons, me.modifiers);
317 if (me.destination == View) {
318 QCoreApplication::sendEvent(m_view, &event);
320 QCoreApplication::sendEvent(m_view->viewport(), &event);
324 for (int ii = 0; ii < m_keyEvents.count(); ++ii) {
325 KeyEvent &ke = m_keyEvents[ii];
327 QKeyEvent event(ke.type, ke.key, ke.modifiers, ke.text, ke.autorep, ke.count);
329 if (ke.destination == View) {
330 QCoreApplication::sendEvent(m_view, &event);
332 QCoreApplication::sendEvent(m_view->viewport(), &event);
335 m_savedMouseEvents.append(m_mouseEvents);
336 m_savedKeyEvents.append(m_keyEvents);
339 m_mouseEvents.clear();
342 // Advance test script
343 while (testscript && testscript->count() > testscriptidx) {
345 QObject *event = testscript->event(testscriptidx);
347 if (QDeclarativeVisualTestFrame *frame = qobject_cast<QDeclarativeVisualTestFrame *>(event)) {
348 if (frame->msec() < msec) {
349 if (options & QDeclarativeViewer::TestImages && !(options & QDeclarativeViewer::Record)) {
350 qWarning() << "QDeclarativeTester(" << m_script << "): Extra frame. Seen:"
351 << msec << "Expected:" << frame->msec();
354 } else if (frame->msec() == msec) {
355 if (!frame->hash().isEmpty() && frame->hash().toUtf8() != fe.hash.toHex()) {
356 if (options & QDeclarativeViewer::TestImages && !(options & QDeclarativeViewer::Record)) {
357 qWarning() << "QDeclarativeTester(" << m_script << "): Mismatched frame hash at" << msec
358 << ". Seen:" << fe.hash.toHex()
359 << "Expected:" << frame->hash().toUtf8();
363 } else if (frame->msec() > msec) {
367 if (options & QDeclarativeViewer::TestImages && !(options & QDeclarativeViewer::Record) && !frame->image().isEmpty()) {
368 QImage goodImage(frame->image().toLocalFile());
369 if (frame->msec() == 16 && goodImage.size() != img.size()){
370 //Also an image mismatch, but this warning is more informative. Only checked at start though.
371 qWarning() << "QDeclarativeTester(" << m_script << "): Size mismatch. This test must be run at " << goodImage.size();
374 if (goodImage != img) {
375 QString reject(frame->image().toLocalFile() + QLatin1String(".reject.png"));
376 qWarning() << "QDeclarativeTester(" << m_script << "): Image mismatch. Reject saved to:"
379 bool doDiff = (goodImage.size() == img.size());
381 QImage diffimg(m_view->width(), m_view->height(), QImage::Format_RGB32);
382 diffimg.fill(qRgb(255,255,255));
383 QPainter p(&diffimg);
385 for (int x = 0; x < img.width(); ++x) {
386 for (int y = 0; y < img.height(); ++y) {
387 if (goodImage.pixel(x,y) != img.pixel(x,y)) {
393 QString diff(frame->image().toLocalFile() + QLatin1String(".diff.png"));
395 qWarning().nospace() << " Diff (" << diffCount << " pixels differed) saved to: " << diff;
400 } else if (QDeclarativeVisualTestMouse *mouse = qobject_cast<QDeclarativeVisualTestMouse *>(event)) {
401 QPoint pos(mouse->x(), mouse->y());
402 QPoint globalPos = m_view->mapToGlobal(QPoint(0, 0)) + pos;
403 QMouseEvent event((QEvent::Type)mouse->type(), pos, globalPos, (Qt::MouseButton)mouse->button(), (Qt::MouseButtons)mouse->buttons(), (Qt::KeyboardModifiers)mouse->modifiers());
405 MouseEvent me(&event);
407 if (!mouse->sendToViewport()) {
408 QCoreApplication::sendEvent(m_view, &event);
409 me.destination = View;
411 QCoreApplication::sendEvent(m_view->viewport(), &event);
412 me.destination = ViewPort;
414 m_savedMouseEvents.append(me);
415 } else if (QDeclarativeVisualTestKey *key = qobject_cast<QDeclarativeVisualTestKey *>(event)) {
417 QKeyEvent event((QEvent::Type)key->type(), key->key(), (Qt::KeyboardModifiers)key->modifiers(), QString::fromUtf8(QByteArray::fromHex(key->text().toUtf8())), key->autorep(), key->count());
421 if (!key->sendToViewport()) {
422 QCoreApplication::sendEvent(m_view, &event);
423 ke.destination = View;
425 QCoreApplication::sendEvent(m_view->viewport(), &event);
426 ke.destination = ViewPort;
428 m_savedKeyEvents.append(ke);
435 if (testscript && testscript->count() <= testscriptidx) {
436 //if (msec == 16) //for a snapshot, leave it up long enough to see
442 void QDeclarativeTester::registerTypes()
444 qmlRegisterType<QDeclarativeVisualTest>("Qt.VisualTest", 4,7, "VisualTest");
445 qmlRegisterType<QDeclarativeVisualTestFrame>("Qt.VisualTest", 4,7, "Frame");
446 qmlRegisterType<QDeclarativeVisualTestMouse>("Qt.VisualTest", 4,7, "Mouse");
447 qmlRegisterType<QDeclarativeVisualTestKey>("Qt.VisualTest", 4,7, "Key");