summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDebao Zhang <dbzhang800@gmail.com>2012-03-08 16:48:17 -0800
committerQt by Nokia <qt-info@nokia.com>2012-03-12 17:33:15 +0100
commitdb390d46c3a983f0270fb40be7f59297e9a77d64 (patch)
tree77135ef6a3c2195f9590e16b5669c632ced8d62f
parent9f266efa8f0962c5769b90e3f79a5658fc9d3332 (diff)
Move the auto test of QPixmap::grabWidget() from QPixmap to QWidget
Change-Id: Id565fa1eb8fe13c62a93a5afa39a5701ce7b20ea QPixmap::grabWidget is deprecated, which calls QWidget::grab() at present. Reviewed-by: Gunnar Sletta <gunnar.sletta@nokia.com> Reviewed-by: Friedemann Kleint <Friedemann.Kleint@nokia.com>
-rw-r--r--tests/auto/gui/image/qpixmap/tst_qpixmap.cpp43
-rw-r--r--tests/auto/widgets/kernel/qwidget/tst_qwidget.cpp75
2 files changed, 75 insertions, 43 deletions
diff --git a/tests/auto/gui/image/qpixmap/tst_qpixmap.cpp b/tests/auto/gui/image/qpixmap/tst_qpixmap.cpp
index 563baef486..c7652e445f 100644
--- a/tests/auto/gui/image/qpixmap/tst_qpixmap.cpp
+++ b/tests/auto/gui/image/qpixmap/tst_qpixmap.cpp
@@ -48,7 +48,6 @@
#include <qmatrix.h>
#include <qdesktopwidget.h>
#include <qpaintengine.h>
-#include <qtreewidget.h>
#include <qsplashscreen.h>
#include <qplatformpixmap_qpa.h>
@@ -111,7 +110,6 @@ private slots:
void setGetMask();
void cacheKey();
void drawBitmap();
- void grabWidget();
void grabWindow();
void isNull();
void task_246446();
@@ -718,47 +716,6 @@ void tst_QPixmap::drawBitmap()
QVERIFY(lenientCompare(pixmap, expected));
}
-void tst_QPixmap::grabWidget()
-{
- for (int opaque = 0; opaque < 2; ++opaque) {
- QWidget widget;
- QImage image(128, 128, opaque ? QImage::Format_RGB32 : QImage::Format_ARGB32_Premultiplied);
- for (int row = 0; row < image.height(); ++row) {
- QRgb *line = reinterpret_cast<QRgb *>(image.scanLine(row));
- for (int col = 0; col < image.width(); ++col)
- line[col] = qRgba(rand() & 255, row, col, opaque ? 255 : 127);
- }
-
- QPalette pal = widget.palette();
- pal.setBrush(QPalette::Window, QBrush(image));
- widget.setPalette(pal);
- widget.resize(128, 128);
-
- QPixmap expected(64, 64);
- if (!opaque)
- expected.fill(Qt::transparent);
-
- QPainter p(&expected);
- p.translate(-64, -64);
- p.drawTiledPixmap(0, 0, 128, 128, pal.brush(QPalette::Window).texture(), 0, 0);
- p.end();
-
- QPixmap actual = QPixmap::grabWidget(&widget, QRect(64, 64, 64, 64));
- QVERIFY(lenientCompare(actual, expected));
-
- actual = QPixmap::grabWidget(&widget, 64, 64);
- QVERIFY(lenientCompare(actual, expected));
-
- // Make sure a widget that is not yet shown is grabbed correctly.
- QTreeWidget widget2;
- actual = QPixmap::grabWidget(&widget2);
- widget2.show();
- expected = QPixmap::grabWidget(&widget2);
-
- QVERIFY(lenientCompare(actual, expected));
- }
-}
-
void tst_QPixmap::grabWindow()
{
// ### fixme: Check platforms
diff --git a/tests/auto/widgets/kernel/qwidget/tst_qwidget.cpp b/tests/auto/widgets/kernel/qwidget/tst_qwidget.cpp
index dab9dd7690..49c93f8f03 100644
--- a/tests/auto/widgets/kernel/qwidget/tst_qwidget.cpp
+++ b/tests/auto/widgets/kernel/qwidget/tst_qwidget.cpp
@@ -68,6 +68,7 @@
#include <QtGui/qguiapplication.h>
#include <qmenubar.h>
#include <qtableview.h>
+#include <qtreewidget.h>
#include <QtWidgets/QGraphicsView>
#include <QtWidgets/QGraphicsProxyWidget>
@@ -412,6 +413,7 @@ private slots:
void taskQTBUG_17333_ResizeInfiniteRecursion();
void nativeChildFocus();
+ void grab();
private:
bool ensureScreenSize(int width, int height);
@@ -9288,5 +9290,78 @@ void tst_QWidget::nativeChildFocus()
QTest::qWait(1000);
}
+static bool lenientCompare(const QPixmap &actual, const QPixmap &expected)
+{
+ QImage expectedImage = expected.toImage().convertToFormat(QImage::Format_RGB32);
+ QImage actualImage = actual.toImage().convertToFormat(QImage::Format_RGB32);
+
+ if (expectedImage.size() != actualImage.size()) {
+ qWarning("Image size comparison failed: expected: %dx%d, got %dx%d",
+ expectedImage.size().width(), expectedImage.size().height(),
+ actualImage.size().width(), actualImage.size().height());
+ return false;
+ }
+
+ const int size = actual.width() * actual.height();
+ const int threshold = QPixmap::defaultDepth() == 16 ? 10 : 2;
+
+ QRgb *a = (QRgb *)actualImage.bits();
+ QRgb *e = (QRgb *)expectedImage.bits();
+ for (int i = 0; i < size; ++i) {
+ const QColor ca(a[i]);
+ const QColor ce(e[i]);
+ if (qAbs(ca.red() - ce.red()) > threshold
+ || qAbs(ca.green() - ce.green()) > threshold
+ || qAbs(ca.blue() - ce.blue()) > threshold) {
+ qWarning("Color mismatch at pixel #%d: Expected: %d,%d,%d, got %d,%d,%d",
+ i, ce.red(), ce.green(), ce.blue(), ca.red(), ca.green(), ca.blue());
+ return false;
+ }
+ }
+
+ return true;
+}
+
+void tst_QWidget::grab()
+{
+ for (int opaque = 0; opaque < 2; ++opaque) {
+ QWidget widget;
+ QImage image(128, 128, opaque ? QImage::Format_RGB32 : QImage::Format_ARGB32_Premultiplied);
+ for (int row = 0; row < image.height(); ++row) {
+ QRgb *line = reinterpret_cast<QRgb *>(image.scanLine(row));
+ for (int col = 0; col < image.width(); ++col)
+ line[col] = qRgba(rand() & 255, row, col, opaque ? 255 : 127);
+ }
+
+ QPalette pal = widget.palette();
+ pal.setBrush(QPalette::Window, QBrush(image));
+ widget.setPalette(pal);
+ widget.resize(128, 128);
+
+ QPixmap expected(64, 64);
+ if (!opaque)
+ expected.fill(Qt::transparent);
+
+ QPainter p(&expected);
+ p.translate(-64, -64);
+ p.drawTiledPixmap(0, 0, 128, 128, pal.brush(QPalette::Window).texture(), 0, 0);
+ p.end();
+
+ QPixmap actual = widget.grab(QRect(64, 64, 64, 64));
+ QVERIFY(lenientCompare(actual, expected));
+
+ actual = widget.grab(QRect(64, 64, -1, -1));
+ QVERIFY(lenientCompare(actual, expected));
+
+ // Make sure a widget that is not yet shown is grabbed correctly.
+ QTreeWidget widget2;
+ actual = widget2.grab(QRect());
+ widget2.show();
+ expected = widget2.grab(QRect());
+
+ QVERIFY(lenientCompare(actual, expected));
+ }
+}
+
QTEST_MAIN(tst_QWidget)
#include "tst_qwidget.moc"