templateparsertest.cpp 11.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/* Copyright 2011 Sudhendu Kumar <sudhendu.kumar.roy@gmail.com>

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2 of
   the License or (at your option) version 3 or any later version
   accepted by the membership of KDE e.V. (or its successor approved
   by the membership of KDE e.V.), which shall act as a proxy
   defined in Section 14 of version 3 of the license.

   This program 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 General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "templateparsertest.h"
Sandro Knauß's avatar
Sandro Knauß committed
21
#define protected public
22
#include "templateparser/templateparser.h"
Sandro Knauß's avatar
Sandro Knauß committed
23
#undef protected
Laurent Montel's avatar
Laurent Montel committed
24 25
#include "messageviewer/viewer/objecttreeparser.h"
#include "messageviewer/viewer/objecttreeemptysource.h"
26 27
#include <kpimidentities/identitymanager.h>
#include <kpimidentities/identity.h>
28 29
#include "qwebpage.h"
#include "qwebframe.h"
30
#include "qtest_kde.h"
31
#include "kdebug.h"
32

33
using namespace MessageViewer;
34

35
void TemplateParserTester::test_convertedHtml_data()
36
{
Laurent Montel's avatar
Laurent Montel committed
37 38
    QTest::addColumn<QString>( "mailFileName" );
    QTest::addColumn<QString>( "referenceFileName" );
39

Laurent Montel's avatar
Laurent Montel committed
40 41 42 43
    QDir dir( QLatin1String(MAIL_DATA_DIR) );
    foreach ( const QString &file, dir.entryList( QStringList(QLatin1String("plain*.mbox")), QDir::Files | QDir::Readable | QDir::NoSymLinks  ) ) {
        QTest::newRow( file.toLatin1() ) << QString(dir.path() + QLatin1Char('/') +  file) << QString(dir.path() + QLatin1Char('/') + file + QLatin1String(".html"));
    }
44
}
45

46
void TemplateParserTester::test_convertedHtml()
47
{
Laurent Montel's avatar
Laurent Montel committed
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
    QFETCH( QString, mailFileName );
    QFETCH( QString, referenceFileName );

    // load input mail
    QFile mailFile( mailFileName );
    QVERIFY( mailFile.open( QIODevice::ReadOnly ) );
    const QByteArray mailData = KMime::CRLFtoLF( mailFile.readAll() );
    QVERIFY( !mailData.isEmpty() );
    KMime::Message::Ptr msg( new KMime::Message );
    msg->setContent( mailData );
    msg->parse();

    // load expected result
    QFile referenceFile( referenceFileName );
    QVERIFY( referenceFile.open( QIODevice::ReadOnly ) );
    const QByteArray referenceRawData = KMime::CRLFtoLF( referenceFile.readAll() );
    const QString referenceData = QString::fromLatin1(referenceRawData );
    QVERIFY( !referenceData.isEmpty() );

    EmptySource emptySource;

    QCOMPARE( msg->subject()->as7BitString( false ).constData(), "Plain Message Test" );
    QCOMPARE( msg->contents().size(), 0 );

    ObjectTreeParser otp( &emptySource );
    otp.parseObjectTree( msg.get() );

    QVERIFY( otp.htmlContent().isEmpty() );
    QVERIFY( !otp.plainTextContent().isEmpty() );
    const QString convertedHtmlContent = otp.convertedHtmlContent();
    QVERIFY( !convertedHtmlContent.isEmpty() );

    QCOMPARE( convertedHtmlContent, referenceData );
81 82
}

83 84
void TemplateParserTester::test_bodyFromHtml()
{
Laurent Montel's avatar
Laurent Montel committed
85 86 87 88 89 90 91 92
    const QString content( QLatin1String("<html><head><title>Plain mail with signature</title></head>"
                                         "<body>This is the message text from Sudhendu Kumar&lt;"
                                         "dontspamme@yoohoo.com&gt;.<br /><br />-- <br />Thanks &amp; "
                                         "Regards<br />Sudhendu Kumar</body></html>"));
    QWebPage page;
    page.settings()->setAttribute( QWebSettings::JavascriptEnabled, false );
    page.settings()->setAttribute( QWebSettings::JavaEnabled, false );
    page.settings()->setAttribute( QWebSettings::PluginsEnabled, false );
93

Laurent Montel's avatar
Laurent Montel committed
94
    page.currentFrame()->setHtml( content );
95

Laurent Montel's avatar
Laurent Montel committed
96
    page.settings()->setAttribute( QWebSettings::JavascriptEnabled, true );
97

Laurent Montel's avatar
Laurent Montel committed
98 99
    const QString bodyElement = page.currentFrame()->evaluateJavaScript(
                QLatin1String("document.getElementsByTagName('body')[0].innerHTML")).toString();
100

Laurent Montel's avatar
Laurent Montel committed
101
    page.settings()->setAttribute( QWebSettings::JavascriptEnabled, false );
102

Laurent Montel's avatar
Laurent Montel committed
103 104 105
    const QString expectedBody( QLatin1String("This is the message text from Sudhendu Kumar"
                                              "&lt;dontspamme@yoohoo.com&gt;.<br><br>-- <br>"
                                              "Thanks &amp; Regards<br>Sudhendu Kumar") );
106

Laurent Montel's avatar
Laurent Montel committed
107
    QCOMPARE( bodyElement, expectedBody );
108

Laurent Montel's avatar
Laurent Montel committed
109
    page.settings()->setAttribute( QWebSettings::JavascriptEnabled, true );
110

Laurent Montel's avatar
Laurent Montel committed
111 112
    const QString headElement = page.currentFrame()->evaluateJavaScript(
                QLatin1String("document.getElementsByTagName('head')[0].innerHTML") ).toString();
113

Laurent Montel's avatar
Laurent Montel committed
114
    page.settings()->setAttribute( QWebSettings::JavascriptEnabled, false );
115

Laurent Montel's avatar
Laurent Montel committed
116
    const QString expectedHead( QLatin1String("<title>Plain mail with signature</title>") );
117

Laurent Montel's avatar
Laurent Montel committed
118
    QCOMPARE( headElement, expectedHead );
119
}
120

121
void TemplateParserTester::test_processWithTemplatesForBody_data()
122
{
Laurent Montel's avatar
Laurent Montel committed
123 124 125 126 127 128
    QTest::addColumn<QString>( "command" );
    QTest::addColumn<QString>( "text" );
    QTest::addColumn<QString>( "expected" );
    QTest::addColumn<QString>( "selection" );

    QTest::newRow( "%OTEXT") << "%OTEXT" << "Original text.\nLine two." << "Original text.\nLine two." << "";
Daniel Vrátil's avatar
Daniel Vrátil committed
129 130 131 132 133 134 135 136 137 138 139 140
    QTest::newRow( "%OTEXT") << "%OTEXT" << "-----BEGIN PGP MESSAGE-----\nVersion: GnuPG v1.4.12 (GNU/Linux)\n"
                                "\n"
                                "hQEMAwzOQ1qnzNo7AQgA1345CrnOBTGf2eo4ABR6wkOdasI9SELRBKA1fNkFcq+Z\n"
                                "Qg0gWB5RLapU+VFRc5hK1zPOZ1dY6j3+uPHO4RhjfUgfiZ8T7oaWav15yP+07u21\n"
                                "EI9W9sk+eQU9GZSOayURucmZa/mbBz9hrsmePpORxD+C3uNTYa6ePTFlQP6wEZOI\n"
                                "7E53DrtJnF0EzIsCBIVep6CyuYfuSSwQ5gMgyPzfBqiGHNw96w2i/eayErc6lquL\n"
                                "JPFhIcMMq8w9Yo9+vXCAbkns6dtBAzlnAzuV86VFUZ/MnHTlCNk2yHyGLP6BS6hG\n"
                                "kFEUmgdHrGRizdz1sjo1tSmOLu+Gyjlv1Ir/Sqr8etJQAeTq3heKslAfhtotAMMt\n"
                                "R3tk228Su13Q3CAP/rktAyuGMDFtH8klW09zFdsZBDu8svE6d9e2nZ541NGspFVI\n"
                                "6XTZHUMMdlgnTBcu3aPc0ow=\n"
                                "=0xtc\n"
                                "-----END PGP MESSAGE-----" << "Crypted line.\nCrypted line two." << "";
Laurent Montel's avatar
Laurent Montel committed
141
            QTest::newRow( "%QUOTE") << "%QUOTE" << "Quoted text.\nLine two." << "> Quoted text.\n> Line two." << "";
142 143
}

144
void TemplateParserTester::test_processWithTemplatesForBody()
145
{
Laurent Montel's avatar
Laurent Montel committed
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
    QFETCH( QString, command );
    QFETCH( QString, text );
    QFETCH( QString, expected );
    QFETCH( QString, selection );

    KMime::Message::Ptr msg( new KMime::Message() );
    msg->setBody( text.toLocal8Bit() );
    msg->parse();
    TemplateParser::TemplateParser parser( msg, TemplateParser::TemplateParser::Reply );
    parser.setSelection( selection );
    KPIMIdentities::IdentityManager* identMan = new KPIMIdentities::IdentityManager;
    parser.setIdentityManager( identMan );
    parser.setAllowDecryption( true );
    parser.mOrigMsg = msg;

    parser.processWithTemplate( command );

    identMan->deleteLater();
    QCOMPARE( QString::fromLatin1( msg->encodedBody() ), expected );
165 166
}

167 168
void TemplateParserTester::test_processWithTemplatesForContent_data()
{
Laurent Montel's avatar
Laurent Montel committed
169 170
    QTest::addColumn<QString>( "command" );
    QTest::addColumn<QString>( "mailFileName" );
Laurent Montel's avatar
Laurent Montel committed
171
    QTest::addColumn<QString>( "expectedBody" );
Laurent Montel's avatar
Laurent Montel committed
172
    QTest::addColumn<bool>( "hasDictionary" );
Laurent Montel's avatar
Laurent Montel committed
173 174

    QDir dir( QLatin1String(MAIL_DATA_DIR) );
Laurent Montel's avatar
Laurent Montel committed
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
    const QString file = QLatin1String("plain-message.mbox");
    const QString fileName = QString(dir.path() + QLatin1Char('/') +  file);
    QTest::newRow( file.toLatin1() ) << "%OTIME" << fileName << "11:30" << false;
    QTest::newRow( file.toLatin1() ) << "%OTIMELONG" << fileName << "11:30:27" << false;
    QTest::newRow( file.toLatin1() ) << "%OTIMELONGEN" << fileName << "11:30:27" << false;
    QTest::newRow( file.toLatin1() ) << "%ODATE" << fileName << "Sunday 07 August 2011" << false;
    QTest::newRow( file.toLatin1() ) << "%ODATESHORT" << fileName << "2011-08-07" << false;
    QTest::newRow( file.toLatin1() ) << "%ODATEEN" << fileName << "Sunday 07 August 2011" << false;
    QTest::newRow( file.toLatin1() ) << "%OFULLSUBJ" << fileName << "Plain Message Test" << false;
    QTest::newRow( file.toLatin1() ) << "%OFULLSUBJECT" << fileName << "Plain Message Test" << false;
    QTest::newRow( file.toLatin1() ) << "%OFROMFNAME" << fileName << "Sudhendu" << false;
    QTest::newRow( file.toLatin1() ) << "%OFROMLNAME" << fileName  << "Kumar" << false;
    QTest::newRow( file.toLatin1() ) << "%OFROMNAME" << fileName << "Sudhendu Kumar" << false;
    QTest::newRow( file.toLatin1() ) << "%OFROMADDR" << fileName << "Sudhendu Kumar <dontspamme@yoohoo.com>" << false;
    QTest::newRow( file.toLatin1() ) << "%OTOADDR" << fileName << "kde <foo@yoohoo.org>" << false;
    QTest::newRow( file.toLatin1() ) << "%OTOFNAME" << fileName << "kde" << false;
    QTest::newRow( file.toLatin1() ) << "%OTONAME" << fileName << "kde" << false;
    QTest::newRow( file.toLatin1() ) << "%OTOLNAME" << fileName << "" << false;
    QTest::newRow( file.toLatin1() ) << "%OTOLIST" << fileName << "kde <foo@yoohoo.org>" << false;
    QTest::newRow( file.toLatin1() ) << "%ODOW" << fileName << "Sunday" << false;
    QTest::newRow( file.toLatin1() ) << "%BLANK" << fileName << "" << false;
    QTest::newRow( file.toLatin1() ) << "%NOP" << fileName << "" << false;
    QTest::newRow( file.toLatin1() ) << "%DICTIONARYLANGUAGE=\"en\"" << fileName << "" << true;
    QTest::newRow( file.toLatin1() ) << "%DICTIONARYLANGUAGE=\"\"" << fileName << "" << false;
    QTest::newRow( file.toLatin1() ) << "%OTIMELONG %OFULLSUBJECT" << fileName << "11:30:27 Plain Message Test" << false;
    QTest::newRow( file.toLatin1() ) << "%OTIMELONG\n%OFULLSUBJECT" << fileName << "11:30:27\nPlain Message Test" << false;
Laurent Montel's avatar
Laurent Montel committed
201 202 203
    QTest::newRow( file.toLatin1() ) << "%REM=\"sdfsfsdsdfsdf\"" << fileName << "" << false;
    QTest::newRow( file.toLatin1() ) << "%CLEAR" << fileName << "" << false;
    QTest::newRow( file.toLatin1() ) << "FOO foo" << fileName << "FOO foo" << false;
Laurent Montel's avatar
Laurent Montel committed
204
    const QString insertFileName = QString(dir.path() + QLatin1Char('/') +  QLatin1String("insert-file.txt"));
Laurent Montel's avatar
Laurent Montel committed
205 206 207
    QString insertFileNameCommand = QString::fromLatin1("%INSERT=\"%1\"").arg(insertFileName);
    QTest::newRow( file.toLatin1() ) << insertFileNameCommand << fileName << "test insert file!\n" << false;
    insertFileNameCommand = QString::fromLatin1("%PUT=\"%1\"").arg(insertFileName);
Laurent Montel's avatar
Laurent Montel committed
208
    QTest::newRow( file.toLatin1() ) << insertFileNameCommand << fileName << "test insert file!\n" << false;
209 210 211 212
}

void TemplateParserTester::test_processWithTemplatesForContent()
{
Laurent Montel's avatar
Laurent Montel committed
213 214
    QFETCH( QString, command );
    QFETCH( QString, mailFileName );
Laurent Montel's avatar
Laurent Montel committed
215
    QFETCH( QString, expectedBody );
Laurent Montel's avatar
Laurent Montel committed
216
    QFETCH( bool, hasDictionary );
Laurent Montel's avatar
Laurent Montel committed
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231

    QFile mailFile( mailFileName );
    QVERIFY( mailFile.open( QIODevice::ReadOnly ) );
    const QByteArray mailData = KMime::CRLFtoLF( mailFile.readAll() );
    QVERIFY( !mailData.isEmpty() );
    KMime::Message::Ptr msg( new KMime::Message );
    msg->setContent( mailData );
    msg->parse();

    TemplateParser::TemplateParser parser( msg, TemplateParser::TemplateParser::Reply );
    KPIMIdentities::IdentityManager* identMan = new KPIMIdentities::IdentityManager;
    parser.setIdentityManager( identMan );
    parser.setAllowDecryption( false );
    parser.mOrigMsg = msg;
    parser.processWithTemplate( command );
Laurent Montel's avatar
Laurent Montel committed
232
    QCOMPARE(msg->hasHeader("X-KMail-Dictionary"), hasDictionary);
Laurent Montel's avatar
Laurent Montel committed
233 234

    identMan->deleteLater();
Laurent Montel's avatar
Laurent Montel committed
235
    QCOMPARE( QString::fromLatin1( msg->encodedBody() ), expectedBody );
236 237
}

238 239
QTEST_KDEMAIN( TemplateParserTester, GUI )