|
| 1 | +/* |
| 2 | + * Copyright (c) 2014-2025 Stream.io Inc. All rights reserved. |
| 3 | + * |
| 4 | + * Licensed under the Stream License; |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://github.com/GetStream/stream-chat-android/blob/main/LICENSE |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.getstream.chat.android.client.notifications |
| 18 | + |
| 19 | +import android.content.Context |
| 20 | +import androidx.test.core.app.ApplicationProvider |
| 21 | +import androidx.test.ext.junit.runners.AndroidJUnit4 |
| 22 | +import androidx.work.WorkInfo |
| 23 | +import androidx.work.WorkManager |
| 24 | +import androidx.work.testing.WorkManagerTestInitHelper |
| 25 | +import io.getstream.chat.android.client.ChatClient |
| 26 | +import io.getstream.chat.android.client.notifications.handler.NotificationConfig |
| 27 | +import io.getstream.chat.android.client.notifications.handler.NotificationHandler |
| 28 | +import io.getstream.chat.android.client.randomPushMessage |
| 29 | +import io.getstream.chat.android.client.receipts.MessageReceiptManager |
| 30 | +import io.getstream.chat.android.models.Message |
| 31 | +import io.getstream.chat.android.models.PushMessage |
| 32 | +import org.junit.Test |
| 33 | +import org.junit.jupiter.api.Assertions.assertNotNull |
| 34 | +import org.junit.jupiter.api.Assertions.assertNull |
| 35 | +import org.junit.runner.RunWith |
| 36 | +import org.mockito.kotlin.doReturn |
| 37 | +import org.mockito.kotlin.mock |
| 38 | +import org.mockito.kotlin.verify |
| 39 | +import org.mockito.kotlin.whenever |
| 40 | +import org.robolectric.annotation.Config |
| 41 | + |
| 42 | +@RunWith(AndroidJUnit4::class) |
| 43 | +@Config(sdk = [33]) |
| 44 | +internal class ChatNotificationsImplTest { |
| 45 | + |
| 46 | + @Test |
| 47 | + fun `onPushMessage calls onPushNotificationReceived`() { |
| 48 | + val pushMessage = randomPushMessage() |
| 49 | + val mockListener = mock<PushNotificationReceivedListener>() |
| 50 | + val fixture = Fixture() |
| 51 | + val sut = fixture.get() |
| 52 | + |
| 53 | + sut.onPushMessage(pushMessage, mockListener) |
| 54 | + |
| 55 | + verify(mockListener).onPushNotificationReceived( |
| 56 | + channelType = pushMessage.channelType, |
| 57 | + channelId = pushMessage.channelId, |
| 58 | + ) |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + fun `onPushMessage marks message as delivered`() { |
| 63 | + val pushMessage = randomPushMessage() |
| 64 | + val fixture = Fixture() |
| 65 | + val sut = fixture.get() |
| 66 | + |
| 67 | + sut.onPushMessage(pushMessage) |
| 68 | + |
| 69 | + val messages = listOf( |
| 70 | + Message( |
| 71 | + id = pushMessage.messageId, |
| 72 | + cid = "${pushMessage.channelType}:${pushMessage.channelId}", |
| 73 | + ), |
| 74 | + ) |
| 75 | + fixture.verifyMarkMessagesAsDeliveredCalled(messages) |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + fun `onPushMessage schedules work when shouldShowNotificationOnPush is true and handler does not handle message`() { |
| 80 | + val pushMessage = randomPushMessage() |
| 81 | + val fixture = Fixture() |
| 82 | + val sut = fixture.get() |
| 83 | + |
| 84 | + sut.onPushMessage(pushMessage) |
| 85 | + |
| 86 | + val workInfos = getLoadNotificationDataWorkerInfos() |
| 87 | + assertNotNull(workInfos.firstOrNull()) |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + fun `onPushMessage does not schedule work when shouldShowNotificationOnPush is false`() { |
| 92 | + val pushMessage = randomPushMessage() |
| 93 | + val fixture = Fixture() |
| 94 | + .givenNotificationConfig(config = NotificationConfig(shouldShowNotificationOnPush = { false })) |
| 95 | + val sut = fixture.get() |
| 96 | + |
| 97 | + sut.onPushMessage(pushMessage) |
| 98 | + |
| 99 | + val workInfos = getLoadNotificationDataWorkerInfos() |
| 100 | + assertNull(workInfos.firstOrNull()) |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + fun `onPushMessage does not schedule work when handler handles message`() { |
| 105 | + val pushMessage = randomPushMessage() |
| 106 | + val fixture = Fixture() |
| 107 | + .givenOnPushMessageHandled(pushMessage = pushMessage, handled = true) |
| 108 | + val sut = fixture.get() |
| 109 | + |
| 110 | + sut.onPushMessage(pushMessage) |
| 111 | + |
| 112 | + val workInfos = getLoadNotificationDataWorkerInfos() |
| 113 | + assertNull(workInfos.firstOrNull()) |
| 114 | + } |
| 115 | + |
| 116 | + private class Fixture { |
| 117 | + |
| 118 | + private var mockNotificationHandler = mock<NotificationHandler>() |
| 119 | + private var notificationConfig = NotificationConfig() |
| 120 | + private val mockMessageReceiptManager = mock<MessageReceiptManager>() |
| 121 | + |
| 122 | + private val mockChatClient = mock<ChatClient> { |
| 123 | + on { messageReceiptManager } doReturn mockMessageReceiptManager |
| 124 | + } |
| 125 | + |
| 126 | + fun givenOnPushMessageHandled(pushMessage: PushMessage, handled: Boolean) = apply { |
| 127 | + whenever(mockNotificationHandler.onPushMessage(pushMessage)) doReturn handled |
| 128 | + } |
| 129 | + |
| 130 | + fun givenNotificationConfig(config: NotificationConfig) = apply { |
| 131 | + notificationConfig = config |
| 132 | + } |
| 133 | + |
| 134 | + fun verifyMarkMessagesAsDeliveredCalled(messages: List<Message>) { |
| 135 | + verify(mockMessageReceiptManager).markMessagesAsDelivered(messages) |
| 136 | + } |
| 137 | + |
| 138 | + fun get(): ChatNotificationsImpl { |
| 139 | + val context = ApplicationProvider.getApplicationContext<Context>() |
| 140 | + WorkManagerTestInitHelper.initializeTestWorkManager(context) |
| 141 | + |
| 142 | + return ChatNotificationsImpl( |
| 143 | + handler = mockNotificationHandler, |
| 144 | + notificationConfig = notificationConfig, |
| 145 | + context = context, |
| 146 | + scope = mock(), |
| 147 | + chatClient = mockChatClient, |
| 148 | + ) |
| 149 | + } |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +private fun getLoadNotificationDataWorkerInfos(): List<WorkInfo> { |
| 154 | + val workInfos = WorkManager |
| 155 | + .getInstance(ApplicationProvider.getApplicationContext()) |
| 156 | + .getWorkInfosByTag(LoadNotificationDataWorker::class.qualifiedName!!).get() |
| 157 | + return workInfos |
| 158 | +} |
0 commit comments