Search in sources :

Example 26 with Callback

use of com.fsck.k9.message.MessageBuilder.Callback in project k-9 by k9mail.

the class LockableDatabase method execute.

/**
 * Execute a DB callback in a shared context (doesn't prevent concurrent
 * shared executions), taking care of locking the DB storage.
 *
 * <p>
 * Can be instructed to start a transaction if none is currently active in
 * the current thread. Callback will participate in any active transaction (no
 * inner transaction created).
 * </p>
 *
 * @param transactional
 *            <code>true</code> the callback must be executed in a
 *            transactional context.
 * @param callback
 *            Never <code>null</code>.
 * @return Whatever {@link DbCallback#doDbWork(SQLiteDatabase)} returns.
 */
public <T> T execute(final boolean transactional, final DbCallback<T> callback) throws MessagingException {
    lockRead();
    final boolean doTransaction = transactional && inTransaction.get() == null;
    try {
        final boolean debug = K9.isDebugLoggingEnabled();
        if (doTransaction) {
            inTransaction.set(Boolean.TRUE);
            mDb.beginTransaction();
        }
        try {
            final T result = callback.doDbWork(mDb);
            if (doTransaction) {
                mDb.setTransactionSuccessful();
            }
            return result;
        } finally {
            if (doTransaction) {
                final long begin;
                if (debug) {
                    begin = System.currentTimeMillis();
                } else {
                    begin = 0L;
                }
                // not doing endTransaction in the same 'finally' block of unlockRead() because endTransaction() may throw an exception
                mDb.endTransaction();
                if (debug) {
                    Timber.v("LockableDatabase: Transaction ended, took %d ms / %s", currentTimeMillis() - begin, new Exception().getStackTrace()[1]);
                }
            }
        }
    } finally {
        if (doTransaction) {
            inTransaction.set(null);
        }
        unlockRead();
    }
}
Also used : SQLiteException(android.database.sqlite.SQLiteException) MessagingException(com.fsck.k9.mail.MessagingException)

Example 27 with Callback

use of com.fsck.k9.message.MessageBuilder.Callback in project k-9 by k9mail.

the class MessageBuilderTest method build_usingHtmlFormat_shouldUseMultipartAlternativeInCorrectOrder.

@Test
public void build_usingHtmlFormat_shouldUseMultipartAlternativeInCorrectOrder() {
    MessageBuilder messageBuilder = createHtmlMessageBuilder();
    messageBuilder.buildAsync(callback);
    MimeMessage message = getMessageFromCallback();
    assertEquals(MimeMultipart.class, message.getBody().getClass());
    assertEquals("multipart/alternative", ((MimeMultipart) message.getBody()).getMimeType());
    List<BodyPart> parts = ((MimeMultipart) message.getBody()).getBodyParts();
    // RFC 2046 - 5.1.4. - Best type is last displayable
    assertEquals("text/plain", parts.get(0).getMimeType());
    assertEquals("text/html", parts.get(1).getMimeType());
}
Also used : BodyPart(com.fsck.k9.mail.BodyPart) MimeMessage(com.fsck.k9.mail.internet.MimeMessage) MimeMultipart(com.fsck.k9.mail.internet.MimeMultipart) RobolectricTest(com.fsck.k9.RobolectricTest) Test(org.junit.Test)

Example 28 with Callback

use of com.fsck.k9.message.MessageBuilder.Callback in project k-9 by k9mail.

the class MessageBuilderTest method build_withAttachment_shouldSucceed.

@Test
public void build_withAttachment_shouldSucceed() throws Exception {
    MessageBuilder messageBuilder = createSimpleMessageBuilder();
    Attachment attachment = createAttachmentWithContent("text/plain", "attach.txt", TEST_ATTACHMENT_TEXT);
    messageBuilder.setAttachments(Collections.singletonList(attachment));
    messageBuilder.buildAsync(callback);
    MimeMessage message = getMessageFromCallback();
    assertEquals(MESSAGE_HEADERS + MESSAGE_CONTENT_WITH_ATTACH, getMessageContents(message));
}
Also used : MimeMessage(com.fsck.k9.mail.internet.MimeMessage) RobolectricTest(com.fsck.k9.RobolectricTest) Test(org.junit.Test)

Example 29 with Callback

use of com.fsck.k9.message.MessageBuilder.Callback in project k-9 by k9mail.

the class MessageBuilderTest method build_withAttachment_nonAscii_shouldSucceed.

@Test
public void build_withAttachment_nonAscii_shouldSucceed() throws Exception {
    MessageBuilder messageBuilder = createSimpleMessageBuilder();
    Attachment attachment = createAttachmentWithContent("text/plain", ATTACHMENT_FILENAME_NON_ASCII, TEST_ATTACHMENT_TEXT);
    messageBuilder.setAttachments(Collections.singletonList(attachment));
    messageBuilder.buildAsync(callback);
    MimeMessage message = getMessageFromCallback();
    assertEquals(MESSAGE_HEADERS + MESSAGE_CONTENT_WITH_ATTACH_NON_ASCII_FILENAME, getMessageContents(message));
}
Also used : MimeMessage(com.fsck.k9.mail.internet.MimeMessage) RobolectricTest(com.fsck.k9.RobolectricTest) Test(org.junit.Test)

Example 30 with Callback

use of com.fsck.k9.message.MessageBuilder.Callback in project k-9 by k9mail.

the class MessageBuilderTest method buildWithException_detachAndReattach_shouldThrow.

@Test
public void buildWithException_detachAndReattach_shouldThrow() throws MessagingException {
    Callback anotherCallback = mock(Callback.class);
    MessageBuilder messageBuilder = new SimpleMessageBuilder(messageIdGenerator, boundaryGenerator, resourceProvider) {

        @Override
        protected void buildMessageInternal() {
            queueMessageBuildException(new MessagingException("expected error"));
        }
    };
    Robolectric.getBackgroundThreadScheduler().pause();
    messageBuilder.buildAsync(callback);
    messageBuilder.detachCallback();
    Robolectric.getBackgroundThreadScheduler().unPause();
    messageBuilder.reattachCallback(anotherCallback);
    verifyNoMoreInteractions(callback);
    verify(anotherCallback).onMessageBuildException(any(MessagingException.class));
    verifyNoMoreInteractions(anotherCallback);
}
Also used : Callback(com.fsck.k9.message.MessageBuilder.Callback) MessagingException(com.fsck.k9.mail.MessagingException) RobolectricTest(com.fsck.k9.RobolectricTest) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)23 MimeMessage (com.fsck.k9.mail.internet.MimeMessage)14 Callback (com.fsck.k9.message.MessageBuilder.Callback)14 MessagingException (com.fsck.k9.mail.MessagingException)11 RobolectricTest (com.fsck.k9.RobolectricTest)10 PendingIntent (android.app.PendingIntent)9 Intent (android.content.Intent)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 OutputStream (java.io.OutputStream)9 OpenPgpDataSource (org.openintents.openpgp.util.OpenPgpApi.OpenPgpDataSource)9 ComposeCryptoStatus (com.fsck.k9.activity.compose.ComposeCryptoStatus)7 Recipient (com.fsck.k9.view.RecipientSelectView.Recipient)5 BodyPart (com.fsck.k9.mail.BodyPart)4 MimeMultipart (com.fsck.k9.mail.internet.MimeMultipart)4 IOException (java.io.IOException)3 BinaryTempFileBody (com.fsck.k9.mail.internet.BinaryTempFileBody)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 HashMap (java.util.HashMap)2 Activity (android.app.Activity)1