1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
|
/*
SPDX-FileCopyrightText: 2013 Martin Klapetek <mklapetek@kde.org>
SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
*/
#include "kiconutilstest.h"
#include <kiconutils.h>
#include <QIcon>
#include <QPainter>
#include <QPixmap>
#include <QTest>
QTEST_MAIN(KIconUtilsTest)
void KIconUtilsTest::addOverlayTest()
{
QPixmap rectanglePixmap(32, 32);
rectanglePixmap.fill(Qt::red);
QIcon icon(rectanglePixmap);
QPixmap overlay(32, 32);
overlay.fill(Qt::blue);
QIcon overlayIcon(overlay);
QIcon iconWithOverlay = KIconUtils::addOverlay(icon, overlayIcon, Qt::BottomRightCorner);
QImage result = iconWithOverlay.pixmap(32, 32).toImage();
int bluePixels = 0;
int redPixels = 0;
// Go over the image and count red and blue pixels
for (int y = 0; y < result.height(); ++y) {
for (int x = 0; x < result.width(); ++x) {
if (qRed(result.pixel(x, y)) == 255) {
redPixels++;
} else if (qBlue(result.pixel(x, y)) == 255) {
bluePixels++;
}
}
}
// For icon of size 32x32, the overlay should be 16x16 (=256)
QCOMPARE(bluePixels, 256);
QCOMPARE(redPixels, 768);
// Try different size and position
rectanglePixmap = rectanglePixmap.scaled(96, 96);
icon = QIcon(rectanglePixmap);
overlay = overlay.scaled(96, 96);
overlayIcon = QIcon(overlay);
iconWithOverlay = KIconUtils::addOverlay(icon, overlayIcon, Qt::BottomRightCorner);
// Test if unsetting the overlay works;
// the result should have just one blue square
iconWithOverlay = KIconUtils::addOverlay(icon, QIcon(), Qt::BottomRightCorner);
iconWithOverlay = KIconUtils::addOverlay(icon, overlayIcon, Qt::TopLeftCorner);
result = iconWithOverlay.pixmap(96, 96).toImage();
bluePixels = 0;
redPixels = 0;
for (int y = 0; y < result.height(); ++y) {
for (int x = 0; x < result.width(); ++x) {
if (qRed(result.pixel(x, y)) == 255) {
redPixels++;
} else if (qBlue(result.pixel(x, y)) == 255) {
bluePixels++;
}
}
}
// 96x96 big icon will have 32x32 big overlay (=1024 blue pixels)
QCOMPARE(bluePixels, 1024);
QCOMPARE(redPixels, 8192);
// Try paint method
icon = QIcon(rectanglePixmap);
iconWithOverlay = KIconUtils::addOverlay(icon, overlayIcon, Qt::TopLeftCorner);
QPixmap a(96, 96);
QPainter p(&a);
iconWithOverlay.paint(&p, a.rect(), Qt::AlignCenter, QIcon::Normal, QIcon::Off);
result = a.toImage();
bluePixels = 0;
redPixels = 0;
for (int y = 0; y < result.height(); ++y) {
for (int x = 0; x < result.width(); ++x) {
if (qRed(result.pixel(x, y)) == 255) {
redPixels++;
} else if (qBlue(result.pixel(x, y)) == 255) {
bluePixels++;
}
}
}
// 96x96 big icon will have 32x32 big overlay (=1024 blue pixels)
QCOMPARE(bluePixels, 1024);
QCOMPARE(redPixels, 8192);
}
void KIconUtilsTest::addOverlaysTest()
{
QPixmap rectanglePixmap(32, 32);
rectanglePixmap.fill(Qt::red);
QIcon icon(rectanglePixmap);
QPixmap overlay(32, 32);
overlay.fill(Qt::blue);
QIcon overlayIcon(overlay);
QHash<Qt::Corner, QIcon> overlays;
overlays.insert(Qt::BottomRightCorner, overlayIcon);
overlays.insert(Qt::TopLeftCorner, overlayIcon);
QIcon iconWithOverlay = KIconUtils::addOverlays(icon, overlays);
QImage result = iconWithOverlay.pixmap(32, 32).toImage();
int bluePixels = 0;
int redPixels = 0;
// Go over the image and count red and blue pixels
for (int y = 0; y < result.height(); ++y) {
for (int x = 0; x < result.width(); ++x) {
if (qRed(result.pixel(x, y)) == 255) {
redPixels++;
} else if (qBlue(result.pixel(x, y)) == 255) {
bluePixels++;
}
}
}
// Two blue overlays in icon size 32x32 would intersect with 16 pixels,
// so the amount of blue pixels should be 2x256-16 = 496
QCOMPARE(bluePixels, 496);
QCOMPARE(redPixels, 528);
// Try different size
rectanglePixmap = rectanglePixmap.scaled(96, 96);
icon = QIcon(rectanglePixmap);
overlay = overlay.scaled(96, 96);
overlayIcon = QIcon(overlay);
// Clear the old sizes first
overlays.clear();
overlays.insert(Qt::BottomRightCorner, overlayIcon);
overlays.insert(Qt::TopRightCorner, overlayIcon);
overlays.insert(Qt::TopLeftCorner, overlayIcon);
// Now it will have 3 overlays
iconWithOverlay = KIconUtils::addOverlays(icon, overlays);
QPixmap a(96, 96);
QPainter p(&a);
iconWithOverlay.paint(&p, a.rect(), Qt::AlignCenter, QIcon::Normal, QIcon::Off);
result = a.toImage();
bluePixels = 0;
redPixels = 0;
for (int y = 0; y < result.height(); ++y) {
for (int x = 0; x < result.width(); ++x) {
if (qRed(result.pixel(x, y)) == 255) {
redPixels++;
} else if (qBlue(result.pixel(x, y)) == 255) {
bluePixels++;
}
}
}
// 96x96 big icon will have 32x32 big overlays (=3072 blue pixels)
QCOMPARE(bluePixels, 3072);
QCOMPARE(redPixels, 6144);
}
#include "moc_kiconutilstest.cpp"
|