From 206abcae10f6cebf7b3b7045fec0cc753f0bb246 Mon Sep 17 00:00:00 2001 From: evgeny Date: Wed, 28 Oct 2026 02:03:04 +0100 Subject: [PATCH] Test typed desktop images in channels and PM and reject corrupted attachments --- tools/chatgui/tests/test_file_drop.cpp | 40 ++++++++++++++++++++------ 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/tools/chatgui/tests/test_file_drop.cpp b/tools/chatgui/tests/test_file_drop.cpp index 3eac3536..f5459dc6 100644 --- a/tools/chatgui/tests/test_file_drop.cpp +++ b/tools/chatgui/tests/test_file_drop.cpp @@ -31,6 +31,7 @@ extern "C" { #include #include #include +#include static QString makeFile(const QTemporaryDir& dir, const QString& name, const QByteArray& contents = "test") { const QString path = dir.filePath(name); @@ -521,20 +522,43 @@ private slots: if (!files.isEmpty()) QCOMPARE(files.at(0).at(1).toString(), mode == "cancel" ? first : second); } - void imageKeepsTheExistingGroupAndPmBehaviour_data() { + void imageHasTypedMetadataInGroupAndPm_data() { QTest::addColumn("dm"); - QTest::newRow("group-image") << false; QTest::newRow("dm-file") << true; + QTest::addColumn("format"); + for (bool dm : {false, true}) for (const QString& format : {QString("PNG"), QString("JPEG")}) + QTest::newRow(qPrintable(QString("%1-%2").arg(dm ? "pm" : "group", format))) << dm << format; } - void imageKeepsTheExistingGroupAndPmBehaviour() { - QFETCH(bool, dm); + void imageHasTypedMetadataInGroupAndPm() { + QFETCH(bool, dm); QFETCH(QString, format); QTemporaryDir dir; QVERIFY(dir.isValid()); InputBar bar(nullptr, nullptr); bar.show(); - bar.setRecordingContext(dir.filePath("media"), dm ? "dm:abcd" : "123"); bar.setDmMode(dm); + bar.setRecordingContext(dir.filePath("media"), dm ? "dm:456" : "123"); bar.setDmMode(dm); bar.setDmGeneration(7); auto* editor = bar.findChild(); QVERIFY(editor); - QSignalSpy files(&bar, &InputBar::sendFileAttachment), images(&bar, &InputBar::sendImageAttachment); - QVERIFY(dropUrls(editor->viewport(), {QUrl::fromLocalFile(makeFile(dir, "фото.PNG"))})); + const QString path = dir.filePath("фото.PNG"); // Фактический JPEG должен победить расширение PNG. + QImage image(120, 80, QImage::Format_ARGB32); image.fill(Qt::red); QVERIFY(image.save(path, qPrintable(format))); + int sends = 0; + connect(&bar, &InputBar::submitAttachment, &bar, [&](const QString& target, attachment_send_req* request) { + ++sends; QCOMPARE(target, dm ? QString("dm:456") : QString("123")); + QCOMPARE(request->info.kind, format == "PNG" ? ATTACHMENT_PNG : ATTACHMENT_JPEG); + QCOMPARE(request->info.width, 120); QCOMPARE(request->info.height, 80); QCOMPARE(request->dm_generation, UINT64_C(7)); + QCOMPARE(QString::fromUtf8(request->source), path); QVERIFY(!request->temporary_source); + const auto id = request->request_id; attachment_send_discard(request); bar.submissionResult(id, true); + }); + QVERIFY(dropUrls(editor->viewport(), {QUrl::fromLocalFile(path)})); QTest::keyClick(editor, Qt::Key_Return); - QCOMPARE(files.count(), dm ? 1 : 0); QCOMPARE(images.count(), dm ? 0 : 1); + QCOMPARE(sends, 1); QVERIFY(QFile::exists(path)); + } + + void corruptImageIsRejectedAndPreservesSelection() { + QTemporaryDir dir; InputBar bar(nullptr, nullptr); bar.show(); bar.setRecordingContext(dir.path(), "123"); + auto* editor = bar.findChild(); QVERIFY(editor); + QSignalSpy errors(&bar, &InputBar::attachmentRejected); + int sends = 0; connect(&bar, &InputBar::submitAttachment, &bar, [&](const QString&, attachment_send_req* req) { + ++sends; attachment_send_discard(req); + }); + const QString path = makeFile(dir, "broken.jpg"); + QVERIFY(dropUrls(editor->viewport(), {QUrl::fromLocalFile(path)})); QTest::keyClick(editor, Qt::Key_Return); + QCOMPARE(sends, 0); QVERIFY(!errors.isEmpty()); QVERIFY(QFile::exists(path)); } void videoModeSelectionAndCancellation_data() {