ImapStoreUnitTests.java revision 423206653fc1841153f6c6c00599a65d5c5f2191
1/*
2 * Copyright (C) 2008 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.store;
18
19import com.android.email.mail.Flag;
20import com.android.email.mail.Folder;
21import com.android.email.mail.MessagingException;
22import com.android.email.mail.Transport;
23import com.android.email.mail.Folder.OpenMode;
24import com.android.email.mail.internet.BinaryTempFileBody;
25import com.android.email.mail.transport.MockTransport;
26
27import android.test.AndroidTestCase;
28import android.test.suitebuilder.annotation.SmallTest;
29
30import java.util.Date;
31import java.util.Locale;
32
33/**
34 * This is a series of unit tests for the ImapStore class.  These tests must be locally
35 * complete - no server(s) required.
36 */
37@SmallTest
38public class ImapStoreUnitTests extends AndroidTestCase {
39
40    /* These values are provided by setUp() */
41    private ImapStore mStore = null;
42    private ImapStore.ImapFolder mFolder = null;
43
44    /**
45     * Setup code.  We generate a lightweight ImapStore and ImapStore.ImapFolder.
46     */
47    @Override
48    protected void setUp() throws Exception {
49        super.setUp();
50
51        // These are needed so we can get at the inner classes
52        mStore = (ImapStore) ImapStore.newInstance("imap://user:password@server:999",
53                getContext(), null);
54        mFolder = (ImapStore.ImapFolder) mStore.getFolder("INBOX");
55
56        // This is needed for parsing mime messages
57        BinaryTempFileBody.setTempDirectory(this.getContext().getCacheDir());
58    }
59
60    /**
61     * Confirms simple non-SSL non-TLS login
62     */
63    public void testSimpleLogin() throws MessagingException {
64
65        MockTransport mockTransport = openAndInjectMockTransport();
66
67        // try to open it
68        setupOpenFolder(mockTransport);
69        mFolder.open(OpenMode.READ_WRITE, null);
70
71        // TODO: inject specific facts in the initial folder SELECT and check them here
72    }
73
74    /**
75     * Confirms that ImapList object correctly returns an appropriate Date object
76     * without throwning MessagingException when getKeyedDate() is called.
77     *
78     * Here, we try a same test twice using two locales, Locale.US and the other.
79     * ImapList uses Locale class internally, and as a result, there's a
80     * possibility in which it may throw a MessageException when Locale is
81     * not Locale.US. Locale.JAPAN is a typical locale which emits different
82     * date formats, which had caused a bug before.
83     * @throws MessagingException
84     */
85    public void testImapListWithUsLocale() throws MessagingException {
86        Locale savedLocale = Locale.getDefault();
87        Locale.setDefault(Locale.US);
88        doTestImapList();
89        Locale.setDefault(Locale.JAPAN);
90        doTestImapList();
91        Locale.setDefault(savedLocale);
92    }
93
94    private void doTestImapList() throws MessagingException {
95        ImapResponseParser parser = new ImapResponseParser(null);
96        ImapResponseParser.ImapList list = parser.new ImapList();
97        String key = "key";
98        String date = "01-Jan-2009 01:00:00 -0800";
99        list.add(key);
100        list.add(date);
101        Date result = list.getKeyedDate(key);
102        // "01-Jan-2009 09:00:00 +0000" => 1230800400000L
103        assertEquals(1230800400000L, result.getTime());
104    }
105
106    /**
107     * TODO: Test with SSL negotiation (faked)
108     * TODO: Test with SSL required but not supported
109     * TODO: Test with TLS negotiation (faked)
110     * TODO: Test with TLS required but not supported
111     * TODO: Test calling getMessageCount(), getMessages(), etc.
112     */
113
114    /**
115     * TODO: Test the operation of checkSettings()
116     * TODO: Test small Store & Folder functions that manage folders & namespace
117     */
118
119    /**
120     * Test small Folder functions that don't really do anything in Imap
121     * TODO: Test all of the small Folder functions.
122     */
123    public void testSmallFolderFunctions() throws MessagingException {
124        // getPermanentFlags() returns { Flag.DELETED, Flag.SEEN, Flag.FLAGGED }
125        Flag[] flags = mFolder.getPermanentFlags();
126        assertEquals(3, flags.length);
127        // TODO: Write flags into hashset and compare them to a hashset and compare them
128        assertEquals(Flag.DELETED, flags[0]);
129        assertEquals(Flag.SEEN, flags[1]);
130        assertEquals(Flag.FLAGGED, flags[2]);
131    }
132
133    /**
134     * Lightweight test to confirm that IMAP hasn't implemented any folder roles yet.
135     *
136     * TODO: Test this with multiple folders provided by mock server
137     * TODO: Implement XLIST and then support this
138     */
139    public void testNoFolderRolesYet() {
140        assertEquals(Folder.FolderRole.UNKNOWN, mFolder.getRole());
141    }
142
143    /**
144     * Lightweight test to confirm that IMAP isn't requesting structure prefetch.
145     */
146    public void testNoStructurePrefetch() {
147        assertFalse(mStore.requireStructurePrefetch());
148    }
149
150    /**
151     * Lightweight test to confirm that IMAP is requesting sent-message-upload.
152     * TODO: Implement Gmail-specific cases and handle this server-side
153     */
154    public void testSentUploadRequested() {
155        assertTrue(mStore.requireCopyMessageToSentFolder());
156    }
157
158    /**
159     * TODO: Test the process of opening and indexing a mailbox with one unread message in it.
160     */
161
162    /**
163     * TODO: Test the scenario where the transport is "open" but not really (e.g. server closed).
164    /**
165     * Set up a basic MockTransport. open it, and inject it into mStore
166     */
167    private MockTransport openAndInjectMockTransport() {
168        // Create mock transport and inject it into the ImapStore that's already set up
169        MockTransport mockTransport = new MockTransport();
170        mockTransport.setSecurity(Transport.CONNECTION_SECURITY_NONE);
171        mStore.setTransport(mockTransport);
172        return mockTransport;
173    }
174
175    /**
176     * Helper which stuffs the mock with enough strings to satisfy a call to ImapFolder.open()
177     *
178     * @param mockTransport the mock transport we're using
179     */
180    private void setupOpenFolder(MockTransport mockTransport) {
181        mockTransport.expect(null, "* OK Imap 2000 Ready To Assist You");
182        mockTransport.expect("1 LOGIN user \"password\"",
183                "1 OK user authenticated (Success)");
184        mockTransport.expect("2 SELECT \"INBOX\"", new String[] {
185                "* FLAGS (\\Answered \\Flagged \\Draft \\Deleted \\Seen)",
186                "* OK [PERMANENTFLAGS (\\Answered \\Flagged \\Draft \\Deleted \\Seen \\*)]",
187                "* 0 EXISTS",
188                "* 0 RECENT",
189                "* OK [UNSEEN 0]",
190                "* OK [UIDNEXT 1]",
191                "2 OK [READ-WRITE] INBOX selected. (Success)"});
192    }
193
194    /**
195     * Test for getUnreadMessageCount with quoted string in the middle of response.
196     */
197    public void testGetUnreadMessageCountWithQuotedString() throws Exception {
198        MockTransport mock = openAndInjectMockTransport();
199        setupOpenFolder(mock);
200        mock.expect("3 STATUS \"INBOX\" \\(UNSEEN\\)", new String[] {
201                "* STATUS \"INBOX\" (UNSEEN 2)",
202                "3 OK STATUS completed"});
203        mFolder.open(OpenMode.READ_WRITE, null);
204        int unreadCount = mFolder.getUnreadMessageCount();
205        assertEquals("getUnreadMessageCount with quoted string", 2, unreadCount);
206    }
207
208    /**
209     * Test for getUnreadMessageCount with literal string in the middle of response.
210     */
211    public void testGetUnreadMessageCountWithLiteralString() throws Exception {
212        MockTransport mock = openAndInjectMockTransport();
213        setupOpenFolder(mock);
214        mock.expect("3 STATUS \"INBOX\" \\(UNSEEN\\)", new String[] {
215                "* STATUS {5}",
216                "INBOX (UNSEEN 10)",
217                "3 OK STATUS completed"});
218        mFolder.open(OpenMode.READ_WRITE, null);
219        int unreadCount = mFolder.getUnreadMessageCount();
220        assertEquals("getUnreadMessageCount with literal string", 10, unreadCount);
221    }
222}
223