diff options
author | Konstantin Ritt <ritt.ks@gmail.com> | 2012-10-08 16:40:13 +0300 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2012-10-10 13:24:23 +0200 |
commit | 008e5ba61a68846ad29c65a2fe10b8c19c74f6eb (patch) | |
tree | 59f1bc75adb09b41b862de4145924368529ef95d /tests/auto/gui/image/qimage | |
parent | 34c31cd74cde7a1c36f3603a625fb0d28c259a6e (diff) |
Invalidate old QImage data if load()/loadFromData() has failed
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 <samuel.rodal@digia.com>
Diffstat (limited to 'tests/auto/gui/image/qimage')
-rw-r--r-- | tests/auto/gui/image/qimage/tst_qimage.cpp | 85 |
1 files changed, 85 insertions, 0 deletions
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<int>("format"); |