StoreTests.java revision 12b82d9374947c9268217f45befe8a74bd9b60d7
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.HostAuth;
24import com.android.emailcommon.provider.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        testAuth.mProtocol = "protocol";
48        testAuth.mPort = 80;
49
50        // No address defined; throws an exception
51        try {
52            testKey = Store.getStoreKey(mContext, testAccount);
53            fail("MesasginException not thrown for missing address");
54        } catch (MessagingException expected) {
55        }
56
57        // Empty address defined; throws an exception
58        testAuth.mAddress = " \t ";
59        try {
60            testKey = Store.getStoreKey(mContext, testAccount);
61            fail("MesasginException not thrown for empty address");
62        } catch (MessagingException expected) {
63        }
64
65        // Address defined, no login
66        testAuth.mAddress = "a.valid.address.com";
67        testKey = Store.getStoreKey(mContext, testAccount);
68        assertEquals("protocol://a.valid.address.com:80", testKey);
69
70        // Address & login defined
71        testAuth.mAddress = "address.org";
72        testAuth.mFlags = HostAuth.FLAG_AUTHENTICATE;
73        testAuth.mLogin = "auser";
74        testAuth.mPassword = "password";
75        testKey = Store.getStoreKey(mContext, testAccount);
76        assertEquals("protocol://auser:password@address.org:80", testKey);
77    }
78
79    public void testGetStoreInfo() {
80        StoreInfo testInfo;
81
82        // POP3
83        testInfo = Store.StoreInfo.getStoreInfo("pop3", mContext);
84        assertNotNull(testInfo);
85        assertNotNull(testInfo.mScheme);
86        assertNotNull(testInfo.mClassName);
87        assertFalse(testInfo.mPushSupported);
88        assertEquals(Email.VISIBLE_LIMIT_DEFAULT, testInfo.mVisibleLimitDefault);
89        assertEquals(Email.VISIBLE_LIMIT_INCREMENT, testInfo.mVisibleLimitIncrement);
90
91        // IMAP
92        testInfo = Store.StoreInfo.getStoreInfo("imap", mContext);
93        assertNotNull(testInfo);
94        assertNotNull(testInfo.mScheme);
95        assertNotNull(testInfo.mClassName);
96        assertFalse(testInfo.mPushSupported);
97        assertEquals(Email.VISIBLE_LIMIT_DEFAULT, testInfo.mVisibleLimitDefault);
98        assertEquals(Email.VISIBLE_LIMIT_INCREMENT, testInfo.mVisibleLimitIncrement);
99
100        // Unknown
101        testInfo = Store.StoreInfo.getStoreInfo("unknownscheme", mContext);
102        assertNull(testInfo);
103    }
104
105    public void testGetInstance() throws MessagingException {
106        HostAuth testAuth = new HostAuth();
107        Account testAccount = new Account();
108        Store testStore;
109
110        // Make sure to set the host auth; otherwise we create entries in the hostauth db
111        testAccount.mHostAuthRecv = testAuth;
112
113        // POP3
114        testAuth.mAddress = "pop3.google.com";
115        testAuth.mProtocol = "pop3";
116        testStore = Store.getInstance(testAccount, getContext(), null);
117        assertEquals(1, Store.sStores.size());
118        assertSame(testStore, Store.sStores.get(testAuth.getStoreUri()));
119        Store.sStores.clear();
120
121        // IMAP
122        testAuth.mAddress = "imap.google.com";
123        testAuth.mProtocol = "imap";
124        testStore = Store.getInstance(testAccount, getContext(), null);
125        assertEquals(1, Store.sStores.size());
126        assertSame(testStore, Store.sStores.get(testAuth.getStoreUri()));
127        Store.sStores.clear();
128
129        // IMAP; host auth changes
130        testAuth.mAddress = "imap.google.com";
131        testAuth.mProtocol = "imap";
132        testStore = Store.getInstance(testAccount, getContext(), null); // cache first store
133        assertEquals(1, Store.sStores.size());
134        assertSame(testStore, Store.sStores.get(testAuth.getStoreUri()));
135        testAuth.mAddress = "mail.google.com";   // change the auth hostname
136        Store.getInstance(testAccount, getContext(), null); // cache second store
137        assertEquals(2, Store.sStores.size());   // Now there are two entries ...
138        assertNotSame(testStore, Store.sStores.get(testAuth.getStoreUri()));
139        Store.sStores.clear();
140
141        // Unknown
142        testAuth.mAddress = "unknown.google.com";
143        testAuth.mProtocol = "unknown";
144        try {
145            testStore = Store.getInstance(testAccount, getContext(), null);
146            fail("Store#getInstance() should have thrown an exception");
147        } catch (MessagingException expected) {
148        }
149        assertEquals(0, Store.sStores.size());
150    }
151
152    public void testUpdateMailbox() {
153        Mailbox testMailbox = new Mailbox();
154
155        Store.updateMailbox(testMailbox, 1L, "inbox", '/', true, Mailbox.TYPE_MAIL);
156        assertEquals(1L, testMailbox.mAccountKey);
157        assertEquals("inbox", testMailbox.mDisplayName);
158        assertEquals("inbox", testMailbox.mServerId);
159        assertEquals('/', testMailbox.mDelimiter);
160
161        Store.updateMailbox(testMailbox, 2L, "inbox/a", '/', true, Mailbox.TYPE_MAIL);
162        assertEquals(2L, testMailbox.mAccountKey);
163        assertEquals("a", testMailbox.mDisplayName);
164        assertEquals("inbox/a", testMailbox.mServerId);
165        assertEquals('/', testMailbox.mDelimiter);
166
167        Store.updateMailbox(testMailbox, 3L, "inbox/a/b/c/d", '/', true, Mailbox.TYPE_MAIL);
168        assertEquals(3L, testMailbox.mAccountKey);
169        assertEquals("d", testMailbox.mDisplayName);
170        assertEquals("inbox/a/b/c/d", testMailbox.mServerId);
171        assertEquals('/', testMailbox.mDelimiter);
172
173        Store.updateMailbox(testMailbox, 4L, "inbox/a/b/c", '\0', true, Mailbox.TYPE_MAIL);
174        assertEquals(4L, testMailbox.mAccountKey);
175        assertEquals("inbox/a/b/c", testMailbox.mDisplayName);
176        assertEquals("inbox/a/b/c", testMailbox.mServerId);
177        assertEquals('\0', testMailbox.mDelimiter);
178    }
179}
180