[frameworks/kio] autotests: Test FilePreviewJob::emitPreview output size and device pixel ratio

Méven Car <[email protected]>
Newsgroups gmane.comp.kde.cvs
Message-ID <[email protected]>
Git commit d4c3cbd9f7a0e41353ac99aaca78030c626d3fea by Méven Car.
Committed on 30/07/2026 at 17:17.
Pushed by meven into branch 'master'.

Test FilePreviewJob::emitPreview output size and device pixel ratio

emitPreview() had no automated coverage. Add a data-driven test checking
the emitted image is scaled to the requested device-pixel size (and
passed through unscaled when it already fits within it) and carries the
device pixel ratio.

M  +49   -0    autotests/filepreviewjobtest.cpp
M  +2    -0    autotests/filepreviewjobtest.h

https://invent.kde.org/frameworks/kio/-/commit/d4c3cbd9f7a0e41353ac99aaca78030c626d3fea

diff --git a/autotests/filepreviewjobtest.cpp b/autotests/filepreviewjobtest.cpp
index adb34b9e9b..480fc52f14 100644
--- a/autotests/filepreviewjobtest.cpp
+++ b/autotests/filepreviewjobtest.cpp
@@ -159,3 +159,52 @@ void FilePreviewJobTest::testCacheSizeValidation()
 
     delete job;
 }
+
+void FilePreviewJobTest::testGeneratedImageDevicePixelRatio_data()
+{
+    QTest::addColumn<qreal>("dpr");
+    QTest::addColumn<QSize>("thumbSize"); // source thumbnail, in device pixels
+    QTest::addColumn<QSize>("requestSize"); // PreviewOptions::size, in logical pixels
+    QTest::addColumn<QSize>("expectedSize"); // emitted image, in device pixels
+
+    // Source larger than requestSize * dpr, so emitPreview() scales it down to that.
+    QTest::newRow("scaled down, 1.75x") << 1.75 << QSize(448, 448) << QSize(128, 128) << QSize(224, 224);
+    QTest::newRow("scaled down, 2x") << 2.0 << QSize(512, 512) << QSize(128, 128) << QSize(256, 256);
+    // Source already fits within requestSize * dpr, so it is passed through unscaled.
+    QTest::newRow("not scaled, 1.75x") << 1.75 << QSize(100, 100) << QSize(256, 256) << QSize(100, 100);
+}
+
+void FilePreviewJobTest::testGeneratedImageDevicePixelRatio()
+{
+    QFETCH(qreal, dpr);
+    QFETCH(QSize, thumbSize);
+    QFETCH(QSize, requestSize);
+    QFETCH(QSize, expectedSize);
+
+    QTemporaryDir thumbRoot;
+    QVERIFY(thumbRoot.isValid());
+
+    const KFileItem item(QUrl::fromLocalFile(QStringLiteral("/tmp/does-not-matter.png")));
+
+    PreviewOptions options;
+    options.size = requestSize;
+    options.devicePixelRatio = dpr;
+
+    PreviewSetupData setupData;
+    setupData.thumbRoot = thumbRoot.path();
+
+    auto *job = new FilePreviewJob(item, FilePreviewJob::UnknownDeviceId, options, setupData);
+    job->setAutoDelete(false);
+
+    QImage thumb(thumbSize, QImage::Format_ARGB32);
+    thumb.fill(Qt::black);
+    thumb.setDevicePixelRatio(dpr);
+
+    // emitPreview() produces the image delivered by PreviewJob::generated(); it must
+    // carry the device pixel ratio and be scaled to the requested device-pixel size.
+    job->emitPreview(thumb);
+    QCOMPARE(job->previewImage().devicePixelRatio(), dpr);
+    QCOMPARE(job->previewImage().size(), expectedSize);
+
+    delete job;
+}
diff --git a/autotests/filepreviewjobtest.h b/autotests/filepreviewjobtest.h
index a9bc40440d..38b55c3ce4 100644
--- a/autotests/filepreviewjobtest.h
+++ b/autotests/filepreviewjobtest.h
@@ -19,6 +19,8 @@ private Q_SLOTS:
     void testTimeoutTimerStoppedOnFinish();
     void testCacheSizeValidation_data();
     void testCacheSizeValidation();
+    void testGeneratedImageDevicePixelRatio_data();
+    void testGeneratedImageDevicePixelRatio();
 };
 
 #endif
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.