MessagingControllerUnitTests.java revision a13aea24a396ae8f115b016b702bd2457ad04e6d
1/*
2 * Copyright (C) 2009 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.email.mail.Folder;
20import com.android.email.mail.MockFolder;
21
22import android.test.AndroidTestCase;
23import android.test.suitebuilder.annotation.SmallTest;
24
25/**
26 * This is a series of unit tests for the MessagingController class.
27 *
28 * Technically these are functional because they use the underlying preferences framework.
29 */
30@SmallTest
31public class MessagingControllerUnitTests extends AndroidTestCase {
32
33    private Preferences mPreferences;
34
35    private String mUuid;
36    private Account mAccount;
37
38    @Override
39    protected void setUp() throws Exception {
40        super.setUp();
41
42        mPreferences = Preferences.getPreferences(getContext());
43    }
44
45    /**
46     * Delete any dummy accounts we set up for this test
47     */
48    @Override
49    protected void tearDown() throws Exception {
50        super.tearDown();
51
52        if (mAccount != null && mPreferences != null) {
53            mAccount.delete(mPreferences);
54        }
55    }
56
57    /**
58     * Test the code that copies server-supplied folder names into the account data
59     */
60    public void testUpdateAccountFolderNames() {
61        MessagingController mc = MessagingController.getInstance(getContext());
62        // Create a dummy account
63        createTestAccount();
64        // Refresh it to fill in all fields (many will have default values)
65        mAccount.refresh(mPreferences);
66
67        // Replace one entry, others are not included
68        Folder[] folders1 = new Folder[] {
69                new MyMockFolder(Folder.FolderRole.DRAFTS, "DRAFTS_1"),
70        };
71        mc.updateAccountFolderNames(mAccount, folders1);
72        checkServerFolderNames("folders1", mAccount, "DRAFTS_1", "Sent", "Trash", "Outbox");
73
74        // test that the data is shared across multiple account instantiations
75        Account account2 = new Account(mPreferences, mUuid);
76        checkServerFolderNames("folders1-2", account2, "DRAFTS_1", "Sent", "Trash", "Outbox");
77
78        // Replace one entry, others are included but called out as unknown
79        Folder[] folders2 = new Folder[] {
80                new MyMockFolder(Folder.FolderRole.UNKNOWN, "DRAFTS_2"),
81                new MyMockFolder(Folder.FolderRole.SENT, "SENT_2"),
82                new MyMockFolder(Folder.FolderRole.UNKNOWN, "TRASH_2"),
83                new MyMockFolder(Folder.FolderRole.UNKNOWN, "OUTBOX_2"),
84        };
85        mc.updateAccountFolderNames(mAccount, folders2);
86        checkServerFolderNames("folders2", mAccount, "DRAFTS_1", "SENT_2", "Trash", "Outbox");
87
88        // test that the data is shared across multiple account instantiations
89        account2 = new Account(mPreferences, mUuid);
90        checkServerFolderNames("folders2-2", account2, "DRAFTS_1", "SENT_2", "Trash", "Outbox");
91
92        // Replace one entry, check that "other" is ignored, check that Outbox is ignored
93        Folder[] folders3 = new Folder[] {
94                new MyMockFolder(Folder.FolderRole.OTHER, "OTHER_3a"),
95                new MyMockFolder(Folder.FolderRole.TRASH, "TRASH_3"),
96                new MyMockFolder(Folder.FolderRole.OTHER, "OTHER_3b"),
97                new MyMockFolder(Folder.FolderRole.OUTBOX, "OUTBOX_3"),
98        };
99        mc.updateAccountFolderNames(mAccount, folders3);
100        checkServerFolderNames("folders3", mAccount, "DRAFTS_1", "SENT_2", "TRASH_3", "Outbox");
101
102        // test that the data is shared across multiple account instantiations
103        account2 = new Account(mPreferences, mUuid);
104        checkServerFolderNames("folders3-2", account2, "DRAFTS_1", "SENT_2", "TRASH_3", "Outbox");
105    }
106
107    /**
108     * Quickly check all four folder name slots in mAccount
109     */
110    private void checkServerFolderNames(String diagnostic, Account account,
111            String drafts, String sent, String trash, String outbox) {
112        assertEquals(diagnostic, drafts, account.getDraftsFolderName());
113        assertEquals(diagnostic, sent, account.getSentFolderName());
114        assertEquals(diagnostic, trash, account.getTrashFolderName());
115        assertEquals(diagnostic, outbox, account.getOutboxFolderName());
116    }
117
118    /**
119     * MockFolder allows setting and retrieving role & name
120     */
121    private static class MyMockFolder extends MockFolder {
122        private FolderRole mRole;
123        private String mName;
124
125        public MyMockFolder(FolderRole role, String name) {
126            mRole = role;
127            mName = name;
128        }
129
130        public String getName() {
131            return mName;
132        }
133
134        @Override
135        public FolderRole getRole() {
136            return mRole;
137        }
138    }
139
140    /**
141     * Create a dummy account with minimal fields
142     */
143    private void createTestAccount() {
144        mAccount = new Account(getContext());
145        mAccount.save(mPreferences);
146
147        mUuid = mAccount.getUuid();
148    }
149
150}
151