Page MenuHomePhorge

formattest.cpp
No OneTemporary

Authored By
Unknown
Size
16 KB
Referenced Files
None
Subscribers
None

formattest.cpp

/*
<one line to give the library's name and an idea of what it does.>
Copyright (C) 2012 <copyright holder> <email>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "formattest.h"
#include <QTest>
#include <QFile>
#include <QDebug>
#include <qprocess.h>
#include <qtemporaryfile.h>
#include <kdebug.h>
#include <kolab/kolabcontainers.h>
#include <kolab/kolabformat.h>
#include <kcalcore/icalformat.h>
#include <kabc/vcardconverter.h>
#include "testutils.h"
#include "kolabformat/kolabobject.h"
static bool compareMimeMessage( const KMime::Message::Ptr &msg, const KMime::Message::Ptr &expectedMsg )
{
// headers
KCOMPARE( msg->subject()->asUnicodeString(), expectedMsg->subject()->asUnicodeString() );
if ( msg->from()->isEmpty() || expectedMsg->from()->isEmpty() ) {
KCOMPARE( msg->from()->asUnicodeString(), expectedMsg->from()->asUnicodeString() );
} else {
KCOMPARE( msg->from()->mailboxes().first().address(), expectedMsg->from()->mailboxes().first().address() ); // matching address is enough, we don't need a display name
}
KCOMPARE( msg->contentType()->mimeType(), expectedMsg->contentType()->mimeType() );
KCOMPARE( msg->headerByType( "X-Kolab-Type" )->as7BitString(), expectedMsg->headerByType( "X-Kolab-Type" )->as7BitString() );
// date contains conversion time...
// KCOMPARE( msg->date()->asUnicodeString(), expectedMsg->date()->asUnicodeString() );
// body parts
KCOMPARE( msg->contents().size(), expectedMsg->contents().size() );
for ( int i = 0; i < msg->contents().size(); ++i ) {
KMime::Content *part = msg->contents().at( i );
KMime::Content *expectedPart = expectedMsg->contents().at( i );
// part headers
KCOMPARE( part->contentType()->mimeType(), expectedPart->contentType()->mimeType() );
KCOMPARE( part->contentDisposition()->filename(), expectedPart->contentDisposition()->filename() );
KCOMPARE( part->decodedContent().isEmpty(), false );
// part content
KCOMPARE( QString(part->decodedContent()).simplified(), QString(expectedPart->decodedContent()).simplified() );
}
return true;
}
template <template <typename> class Op, typename T>
static bool LexicographicalCompare( const T &_x, const T &_y )
{
T x( _x );
x.setId( QString() );
T y( _y );
y.setId( QString() );
Op<QString> op;
return op( x.toString(), y.toString() );
}
static bool normalizePhoneNumbers( KABC::Addressee &addressee, const KABC::Addressee &refAddressee )
{
KABC::PhoneNumber::List phoneNumbers = addressee.phoneNumbers();
KABC::PhoneNumber::List refPhoneNumbers = refAddressee.phoneNumbers();
if ( phoneNumbers.size() != refPhoneNumbers.size() )
return false;
std::sort( phoneNumbers.begin(), phoneNumbers.end(), LexicographicalCompare<std::less, KABC::PhoneNumber> );
std::sort( refPhoneNumbers.begin(), refPhoneNumbers.end(), LexicographicalCompare<std::less, KABC::PhoneNumber> );
for ( int i = 0; i < phoneNumbers.size(); ++i ) {
KABC::PhoneNumber phoneNumber = phoneNumbers.at( i );
const KABC::PhoneNumber refPhoneNumber = refPhoneNumbers.at( i );
KCOMPARE( LexicographicalCompare<std::equal_to>( phoneNumber, refPhoneNumber ), true );
addressee.removePhoneNumber( phoneNumber );
phoneNumber.setId( refPhoneNumber.id() );
addressee.insertPhoneNumber( phoneNumber );
}
return true;
}
static bool normalizeAddresses( KABC::Addressee &addressee, const KABC::Addressee &refAddressee )
{
KABC::Address::List addresses = addressee.addresses();
KABC::Address::List refAddresses = refAddressee.addresses();
if ( addresses.size() != refAddresses.size() )
return false;
std::sort( addresses.begin(), addresses.end(), LexicographicalCompare<std::less, KABC::Address> );
std::sort( refAddresses.begin(), refAddresses.end(), LexicographicalCompare<std::less, KABC::Address> );
for ( int i = 0; i < addresses.size(); ++i ) {
KABC::Address address = addresses.at( i );
const KABC::Address refAddress = refAddresses.at( i );
KCOMPARE( LexicographicalCompare<std::equal_to>( address, refAddress ), true );
addressee.removeAddress( address );
address.setId( refAddress.id() );
addressee.insertAddress( address );
}
return true;
}
void createIndidenceTestSet()
{
QTest::addColumn<Kolab::Version>( "version" );
QTest::addColumn<Kolab::ObjectType>( "type" );
QTest::addColumn<QString>( "icalFileName" );
QTest::addColumn<QString>( "mimeFileName" );
// QTest::newRow( "v2eventSimple" ) << Kolab::KolabV2 << Kolab::EventObject << TESTFILEDIR+QString::fromLatin1("v2/event/simple.ics") << TESTFILEDIR+QString::fromLatin1("v2/event/simple.ics.mime");
// QTest::newRow( "v2eventComplex" ) << Kolab::KolabV2 << Kolab::EventObject << TESTFILEDIR+QString::fromLatin1("v2/event/complex.ics") << TESTFILEDIR+QString::fromLatin1("v2/event/complex.ics.mime");
// QTest::newRow( "v2eventAttachment" ) << Kolab::KolabV2 << Kolab::EventObject << TESTFILEDIR+QString::fromLatin1("v2/event/attachment.ics") << TESTFILEDIR+QString::fromLatin1("v2/event/attachment.ics.mime");
// QTest::newRow( "v2eventAllday" ) << Kolab::KolabV2 << Kolab::EventObject << TESTFILEDIR+QString::fromLatin1("v2/event/allday.ics") << TESTFILEDIR+QString::fromLatin1("v2/event/allday.ics.mime");
// QTest::newRow( "v2eventHorde" ) << Kolab::KolabV2 << Kolab::EventObject << TESTFILEDIR+QString::fromLatin1("v2/event/horde.ics") << TESTFILEDIR+QString::fromLatin1("v2/event/horde.ics.mime");
// QTest::newRow( "v2todoComplex" ) << Kolab::KolabV2 << Kolab::TodoObject << TESTFILEDIR+QString::fromLatin1("v2/task/complex.ics") << TESTFILEDIR+QString::fromLatin1("v2/task/complex.ics.mime");
// QTest::newRow( "v2todoPrio1" ) << Kolab::KolabV2 << Kolab::TodoObject << TESTFILEDIR+QString::fromLatin1("v2/task/prioritytest1.ics") << TESTFILEDIR+QString::fromLatin1("v2/task/prioritytest1.ics.mime");
// QTest::newRow( "v2todoPrio2" ) << Kolab::KolabV2 << Kolab::TodoObject << TESTFILEDIR+QString::fromLatin1("v2/task/prioritytest2.ics") << TESTFILEDIR+QString::fromLatin1("v2/task/prioritytest2.ics.mime");
// QTest::newRow( "v2journalComplex" ) << Kolab::KolabV2 << Kolab::JournalObject << TESTFILEDIR+QString::fromLatin1("v2/journal/complex.ics") << TESTFILEDIR+QString::fromLatin1("v2/journal/complex.ics.mime");
//
QTest::newRow( "v3eventSimple" ) << Kolab::KolabV3 << Kolab::EventObject << TESTFILEDIR+QString::fromLatin1("v3/event/simple.ics") << TESTFILEDIR+QString::fromLatin1("v3/event/simple.ics.mime");
QTest::newRow( "v3eventComplex" ) << Kolab::KolabV3 << Kolab::EventObject << TESTFILEDIR+QString::fromLatin1("v3/event/complex.ics") << TESTFILEDIR+QString::fromLatin1("v3/event/complex.ics.mime");
}
void FormatTest::testIncidence_data()
{
createIndidenceTestSet();
}
void FormatTest::testIncidence()
{
QFETCH( Kolab::Version, version );
QFETCH( Kolab::ObjectType, type );
QFETCH( QString, icalFileName ); //To compare
QFETCH( QString, mimeFileName ); //For parsing
//Parse mime message
bool ok = false;
const KMime::Message::Ptr &msg = readMimeFile( mimeFileName, ok );
QVERIFY(ok);
Kolab::KolabObjectReader reader;
Kolab::ObjectType t = reader.parseMimeMessage(msg);
QCOMPARE(t, type);
QCOMPARE(reader.getVersion(), version);
KCalCore::Incidence::Ptr convertedIncidence = reader.getIncidence();
//Parse ICalFile for comparison
QFile icalFile( icalFileName );
QVERIFY( icalFile.open( QFile::ReadOnly ) );
KCalCore::ICalFormat format;
const KCalCore::Incidence::Ptr realIncidence( format.fromString( QString::fromUtf8( icalFile.readAll() ) ) );
// fix up the converted incidence for comparisson
foreach ( KCalCore::Attendee::Ptr a, convertedIncidence->attendees() ) {
a->setUid( QString() ); // contains random values
}
foreach ( KCalCore::Attendee::Ptr a, realIncidence->attendees() ) {
a->setUid( QString() ); // contains random values
}
if ( type == Kolab::TodoObject ) {
KCalCore::Todo::Ptr todo = realIncidence.dynamicCast<KCalCore::Todo>();
QVERIFY(todo);
if ( !todo->hasDueDate() && !todo->hasStartDate() )
convertedIncidence->setAllDay( realIncidence->allDay() ); // all day has no meaning if there are no start and due dates but may differ nevertheless
}
// recurrence objects are created on demand, but KCalCore::Incidence::operator==() doesn't take that into account
// so make sure both incidences have one
realIncidence->recurrence();
convertedIncidence->recurrence();
if ( *(realIncidence.data()) != *(convertedIncidence.data()) ) {
showDiff(format.toString( realIncidence ), format.toString( convertedIncidence ));
}
QVERIFY( *(realIncidence.data()) == *(convertedIncidence.data()) );
//Write
Kolab::overrideTimestamp(Kolab::cDateTime(2012, 5, 5, 5,5,5, true));
KMime::Message::Ptr convertedMime;
switch ( realIncidence->type() ) {
case KCalCore::IncidenceBase::TypeEvent:
convertedMime = Kolab::KolabObjectWriter::writeEvent(realIncidence.dynamicCast<KCalCore::Event>(), version);
break;
case KCalCore::IncidenceBase::TypeTodo:
convertedMime = Kolab::KolabObjectWriter::writeTodo(realIncidence.dynamicCast<KCalCore::Todo>(), version);
break;
case KCalCore::IncidenceBase::TypeJournal:
convertedMime = Kolab::KolabObjectWriter::writeJournal(realIncidence.dynamicCast<KCalCore::Journal>(), version);
break;
default: QFAIL( "incidence type not supported" );
}
if ( !compareMimeMessage( convertedMime, msg )) {
showDiff(msg->encodedContent(), convertedMime->encodedContent());
QVERIFY( false );
}
}
void createContactsTestSet()
{
QTest::addColumn<Kolab::Version>( "version" );
QTest::addColumn<Kolab::ObjectType>( "type" );
QTest::addColumn<QString>( "vcardFileName" );
QTest::addColumn<QString>( "mimeFileName" );
// QTest::newRow( "v2contactSimple" ) << Kolab::KolabV2 << Kolab::ContactObject << TESTFILEDIR+QString::fromLatin1("v2/contacts/simple.vcf") << TESTFILEDIR+QString::fromLatin1("v2/contacts/simple.vcf.mime");
// QTest::newRow( "v2contactComplex" ) << Kolab::KolabV2 << Kolab::ContactObject << TESTFILEDIR+QString::fromLatin1("v2/contacts/complex.vcf") << TESTFILEDIR+QString::fromLatin1("v2/contacts/complex.vcf.mime");
// QTest::newRow( "v2contactAddress" ) << Kolab::KolabV2 << Kolab::ContactObject << TESTFILEDIR+QString::fromLatin1("v2/contacts/address.vcf") << TESTFILEDIR+QString::fromLatin1("v2/contacts/address.vcf.mime");
// QTest::newRow( "v2contactBug238996" ) << Kolab::KolabV2 << Kolab::ContactObject << TESTFILEDIR+QString::fromLatin1("v2/contacts/bug238996.vcf") << TESTFILEDIR+QString::fromLatin1("v2/contacts/bug238996.vcf.mime");
// QTest::newRow( "v2contactDisplayname" ) << Kolab::KolabV2 << Kolab::ContactObject << TESTFILEDIR+QString::fromLatin1("v2/contacts/displayname.vcf") << TESTFILEDIR+QString::fromLatin1("v2/contacts/displayname.vcf.mime");
// QTest::newRow( "v2contactEmails" ) << Kolab::KolabV2 << Kolab::ContactObject << TESTFILEDIR+QString::fromLatin1("v2/contacts/emails.vcf") << TESTFILEDIR+QString::fromLatin1("v2/contacts/emails.vcf.mime");
// QTest::newRow( "v2contactPhonenumbers" ) << Kolab::KolabV2 << Kolab::ContactObject << TESTFILEDIR+QString::fromLatin1("v2/contacts/phonenumbers.vcf") << TESTFILEDIR+QString::fromLatin1("v2/contacts/phonenumbers.vcf.mime");
// QTest::newRow( "v2contactPicture" ) << Kolab::KolabV2 << Kolab::ContactObject << TESTFILEDIR+QString::fromLatin1("v2/contacts/picture.vcf") << TESTFILEDIR+QString::fromLatin1("v2/contacts/picture.vcf.mime");
QTest::newRow( "v3contactSimple" ) << Kolab::KolabV3 << Kolab::ContactObject << TESTFILEDIR+QString::fromLatin1("v3/contacts/simple.vcf") << TESTFILEDIR+QString::fromLatin1("v3/contacts/simple.vcf.mime");
QTest::newRow( "v3contactComplex" ) << Kolab::KolabV3 << Kolab::ContactObject << TESTFILEDIR+QString::fromLatin1("v3/contacts/complex.vcf") << TESTFILEDIR+QString::fromLatin1("v3/contacts/complex.vcf.mime");
}
void FormatTest::testContact_data()
{
createContactsTestSet();
}
void FormatTest::testContact()
{
QFETCH( Kolab::Version, version );
QFETCH( Kolab::ObjectType, type );
QFETCH( QString, vcardFileName ); //To compare
QFETCH( QString, mimeFileName ); //For parsing
//Parse mime message
bool ok = false;
const KMime::Message::Ptr &msg = readMimeFile( mimeFileName, ok );
QVERIFY(ok);
Kolab::KolabObjectReader reader;
Kolab::ObjectType t = reader.parseMimeMessage(msg);
kDebug() << t << type;
QCOMPARE(t, type);
kDebug() << reader.getVersion() << version;
QCOMPARE(reader.getVersion(), version);
KABC::Addressee convertedAddressee = reader.getContact();
//Parse vcard
QFile vcardFile( vcardFileName );
QVERIFY( vcardFile.open( QFile::ReadOnly ) );
KABC::VCardConverter converter;
const KABC::Addressee realAddressee = converter.parseVCard( vcardFile.readAll() );
// fix up the converted addressee for comparisson
convertedAddressee.setName( realAddressee.name() ); // name() apparently is something strange
if (version == Kolab::KolabV2) { //No creation date in xcal
QVERIFY( !convertedAddressee.custom( "KOLAB", "CreationDate" ).isEmpty() );
convertedAddressee.removeCustom( "KOLAB", "CreationDate" ); // that's conversion time !?
}
QVERIFY( normalizePhoneNumbers( convertedAddressee, realAddressee ) ); // phone number ids are random
QVERIFY( normalizeAddresses( convertedAddressee, realAddressee ) ); // same here
if (realAddressee != convertedAddressee) {
showDiff(realAddressee.toString(), convertedAddressee.toString());
}
QCOMPARE( realAddressee, convertedAddressee );
//Write
const KMime::Message::Ptr &convertedMime = Kolab::KolabObjectWriter::writeContact(realAddressee, version);
if ( !compareMimeMessage( convertedMime, msg )) {
showDiff(msg->encodedContent(), convertedMime->encodedContent());
QVERIFY( false );
}
}
//This function exists only to generate the reference files, it's not a real test.
void FormatTest::generateMimefile()
{
QFile icalFile( TESTFILEDIR+QString::fromLatin1("v3/event/complex.ics") );
QVERIFY( icalFile.open( QFile::ReadOnly ) );
KCalCore::ICalFormat format;
const KCalCore::Incidence::Ptr realIncidence( format.fromString( QString::fromUtf8( icalFile.readAll() ) ) );
QString result;
QTextStream s(&result);
Kolab::overrideTimestamp(Kolab::cDateTime(2012, 5, 5, 5,5,5, true));
Kolab::KolabObjectWriter::writeEvent(realIncidence.dynamicCast<KCalCore::Event>(), Kolab::KolabV3)->toStream(s);
// QFile vcardFile( TESTFILEDIR+QString::fromLatin1("v3/contacts/complex.vcf") );
// QVERIFY( vcardFile.open( QFile::ReadOnly ) );
// KABC::VCardConverter converter;
// const KABC::Addressee realAddressee = converter.parseVCard( vcardFile.readAll() );
//
// kDebug() << realAddressee.photo().data();
//
// QString result;
// QTextStream s(&result);
// Kolab::overrideTimestamp(Kolab::cDateTime(2012, 5, 5, 5,5,5, true));
// Kolab::KolabObjectWriter::writeContact(realAddressee, Kolab::KolabV3)->toStream(s);
kDebug() << result;
}
QTEST_MAIN( FormatTest )
#include "formattest.moc"

File Metadata

Mime Type
text/x-c++
Expires
Mon, Apr 6, 12:07 AM (1 w, 4 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
18827632
Default Alt Text
formattest.cpp (16 KB)

Event Timeline