|
| 1 | +package com.fsck.k9.notification |
| 2 | + |
| 3 | +import android.app.Application |
| 4 | +import android.app.Notification |
| 5 | +import android.app.PendingIntent |
| 6 | +import android.graphics.Bitmap |
| 7 | +import androidx.core.app.NotificationCompat |
| 8 | +import androidx.test.core.app.ApplicationProvider |
| 9 | +import app.k9mail.legacy.message.controller.MessageReference |
| 10 | +import assertk.assertThat |
| 11 | +import assertk.assertions.isEqualTo |
| 12 | +import com.fsck.k9.mail.Address |
| 13 | +import kotlinx.coroutines.flow.Flow |
| 14 | +import kotlinx.coroutines.flow.MutableStateFlow |
| 15 | +import kotlinx.coroutines.flow.update |
| 16 | +import kotlinx.coroutines.test.UnconfinedTestDispatcher |
| 17 | +import kotlinx.coroutines.test.runTest |
| 18 | +import net.thunderbird.core.android.account.LegacyAccountDto |
| 19 | +import net.thunderbird.core.android.testing.MockHelper.mockBuilder |
| 20 | +import net.thunderbird.core.android.testing.RobolectricTest |
| 21 | +import net.thunderbird.core.preference.notification.NotificationPreference |
| 22 | +import net.thunderbird.core.preference.notification.NotificationPreferenceManager |
| 23 | +import net.thunderbird.core.testing.coroutines.MainDispatcherHelper |
| 24 | +import org.junit.After |
| 25 | +import org.junit.Before |
| 26 | +import org.junit.Test |
| 27 | +import org.mockito.kotlin.any |
| 28 | +import org.mockito.kotlin.doReturn |
| 29 | +import org.mockito.kotlin.mock |
| 30 | + |
| 31 | +class SingleMessageNotificationCreatorTest : RobolectricTest() { |
| 32 | + private val mainDispatcher = MainDispatcherHelper(UnconfinedTestDispatcher()) |
| 33 | + private val notificationPreferenceManager = FakeNotificationPreferenceManager() |
| 34 | + private val resourceProvider = TestAvatarNotificationResourceProvider() |
| 35 | + private val notification = mock<Notification>() |
| 36 | + private val builder = mockBuilder<NotificationCompat.Builder> { |
| 37 | + on { build() } doReturn notification |
| 38 | + } |
| 39 | + |
| 40 | + private lateinit var testSubject: SingleMessageNotificationCreator |
| 41 | + |
| 42 | + @Before |
| 43 | + fun setUp() { |
| 44 | + mainDispatcher.setUp() |
| 45 | + testSubject = SingleMessageNotificationCreator( |
| 46 | + notificationHelper = createNotificationHelper(), |
| 47 | + actionCreator = createNotificationActionCreator(), |
| 48 | + resourceProvider = resourceProvider, |
| 49 | + lockScreenNotificationCreator = mock(), |
| 50 | + notificationPreferenceManager = notificationPreferenceManager, |
| 51 | + application = ApplicationProvider.getApplicationContext<Application>(), |
| 52 | + ) |
| 53 | + } |
| 54 | + |
| 55 | + @After |
| 56 | + fun tearDown() { |
| 57 | + mainDispatcher.tearDown() |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + fun `create notification looks up avatar when notification contact pictures are enabled`() = runTest { |
| 62 | + notificationPreferenceManager.setShowContactPictureInNotification(true) |
| 63 | + |
| 64 | + testSubject.createSingleNotification( |
| 65 | + baseNotificationData = createBaseNotificationData(), |
| 66 | + singleNotificationData = createSingleNotificationData(), |
| 67 | + ).join() |
| 68 | + |
| 69 | + assertThat(resourceProvider.avatarCalls).isEqualTo(1) |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + fun `create notification skips avatar lookup when notification contact pictures are disabled`() = runTest { |
| 74 | + notificationPreferenceManager.setShowContactPictureInNotification(false) |
| 75 | + |
| 76 | + testSubject.createSingleNotification( |
| 77 | + baseNotificationData = createBaseNotificationData(), |
| 78 | + singleNotificationData = createSingleNotificationData(), |
| 79 | + ).join() |
| 80 | + |
| 81 | + assertThat(resourceProvider.avatarCalls).isEqualTo(0) |
| 82 | + } |
| 83 | + |
| 84 | + private fun createNotificationHelper(): NotificationHelper { |
| 85 | + return mock { |
| 86 | + on { createNotificationBuilder(any(), any()) } doReturn builder |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + private fun createNotificationActionCreator(): NotificationActionCreator { |
| 91 | + val pendingIntent = mock<PendingIntent>() |
| 92 | + return mock { |
| 93 | + on { createViewMessagePendingIntent(any()) } doReturn pendingIntent |
| 94 | + on { createDismissMessagePendingIntent(any()) } doReturn pendingIntent |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + private fun createBaseNotificationData(): BaseNotificationData { |
| 99 | + return BaseNotificationData( |
| 100 | + account = LegacyAccountDto("00000000-0000-0000-0000-000000000000"), |
| 101 | + accountName = "Account name", |
| 102 | + groupKey = "group", |
| 103 | + color = 0, |
| 104 | + newMessagesCount = 1, |
| 105 | + lockScreenNotificationData = LockScreenNotificationData.None, |
| 106 | + appearance = NotificationAppearance( |
| 107 | + ringtone = null, |
| 108 | + vibrationPattern = null, |
| 109 | + ledColor = null, |
| 110 | + ), |
| 111 | + ) |
| 112 | + } |
| 113 | + |
| 114 | + private fun createSingleNotificationData(): SingleNotificationData { |
| 115 | + return SingleNotificationData( |
| 116 | + notificationId = 23, |
| 117 | + isSilent = true, |
| 118 | + timestamp = 9000, |
| 119 | + content = NotificationContent( |
| 120 | + messageReference = MessageReference("account", 1, "uid"), |
| 121 | + sender = Address("alice@example.com", "Alice"), |
| 122 | + subject = "Subject", |
| 123 | + preview = "Preview", |
| 124 | + summary = "Summary", |
| 125 | + ), |
| 126 | + actions = emptyList(), |
| 127 | + wearActions = emptyList(), |
| 128 | + addLockScreenNotification = false, |
| 129 | + ) |
| 130 | + } |
| 131 | + |
| 132 | + private class TestAvatarNotificationResourceProvider : |
| 133 | + NotificationResourceProvider by TestNotificationResourceProvider() { |
| 134 | + var avatarCalls = 0 |
| 135 | + |
| 136 | + override suspend fun avatar(address: Address): Bitmap? { |
| 137 | + avatarCalls += 1 |
| 138 | + return Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888) |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + private class FakeNotificationPreferenceManager : NotificationPreferenceManager { |
| 143 | + private val prefs = MutableStateFlow(NotificationPreference()) |
| 144 | + |
| 145 | + override fun save(config: NotificationPreference) = Unit |
| 146 | + |
| 147 | + override fun getConfig(): NotificationPreference = prefs.value |
| 148 | + |
| 149 | + override fun getConfigFlow(): Flow<NotificationPreference> = prefs |
| 150 | + |
| 151 | + fun setShowContactPictureInNotification(isEnabled: Boolean) { |
| 152 | + prefs.update { it.copy(isShowContactPictureInNotification = isEnabled) } |
| 153 | + } |
| 154 | + } |
| 155 | +} |
0 commit comments