File indexing completed on 2024-06-02 05:29:11

0001 /*
0002   SPDX-FileCopyrightText: 2009 Constantin Berzan <exit3219@gmail.com>
0003 
0004   SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "attachmentjobtest.h"
0008 #include "qtest_messagecomposer.h"
0009 
0010 #include <QDebug>
0011 #include <QTest>
0012 
0013 #include <KMime/Content>
0014 #include <KMime/Headers>
0015 using namespace KMime;
0016 
0017 #include <MessageComposer/AttachmentJob>
0018 #include <MessageComposer/Composer>
0019 #include <MessageComposer/GlobalPart>
0020 using namespace MessageComposer;
0021 
0022 #include <MessageCore/AttachmentPart>
0023 using namespace MessageCore;
0024 
0025 #define PATH_ATTACHMENTS QLatin1StringView(KDESRCDIR "/attachments/")
0026 
0027 QTEST_MAIN(AttachmentJobTest)
0028 
0029 void AttachmentJobTest::testAttachment()
0030 {
0031     const QString name = QStringLiteral("name");
0032     const QString fileName = QStringLiteral("filename");
0033     const QString description = QStringLiteral("long long long description...");
0034     const QByteArray mimeType("x-some/x-type");
0035     const QByteArray data("la la la");
0036 
0037     AttachmentPart::Ptr part = AttachmentPart::Ptr(new AttachmentPart);
0038     part->setName(name);
0039     part->setFileName(fileName);
0040     part->setDescription(description);
0041     part->setMimeType(mimeType);
0042     part->setData(data);
0043 
0044     Composer composer;
0045     composer.globalPart()->setFallbackCharsetEnabled(true);
0046     auto ajob = new AttachmentJob(part, &composer);
0047     QVERIFY(ajob->exec());
0048     Content *result = ajob->content();
0049     result->assemble();
0050     qDebug() << result->encodedContent();
0051 
0052     QCOMPARE(result->contentType(false)->name(), name);
0053     QCOMPARE(result->contentDisposition(false)->filename(), fileName);
0054     QCOMPARE(result->contentDescription(false)->asUnicodeString(), description);
0055     QCOMPARE(result->contentType(false)->mimeType(), mimeType);
0056     QCOMPARE(result->body(), data);
0057     QVERIFY(result->contentDisposition(false)->disposition() == Headers::CDattachment);
0058     delete ajob;
0059     ajob = nullptr;
0060     // delete result;
0061 }
0062 
0063 #if 0
0064 // Disabled: using UTF-8 instead of trying to detect charset.
0065 
0066 void AttachmentJobTest::testTextCharsetAutodetect_data()
0067 {
0068     QTest::addColumn<QUrl>("url");
0069     QTest::addColumn<QByteArray>("charset");
0070 
0071     // PATH_ATTACHMENTS is defined by CMake.
0072     QTest::newRow("ascii") << QUrl::fromLocalFile(PATH_ATTACHMENTS + QString::fromLatin1("ascii.txt"))
0073                            << QByteArray("us-ascii");
0074     QTest::newRow("iso8859-2") << QUrl::fromLocalFile(PATH_ATTACHMENTS + QString::fromLatin1("iso8859-2.txt"))
0075                                << QByteArray("iso-8859-2");
0076     // TODO not sure how to test utf-16.
0077 }
0078 
0079 void AttachmentJobTest::testTextCharsetAutodetect()
0080 {
0081     QFETCH(QUrl, url);
0082     QFETCH(QByteArray, charset);
0083 
0084     AttachmentFromUrlJob *ljob = new AttachmentFromUrlJob(url);
0085     VERIFYEXEC(ljob);
0086     AttachmentPart::Ptr part = ljob->attachmentPart();
0087     delete ljob;
0088     ljob = 0;
0089 
0090     Composer *composer = new Composer;
0091     composer->globalPart()->setFallbackCharsetEnabled(true);
0092     AttachmentJob *ajob = new AttachmentJob(part, composer);
0093     VERIFYEXEC(ajob);
0094     Content *result = ajob->content();
0095     delete ajob;
0096     ajob = 0;
0097     result->assemble();
0098     qDebug() << result->encodedContent();
0099 
0100     QCOMPARE(result->contentType(false)->charset(), charset);
0101 }
0102 
0103 #endif
0104 
0105 #include "moc_attachmentjobtest.cpp"