2012-02-17 18:25:14 -05:00
|
|
|
package com.fsck.k9.mail.internet;
|
|
|
|
|
|
|
|
import java.util.Date;
|
|
|
|
import java.util.Locale;
|
|
|
|
import java.util.TimeZone;
|
|
|
|
import android.test.AndroidTestCase;
|
2012-05-30 10:02:00 -04:00
|
|
|
|
2012-10-08 08:43:34 -04:00
|
|
|
import com.fsck.k9.activity.K9ActivityCommon;
|
2012-02-17 18:25:14 -05:00
|
|
|
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 =
|
2013-03-01 18:48:46 -05:00
|
|
|
"<pre class=\"k9mail\">" +
|
2012-02-17 18:25:14 -05:00
|
|
|
"K-9 Mail rocks :>" +
|
2013-03-01 12:30:25 -05:00
|
|
|
"</pre>";
|
2012-02-17 18:25:14 -05:00
|
|
|
|
|
|
|
assertEquals(expectedText, container.text);
|
|
|
|
assertEquals(expectedHtml, container.html);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testSimpleHtmlMessage() throws MessagingException {
|
|
|
|
String bodyText = "<strong>K-9 Mail</strong> 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 =
|
2013-03-01 12:30:25 -05:00
|
|
|
bodyText;
|
2012-02-17 18:25:14 -05:00
|
|
|
|
|
|
|
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 =
|
2013-10-08 17:07:21 -04:00
|
|
|
bodyText1 + "\r\n\r\n" +
|
|
|
|
"------------------------------------------------------------------------\r\n\r\n" +
|
2012-02-17 18:25:14 -05:00
|
|
|
bodyText2;
|
|
|
|
String expectedHtml =
|
2013-03-01 18:48:46 -05:00
|
|
|
"<pre class=\"k9mail\">" +
|
2012-02-17 18:25:14 -05:00
|
|
|
bodyText1 +
|
|
|
|
"</pre>" +
|
|
|
|
"<p style=\"margin-top: 2.5em; margin-bottom: 1em; " +
|
|
|
|
"border-bottom: 1px solid #000\"></p>" +
|
2013-03-01 18:48:46 -05:00
|
|
|
"<pre class=\"k9mail\">" +
|
2012-02-17 18:25:14 -05:00
|
|
|
bodyText2 +
|
2013-03-01 12:30:25 -05:00
|
|
|
"</pre>";
|
2012-02-17 18:25:14 -05:00
|
|
|
|
|
|
|
|
|
|
|
assertEquals(expectedText, container.text);
|
|
|
|
assertEquals(expectedHtml, container.html);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testTextPlusRfc822Message() throws MessagingException {
|
2012-10-08 08:43:34 -04:00
|
|
|
K9ActivityCommon.setLanguage(getContext(), "en");
|
2012-02-17 18:25:14 -05:00
|
|
|
Locale.setDefault(Locale.US);
|
2012-06-08 18:39:15 -04:00
|
|
|
TimeZone.setDefault(TimeZone.getTimeZone("GMT+01:00"));
|
2012-02-17 18:25:14 -05:00
|
|
|
|
|
|
|
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 +
|
2013-10-08 17:07:21 -04:00
|
|
|
"\r\n\r\n" +
|
2012-02-17 18:25:14 -05:00
|
|
|
"----- message.eml ------------------------------------------------------" +
|
2013-10-08 17:07:21 -04:00
|
|
|
"\r\n\r\n" +
|
|
|
|
"From: from@example.com" + "\r\n" +
|
|
|
|
"To: to@example.com" + "\r\n" +
|
|
|
|
"Sent: Sat Mar 17 00:00:00 GMT+01:00 2012" + "\r\n" +
|
|
|
|
"Subject: Subject" + "\r\n" +
|
|
|
|
"\r\n" +
|
2012-02-17 18:25:14 -05:00
|
|
|
innerBodyText;
|
|
|
|
String expectedHtml =
|
2013-03-01 18:48:46 -05:00
|
|
|
"<pre class=\"k9mail\">" +
|
2012-02-17 18:25:14 -05:00
|
|
|
bodyText +
|
|
|
|
"</pre>" +
|
|
|
|
"<p style=\"margin-top: 2.5em; margin-bottom: 1em; border-bottom: " +
|
|
|
|
"1px solid #000\">message.eml</p>" +
|
|
|
|
"<table style=\"border: 0\">" +
|
|
|
|
"<tr>" +
|
|
|
|
"<th style=\"text-align: left; vertical-align: top;\">From:</th>" +
|
|
|
|
"<td>from@example.com</td>" +
|
|
|
|
"</tr><tr>" +
|
|
|
|
"<th style=\"text-align: left; vertical-align: top;\">To:</th>" +
|
|
|
|
"<td>to@example.com</td>" +
|
|
|
|
"</tr><tr>" +
|
|
|
|
"<th style=\"text-align: left; vertical-align: top;\">Sent:</th>" +
|
2012-06-08 18:39:15 -04:00
|
|
|
"<td>Sat Mar 17 00:00:00 GMT+01:00 2012</td>" +
|
2012-02-17 18:25:14 -05:00
|
|
|
"</tr><tr>" +
|
|
|
|
"<th style=\"text-align: left; vertical-align: top;\">Subject:</th>" +
|
|
|
|
"<td>Subject</td>" +
|
|
|
|
"</tr>" +
|
|
|
|
"</table>" +
|
2013-03-01 18:48:46 -05:00
|
|
|
"<pre class=\"k9mail\">" +
|
2012-02-17 18:25:14 -05:00
|
|
|
innerBodyText +
|
2013-03-01 12:30:25 -05:00
|
|
|
"</pre>";
|
2012-02-17 18:25:14 -05:00
|
|
|
|
|
|
|
assertEquals(expectedText, container.text);
|
|
|
|
assertEquals(expectedHtml, container.html);
|
|
|
|
}
|
|
|
|
}
|