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>
44 #include <QApplication>
45 #include <qdeclarativeview.h>
47 #include <QDeclarativeComponent>
49 #include <QCryptographicHash>
50 #include <private/qabstractanimation_p.h>
51 #include <QGraphicsObject>
53 #include <private/qdeclarativeitem_p.h>
58 extern Q_GUI_EXPORT bool qt_applefontsmoothing_enabled;
60 QDeclarativeTester::QDeclarativeTester(const QString &script, QDeclarativeViewer::ScriptOptions opts,
61 QDeclarativeView *parent)
62 : QAbstractAnimation(parent), m_script(script), m_view(parent), filterEvents(true), options(opts),
63 testscript(0), hasCompleted(false), hasFailed(false)
65 parent->viewport()->installEventFilter(this);
66 parent->installEventFilter(this);
67 QUnifiedTimer::instance()->setConsistentTiming(true);
69 //Font antialiasing makes tests system-specific, so disable it
70 QFont noAA = QApplication::font();
71 noAA.setStyleStrategy(QFont::NoAntialias);
72 QApplication::setFont(noAA);
74 if (options & QDeclarativeViewer::Play)
79 QDeclarativeTester::~QDeclarativeTester()
82 options & QDeclarativeViewer::Record &&
83 options & QDeclarativeViewer::SaveOnExit)
87 int QDeclarativeTester::duration() const
92 void QDeclarativeTester::addMouseEvent(Destination dest, QMouseEvent *me)
99 void QDeclarativeTester::addKeyEvent(Destination dest, QKeyEvent *ke)
102 e.destination = dest;
106 bool QDeclarativeTester::eventFilter(QObject *o, QEvent *e)
111 Destination destination;
114 } else if (o == m_view->viewport()) {
115 destination = ViewPort;
121 case QEvent::KeyPress:
122 case QEvent::KeyRelease:
123 addKeyEvent(destination, (QKeyEvent *)e);
125 case QEvent::MouseButtonPress:
126 case QEvent::MouseButtonRelease:
127 case QEvent::MouseMove:
128 case QEvent::MouseButtonDblClick:
129 addMouseEvent(destination, (QMouseEvent *)e);
137 void QDeclarativeTester::executefailure()
141 if (options & QDeclarativeViewer::ExitOnFailure)
145 void QDeclarativeTester::imagefailure()
149 if (options & QDeclarativeViewer::ExitOnFailure){
151 exit(hasFailed?-1:0);
155 void QDeclarativeTester::testSkip()
157 if (options & QDeclarativeViewer::TestSkipProperty){
158 QString e = m_view->rootObject()->property("skip").toString();
161 qWarning() << "Test failed, but skipping it: " << e;
163 qWarning() << "Test skipped: " << e;
170 void QDeclarativeTester::complete()
172 if ((options & QDeclarativeViewer::TestErrorProperty) && !hasFailed) {
173 QString e = m_view->rootObject()->property("error").toString();
175 qWarning() << "Test failed:" << e;
182 if (options & QDeclarativeViewer::ExitOnComplete)
183 QApplication::exit(hasFailed?-1:0);
189 if (options & QDeclarativeViewer::Play)
190 qWarning("Script playback complete");
193 void QDeclarativeTester::run()
195 QDeclarativeComponent c(m_view->engine(), m_script + QLatin1String(".qml"));
197 testscript = qobject_cast<QDeclarativeVisualTest *>(c.create());
198 if (testscript) testscript->setParent(this);
199 else { executefailure(); exit(-1); }
203 void QDeclarativeTester::save()
205 QString filename = m_script + QLatin1String(".qml");
206 QFileInfo filenameInfo(filename);
207 QDir saveDir = filenameInfo.absoluteDir();
210 QFile file(filename);
211 file.open(QIODevice::WriteOnly);
212 QTextStream ts(&file);
214 ts << "import Qt.VisualTest 4.7\n\n";
215 ts << "VisualTest {\n";
218 QList<KeyEvent> keyevents = m_savedKeyEvents;
219 QList<MouseEvent> mouseevents = m_savedMouseEvents;
220 for (int ii = 0; ii < m_savedFrameEvents.count(); ++ii) {
221 const FrameEvent &fe = m_savedFrameEvents.at(ii);
223 ts << " msec: " << fe.msec << "\n";
224 if (!fe.hash.isEmpty()) {
225 ts << " hash: \"" << fe.hash.toHex() << "\"\n";
226 } else if (!fe.image.isNull()) {
227 QString filename = filenameInfo.baseName() + "." + QString::number(imgCount) + ".png";
228 fe.image.save(m_script + "." + QString::number(imgCount) + ".png");
230 ts << " image: \"" << filename << "\"\n";
234 while (!mouseevents.isEmpty() &&
235 mouseevents.first().msec == fe.msec) {
236 MouseEvent me = mouseevents.takeFirst();
239 ts << " type: " << me.type << "\n";
240 ts << " button: " << me.button << "\n";
241 ts << " buttons: " << me.buttons << "\n";
242 ts << " x: " << me.pos.x() << "; y: " << me.pos.y() << "\n";
243 ts << " modifiers: " << me.modifiers << "\n";
244 if (me.destination == ViewPort)
245 ts << " sendToViewport: true\n";
249 while (!keyevents.isEmpty() &&
250 keyevents.first().msec == fe.msec) {
251 KeyEvent ke = keyevents.takeFirst();
254 ts << " type: " << ke.type << "\n";
255 ts << " key: " << ke.key << "\n";
256 ts << " modifiers: " << ke.modifiers << "\n";
257 ts << " text: \"" << ke.text.toUtf8().toHex() << "\"\n";
258 ts << " autorep: " << (ke.autorep?"true":"false") << "\n";
259 ts << " count: " << ke.count << "\n";
260 if (ke.destination == ViewPort)
261 ts << " sendToViewport: true\n";
270 void QDeclarativeTester::updateCurrentTime(int msec)
273 QDeclarativeItemPrivate::setConsistentTime(msec);
275 if (!testscript && msec > 16 && options & QDeclarativeViewer::Snapshot)
278 QImage img(m_view->width(), m_view->height(), QImage::Format_RGB32);
280 if (options & QDeclarativeViewer::TestImages) {
281 img.fill(qRgb(255,255,255));
284 bool oldSmooth = qt_applefontsmoothing_enabled;
285 qt_applefontsmoothing_enabled = false;
289 qt_applefontsmoothing_enabled = oldSmooth;
295 bool snapshot = msec == 16 && (options & QDeclarativeViewer::Snapshot
296 || (testscript && testscript->count() == 2));
300 if (msec == 0 || !(options & QDeclarativeViewer::TestImages)) {
301 // Skip first frame, skip if not doing images
302 } else if (0 == ((m_savedFrameEvents.count()-1) % 60) || snapshot) {
305 QCryptographicHash hash(QCryptographicHash::Md5);
306 hash.addData((const char *)img.bits(), img.bytesPerLine() * img.height());
307 fe.hash = hash.result();
309 m_savedFrameEvents.append(fe);
311 // Deliver mouse events
312 filterEvents = false;
315 for (int ii = 0; ii < m_mouseEvents.count(); ++ii) {
316 MouseEvent &me = m_mouseEvents[ii];
318 QMouseEvent event(me.type, me.pos, me.button, me.buttons, me.modifiers);
320 if (me.destination == View) {
321 QCoreApplication::sendEvent(m_view, &event);
323 QCoreApplication::sendEvent(m_view->viewport(), &event);
327 for (int ii = 0; ii < m_keyEvents.count(); ++ii) {
328 KeyEvent &ke = m_keyEvents[ii];
330 QKeyEvent event(ke.type, ke.key, ke.modifiers, ke.text, ke.autorep, ke.count);
332 if (ke.destination == View) {
333 QCoreApplication::sendEvent(m_view, &event);
335 QCoreApplication::sendEvent(m_view->viewport(), &event);
338 m_savedMouseEvents.append(m_mouseEvents);
339 m_savedKeyEvents.append(m_keyEvents);
342 m_mouseEvents.clear();
345 // Advance test script
346 while (testscript && testscript->count() > testscriptidx) {
348 QObject *event = testscript->event(testscriptidx);
350 if (QDeclarativeVisualTestFrame *frame = qobject_cast<QDeclarativeVisualTestFrame *>(event)) {
351 if (frame->msec() < msec) {
352 if (options & QDeclarativeViewer::TestImages && !(options & QDeclarativeViewer::Record)) {
353 qWarning() << "QDeclarativeTester(" << m_script << "): Extra frame. Seen:"
354 << msec << "Expected:" << frame->msec();
357 } else if (frame->msec() == msec) {
358 if (!frame->hash().isEmpty() && frame->hash().toUtf8() != fe.hash.toHex()) {
359 if (options & QDeclarativeViewer::TestImages && !(options & QDeclarativeViewer::Record)) {
360 qWarning() << "QDeclarativeTester(" << m_script << "): Mismatched frame hash at" << msec
361 << ". Seen:" << fe.hash.toHex()
362 << "Expected:" << frame->hash().toUtf8();
366 } else if (frame->msec() > msec) {
370 if (options & QDeclarativeViewer::TestImages && !(options & QDeclarativeViewer::Record) && !frame->image().isEmpty()) {
371 QImage goodImage(frame->image().toLocalFile());
372 if (frame->msec() == 16 && goodImage.size() != img.size()){
373 //Also an image mismatch, but this warning is more informative. Only checked at start though.
374 qWarning() << "QDeclarativeTester(" << m_script << "): Size mismatch. This test must be run at " << goodImage.size();
377 if (goodImage != img) {
378 QString reject(frame->image().toLocalFile() + ".reject.png");
379 qWarning() << "QDeclarativeTester(" << m_script << "): Image mismatch. Reject saved to:"
382 bool doDiff = (goodImage.size() == img.size());
384 QImage diffimg(m_view->width(), m_view->height(), QImage::Format_RGB32);
385 diffimg.fill(qRgb(255,255,255));
386 QPainter p(&diffimg);
388 for (int x = 0; x < img.width(); ++x) {
389 for (int y = 0; y < img.height(); ++y) {
390 if (goodImage.pixel(x,y) != img.pixel(x,y)) {
396 QString diff(frame->image().toLocalFile() + ".diff.png");
398 qWarning().nospace() << " Diff (" << diffCount << " pixels differed) saved to: " << diff;
403 } else if (QDeclarativeVisualTestMouse *mouse = qobject_cast<QDeclarativeVisualTestMouse *>(event)) {
404 QPoint pos(mouse->x(), mouse->y());
405 QPoint globalPos = m_view->mapToGlobal(QPoint(0, 0)) + pos;
406 QMouseEvent event((QEvent::Type)mouse->type(), pos, globalPos, (Qt::MouseButton)mouse->button(), (Qt::MouseButtons)mouse->buttons(), (Qt::KeyboardModifiers)mouse->modifiers());
408 MouseEvent me(&event);
410 if (!mouse->sendToViewport()) {
411 QCoreApplication::sendEvent(m_view, &event);
412 me.destination = View;
414 QCoreApplication::sendEvent(m_view->viewport(), &event);
415 me.destination = ViewPort;
417 m_savedMouseEvents.append(me);
418 } else if (QDeclarativeVisualTestKey *key = qobject_cast<QDeclarativeVisualTestKey *>(event)) {
420 QKeyEvent event((QEvent::Type)key->type(), key->key(), (Qt::KeyboardModifiers)key->modifiers(), QString::fromUtf8(QByteArray::fromHex(key->text().toUtf8())), key->autorep(), key->count());
424 if (!key->sendToViewport()) {
425 QCoreApplication::sendEvent(m_view, &event);
426 ke.destination = View;
428 QCoreApplication::sendEvent(m_view->viewport(), &event);
429 ke.destination = ViewPort;
431 m_savedKeyEvents.append(ke);
438 if (testscript && testscript->count() <= testscriptidx) {
439 //if (msec == 16) //for a snapshot, leave it up long enough to see
445 void QDeclarativeTester::registerTypes()
447 qmlRegisterType<QDeclarativeVisualTest>("Qt.VisualTest", 4,7, "VisualTest");
448 qmlRegisterType<QDeclarativeVisualTestFrame>("Qt.VisualTest", 4,7, "Frame");
449 qmlRegisterType<QDeclarativeVisualTestMouse>("Qt.VisualTest", 4,7, "Mouse");
450 qmlRegisterType<QDeclarativeVisualTestKey>("Qt.VisualTest", 4,7, "Key");