StoreTests.java revision f5418f1f93b02e7fab9f15eb201800b65510998e
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 android.content.Context; 20import android.test.ProviderTestCase2; 21import android.test.suitebuilder.annotation.MediumTest; 22 23import com.android.email.Email; 24import com.android.email.mail.Store.StoreInfo; 25import com.android.email.provider.EmailProvider; 26import com.android.email.provider.ProviderTestUtils; 27import com.android.emailcommon.mail.MessagingException; 28import com.android.emailcommon.provider.Account; 29import com.android.emailcommon.provider.EmailContent; 30import com.android.emailcommon.provider.HostAuth; 31import com.android.emailcommon.provider.Mailbox; 32 33/** 34 * Tests of StoreInfo & Store lookup in the Store abstract class 35 * 36 * You can run this entire test case with: 37 * runtest -c com.android.email.mail.store.StoreTests email 38 * 39 */ 40 41@MediumTest 42public class StoreTests extends ProviderTestCase2<EmailProvider> { 43 44 private Context mMockContext; 45 46 @Override 47 public void setUp() throws Exception { 48 super.setUp(); 49 mMockContext = getMockContext(); 50 Store.sStores.clear(); 51 } 52 53 public StoreTests(Class<EmailProvider> providerClass, String providerAuthority) { 54 super(EmailProvider.class, EmailContent.AUTHORITY); 55 } 56 57 public void testGetStoreInfo() { 58 StoreInfo testInfo; 59 60 // POP3 61 testInfo = Store.StoreInfo.getStoreInfo("pop3", mContext); 62 assertNotNull(testInfo); 63 assertNotNull(testInfo.mScheme); 64 assertNotNull(testInfo.mClassName); 65 assertFalse(testInfo.mPushSupported); 66 assertEquals(Email.VISIBLE_LIMIT_DEFAULT, testInfo.mVisibleLimitDefault); 67 assertEquals(Email.VISIBLE_LIMIT_INCREMENT, testInfo.mVisibleLimitIncrement); 68 69 // IMAP 70 testInfo = Store.StoreInfo.getStoreInfo("imap", mContext); 71 assertNotNull(testInfo); 72 assertNotNull(testInfo.mScheme); 73 assertNotNull(testInfo.mClassName); 74 assertFalse(testInfo.mPushSupported); 75 assertEquals(Email.VISIBLE_LIMIT_DEFAULT, testInfo.mVisibleLimitDefault); 76 assertEquals(Email.VISIBLE_LIMIT_INCREMENT, testInfo.mVisibleLimitIncrement); 77 78 // Unknown 79 testInfo = Store.StoreInfo.getStoreInfo("unknownscheme", mContext); 80 assertNull(testInfo); 81 } 82 83 public void testGetInstance() throws MessagingException { 84 Store testStore; 85 86 // POP3 87 Account testAccount = ProviderTestUtils.setupAccount("pop", false, mMockContext); 88 HostAuth testAuth = new HostAuth(); 89 testAccount.mHostAuthRecv = testAuth; 90 testAuth.mAddress = "pop3.google.com"; 91 testAuth.mProtocol = "pop3"; 92 testAccount.save(mMockContext); 93 94 testStore = Store.getInstance(testAccount, getContext(), null); 95 assertEquals(1, Store.sStores.size()); 96 assertSame(testStore, Store.sStores.get(testAccount.mId)); 97 Store.sStores.clear(); 98 99 // IMAP 100 testAccount = ProviderTestUtils.setupAccount("pop", false, mMockContext); 101 testAuth = new HostAuth(); 102 testAccount.mHostAuthRecv = testAuth; 103 testAuth.mAddress = "imap.google.com"; 104 testAuth.mProtocol = "imap"; 105 testAccount.save(mMockContext); 106 testStore = Store.getInstance(testAccount, getContext(), null); 107 assertEquals(1, Store.sStores.size()); 108 assertSame(testStore, Store.sStores.get(testAccount.mId)); 109 Store.sStores.clear(); 110 111 // Unknown 112 testAccount = ProviderTestUtils.setupAccount("unknown", false, mMockContext); 113 testAuth = new HostAuth(); 114 testAuth.mAddress = "unknown.google.com"; 115 testAuth.mProtocol = "unknown"; 116 try { 117 testStore = Store.getInstance(testAccount, getContext(), null); 118 fail("Store#getInstance() should have thrown an exception"); 119 } catch (MessagingException expected) { 120 } 121 assertEquals(0, Store.sStores.size()); 122 } 123 124 public void testUpdateMailbox() { 125 Mailbox testMailbox = new Mailbox(); 126 127 Store.updateMailbox(testMailbox, 1L, "inbox", '/', true, Mailbox.TYPE_MAIL); 128 assertEquals(1L, testMailbox.mAccountKey); 129 assertEquals("inbox", testMailbox.mDisplayName); 130 assertEquals("inbox", testMailbox.mServerId); 131 assertEquals('/', testMailbox.mDelimiter); 132 133 Store.updateMailbox(testMailbox, 2L, "inbox/a", '/', true, Mailbox.TYPE_MAIL); 134 assertEquals(2L, testMailbox.mAccountKey); 135 assertEquals("a", testMailbox.mDisplayName); 136 assertEquals("inbox/a", testMailbox.mServerId); 137 assertEquals('/', testMailbox.mDelimiter); 138 139 Store.updateMailbox(testMailbox, 3L, "inbox/a/b/c/d", '/', true, Mailbox.TYPE_MAIL); 140 assertEquals(3L, testMailbox.mAccountKey); 141 assertEquals("d", testMailbox.mDisplayName); 142 assertEquals("inbox/a/b/c/d", testMailbox.mServerId); 143 assertEquals('/', testMailbox.mDelimiter); 144 145 Store.updateMailbox(testMailbox, 4L, "inbox/a/b/c", '\0', true, Mailbox.TYPE_MAIL); 146 assertEquals(4L, testMailbox.mAccountKey); 147 assertEquals("inbox/a/b/c", testMailbox.mDisplayName); 148 assertEquals("inbox/a/b/c", testMailbox.mServerId); 149 assertEquals('\0', testMailbox.mDelimiter); 150 } 151} 152