From 75f33bb43cb65c5c2893a5ab49ed47cc73e3350c Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Wed, 7 Jun 2023 07:15:30 +0200 Subject: [PATCH] tst_QPixmapCache: check insert() reports failure None of the existing tests failed when I started to return a valid key from a failed insert(QPixmap), so add a test that would fail. Manual conflict resolutions: - QCOMPARE_GT() -> QVERIFY( > ) - u""_s -> QStringLiteral Change-Id: I74f23d2ec4c04151f8f1266c0c503713d4642f3a Reviewed-by: Qt CI Bot Reviewed-by: Volker Hilsheimer (cherry picked from commit e409d771d922b9772a0ecb4da575c6fd6f95676f) --- .../image/qpixmapcache/tst_qpixmapcache.cpp | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/tests/auto/gui/image/qpixmapcache/tst_qpixmapcache.cpp b/tests/auto/gui/image/qpixmapcache/tst_qpixmapcache.cpp index 227e7b60af1..b161f9f9063 100644 --- a/tests/auto/gui/image/qpixmapcache/tst_qpixmapcache.cpp +++ b/tests/auto/gui/image/qpixmapcache/tst_qpixmapcache.cpp @@ -50,6 +50,7 @@ private slots: void setCacheLimit(); void find(); void insert(); + void failedInsertReturnsInvalidKey(); void replace(); void remove(); void clear(); @@ -318,6 +319,7 @@ void tst_QPixmapCache::insert() for (int i = 0; i < numberOfKeys; ++i) { QPixmap p3(10,10); keys.append(QPixmapCache::insert(p3)); + QVERIFY(keys.back().isValid()); } num = 0; @@ -331,6 +333,32 @@ void tst_QPixmapCache::insert() QVERIFY(num <= estimatedNum); } +void tst_QPixmapCache::failedInsertReturnsInvalidKey() +{ + // + // GIVEN: a pixmap whose memory footprint exceeds the cache's limit: + // + QPixmapCache::setCacheLimit(20); + + QPixmap pm(256, 256); + pm.fill(Qt::transparent); + QVERIFY(pm.width() * pm.height() * pm.depth() / 8 + > QPixmapCache::cacheLimit() * 1024); + + // + // WHEN: trying to add this pixmap to the cache + // + const auto success = QPixmapCache::insert(QStringLiteral("foo"), pm); // QString API + { QPixmap r; QVERIFY(!QPixmapCache::find(QStringLiteral("foo"), &r)); } + const auto key = QPixmapCache::insert(pm); // "int" API + + // + // THEN: failure is reported to the user + // + QVERIFY(!key.isValid()); // "int" API + QVERIFY(!success); // QString API +} + void tst_QPixmapCache::replace() { //The int part of the API