diff --git a/tests/auto/gui/image/qpixmapcache/tst_qpixmapcache.cpp b/tests/auto/gui/image/qpixmapcache/tst_qpixmapcache.cpp index 27aac5e5176..99eeadb994c 100644 --- a/tests/auto/gui/image/qpixmapcache/tst_qpixmapcache.cpp +++ b/tests/auto/gui/image/qpixmapcache/tst_qpixmapcache.cpp @@ -7,6 +7,8 @@ #include #include "private/qpixmapcache_p.h" +#include + QT_BEGIN_NAMESPACE // The test requires QT_BUILD_INTERNAL Q_AUTOTEST_EXPORT void qt_qpixmapcache_flush_detached_pixmaps(); Q_AUTOTEST_EXPORT int qt_qpixmapcache_qpixmapcache_total_used(); @@ -40,8 +42,13 @@ private slots: void pixmapKey(); void noLeak(); void clearDoesNotLeakStringKeys(); + void evictionDoesNotLeakStringKeys(); + void reducingCacheLimitDoesNotLeakStringKeys(); void strictCacheLimit(); void noCrashOnLargeInsert(); + +private: + void stringLeak_impl(std::function whenOp); }; static QPixmapCache::KeyData* getPrivate(QPixmapCache::Key &key) @@ -522,6 +529,35 @@ void tst_QPixmapCache::noLeak() void tst_QPixmapCache::clearDoesNotLeakStringKeys() { + stringLeak_impl([] { QPixmapCache::clear(); }); +} + +void tst_QPixmapCache::evictionDoesNotLeakStringKeys() +{ + stringLeak_impl([] { + // fill the cache with other pixmaps to force eviction of "our" pixmap: + constexpr int Iterations = 10; + for (int i = 0; i < Iterations; ++i) { + QPixmap pm(64, 64); + pm.fill(Qt::transparent); + [[maybe_unused]] auto r = QPixmapCache::insert(pm); + } + QEXPECT_FAIL("", "QTBUG-112200", Continue); + }); +} + +void tst_QPixmapCache::reducingCacheLimitDoesNotLeakStringKeys() +{ + stringLeak_impl([] { + QPixmapCache::setCacheLimit(0); + QEXPECT_FAIL("", "QTBUG-112200", Continue); + }); +} + +void tst_QPixmapCache::stringLeak_impl(std::function whenOp) +{ + QVERIFY(whenOp); + QPixmapCache::setCacheLimit(20); // 20KiB // // GIVEN: a QPixmap with QString key `key` in QPixmapCache @@ -539,9 +575,11 @@ void tst_QPixmapCache::clearDoesNotLeakStringKeys() QVERIFY(!key.isDetached()); // was saved inside QPixmapCache // - // WHEN: clearing the cache: + // WHEN: performing the given operation // - QPixmapCache::clear(); + whenOp(); + if (QTest::currentTestFailed()) + return; // // THEN: `key` is no longer referenced by QPixmapCache: