diff --git a/backend/imap/src/main/java/com/fsck/k9/backend/imap/ImapSync.kt b/backend/imap/src/main/java/com/fsck/k9/backend/imap/ImapSync.kt index cfe5c6b3c..3fe9a0c8f 100644 --- a/backend/imap/src/main/java/com/fsck/k9/backend/imap/ImapSync.kt +++ b/backend/imap/src/main/java/com/fsck/k9/backend/imap/ImapSync.kt @@ -75,6 +75,9 @@ internal class ImapSync( if (syncConfig.expungePolicy === ExpungePolicy.ON_POLL) { Timber.d("SYNC: Expunging folder %s:%s", accountName, folder) + if (!remoteFolder.isOpen || remoteFolder.mode != OpenMode.READ_WRITE) { + remoteFolder.open(OpenMode.READ_WRITE) + } remoteFolder.expunge() } diff --git a/backend/imap/src/test/java/com/fsck/k9/backend/imap/TestImapFolder.kt b/backend/imap/src/test/java/com/fsck/k9/backend/imap/TestImapFolder.kt index 9e3b6a9d4..306fb43f3 100644 --- a/backend/imap/src/test/java/com/fsck/k9/backend/imap/TestImapFolder.kt +++ b/backend/imap/src/test/java/com/fsck/k9/backend/imap/TestImapFolder.kt @@ -17,6 +17,9 @@ open class TestImapFolder(override val serverId: String) : ImapFolder { override var mode: OpenMode? = null protected set + override val isOpen: Boolean + get() = mode != null + override var messageCount: Int = 0 var wasExpunged: Boolean = false diff --git a/mail/protocols/imap/src/main/java/com/fsck/k9/mail/store/imap/ImapFolder.kt b/mail/protocols/imap/src/main/java/com/fsck/k9/mail/store/imap/ImapFolder.kt index 784cab2b6..1e2195a80 100644 --- a/mail/protocols/imap/src/main/java/com/fsck/k9/mail/store/imap/ImapFolder.kt +++ b/mail/protocols/imap/src/main/java/com/fsck/k9/mail/store/imap/ImapFolder.kt @@ -14,6 +14,7 @@ interface ImapFolder { val serverId: String val mode: OpenMode? val messageCount: Int + val isOpen: Boolean @Throws(MessagingException::class) fun exists(): Boolean diff --git a/mail/protocols/imap/src/main/java/com/fsck/k9/mail/store/imap/RealImapFolder.kt b/mail/protocols/imap/src/main/java/com/fsck/k9/mail/store/imap/RealImapFolder.kt index 0bce310d7..0ba66a73b 100644 --- a/mail/protocols/imap/src/main/java/com/fsck/k9/mail/store/imap/RealImapFolder.kt +++ b/mail/protocols/imap/src/main/java/com/fsck/k9/mail/store/imap/RealImapFolder.kt @@ -46,7 +46,7 @@ internal class RealImapFolder( override var mode: OpenMode? = null private set - val isOpen: Boolean + override val isOpen: Boolean get() = connection != null override fun getUidValidity(): Long? { @@ -1073,7 +1073,6 @@ internal class RealImapFolder( @Throws(MessagingException::class) override fun expunge() { - open(OpenMode.READ_WRITE) checkOpen() try { diff --git a/mail/protocols/imap/src/test/java/com/fsck/k9/mail/store/imap/RealImapFolderTest.kt b/mail/protocols/imap/src/test/java/com/fsck/k9/mail/store/imap/RealImapFolderTest.kt index 8a849c01f..3a07d118a 100644 --- a/mail/protocols/imap/src/test/java/com/fsck/k9/mail/store/imap/RealImapFolderTest.kt +++ b/mail/protocols/imap/src/test/java/com/fsck/k9/mail/store/imap/RealImapFolderTest.kt @@ -1076,10 +1076,23 @@ class RealImapFolderTest { assertThat(uid).isEqualTo("23") } + @Test + fun `expunge() on closed folder should throw`() { + val folder = createFolder("Folder") + + assertFailure { + folder.expunge() + }.isInstanceOf() + .hasMessage("Folder Folder is not open.") + + verifyNoMoreInteractions(imapConnection) + } + @Test fun expunge_shouldIssueExpungeCommand() { val folder = createFolder("Folder") prepareImapFolderForOpen(OpenMode.READ_WRITE) + folder.open(OpenMode.READ_WRITE) folder.expunge() diff --git a/mail/protocols/imap/src/test/java/com/fsck/k9/mail/store/imap/TestImapFolder.kt b/mail/protocols/imap/src/test/java/com/fsck/k9/mail/store/imap/TestImapFolder.kt index 8bd973fe0..12e96f6ad 100644 --- a/mail/protocols/imap/src/test/java/com/fsck/k9/mail/store/imap/TestImapFolder.kt +++ b/mail/protocols/imap/src/test/java/com/fsck/k9/mail/store/imap/TestImapFolder.kt @@ -18,7 +18,7 @@ internal open class TestImapFolder( override var messageCount: Int = 0 protected set - var isOpen: Boolean = false + override var isOpen: Boolean = false protected set private var openAction: () -> Unit = {}