1 /******************************************************************************* 2 * Copyright (C) 2012 Google Inc. 3 * Licensed to The Android Open Source Project. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 *******************************************************************************/ 17 18 package com.android.mail.browse; 19 20 import android.test.AndroidTestCase; 21 import android.test.suitebuilder.annotation.SmallTest; 22 import android.text.SpannableString; 23 24 import com.android.mail.providers.ConversationInfo; 25 import com.android.mail.providers.MessageInfo; 26 27 import java.util.ArrayList; 28 29 @SmallTest 30 public class SendersFormattingTests extends AndroidTestCase { 31 32 private static ConversationInfo createConversationInfo(int count) { 33 int draftCount = 5; 34 String first = "snippet", firstUnread = first, last = first; 35 return new ConversationInfo(count, draftCount, first, firstUnread, last); 36 } 37 38 public void testMe() { 39 // Blank sender == from "me" 40 ConversationInfo conv = createConversationInfo(1); 41 boolean read = false, starred = false; 42 MessageInfo info = new MessageInfo(read, starred, null, -1, null); 43 conv.addMessage(info); 44 ArrayList<SpannableString> strings = new ArrayList<SpannableString>(); 45 ArrayList<String> emailDisplays = null; 46 SendersView.format(getContext(), conv, "", 100, strings, emailDisplays, emailDisplays, 47 null, false); 48 assertEquals(1, strings.size()); 49 assertEquals(strings.get(0).toString(), "me"); 50 51 ConversationInfo conv2 = createConversationInfo(1); 52 MessageInfo info2 = new MessageInfo(read, starred, "", -1, null); 53 strings.clear(); 54 conv2.addMessage(info2); 55 SendersView.format(getContext(), conv, "", 100, strings, emailDisplays, emailDisplays, 56 null, false); 57 assertEquals(1, strings.size()); 58 assertEquals(strings.get(0).toString(), "me"); 59 60 ConversationInfo conv3 = createConversationInfo(2); 61 MessageInfo info3 = new MessageInfo(read, starred, "", -1, null); 62 conv3.addMessage(info3); 63 MessageInfo info4 = new MessageInfo(read, starred, "", -1, null); 64 conv3.addMessage(info4); 65 strings.clear(); 66 SendersView.format(getContext(), conv, "", 100, strings, emailDisplays, emailDisplays, 67 null, false); 68 assertEquals(1, strings.size()); 69 assertEquals(strings.get(0).toString(), "me"); 70 } 71 72 public void testDupes() { 73 // Duplicate sender; should only return 1 74 ArrayList<SpannableString> strings = new ArrayList<SpannableString>(); 75 ArrayList<String> emailDisplays = null; 76 ConversationInfo conv = createConversationInfo(2); 77 boolean read = false, starred = false; 78 String sender = "sender (at) sender.com"; 79 MessageInfo info = new MessageInfo(read, starred, sender, -1, null); 80 conv.addMessage(info); 81 MessageInfo info2 = new MessageInfo(read, starred, sender, -1, null); 82 conv.addMessage(info2); 83 SendersView.format(getContext(), conv, "", 100, strings, emailDisplays, emailDisplays, 84 null, false); 85 // We actually don't remove the item, we just set it to null, so count 86 // just the non-null items. 87 int count = 0; 88 for (int i = 0; i < strings.size(); i++) { 89 if (strings.get(i) != null) { 90 count++; 91 assertEquals(strings.get(i).toString(), sender); 92 } 93 } 94 assertEquals(1, count); 95 } 96 97 public void testSenderNameBadInput() { 98 final ConversationInfo conv = createConversationInfo(1); 99 final MessageInfo msg = new MessageInfo(false, false, "****^****", 0, null); 100 conv.addMessage(msg); 101 102 final byte[] serialized = conv.toBlob(); 103 104 ConversationInfo conv2 = ConversationInfo.fromBlob(serialized); 105 assertEquals(1, conv2.messageInfos.size()); 106 assertEquals(msg.sender, conv2.messageInfos.get(0).sender); 107 } 108 109 public void testConversationSnippetsBadInput() { 110 final String firstSnippet = "*^*"; 111 final String firstUnreadSnippet = "*^*^*"; 112 final String lastSnippet = "*^*^*^*"; 113 114 final ConversationInfo conv = new ConversationInfo(42, 49, firstSnippet, 115 firstUnreadSnippet, lastSnippet); 116 final MessageInfo msg = new MessageInfo(false, false, "Foo Bar", 0, null); 117 conv.addMessage(msg); 118 119 assertEquals(firstSnippet, conv.firstSnippet); 120 assertEquals(firstUnreadSnippet, conv.firstUnreadSnippet); 121 assertEquals(lastSnippet, conv.lastSnippet); 122 123 final byte[] serialized = conv.toBlob(); 124 125 ConversationInfo conv2 = ConversationInfo.fromBlob(serialized); 126 127 assertEquals(conv.firstSnippet, conv2.firstSnippet); 128 assertEquals(conv.firstUnreadSnippet, conv2.firstUnreadSnippet); 129 assertEquals(conv.lastSnippet, conv2.lastSnippet); 130 } 131 132 } 133