File indexing completed on 2024-09-08 12:18:03
0001 /* 0002 This file is part of the KDE libraries 0003 SPDX-FileCopyrightText: 2016 Aleix Pol Gonzalez 0004 0005 SPDX-License-Identifier: LGPL-2.0-or-later 0006 */ 0007 0008 #include <kiconengine.h> 0009 #include <kiconloader.h> 0010 0011 #include <QStandardPaths> 0012 #include <QTest> 0013 0014 class KIconLoader_Benchmark : public QObject 0015 { 0016 Q_OBJECT 0017 private Q_SLOTS: 0018 0019 void init() 0020 { 0021 #if 0 // Enable this code to benchmark very first startup. 0022 // Starting the application again uses the on-disk cache, so actually benchmarking -with- a cache is more relevant. 0023 // Remove icon cache 0024 const QString cacheFile = QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation) + "/icon-cache.kcache"; 0025 QFile::remove(cacheFile); 0026 0027 // Clear SHM cache 0028 KIconLoader::global()->reconfigure(QString()); 0029 #endif 0030 } 0031 0032 void benchmarkExistingIcons() 0033 { 0034 // icon list I get to load kwrite 0035 static QStringList icons = {QStringLiteral("accessories-text-editor"), 0036 QStringLiteral("bookmarks"), 0037 QStringLiteral("dialog-close"), 0038 QStringLiteral("edit-cut"), 0039 QStringLiteral("edit-paste"), 0040 QStringLiteral("edit-copy"), 0041 QStringLiteral("document-save"), 0042 QStringLiteral("edit-undo"), 0043 QStringLiteral("edit-redo"), 0044 QStringLiteral("code-context"), 0045 QStringLiteral("document-print"), 0046 QStringLiteral("document-print-preview"), 0047 QStringLiteral("view-refresh"), 0048 QStringLiteral("document-save-as"), 0049 QStringLiteral("preferences-other"), 0050 QStringLiteral("edit-select-all"), 0051 QStringLiteral("zoom-in"), 0052 QStringLiteral("zoom-out"), 0053 QStringLiteral("edit-find"), 0054 QStringLiteral("go-down-search"), 0055 QStringLiteral("go-up-search"), 0056 QStringLiteral("tools-check-spelling"), 0057 QStringLiteral("bookmark-new"), 0058 QStringLiteral("format-indent-more"), 0059 QStringLiteral("format-indent-less"), 0060 QStringLiteral("text-plain"), 0061 QStringLiteral("go-up"), 0062 QStringLiteral("go-down"), 0063 QStringLiteral("dialog-ok"), 0064 QStringLiteral("dialog-cancel"), 0065 QStringLiteral("window-close"), 0066 QStringLiteral("document-new"), 0067 QStringLiteral("document-open"), 0068 QStringLiteral("document-open-recent"), 0069 QStringLiteral("window-new"), 0070 QStringLiteral("application-exit"), 0071 QStringLiteral("show-menu"), 0072 QStringLiteral("configure-shortcuts"), 0073 QStringLiteral("configure-toolbars"), 0074 QStringLiteral("help-contents"), 0075 QStringLiteral("help-contextual"), 0076 QStringLiteral("tools-report-bug"), 0077 QStringLiteral("preferences-desktop-locale"), 0078 QStringLiteral("kde")}; 0079 0080 QBENCHMARK { 0081 for (const QString &iconName : std::as_const(icons)) { 0082 const QIcon icon = QIcon::fromTheme(iconName); 0083 if (icon.isNull()) { 0084 QSKIP("missing icons"); 0085 } 0086 QVERIFY(!icon.pixmap(24, 24).isNull()); 0087 // QVERIFY(!icon.pixmap(512, 512).isNull()); 0088 } 0089 } 0090 } 0091 0092 void benchmarkNonExistingIcon_notCached() 0093 { 0094 QBENCHMARK { 0095 // Remove icon cache 0096 const QString cacheFile = QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation) + QStringLiteral("/icon-cache.kcache"); 0097 QFile::remove(cacheFile); 0098 // Clear SHM cache 0099 KIconLoader::global()->reconfigure(QString()); 0100 0101 QIcon icon(new KIconEngine(QStringLiteral("invalid-icon-name"), KIconLoader::global())); 0102 QVERIFY(icon.isNull()); 0103 QVERIFY2(icon.name().isEmpty(), qPrintable(icon.name())); 0104 QVERIFY(!icon.pixmap(QSize(16, 16), QIcon::Normal).isNull()); 0105 } 0106 } 0107 0108 void benchmarkNonExistingIcon_cached() 0109 { 0110 QBENCHMARK { 0111 QIcon icon(new KIconEngine(QStringLiteral("invalid-icon-name"), KIconLoader::global())); 0112 QVERIFY(icon.isNull()); 0113 QVERIFY2(icon.name().isEmpty(), qPrintable(icon.name())); 0114 QVERIFY(!icon.pixmap(QSize(16, 16), QIcon::Normal).isNull()); 0115 } 0116 } 0117 }; 0118 0119 QTEST_MAIN(KIconLoader_Benchmark) 0120 0121 #include "kiconloader_benchmark.moc"