File indexing completed on 2024-11-10 04:30:39
0001 /* 0002 SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0003 SPDX-FileCopyrightText: 2022 Nicolas Fella <nicolas.fella@gmx.de> 0004 */ 0005 0006 #include <QSignalSpy> 0007 #include <QStandardPaths> 0008 #include <QTest> 0009 0010 #include <KIO/PreviewJob> 0011 0012 class ThumbnailTest : public QObject 0013 { 0014 Q_OBJECT 0015 private Q_SLOTS: 0016 0017 void testThumbnail_data() 0018 { 0019 QTest::addColumn<QString>("inputFile"); 0020 QTest::addColumn<QString>("expectedThumbnail"); 0021 QTest::addColumn<qreal>("dpr"); 0022 0023 QTest::addRow("png") << "cherry_tree.png" 0024 << "cherry_tree_thumb.png" << 1.0; 0025 0026 QTest::addRow("png_dpr2") << "cherry_tree.png" 0027 << "cherry_tree_thumb@2.png" << 2.0; 0028 0029 QTest::addRow("jpg") << "boxes.jpg" 0030 << "boxes_thumb.png" << 1.0; 0031 0032 QTest::addRow("jpg_dpr2") << "boxes.jpg" 0033 << "boxes_thumb@2.png" << 2.0; 0034 0035 QTest::addRow("jpg_embedded_thumbnail") << "castle.jpg" 0036 << "castle_thumb.png" << 1.0; 0037 0038 // ignoring embedded thumbnail as it is too small 0039 QTest::addRow("jpg_embedded_thumbnail2") << "castle.jpg" 0040 << "castle_thumb_256.png" << 2.0; 0041 0042 // image using 4 colors table 0043 QTest::addRow("Screen_color_test_Amiga_4colors") << "Screen_color_test_Amiga_4colors.png" 0044 << "Screen_color_test_Amiga_4colors_converted.png" << 2.0; 0045 0046 QTest::addRow("folder-database.svg") << "folder-database.svg" 0047 << "folder-database-128@2.png" << 2.0; 0048 } 0049 0050 void testThumbnail() 0051 { 0052 QFETCH(QString, inputFile); 0053 QFETCH(QString, expectedThumbnail); 0054 QFETCH(qreal, dpr); 0055 0056 QStandardPaths::setTestModeEnabled(true); 0057 qputenv("KIOWORKER_ENABLE_TESTMODE", "1"); // ensure the worker call QStandardPaths::setTestModeEnabled too 0058 0059 // wipe thumbnail cache so we always start clean 0060 QDir cacheDir(QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation)); 0061 cacheDir.removeRecursively(); 0062 0063 QString path = QFINDTESTDATA("data/" + inputFile); 0064 0065 KFileItemList items; 0066 items.append(KFileItem(QUrl::fromLocalFile(path))); 0067 0068 QStringList enabledPlugins{"svgthumbnail", "imagethumbnail", "jpegthumbnail"}; 0069 auto *job = KIO::filePreview(items, QSize(128, 128), &enabledPlugins); 0070 job->setDevicePixelRatio(dpr); 0071 0072 connect(job, &KIO::PreviewJob::gotPreview, this, [path, expectedThumbnail, dpr](const KFileItem &item, const QPixmap &preview) { 0073 QCOMPARE(item.url(), QUrl::fromLocalFile(path)); 0074 0075 QImage expectedImage; 0076 expectedImage.load(QFINDTESTDATA("data/" + expectedThumbnail)); 0077 expectedImage.setDevicePixelRatio(dpr); 0078 0079 if (expectedImage.format() == QImage::Format_ARGB32) { 0080 // QImage load loads differently from KPreviewJob 0081 expectedImage = expectedImage.convertToFormat(QImage::Format_ARGB32_Premultiplied); 0082 } 0083 0084 QCOMPARE(preview.devicePixelRatio(), dpr); 0085 QCOMPARE(preview.toImage(), expectedImage); 0086 }); 0087 0088 QSignalSpy failedSpy(job, &KIO::PreviewJob::failed); 0089 QSignalSpy gotPreviewSpy(job, &KIO::PreviewJob::gotPreview); 0090 QSignalSpy resultSpy(job, &KIO::PreviewJob::result); 0091 0092 resultSpy.wait(); 0093 0094 QVERIFY2(failedSpy.empty(), qPrintable(job->errorString())); 0095 QVERIFY(!gotPreviewSpy.empty()); 0096 } 0097 }; 0098 0099 QTEST_MAIN(ThumbnailTest) 0100 0101 #include "thumbnailtest.moc"