aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorCharles Yin <charles.yin@nokia.com>2012-06-06 12:10:32 +1000
committerQt by Nokia <qt-info@nokia.com>2012-06-12 15:40:13 +0200
commit41e8b0e0d990f34913449de6456a13371f4f9297 (patch)
tree9a99a253de7b37f8ba191568fa64d853e2887492 /src
parent7e4d6efadf71d6c8ec5d06cdffcee117f01c6160 (diff)
Add pixel comparation support to qmltest
Change-Id: Icdee3fab497cc46260bbb9af89f4402fdc027fef Reviewed-by: Michael Brasser <michael.brasser@nokia.com>
Diffstat (limited to 'src')
-rw-r--r--src/imports/testlib/TestCase.qml4
-rw-r--r--src/imports/testlib/testcase.qdoc25
-rw-r--r--src/qmltest/quicktestresult.cpp66
-rw-r--r--src/qmltest/quicktestresult_p.h3
4 files changed, 98 insertions, 0 deletions
diff --git a/src/imports/testlib/TestCase.qml b/src/imports/testlib/TestCase.qml
index 41e1686a27..883a864e59 100644
--- a/src/imports/testlib/TestCase.qml
+++ b/src/imports/testlib/TestCase.qml
@@ -284,6 +284,10 @@ Item {
}
}
+ function grabImage(item) {
+ return qtest_results.grabImage(item);
+ }
+
function tryCompare(obj, prop, value, timeout) {
if (!timeout)
timeout = 5000
diff --git a/src/imports/testlib/testcase.qdoc b/src/imports/testlib/testcase.qdoc
index b7f9a10703..469614da6d 100644
--- a/src/imports/testlib/testcase.qdoc
+++ b/src/imports/testlib/testcase.qdoc
@@ -374,6 +374,31 @@
*/
/*!
+ \qmlmethod object TestCase::grabImage(item)
+
+ Returns a snapshot image object of the given \a item.
+
+ The returned image object has the following methods:
+ \list
+ \li red(x, y) Returns the red channel value of the pixel at \a x, \a y position
+ \li green(x, y) Returns the green channel value of the pixel at \a x, \a y position
+ \li blue(x, y) Returns the blue channel value of the pixel at \a x, \a y position
+ \li alpha(x, y) Returns the alpha channel value of the pixel at \a x, \a y position
+ \li pixel(x, y) Returns the color value of the pixel at \a x, \a y position
+ For example:
+
+ \code
+ var image = grabImage(rect);
+ compare(image.red(10, 10), 255);
+ compare(image.pixel(20, 20), Qt.rgba(255, 0, 0, 255);
+ \endcode
+
+ \endlist
+
+ \sa
+*/
+
+/*!
\qmlmethod TestCase::skip(message = "")
Skips the current test case and prints the optional \a message.
diff --git a/src/qmltest/quicktestresult.cpp b/src/qmltest/quicktestresult.cpp
index d33eab4943..2fbede45d7 100644
--- a/src/qmltest/quicktestresult.cpp
+++ b/src/qmltest/quicktestresult.cpp
@@ -55,6 +55,7 @@
#include <QtCore/qdebug.h>
#include <QtCore/QUrl>
#include <QtCore/QDir>
+#include <QtQuick/qquickcanvas.h>
QT_BEGIN_NAMESPACE
@@ -62,6 +63,57 @@ static const char *globalProgramName = 0;
static bool loggingStarted = false;
static QBenchmarkGlobalData globalBenchmarkData;
+class Q_QUICK_TEST_EXPORT QuickTestImageObject : public QObject
+{
+ Q_OBJECT
+public:
+ QuickTestImageObject(const QImage& img, QObject *parent = 0)
+ : QObject(parent)
+ , m_image(img)
+ {
+ }
+
+ ~QuickTestImageObject() {}
+
+public Q_SLOTS:
+ int red(int x, int y) const
+ {
+ return pixel(x, y).value<QColor>().red();
+ }
+
+ int green(int x, int y) const
+ {
+ return pixel(x, y).value<QColor>().green();
+ }
+
+ int blue(int x, int y) const
+ {
+ return pixel(x, y).value<QColor>().blue();
+ }
+
+ int alpha(int x, int y) const
+ {
+ return pixel(x, y).value<QColor>().alpha();
+ }
+
+ QVariant pixel(int x, int y) const
+ {
+ if (m_image.isNull()
+ || x >= m_image.width()
+ || y >= m_image.height()
+ || x < 0
+ || y < 0
+ || x * y >= m_image.width() * m_image.height())
+ return QVariant();
+
+ const QRgb* pixel = reinterpret_cast<const QRgb*>(m_image.constScanLine(y));
+ pixel += x;
+ return QColor::fromRgba(*pixel);
+ }
+private:
+ QImage m_image;
+};
+
class QuickTestResultPrivate
{
public:
@@ -534,6 +586,18 @@ void QuickTestResult::stopBenchmark()
d->benchmarkIter = 0;
}
+QObject *QuickTestResult::grabImage(QQuickItem *item)
+{
+ Q_D(QuickTestResult);
+ if (item) {
+ QQuickCanvas *canvas = item->canvas();
+ QImage grabbed = canvas->grabFrameBuffer();
+ 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()));
+ }
+ return 0;
+}
namespace QTest {
void qtest_qParseArgs(int argc, char *argv[], bool qml);
};
@@ -574,4 +638,6 @@ int QuickTestResult::exitCode()
#endif
}
+#include "quicktestresult.moc"
+
QT_END_NAMESPACE
diff --git a/src/qmltest/quicktestresult_p.h b/src/qmltest/quicktestresult_p.h
index 697cfd7c89..76761b68ed 100644
--- a/src/qmltest/quicktestresult_p.h
+++ b/src/qmltest/quicktestresult_p.h
@@ -47,6 +47,7 @@
#include <QtCore/qstring.h>
#include <QtCore/qstringlist.h>
#include <QtCore/qscopedpointer.h>
+#include <QtQuick/qquickitem.h>
QT_BEGIN_NAMESPACE
@@ -139,6 +140,8 @@ public Q_SLOTS:
void nextBenchmark();
void stopBenchmark();
+ QObject *grabImage(QQuickItem *item);
+
public:
// Helper functions for the C++ main() shell.
static void parseArgs(int argc, char *argv[]);