From 41e8b0e0d990f34913449de6456a13371f4f9297 Mon Sep 17 00:00:00 2001 From: Charles Yin Date: Wed, 6 Jun 2012 12:10:32 +1000 Subject: Add pixel comparation support to qmltest Change-Id: Icdee3fab497cc46260bbb9af89f4402fdc027fef Reviewed-by: Michael Brasser --- src/qmltest/quicktestresult.cpp | 66 +++++++++++++++++++++++++++++++++++++++++ src/qmltest/quicktestresult_p.h | 3 ++ 2 files changed, 69 insertions(+) (limited to 'src/qmltest') 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 #include #include +#include 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().red(); + } + + int green(int x, int y) const + { + return pixel(x, y).value().green(); + } + + int blue(int x, int y) const + { + return pixel(x, y).value().blue(); + } + + int alpha(int x, int y) const + { + return pixel(x, y).value().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(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 #include #include +#include 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[]); -- cgit v1.2.3