2009-03-04 03:32:22 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2008 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package com.android.email.mail.store;
|
|
|
|
|
2009-05-20 17:36:16 +00:00
|
|
|
import com.android.email.mail.Flag;
|
2009-04-15 19:58:19 +00:00
|
|
|
import com.android.email.mail.Folder;
|
2009-03-04 03:32:22 +00:00
|
|
|
import com.android.email.mail.MessagingException;
|
|
|
|
import com.android.email.mail.Transport;
|
2009-09-25 21:54:32 +00:00
|
|
|
import com.android.email.mail.Folder.FolderType;
|
2009-03-04 03:32:22 +00:00
|
|
|
import com.android.email.mail.Folder.OpenMode;
|
|
|
|
import com.android.email.mail.internet.BinaryTempFileBody;
|
|
|
|
import com.android.email.mail.transport.MockTransport;
|
|
|
|
|
|
|
|
import android.test.AndroidTestCase;
|
|
|
|
import android.test.suitebuilder.annotation.SmallTest;
|
2009-04-15 19:58:19 +00:00
|
|
|
|
|
|
|
import java.util.Date;
|
|
|
|
import java.util.Locale;
|
2009-03-04 03:32:22 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This is a series of unit tests for the ImapStore class. These tests must be locally
|
|
|
|
* complete - no server(s) required.
|
|
|
|
*/
|
|
|
|
@SmallTest
|
|
|
|
public class ImapStoreUnitTests extends AndroidTestCase {
|
|
|
|
|
|
|
|
/* These values are provided by setUp() */
|
|
|
|
private ImapStore mStore = null;
|
|
|
|
private ImapStore.ImapFolder mFolder = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup code. We generate a lightweight ImapStore and ImapStore.ImapFolder.
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
protected void setUp() throws Exception {
|
|
|
|
super.setUp();
|
|
|
|
|
|
|
|
// These are needed so we can get at the inner classes
|
2009-03-27 00:05:25 +00:00
|
|
|
mStore = (ImapStore) ImapStore.newInstance("imap://user:password@server:999",
|
2009-04-14 03:07:56 +00:00
|
|
|
getContext(), null);
|
2009-03-04 03:32:22 +00:00
|
|
|
mFolder = (ImapStore.ImapFolder) mStore.getFolder("INBOX");
|
|
|
|
|
|
|
|
// This is needed for parsing mime messages
|
|
|
|
BinaryTempFileBody.setTempDirectory(this.getContext().getCacheDir());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Confirms simple non-SSL non-TLS login
|
|
|
|
*/
|
|
|
|
public void testSimpleLogin() throws MessagingException {
|
|
|
|
|
|
|
|
MockTransport mockTransport = openAndInjectMockTransport();
|
|
|
|
|
|
|
|
// try to open it
|
|
|
|
setupOpenFolder(mockTransport);
|
AI 146134: Add persistence API for remote stores & folders to use while
syncing. This provides a key-value store, per folder, that
can be used by network Stores to record persistent data such
as sync status, server keys, etc.
Note that, by definition, this only applies to remote folders
(e.g. IMAP, POP3). You'll see everywhere that LocalFolder is
passed null, and this is correct - LocalFolder *is* persistent
storage and does not need external help.
Note to reviewers: The core changes are Folder.java,
LocalStore.java, and LocalStoreUnitTests.java, so please give
them the bulk of your reviewer attention. The other files
are just following along with minor API changes. Of those,
the one worth close examination is MessagingController.java,
which is the only place in the system where remote Folders
are bonded with Local Folders and thus where this new API
comes into play.
Note to jham: Can you please take a look at
LocalStore.LocalFolder.setPersistentString() and recommend
better SQL foo than my primitive test-then-update-or-insert
logic, which is not transactional or threadsafe.
BUG=1786939
Automated import of CL 146134
2009-04-14 17:15:07 +00:00
|
|
|
mFolder.open(OpenMode.READ_WRITE, null);
|
2009-03-04 03:32:22 +00:00
|
|
|
|
|
|
|
// TODO: inject specific facts in the initial folder SELECT and check them here
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Confirms that ImapList object correctly returns an appropriate Date object
|
|
|
|
* without throwning MessagingException when getKeyedDate() is called.
|
|
|
|
*
|
|
|
|
* Here, we try a same test twice using two locales, Locale.US and the other.
|
|
|
|
* ImapList uses Locale class internally, and as a result, there's a
|
|
|
|
* possibility in which it may throw a MessageException when Locale is
|
|
|
|
* not Locale.US. Locale.JAPAN is a typical locale which emits different
|
|
|
|
* date formats, which had caused a bug before.
|
|
|
|
* @throws MessagingException
|
|
|
|
*/
|
|
|
|
public void testImapListWithUsLocale() throws MessagingException {
|
|
|
|
Locale savedLocale = Locale.getDefault();
|
|
|
|
Locale.setDefault(Locale.US);
|
|
|
|
doTestImapList();
|
|
|
|
Locale.setDefault(Locale.JAPAN);
|
|
|
|
doTestImapList();
|
|
|
|
Locale.setDefault(savedLocale);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void doTestImapList() throws MessagingException {
|
|
|
|
ImapResponseParser parser = new ImapResponseParser(null);
|
|
|
|
ImapResponseParser.ImapList list = parser.new ImapList();
|
|
|
|
String key = "key";
|
|
|
|
String date = "01-Jan-2009 01:00:00 -0800";
|
|
|
|
list.add(key);
|
|
|
|
list.add(date);
|
|
|
|
Date result = list.getKeyedDate(key);
|
|
|
|
// "01-Jan-2009 09:00:00 +0000" => 1230800400000L
|
|
|
|
assertEquals(1230800400000L, result.getTime());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* TODO: Test with SSL negotiation (faked)
|
|
|
|
* TODO: Test with SSL required but not supported
|
|
|
|
* TODO: Test with TLS negotiation (faked)
|
|
|
|
* TODO: Test with TLS required but not supported
|
|
|
|
* TODO: Test calling getMessageCount(), getMessages(), etc.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* TODO: Test the operation of checkSettings()
|
|
|
|
* TODO: Test small Store & Folder functions that manage folders & namespace
|
|
|
|
*/
|
2009-05-20 17:36:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test small Folder functions that don't really do anything in Imap
|
|
|
|
* TODO: Test all of the small Folder functions.
|
|
|
|
*/
|
|
|
|
public void testSmallFolderFunctions() throws MessagingException {
|
2009-08-27 06:12:02 +00:00
|
|
|
// getPermanentFlags() returns { Flag.DELETED, Flag.SEEN, Flag.FLAGGED }
|
2009-05-20 17:36:16 +00:00
|
|
|
Flag[] flags = mFolder.getPermanentFlags();
|
2009-08-27 06:12:02 +00:00
|
|
|
assertEquals(3, flags.length);
|
2009-05-20 17:36:16 +00:00
|
|
|
// TODO: Write flags into hashset and compare them to a hashset and compare them
|
|
|
|
assertEquals(Flag.DELETED, flags[0]);
|
|
|
|
assertEquals(Flag.SEEN, flags[1]);
|
2009-08-27 06:12:02 +00:00
|
|
|
assertEquals(Flag.FLAGGED, flags[2]);
|
2009-09-25 21:54:32 +00:00
|
|
|
|
|
|
|
// canCreate() returns true
|
|
|
|
assertTrue(mFolder.canCreate(FolderType.HOLDS_FOLDERS));
|
|
|
|
assertTrue(mFolder.canCreate(FolderType.HOLDS_MESSAGES));
|
2009-05-20 17:36:16 +00:00
|
|
|
}
|
|
|
|
|
2009-04-15 19:58:19 +00:00
|
|
|
/**
|
|
|
|
* Lightweight test to confirm that IMAP hasn't implemented any folder roles yet.
|
|
|
|
*
|
|
|
|
* TODO: Test this with multiple folders provided by mock server
|
2009-05-01 23:36:34 +00:00
|
|
|
* TODO: Implement XLIST and then support this
|
2009-04-15 19:58:19 +00:00
|
|
|
*/
|
|
|
|
public void testNoFolderRolesYet() {
|
|
|
|
assertEquals(Folder.FolderRole.UNKNOWN, mFolder.getRole());
|
|
|
|
}
|
|
|
|
|
2009-05-01 23:36:34 +00:00
|
|
|
/**
|
|
|
|
* Lightweight test to confirm that IMAP isn't requesting structure prefetch.
|
|
|
|
*/
|
|
|
|
public void testNoStructurePrefetch() {
|
|
|
|
assertFalse(mStore.requireStructurePrefetch());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Lightweight test to confirm that IMAP is requesting sent-message-upload.
|
|
|
|
* TODO: Implement Gmail-specific cases and handle this server-side
|
|
|
|
*/
|
|
|
|
public void testSentUploadRequested() {
|
|
|
|
assertTrue(mStore.requireCopyMessageToSentFolder());
|
|
|
|
}
|
|
|
|
|
2009-03-04 03:32:22 +00:00
|
|
|
/**
|
|
|
|
* TODO: Test the process of opening and indexing a mailbox with one unread message in it.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* TODO: Test the scenario where the transport is "open" but not really (e.g. server closed).
|
|
|
|
/**
|
|
|
|
* Set up a basic MockTransport. open it, and inject it into mStore
|
|
|
|
*/
|
|
|
|
private MockTransport openAndInjectMockTransport() {
|
|
|
|
// Create mock transport and inject it into the ImapStore that's already set up
|
|
|
|
MockTransport mockTransport = new MockTransport();
|
2009-09-29 22:28:43 +00:00
|
|
|
mockTransport.setSecurity(Transport.CONNECTION_SECURITY_NONE, false);
|
2009-03-04 03:32:22 +00:00
|
|
|
mStore.setTransport(mockTransport);
|
|
|
|
return mockTransport;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper which stuffs the mock with enough strings to satisfy a call to ImapFolder.open()
|
|
|
|
*
|
|
|
|
* @param mockTransport the mock transport we're using
|
|
|
|
*/
|
|
|
|
private void setupOpenFolder(MockTransport mockTransport) {
|
|
|
|
mockTransport.expect(null, "* OK Imap 2000 Ready To Assist You");
|
|
|
|
mockTransport.expect("1 LOGIN user \"password\"",
|
|
|
|
"1 OK user authenticated (Success)");
|
|
|
|
mockTransport.expect("2 SELECT \"INBOX\"", new String[] {
|
|
|
|
"* FLAGS (\\Answered \\Flagged \\Draft \\Deleted \\Seen)",
|
|
|
|
"* OK [PERMANENTFLAGS (\\Answered \\Flagged \\Draft \\Deleted \\Seen \\*)]",
|
|
|
|
"* 0 EXISTS",
|
|
|
|
"* 0 RECENT",
|
|
|
|
"* OK [UNSEEN 0]",
|
|
|
|
"* OK [UIDNEXT 1]",
|
|
|
|
"2 OK [READ-WRITE] INBOX selected. (Success)"});
|
|
|
|
}
|
2009-05-19 21:54:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test for getUnreadMessageCount with quoted string in the middle of response.
|
|
|
|
*/
|
|
|
|
public void testGetUnreadMessageCountWithQuotedString() throws Exception {
|
|
|
|
MockTransport mock = openAndInjectMockTransport();
|
|
|
|
setupOpenFolder(mock);
|
|
|
|
mock.expect("3 STATUS \"INBOX\" \\(UNSEEN\\)", new String[] {
|
|
|
|
"* STATUS \"INBOX\" (UNSEEN 2)",
|
|
|
|
"3 OK STATUS completed"});
|
|
|
|
mFolder.open(OpenMode.READ_WRITE, null);
|
|
|
|
int unreadCount = mFolder.getUnreadMessageCount();
|
|
|
|
assertEquals("getUnreadMessageCount with quoted string", 2, unreadCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test for getUnreadMessageCount with literal string in the middle of response.
|
|
|
|
*/
|
|
|
|
public void testGetUnreadMessageCountWithLiteralString() throws Exception {
|
|
|
|
MockTransport mock = openAndInjectMockTransport();
|
|
|
|
setupOpenFolder(mock);
|
|
|
|
mock.expect("3 STATUS \"INBOX\" \\(UNSEEN\\)", new String[] {
|
|
|
|
"* STATUS {5}",
|
|
|
|
"INBOX (UNSEEN 10)",
|
|
|
|
"3 OK STATUS completed"});
|
|
|
|
mFolder.open(OpenMode.READ_WRITE, null);
|
|
|
|
int unreadCount = mFolder.getUnreadMessageCount();
|
|
|
|
assertEquals("getUnreadMessageCount with literal string", 10, unreadCount);
|
|
|
|
}
|
2009-03-04 03:32:22 +00:00
|
|
|
}
|