From 008e5ba61a68846ad29c65a2fe10b8c19c74f6eb Mon Sep 17 00:00:00 2001 From: Konstantin Ritt Date: Mon, 8 Oct 2012 16:40:13 +0300 Subject: Invalidate old QImage data if load()/loadFromData() has failed MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This guarantees one will never get `!img.isNull()` after load()/loadFromData() has failed, even if the image was not null before. Apply the same fix to QPixmap and QPicture. Change-Id: Ida1ad6a6f0fc830df8e75ada0c163fc2d3360dea Reviewed-by: Samuel Rødal --- tests/auto/gui/image/qimage/tst_qimage.cpp | 85 ++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) (limited to 'tests/auto/gui/image/qimage') diff --git a/tests/auto/gui/image/qimage/tst_qimage.cpp b/tests/auto/gui/image/qimage/tst_qimage.cpp index cf1727a106..1b0dbf754b 100644 --- a/tests/auto/gui/image/qimage/tst_qimage.cpp +++ b/tests/auto/gui/image/qimage/tst_qimage.cpp @@ -95,6 +95,12 @@ private slots: void copy(); + void load(); + void loadFromData(); +#if !defined(QT_NO_DATASTREAM) + void loadFromDataStream(); +#endif + void setPixel_data(); void setPixel(); @@ -1006,6 +1012,85 @@ void tst_QImage::copy() } } +void tst_QImage::load() +{ + const QString prefix = QFINDTESTDATA("images/"); + if (prefix.isEmpty()) + QFAIL("can not find images directory!"); + const QString filePath = prefix + QLatin1String("image.jpg"); + + QImage dest(filePath); + QVERIFY(!dest.isNull()); + QVERIFY(!dest.load("image_that_does_not_exist.png")); + QVERIFY(dest.isNull()); + QVERIFY(dest.load(filePath)); + QVERIFY(!dest.isNull()); +} + +void tst_QImage::loadFromData() +{ + const QString prefix = QFINDTESTDATA("images/"); + if (prefix.isEmpty()) + QFAIL("can not find images directory!"); + const QString filePath = prefix + QLatin1String("image.jpg"); + + QImage original(filePath); + QVERIFY(!original.isNull()); + + QByteArray ba; + { + QBuffer buf(&ba); + QVERIFY(buf.open(QIODevice::WriteOnly)); + QVERIFY(original.save(&buf, "BMP")); + } + QVERIFY(!ba.isEmpty()); + + QImage dest; + QVERIFY(dest.loadFromData(ba, "BMP")); + QVERIFY(!dest.isNull()); + + QCOMPARE(original, dest); + + QVERIFY(!dest.loadFromData(QByteArray())); + QVERIFY(dest.isNull()); +} + +#if !defined(QT_NO_DATASTREAM) +void tst_QImage::loadFromDataStream() +{ + const QString prefix = QFINDTESTDATA("images/"); + if (prefix.isEmpty()) + QFAIL("can not find images directory!"); + const QString filePath = prefix + QLatin1String("image.jpg"); + + QImage original(filePath); + QVERIFY(!original.isNull()); + + QByteArray ba; + { + QDataStream s(&ba, QIODevice::WriteOnly); + s << original; + } + QVERIFY(!ba.isEmpty()); + + QImage dest; + { + QDataStream s(&ba, QIODevice::ReadOnly); + s >> dest; + } + QVERIFY(!dest.isNull()); + + QCOMPARE(original, dest); + + { + ba.clear(); + QDataStream s(&ba, QIODevice::ReadOnly); + s >> dest; + } + QVERIFY(dest.isNull()); +} +#endif // QT_NO_DATASTREAM + void tst_QImage::setPixel_data() { QTest::addColumn("format"); -- cgit v1.2.3