From e68d06714fa29f986d0fc7de324b79ea94493dfc Mon Sep 17 00:00:00 2001 From: Olivier Goffart Date: Tue, 8 Sep 2015 17:22:01 +0200 Subject: QIconLoader: Use the GTK+ icon caches Loading icons is quite slow because we need to stat many files in many directories. That's why gtk adds a cache in the icon theme directory so it avoids stating lots of files. The cache file can be generated with gtk-update-icon-cache utility on a theme directory. If the cache is not present, corrupted, or outdated, the normal slow lookup is still run. [ChangeLog][QtGui][QIcon] fromTheme gained the ability to use the GTK icon cache to speed up lookups. Change-Id: I3ab8a9910be67a34034556023be61a86789a7893 Reviewed-by: David Faure --- .../image/qicon/icons/themeparent/icon-theme.cache | Bin 0 -> 280 bytes tests/auto/gui/image/qicon/tst_qicon.cpp | 64 +++++++++++++++++++++ tests/auto/gui/image/qicon/tst_qicon.qrc | 1 + 3 files changed, 65 insertions(+) create mode 100644 tests/auto/gui/image/qicon/icons/themeparent/icon-theme.cache (limited to 'tests/auto/gui/image/qicon') diff --git a/tests/auto/gui/image/qicon/icons/themeparent/icon-theme.cache b/tests/auto/gui/image/qicon/icons/themeparent/icon-theme.cache new file mode 100644 index 0000000000..a323875989 Binary files /dev/null and b/tests/auto/gui/image/qicon/icons/themeparent/icon-theme.cache differ diff --git a/tests/auto/gui/image/qicon/tst_qicon.cpp b/tests/auto/gui/image/qicon/tst_qicon.cpp index 9ed3873682..afa72f6922 100644 --- a/tests/auto/gui/image/qicon/tst_qicon.cpp +++ b/tests/auto/gui/image/qicon/tst_qicon.cpp @@ -63,6 +63,7 @@ private slots: void streamAvailableSizes_data(); void streamAvailableSizes(); void fromTheme(); + void fromThemeCache(); #ifndef QT_NO_WIDGETS void task184901_badCache(); @@ -633,6 +634,69 @@ void tst_QIcon::fromTheme() QVERIFY(abIcon.isNull()); } +void tst_QIcon::fromThemeCache() +{ + QTemporaryDir dir; + QVERIFY(QDir().mkpath(dir.path() + QLatin1String("/testcache/16x16/actions"))); + QVERIFY(QFile(QStringLiteral(":/styles/commonstyle/images/standardbutton-open-16.png")) + .copy( dir.path() + QLatin1String("/testcache/16x16/actions/button-open.png"))); + + { + QFile index(dir.path() + QLatin1String("/testcache/index.theme")); + QVERIFY(index.open(QFile::WriteOnly)); + index.write("[Icon Theme]\nDirectories=16x16/actions\n[16x16/actions]\nSize=16\nContext=Actions\nType=Fixed\n"); + } + QIcon::setThemeSearchPaths(QStringList() << dir.path()); + QIcon::setThemeName("testcache"); + + // We just created a theme with that icon, it must exist + QVERIFY(!QIcon::fromTheme("button-open").isNull()); + + QString cacheName = dir.path() + QLatin1String("/testcache/icon-theme.cache"); + + // An invalid cache should not prevent lookup + { + QFile cacheFile(cacheName); + QVERIFY(cacheFile.open(QFile::WriteOnly)); + QDataStream(&cacheFile) << quint16(1) << quint16(0) << "invalid corrupted stuff in there\n"; + } + QIcon::setThemeSearchPaths(QStringList() << dir.path()); // reload themes + QVERIFY(!QIcon::fromTheme("button-open").isNull()); + + // An empty cache should prevent the lookup + { + QFile cacheFile(cacheName); + QVERIFY(cacheFile.open(QFile::WriteOnly)); + QDataStream ds(&cacheFile); + ds << quint16(1) << quint16(0); // 0: version + ds << quint32(12) << quint32(20); // 4: hash offset / dir list offset + ds << quint32(1) << quint32(0xffffffff); // 12: one empty bucket + ds << quint32(1) << quint32(28); // 20: list with one element + ds.writeRawData("16x16/actions", sizeof("16x16/actions")); // 28 + } + QIcon::setThemeSearchPaths(QStringList() << dir.path()); // reload themes + QVERIFY(QIcon::fromTheme("button-open").isNull()); // The icon was not in the cache, it should not be found + + // Adding an icon should be changing the modification date of one sub directory which should make the cache ignored + QTest::qWait(1000); // wait enough to have a different modification time in seconds + QVERIFY(QFile(QStringLiteral(":/styles/commonstyle/images/standardbutton-save-16.png")) + .copy(dir.path() + QLatin1String("/testcache/16x16/actions/button-save.png"))); + QVERIFY(QFileInfo(cacheName).lastModified() < QFileInfo(dir.path() + QLatin1String("/testcache/16x16/actions")).lastModified()); + QIcon::setThemeSearchPaths(QStringList() << dir.path()); // reload themes + QVERIFY(!QIcon::fromTheme("button-open").isNull()); + + // Try to run the actual gtk-update-icon-cache and make sure that icons are still found + QProcess process; + process.start(QStringLiteral("gtk-update-icon-cache"), + QStringList() << QStringLiteral("-f") << QStringLiteral("-t") << (dir.path() + QLatin1String("/testcache"))); + if (!process.waitForFinished()) + QSKIP("gtk-update-icon-cache not run"); + QVERIFY(QFileInfo(cacheName).lastModified() >= QFileInfo(dir.path() + QLatin1String("/testcache/16x16/actions")).lastModified()); + QIcon::setThemeSearchPaths(QStringList() << dir.path()); // reload themes + QVERIFY(!QIcon::fromTheme("button-open").isNull()); + QVERIFY(!QIcon::fromTheme("button-open-fallback").isNull()); + QVERIFY(QIcon::fromTheme("notexist-fallback").isNull()); +} void tst_QIcon::task223279_inconsistentAddFile() { diff --git a/tests/auto/gui/image/qicon/tst_qicon.qrc b/tests/auto/gui/image/qicon/tst_qicon.qrc index 1505ca925b..3c8fbba7c2 100644 --- a/tests/auto/gui/image/qicon/tst_qicon.qrc +++ b/tests/auto/gui/image/qicon/tst_qicon.qrc @@ -15,6 +15,7 @@ ./icons/themeparent/32x32/actions/address-book-new.png ./icons/themeparent/32x32/actions/appointment-new.png ./icons/themeparent/index.theme +./icons/themeparent/icon-theme.cache ./icons/themeparent/scalable/actions/address-book-new.svg ./icons/themeparent/scalable/actions/appointment-new.svg ./styles/commonstyle/images/standardbutton-open-16.png -- cgit v1.2.3