From e409d771d922b9772a0ecb4da575c6fd6f95676f 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. Pick-to: 6.6 6.5 6.2 5.15 Change-Id: I74f23d2ec4c04151f8f1266c0c503713d4642f3a Reviewed-by: Qt CI Bot Reviewed-by: Volker Hilsheimer --- .../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 407117c75b..84b4831768 100644 --- a/tests/auto/gui/image/qpixmapcache/tst_qpixmapcache.cpp +++ b/tests/auto/gui/image/qpixmapcache/tst_qpixmapcache.cpp @@ -31,6 +31,7 @@ private slots: void setCacheLimit(); void find(); void insert(); + void failedInsertReturnsInvalidKey(); void replace(); void remove(); void clear(); @@ -269,6 +270,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; @@ -282,6 +284,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); + QCOMPARE_GT(pm.width() * pm.height() * pm.depth() / 8, + QPixmapCache::cacheLimit() * 1024); + + // + // WHEN: trying to add this pixmap to the cache + // + const auto success = QPixmapCache::insert(u"foo"_s, pm); // QString API + { QPixmap r; QVERIFY(!QPixmapCache::find(u"foo"_s, &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