File indexing completed on 2024-09-08 12:18:03
0001 /* 0002 SPDX-FileCopyrightText: 2021 Volker Krause <vkrause@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include <QImageReader> 0008 #include <QStandardPaths> 0009 #include <QTest> 0010 0011 #include <KIconEngine> 0012 #include <KIconLoader> 0013 0014 static int setupEnvBeforeQApplication = []() { 0015 qunsetenv("QT_SCREEN_SCALE_FACTORS"); 0016 qputenv("QT_SCALE_FACTOR", "2.0"); 0017 return 1; 0018 }(); 0019 0020 class KIconEngine_Scaled_UnitTest : public QObject 0021 { 0022 Q_OBJECT 0023 0024 private Q_SLOTS: 0025 void initTestCase() 0026 { 0027 QGuiApplication::setAttribute(Qt::AA_UseHighDpiPixmaps); 0028 QStandardPaths::setTestModeEnabled(true); 0029 } 0030 0031 void testCenterIcon() 0032 { 0033 QIcon icon(new KIconEngine(QStringLiteral(":/test-22x22.png"), KIconLoader::global())); 0034 QVERIFY(!icon.isNull()); 0035 0036 QWindow w; 0037 QCOMPARE(w.devicePixelRatio(), 2.0); 0038 auto image = icon.pixmap(&w, QSize(22, 22)).toImage(); 0039 QCOMPARE(image.devicePixelRatio(), 2.0); 0040 QCOMPARE(image.size(), QSize(44, 44)); 0041 0042 QImageReader reader(QStringLiteral(":/test-22x22.png")); 0043 reader.setScaledSize(QSize(44, 44)); 0044 QImage unscaled = reader.read(); 0045 QVERIFY(!unscaled.isNull()); 0046 QCOMPARE(unscaled.size(), QSize(44, 44)); 0047 unscaled.setDevicePixelRatio(2.0); 0048 QCOMPARE(image, unscaled); 0049 0050 // center vertically 0051 QVERIFY(icon.pixmap(&w, QSize(22, 26)).toImage().copy(0, 4, 44, 44) == image); 0052 // center horizontally 0053 QVERIFY(icon.pixmap(&w, QSize(26, 22)).toImage().copy(4, 0, 44, 44) == image); 0054 } 0055 }; 0056 0057 QTEST_MAIN(KIconEngine_Scaled_UnitTest) 0058 0059 #include "kiconengine_scaled_unittest.moc"