SendersFormattingTests.java revision 9c0a2b79d135f5affdd67f5cf052c5a66e9c8544
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.text.SpannableString;
22
23import com.android.mail.providers.ConversationInfo;
24import com.android.mail.providers.MessageInfo;
25
26public class SendersFormattingTests extends AndroidTestCase {
27
28    private static ConversationInfo createConversationInfo(int count) {
29        int draftCount = 5;
30        String first = "snippet", firstUnread = first, last = first;
31        return new ConversationInfo(count, draftCount, first, firstUnread, last);
32    }
33
34    public void testMe() {
35        // Blank sender == from "me"
36        ConversationInfo conv = createConversationInfo(1);
37        boolean read = false, starred = false;
38        MessageInfo info = new MessageInfo(read, starred, null);
39        conv.addMessage(info);
40        SpannableString[] strings = SendersView.format(getContext(), conv);
41        assertEquals(strings.length, 1);
42        assertEquals(strings[0].toString(), "me");
43
44        ConversationInfo conv2 = createConversationInfo(1);
45        MessageInfo info2 = new MessageInfo(read, starred, "");
46        conv2.addMessage(info2);
47        strings = SendersView.format(getContext(), conv);
48        assertEquals(strings.length, 1);
49        assertEquals(strings[0].toString(), "me");
50
51        ConversationInfo conv3 = createConversationInfo(2);
52        MessageInfo info3 = new MessageInfo(read, starred, "");
53        conv3.addMessage(info3);
54        MessageInfo info4 = new MessageInfo(read, starred, "");
55        conv3.addMessage(info4);
56        strings = SendersView.format(getContext(), conv);
57        assertEquals(strings.length, 1);
58        assertEquals(strings[0].toString(), "me");
59    }
60
61    public void testDupes() {
62        // Duplicate sender; should only return 1
63        ConversationInfo conv = createConversationInfo(2);
64        boolean read = false, starred = false;
65        String sender = "sender@sender.com";
66        MessageInfo info = new MessageInfo(read, starred, sender);
67        conv.addMessage(info);
68        MessageInfo info2 = new MessageInfo(read, starred, sender);
69        conv.addMessage(info2);
70        SpannableString[] strings = SendersView.format(getContext(), conv);
71        assertEquals(strings.length, 1);
72        assertEquals(strings[0].toString(), sender);
73
74        // Test that the 2nd instance of the duped sender shows and not the first.
75        ConversationInfo conv2 = createConversationInfo(3);
76        String sender2 = "sender2@sender.com";
77        conv2.addMessage(info);
78        MessageInfo info3 = new MessageInfo(read, starred, sender2);
79        conv2.addMessage(info3);
80        conv2.addMessage(info2);
81        strings = SendersView.format(getContext(), conv2);
82        assertEquals(strings.length, 2);
83        assertEquals(strings[0].toString(), sender2);
84        assertEquals(strings[1].toString(), sender);
85    }
86}
87