tst_QPixmapCache: check we leak QString keys of evicted pixmaps

Pick-to: 6.6 6.5
Task-number: QTBUG-112200
Change-Id: Icf0a40b68a4ef3ec930a74b47e6ca88d9d0060ca
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
This commit is contained in:
Marc Mutz 2023-06-06 07:13:42 +02:00
parent aa8e8e94b9
commit b74ef9ee48

View File

@ -7,6 +7,8 @@
#include <qpixmapcache.h>
#include "private/qpixmapcache_p.h"
#include <functional>
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<void()> 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<void()> 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: