LegacyConversionsTests.java revision 3f1ac4da947f426775c9546f2e37206f58ce1a6e
1/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
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
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17package com.android.email;
18
19import com.android.email.mail.BodyPart;
20import com.android.email.mail.Message;
21import com.android.email.mail.MessageTestUtils;
22import com.android.email.mail.MessagingException;
23import com.android.email.mail.Part;
24import com.android.email.mail.MessageTestUtils.MessageBuilder;
25import com.android.email.mail.MessageTestUtils.MultipartBuilder;
26import com.android.email.mail.internet.MimeHeader;
27import com.android.email.mail.internet.MimeUtility;
28import com.android.email.provider.EmailContent;
29import com.android.email.provider.EmailProvider;
30import com.android.email.provider.ProviderTestUtils;
31import com.android.email.provider.EmailContent.Attachment;
32
33import android.content.ContentUris;
34import android.content.Context;
35import android.database.Cursor;
36import android.net.Uri;
37import android.test.ProviderTestCase2;
38
39import java.io.IOException;
40import java.util.ArrayList;
41
42/**
43 * Tests of the Legacy Conversions code (used by MessagingController).
44 *
45 * NOTE:  It would probably make sense to rewrite this using a MockProvider, instead of the
46 * ProviderTestCase (which is a real provider running on a temp database).  This would be more of
47 * a true "unit test".
48 *
49 * You can run this entire test case with:
50 *   runtest -c com.android.email.LegacyConversionsTests email
51 */
52public class LegacyConversionsTests extends ProviderTestCase2<EmailProvider> {
53
54    EmailProvider mProvider;
55    Context mProviderContext;
56    Context mContext;
57
58    public LegacyConversionsTests() {
59        super(EmailProvider.class, EmailProvider.EMAIL_AUTHORITY);
60    }
61
62    @Override
63    public void setUp() throws Exception {
64        super.setUp();
65        mProviderContext = getMockContext();
66        mContext = getContext();
67    }
68
69    @Override
70    public void tearDown() throws Exception {
71        super.tearDown();
72    }
73
74    /**
75     * TODO: basic Legacy -> Provider Message conversions
76     * TODO: basic Legacy -> Provider Body conversions
77     * TODO: rainy day tests of all kinds
78     */
79
80    /**
81     * Sunny day test of adding attachments from an IMAP message.
82     */
83    public void testAddAttachments() throws MessagingException, IOException {
84        // Prepare a local message to add the attachments to
85        final long accountId = 1;
86        final long mailboxId = 1;
87        final EmailContent.Message localMessage = ProviderTestUtils.setupMessage(
88                "local-message", accountId, mailboxId, false, true, mProviderContext);
89
90        // Prepare a legacy message with attachments
91        Part attachment1Part = MessageTestUtils.bodyPart("image/gif", null);
92        attachment1Part.setHeader(MimeHeader.HEADER_CONTENT_TYPE,
93                "image/gif;\n name=\"attachment1\"");
94        attachment1Part.setHeader(MimeHeader.HEADER_CONTENT_TRANSFER_ENCODING, "base64");
95        attachment1Part.setHeader(MimeHeader.HEADER_CONTENT_DISPOSITION,
96                "attachment;\n filename=\"attachment1\";\n size=100");
97        attachment1Part.setHeader(MimeHeader.HEADER_ANDROID_ATTACHMENT_STORE_DATA, "101");
98
99        Part attachment2Part = MessageTestUtils.bodyPart("image/jpg", null);
100        attachment2Part.setHeader(MimeHeader.HEADER_CONTENT_TYPE,
101                "image/jpg;\n name=\"attachment2\"");
102        attachment2Part.setHeader(MimeHeader.HEADER_CONTENT_TRANSFER_ENCODING, "base64");
103        attachment2Part.setHeader(MimeHeader.HEADER_CONTENT_DISPOSITION,
104                "attachment;\n filename=\"attachment2\";\n size=200");
105        attachment2Part.setHeader(MimeHeader.HEADER_ANDROID_ATTACHMENT_STORE_DATA, "102");
106
107        final Message legacyMessage = new MessageBuilder()
108            .setBody(new MultipartBuilder("multipart/mixed")
109                     .addBodyPart(MessageTestUtils.bodyPart("text/html", null))
110                     .addBodyPart(new MultipartBuilder("multipart/mixed")
111                             .addBodyPart((BodyPart)attachment1Part)
112                             .addBodyPart((BodyPart)attachment2Part)
113                             .buildBodyPart())
114                     .build())
115                .build();
116
117        // Now, convert from legacy to provider and see what happens
118        ArrayList<Part> viewables = new ArrayList<Part>();
119        ArrayList<Part> attachments = new ArrayList<Part>();
120        MimeUtility.collectParts(legacyMessage, viewables, attachments);
121        LegacyConversions.updateAttachments(mProviderContext, localMessage, attachments);
122
123        // Read back all attachments for message and check field values
124        Uri uri = ContentUris.withAppendedId(Attachment.MESSAGE_ID_URI, localMessage.mId);
125        Cursor c = mProviderContext.getContentResolver().query(uri, Attachment.CONTENT_PROJECTION,
126                null, null, null);
127        try {
128            assertEquals(2, c.getCount());
129            while (c.moveToNext()) {
130                Attachment attachment = Attachment.getContent(c, Attachment.class);
131                if ("101".equals(attachment.mLocation)) {
132                    checkAttachment("attachment1Part", attachment1Part, attachment);
133                } else if ("102".equals(attachment.mLocation)) {
134                    checkAttachment("attachment2Part", attachment2Part, attachment);
135                } else {
136                    fail("Unexpected attachment with location " + attachment.mLocation);
137                }
138            }
139        } finally {
140            c.close();
141        }
142    }
143
144    /**
145     * Compare attachment that was converted from Part (expected) to Provider Attachment (actual)
146     *
147     * TODO content URI should only be set if we also saved a file
148     * TODO other data encodings
149     */
150    private void checkAttachment(String tag, Part expected, EmailContent.Attachment actual)
151            throws MessagingException {
152        String contentType = MimeUtility.unfoldAndDecode(expected.getContentType());
153        String expectedName = MimeUtility.getHeaderParameter(contentType, "name");
154        assertEquals(tag, expectedName, actual.mFileName);
155        assertEquals(tag, expected.getMimeType(), actual.mMimeType);
156        String disposition = expected.getDisposition();
157        String sizeString = MimeUtility.getHeaderParameter(disposition, "size");
158        long expectedSize = Long.parseLong(sizeString);
159        assertEquals(tag, expectedSize, actual.mSize);
160        assertEquals(tag, expected.getContentId(), actual.mContentId);
161        assertNull(tag, actual.mContentUri);
162        assertTrue(tag, 0 != actual.mMessageKey);
163        String expectedPartId =
164            expected.getHeader(MimeHeader.HEADER_ANDROID_ATTACHMENT_STORE_DATA)[0];
165        assertEquals(tag, expectedPartId, actual.mLocation);
166        assertEquals(tag, "B", actual.mEncoding);
167    }
168
169    /**
170     * TODO: Sunny day test of adding attachments from a POP message.
171     */
172
173}
174