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