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