StoreTests.java revision 22208771b7b39c5d131372ba6bc45ab23cc22232
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.mail;
18
19import com.android.email.Email;
20import com.android.email.mail.Store.StoreInfo;
21import com.android.emailcommon.mail.MessagingException;
22import com.android.emailcommon.provider.EmailContent.Account;
23import com.android.emailcommon.provider.EmailContent.HostAuth;
24import com.android.emailcommon.provider.EmailContent.Mailbox;
25
26import android.test.AndroidTestCase;
27import android.test.suitebuilder.annotation.MediumTest;
28
29/**
30 * Tests of StoreInfo & Store lookup in the Store abstract class
31 */
32@MediumTest
33public class StoreTests extends AndroidTestCase {
34
35    @Override
36    public void setUp() {
37        Store.sStores.clear();
38    }
39
40    public void testGetStoreKey() throws MessagingException {
41        HostAuth testAuth = new HostAuth();
42        Account testAccount = new Account();
43        String testKey;
44
45        // Make sure to set the host auth; otherwise we create entries in the hostauth db
46        testAccount.mHostAuthRecv = testAuth;
47
48        // No address defined; throws an exception
49        try {
50            testKey = Store.getStoreKey(mContext, testAccount);
51            fail("MesasginException not thrown for missing address");
52        } catch (MessagingException expected) {
53        }
54
55        // Empty address defined; throws an exception
56        testAuth.mAddress = " \t ";
57        try {
58            testKey = Store.getStoreKey(mContext, testAccount);
59            fail("MesasginException not thrown for empty address");
60        } catch (MessagingException expected) {
61        }
62
63        // Address defined, no login
64        testAuth.mAddress = "a.valid.address.com";
65        testKey = Store.getStoreKey(mContext, testAccount);
66        assertEquals("a.valid.address.com", testKey);
67
68        // Address & login defined
69        testAuth.mAddress = "address.org";
70        testAuth.mLogin = "auser";
71        testKey = Store.getStoreKey(mContext, testAccount);
72        assertEquals("address.orgauser", testKey);
73    }
74
75    public void testGetStoreInfo() {
76        StoreInfo testInfo;
77
78        // POP3
79        testInfo = Store.StoreInfo.getStoreInfo("pop3", mContext);
80        assertNotNull(testInfo);
81        assertNotNull(testInfo.mScheme);
82        assertNotNull(testInfo.mClassName);
83        assertFalse(testInfo.mPushSupported);
84        assertEquals(Email.VISIBLE_LIMIT_DEFAULT, testInfo.mVisibleLimitDefault);
85        assertEquals(Email.VISIBLE_LIMIT_INCREMENT, testInfo.mVisibleLimitIncrement);
86
87        // IMAP
88        testInfo = Store.StoreInfo.getStoreInfo("imap", mContext);
89        assertNotNull(testInfo);
90        assertNotNull(testInfo.mScheme);
91        assertNotNull(testInfo.mClassName);
92        assertFalse(testInfo.mPushSupported);
93        assertEquals(Email.VISIBLE_LIMIT_DEFAULT, testInfo.mVisibleLimitDefault);
94        assertEquals(Email.VISIBLE_LIMIT_INCREMENT, testInfo.mVisibleLimitIncrement);
95
96        // Unknown
97        testInfo = Store.StoreInfo.getStoreInfo("unknownscheme", mContext);
98        assertNull(testInfo);
99    }
100
101    public void testGetInstance() throws MessagingException {
102        HostAuth testAuth = new HostAuth();
103        Account testAccount = new Account();
104        Store testStore;
105
106        // Make sure to set the host auth; otherwise we create entries in the hostauth db
107        testAccount.mHostAuthRecv = testAuth;
108
109        // POP3
110        testAuth.mAddress = "pop3.google.com";
111        testAuth.mProtocol = "pop3";
112        testStore = Store.getInstance(testAccount, getContext(), null);
113        assertEquals(1, Store.sStores.size());
114        assertSame(testStore, Store.sStores.get("pop3.google.com"));
115        Store.sStores.clear();
116
117        // IMAP
118        testAuth.mAddress = "imap.google.com";
119        testAuth.mProtocol = "imap";
120        testStore = Store.getInstance(testAccount, getContext(), null);
121        assertEquals(1, Store.sStores.size());
122        assertSame(testStore, Store.sStores.get("imap.google.com"));
123        Store.sStores.clear();
124
125        // Unknown
126        testAuth.mAddress = "unknown.google.com";
127        testAuth.mProtocol = "unknown";
128        try {
129            testStore = Store.getInstance(testAccount, getContext(), null);
130            fail("Store#getInstance() should have thrown an exception");
131        } catch (MessagingException expected) {
132        }
133        assertEquals(0, Store.sStores.size());
134    }
135
136    public void testUpdateMailbox() {
137        Mailbox testMailbox = new Mailbox();
138
139        Store.updateMailbox(testMailbox, 1L, "inbox", '/', Mailbox.TYPE_MAIL);
140        assertEquals(1L, testMailbox.mAccountKey);
141        assertEquals("inbox", testMailbox.mDisplayName);
142        assertEquals("inbox", testMailbox.mServerId);
143        assertEquals('/', testMailbox.mDelimiter);
144
145        Store.updateMailbox(testMailbox, 2L, "inbox/a", '/', Mailbox.TYPE_MAIL);
146        assertEquals(2L, testMailbox.mAccountKey);
147        assertEquals("a", testMailbox.mDisplayName);
148        assertEquals("inbox/a", testMailbox.mServerId);
149        assertEquals('/', testMailbox.mDelimiter);
150
151        Store.updateMailbox(testMailbox, 3L, "inbox/a/b/c/d", '/', Mailbox.TYPE_MAIL);
152        assertEquals(3L, testMailbox.mAccountKey);
153        assertEquals("d", testMailbox.mDisplayName);
154        assertEquals("inbox/a/b/c/d", testMailbox.mServerId);
155        assertEquals('/', testMailbox.mDelimiter);
156
157        Store.updateMailbox(testMailbox, 4L, "inbox/a/b/c", '\0', Mailbox.TYPE_MAIL);
158        assertEquals(4L, testMailbox.mAccountKey);
159        assertEquals("inbox/a/b/c", testMailbox.mDisplayName);
160        assertEquals("inbox/a/b/c", testMailbox.mServerId);
161        assertEquals('\0', testMailbox.mDelimiter);
162    }
163}
164