File indexing completed on 2024-12-01 12:32:57

0001 /*
0002     SPDX-FileCopyrightText: 2013 Martin Klapetek <mklapetek@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005  */
0006 
0007 #include "kiconutilstest.h"
0008 #include <kiconutils.h>
0009 
0010 #include <QIcon>
0011 #include <QPainter>
0012 #include <QPixmap>
0013 #include <QTest>
0014 
0015 QTEST_MAIN(KIconUtilsTest)
0016 
0017 void KIconUtilsTest::addOverlayTest()
0018 {
0019     QPixmap rectanglePixmap(32, 32);
0020     rectanglePixmap.fill(Qt::red);
0021 
0022     QIcon icon(rectanglePixmap);
0023 
0024     QPixmap overlay(32, 32);
0025     overlay.fill(Qt::blue);
0026 
0027     QIcon overlayIcon(overlay);
0028 
0029     QIcon iconWithOverlay = KIconUtils::addOverlay(icon, overlayIcon, Qt::BottomRightCorner);
0030     QImage result = iconWithOverlay.pixmap(32, 32).toImage();
0031 
0032     int bluePixels = 0;
0033     int redPixels = 0;
0034 
0035     // Go over the image and count red and blue pixels
0036     for (int y = 0; y < result.height(); ++y) {
0037         for (int x = 0; x < result.width(); ++x) {
0038             if (qRed(result.pixel(x, y)) == 255) {
0039                 redPixels++;
0040             } else if (qBlue(result.pixel(x, y)) == 255) {
0041                 bluePixels++;
0042             }
0043         }
0044     }
0045 
0046     // For icon of size 32x32, the overlay should be 16x16 (=256)
0047     QCOMPARE(bluePixels, 256);
0048     QCOMPARE(redPixels, 768);
0049 
0050     // Try different size and position
0051     rectanglePixmap = rectanglePixmap.scaled(96, 96);
0052 
0053     icon = QIcon(rectanglePixmap);
0054 
0055     overlay = overlay.scaled(96, 96);
0056 
0057     overlayIcon = QIcon(overlay);
0058 
0059     iconWithOverlay = KIconUtils::addOverlay(icon, overlayIcon, Qt::BottomRightCorner);
0060 
0061     // Test if unsetting the overlay works;
0062     // the result should have just one blue square
0063     iconWithOverlay = KIconUtils::addOverlay(icon, QIcon(), Qt::BottomRightCorner);
0064 
0065     iconWithOverlay = KIconUtils::addOverlay(icon, overlayIcon, Qt::TopLeftCorner);
0066     result = iconWithOverlay.pixmap(96, 96).toImage();
0067 
0068     bluePixels = 0;
0069     redPixels = 0;
0070 
0071     for (int y = 0; y < result.height(); ++y) {
0072         for (int x = 0; x < result.width(); ++x) {
0073             if (qRed(result.pixel(x, y)) == 255) {
0074                 redPixels++;
0075             } else if (qBlue(result.pixel(x, y)) == 255) {
0076                 bluePixels++;
0077             }
0078         }
0079     }
0080 
0081     // 96x96 big icon will have 32x32 big overlay (=1024 blue pixels)
0082     QCOMPARE(bluePixels, 1024);
0083     QCOMPARE(redPixels, 8192);
0084 
0085     // Try paint method
0086     icon = QIcon(rectanglePixmap);
0087     iconWithOverlay = KIconUtils::addOverlay(icon, overlayIcon, Qt::TopLeftCorner);
0088 
0089     QPixmap a(96, 96);
0090     QPainter p(&a);
0091     iconWithOverlay.paint(&p, a.rect(), Qt::AlignCenter, QIcon::Normal, QIcon::Off);
0092 
0093     result = a.toImage();
0094 
0095     bluePixels = 0;
0096     redPixels = 0;
0097 
0098     for (int y = 0; y < result.height(); ++y) {
0099         for (int x = 0; x < result.width(); ++x) {
0100             if (qRed(result.pixel(x, y)) == 255) {
0101                 redPixels++;
0102             } else if (qBlue(result.pixel(x, y)) == 255) {
0103                 bluePixels++;
0104             }
0105         }
0106     }
0107 
0108     // 96x96 big icon will have 32x32 big overlay (=1024 blue pixels)
0109     QCOMPARE(bluePixels, 1024);
0110     QCOMPARE(redPixels, 8192);
0111 }
0112 
0113 void KIconUtilsTest::addOverlaysTest()
0114 {
0115     QPixmap rectanglePixmap(32, 32);
0116     rectanglePixmap.fill(Qt::red);
0117 
0118     QIcon icon(rectanglePixmap);
0119 
0120     QPixmap overlay(32, 32);
0121     overlay.fill(Qt::blue);
0122 
0123     QIcon overlayIcon(overlay);
0124 
0125     QHash<Qt::Corner, QIcon> overlays;
0126     overlays.insert(Qt::BottomRightCorner, overlayIcon);
0127     overlays.insert(Qt::TopLeftCorner, overlayIcon);
0128 
0129     QIcon iconWithOverlay = KIconUtils::addOverlays(icon, overlays);
0130     QImage result = iconWithOverlay.pixmap(32, 32).toImage();
0131 
0132     int bluePixels = 0;
0133     int redPixels = 0;
0134 
0135     // Go over the image and count red and blue pixels
0136     for (int y = 0; y < result.height(); ++y) {
0137         for (int x = 0; x < result.width(); ++x) {
0138             if (qRed(result.pixel(x, y)) == 255) {
0139                 redPixels++;
0140             } else if (qBlue(result.pixel(x, y)) == 255) {
0141                 bluePixels++;
0142             }
0143         }
0144     }
0145 
0146     // Two blue overlays in icon size 32x32 would intersect with 16 pixels,
0147     // so the amount of blue pixels should be 2x256-16 = 496
0148     QCOMPARE(bluePixels, 496);
0149     QCOMPARE(redPixels, 528);
0150 
0151     // Try different size
0152 
0153     rectanglePixmap = rectanglePixmap.scaled(96, 96);
0154     icon = QIcon(rectanglePixmap);
0155     overlay = overlay.scaled(96, 96);
0156     overlayIcon = QIcon(overlay);
0157 
0158     // Clear the old sizes first
0159     overlays.clear();
0160     overlays.insert(Qt::BottomRightCorner, overlayIcon);
0161     overlays.insert(Qt::TopRightCorner, overlayIcon);
0162     overlays.insert(Qt::TopLeftCorner, overlayIcon);
0163 
0164     // Now it will have 3 overlays
0165     iconWithOverlay = KIconUtils::addOverlays(icon, overlays);
0166 
0167     QPixmap a(96, 96);
0168     QPainter p(&a);
0169     iconWithOverlay.paint(&p, a.rect(), Qt::AlignCenter, QIcon::Normal, QIcon::Off);
0170 
0171     result = a.toImage();
0172 
0173     bluePixels = 0;
0174     redPixels = 0;
0175 
0176     for (int y = 0; y < result.height(); ++y) {
0177         for (int x = 0; x < result.width(); ++x) {
0178             if (qRed(result.pixel(x, y)) == 255) {
0179                 redPixels++;
0180             } else if (qBlue(result.pixel(x, y)) == 255) {
0181                 bluePixels++;
0182             }
0183         }
0184     }
0185 
0186     // 96x96 big icon will have 32x32 big overlays (=3072 blue pixels)
0187     QCOMPARE(bluePixels, 3072);
0188     QCOMPARE(redPixels, 6144);
0189 }
0190 
0191 #include "moc_kiconutilstest.cpp"