void AttachmentCompressJobTest::testCompress() { // Some data. QByteArray data; for( int i = 0; i < 100; i++ ) { data += "This is some highly compressible text...\n"; } const QString name = QString::fromLatin1( "name" ); const QString fileName = QString::fromLatin1( "name.txt" ); const QString description = QString::fromLatin1( "description" ); // Create the original part. AttachmentPart::Ptr origPart = AttachmentPart::Ptr( new AttachmentPart ); origPart->setName( name ); origPart->setFileName( fileName ); origPart->setDescription( description ); origPart->setMimeType( "text/plain" ); origPart->setEncoding( KMime::Headers::CE7Bit ); QVERIFY( !origPart->isAutoEncoding() ); origPart->setData( data ); QVERIFY( !origPart->isCompressed() ); // Compress the part and verify it. AttachmentCompressJob *cjob = new AttachmentCompressJob( origPart, this ); VERIFYEXEC( cjob ); QCOMPARE( cjob->originalPart(), origPart ); AttachmentPart::Ptr zipPart = cjob->compressedPart(); //kDebug() << data; //kDebug() << zipPart->data(); QVERIFY( zipPart->isAutoEncoding() ); QVERIFY( zipPart->isCompressed() ); QCOMPARE( zipPart->name(), name + QString::fromLatin1( ".zip" ) ); QCOMPARE( zipPart->fileName(), fileName + QString::fromLatin1( ".zip" ) ); QCOMPARE( zipPart->description(), description ); QCOMPARE( zipPart->mimeType(), QByteArray( "application/zip" ) ); // Uncompress the data and verify it. // (Stuff below is stolen from KMail code.) QByteArray zipData = zipPart->data(); QBuffer buffer( &zipData ); KZip zip( &buffer ); QVERIFY( zip.open( QIODevice::ReadOnly ) ); const KArchiveDirectory *dir = zip.directory(); QCOMPARE( dir->entries().count(), 1 ); const KZipFileEntry *entry = (KZipFileEntry*)dir->entry( dir->entries()[0] ); QCOMPARE( entry->data(), data ); QCOMPARE( entry->name(), name ); zip.close(); }
void AttachmentCompressJobTest::testCompressedSizeLarger() { // Some data. QByteArray data( "This is short enough that compressing it is not efficient." ); const QString name = QString::fromLatin1( "name.txt" ); const QString description = QString::fromLatin1( "description" ); // Create the original part. AttachmentPart::Ptr origPart = AttachmentPart::Ptr( new AttachmentPart ); origPart->setName( name ); origPart->setDescription( description ); origPart->setMimeType( "text/plain" ); origPart->setEncoding( KMime::Headers::CE7Bit ); QVERIFY( !origPart->isAutoEncoding() ); origPart->setData( data ); QVERIFY( !origPart->isCompressed() ); // Compress the part and verify that it is aware of its folly. AttachmentCompressJob *cjob = new AttachmentCompressJob( origPart, this ); VERIFYEXEC( cjob ); QVERIFY( cjob->isCompressedPartLarger() ); }