SendersFormattingTests.java revision ca87392454b642a9f3df68f7ccb56da3ea70fe45
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 18package com.android.mail.browse; 19 20import android.test.AndroidTestCase; 21import android.test.suitebuilder.annotation.SmallTest; 22import android.text.SpannableString; 23 24import com.android.mail.providers.Account; 25import com.android.mail.providers.ConversationInfo; 26import com.android.mail.providers.ParticipantInfo; 27import com.android.mail.providers.UIProvider; 28import com.google.common.collect.Lists; 29 30import org.json.JSONException; 31import org.json.JSONObject; 32 33import java.util.ArrayList; 34import java.util.HashMap; 35import java.util.Map; 36 37@SmallTest 38public class SendersFormattingTests extends AndroidTestCase { 39 40 private static ConversationInfo createConversationInfo() { 41 return new ConversationInfo(0, 5, "snippet", "snippet", "snippet"); 42 } 43 44 public void testMeFromNullName() { 45 final ConversationInfo conv = createConversationInfo(); 46 conv.addParticipant(new ParticipantInfo(null, "something@somewhere.com", 0, false)); 47 final ArrayList<SpannableString> strings = Lists.newArrayList(); 48 assertEquals(0, strings.size()); 49 50 final Account account = createAccount(); 51 SendersView.format(getContext(), conv, "", 100, strings, null, null, account, false, false); 52 assertEquals(1, strings.size()); 53 assertEquals("me", strings.get(0).toString()); 54 } 55 56 public void testMeFromEmptyName() { 57 final ConversationInfo conv = createConversationInfo(); 58 conv.addParticipant(new ParticipantInfo("", "something@somewhere.com", 0, false)); 59 final ArrayList<SpannableString> strings = Lists.newArrayList(); 60 assertEquals(0, strings.size()); 61 62 final Account account = createAccount(); 63 SendersView.format(getContext(), conv, "", 100, strings, null, null, account, false, false); 64 assertEquals(1, strings.size()); 65 assertEquals("me", strings.get(0).toString()); 66 } 67 68 public void testMeFromDuplicateEmptyNames() { 69 final ConversationInfo conv = createConversationInfo(); 70 conv.addParticipant(new ParticipantInfo("", "something@somewhere.com", 0, false)); 71 conv.addParticipant(new ParticipantInfo("", "something@somewhere.com", 0, false)); 72 final ArrayList<SpannableString> strings = Lists.newArrayList(); 73 assertEquals(0, strings.size()); 74 75 final Account account = createAccount(); 76 SendersView.format(getContext(), conv, "", 100, strings, null, null, account, false, false); 77 assertEquals(2, strings.size()); 78 assertNull(strings.get(0)); 79 assertEquals("me", strings.get(1).toString()); 80 } 81 82 public void testDuplicates() { 83 final ConversationInfo conv = createConversationInfo(); 84 conv.addParticipant(new ParticipantInfo("Something", "something@somewhere.com", 0, false)); 85 conv.addParticipant(new ParticipantInfo("Something", "something@somewhere.com", 0, false)); 86 87 final ArrayList<SpannableString> strings = Lists.newArrayList(); 88 assertEquals(0, strings.size()); 89 90 final Account account = createAccount(); 91 SendersView.format(getContext(), conv, "", 100, strings, null, null, account, false, false); 92 assertEquals(2, strings.size()); 93 assertNull(strings.get(0)); 94 assertEquals("Something", strings.get(1).toString()); 95 } 96 97 public void testSenderNameBadInput() { 98 final ConversationInfo before = createConversationInfo(); 99 before.addParticipant(new ParticipantInfo("****^****", null, 0, false)); 100 101 final byte[] serialized = before.toBlob(); 102 103 final ConversationInfo after = ConversationInfo.fromBlob(serialized); 104 assertEquals(1, after.participantInfos.size()); 105 assertEquals(before.participantInfos.get(0).name, after.participantInfos.get(0).name); 106 } 107 108 public void testConversationSnippetsBadInput() { 109 final String first = "*^*"; 110 final String firstUnread = "*^*^*"; 111 final String last = "*^*^*^*"; 112 113 final ConversationInfo before = new ConversationInfo(42, 49, first, firstUnread, last); 114 before.addParticipant(new ParticipantInfo("Foo Bar", "foo@bar.com", 0, false)); 115 assertEquals(first, before.firstSnippet); 116 assertEquals(firstUnread, before.firstUnreadSnippet); 117 assertEquals(last, before.lastSnippet); 118 119 final byte[] serialized = before.toBlob(); 120 121 final ConversationInfo after = ConversationInfo.fromBlob(serialized); 122 assertEquals(before.firstSnippet, after.firstSnippet); 123 assertEquals(before.firstUnreadSnippet, after.firstUnreadSnippet); 124 assertEquals(before.lastSnippet, after.lastSnippet); 125 } 126 127 public void testSenderAvatarIsSenderOfFirstUnreadMessage() { 128 final ConversationInfo conv = createConversationInfo(); 129 conv.addParticipant(new ParticipantInfo("a", "a@a.com", 0, true)); 130 conv.addParticipant(new ParticipantInfo("b", "b@b.com", 0, false)); 131 conv.addParticipant(new ParticipantInfo("c", "c@c.com", 0, false)); 132 133 final ArrayList<SpannableString> styledSenders = Lists.newArrayList(); 134 final ArrayList<String> displayableSenderNames = Lists.newArrayList(); 135 final ConversationItemViewModel.SenderAvatarModel senderAvatarModel = 136 new ConversationItemViewModel.SenderAvatarModel(); 137 138 final Account account = createAccount(); 139 SendersView.format(getContext(), conv, "", 100, styledSenders, displayableSenderNames, 140 senderAvatarModel, account, false, false); 141 142 assertEquals("b@b.com", senderAvatarModel.getEmailAddress()); 143 assertEquals("b", senderAvatarModel.getName()); 144 } 145 146 public void testSenderAvatarIsLastSenderIfAllMessagesAreRead() { 147 final ConversationInfo conv = createConversationInfo(); 148 conv.addParticipant(new ParticipantInfo("a", "a@a.com", 0, true)); 149 conv.addParticipant(new ParticipantInfo("b", "b@b.com", 0, true)); 150 conv.addParticipant(new ParticipantInfo("c", "c@c.com", 0, true)); 151 152 final ArrayList<SpannableString> styledSenders = Lists.newArrayList(); 153 final ArrayList<String> displayableSenderNames = Lists.newArrayList(); 154 final ConversationItemViewModel.SenderAvatarModel senderAvatarModel = 155 new ConversationItemViewModel.SenderAvatarModel(); 156 157 final Account account = createAccount(); 158 SendersView.format(getContext(), conv, "", 100, styledSenders, displayableSenderNames, 159 senderAvatarModel, account, false, false); 160 161 assertEquals("c@c.com", senderAvatarModel.getEmailAddress()); 162 assertEquals("c", senderAvatarModel.getName()); 163 } 164 165 public void testSenderAvatarIsLastSenderThatIsNotTheCurrentAccountIfAllMessagesAreRead() { 166 final ConversationInfo conv = createConversationInfo(); 167 conv.addParticipant(new ParticipantInfo("a", "a@a.com", 0, true)); 168 conv.addParticipant(new ParticipantInfo("b", "b@b.com", 0, true)); 169 // empty name indicates it is the current account 170 conv.addParticipant(new ParticipantInfo("", "c@c.com", 0, true)); 171 172 final ArrayList<SpannableString> styledSenders = Lists.newArrayList(); 173 final ArrayList<String> displayableSenderNames = Lists.newArrayList(); 174 final ConversationItemViewModel.SenderAvatarModel senderAvatarModel = 175 new ConversationItemViewModel.SenderAvatarModel(); 176 177 final Account account = createAccount(); 178 SendersView.format(getContext(), conv, "", 100, styledSenders, displayableSenderNames, 179 senderAvatarModel, account, false, false); 180 181 assertEquals("b@b.com", senderAvatarModel.getEmailAddress()); 182 assertEquals("b", senderAvatarModel.getName()); 183 } 184 185 public void testSenderAvatarIsCurrentAccountIfAllSendersAreCurrentAccount() { 186 final ConversationInfo conv = createConversationInfo(); 187 // empty name indicates it is the current account 188 conv.addParticipant(new ParticipantInfo("", "a@a.com", 0, true)); 189 190 final ArrayList<SpannableString> styledSenders = Lists.newArrayList(); 191 final ArrayList<String> displayableSenderNames = Lists.newArrayList(); 192 final ConversationItemViewModel.SenderAvatarModel senderAvatarModel = 193 new ConversationItemViewModel.SenderAvatarModel(); 194 195 final Account account = createAccount(); 196 SendersView.format(getContext(), conv, "", 100, styledSenders, displayableSenderNames, 197 senderAvatarModel, account, false, false); 198 199 assertEquals("fflintstone@example.com", senderAvatarModel.getEmailAddress()); 200 assertEquals("Fred Flintstone", senderAvatarModel.getName()); 201 } 202 203 /** 204 * Two senders in a thread should be kept distinct if they have unique email addresses, even if 205 * they happen to share the same name. 206 */ 207 public void testSenderNamesWhenNamesMatchButEmailAddressesDiffer() { 208 final ConversationInfo conv = createConversationInfo(); 209 conv.addParticipant(new ParticipantInfo("Andrew", "aholmes@awesome.com", 0, true)); 210 conv.addParticipant(new ParticipantInfo("Andrew", "ajohnson@wicked.com", 0, true)); 211 212 final ArrayList<SpannableString> styledSenders = Lists.newArrayList(); 213 final ArrayList<String> displayableSenderNames = Lists.newArrayList(); 214 final ConversationItemViewModel.SenderAvatarModel senderAvatarModel = 215 new ConversationItemViewModel.SenderAvatarModel(); 216 217 final Account account = createAccount(); 218 SendersView.format(getContext(), conv, "", 100, styledSenders, displayableSenderNames, 219 senderAvatarModel, account, false, false); 220 221 assertEquals(2, displayableSenderNames.size()); 222 assertEquals("Andrew", displayableSenderNames.get(0)); 223 assertEquals("Andrew", displayableSenderNames.get(1)); 224 } 225 226 private static Account createAccount() { 227 try { 228 final Map<String, Object> map = new HashMap<>(2); 229 map.put(UIProvider.AccountColumns.NAME, "Fred Flintstone"); 230 map.put(UIProvider.AccountColumns.ACCOUNT_MANAGER_NAME, "fflintstone@example.com"); 231 map.put(UIProvider.AccountColumns.TYPE, "IMAP"); 232 map.put(UIProvider.AccountColumns.PROVIDER_VERSION, 1); 233 map.put(UIProvider.AccountColumns.CAPABILITIES, 0); 234 235 final JSONObject json = new JSONObject(map); 236 237 return Account.builder().buildFrom(json); 238 } catch (JSONException je) { 239 throw new RuntimeException(je); 240 } 241 } 242}