From 1ccbfea756f434e7ce737f5f0c0ee5f634a8d52b Mon Sep 17 00:00:00 2001 From: cketti Date: Tue, 21 Mar 2023 18:25:30 +0100 Subject: [PATCH 1/2] Add failing test for `DefaultMessageCountsProvider.getMessageCounts(Account)` --- .../DefaultMessageCountsProviderTest.kt | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 app/core/src/test/java/com/fsck/k9/controller/DefaultMessageCountsProviderTest.kt diff --git a/app/core/src/test/java/com/fsck/k9/controller/DefaultMessageCountsProviderTest.kt b/app/core/src/test/java/com/fsck/k9/controller/DefaultMessageCountsProviderTest.kt new file mode 100644 index 000000000..62f0e55a3 --- /dev/null +++ b/app/core/src/test/java/com/fsck/k9/controller/DefaultMessageCountsProviderTest.kt @@ -0,0 +1,48 @@ +package com.fsck.k9.controller + +import assertk.assertThat +import assertk.assertions.isEqualTo +import com.fsck.k9.Account +import com.fsck.k9.Account.FolderMode +import com.fsck.k9.Preferences +import com.fsck.k9.mailstore.ListenableMessageStore +import com.fsck.k9.mailstore.MessageStoreManager +import com.fsck.k9.search.ConditionsTreeNode +import org.junit.Test +import org.mockito.kotlin.anyOrNull +import org.mockito.kotlin.doReturn +import org.mockito.kotlin.mock + +private const val ACCOUNT_UUID = "irrelevant" +private const val UNREAD_COUNT = 2 +private const val STARRED_COUNT = 3 + +class DefaultMessageCountsProviderTest { + private val preferences = mock() + private val account = Account(ACCOUNT_UUID) + private val messageStore = mock { + on { getUnreadMessageCount(anyOrNull()) } doReturn UNREAD_COUNT + on { getStarredMessageCount(anyOrNull()) } doReturn STARRED_COUNT + } + private val messageStoreManager = mock { + on { getMessageStore(account) } doReturn messageStore + } + + private val messageCountsProvider = DefaultMessageCountsProvider(preferences, messageStoreManager) + + @Test + fun `getMessageCounts() without any special folders and displayMode = ALL`() { + account.inboxFolderId = null + account.trashFolderId = null + account.draftsFolderId = null + account.spamFolderId = null + account.outboxFolderId = null + account.sentFolderId = null + account.folderDisplayMode = FolderMode.ALL + + val messageCounts = messageCountsProvider.getMessageCounts(account) + + assertThat(messageCounts.unread).isEqualTo(UNREAD_COUNT) + assertThat(messageCounts.starred).isEqualTo(STARRED_COUNT) + } +} From b5964f32f21e69a2d226f07796138c542ead2b85 Mon Sep 17 00:00:00 2001 From: cketti Date: Tue, 21 Mar 2023 18:26:55 +0100 Subject: [PATCH 2/2] Change `DefaultMessageCountsProvider` to support `LocalSearch.conditions = null` --- .../k9/controller/MessageCountsProvider.kt | 2 +- .../com/fsck/k9/mailstore/MessageStore.kt | 4 +-- .../k9/storage/messages/K9MessageStore.kt | 4 +-- .../messages/RetrieveFolderOperations.kt | 6 ++-- .../messages/RetrieveFolderOperationsTest.kt | 30 +++++++++++++++++++ 5 files changed, 38 insertions(+), 8 deletions(-) diff --git a/app/core/src/main/java/com/fsck/k9/controller/MessageCountsProvider.kt b/app/core/src/main/java/com/fsck/k9/controller/MessageCountsProvider.kt index 242808ec4..518b59fb8 100644 --- a/app/core/src/main/java/com/fsck/k9/controller/MessageCountsProvider.kt +++ b/app/core/src/main/java/com/fsck/k9/controller/MessageCountsProvider.kt @@ -61,7 +61,7 @@ internal class DefaultMessageCountsProvider( } } - private fun getMessageCounts(account: Account, conditions: ConditionsTreeNode): MessageCounts { + private fun getMessageCounts(account: Account, conditions: ConditionsTreeNode?): MessageCounts { return try { val messageStore = messageStoreManager.getMessageStore(account) return MessageCounts( diff --git a/app/core/src/main/java/com/fsck/k9/mailstore/MessageStore.kt b/app/core/src/main/java/com/fsck/k9/mailstore/MessageStore.kt index 05f7e2c7a..01ebbd6fb 100644 --- a/app/core/src/main/java/com/fsck/k9/mailstore/MessageStore.kt +++ b/app/core/src/main/java/com/fsck/k9/mailstore/MessageStore.kt @@ -235,12 +235,12 @@ interface MessageStore { /** * Retrieve the number of unread messages matching [conditions]. */ - fun getUnreadMessageCount(conditions: ConditionsTreeNode): Int + fun getUnreadMessageCount(conditions: ConditionsTreeNode?): Int /** * Retrieve the number of starred messages matching [conditions]. */ - fun getStarredMessageCount(conditions: ConditionsTreeNode): Int + fun getStarredMessageCount(conditions: ConditionsTreeNode?): Int /** * Update a folder's name and type. diff --git a/app/storage/src/main/java/com/fsck/k9/storage/messages/K9MessageStore.kt b/app/storage/src/main/java/com/fsck/k9/storage/messages/K9MessageStore.kt index 3f168ab40..4d1f57d9c 100644 --- a/app/storage/src/main/java/com/fsck/k9/storage/messages/K9MessageStore.kt +++ b/app/storage/src/main/java/com/fsck/k9/storage/messages/K9MessageStore.kt @@ -185,11 +185,11 @@ class K9MessageStore( return retrieveFolderOperations.getUnreadMessageCount(folderId) } - override fun getUnreadMessageCount(conditions: ConditionsTreeNode): Int { + override fun getUnreadMessageCount(conditions: ConditionsTreeNode?): Int { return retrieveFolderOperations.getUnreadMessageCount(conditions) } - override fun getStarredMessageCount(conditions: ConditionsTreeNode): Int { + override fun getStarredMessageCount(conditions: ConditionsTreeNode?): Int { return retrieveFolderOperations.getStarredMessageCount(conditions) } diff --git a/app/storage/src/main/java/com/fsck/k9/storage/messages/RetrieveFolderOperations.kt b/app/storage/src/main/java/com/fsck/k9/storage/messages/RetrieveFolderOperations.kt index a9535daaf..2dbc34f66 100644 --- a/app/storage/src/main/java/com/fsck/k9/storage/messages/RetrieveFolderOperations.kt +++ b/app/storage/src/main/java/com/fsck/k9/storage/messages/RetrieveFolderOperations.kt @@ -172,15 +172,15 @@ $displayModeSelection } } - fun getUnreadMessageCount(conditions: ConditionsTreeNode): Int { + fun getUnreadMessageCount(conditions: ConditionsTreeNode?): Int { return getMessageCount(condition = "messages.read = 0", conditions) } - fun getStarredMessageCount(conditions: ConditionsTreeNode): Int { + fun getStarredMessageCount(conditions: ConditionsTreeNode?): Int { return getMessageCount(condition = "messages.flagged = 1", conditions) } - private fun getMessageCount(condition: String, extraConditions: ConditionsTreeNode): Int { + private fun getMessageCount(condition: String, extraConditions: ConditionsTreeNode?): Int { val whereBuilder = StringBuilder() val queryArgs = mutableListOf() SqlQueryBuilder.buildWhereClause(extraConditions, whereBuilder, queryArgs) diff --git a/app/storage/src/test/java/com/fsck/k9/storage/messages/RetrieveFolderOperationsTest.kt b/app/storage/src/test/java/com/fsck/k9/storage/messages/RetrieveFolderOperationsTest.kt index bdeaa2ea2..ea6c03913 100644 --- a/app/storage/src/test/java/com/fsck/k9/storage/messages/RetrieveFolderOperationsTest.kt +++ b/app/storage/src/test/java/com/fsck/k9/storage/messages/RetrieveFolderOperationsTest.kt @@ -413,12 +413,27 @@ class RetrieveFolderOperationsTest : RobolectricTest() { val folderId2 = sqliteDatabase.createFolder(integrate = true) sqliteDatabase.createMessage(folderId = folderId2, read = false) sqliteDatabase.createMessage(folderId = folderId2, read = true) + val folderId3 = sqliteDatabase.createFolder(integrate = false) + sqliteDatabase.createMessage(folderId = folderId3, read = false) val result = retrieveFolderOperations.getUnreadMessageCount(unifiedInboxConditions) assertThat(result).isEqualTo(3) } + @Test + fun `get unread message count without condition`() { + val folderId1 = sqliteDatabase.createFolder(integrate = true) + sqliteDatabase.createMessage(folderId = folderId1, read = false) + val folderId2 = sqliteDatabase.createFolder(integrate = false) + sqliteDatabase.createMessage(folderId = folderId2, read = false) + sqliteDatabase.createMessage(folderId = folderId2, read = true) + + val result = retrieveFolderOperations.getUnreadMessageCount(conditions = null) + + assertThat(result).isEqualTo(2) + } + @Test fun `get starred message count with condition from empty folder`() { sqliteDatabase.createFolder(integrate = true) @@ -444,12 +459,27 @@ class RetrieveFolderOperationsTest : RobolectricTest() { sqliteDatabase.createMessage(folderId = folderId2, flagged = true) sqliteDatabase.createMessage(folderId = folderId2, flagged = true) sqliteDatabase.createMessage(folderId = folderId2, flagged = false) + val folderId3 = sqliteDatabase.createFolder(integrate = false) + sqliteDatabase.createMessage(folderId = folderId3, flagged = true) val result = retrieveFolderOperations.getStarredMessageCount(unifiedInboxConditions) assertThat(result).isEqualTo(3) } + @Test + fun `get starred message count without condition`() { + val folderId1 = sqliteDatabase.createFolder(integrate = true) + sqliteDatabase.createMessage(folderId = folderId1, flagged = true) + val folderId2 = sqliteDatabase.createFolder(integrate = false) + sqliteDatabase.createMessage(folderId = folderId2, flagged = true) + sqliteDatabase.createMessage(folderId = folderId2, flagged = false) + + val result = retrieveFolderOperations.getStarredMessageCount(conditions = null) + + assertThat(result).isEqualTo(2) + } + @Test fun `get 'more messages' value from non-existent folder`() { assertThat {