1 /* 2 * Copyright (C) 2009 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * 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, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 17 package com.android.email.mail; 18 19 import android.content.Context; 20 import android.test.ProviderTestCase2; 21 import android.test.suitebuilder.annotation.MediumTest; 22 import android.test.suitebuilder.annotation.Suppress; 23 24 import com.android.email.provider.EmailProvider; 25 import com.android.email.provider.ProviderTestUtils; 26 import com.android.emailcommon.mail.MessagingException; 27 import com.android.emailcommon.provider.Account; 28 import com.android.emailcommon.provider.EmailContent; 29 import com.android.emailcommon.provider.HostAuth; 30 import com.android.emailcommon.provider.Mailbox; 31 32 /** 33 * Tests of StoreInfo & Store lookup in the Store abstract class 34 * 35 * You can run this entire test case with: 36 * runtest -c com.android.email.mail.store.StoreTests email 37 * 38 */ 39 @Suppress 40 @MediumTest 41 public class StoreTests extends ProviderTestCase2<EmailProvider> { 42 43 private Context mMockContext; 44 45 @Override 46 public void setUp() throws Exception { 47 super.setUp(); 48 mMockContext = getMockContext(); 49 Store.sStores.clear(); 50 } 51 52 public StoreTests() { 53 super(EmailProvider.class, EmailContent.AUTHORITY); 54 } 55 56 public StoreTests(Class<EmailProvider> providerClass, String providerAuthority) { 57 super(EmailProvider.class, EmailContent.AUTHORITY); 58 } 59 60 public void testGetInstance() throws MessagingException { 61 Store testStore; 62 63 // POP3 64 Account testAccount = ProviderTestUtils.setupAccount("pop", false, mMockContext); 65 HostAuth testAuth = new HostAuth(); 66 testAccount.mHostAuthRecv = testAuth; 67 testAuth.mAddress = "pop3.google.com"; 68 testAuth.mProtocol = "pop3"; 69 testAccount.save(mMockContext); 70 71 testStore = Store.getInstance(testAccount, getContext()); 72 assertEquals(1, Store.sStores.size()); 73 assertSame(testStore, Store.sStores.get(testAccount.mId)); 74 Store.sStores.clear(); 75 76 // IMAP 77 testAccount = ProviderTestUtils.setupAccount("pop", false, mMockContext); 78 testAuth = new HostAuth(); 79 testAccount.mHostAuthRecv = testAuth; 80 testAuth.mAddress = "imap.google.com"; 81 testAuth.mProtocol = "imap"; 82 testAccount.save(mMockContext); 83 testStore = Store.getInstance(testAccount, getContext()); 84 assertEquals(1, Store.sStores.size()); 85 assertSame(testStore, Store.sStores.get(testAccount.mId)); 86 Store.sStores.clear(); 87 88 // Unknown 89 testAccount = ProviderTestUtils.setupAccount("unknown", false, mMockContext); 90 testAuth = new HostAuth(); 91 testAuth.mAddress = "unknown.google.com"; 92 testAuth.mProtocol = "unknown"; 93 try { 94 testStore = Store.getInstance(testAccount, getContext()); 95 fail("Store#getInstance() should have thrown an exception"); 96 } catch (MessagingException expected) { 97 } 98 assertEquals(0, Store.sStores.size()); 99 } 100 101 public void testUpdateMailbox() { 102 Mailbox testMailbox = new Mailbox(); 103 104 Store.updateMailbox(testMailbox, 1L, "inbox", '/', true, Mailbox.TYPE_MAIL); 105 assertEquals(1L, testMailbox.mAccountKey); 106 assertEquals("inbox", testMailbox.mDisplayName); 107 assertEquals("inbox", testMailbox.mServerId); 108 assertEquals('/', testMailbox.mDelimiter); 109 110 Store.updateMailbox(testMailbox, 2L, "inbox/a", '/', true, Mailbox.TYPE_MAIL); 111 assertEquals(2L, testMailbox.mAccountKey); 112 assertEquals("a", testMailbox.mDisplayName); 113 assertEquals("inbox/a", testMailbox.mServerId); 114 assertEquals('/', testMailbox.mDelimiter); 115 116 Store.updateMailbox(testMailbox, 3L, "inbox/a/b/c/d", '/', true, Mailbox.TYPE_MAIL); 117 assertEquals(3L, testMailbox.mAccountKey); 118 assertEquals("d", testMailbox.mDisplayName); 119 assertEquals("inbox/a/b/c/d", testMailbox.mServerId); 120 assertEquals('/', testMailbox.mDelimiter); 121 122 Store.updateMailbox(testMailbox, 4L, "inbox/a/b/c", '\0', true, Mailbox.TYPE_MAIL); 123 assertEquals(4L, testMailbox.mAccountKey); 124 assertEquals("inbox/a/b/c", testMailbox.mDisplayName); 125 assertEquals("inbox/a/b/c", testMailbox.mServerId); 126 assertEquals('\0', testMailbox.mDelimiter); 127 } 128 } 129