void ComposerTest::testAttachments()
{
    Composer *composer = new Composer;
    fillComposerData(composer);
    AttachmentPart::Ptr attachment = AttachmentPart::Ptr(new AttachmentPart);
    attachment->setData("abc");
    attachment->setMimeType("x-some/x-type");
    composer->addAttachmentPart(attachment);

    QVERIFY(composer->exec());
    QCOMPARE(composer->resultMessages().size(), 1);
    KMime::Message::Ptr message = composer->resultMessages().first();
    delete composer;
    composer = Q_NULLPTR;

    // multipart/mixed
    {
        QVERIFY(message->contentType(false));
        QCOMPARE(message->contentType()->mimeType(), QByteArray("multipart/mixed"));
        QCOMPARE(message->contents().count(), 2);
        // text/plain
        {
            Content *plain = message->contents().at(0);
            QVERIFY(plain->contentType(false));
            QCOMPARE(plain->contentType()->mimeType(), QByteArray("text/plain"));
        }
        // x-some/x-type (attachment)
        {
            Content *plain = message->contents().at(1);
            QVERIFY(plain->contentType(false));
            QCOMPARE(plain->contentType()->mimeType(), QByteArray("x-some/x-type"));
        }
    }
}
void ComposerTest::testAutoSave()
{
    Composer *composer = new Composer;
    fillComposerData(composer);
    AttachmentPart::Ptr attachment = AttachmentPart::Ptr(new AttachmentPart);
    attachment->setData("abc");
    attachment->setMimeType("x-some/x-type");
    composer->addAttachmentPart(attachment);

    // This tests if autosave in crash mode works without invoking an event loop, since using an
    // event loop in the crash handler would be a pretty bad idea
    composer->setAutoSave(true);
    composer->start();
    QVERIFY(composer->finished());
    QCOMPARE(composer->resultMessages().size(), 1);
    delete composer;
    composer = Q_NULLPTR;

}
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() );
}
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();
}