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