StoreTests.java revision a50fc99b0c433f0cde31ba1c7ab87fb9ea86345d
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;
24
25import android.test.AndroidTestCase;
26import android.test.suitebuilder.annotation.MediumTest;
27
28/**
29 * Tests of StoreInfo & Store lookup in the Store abstract class
30 */
31@MediumTest
32public class StoreTests extends AndroidTestCase {
33    public void testGetStoreKey() throws MessagingException {
34        HostAuth testAuth = new HostAuth();
35        Account testAccount = new Account();
36        String testKey;
37
38        // Make sure to set the host auth; otherwise we create entries in the hostauth db
39        testAccount.mHostAuthRecv = testAuth;
40
41        // No address defined; throws an exception
42        try {
43            testKey = Store.getStoreKey(mContext, testAccount);
44            fail("MesasginException not thrown for missing address");
45        } catch (MessagingException expected) {
46        }
47
48        // Empty address defined; throws an exception
49        testAuth.mAddress = " \t ";
50        try {
51            testKey = Store.getStoreKey(mContext, testAccount);
52            fail("MesasginException not thrown for empty address");
53        } catch (MessagingException expected) {
54        }
55
56        // Address defined, no login
57        testAuth.mAddress = "a.valid.address.com";
58        testKey = Store.getStoreKey(mContext, testAccount);
59        assertEquals("a.valid.address.com", testKey);
60
61        // Address & login defined
62        testAuth.mAddress = "address.org";
63        testAuth.mLogin = "auser";
64        testKey = Store.getStoreKey(mContext, testAccount);
65        assertEquals("address.orgauser", testKey);
66    }
67
68    public void testGetStoreInfo() {
69        StoreInfo testInfo;
70
71        // POP3
72        testInfo = Store.StoreInfo.getStoreInfo("pop3", mContext);
73        assertNotNull(testInfo);
74        assertNotNull(testInfo.mScheme);
75        assertNotNull(testInfo.mClassName);
76        assertFalse(testInfo.mPushSupported);
77        assertEquals(Email.VISIBLE_LIMIT_DEFAULT, testInfo.mVisibleLimitDefault);
78        assertEquals(Email.VISIBLE_LIMIT_INCREMENT, testInfo.mVisibleLimitIncrement);
79
80        // IMAP
81        testInfo = Store.StoreInfo.getStoreInfo("imap", mContext);
82        assertNotNull(testInfo);
83        assertNotNull(testInfo.mScheme);
84        assertNotNull(testInfo.mClassName);
85        assertFalse(testInfo.mPushSupported);
86        assertEquals(Email.VISIBLE_LIMIT_DEFAULT, testInfo.mVisibleLimitDefault);
87        assertEquals(Email.VISIBLE_LIMIT_INCREMENT, testInfo.mVisibleLimitIncrement);
88
89        // Unknown
90        testInfo = Store.StoreInfo.getStoreInfo("unknownscheme", mContext);
91        assertNull(testInfo);
92    }
93
94    public void testGetInstance() throws MessagingException {
95        HostAuth testAuth = new HostAuth();
96        Account testAccount = new Account();
97        Store testStore;
98
99        // Make sure to set the host auth; otherwise we create entries in the hostauth db
100        testAccount.mHostAuthRecv = testAuth;
101
102        // POP3
103        testAuth.mAddress = "pop3.google.com";
104        testAuth.mProtocol = "pop3";
105        testStore = Store.getInstance(testAccount, getContext(), null);
106        assertEquals(1, Store.sStores.size());
107        assertSame(testStore, Store.sStores.get("pop3.google.com"));
108        Store.sStores.clear();
109
110        // IMAP
111        testAuth.mAddress = "imap.google.com";
112        testAuth.mProtocol = "imap";
113        testStore = Store.getInstance(testAccount, getContext(), null);
114        assertEquals(1, Store.sStores.size());
115        assertSame(testStore, Store.sStores.get("imap.google.com"));
116        Store.sStores.clear();
117
118        // Unknown
119        testAuth.mAddress = "unknown.google.com";
120        testAuth.mProtocol = "unknown";
121        try {
122            testStore = Store.getInstance(testAccount, getContext(), null);
123            fail("Store#getInstance() should have thrown an exception");
124        } catch (MessagingException expected) {
125        }
126        assertEquals(0, Store.sStores.size());
127    }
128
129}
130