singlepartjobtest.cpp 6.42 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
  Copyright (c) 2009 Constantin Berzan <exit3219@gmail.com>

  This library is free software; you can redistribute it and/or modify it
  under the terms of the GNU Library General Public License as published by
  the Free Software Foundation; either version 2 of the License, or (at your
  option) any later version.

  This library is distributed in the hope that it will be useful, but WITHOUT
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
  License for more details.

  You should have received a copy of the GNU Library General Public License
  along with this library; see the file COPYING.LIB.  If not, write to the
  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  02110-1301, USA.
*/

#include "singlepartjobtest.h"

#include <QDebug>
Laurent Montel's avatar
Laurent Montel committed
23
#include <QTest>
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45

#include <kmime/kmime_content.h>
using namespace KMime;

#include <MessageComposer/Composer>
#include <MessageComposer/GlobalPart>
#include <MessageComposer/SinglepartJob>
using namespace MessageComposer;

QTEST_MAIN(SinglepartJobTest)

void SinglepartJobTest::testContent()
{
    Composer *composer = new Composer;
    SinglepartJob *cjob = new SinglepartJob(composer);
    QByteArray data("birds came flying from the underground");
    cjob->setData(data);
    QVERIFY(cjob->exec());
    Content *result = cjob->content();
    result->assemble();
    qDebug() << result->encodedContent();
    QCOMPARE(result->body(), data);
Laurent Montel's avatar
Laurent Montel committed
46 47
    QVERIFY(result->contentDisposition(false) == nullptr);     // Not created unless demanded.
    QVERIFY(result->contentType(false) == nullptr);     // Not created unless demanded.
48
    QVERIFY(result->contentTransferEncoding(false));     // KMime gives it a default one (7bit).
Laurent Montel's avatar
Laurent Montel committed
49 50
    delete cjob;
    delete composer;
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
}

void SinglepartJobTest::testContentDisposition()
{
    Composer *composer = new Composer;
    SinglepartJob *cjob = new SinglepartJob(composer);
    QByteArray data("birds came flying from the underground");
    cjob->setData(data);
    QString filename = QStringLiteral("test_ăîşţâ.txt");
    cjob->contentDisposition()->setDisposition(Headers::CDattachment);
    cjob->contentDisposition()->setFilename(filename);
    QVERIFY(cjob->exec());
    Content *result = cjob->content();
    result->assemble();
    qDebug() << result->encodedContent();
    QCOMPARE(result->body(), data);
    QVERIFY(result->contentDisposition(false));
    QCOMPARE(result->contentDisposition()->disposition(), Headers::CDattachment);
    QCOMPARE(result->contentDisposition()->filename(), filename);
Laurent Montel's avatar
Laurent Montel committed
70 71
    delete cjob;
    delete composer;
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
}

void SinglepartJobTest::testContentID()
{
    Composer *composer = new Composer;
    SinglepartJob *cjob = new SinglepartJob(composer);
    QByteArray data("birds came flying from the underground");
    QByteArray id("play@cold");
    cjob->setData(data);
    cjob->contentID()->setIdentifier(id);
    QVERIFY(cjob->exec());
    Content *result = cjob->content();
    result->assemble();
    qDebug() << result->encodedContent();
    QCOMPARE(result->body(), data);
    QVERIFY(result->header<Headers::ContentID>());
    QCOMPARE(result->header<Headers::ContentID>()->identifier(), id);
Laurent Montel's avatar
Laurent Montel committed
89 90 91
    delete cjob;

    delete composer;
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
}

void SinglepartJobTest::testContentType()
{
    Composer *composer = new Composer;
    SinglepartJob *cjob = new SinglepartJob(composer);
    QByteArray data("birds came flying from the underground");
    cjob->setData(data);
    QByteArray mimeType("text/plain");
    QByteArray charset("utf-8");
    cjob->contentType()->setMimeType(mimeType);
    cjob->contentType()->setCharset(charset);
    QVERIFY(cjob->exec());
    Content *result = cjob->content();
    result->assemble();
    qDebug() << result->encodedContent();
    QCOMPARE(result->body(), data);
    QVERIFY(result->contentType(false));
    QCOMPARE(result->contentType()->mimeType(), mimeType);
    QCOMPARE(result->contentType()->charset(), charset);
Laurent Montel's avatar
Laurent Montel committed
112 113 114
    delete cjob;

    delete composer;
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
}

void SinglepartJobTest::testContentTransferEncoding()
{
    Composer *composer = new Composer;
    QVERIFY(!composer->globalPart()->is8BitAllowed());
    composer->globalPart()->setFallbackCharsetEnabled(true);

    // 7bit if possible.
    {
        SinglepartJob *cjob = new SinglepartJob(composer);
        QByteArray data("and the sun will set for you...");
        cjob->setData(data);
        QVERIFY(cjob->exec());
        Content *result = cjob->content();
        result->assemble();
        qDebug() << result->encodedContent();
        QVERIFY(result->contentTransferEncoding(false));
        QCOMPARE(result->contentTransferEncoding()->encoding(), Headers::CE7Bit);
        QCOMPARE(result->body(), data);
Laurent Montel's avatar
Laurent Montel committed
135
        delete cjob;
136 137 138 139 140 141 142 143 144 145 146 147 148 149
    }

    // quoted-printable if text doesn't fit in 7bit.
    {
        SinglepartJob *cjob = new SinglepartJob(composer);
        QByteArray data("some long text to make qupr more compact than base64 [ăîşţâ]");   // utf-8
        cjob->setData(data);
        QVERIFY(cjob->exec());
        Content *result = cjob->content();
        result->assemble();
        qDebug() << result->encodedContent();
        QVERIFY(result->contentTransferEncoding(false));
        QCOMPARE(result->contentTransferEncoding()->encoding(), Headers::CEquPr);
        QCOMPARE(result->body(), data);
Laurent Montel's avatar
Laurent Montel committed
150
        delete cjob;
151 152 153 154 155 156 157 158 159 160 161 162 163 164
    }

    // base64 if it's shorter than quoted-printable
    {
        SinglepartJob *cjob = new SinglepartJob(composer);
        QByteArray data("[ăîşţâ]");   // utf-8
        cjob->setData(data);
        QVERIFY(cjob->exec());
        Content *result = cjob->content();
        result->assemble();
        qDebug() << result->encodedContent();
        QVERIFY(result->contentTransferEncoding(false));
        QCOMPARE(result->contentTransferEncoding()->encoding(), Headers::CEbase64);
        QCOMPARE(result->body(), data);
Laurent Montel's avatar
Laurent Montel committed
165
        delete cjob;
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
    }

    // 8bit if asked for and allowed.
    {
        composer->globalPart()->set8BitAllowed(true);
        QByteArray data("[ăîşţâ]");   // utf-8
        SinglepartJob *cjob = new SinglepartJob(composer);
        cjob->setData(data);
        QVERIFY(cjob->exec());
        Content *result = cjob->content();
        result->assemble();
        qDebug() << result->encodedContent();
        QVERIFY(result->contentTransferEncoding(false));
        QCOMPARE(result->contentTransferEncoding()->encoding(), Headers::CE8Bit);
        QCOMPARE(result->body(), data);
Laurent Montel's avatar
Laurent Montel committed
181
        delete cjob;
182
    }
Laurent Montel's avatar
Laurent Montel committed
183
    delete composer;
184
}