From 9d2dae67506983c64f72350a4fb5967cfd85b9a8 Mon Sep 17 00:00:00 2001 From: Todd Kennedy Date: Thu, 9 Jun 2011 11:31:52 -0700 Subject: [PATCH] Set proper default values for new mailboxes When creating mailboxes (specifically 'drafts' or 'sent'), we need to ensure that its default values for flags & server key are set correctly. In most cases this is "okay" because the mailbox will be created on the server and the values for these fields will be reset. However, in cases where system mailboxes cannot be created on the sever (for example, the 'drafts' folder on any POP3 account or an IMAP gmail account), these default values do not get updated and we are unable to view the contents of these mailboxes. bug 4356871 Change-Id: I9e6a394145f471b555c5827d5114bca243dbc37c --- src/com/android/email/Controller.java | 8 +++- .../email/ControllerProviderOpsTests.java | 40 +++++++++++++++---- 2 files changed, 39 insertions(+), 9 deletions(-) diff --git a/src/com/android/email/Controller.java b/src/com/android/email/Controller.java index 3de1263fd..5d46a4750 100644 --- a/src/com/android/email/Controller.java +++ b/src/com/android/email/Controller.java @@ -525,7 +525,8 @@ public class Controller { * Create a mailbox given the account and mailboxType. * TODO: Does this need to be signaled explicitly to the sync engines? */ - /* package */ long createMailbox(long accountId, int mailboxType) { + @VisibleForTesting + long createMailbox(long accountId, int mailboxType) { if (accountId < 0 || mailboxType < 0) { String mes = "Invalid arguments " + accountId + ' ' + mailboxType; Log.e(Logging.LOG_TAG, mes); @@ -537,6 +538,11 @@ public class Controller { box.mSyncInterval = EmailContent.Account.CHECK_INTERVAL_NEVER; box.mFlagVisible = true; box.mServerId = box.mDisplayName = getMailboxServerName(mailboxType); + // All system mailboxes are off the top-level & can hold mail + if (mailboxType != Mailbox.TYPE_MAIL) { + box.mParentKey = Mailbox.NO_MAILBOX; + box.mFlags = Mailbox.FLAG_HOLDS_MAIL; + } box.save(mProviderContext); return box.mId; } diff --git a/tests/src/com/android/email/ControllerProviderOpsTests.java b/tests/src/com/android/email/ControllerProviderOpsTests.java index 3cc2f9e01..a801e0aaf 100644 --- a/tests/src/com/android/email/ControllerProviderOpsTests.java +++ b/tests/src/com/android/email/ControllerProviderOpsTests.java @@ -110,17 +110,41 @@ public class ControllerProviderOpsTests extends ProviderTestCase2 * Does not test duplication, bad accountID, or any other bad input. */ public void testCreateMailbox() { - Account account = ProviderTestUtils.setupAccount("mailboxid", true, mProviderContext); - long accountId = account.mId; + // safety check that system mailboxes don't exist ... + assertEquals(Mailbox.NO_MAILBOX, + Mailbox.findMailboxOfType(mProviderContext, 1L, Mailbox.TYPE_DRAFTS)); + assertEquals(Mailbox.NO_MAILBOX, + Mailbox.findMailboxOfType(mProviderContext, 1L, Mailbox.TYPE_SENT)); - long oldBoxId = Mailbox.findMailboxOfType(mProviderContext, accountId, Mailbox.TYPE_DRAFTS); - assertEquals(Mailbox.NO_MAILBOX, oldBoxId); + long testMailboxId; + Mailbox testMailbox; - mTestController.createMailbox(accountId, Mailbox.TYPE_DRAFTS); - long boxId = Mailbox.findMailboxOfType(mProviderContext, accountId, Mailbox.TYPE_DRAFTS); + // Test creating "drafts" mailbox + mTestController.createMailbox(1L, Mailbox.TYPE_DRAFTS); + testMailboxId = Mailbox.findMailboxOfType(mProviderContext, 1L, Mailbox.TYPE_DRAFTS); + assertTrue(testMailboxId != Mailbox.NO_MAILBOX); + testMailbox = Mailbox.restoreMailboxWithId(mProviderContext, testMailboxId); + assertNotNull(testMailbox); + assertEquals(8, testMailbox.mFlags); // Flags should be "holds mail" + assertEquals(-1L, testMailbox.mParentKey); // Parent is off the top-level - // check that the drafts mailbox exists - assertTrue("mailbox exists", boxId != Mailbox.NO_MAILBOX); + // Test creating "sent" mailbox; same as drafts + mTestController.createMailbox(1L, Mailbox.TYPE_SENT); + testMailboxId = Mailbox.findMailboxOfType(mProviderContext, 1L, Mailbox.TYPE_SENT); + assertTrue(testMailboxId != Mailbox.NO_MAILBOX); + testMailbox = Mailbox.restoreMailboxWithId(mProviderContext, testMailboxId); + assertNotNull(testMailbox); + assertEquals(8, testMailbox.mFlags); // Flags should be "holds mail" + assertEquals(-1L, testMailbox.mParentKey); // Parent is off the top-level + + // Test creating user mailbox; some fields have changed + mTestController.createMailbox(1L, Mailbox.TYPE_MAIL); + testMailboxId = Mailbox.findMailboxOfType(mProviderContext, 1L, Mailbox.TYPE_MAIL); + assertTrue(testMailboxId != Mailbox.NO_MAILBOX); + testMailbox = Mailbox.restoreMailboxWithId(mProviderContext, testMailboxId); + assertNotNull(testMailbox); + assertEquals(0, testMailbox.mFlags); // Flags are not set + assertEquals(0L, testMailbox.mParentKey); // Parent is not set } /**