// SPDX-FileCopyrightText: 2026 Matvey Shcherbinin // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL #include #include #include #include #include #include #include #include #include "../graphics/ImageCrop.h" namespace { // a 100x60 picture with a 30x20 black chip at (40,20) on a red field, so both a // crop and a crop-plus-scale have a colour to assert on. QString makeSource(QTemporaryDir &dir) { QImage image(100, 60, QImage::Format_RGB32); image.fill(Qt::red); for (int y = 20; y < 40; ++y) { for (int x = 40; x < 70; ++x) { image.setPixel(x, y, qRgb(0, 0, 0)); } } const QString path = dir.filePath(QStringLiteral("source.png")); if (!image.save(path)) { qFatal("could not write the source picture"); } return path; } } // namespace class ImageCropTest : public QObject { Q_OBJECT private Q_SLOTS: void regionIsCutRatherThanScaled() { QTemporaryDir dir; ImageCrop crop; const QString out = dir.filePath(QStringLiteral("part.png")); // the chip, copied at its own size QVERIFY(crop.cropToFile(makeSource(dir), QRectF(40, 20, 30, 20), 0, 0, out)); QImage result(out); QCOMPARE(result.size(), QSize(30, 20)); QCOMPARE(result.pixelColor(0, 0), QColor(Qt::black)); QCOMPARE(result.pixelColor(29, 19), QColor(Qt::black)); } void regionIsScaledToTarget() { QTemporaryDir dir; ImageCrop crop; const QString out = dir.filePath(QStringLiteral("scaled.png")); QVERIFY(crop.cropToFile(makeSource(dir), QRectF(40, 20, 30, 20), 15, 10, out)); QImage result(out); QCOMPARE(result.size(), QSize(15, 10)); QCOMPARE(result.pixelColor(0, 0), QColor(Qt::black)); } void wholePicturePassesThrough() { QTemporaryDir dir; ImageCrop crop; const QString out = dir.filePath(QStringLiteral("whole.png")); QVERIFY(crop.cropToFile(makeSource(dir), QRectF(0, 0, 100, 60), 0, 0, out)); QImage result(out); QCOMPARE(result.size(), QSize(100, 60)); QCOMPARE(result.pixelColor(0, 0), QColor(Qt::red)); } void fileUrlIsAccepted() { QTemporaryDir dir; ImageCrop crop; const QString out = dir.filePath(QStringLiteral("fromurl.png")); const QString url = QUrl::fromLocalFile(makeSource(dir)).toString(); QVERIFY(url.startsWith(QLatin1String("file://"))); QVERIFY(crop.cropToFile(url, QRectF(40, 20, 30, 20), 0, 0, out)); QImage result(out); QCOMPARE(result.size(), QSize(30, 20)); } void missingSourceIsRefused() { QTemporaryDir dir; ImageCrop crop; QVERIFY(!crop.cropToFile(dir.filePath(QStringLiteral("nope.png")), QRectF(0, 0, 10, 10), 0, 0, dir.filePath(QStringLiteral("out.png")))); } void gapOffThePictureIsRefused() { QTemporaryDir dir; ImageCrop crop; QVERIFY(!crop.cropToFile(makeSource(dir), QRectF(300, 300, 30, 30), 0, 0, dir.filePath(QStringLiteral("out.png")))); } void writablePathLivesUnderAppData() { ImageCrop crop; // test mode points AppDataLocation at the sandboxed test data dir const QString path = crop.writableImagePath(QStringLiteral("avatar-crop.png")); QVERIFY(path.startsWith(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation))); QVERIFY(path.contains(QStringLiteral("/avatar-crop-"))); QVERIFY(path.endsWith(QStringLiteral(".png"))); // every call hands out a fresh name, so the QML image cache and the // avatar-exchange dedup never see the same path twice for a new crop. QVERIFY(crop.writableImagePath(QStringLiteral("avatar-crop.png")) != path); // the directory exists by the time the path is handed out QVERIFY(QFileInfo::exists(QFileInfo(path).absolutePath())); } }; int main(int argc, char *argv[]) { QStandardPaths::setTestModeEnabled(true); QCoreApplication app(argc, argv); // without a domain ki18n warns on every string, burying the test output. KLocalizedString::setApplicationDomain(QByteArrayLiteral("koutnet")); QCoreApplication::setOrganizationName(QStringLiteral("koutnet-tests")); QCoreApplication::setApplicationName(QStringLiteral("image-crop")); ImageCropTest tc; QTEST_SET_MAIN_SOURCE_PATH return QTest::qExec(&tc, argc, argv); } #include "ImageCropTest.moc"