Add pixel comparation support to qmltest
authorCharles Yin <charles.yin@nokia.com>
Wed, 6 Jun 2012 02:10:32 +0000 (12:10 +1000)
committerQt by Nokia <qt-info@nokia.com>
Tue, 12 Jun 2012 13:40:13 +0000 (15:40 +0200)
Change-Id: Icdee3fab497cc46260bbb9af89f4402fdc027fef
Reviewed-by: Michael Brasser <michael.brasser@nokia.com>
src/imports/testlib/TestCase.qml
src/imports/testlib/testcase.qdoc
src/qmltest/quicktestresult.cpp
src/qmltest/quicktestresult_p.h
tests/auto/qmltest/pixel/tst_pixel.qml [new file with mode: 0644]

index 41e1686..883a864 100644 (file)
@@ -284,6 +284,10 @@ Item {
         }
     }
 
+    function grabImage(item) {
+        return qtest_results.grabImage(item);
+    }
+
     function tryCompare(obj, prop, value, timeout) {
         if (!timeout)
             timeout = 5000
index b7f9a10..469614d 100644 (file)
 */
 
 /*!
+    \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.
index d33eab4..2fbede4 100644 (file)
@@ -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
index 697cfd7..76761b6 100644 (file)
@@ -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[]);
diff --git a/tests/auto/qmltest/pixel/tst_pixel.qml b/tests/auto/qmltest/pixel/tst_pixel.qml
new file mode 100644 (file)
index 0000000..624f084
--- /dev/null
@@ -0,0 +1,74 @@
+/****************************************************************************
+**
+** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/
+**
+** This file is part of the test suite of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** GNU Lesser General Public License Usage
+** 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.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU General
+** Public License version 3.0 as published by the Free Software Foundation
+** and appearing in the file LICENSE.GPL included in the packaging of this
+** file. Please review the following information to ensure the GNU General
+** Public License version 3.0 requirements will be met:
+** http://www.gnu.org/copyleft/gpl.html.
+**
+** Other Usage
+** Alternatively, this file may be used in accordance with the terms and
+** conditions contained in a signed written agreement between you and Nokia.
+**
+**
+**
+**
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+import QtQuick 2.0
+import QtTest 1.0
+
+Rectangle {
+    id:rect
+    width: 40
+    height: 40
+    color:"red"
+    TestCase {
+        name: "Pixels"
+        when: windowShown
+
+        function test_pixel() {
+           wait(200);
+           var img = grabImage(rect);
+           compare(img.pixel(20, 20), Qt.rgba(255, 0, 0, 255));
+           compare(img.red(1,1), 255);
+           compare(img.green(1,1), 0);
+           compare(img.blue(1,1), 0);
+           compare(img.alpha(1,1), 255);
+
+           rect.color = "blue";
+           wait(200);
+           img = grabImage(rect);
+           compare(img.pixel(20, 20), Qt.rgba(0, 0, 255, 255));
+           compare(img.red(1,1), 0);
+           compare(img.green(1,1), 0);
+           compare(img.blue(1,1), 255);
+           compare(img.alpha(1,1), 255);
+        }
+
+    }
+}
\ No newline at end of file