From 4f23ebd3dcca596533a33e6faedb446a814d32e3 Mon Sep 17 00:00:00 2001 From: "Takaoka G. Tadashi" Date: Thu, 25 Jun 2009 15:53:32 +0900 Subject: [PATCH] Fixed Pop3Store to be able to delete message in Inbox. When delete policy is DELETE_POLICY_ON_DELETE, PENDING_COMMAND_TRASH will be invoked. But in POP3 account, because of 1) Inbox is OpenMode.READ_ONLY, 2) getMessage(uid) just after open() is not work correctly, 3) remote Inbox folder is not closed after deleting message, PENDING_COMMAND_TRASH does not work. And also the fat that some POP3 server use UID with '-' prevents PENDING_COMMAND_TRASH to recognize the message is remote message. Internal Bug: 1651709 --- .../android/email/MessagingController.java | 9 +++-- .../android/email/mail/store/Pop3Store.java | 16 ++++++--- .../email/mail/store/Pop3StoreUnitTests.java | 34 +++++++++++++++++-- 3 files changed, 51 insertions(+), 8 deletions(-) diff --git a/src/com/android/email/MessagingController.java b/src/com/android/email/MessagingController.java index bc9aff822..81a112ab8 100644 --- a/src/com/android/email/MessagingController.java +++ b/src/com/android/email/MessagingController.java @@ -1100,15 +1100,16 @@ public class MessagingController implements Runnable { } remoteFolder.open(OpenMode.READ_WRITE, localFolder.getPersistentCallbacks()); if (remoteFolder.getMode() != OpenMode.READ_WRITE) { + remoteFolder.close(false); return; } Message remoteMessage = null; - if (!uid.startsWith("Local") - && !uid.contains("-")) { + if (!uid.startsWith("Local")) { remoteMessage = remoteFolder.getMessage(uid); } if (remoteMessage == null) { + remoteFolder.close(false); return; } @@ -1132,6 +1133,8 @@ public class MessagingController implements Runnable { (LocalFolder) localStore.getFolder(account.getTrashFolderName()); remoteTrashFolder.open(OpenMode.READ_WRITE, localTrashFolder.getPersistentCallbacks()); if (remoteTrashFolder.getMode() != OpenMode.READ_WRITE) { + remoteFolder.close(false); + remoteTrashFolder.close(false); return; } @@ -1164,10 +1167,12 @@ public class MessagingController implements Runnable { } ); + remoteTrashFolder.close(false); } remoteMessage.setFlag(Flag.DELETED, true); remoteFolder.expunge(); + remoteFolder.close(false); } /** diff --git a/src/com/android/email/mail/store/Pop3Store.java b/src/com/android/email/mail/store/Pop3Store.java index 8104e859f..ea0976694 100644 --- a/src/com/android/email/mail/store/Pop3Store.java +++ b/src/com/android/email/mail/store/Pop3Store.java @@ -294,7 +294,7 @@ public class Pop3Store extends Store { @Override public OpenMode getMode() throws MessagingException { - return OpenMode.READ_ONLY; + return OpenMode.READ_WRITE; } /** @@ -342,10 +342,18 @@ public class Pop3Store extends Store { @Override public Message getMessage(String uid) throws MessagingException { - Pop3Message message = mUidToMsgMap.get(uid); - if (message == null) { - message = new Pop3Message(uid, this); + if (mUidToMsgNumMap.size() == 0) { + try { + indexMsgNums(1, mMessageCount); + } catch (IOException ioe) { + mTransport.close(); + if (Email.DEBUG) { + Log.d(Email.LOG_TAG, "Unable to index during getMessage " + ioe); + } + throw new MessagingException("getMessages", ioe); + } } + Pop3Message message = mUidToMsgMap.get(uid); return message; } diff --git a/tests/src/com/android/email/mail/store/Pop3StoreUnitTests.java b/tests/src/com/android/email/mail/store/Pop3StoreUnitTests.java index da8fe4381..a3469df0a 100644 --- a/tests/src/com/android/email/mail/store/Pop3StoreUnitTests.java +++ b/tests/src/com/android/email/mail/store/Pop3StoreUnitTests.java @@ -32,6 +32,7 @@ import com.android.email.mail.transport.MockTransport; import android.test.AndroidTestCase; import android.test.suitebuilder.annotation.SmallTest; +import android.util.Log; /** * This is a series of unit tests for the POP3 Store class. These tests must be locally @@ -234,8 +235,8 @@ public class Pop3StoreUnitTests extends AndroidTestCase { */ public void testSmallFolderFunctions() throws MessagingException { - // getMode() returns OpenMode.READ_ONLY - assertEquals(OpenMode.READ_ONLY, mFolder.getMode()); + // getMode() returns OpenMode.READ_WRITE + assertEquals(OpenMode.READ_WRITE, mFolder.getMode()); // create() return false assertFalse(mFolder.create(FolderType.HOLDS_FOLDERS)); @@ -321,6 +322,35 @@ public class Pop3StoreUnitTests extends AndroidTestCase { checkOneUnread(mockTransport); } + /** + * Test the process of opening and getting message by uid. + */ + public void testGetMessageByUid() throws MessagingException { + + MockTransport mockTransport = openAndInjectMockTransport(); + + setupOpenFolder(mockTransport, 2, null); + mFolder.open(OpenMode.READ_WRITE, null); + // check message count + assertEquals(2, mFolder.getMessageCount()); + + // setup 2 messages + setupUidlSequence(mockTransport, 2); + String uid1 = getSingleMessageUID(1); + String uid2 = getSingleMessageUID(2); + String uid3 = getSingleMessageUID(3); + + Message msg1 = mFolder.getMessage(uid1); + assertTrue("message with uid1", msg1 != null); + + // uid3 does not exist + Message msg3 = mFolder.getMessage(uid3); + assertTrue("message with uid3", msg3 == null); + + Message msg2 = mFolder.getMessage(uid2); + assertTrue("message with uid2", msg2 != null); + } + /** * Test the scenario where the transport is "open" but not really (e.g. server closed). Two * things should happen: We should see an intermediate failure that makes sense, and the next