diff options
author | Robin Burchell <robin.burchell@crimson.no> | 2017-04-22 19:22:48 +0200 |
---|---|---|
committer | Robin Burchell <robin.burchell@crimson.no> | 2017-05-11 07:31:12 +0000 |
commit | a9339e3ed4d2db11e75ed6c77fbe420708b44712 (patch) | |
tree | e30a558444933c1b4e017b7e0fa6c1b86698595c /src/qmltest | |
parent | cef5e9d385d0cf78e43e8ea41edf253b78f0ca13 (diff) |
TestCase: Grow some new functionality on grabImage
Add some meta-information about the image that we can use to do slightly
stronger checks than just blindly poking pixel data (width & height
props).
In addition, when a test of a grabbed image fails (which they certainly do),
it is always nice if we can perform some diagnostics. To this end, add a
save() method to save the grabbed QImage to disk.
Tests for the new functionality are blacklisted on Linux at present, as they do
not yet appear stable, inheriting the other tst_grabImage flakiness.
[ChangeLog][QmlTest] The returned object from TestCase::grabImage now has
'width', 'height', and 'size' properties for additional verification. In
addition, there is a save() method to be able to persist the grabbed
image to disk (for diagnostics purposes, for example).
Change-Id: Ic651e0257102c514c39c3f648c05870f9d9c52e8
Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
Diffstat (limited to 'src/qmltest')
-rw-r--r-- | src/qmltest/quicktestresult.cpp | 40 |
1 files changed, 39 insertions, 1 deletions
diff --git a/src/qmltest/quicktestresult.cpp b/src/qmltest/quicktestresult.cpp index dc6caf505b..3650df8f3a 100644 --- a/src/qmltest/quicktestresult.cpp +++ b/src/qmltest/quicktestresult.cpp @@ -1,5 +1,6 @@ /**************************************************************************** ** +** Copyright (C) 2017 Crimson AS <info@crimson.no> ** Copyright (C) 2016 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** @@ -61,7 +62,10 @@ #include <QtCore/QDir> #include <QtQuick/qquickwindow.h> #include <QtGui/qvector3d.h> +#include <QtGui/qimagewriter.h> #include <QtQml/private/qqmlglobal_p.h> +#include <QtQml/QQmlEngine> +#include <QtQml/QQmlContext> #include <private/qv4qobjectwrapper_p.h> #include <algorithm> @@ -77,6 +81,11 @@ extern bool qWaitForSignal(QObject *obj, const char* signal, int timeout = 5000) class Q_QUICK_TEST_EXPORT QuickTestImageObject : public QObject { Q_OBJECT + + Q_PROPERTY(int width READ width CONSTANT) + Q_PROPERTY(int height READ height CONSTANT) + Q_PROPERTY(QSize size READ size CONSTANT) + public: QuickTestImageObject(const QImage& img, QObject *parent = 0) : QObject(parent) @@ -127,6 +136,33 @@ public Q_SLOTS: return m_image == other->m_image; } + + void save(const QString &filePath) + { + QImageWriter writer(filePath); + if (!writer.write(m_image)) { + QQmlEngine *engine = qmlContext(this)->engine(); + QV4::ExecutionEngine *v4 = QV8Engine::getV4(engine->handle()); + v4->throwError(QStringLiteral("Can't save to %1: %2").arg(filePath, writer.errorString())); + } + } + +public: + int width() const + { + return m_image.width(); + } + + int height() const + { + return m_image.height(); + } + + QSize size() const + { + return m_image.size(); + } + private: QImage m_image; }; @@ -706,7 +742,9 @@ QObject *QuickTestResult::grabImage(QQuickItem *item) QImage grabbed = window->grabWindow(); QRectF rf(item->x(), item->y(), item->width(), item->height()); rf = rf.intersected(QRectF(0, 0, grabbed.width(), grabbed.height())); - return new QuickTestImageObject(grabbed.copy(rf.toAlignedRect())); + QObject *o = new QuickTestImageObject(grabbed.copy(rf.toAlignedRect())); + QQmlEngine::setContextForObject(o, qmlContext(this)); + return o; } return 0; } |