1/*
2 * Copyright (C) 2011 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
17package com.android.email;
18
19import com.android.emailcommon.provider.EmailContent.MailboxColumns;
20import com.android.emailcommon.provider.Mailbox;
21
22import android.content.Context;
23import android.database.Cursor;
24import android.database.MatrixCursor;
25import android.graphics.drawable.Drawable;
26import android.test.AndroidTestCase;
27
28import java.util.HashSet;
29import java.util.Set;
30
31public class FolderPropertiesTests extends AndroidTestCase {
32
33    private static Cursor buildCursor(String[] columns, Object... values) {
34        MatrixCursor c = new MatrixCursor(columns, 1);
35        c.addRow(values);
36        c.moveToFirst();
37        return c;
38    }
39
40    /**
41     * Tests of the syncronization of array and types of the display folder names
42     */
43    public void testGetDisplayName() {
44        Context context = getContext();
45        FolderProperties fp = FolderProperties.getInstance(context);
46
47        assertEquals(
48                    context.getString(R.string.mailbox_name_display_inbox),
49                        fp.getDisplayName(Mailbox.TYPE_INBOX, 0, "ignored"));
50
51        assertEquals(
52                "*name*",
53                fp.getDisplayName(Mailbox.TYPE_MAIL, 0, "*name*"));
54
55        assertEquals(
56                "*name2*",
57                fp.getDisplayName(Mailbox.TYPE_PARENT, 0, "*name2*"));
58
59        assertEquals(
60                context.getString(R.string.mailbox_name_display_drafts),
61                fp.getDisplayName(Mailbox.TYPE_DRAFTS, 0, "ignored"));
62
63        assertEquals(
64                context.getString(R.string.mailbox_name_display_outbox),
65                fp.getDisplayName(Mailbox.TYPE_OUTBOX, 0, "ignored"));
66
67        assertEquals(
68                context.getString(R.string.mailbox_name_display_sent),
69                fp.getDisplayName(Mailbox.TYPE_SENT, 0, "ignored"));
70
71        assertEquals(
72                context.getString(R.string.mailbox_name_display_trash),
73                fp.getDisplayName(Mailbox.TYPE_TRASH, 0, "ignored"));
74
75        assertEquals(
76                context.getString(R.string.mailbox_name_display_junk),
77                fp.getDisplayName(Mailbox.TYPE_JUNK, 0, "ignored"));
78
79        // Testing illegal index
80        assertEquals(
81                "some name",
82                fp.getDisplayName(8, 12345678890L, "some name"));
83
84
85        // Combined mailboxes
86        assertEquals(
87                context.getString(R.string.account_folder_list_summary_inbox),
88                fp.getDisplayName(0, Mailbox.QUERY_ALL_INBOXES, "ignored"));
89        assertEquals(
90                context.getString(R.string.account_folder_list_summary_starred),
91                fp.getDisplayName(0, Mailbox.QUERY_ALL_FAVORITES, "ignored"));
92        assertEquals(
93                context.getString(R.string.account_folder_list_summary_drafts),
94                fp.getDisplayName(0, Mailbox.QUERY_ALL_DRAFTS, "ignored"));
95        assertEquals(
96                context.getString(R.string.account_folder_list_summary_outbox),
97                fp.getDisplayName(0, Mailbox.QUERY_ALL_OUTBOX, "ignored"));
98    }
99
100    public void testGetDisplayNameWithCursor() {
101        Context context = getContext();
102        FolderProperties fp = FolderProperties.getInstance(context);
103        String[] columns = new String[] {MailboxColumns.ID, MailboxColumns.TYPE,
104                MailboxColumns.DISPLAY_NAME};
105
106        assertEquals(
107                context.getString(R.string.mailbox_name_display_inbox),
108                fp.getDisplayName(buildCursor(columns, 1, Mailbox.TYPE_INBOX, "ignored"))
109                );
110
111        assertEquals(
112                "name",
113                fp.getDisplayName(buildCursor(columns, 1, Mailbox.TYPE_MAIL, "name"))
114                );
115    }
116
117    /**
118     * Confirm that all of the special icons are available and unique
119     */
120    public void testSpecialIcons() {
121        FolderProperties fp = FolderProperties.getInstance(mContext);
122
123        // Make sure they're available
124        Drawable inbox = fp.getIcon(Mailbox.TYPE_INBOX, -1, 0);
125        Drawable mail = fp.getIcon(Mailbox.TYPE_MAIL, -1, 0);
126        Drawable parent = fp.getIcon(Mailbox.TYPE_PARENT, -1, 0);
127        Drawable drafts = fp.getIcon(Mailbox.TYPE_DRAFTS, -1, 0);
128        Drawable outbox = fp.getIcon(Mailbox.TYPE_OUTBOX, -1, 0);
129        Drawable sent = fp.getIcon(Mailbox.TYPE_SENT, -1, 0);
130        Drawable trash = fp.getIcon(Mailbox.TYPE_TRASH, -1, 0);
131        Drawable junk = fp.getIcon(Mailbox.TYPE_JUNK, -1, 0);
132
133        // Make sure they're unique
134        Set<Drawable> set = new HashSet<Drawable>();
135        set.add(inbox);
136        set.add(parent);
137        set.add(drafts);
138        set.add(outbox);
139        set.add(sent);
140        set.add(trash);
141        set.add(junk);
142        assertEquals(7, set.size());
143
144        assertNull(mail);
145    }
146
147    public void testGetMessageCountWithCursor() {
148        Context context = getContext();
149        FolderProperties fp = FolderProperties.getInstance(context);
150        String[] columns = new String[] {MailboxColumns.TYPE, MailboxColumns.UNREAD_COUNT,
151                MailboxColumns.MESSAGE_COUNT};
152
153        assertEquals(
154                1,
155                fp.getMessageCount(buildCursor(columns, Mailbox.TYPE_INBOX, 1, 2))
156                );
157        assertEquals(
158                1,
159                fp.getMessageCount(buildCursor(columns, Mailbox.TYPE_MAIL, 1, 2))
160                );
161
162        assertEquals(
163                2,
164                fp.getMessageCount(buildCursor(columns, Mailbox.TYPE_DRAFTS, 1, 2))
165                );
166        assertEquals(
167                2,
168                fp.getMessageCount(buildCursor(columns, Mailbox.TYPE_OUTBOX, 1, 2))
169                );
170
171        assertEquals(
172                0,
173                fp.getMessageCount(buildCursor(columns, Mailbox.TYPE_TRASH, 1, 2))
174                );
175
176        assertEquals(
177                0,
178                fp.getMessageCount(buildCursor(columns, Mailbox.TYPE_SENT, 1, 2))
179                );
180    }
181}
182