File indexing completed on 2024-10-06 06:40:35

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         QStandardPaths::setTestModeEnabled(true);
0028     }
0029 
0030     void testCenterIcon()
0031     {
0032         QIcon icon(new KIconEngine(QStringLiteral(":/test-22x22.png"), KIconLoader::global()));
0033         QVERIFY(!icon.isNull());
0034 
0035         QWindow w;
0036         QCOMPARE(w.devicePixelRatio(), 2.0);
0037         auto image = icon.pixmap(QSize(22, 22), w.devicePixelRatio()).toImage();
0038         QCOMPARE(image.devicePixelRatio(), 2.0);
0039         QCOMPARE(image.size(), QSize(44, 44));
0040 
0041         QImageReader reader(QStringLiteral(":/test-22x22.png"));
0042         reader.setScaledSize(QSize(44, 44));
0043         QImage unscaled = reader.read();
0044         QVERIFY(!unscaled.isNull());
0045         QCOMPARE(unscaled.size(), QSize(44, 44));
0046         unscaled.setDevicePixelRatio(2.0);
0047         QCOMPARE(image, unscaled);
0048 
0049         // center vertically
0050         QVERIFY(icon.pixmap(QSize(22, 26), w.devicePixelRatio()).toImage().copy(0, 4, 44, 44) == image);
0051         // center horizontally
0052         QVERIFY(icon.pixmap(QSize(26, 22), w.devicePixelRatio()).toImage().copy(4, 0, 44, 44) == image);
0053     }
0054 };
0055 
0056 QTEST_MAIN(KIconEngine_Scaled_UnitTest)
0057 
0058 #include "kiconengine_scaled_unittest.moc"