diff --git a/autotests/CMakeLists.txt b/autotests/CMakeLists.txt --- a/autotests/CMakeLists.txt +++ b/autotests/CMakeLists.txt @@ -36,5 +36,6 @@ endforeach() ecm_add_test(klanguagenametest.cpp LINK_LIBRARIES Qt5::Test KF5::ConfigWidgets) +ecm_add_test(kcolorschemetest.cpp LINK_LIBRARIES Qt5::Test KF5::ConfigWidgets) ecm_add_test(krecentfilesactiontest.cpp TEST_NAME "krecentfilesaction_test" LINK_LIBRARIES Qt5::Test KF5::ConfigWidgets) diff --git a/autotests/kcolorschemetest.cpp b/autotests/kcolorschemetest.cpp new file mode 100644 --- /dev/null +++ b/autotests/kcolorschemetest.cpp @@ -0,0 +1,58 @@ +/* + Copyright (c) 2019 Milian Wolff + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) version 3, or any + later version accepted by the membership of KDE e.V. (or its + successor approved by the membership of KDE e.V.), which shall + act as a proxy defined in Section 6 of version 3 of the license. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library. If not, see . +*/ + +#include +#include + +#include "kcolorscheme.h" +#include "kcolorschememanager.h" + +class KColorSchemeTest : public QObject +{ + Q_OBJECT + +private Q_SLOTS: + void benchConstruction_data() + { + KColorSchemeManager manager; + QVERIFY(manager.model()->rowCount() > 0); + const auto anyScheme = manager.model()->index(0, 0).data(Qt::UserRole).toString(); + QVERIFY(QFile::exists(anyScheme)); + + QTest::addColumn("file"); + + QTest::newRow("default") << QString(); + QTest::newRow("explicit") << anyScheme; + } + + void benchConstruction() + { + QFETCH(QString, file); + qApp->setProperty("KDE_COLOR_SCHEME_PATH", file); + + QBENCHMARK { + KColorScheme scheme(QPalette::Active); + } + } +}; + +QTEST_MAIN(KColorSchemeTest) + +#include "kcolorschemetest.moc" diff --git a/src/kcolorscheme.cpp b/src/kcolorscheme.cpp --- a/src/kcolorscheme.cpp +++ b/src/kcolorscheme.cpp @@ -252,9 +252,15 @@ //END default colors KSharedConfigPtr defaultConfig() { + // cache the value we'll return, since usually it's going to be the same value + static thread_local KSharedConfigPtr config; // Read from the application's color scheme file (as set by KColorSchemeManager). // If unset, this is equivalent to openConfig() and the system scheme is used. - return KSharedConfig::openConfig(qApp->property("KDE_COLOR_SCHEME_PATH").toString()); + const auto colorSchemePath = qApp->property("KDE_COLOR_SCHEME_PATH").toString(); + if (!config || config->name() != colorSchemePath) { + config = KSharedConfig::openConfig(colorSchemePath); + } + return config; } //BEGIN KColorSchemePrivate