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 17 package com.android.email.mail.internet; 18 19 import com.android.email.Account; 20 import com.android.email.Preferences; 21 import com.android.email.mail.Message; 22 import com.android.email.mail.MessageTestUtils; 23 import com.android.email.mail.MessagingException; 24 import com.android.email.mail.MessageTestUtils.MessageBuilder; 25 import com.android.email.mail.MessageTestUtils.MultipartBuilder; 26 import com.android.email.mail.MessageTestUtils.TextBuilder; 27 import com.android.email.mail.store.LocalStore; 28 29 import android.net.Uri; 30 import android.test.AndroidTestCase; 31 import android.test.suitebuilder.annotation.MediumTest; 32 33 import java.io.IOException; 34 35 @MediumTest 36 public class EmailHtmlUtilTest extends AndroidTestCase { 37 private Account mAccount; 38 39 private static final String textTags = "<b>Plain</b> &"; 40 private static final String textSpaces = "3 spaces end."; 41 private static final String textNewlines = "ab \r\n \n \n\r\n"; 42 43 @Override setUp()44 protected void setUp() throws Exception { 45 super.setUp(); 46 Account[] accounts = Preferences.getPreferences(mContext).getAccounts(); 47 if (accounts.length > 0) 48 { 49 // This depends on getDefaultAccount() to auto-assign the default account, if necessary 50 mAccount = Preferences.getPreferences(mContext).getAccounts()[0]; 51 } 52 53 // This is needed for mime image bodypart. 54 BinaryTempFileBody.setTempDirectory(getContext().getCacheDir()); 55 } 56 57 // TODO write test for renderMessageText() 58 59 /** 60 * Tests for resolving inline image src cid: reference to content uri. 61 */ 62 testResolveInlineImage()63 public void testResolveInlineImage() throws MessagingException, IOException { 64 final LocalStore store = (LocalStore) LocalStore.newInstance(mAccount.getLocalStoreUri(), 65 mContext, null); 66 // Single cid case. 67 final String cid1 = "cid.1@android.com"; 68 final long aid1 = 10; 69 final Uri uri1 = MessageTestUtils.contentUri(aid1, mAccount); 70 final String text1 = new TextBuilder("text1 > ").addCidImg(cid1).build(" <."); 71 final String expected1 = new TextBuilder("text1 > ").addUidImg(uri1).build(" <."); 72 73 // message with cid1 74 final Message msg1 = new MessageBuilder() 75 .setBody(new MultipartBuilder("multipart/related") 76 .addBodyPart(MessageTestUtils.textPart("text/html", text1)) 77 .addBodyPart(MessageTestUtils.imagePart("image/jpeg", "<"+cid1+">", aid1, store)) 78 .build()) 79 .build(); 80 // Simple case. 81 final String actual1 = EmailHtmlUtil.resolveInlineImage( 82 getContext().getContentResolver(), mAccount, text1, msg1, 0); 83 assertEquals("one content id reference is not resolved", 84 expected1, actual1); 85 86 // Exceed recursive limit. 87 final String actual0 = EmailHtmlUtil.resolveInlineImage( 88 getContext().getContentResolver(), mAccount, text1, msg1, 10); 89 assertEquals("recursive call limit may exceeded", 90 text1, actual0); 91 92 // Multiple cids case. 93 final String cid2 = "cid.2@android.com"; 94 final long aid2 = 20; 95 final Uri uri2 = MessageTestUtils.contentUri(aid2, mAccount); 96 final String text2 = new TextBuilder("text2 ").addCidImg(cid2).build("."); 97 final String expected2 = new TextBuilder("text2 ").addUidImg(uri2).build("."); 98 99 // message with only cid2 100 final Message msg2 = new MessageBuilder() 101 .setBody(new MultipartBuilder("multipart/related") 102 .addBodyPart(MessageTestUtils.textPart("text/html", text1 + text2)) 103 .addBodyPart(MessageTestUtils.imagePart("image/gif", cid2, aid2, store)) 104 .build()) 105 .build(); 106 // cid1 is not replaced 107 final String actual2 = EmailHtmlUtil.resolveInlineImage( 108 getContext().getContentResolver(), mAccount, text1 + text2, msg2, 0); 109 assertEquals("only one of two content id is resolved", 110 text1 + expected2, actual2); 111 112 // message with cid1 and cid2 113 final Message msg3 = new MessageBuilder() 114 .setBody(new MultipartBuilder("multipart/related") 115 .addBodyPart(MessageTestUtils.textPart("text/html", text2 + text1)) 116 .addBodyPart(MessageTestUtils.imagePart("image/jpeg", cid1, aid1, store)) 117 .addBodyPart(MessageTestUtils.imagePart("image/gif", cid2, aid2, store)) 118 .build()) 119 .build(); 120 // cid1 and cid2 are replaced 121 final String actual3 = EmailHtmlUtil.resolveInlineImage( 122 getContext().getContentResolver(), mAccount, text2 + text1, msg3, 0); 123 assertEquals("two content ids are resolved correctly", 124 expected2 + expected1, actual3); 125 126 // message with many cids and normal attachments 127 final Message msg4 = new MessageBuilder() 128 .setBody(new MultipartBuilder("multipart/mixed") 129 .addBodyPart(MessageTestUtils.imagePart("image/jpeg", null, 30, store)) 130 .addBodyPart(MessageTestUtils.imagePart("application/pdf", cid1, aid1, store)) 131 .addBodyPart(new MultipartBuilder("multipart/related") 132 .addBodyPart(MessageTestUtils.textPart("text/html", text2 + text1)) 133 .addBodyPart(MessageTestUtils.imagePart("image/jpg", cid1, aid1, store)) 134 .addBodyPart(MessageTestUtils.imagePart("image/gif", cid2, aid2, store)) 135 .buildBodyPart()) 136 .addBodyPart(MessageTestUtils.imagePart("application/pdf", cid2, aid2, store)) 137 .build()) 138 .build(); 139 // cid1 and cid2 are replaced 140 final String actual4 = EmailHtmlUtil.resolveInlineImage( 141 getContext().getContentResolver(), mAccount, text2 + text1, msg4, 0); 142 assertEquals("two content ids in deep multipart level are resolved", 143 expected2 + expected1, actual4); 144 145 // No crash on null text 146 final String actual5 = EmailHtmlUtil.resolveInlineImage(getContext().getContentResolver(), 147 mAccount, null, msg4, 0); 148 assertNull(actual5); 149 } 150 151 /** 152 * Test for escapeCharacterToDisplay in plain text mode. 153 */ testEscapeCharacterToDisplayPlainText()154 public void testEscapeCharacterToDisplayPlainText() { 155 String plainTags = EmailHtmlUtil.escapeCharacterToDisplay(textTags); 156 assertEquals("plain tag", "<b>Plain</b> &", plainTags); 157 158 // Successive spaces will be escaped as " " 159 String plainSpaces = EmailHtmlUtil.escapeCharacterToDisplay(textSpaces); 160 assertEquals("plain spaces", "3 spaces end.", plainSpaces); 161 162 // Newlines will be escaped as "<br>" 163 String plainNewlines = EmailHtmlUtil.escapeCharacterToDisplay(textNewlines); 164 assertEquals("plain spaces", "ab <br> <br> <br><br>", plainNewlines); 165 166 // All combinations. 167 String textAll = textTags + "\n" + textSpaces + "\n" + textNewlines; 168 String plainAll = EmailHtmlUtil.escapeCharacterToDisplay(textAll); 169 assertEquals("plain all", 170 "<b>Plain</b> &<br>" + 171 "3 spaces end.<br>" + 172 "ab <br> <br> <br><br>", 173 plainAll); 174 } 175 } 176