diff --git a/tests/src/com/fsck/k9/mail/internet/ViewablesTest.java b/tests/src/com/fsck/k9/mail/internet/ViewablesTest.java new file mode 100644 index 000000000..ded79c3d5 --- /dev/null +++ b/tests/src/com/fsck/k9/mail/internet/ViewablesTest.java @@ -0,0 +1,188 @@ +package com.fsck.k9.mail.internet; + +import java.util.Date; +import java.util.Locale; +import java.util.TimeZone; +import android.test.AndroidTestCase; +import com.fsck.k9.mail.Address; +import com.fsck.k9.mail.MessagingException; +import com.fsck.k9.mail.Message.RecipientType; +import com.fsck.k9.mail.internet.MimeUtility.ViewableContainer; + +public class ViewablesTest extends AndroidTestCase { + + public void testSimplePlainTextMessage() throws MessagingException { + String bodyText = "K-9 Mail rocks :>"; + + // Create text/plain body + TextBody body = new TextBody(bodyText); + + // Create message + MimeMessage message = new MimeMessage(); + message.setBody(body); + + // Extract text + ViewableContainer container = MimeUtility.extractTextAndAttachments(getContext(), message); + + String expectedText = bodyText; + String expectedHtml = + "
" + + "" + + "K-9 Mail rocks :>" + + "" + + ""; + + assertEquals(expectedText, container.text); + assertEquals(expectedHtml, container.html); + } + + public void testSimpleHtmlMessage() throws MessagingException { + String bodyText = "K-9 Mail rocks :>"; + + // Create text/plain body + TextBody body = new TextBody(bodyText); + + // Create message + MimeMessage message = new MimeMessage(); + message.setHeader("Content-Type", "text/html"); + message.setBody(body); + + // Extract text + ViewableContainer container = MimeUtility.extractTextAndAttachments(getContext(), message); + + String expectedText = "K-9 Mail rocks :>"; + String expectedHtml = + "" + + bodyText + + ""; + + assertEquals(expectedText, container.text); + assertEquals(expectedHtml, container.html); + } + + public void testMultipartPlainTextMessage() throws MessagingException { + String bodyText1 = "text body 1"; + String bodyText2 = "text body 2"; + + // Create text/plain bodies + TextBody body1 = new TextBody(bodyText1); + TextBody body2 = new TextBody(bodyText2); + + // Create multipart/mixed part + MimeMultipart multipart = new MimeMultipart(); + MimeBodyPart bodyPart1 = new MimeBodyPart(body1, "text/plain"); + MimeBodyPart bodyPart2 = new MimeBodyPart(body2, "text/plain"); + multipart.addBodyPart(bodyPart1); + multipart.addBodyPart(bodyPart2); + + // Create message + MimeMessage message = new MimeMessage(); + message.setBody(multipart); + + // Extract text + ViewableContainer container = MimeUtility.extractTextAndAttachments(getContext(), message); + + String expectedText = + bodyText1 + "\n\n" + + "------------------------------------------------------------------------\n\n" + + bodyText2; + String expectedHtml = + "" + + "
" + + bodyText1 + + "" + + "" + + "
" + + bodyText2 + + "" + + ""; + + + assertEquals(expectedText, container.text); + assertEquals(expectedHtml, container.html); + } + + public void testTextPlusRfc822Message() throws MessagingException { + Locale.setDefault(Locale.US); + TimeZone.setDefault(TimeZone.getTimeZone("UTC")); + + String bodyText = "Some text here"; + String innerBodyText = "Hey there. I'm inside a message/rfc822 (inline) attachment."; + + // Create text/plain body + TextBody textBody = new TextBody(bodyText); + + // Create inner text/plain body + TextBody innerBody = new TextBody(innerBodyText); + + // Create message/rfc822 body + MimeMessage innerMessage = new MimeMessage(); + innerMessage.addSentDate(new Date(112, 02, 17)); + innerMessage.setRecipients(RecipientType.TO, new Address[] { new Address("to@example.com") }); + innerMessage.setSubject("Subject"); + innerMessage.setFrom(new Address("from@example.com")); + innerMessage.setBody(innerBody); + + // Create multipart/mixed part + MimeMultipart multipart = new MimeMultipart(); + MimeBodyPart bodyPart1 = new MimeBodyPart(textBody, "text/plain"); + MimeBodyPart bodyPart2 = new MimeBodyPart(innerMessage, "message/rfc822"); + bodyPart2.setHeader("Content-Disposition", "inline; filename=\"message.eml\""); + multipart.addBodyPart(bodyPart1); + multipart.addBodyPart(bodyPart2); + + // Create message + MimeMessage message = new MimeMessage(); + message.setBody(multipart); + + // Extract text + ViewableContainer container = MimeUtility.extractTextAndAttachments(getContext(), message); + + String expectedText = + bodyText + + "\n\n" + + "----- message.eml ------------------------------------------------------" + + "\n\n" + + "From: from@example.com" + "\n" + + "To: to@example.com" + "\n" + + "Sent: Sat Mar 17 00:00:00 GMT+00:00 2012" + "\n" + + "Subject: Subject" + "\n" + + "\n" + + innerBodyText; + String expectedHtml = + "" + + "
" + + bodyText + + "" + + "
message.eml
" + + "From: | " + + "from@example.com | " + + "
---|---|
To: | " + + "to@example.com | " + + "
Sent: | " + + "Sat Mar 17 00:00:00 GMT+00:00 2012 | " + + "
Subject: | " + + "Subject | " + + "
" + + innerBodyText + + "" + + ""; + + assertEquals(expectedText, container.text); + assertEquals(expectedHtml, container.html); + } +}