diff --git a/k9mail-library/src/androidTest/java/com/fsck/k9/mail/MessageTest.java b/k9mail-library/src/test/java/com/fsck/k9/mail/MessageTest.java similarity index 59% rename from k9mail-library/src/androidTest/java/com/fsck/k9/mail/MessageTest.java rename to k9mail-library/src/test/java/com/fsck/k9/mail/MessageTest.java index 5a3607282..07dc6b336 100644 --- a/k9mail-library/src/androidTest/java/com/fsck/k9/mail/MessageTest.java +++ b/k9mail-library/src/test/java/com/fsck/k9/mail/MessageTest.java @@ -1,5 +1,6 @@ package com.fsck.k9.mail; + import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -8,8 +9,7 @@ import java.io.OutputStream; import java.util.Date; import java.util.TimeZone; -import android.support.test.InstrumentationRegistry; -import android.support.test.runner.AndroidJUnit4; +import android.content.Context; import com.fsck.k9.mail.Message.RecipientType; import com.fsck.k9.mail.internet.BinaryTempFileBody; @@ -26,137 +26,27 @@ import org.apache.james.mime4j.util.MimeUtil; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.RuntimeEnvironment; +import org.robolectric.annotation.Config; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; -@RunWith(AndroidJUnit4.class) +@RunWith(RobolectricTestRunner.class) +@Config(manifest = Config.NONE, sdk = 21) public class MessageTest { + + private Context context; + @Before public void setUp() throws Exception { - TimeZone.setDefault(TimeZone.getTimeZone("Asia/Tokyo")); - BinaryTempFileBody.setTempDirectory(InstrumentationRegistry.getTargetContext().getCacheDir()); - } + context = RuntimeEnvironment.application; - private static final String EIGHT_BIT_RESULT = - "From: from@example.com\r\n" - + "To: to@example.com\r\n" - + "Subject: Test Message\r\n" - + "Date: Wed, 28 Aug 2013 08:51:09 -0400\r\n" - + "MIME-Version: 1.0\r\n" - + "Content-Type: multipart/mixed; boundary=\"----Boundary103\"\r\n" - + "Content-Transfer-Encoding: 8bit\r\n" - + "\r\n" - + "------Boundary103\r\n" - + "Content-Type: text/plain;\r\n" - + " charset=utf-8\r\n" - + "Content-Transfer-Encoding: 8bit\r\n" - + "\r\n" - + "Testing.\r\n" - + "This is a text body with some greek characters.\r\n" - + "αβγδεζηθ\r\n" - + "End of test.\r\n" - + "\r\n" - + "------Boundary103\r\n" - + "Content-Type: text/plain;\r\n" - + " charset=utf-8\r\n" - + "Content-Transfer-Encoding: quoted-printable\r\n" - + "\r\n" - + "Testing=2E\r\n" - + "This is a text body with some greek characters=2E\r\n" - + "=CE=B1=CE=B2=CE=B3=CE=B4=CE=B5=CE=B6=CE=B7=CE=B8\r\n" - + "End of test=2E\r\n" - + "\r\n" - + "------Boundary103\r\n" - + "Content-Type: application/octet-stream\r\n" - + "Content-Transfer-Encoding: base64\r\n" - + "\r\n" - + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/\r\n" - + "\r\n" - + "------Boundary103\r\n" - + "Content-Type: message/rfc822\r\n" - + "Content-Disposition: attachment\r\n" - + "Content-Transfer-Encoding: 8bit\r\n" - + "\r\n" - + "From: from@example.com\r\n" - + "To: to@example.com\r\n" - + "Subject: Test Message\r\n" - + "Date: Wed, 28 Aug 2013 08:51:09 -0400\r\n" - + "MIME-Version: 1.0\r\n" - + "Content-Type: multipart/mixed; boundary=\"----Boundary102\"\r\n" - + "Content-Transfer-Encoding: 8bit\r\n" - + "\r\n" - + "------Boundary102\r\n" - + "Content-Type: text/plain;\r\n" - + " charset=utf-8\r\n" - + "Content-Transfer-Encoding: 8bit\r\n" - + "\r\n" - + "Testing.\r\n" - + "This is a text body with some greek characters.\r\n" - + "αβγδεζηθ\r\n" - + "End of test.\r\n" - + "\r\n" - + "------Boundary102\r\n" - + "Content-Type: text/plain;\r\n" - + " charset=utf-8\r\n" - + "Content-Transfer-Encoding: quoted-printable\r\n" - + "\r\n" - + "Testing=2E\r\n" - + "This is a text body with some greek characters=2E\r\n" - + "=CE=B1=CE=B2=CE=B3=CE=B4=CE=B5=CE=B6=CE=B7=CE=B8\r\n" - + "End of test=2E\r\n" - + "\r\n" - + "------Boundary102\r\n" - + "Content-Type: application/octet-stream\r\n" - + "Content-Transfer-Encoding: base64\r\n" - + "\r\n" - + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/\r\n" - + "\r\n" - + "------Boundary102\r\n" - + "Content-Type: message/rfc822\r\n" - + "Content-Disposition: attachment\r\n" - + "Content-Transfer-Encoding: 8bit\r\n" - + "\r\n" - + "From: from@example.com\r\n" - + "To: to@example.com\r\n" - + "Subject: Test Message\r\n" - + "Date: Wed, 28 Aug 2013 08:51:09 -0400\r\n" - + "MIME-Version: 1.0\r\n" - + "Content-Type: multipart/mixed; boundary=\"----Boundary101\"\r\n" - + "Content-Transfer-Encoding: 8bit\r\n" - + "\r\n" - + "------Boundary101\r\n" - + "Content-Type: text/plain;\r\n" - + " charset=utf-8\r\n" - + "Content-Transfer-Encoding: 8bit\r\n" - + "\r\n" - + "Testing.\r\n" - + "This is a text body with some greek characters.\r\n" - + "αβγδεζηθ\r\n" - + "End of test.\r\n" - + "\r\n" - + "------Boundary101\r\n" - + "Content-Type: text/plain;\r\n" - + " charset=utf-8\r\n" - + "Content-Transfer-Encoding: quoted-printable\r\n" - + "\r\n" - + "Testing=2E\r\n" - + "This is a text body with some greek characters=2E\r\n" - + "=CE=B1=CE=B2=CE=B3=CE=B4=CE=B5=CE=B6=CE=B7=CE=B8\r\n" - + "End of test=2E\r\n" - + "\r\n" - + "------Boundary101\r\n" - + "Content-Type: application/octet-stream\r\n" - + "Content-Transfer-Encoding: base64\r\n" - + "\r\n" - + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/\r\n" - + "\r\n" - + "------Boundary101--\r\n" - + "\r\n" - + "------Boundary102--\r\n" - + "\r\n" - + "------Boundary103--\r\n"; + TimeZone.setDefault(TimeZone.getTimeZone("Asia/Tokyo")); + BinaryTempFileBody.setTempDirectory(context.getCacheDir()); + } private static final String SEVEN_BIT_RESULT = "From: from@example.com\r\n" @@ -168,19 +58,9 @@ public class MessageTest { + "Content-Transfer-Encoding: 7bit\r\n" + "\r\n" + "------Boundary103\r\n" + + "Content-Transfer-Encoding: quoted-printable\r\n" + "Content-Type: text/plain;\r\n" + " charset=utf-8\r\n" - + "Content-Transfer-Encoding: quoted-printable\r\n" - + "\r\n" - + "Testing=2E\r\n" - + "This is a text body with some greek characters=2E\r\n" - + "=CE=B1=CE=B2=CE=B3=CE=B4=CE=B5=CE=B6=CE=B7=CE=B8\r\n" - + "End of test=2E\r\n" - + "\r\n" - + "------Boundary103\r\n" - + "Content-Type: text/plain;\r\n" - + " charset=utf-8\r\n" - + "Content-Transfer-Encoding: quoted-printable\r\n" + "\r\n" + "Testing=2E\r\n" + "This is a text body with some greek characters=2E\r\n" @@ -207,19 +87,9 @@ public class MessageTest { + "Content-Transfer-Encoding: 7bit\r\n" + "\r\n" + "------Boundary102\r\n" + + "Content-Transfer-Encoding: quoted-printable\r\n" + "Content-Type: text/plain;\r\n" + " charset=utf-8\r\n" - + "Content-Transfer-Encoding: quoted-printable\r\n" - + "\r\n" - + "Testing=2E\r\n" - + "This is a text body with some greek characters=2E\r\n" - + "=CE=B1=CE=B2=CE=B3=CE=B4=CE=B5=CE=B6=CE=B7=CE=B8\r\n" - + "End of test=2E\r\n" - + "\r\n" - + "------Boundary102\r\n" - + "Content-Type: text/plain;\r\n" - + " charset=utf-8\r\n" - + "Content-Transfer-Encoding: quoted-printable\r\n" + "\r\n" + "Testing=2E\r\n" + "This is a text body with some greek characters=2E\r\n" @@ -246,19 +116,9 @@ public class MessageTest { + "Content-Transfer-Encoding: 7bit\r\n" + "\r\n" + "------Boundary101\r\n" + + "Content-Transfer-Encoding: quoted-printable\r\n" + "Content-Type: text/plain;\r\n" + " charset=utf-8\r\n" - + "Content-Transfer-Encoding: quoted-printable\r\n" - + "\r\n" - + "Testing=2E\r\n" - + "This is a text body with some greek characters=2E\r\n" - + "=CE=B1=CE=B2=CE=B3=CE=B4=CE=B5=CE=B6=CE=B7=CE=B8\r\n" - + "End of test=2E\r\n" - + "\r\n" - + "------Boundary101\r\n" - + "Content-Type: text/plain;\r\n" - + " charset=utf-8\r\n" - + "Content-Transfer-Encoding: quoted-printable\r\n" + "\r\n" + "Testing=2E\r\n" + "This is a text body with some greek characters=2E\r\n" @@ -279,22 +139,12 @@ public class MessageTest { private static final String TO_BODY_PART_RESULT = "Content-Type: multipart/mixed; boundary=\"----Boundary103\"\r\n" - + "Content-Transfer-Encoding: 8bit\r\n" + + "Content-Transfer-Encoding: 7bit\r\n" + "\r\n" + "------Boundary103\r\n" - + "Content-Type: text/plain;\r\n" - + " charset=utf-8\r\n" - + "Content-Transfer-Encoding: 8bit\r\n" - + "\r\n" - + "Testing.\r\n" - + "This is a text body with some greek characters.\r\n" - + "αβγδεζηθ\r\n" - + "End of test.\r\n" - + "\r\n" - + "------Boundary103\r\n" - + "Content-Type: text/plain;\r\n" - + " charset=utf-8\r\n" + "Content-Transfer-Encoding: quoted-printable\r\n" + + "Content-Type: text/plain;\r\n" + + " charset=utf-8\r\n" + "\r\n" + "Testing=2E\r\n" + "This is a text body with some greek characters=2E\r\n" @@ -310,7 +160,7 @@ public class MessageTest { + "------Boundary103\r\n" + "Content-Type: message/rfc822\r\n" + "Content-Disposition: attachment\r\n" - + "Content-Transfer-Encoding: 8bit\r\n" + + "Content-Transfer-Encoding: 7bit\r\n" + "\r\n" + "From: from@example.com\r\n" + "To: to@example.com\r\n" @@ -318,22 +168,12 @@ public class MessageTest { + "Date: Wed, 28 Aug 2013 08:51:09 -0400\r\n" + "MIME-Version: 1.0\r\n" + "Content-Type: multipart/mixed; boundary=\"----Boundary102\"\r\n" - + "Content-Transfer-Encoding: 8bit\r\n" + + "Content-Transfer-Encoding: 7bit\r\n" + "\r\n" + "------Boundary102\r\n" - + "Content-Type: text/plain;\r\n" - + " charset=utf-8\r\n" - + "Content-Transfer-Encoding: 8bit\r\n" - + "\r\n" - + "Testing.\r\n" - + "This is a text body with some greek characters.\r\n" - + "αβγδεζηθ\r\n" - + "End of test.\r\n" - + "\r\n" - + "------Boundary102\r\n" - + "Content-Type: text/plain;\r\n" - + " charset=utf-8\r\n" + "Content-Transfer-Encoding: quoted-printable\r\n" + + "Content-Type: text/plain;\r\n" + + " charset=utf-8\r\n" + "\r\n" + "Testing=2E\r\n" + "This is a text body with some greek characters=2E\r\n" @@ -349,7 +189,7 @@ public class MessageTest { + "------Boundary102\r\n" + "Content-Type: message/rfc822\r\n" + "Content-Disposition: attachment\r\n" - + "Content-Transfer-Encoding: 8bit\r\n" + + "Content-Transfer-Encoding: 7bit\r\n" + "\r\n" + "From: from@example.com\r\n" + "To: to@example.com\r\n" @@ -357,22 +197,12 @@ public class MessageTest { + "Date: Wed, 28 Aug 2013 08:51:09 -0400\r\n" + "MIME-Version: 1.0\r\n" + "Content-Type: multipart/mixed; boundary=\"----Boundary101\"\r\n" - + "Content-Transfer-Encoding: 8bit\r\n" + + "Content-Transfer-Encoding: 7bit\r\n" + "\r\n" + "------Boundary101\r\n" - + "Content-Type: text/plain;\r\n" - + " charset=utf-8\r\n" - + "Content-Transfer-Encoding: 8bit\r\n" - + "\r\n" - + "Testing.\r\n" - + "This is a text body with some greek characters.\r\n" - + "αβγδεζηθ\r\n" - + "End of test.\r\n" - + "\r\n" - + "------Boundary101\r\n" - + "Content-Type: text/plain;\r\n" - + " charset=utf-8\r\n" + "Content-Transfer-Encoding: quoted-printable\r\n" + + "Content-Type: text/plain;\r\n" + + " charset=utf-8\r\n" + "\r\n" + "Testing=2E\r\n" + "This is a text body with some greek characters=2E\r\n" @@ -423,19 +253,12 @@ public class MessageTest { MimeMessage message; ByteArrayOutputStream out; - BinaryTempFileBody.setTempDirectory(InstrumentationRegistry.getTargetContext().getCacheDir()); + BinaryTempFileBody.setTempDirectory(context.getCacheDir()); mMimeBoundary = 101; message = nestedMessage(nestedMessage(sampleMessage())); out = new ByteArrayOutputStream(); message.writeTo(out); - assertEquals(EIGHT_BIT_RESULT, out.toString()); - - mMimeBoundary = 101; - message = nestedMessage(nestedMessage(sampleMessage())); - message.setUsing7bitTransport(); - out = new ByteArrayOutputStream(); - message.writeTo(out); assertEquals(SEVEN_BIT_RESULT, out.toString()); } @@ -452,7 +275,7 @@ public class MessageTest { MimeBodyPart bodyPart = new MimeBodyPart(tempMessageBody, "message/rfc822"); bodyPart.setHeader(MimeHeader.HEADER_CONTENT_DISPOSITION, "attachment"); - bodyPart.setEncoding(MimeUtil.ENC_8BIT); + bodyPart.setEncoding(MimeUtil.ENC_7BIT); MimeMessage parentMessage = sampleMessage(); ((Multipart) parentMessage.getBody()).addBodyPart(bodyPart); @@ -466,11 +289,10 @@ public class MessageTest { message.setRecipient(RecipientType.TO, new Address("to@example.com")); message.setSubject("Test Message"); message.setHeader("Date", "Wed, 28 Aug 2013 08:51:09 -0400"); - message.setEncoding(MimeUtil.ENC_8BIT); + message.setEncoding(MimeUtil.ENC_7BIT); MimeMultipart multipartBody = new MimeMultipart("multipart/mixed", generateBoundary()); - multipartBody.addBodyPart(textBodyPart(MimeUtil.ENC_8BIT)); - multipartBody.addBodyPart(textBodyPart(MimeUtil.ENC_QUOTED_PRINTABLE)); + multipartBody.addBodyPart(textBodyPart()); multipartBody.addBodyPart(binaryBodyPart()); MimeMessageHelper.setBody(message, multipartBody); @@ -501,17 +323,17 @@ public class MessageTest { return bodyPart; } - private MimeBodyPart textBodyPart(String encoding) - throws MessagingException { + private MimeBodyPart textBodyPart() throws MessagingException { TextBody textBody = new TextBody( "Testing.\r\n" + "This is a text body with some greek characters.\r\n" + "αβγδεζηθ\r\n" + "End of test.\r\n"); textBody.setCharset("utf-8"); - MimeBodyPart bodyPart = new MimeBodyPart(textBody, "text/plain"); + + MimeBodyPart bodyPart = new MimeBodyPart(); + MimeMessageHelper.setBody(bodyPart, textBody); CharsetSupport.setCharset("utf-8", bodyPart); - bodyPart.setEncoding(encoding); return bodyPart; } @@ -524,7 +346,7 @@ public class MessageTest { MimeMessage message; ByteArrayOutputStream out; - BinaryTempFileBody.setTempDirectory(InstrumentationRegistry.getTargetContext().getCacheDir()); + BinaryTempFileBody.setTempDirectory(context.getCacheDir()); mMimeBoundary = 101; message = nestedMessage(nestedMessage(sampleMessage()));