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.ConversationInfo;
25import com.android.mail.providers.ParticipantInfo;
26import com.google.common.collect.Lists;
27
28import java.util.ArrayList;
29
30@SmallTest
31public class SendersFormattingTests extends AndroidTestCase {
32
33    private static ConversationInfo createConversationInfo() {
34        return new ConversationInfo(0, 5, "snippet", "snippet", "snippet");
35    }
36
37    public void testMeFromNullName() {
38        final ConversationInfo conv = createConversationInfo();
39        conv.addParticipant(new ParticipantInfo(null, "something@somewhere.com", 0, false));
40        final ArrayList<SpannableString> strings = Lists.newArrayList();
41        assertEquals(0, strings.size());
42
43        SendersView.format(getContext(), conv, "", 100, strings, null, null, null, false, false);
44        assertEquals(1, strings.size());
45        assertEquals("me", strings.get(0).toString());
46    }
47
48    public void testMeFromEmptyName() {
49        final ConversationInfo conv = createConversationInfo();
50        conv.addParticipant(new ParticipantInfo("", "something@somewhere.com", 0, false));
51        final ArrayList<SpannableString> strings = Lists.newArrayList();
52        assertEquals(0, strings.size());
53
54        SendersView.format(getContext(), conv, "", 100, strings, null, null, null, false, false);
55        assertEquals(1, strings.size());
56        assertEquals("me", strings.get(0).toString());
57    }
58
59    public void testMeFromDuplicateEmptyNames() {
60        final ConversationInfo conv = createConversationInfo();
61        conv.addParticipant(new ParticipantInfo("", "something@somewhere.com", 0, false));
62        conv.addParticipant(new ParticipantInfo("", "something@somewhere.com", 0, false));
63        final ArrayList<SpannableString> strings = Lists.newArrayList();
64        assertEquals(0, strings.size());
65
66        SendersView.format(getContext(), conv, "", 100, strings, null, null, null, false, false);
67        assertEquals(2, strings.size());
68        assertNull(strings.get(0));
69        assertEquals("me", strings.get(1).toString());
70    }
71
72    public void testDuplicates() {
73        final ConversationInfo conv = createConversationInfo();
74        conv.addParticipant(new ParticipantInfo("Something", "something@somewhere.com", 0, false));
75        conv.addParticipant(new ParticipantInfo("Something", "something@somewhere.com", 0, false));
76
77        final ArrayList<SpannableString> strings = Lists.newArrayList();
78        assertEquals(0, strings.size());
79
80        SendersView.format(getContext(), conv, "", 100, strings, null, null, null, false, false);
81        assertEquals(2, strings.size());
82        assertNull(strings.get(0));
83        assertEquals("Something", strings.get(1).toString());
84    }
85
86    public void testSenderNameBadInput() {
87        final ConversationInfo before = createConversationInfo();
88        before.addParticipant(new ParticipantInfo("****^****", null, 0, false));
89
90        final byte[] serialized = before.toBlob();
91
92        final ConversationInfo after = ConversationInfo.fromBlob(serialized);
93        assertEquals(1, after.participantInfos.size());
94        assertEquals(before.participantInfos.get(0).name, after.participantInfos.get(0).name);
95    }
96
97    public void testConversationSnippetsBadInput() {
98        final String first = "*^*";
99        final String firstUnread = "*^*^*";
100        final String last = "*^*^*^*";
101
102        final ConversationInfo before = new ConversationInfo(42, 49, first, firstUnread, last);
103        before.addParticipant(new ParticipantInfo("Foo Bar", "foo@bar.com", 0, false));
104        assertEquals(first, before.firstSnippet);
105        assertEquals(firstUnread, before.firstUnreadSnippet);
106        assertEquals(last, before.lastSnippet);
107
108        final byte[] serialized = before.toBlob();
109
110        final ConversationInfo after = ConversationInfo.fromBlob(serialized);
111        assertEquals(before.firstSnippet, after.firstSnippet);
112        assertEquals(before.firstUnreadSnippet, after.firstUnreadSnippet);
113        assertEquals(before.lastSnippet, after.lastSnippet);
114    }
115}