From 8c757e4b84748c19e5f66da432d8af2e7db40cde Mon Sep 17 00:00:00 2001 From: Michael Brasser Date: Wed, 31 Mar 2010 14:14:19 +1000 Subject: Minor cleanup for visual test framework. --- tools/qml/main.cpp | 2 +- tools/qml/qdeclarativetester.cpp | 381 +++++++++++++++++++++++++++++++++++++++ tools/qml/qdeclarativetester.h | 286 +++++++++++++++++++++++++++++ tools/qml/qfxtester.cpp | 381 --------------------------------------- tools/qml/qfxtester.h | 286 ----------------------------- tools/qml/qml.pro | 4 +- tools/qml/qmlruntime.cpp | 2 +- 7 files changed, 671 insertions(+), 671 deletions(-) create mode 100644 tools/qml/qdeclarativetester.cpp create mode 100644 tools/qml/qdeclarativetester.h delete mode 100644 tools/qml/qfxtester.cpp delete mode 100644 tools/qml/qfxtester.h diff --git a/tools/qml/main.cpp b/tools/qml/main.cpp index 8062e8e..5099e49 100644 --- a/tools/qml/main.cpp +++ b/tools/qml/main.cpp @@ -47,7 +47,7 @@ #include #include #include -#include "qfxtester.h" +#include "qdeclarativetester.h" #include "qdeclarativefolderlistmodel.h" QT_USE_NAMESPACE diff --git a/tools/qml/qdeclarativetester.cpp b/tools/qml/qdeclarativetester.cpp new file mode 100644 index 0000000..6245124 --- /dev/null +++ b/tools/qml/qdeclarativetester.cpp @@ -0,0 +1,381 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the tools applications of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +QT_BEGIN_NAMESPACE + + +QDeclarativeTester::QDeclarativeTester(const QString &script, QDeclarativeViewer::ScriptOptions opts, + QDeclarativeView *parent) +: QAbstractAnimation(parent), m_script(script), m_view(parent), filterEvents(true), options(opts), + testscript(0), hasCompleted(false), hasFailed(false) +{ + parent->viewport()->installEventFilter(this); + parent->installEventFilter(this); + QUnifiedTimer::instance()->setConsistentTiming(true); + if (options & QDeclarativeViewer::Play) + this->run(); + start(); +} + +QDeclarativeTester::~QDeclarativeTester() +{ + if (!hasFailed && + options & QDeclarativeViewer::Record && + options & QDeclarativeViewer::SaveOnExit) + save(); +} + +int QDeclarativeTester::duration() const +{ + return -1; +} + +void QDeclarativeTester::addMouseEvent(Destination dest, QMouseEvent *me) +{ + MouseEvent e(me); + e.destination = dest; + m_mouseEvents << e; +} + +void QDeclarativeTester::addKeyEvent(Destination dest, QKeyEvent *ke) +{ + KeyEvent e(ke); + e.destination = dest; + m_keyEvents << e; +} + +bool QDeclarativeTester::eventFilter(QObject *o, QEvent *e) +{ + if (!filterEvents) + return false; + + Destination destination; + if (o == m_view) { + destination = View; + } else if (o == m_view->viewport()) { + destination = ViewPort; + } else { + return false; + } + + switch (e->type()) { + case QEvent::KeyPress: + case QEvent::KeyRelease: + addKeyEvent(destination, (QKeyEvent *)e); + return true; + case QEvent::MouseButtonPress: + case QEvent::MouseButtonRelease: + case QEvent::MouseMove: + case QEvent::MouseButtonDblClick: + addMouseEvent(destination, (QMouseEvent *)e); + return true; + default: + break; + } + return false; +} + +void QDeclarativeTester::executefailure() +{ + hasFailed = true; + + if (options & QDeclarativeViewer::ExitOnFailure) + exit(-1); +} + +void QDeclarativeTester::imagefailure() +{ + hasFailed = true; + + if (options & QDeclarativeViewer::ExitOnFailure) + exit(-1); +} + +void QDeclarativeTester::complete() +{ + if ((options & QDeclarativeViewer::TestErrorProperty) && !hasFailed) { + QString e = m_view->rootObject()->property("error").toString(); + if (!e.isEmpty()) { + qWarning() << "Test failed:" << e; + hasFailed = true; + } + } + if (options & QDeclarativeViewer::ExitOnComplete) + QApplication::exit(hasFailed?-1:0); + + if (hasCompleted) + return; + hasCompleted = true; + + if (options & QDeclarativeViewer::Play) + qWarning("Script playback complete"); +} + +void QDeclarativeTester::run() +{ + QDeclarativeComponent c(m_view->engine(), m_script + QLatin1String(".qml")); + + testscript = qobject_cast(c.create()); + if (testscript) testscript->setParent(this); + else { executefailure(); exit(-1); } + testscriptidx = 0; +} + +void QDeclarativeTester::save() +{ + QString filename = m_script + QLatin1String(".qml"); + QFileInfo filenameInfo(filename); + QDir saveDir = filenameInfo.absoluteDir(); + saveDir.mkpath("."); + + QFile file(filename); + file.open(QIODevice::WriteOnly); + QTextStream ts(&file); + + ts << "import Qt.VisualTest 4.6\n\n"; + ts << "VisualTest {\n"; + + int imgCount = 0; + QList keyevents = m_savedKeyEvents; + QList mouseevents = m_savedMouseEvents; + for (int ii = 0; ii < m_savedFrameEvents.count(); ++ii) { + const FrameEvent &fe = m_savedFrameEvents.at(ii); + ts << " Frame {\n"; + ts << " msec: " << fe.msec << "\n"; + if (!fe.hash.isEmpty()) { + ts << " hash: \"" << fe.hash.toHex() << "\"\n"; + } else if (!fe.image.isNull()) { + QString filename = filenameInfo.baseName() + "." + QString::number(imgCount) + ".png"; + fe.image.save(m_script + "." + QString::number(imgCount) + ".png"); + imgCount++; + ts << " image: \"" << filename << "\"\n"; + } + ts << " }\n"; + + while (!mouseevents.isEmpty() && + mouseevents.first().msec == fe.msec) { + MouseEvent me = mouseevents.takeFirst(); + + ts << " Mouse {\n"; + ts << " type: " << me.type << "\n"; + ts << " button: " << me.button << "\n"; + ts << " buttons: " << me.buttons << "\n"; + ts << " x: " << me.pos.x() << "; y: " << me.pos.y() << "\n"; + ts << " modifiers: " << me.modifiers << "\n"; + if (me.destination == ViewPort) + ts << " sendToViewport: true\n"; + ts << " }\n"; + } + + while (!keyevents.isEmpty() && + keyevents.first().msec == fe.msec) { + KeyEvent ke = keyevents.takeFirst(); + + ts << " Key {\n"; + ts << " type: " << ke.type << "\n"; + ts << " key: " << ke.key << "\n"; + ts << " modifiers: " << ke.modifiers << "\n"; + ts << " text: \"" << ke.text.toUtf8().toHex() << "\"\n"; + ts << " autorep: " << (ke.autorep?"true":"false") << "\n"; + ts << " count: " << ke.count << "\n"; + if (ke.destination == ViewPort) + ts << " sendToViewport: true\n"; + ts << " }\n"; + } + } + + ts << "}\n"; + file.close(); +} + +void QDeclarativeTester::updateCurrentTime(int msec) +{ + QDeclarativeItemPrivate::setConsistentTime(msec); + + QImage img(m_view->width(), m_view->height(), QImage::Format_RGB32); + + if (options & QDeclarativeViewer::TestImages) { + img.fill(qRgb(255,255,255)); + QPainter p(&img); + m_view->render(&p); + } + + FrameEvent fe; + fe.msec = msec; + if (msec == 0 || !(options & QDeclarativeViewer::TestImages)) { + // Skip first frame, skip if not doing images + } else if (0 == (m_savedFrameEvents.count() % 60)) { + fe.image = img; + } else { + QCryptographicHash hash(QCryptographicHash::Md5); + hash.addData((const char *)img.bits(), img.bytesPerLine() * img.height()); + fe.hash = hash.result(); + } + m_savedFrameEvents.append(fe); + + // Deliver mouse events + filterEvents = false; + + if (!testscript) { + for (int ii = 0; ii < m_mouseEvents.count(); ++ii) { + MouseEvent &me = m_mouseEvents[ii]; + me.msec = msec; + QMouseEvent event(me.type, me.pos, me.button, me.buttons, me.modifiers); + + if (me.destination == View) { + QCoreApplication::sendEvent(m_view, &event); + } else { + QCoreApplication::sendEvent(m_view->viewport(), &event); + } + } + + for (int ii = 0; ii < m_keyEvents.count(); ++ii) { + KeyEvent &ke = m_keyEvents[ii]; + ke.msec = msec; + QKeyEvent event(ke.type, ke.key, ke.modifiers, ke.text, ke.autorep, ke.count); + + if (ke.destination == View) { + QCoreApplication::sendEvent(m_view, &event); + } else { + QCoreApplication::sendEvent(m_view->viewport(), &event); + } + } + m_savedMouseEvents.append(m_mouseEvents); + m_savedKeyEvents.append(m_keyEvents); + } + + m_mouseEvents.clear(); + m_keyEvents.clear(); + + // Advance test script + while (testscript && testscript->count() > testscriptidx) { + + QObject *event = testscript->event(testscriptidx); + + if (QDeclarativeVisualTestFrame *frame = qobject_cast(event)) { + if (frame->msec() < msec) { + if (options & QDeclarativeViewer::TestImages && !(options & QDeclarativeViewer::Record)) { + qWarning() << "QDeclarativeTester: Extra frame. Seen:" + << msec << "Expected:" << frame->msec(); + imagefailure(); + } + } else if (frame->msec() == msec) { + if (!frame->hash().isEmpty() && frame->hash().toUtf8() != fe.hash.toHex()) { + if (options & QDeclarativeViewer::TestImages && !(options & QDeclarativeViewer::Record)) { + qWarning() << "QDeclarativeTester: Mismatched frame hash at" << msec + << ". Seen:" << fe.hash.toHex() + << "Expected:" << frame->hash().toUtf8(); + imagefailure(); + } + } + } else if (frame->msec() > msec) { + break; + } + + if (options & QDeclarativeViewer::TestImages && !(options & QDeclarativeViewer::Record) && !frame->image().isEmpty()) { + QImage goodImage(frame->image().toLocalFile()); + if (goodImage != img) { + QString reject(frame->image().toLocalFile() + ".reject.png"); + qWarning() << "QDeclarativeTester: Image mismatch. Reject saved to:" + << reject; + img.save(reject); + imagefailure(); + } + } + } else if (QDeclarativeVisualTestMouse *mouse = qobject_cast(event)) { + QPoint pos(mouse->x(), mouse->y()); + QPoint globalPos = m_view->mapToGlobal(QPoint(0, 0)) + pos; + QMouseEvent event((QEvent::Type)mouse->type(), pos, globalPos, (Qt::MouseButton)mouse->button(), (Qt::MouseButtons)mouse->buttons(), (Qt::KeyboardModifiers)mouse->modifiers()); + + MouseEvent me(&event); + me.msec = msec; + if (!mouse->sendToViewport()) { + QCoreApplication::sendEvent(m_view, &event); + me.destination = View; + } else { + QCoreApplication::sendEvent(m_view->viewport(), &event); + me.destination = ViewPort; + } + m_savedMouseEvents.append(me); + } else if (QDeclarativeVisualTestKey *key = qobject_cast(event)) { + + QKeyEvent event((QEvent::Type)key->type(), key->key(), (Qt::KeyboardModifiers)key->modifiers(), QString::fromUtf8(QByteArray::fromHex(key->text().toUtf8())), key->autorep(), key->count()); + + KeyEvent ke(&event); + ke.msec = msec; + if (!key->sendToViewport()) { + QCoreApplication::sendEvent(m_view, &event); + ke.destination = View; + } else { + QCoreApplication::sendEvent(m_view->viewport(), &event); + ke.destination = ViewPort; + } + m_savedKeyEvents.append(ke); + } + testscriptidx++; + } + + filterEvents = true; + + if (testscript && testscript->count() <= testscriptidx) + complete(); +} + +void QDeclarativeTester::registerTypes() +{ + qmlRegisterType("Qt.VisualTest", 4,6, "VisualTest"); + qmlRegisterType("Qt.VisualTest", 4,6, "Frame"); + qmlRegisterType("Qt.VisualTest", 4,6, "Mouse"); + qmlRegisterType("Qt.VisualTest", 4,6, "Key"); +} + +QT_END_NAMESPACE diff --git a/tools/qml/qdeclarativetester.h b/tools/qml/qdeclarativetester.h new file mode 100644 index 0000000..d49c9b8 --- /dev/null +++ b/tools/qml/qdeclarativetester.h @@ -0,0 +1,286 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the tools applications of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QDECLARATIVETESTER_H +#define QDECLARATIVETESTER_H + +#include +#include +#include +#include +#include +#include + +QT_BEGIN_NAMESPACE + +class QDeclarativeVisualTest : public QObject +{ + Q_OBJECT + Q_PROPERTY(QDeclarativeListProperty events READ events CONSTANT) + Q_CLASSINFO("DefaultProperty", "events") +public: + QDeclarativeVisualTest() {} + + QDeclarativeListProperty events() { return QDeclarativeListProperty(this, m_events); } + + int count() const { return m_events.count(); } + QObject *event(int idx) { return m_events.at(idx); } + +private: + QList m_events; +}; + +QT_END_NAMESPACE + +QML_DECLARE_TYPE(QDeclarativeVisualTest) + +QT_BEGIN_NAMESPACE + +class QDeclarativeVisualTestFrame : public QObject +{ + Q_OBJECT + Q_PROPERTY(int msec READ msec WRITE setMsec) + Q_PROPERTY(QString hash READ hash WRITE setHash) + Q_PROPERTY(QUrl image READ image WRITE setImage) +public: + QDeclarativeVisualTestFrame() : m_msec(-1) {} + + int msec() const { return m_msec; } + void setMsec(int m) { m_msec = m; } + + QString hash() const { return m_hash; } + void setHash(const QString &hash) { m_hash = hash; } + + QUrl image() const { return m_image; } + void setImage(const QUrl &image) { m_image = image; } + +private: + int m_msec; + QString m_hash; + QUrl m_image; +}; + +QT_END_NAMESPACE + +QML_DECLARE_TYPE(QDeclarativeVisualTestFrame) + +QT_BEGIN_NAMESPACE + +class QDeclarativeVisualTestMouse : public QObject +{ + Q_OBJECT + Q_PROPERTY(int type READ type WRITE setType) + Q_PROPERTY(int button READ button WRITE setButton) + Q_PROPERTY(int buttons READ buttons WRITE setButtons) + Q_PROPERTY(int x READ x WRITE setX) + Q_PROPERTY(int y READ y WRITE setY) + Q_PROPERTY(int modifiers READ modifiers WRITE setModifiers) + Q_PROPERTY(bool sendToViewport READ sendToViewport WRITE setSendToViewport) +public: + QDeclarativeVisualTestMouse() : m_type(0), m_button(0), m_buttons(0), m_x(0), m_y(0), m_modifiers(0), m_viewport(false) {} + + int type() const { return m_type; } + void setType(int t) { m_type = t; } + + int button() const { return m_button; } + void setButton(int b) { m_button = b; } + + int buttons() const { return m_buttons; } + void setButtons(int b) { m_buttons = b; } + + int x() const { return m_x; } + void setX(int x) { m_x = x; } + + int y() const { return m_y; } + void setY(int y) { m_y = y; } + + int modifiers() const { return m_modifiers; } + void setModifiers(int modifiers) { m_modifiers = modifiers; } + + bool sendToViewport() const { return m_viewport; } + void setSendToViewport(bool v) { m_viewport = v; } +private: + int m_type; + int m_button; + int m_buttons; + int m_x; + int m_y; + int m_modifiers; + bool m_viewport; +}; + +QT_END_NAMESPACE + +QML_DECLARE_TYPE(QDeclarativeVisualTestMouse) + +QT_BEGIN_NAMESPACE + +class QDeclarativeVisualTestKey : public QObject +{ + Q_OBJECT + Q_PROPERTY(int type READ type WRITE setType) + Q_PROPERTY(int key READ key WRITE setKey) + Q_PROPERTY(int modifiers READ modifiers WRITE setModifiers) + Q_PROPERTY(QString text READ text WRITE setText) + Q_PROPERTY(bool autorep READ autorep WRITE setAutorep) + Q_PROPERTY(int count READ count WRITE setCount) + Q_PROPERTY(bool sendToViewport READ sendToViewport WRITE setSendToViewport) +public: + QDeclarativeVisualTestKey() : m_type(0), m_key(0), m_modifiers(0), m_autorep(false), m_count(0), m_viewport(false) {} + + int type() const { return m_type; } + void setType(int t) { m_type = t; } + + int key() const { return m_key; } + void setKey(int k) { m_key = k; } + + int modifiers() const { return m_modifiers; } + void setModifiers(int m) { m_modifiers = m; } + + QString text() const { return m_text; } + void setText(const QString &t) { m_text = t; } + + bool autorep() const { return m_autorep; } + void setAutorep(bool a) { m_autorep = a; } + + int count() const { return m_count; } + void setCount(int c) { m_count = c; } + + bool sendToViewport() const { return m_viewport; } + void setSendToViewport(bool v) { m_viewport = v; } +private: + int m_type; + int m_key; + int m_modifiers; + QString m_text; + bool m_autorep; + int m_count; + bool m_viewport; +}; + +QT_END_NAMESPACE + +QML_DECLARE_TYPE(QDeclarativeVisualTestKey) + +QT_BEGIN_NAMESPACE + +class QDeclarativeTester : public QAbstractAnimation +{ +public: + QDeclarativeTester(const QString &script, QDeclarativeViewer::ScriptOptions options, QDeclarativeView *parent); + ~QDeclarativeTester(); + + static void registerTypes(); + + virtual int duration() const; + + void run(); + void save(); + + void executefailure(); +protected: + virtual void updateCurrentTime(int msecs); + virtual bool eventFilter(QObject *, QEvent *); + +private: + QString m_script; + + void imagefailure(); + void complete(); + + enum Destination { View, ViewPort }; + void addKeyEvent(Destination, QKeyEvent *); + void addMouseEvent(Destination, QMouseEvent *); + QDeclarativeView *m_view; + + struct MouseEvent { + MouseEvent(QMouseEvent *e) + : type(e->type()), button(e->button()), buttons(e->buttons()), + pos(e->pos()), modifiers(e->modifiers()), destination(View) {} + + QEvent::Type type; + Qt::MouseButton button; + Qt::MouseButtons buttons; + QPoint pos; + Qt::KeyboardModifiers modifiers; + Destination destination; + + int msec; + }; + struct KeyEvent { + KeyEvent(QKeyEvent *e) + : type(e->type()), key(e->key()), modifiers(e->modifiers()), text(e->text()), + autorep(e->isAutoRepeat()), count(e->count()), destination(View) {} + QEvent::Type type; + int key; + Qt::KeyboardModifiers modifiers; + QString text; + bool autorep; + ushort count; + Destination destination; + + int msec; + }; + struct FrameEvent { + QImage image; + QByteArray hash; + int msec; + }; + QList m_mouseEvents; + QList m_keyEvents; + + QList m_savedMouseEvents; + QList m_savedKeyEvents; + QList m_savedFrameEvents; + bool filterEvents; + + QDeclarativeViewer::ScriptOptions options; + int testscriptidx; + QDeclarativeVisualTest *testscript; + + bool hasCompleted; + bool hasFailed; +}; + + +QT_END_NAMESPACE + +#endif // QDECLARATIVETESTER_H diff --git a/tools/qml/qfxtester.cpp b/tools/qml/qfxtester.cpp deleted file mode 100644 index 28bbf5e..0000000 --- a/tools/qml/qfxtester.cpp +++ /dev/null @@ -1,381 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). -** All rights reserved. -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** This file is part of the tools applications of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** No Commercial Usage -** This file contains pre-release code and may not be distributed. -** You may use this file in accordance with the terms and conditions -** contained in the Technology Preview License Agreement accompanying -** this package. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 2.1 requirements -** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain additional -** rights. These rights are described in the Nokia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** If you have questions regarding the use of this file, please contact -** Nokia at qt-info@nokia.com. -** -** -** -** -** -** -** -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -QT_BEGIN_NAMESPACE - - -QDeclarativeTester::QDeclarativeTester(const QString &script, QDeclarativeViewer::ScriptOptions opts, - QDeclarativeView *parent) -: QAbstractAnimation(parent), m_script(script), m_view(parent), filterEvents(true), options(opts), - testscript(0), hasCompleted(false), hasFailed(false) -{ - parent->viewport()->installEventFilter(this); - parent->installEventFilter(this); - QUnifiedTimer::instance()->setConsistentTiming(true); - if (options & QDeclarativeViewer::Play) - this->run(); - start(); -} - -QDeclarativeTester::~QDeclarativeTester() -{ - if (!hasFailed && - options & QDeclarativeViewer::Record && - options & QDeclarativeViewer::SaveOnExit) - save(); -} - -int QDeclarativeTester::duration() const -{ - return -1; -} - -void QDeclarativeTester::addMouseEvent(Destination dest, QMouseEvent *me) -{ - MouseEvent e(me); - e.destination = dest; - m_mouseEvents << e; -} - -void QDeclarativeTester::addKeyEvent(Destination dest, QKeyEvent *ke) -{ - KeyEvent e(ke); - e.destination = dest; - m_keyEvents << e; -} - -bool QDeclarativeTester::eventFilter(QObject *o, QEvent *e) -{ - if (!filterEvents) - return false; - - Destination destination; - if (o == m_view) { - destination = View; - } else if (o == m_view->viewport()) { - destination = ViewPort; - } else { - return false; - } - - switch (e->type()) { - case QEvent::KeyPress: - case QEvent::KeyRelease: - addKeyEvent(destination, (QKeyEvent *)e); - return true; - case QEvent::MouseButtonPress: - case QEvent::MouseButtonRelease: - case QEvent::MouseMove: - case QEvent::MouseButtonDblClick: - addMouseEvent(destination, (QMouseEvent *)e); - return true; - default: - break; - } - return false; -} - -void QDeclarativeTester::executefailure() -{ - hasFailed = true; - - if (options & QDeclarativeViewer::ExitOnFailure) - exit(-1); -} - -void QDeclarativeTester::imagefailure() -{ - hasFailed = true; - - if (options & QDeclarativeViewer::ExitOnFailure) - exit(-1); -} - -void QDeclarativeTester::complete() -{ - if ((options & QDeclarativeViewer::TestErrorProperty) && !hasFailed) { - QString e = m_view->rootObject()->property("error").toString(); - if (!e.isEmpty()) { - qWarning() << "Test failed:" << e; - hasFailed = true; - } - } - if (options & QDeclarativeViewer::ExitOnComplete) - QApplication::exit(hasFailed?-1:0); - - if (hasCompleted) - return; - hasCompleted = true; - - if (options & QDeclarativeViewer::Play) - qWarning("Script playback complete"); -} - -void QDeclarativeTester::run() -{ - QDeclarativeComponent c(m_view->engine(), m_script + QLatin1String(".qml")); - - testscript = qobject_cast(c.create()); - if (testscript) testscript->setParent(this); - else { executefailure(); exit(-1); } - testscriptidx = 0; -} - -void QDeclarativeTester::save() -{ - QString filename = m_script + QLatin1String(".qml"); - QFileInfo filenameInfo(filename); - QDir saveDir = filenameInfo.absoluteDir(); - saveDir.mkpath("."); - - QFile file(filename); - file.open(QIODevice::WriteOnly); - QTextStream ts(&file); - - ts << "import Qt.VisualTest 4.6\n\n"; - ts << "VisualTest {\n"; - - int imgCount = 0; - QList keyevents = m_savedKeyEvents; - QList mouseevents = m_savedMouseEvents; - for (int ii = 0; ii < m_savedFrameEvents.count(); ++ii) { - const FrameEvent &fe = m_savedFrameEvents.at(ii); - ts << " Frame {\n"; - ts << " msec: " << fe.msec << "\n"; - if (!fe.hash.isEmpty()) { - ts << " hash: \"" << fe.hash.toHex() << "\"\n"; - } else if (!fe.image.isNull()) { - QString filename = filenameInfo.baseName() + "." + QString::number(imgCount) + ".png"; - fe.image.save(m_script + "." + QString::number(imgCount) + ".png"); - imgCount++; - ts << " image: \"" << filename << "\"\n"; - } - ts << " }\n"; - - while (!mouseevents.isEmpty() && - mouseevents.first().msec == fe.msec) { - MouseEvent me = mouseevents.takeFirst(); - - ts << " Mouse {\n"; - ts << " type: " << me.type << "\n"; - ts << " button: " << me.button << "\n"; - ts << " buttons: " << me.buttons << "\n"; - ts << " x: " << me.pos.x() << "; y: " << me.pos.y() << "\n"; - ts << " modifiers: " << me.modifiers << "\n"; - if (me.destination == ViewPort) - ts << " sendToViewport: true\n"; - ts << " }\n"; - } - - while (!keyevents.isEmpty() && - keyevents.first().msec == fe.msec) { - KeyEvent ke = keyevents.takeFirst(); - - ts << " Key {\n"; - ts << " type: " << ke.type << "\n"; - ts << " key: " << ke.key << "\n"; - ts << " modifiers: " << ke.modifiers << "\n"; - ts << " text: \"" << ke.text.toUtf8().toHex() << "\"\n"; - ts << " autorep: " << (ke.autorep?"true":"false") << "\n"; - ts << " count: " << ke.count << "\n"; - if (ke.destination == ViewPort) - ts << " sendToViewport: true\n"; - ts << " }\n"; - } - } - - ts << "}\n"; - file.close(); -} - -void QDeclarativeTester::updateCurrentTime(int msec) -{ - QDeclarativeItemPrivate::setConsistentTime(msec); - - QImage img(m_view->width(), m_view->height(), QImage::Format_RGB32); - - if (options & QDeclarativeViewer::TestImages) { - img.fill(qRgb(255,255,255)); - QPainter p(&img); - m_view->render(&p); - } - - FrameEvent fe; - fe.msec = msec; - if (msec == 0 || !(options & QDeclarativeViewer::TestImages)) { - // Skip first frame, skip if not doing images - } else if (0 == (m_savedFrameEvents.count() % 60)) { - fe.image = img; - } else { - QCryptographicHash hash(QCryptographicHash::Md5); - hash.addData((const char *)img.bits(), img.bytesPerLine() * img.height()); - fe.hash = hash.result(); - } - m_savedFrameEvents.append(fe); - - // Deliver mouse events - filterEvents = false; - - if (!testscript) { - for (int ii = 0; ii < m_mouseEvents.count(); ++ii) { - MouseEvent &me = m_mouseEvents[ii]; - me.msec = msec; - QMouseEvent event(me.type, me.pos, me.button, me.buttons, me.modifiers); - - if (me.destination == View) { - QCoreApplication::sendEvent(m_view, &event); - } else { - QCoreApplication::sendEvent(m_view->viewport(), &event); - } - } - - for (int ii = 0; ii < m_keyEvents.count(); ++ii) { - KeyEvent &ke = m_keyEvents[ii]; - ke.msec = msec; - QKeyEvent event(ke.type, ke.key, ke.modifiers, ke.text, ke.autorep, ke.count); - - if (ke.destination == View) { - QCoreApplication::sendEvent(m_view, &event); - } else { - QCoreApplication::sendEvent(m_view->viewport(), &event); - } - } - m_savedMouseEvents.append(m_mouseEvents); - m_savedKeyEvents.append(m_keyEvents); - } - - m_mouseEvents.clear(); - m_keyEvents.clear(); - - // Advance test script - while (testscript && testscript->count() > testscriptidx) { - - QObject *event = testscript->event(testscriptidx); - - if (QDeclarativeVisualTestFrame *frame = qobject_cast(event)) { - if (frame->msec() < msec) { - if (options & QDeclarativeViewer::TestImages && !(options & QDeclarativeViewer::Record)) { - qWarning() << "QDeclarativeTester: Extra frame. Seen:" - << msec << "Expected:" << frame->msec(); - imagefailure(); - } - } else if (frame->msec() == msec) { - if (!frame->hash().isEmpty() && frame->hash().toUtf8() != fe.hash.toHex()) { - if (options & QDeclarativeViewer::TestImages && !(options & QDeclarativeViewer::Record)) { - qWarning() << "QDeclarativeTester: Mismatched frame hash. Seen:" - << fe.hash.toHex() << "Expected:" - << frame->hash().toUtf8(); - imagefailure(); - } - } - } else if (frame->msec() > msec) { - break; - } - - if (options & QDeclarativeViewer::TestImages && !(options & QDeclarativeViewer::Record) && !frame->image().isEmpty()) { - QImage goodImage(frame->image().toLocalFile()); - if (goodImage != img) { - QString reject(frame->image().toLocalFile() + ".reject.png"); - qWarning() << "QDeclarativeTester: Image mismatch. Reject saved to:" - << reject; - img.save(reject); - imagefailure(); - } - } - } else if (QDeclarativeVisualTestMouse *mouse = qobject_cast(event)) { - QPoint pos(mouse->x(), mouse->y()); - QPoint globalPos = m_view->mapToGlobal(QPoint(0, 0)) + pos; - QMouseEvent event((QEvent::Type)mouse->type(), pos, globalPos, (Qt::MouseButton)mouse->button(), (Qt::MouseButtons)mouse->buttons(), (Qt::KeyboardModifiers)mouse->modifiers()); - - MouseEvent me(&event); - me.msec = msec; - if (!mouse->sendToViewport()) { - QCoreApplication::sendEvent(m_view, &event); - me.destination = View; - } else { - QCoreApplication::sendEvent(m_view->viewport(), &event); - me.destination = ViewPort; - } - m_savedMouseEvents.append(me); - } else if (QDeclarativeVisualTestKey *key = qobject_cast(event)) { - - QKeyEvent event((QEvent::Type)key->type(), key->key(), (Qt::KeyboardModifiers)key->modifiers(), QString::fromUtf8(QByteArray::fromHex(key->text().toUtf8())), key->autorep(), key->count()); - - KeyEvent ke(&event); - ke.msec = msec; - if (!key->sendToViewport()) { - QCoreApplication::sendEvent(m_view, &event); - ke.destination = View; - } else { - QCoreApplication::sendEvent(m_view->viewport(), &event); - ke.destination = ViewPort; - } - m_savedKeyEvents.append(ke); - } - testscriptidx++; - } - - filterEvents = true; - - if (testscript && testscript->count() <= testscriptidx) - complete(); -} - -void QDeclarativeTester::registerTypes() -{ - qmlRegisterType("Qt.VisualTest", 4,6, "VisualTest"); - qmlRegisterType("Qt.VisualTest", 4,6, "Frame"); - qmlRegisterType("Qt.VisualTest", 4,6, "Mouse"); - qmlRegisterType("Qt.VisualTest", 4,6, "Key"); -} - -QT_END_NAMESPACE diff --git a/tools/qml/qfxtester.h b/tools/qml/qfxtester.h deleted file mode 100644 index 6521409..0000000 --- a/tools/qml/qfxtester.h +++ /dev/null @@ -1,286 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). -** All rights reserved. -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** This file is part of the tools applications of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** No Commercial Usage -** This file contains pre-release code and may not be distributed. -** You may use this file in accordance with the terms and conditions -** contained in the Technology Preview License Agreement accompanying -** this package. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 2.1 requirements -** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain additional -** rights. These rights are described in the Nokia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** If you have questions regarding the use of this file, please contact -** Nokia at qt-info@nokia.com. -** -** -** -** -** -** -** -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#ifndef QFXTESTER_H -#define QFXTESTER_H - -#include -#include -#include -#include -#include -#include - -QT_BEGIN_NAMESPACE - -class QDeclarativeVisualTest : public QObject -{ - Q_OBJECT - Q_PROPERTY(QDeclarativeListProperty events READ events CONSTANT) - Q_CLASSINFO("DefaultProperty", "events") -public: - QDeclarativeVisualTest() {} - - QDeclarativeListProperty events() { return QDeclarativeListProperty(this, m_events); } - - int count() const { return m_events.count(); } - QObject *event(int idx) { return m_events.at(idx); } - -private: - QList m_events; -}; - -QT_END_NAMESPACE - -QML_DECLARE_TYPE(QDeclarativeVisualTest) - -QT_BEGIN_NAMESPACE - -class QDeclarativeVisualTestFrame : public QObject -{ - Q_OBJECT - Q_PROPERTY(int msec READ msec WRITE setMsec) - Q_PROPERTY(QString hash READ hash WRITE setHash) - Q_PROPERTY(QUrl image READ image WRITE setImage) -public: - QDeclarativeVisualTestFrame() : m_msec(-1) {} - - int msec() const { return m_msec; } - void setMsec(int m) { m_msec = m; } - - QString hash() const { return m_hash; } - void setHash(const QString &hash) { m_hash = hash; } - - QUrl image() const { return m_image; } - void setImage(const QUrl &image) { m_image = image; } - -private: - int m_msec; - QString m_hash; - QUrl m_image; -}; - -QT_END_NAMESPACE - -QML_DECLARE_TYPE(QDeclarativeVisualTestFrame) - -QT_BEGIN_NAMESPACE - -class QDeclarativeVisualTestMouse : public QObject -{ - Q_OBJECT - Q_PROPERTY(int type READ type WRITE setType) - Q_PROPERTY(int button READ button WRITE setButton) - Q_PROPERTY(int buttons READ buttons WRITE setButtons) - Q_PROPERTY(int x READ x WRITE setX) - Q_PROPERTY(int y READ y WRITE setY) - Q_PROPERTY(int modifiers READ modifiers WRITE setModifiers) - Q_PROPERTY(bool sendToViewport READ sendToViewport WRITE setSendToViewport) -public: - QDeclarativeVisualTestMouse() : m_type(0), m_button(0), m_buttons(0), m_x(0), m_y(0), m_modifiers(0), m_viewport(false) {} - - int type() const { return m_type; } - void setType(int t) { m_type = t; } - - int button() const { return m_button; } - void setButton(int b) { m_button = b; } - - int buttons() const { return m_buttons; } - void setButtons(int b) { m_buttons = b; } - - int x() const { return m_x; } - void setX(int x) { m_x = x; } - - int y() const { return m_y; } - void setY(int y) { m_y = y; } - - int modifiers() const { return m_modifiers; } - void setModifiers(int modifiers) { m_modifiers = modifiers; } - - bool sendToViewport() const { return m_viewport; } - void setSendToViewport(bool v) { m_viewport = v; } -private: - int m_type; - int m_button; - int m_buttons; - int m_x; - int m_y; - int m_modifiers; - bool m_viewport; -}; - -QT_END_NAMESPACE - -QML_DECLARE_TYPE(QDeclarativeVisualTestMouse) - -QT_BEGIN_NAMESPACE - -class QDeclarativeVisualTestKey : public QObject -{ - Q_OBJECT - Q_PROPERTY(int type READ type WRITE setType) - Q_PROPERTY(int key READ key WRITE setKey) - Q_PROPERTY(int modifiers READ modifiers WRITE setModifiers) - Q_PROPERTY(QString text READ text WRITE setText) - Q_PROPERTY(bool autorep READ autorep WRITE setAutorep) - Q_PROPERTY(int count READ count WRITE setCount) - Q_PROPERTY(bool sendToViewport READ sendToViewport WRITE setSendToViewport) -public: - QDeclarativeVisualTestKey() : m_type(0), m_key(0), m_modifiers(0), m_autorep(false), m_count(0), m_viewport(false) {} - - int type() const { return m_type; } - void setType(int t) { m_type = t; } - - int key() const { return m_key; } - void setKey(int k) { m_key = k; } - - int modifiers() const { return m_modifiers; } - void setModifiers(int m) { m_modifiers = m; } - - QString text() const { return m_text; } - void setText(const QString &t) { m_text = t; } - - bool autorep() const { return m_autorep; } - void setAutorep(bool a) { m_autorep = a; } - - int count() const { return m_count; } - void setCount(int c) { m_count = c; } - - bool sendToViewport() const { return m_viewport; } - void setSendToViewport(bool v) { m_viewport = v; } -private: - int m_type; - int m_key; - int m_modifiers; - QString m_text; - bool m_autorep; - int m_count; - bool m_viewport; -}; - -QT_END_NAMESPACE - -QML_DECLARE_TYPE(QDeclarativeVisualTestKey) - -QT_BEGIN_NAMESPACE - -class QDeclarativeTester : public QAbstractAnimation -{ -public: - QDeclarativeTester(const QString &script, QDeclarativeViewer::ScriptOptions options, QDeclarativeView *parent); - ~QDeclarativeTester(); - - static void registerTypes(); - - virtual int duration() const; - - void run(); - void save(); - - void executefailure(); -protected: - virtual void updateCurrentTime(int msecs); - virtual bool eventFilter(QObject *, QEvent *); - -private: - QString m_script; - - void imagefailure(); - void complete(); - - enum Destination { View, ViewPort }; - void addKeyEvent(Destination, QKeyEvent *); - void addMouseEvent(Destination, QMouseEvent *); - QDeclarativeView *m_view; - - struct MouseEvent { - MouseEvent(QMouseEvent *e) - : type(e->type()), button(e->button()), buttons(e->buttons()), - pos(e->pos()), modifiers(e->modifiers()), destination(View) {} - - QEvent::Type type; - Qt::MouseButton button; - Qt::MouseButtons buttons; - QPoint pos; - Qt::KeyboardModifiers modifiers; - Destination destination; - - int msec; - }; - struct KeyEvent { - KeyEvent(QKeyEvent *e) - : type(e->type()), key(e->key()), modifiers(e->modifiers()), text(e->text()), - autorep(e->isAutoRepeat()), count(e->count()), destination(View) {} - QEvent::Type type; - int key; - Qt::KeyboardModifiers modifiers; - QString text; - bool autorep; - ushort count; - Destination destination; - - int msec; - }; - struct FrameEvent { - QImage image; - QByteArray hash; - int msec; - }; - QList m_mouseEvents; - QList m_keyEvents; - - QList m_savedMouseEvents; - QList m_savedKeyEvents; - QList m_savedFrameEvents; - bool filterEvents; - - QDeclarativeViewer::ScriptOptions options; - int testscriptidx; - QDeclarativeVisualTest *testscript; - - bool hasCompleted; - bool hasFailed; -}; - - -QT_END_NAMESPACE - -#endif // QFXTESTER_H diff --git a/tools/qml/qml.pro b/tools/qml/qml.pro index a7eb6f5..ba283b6 100644 --- a/tools/qml/qml.pro +++ b/tools/qml/qml.pro @@ -15,13 +15,13 @@ contains(QT_CONFIG, opengl) { # Input HEADERS += qmlruntime.h \ proxysettings.h \ - qfxtester.h \ + qdeclarativetester.h \ deviceorientation.h \ qdeclarativefolderlistmodel.h SOURCES += main.cpp \ qmlruntime.cpp \ proxysettings.cpp \ - qfxtester.cpp \ + qdeclarativetester.cpp \ qdeclarativefolderlistmodel.cpp RESOURCES = qmlruntime.qrc maemo5 { diff --git a/tools/qml/qmlruntime.cpp b/tools/qml/qmlruntime.cpp index 44cab97..1ab528e 100644 --- a/tools/qml/qmlruntime.cpp +++ b/tools/qml/qmlruntime.cpp @@ -95,7 +95,7 @@ #include #endif -#include +#include #if defined (Q_OS_SYMBIAN) #define SYMBIAN_NETWORK_INIT -- cgit v0.12