Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ package com.android.messaging.data.conversationlist.store
import com.android.messaging.data.secondaryuser.SecondaryUserNotifier
import com.android.messaging.datamodel.DataModel
import com.android.messaging.datamodel.SyncManager
import com.android.messaging.datamodel.action.MarkAsSeenAction
import io.mockk.every
import io.mockk.just
import io.mockk.mockk
import io.mockk.mockkStatic
import io.mockk.runs
import io.mockk.unmockkAll
import io.mockk.verify
import org.junit.After
Expand All @@ -24,9 +27,11 @@ internal class ConversationListStatusStoreTest {
@Before
fun setUp() {
mockkStatic(DataModel::class)
mockkStatic(MarkAsSeenAction::class)

every { DataModel.get() } returns dataModel
every { dataModel.syncManager } returns syncManager
every { MarkAsSeenAction.markAllAsSeen(any()) } just runs
}

@After
Expand All @@ -46,6 +51,7 @@ internal class ConversationListStatusStoreTest {
store.setNewestConversationVisible(isVisible = true)

verify { dataModel.isConversationListScrolledToNewestConversation = true }
verify(exactly = 1) { MarkAsSeenAction.markAllAsSeen(false) }
verify { secondaryUserNotifier.cancel() }
}

Expand All @@ -56,6 +62,9 @@ internal class ConversationListStatusStoreTest {
verify(exactly = 1) {
dataModel.isConversationListScrolledToNewestConversation = false
}
verify(exactly = 0) {
MarkAsSeenAction.markAllAsSeen(any())
}
verify(exactly = 0) {
secondaryUserNotifier.cancel()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
package com.android.messaging.datamodel.action

import android.content.Context
import android.database.Cursor
import androidx.core.content.contentValuesOf
import com.android.messaging.FactoryTestAccess
import com.android.messaging.datamodel.BugleNotifications
import com.android.messaging.datamodel.DataModel
import com.android.messaging.datamodel.DatabaseHelper
import com.android.messaging.datamodel.DatabaseWrapper
import com.android.messaging.datamodel.createInMemoryActionSyncTestDatabase
import com.android.messaging.datamodel.data.MessageData
import com.android.messaging.testutil.installTestFactory
import com.android.messaging.util.PendingIntentConstants
import io.mockk.Runs
import io.mockk.every
import io.mockk.just
import io.mockk.mockk
import io.mockk.mockkStatic
import io.mockk.unmockkAll
import io.mockk.verify
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment
import org.robolectric.annotation.Config

@RunWith(RobolectricTestRunner::class)
@Config(sdk = [36])
internal class MarkAsSeenActionTest {

private lateinit var context: Context
private lateinit var database: DatabaseWrapper

@Before
fun setUp() {
context = RuntimeEnvironment.getApplication().applicationContext

val dataModel = mockk<DataModel>(relaxed = true)
installTestFactory(context = context, dataModel = dataModel)
database = createInMemoryActionSyncTestDatabase(context)
every { dataModel.database } returns database

mockkStatic(BugleNotifications::class)
every { BugleNotifications.cancel(any(), any()) } just Runs
every { BugleNotifications.update(any<Int>()) } just Runs
}

@After
fun tearDown() {
unmockkAll()
FactoryTestAccess.reset()
}

@Test
fun executeAction_withoutCancelling_marksAllSeenAndUpdatesNotifications() {
insertUnseenMessage(
conversationName = "First conversation",
recipient = "+15551230000",
)
insertUnseenMessage(
conversationName = "Second conversation",
recipient = "+15551230001",
)

MarkAsSeenAction(null, false).executeAction()

assertEquals(0, countUnseenMessages())
verify(exactly = 1) {
BugleNotifications.update(BugleNotifications.UPDATE_MESSAGES)
}
verify(exactly = 0) {
BugleNotifications.cancel(any(), any())
}
}

@Test
fun executeAction_withoutChanges_doesNotUpdateOrCancelNotifications() {
MarkAsSeenAction(null, false).executeAction()

verify(exactly = 0) {
BugleNotifications.update(any<Int>())
}
verify(exactly = 0) {
BugleNotifications.cancel(any(), any())
}
}

@Test
fun publicConstructor_defaultsToCancellingNotifications() {
MarkAsSeenAction(null as String?).executeAction()

verify(exactly = 1) {
BugleNotifications.cancel(PendingIntentConstants.SMS_NOTIFICATION_ID, null)
}
verify(exactly = 0) {
BugleNotifications.update(any<Int>())
}
}

private fun insertUnseenMessage(conversationName: String, recipient: String) {
val participantId = database.insert(
DatabaseHelper.PARTICIPANTS_TABLE,
null,
contentValuesOf(
DatabaseHelper.ParticipantColumns.NORMALIZED_DESTINATION to recipient,
),
)
assertTrue("participant insert failed", participantId >= 0)

val conversationId = database.insert(
DatabaseHelper.CONVERSATIONS_TABLE,
null,
contentValuesOf(DatabaseHelper.ConversationColumns.NAME to conversationName),
)
assertTrue("conversation insert failed", conversationId >= 0)

val messageId = database.insert(
DatabaseHelper.MESSAGES_TABLE,
null,
contentValuesOf(
DatabaseHelper.MessageColumns.CONVERSATION_ID to conversationId,
DatabaseHelper.MessageColumns.SENDER_PARTICIPANT_ID to participantId,
DatabaseHelper.MessageColumns.SELF_PARTICIPANT_ID to participantId,
DatabaseHelper.MessageColumns.STATUS to MessageData.BUGLE_STATUS_INCOMING_COMPLETE,
DatabaseHelper.MessageColumns.SEEN to 0,
DatabaseHelper.MessageColumns.READ to 0,
DatabaseHelper.MessageColumns.RECEIVED_TIMESTAMP to 1L,
DatabaseHelper.MessageColumns.SENT_TIMESTAMP to 1L,
),
)
assertTrue("message insert failed", messageId >= 0)
}

private fun countUnseenMessages(): Int {
return database.query(
DatabaseHelper.MESSAGES_TABLE,
arrayOf("COUNT(*)"),
"${DatabaseHelper.MessageColumns.SEEN}=0",
null,
null,
null,
null,
).use { cursor -> cursor.readCount() }
}

private fun Cursor.readCount(): Int {
return when {
moveToFirst() -> getInt(0)
else -> 0
}
}
}
Loading