2 * Copyright (C) 2009 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.android.email.mail.transport;
19 import com.android.email.R;
20 import com.android.email.mail.MessagingException;
21 import com.android.email.provider.EmailProvider;
22 import com.android.email.provider.EmailContent.Attachment;
23 import com.android.email.provider.EmailContent.Message;
25 import org.apache.james.mime4j.field.Field;
26 import org.apache.james.mime4j.message.Body;
27 import org.apache.james.mime4j.message.Entity;
28 import org.apache.james.mime4j.message.Header;
29 import org.apache.james.mime4j.message.Multipart;
31 import android.content.Context;
32 import android.test.ProviderTestCase2;
34 import java.io.ByteArrayInputStream;
35 import java.io.ByteArrayOutputStream;
36 import java.io.IOException;
37 import java.util.ArrayList;
38 import java.util.List;
42 * Tests of the Rfc822Output (used for sending mail)
44 * You can run this entire test case with:
45 * runtest -c com.android.email.mail.transport.Rfc822OutputTests email
47 public class Rfc822OutputTests extends ProviderTestCase2<EmailProvider> {
48 private static final String SENDER = "sender@android.com";
49 private static final String REPLYTO = "replyto@android.com";
50 private static final String RECIPIENT_TO = "recipient-to@android.com";
51 private static final String RECIPIENT_CC = "recipient-cc@android.com";
52 private static final String RECIPIENT_BCC = "recipient-bcc@android.com";
53 private static final String SUBJECT = "This is the subject";
54 private static final String BODY = "This is the body. This is also the body.";
55 private static final String TEXT = "Here is some new text.";
57 private Context mMockContext;
58 private String mForwardIntro;
59 private String mReplyIntro;
60 private String mReplyBody;
62 public Rfc822OutputTests () {
63 super(EmailProvider.class, EmailProvider.EMAIL_AUTHORITY);
67 public void setUp() throws Exception {
69 mMockContext = getMockContext();
70 mForwardIntro = mMockContext.getString(R.string.message_compose_fwd_header_fmt, SUBJECT,
71 SENDER, RECIPIENT_TO, RECIPIENT_CC);
72 mReplyIntro = mMockContext.getString(R.string.message_compose_reply_header_fmt, SENDER);
73 mReplyBody = mReplyIntro + ">" + BODY;
76 // TODO Create more tests here. Specifically, we should test to make sure that forward works
77 // properly instead of just reply
79 // TODO Write test that ensures that bcc is handled properly (i.e. sent/not send depending
80 // on the flag passed to writeTo
83 * Test for buildBodyText().
84 * Compare with expected values.
85 * Also test the situation where the message has no body.
87 public void testBuildBodyTextWithReply() {
88 // Create the least necessary; sender, flags, and the body of the reply
89 Message msg = new Message();
92 msg.mFlags = Message.FLAG_TYPE_REPLY;
93 msg.mTextReply = BODY;
94 msg.mIntroText = mReplyIntro;
95 msg.save(mMockContext);
97 String body = Rfc822Output.buildBodyText(mMockContext, msg, true);
98 assertEquals(mReplyBody, body);
100 // Save a different message with no reply body (so we reset the id)
102 msg.mTextReply = null;
103 msg.save(mMockContext);
104 body = Rfc822Output.buildBodyText(mMockContext, msg, true);
105 assertEquals(mReplyIntro, body);
109 * Test for buildBodyText().
110 * Compare with expected values.
111 * Also test the situation where the message has no body.
113 public void testBuildBodyTextWithoutReply() {
114 // Create the least necessary; sender, flags, and the body of the reply
115 Message msg = new Message();
118 msg.mFlags = Message.FLAG_TYPE_REPLY;
119 msg.mTextReply = BODY;
120 msg.mIntroText = mReplyIntro;
121 msg.save(mMockContext);
123 String body = Rfc822Output.buildBodyText(mMockContext, msg, false);
124 assertEquals(TEXT + mReplyIntro, body);
126 // Save a different message with no reply body (so we reset the id)
128 msg.mTextReply = null;
129 msg.save(mMockContext);
130 body = Rfc822Output.buildBodyText(mMockContext, msg, false);
131 assertEquals(TEXT + mReplyIntro, body);
135 * Test for buildBodyText().
136 * Compare with expected values.
138 public void testBuildBodyTextWithForward() {
139 Message msg = new Message();
142 msg.mTo = RECIPIENT_TO;
143 msg.mCc = RECIPIENT_CC;
144 msg.mSubject = SUBJECT;
145 msg.mFlags = Message.FLAG_TYPE_FORWARD;
146 msg.mTextReply = BODY;
147 msg.mIntroText = mForwardIntro;
148 msg.save(mMockContext);
149 String body = Rfc822Output.buildBodyText(mMockContext, msg, true);
150 assertEquals(TEXT + mForwardIntro + BODY, body);
153 public void testWriteToText() throws IOException, MessagingException {
154 // Create a simple text message
155 Message msg = new Message();
159 msg.save(mMockContext);
161 // Write out an Rfc822 message
162 ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
163 Rfc822Output.writeTo(mMockContext, msg.mId, byteStream, false, false);
165 // Get the message and create a mime4j message from it
166 // We'll take advantage of its parsing capabilities
167 ByteArrayInputStream messageInputStream =
168 new ByteArrayInputStream(byteStream.toByteArray());
169 org.apache.james.mime4j.message.Message mimeMessage =
170 new org.apache.james.mime4j.message.Message(messageInputStream);
172 // Make sure its structure is correct
173 checkMimeVersion(mimeMessage);
174 assertFalse(mimeMessage.isMultipart());
175 assertEquals("text/plain", mimeMessage.getMimeType());
178 @SuppressWarnings("unchecked")
179 public void testWriteToAlternativePart() throws IOException, MessagingException {
180 // Create a message with alternative part
181 Message msg = new Message();
184 msg.mAttachments = new ArrayList<Attachment>();
185 // Attach a meeting invitation, which needs to be sent as multipart/alternative
186 Attachment att = new Attachment();
187 att.mContentBytes = "__CONTENT__".getBytes("UTF-8");
188 att.mFlags = Attachment.FLAG_ICS_ALTERNATIVE_PART;
189 att.mMimeType = "text/calendar";
190 att.mFileName = "invite.ics";
191 msg.mAttachments.add(att);
193 msg.save(mMockContext);
195 // Write out an Rfc822 message
196 ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
197 Rfc822Output.writeTo(mMockContext, msg.mId, byteStream, false, false);
199 // Get the message and create a mime4j message from it
200 // We'll take advantage of its parsing capabilities
201 ByteArrayInputStream messageInputStream =
202 new ByteArrayInputStream(byteStream.toByteArray());
203 org.apache.james.mime4j.message.Message mimeMessage =
204 new org.apache.james.mime4j.message.Message(messageInputStream);
206 // Make sure its structure is correct
207 checkMimeVersion(mimeMessage);
208 assertTrue(mimeMessage.isMultipart());
209 Header header = mimeMessage.getHeader();
210 Field contentType = header.getField("content-type");
211 assertTrue(contentType.getBody().contains("multipart/alternative"));
212 Multipart multipart = (Multipart)mimeMessage.getBody();
213 List<Body> partList = multipart.getBodyParts();
214 assertEquals(2, partList.size());
215 Entity part = (Entity)partList.get(0);
216 assertEquals("text/plain", part.getMimeType());
217 part = (Entity)partList.get(1);
218 assertEquals("text/calendar", part.getMimeType());
219 header = part.getHeader();
220 assertNull(header.getField("content-disposition"));
223 @SuppressWarnings("unchecked")
224 public void testWriteToMixedPart() throws IOException, MessagingException {
225 // Create a message with a mixed part
226 Message msg = new Message();
229 msg.mAttachments = new ArrayList<Attachment>();
230 // Attach a simple html "file"
231 Attachment att = new Attachment();
232 att.mContentBytes = "<html>Hi</html>".getBytes("UTF-8");
233 att.mMimeType = "text/html";
234 att.mFileName = "test.html";
235 msg.mAttachments.add(att);
237 msg.save(mMockContext);
239 // Write out an Rfc822 message
240 ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
241 Rfc822Output.writeTo(mMockContext, msg.mId, byteStream, false, false);
243 // Get the message and create a mime4j message from it
244 // We'll take advantage of its parsing capabilities
245 ByteArrayInputStream messageInputStream =
246 new ByteArrayInputStream(byteStream.toByteArray());
247 org.apache.james.mime4j.message.Message mimeMessage =
248 new org.apache.james.mime4j.message.Message(messageInputStream);
250 // Make sure its structure is correct
251 checkMimeVersion(mimeMessage);
252 assertTrue(mimeMessage.isMultipart());
253 Header header = mimeMessage.getHeader();
254 Field contentType = header.getField("content-type");
255 assertTrue(contentType.getBody().contains("multipart/mixed"));
256 Multipart multipart = (Multipart)mimeMessage.getBody();
257 List<Body> partList = multipart.getBodyParts();
258 assertEquals(2, partList.size());
259 Entity part = (Entity)partList.get(0);
260 assertEquals("text/plain", part.getMimeType());
261 part = (Entity)partList.get(1);
262 assertEquals("text/html", part.getMimeType());
263 header = part.getHeader();
264 assertNotNull(header.getField("content-disposition"));
268 * Confirm that the constructed message includes "MIME-VERSION: 1.0"
270 private void checkMimeVersion(org.apache.james.mime4j.message.Message mimeMessage) {
271 Header header = mimeMessage.getHeader();
272 Field contentType = header.getField("MIME-VERSION");
273 assertTrue(contentType.getBody().equals("1.0"));