|
| 1 | +/* |
| 2 | + * Copyright 2025 New Vector Ltd. |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial |
| 5 | + * Please see LICENSE files in the repository root for full details. |
| 6 | + */ |
| 7 | + |
| 8 | +package io.element.android.libraries.push.impl.notifications |
| 9 | + |
| 10 | +import com.google.common.truth.Truth.assertThat |
| 11 | +import io.element.android.libraries.matrix.api.notification.CallNotifyType |
| 12 | +import io.element.android.libraries.matrix.api.notification.NotificationContent |
| 13 | +import io.element.android.libraries.matrix.test.AN_EVENT_ID |
| 14 | +import io.element.android.libraries.matrix.test.A_ROOM_ID |
| 15 | +import io.element.android.libraries.matrix.test.A_ROOM_NAME |
| 16 | +import io.element.android.libraries.matrix.test.A_SESSION_ID |
| 17 | +import io.element.android.libraries.matrix.test.A_USER_ID_2 |
| 18 | +import io.element.android.libraries.matrix.test.A_USER_NAME_2 |
| 19 | +import io.element.android.libraries.matrix.test.FakeMatrixClient |
| 20 | +import io.element.android.libraries.matrix.test.FakeMatrixClientProvider |
| 21 | +import io.element.android.libraries.matrix.test.notification.aNotificationData |
| 22 | +import io.element.android.libraries.matrix.test.room.FakeBaseRoom |
| 23 | +import io.element.android.libraries.matrix.test.room.FakeJoinedRoom |
| 24 | +import io.element.android.libraries.matrix.test.room.aRoomInfo |
| 25 | +import io.element.android.libraries.push.impl.notifications.model.NotifiableMessageEvent |
| 26 | +import io.element.android.libraries.push.impl.notifications.model.NotifiableRingingCallEvent |
| 27 | +import io.element.android.services.appnavstate.test.FakeAppForegroundStateService |
| 28 | +import io.element.android.services.toolbox.test.strings.FakeStringProvider |
| 29 | +import kotlinx.coroutines.test.runTest |
| 30 | +import org.junit.Test |
| 31 | + |
| 32 | +class DefaultCallNotificationEventResolverTest { |
| 33 | + @Test |
| 34 | + fun `resolve CallNotify - RING when call is still ongoing`() = runTest { |
| 35 | + val room = FakeJoinedRoom( |
| 36 | + baseRoom = FakeBaseRoom( |
| 37 | + sessionId = A_SESSION_ID, |
| 38 | + roomId = A_ROOM_ID, |
| 39 | + // The call is still ongoing |
| 40 | + initialRoomInfo = aRoomInfo(hasRoomCall = true), |
| 41 | + ) |
| 42 | + ) |
| 43 | + val client = FakeMatrixClient().apply { |
| 44 | + givenGetRoomResult(A_ROOM_ID, room) |
| 45 | + } |
| 46 | + |
| 47 | + val resolver = createDefaultNotifiableEventResolver( |
| 48 | + clientProvider = FakeMatrixClientProvider(getClient = { Result.success(client) }), |
| 49 | + ) |
| 50 | + val expectedResult = NotifiableRingingCallEvent( |
| 51 | + sessionId = A_SESSION_ID, |
| 52 | + roomId = A_ROOM_ID, |
| 53 | + eventId = AN_EVENT_ID, |
| 54 | + senderId = A_USER_ID_2, |
| 55 | + roomName = A_ROOM_NAME, |
| 56 | + editedEventId = null, |
| 57 | + description = "📹 Incoming call", |
| 58 | + timestamp = 567L, |
| 59 | + canBeReplaced = true, |
| 60 | + isRedacted = false, |
| 61 | + isUpdated = false, |
| 62 | + senderDisambiguatedDisplayName = A_USER_NAME_2, |
| 63 | + senderAvatarUrl = null, |
| 64 | + callNotifyType = CallNotifyType.RING, |
| 65 | + ) |
| 66 | + |
| 67 | + val notificationData = aNotificationData( |
| 68 | + content = NotificationContent.MessageLike.CallNotify(A_USER_ID_2, CallNotifyType.RING) |
| 69 | + ) |
| 70 | + val result = resolver.resolveEvent(A_SESSION_ID, notificationData) |
| 71 | + assertThat(result.getOrNull()).isEqualTo(expectedResult) |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + fun `resolve CallNotify - NOTIFY`() = runTest { |
| 76 | + val room = FakeJoinedRoom( |
| 77 | + baseRoom = FakeBaseRoom( |
| 78 | + sessionId = A_SESSION_ID, |
| 79 | + roomId = A_ROOM_ID, |
| 80 | + // The call already ended |
| 81 | + initialRoomInfo = aRoomInfo(hasRoomCall = true), |
| 82 | + ) |
| 83 | + ) |
| 84 | + val client = FakeMatrixClient().apply { |
| 85 | + givenGetRoomResult(A_ROOM_ID, room) |
| 86 | + } |
| 87 | + |
| 88 | + val resolver = createDefaultNotifiableEventResolver( |
| 89 | + clientProvider = FakeMatrixClientProvider(getClient = { Result.success(client) }), |
| 90 | + ) |
| 91 | + val expectedResult = NotifiableMessageEvent( |
| 92 | + sessionId = A_SESSION_ID, |
| 93 | + roomId = A_ROOM_ID, |
| 94 | + eventId = AN_EVENT_ID, |
| 95 | + senderId = A_USER_ID_2, |
| 96 | + roomName = A_ROOM_NAME, |
| 97 | + editedEventId = null, |
| 98 | + body = "📹 Incoming call", |
| 99 | + timestamp = 567L, |
| 100 | + canBeReplaced = false, |
| 101 | + isRedacted = false, |
| 102 | + isUpdated = false, |
| 103 | + senderDisambiguatedDisplayName = A_USER_NAME_2, |
| 104 | + noisy = true, |
| 105 | + imageUriString = null, |
| 106 | + imageMimeType = null, |
| 107 | + threadId = null, |
| 108 | + type = "m.call.notify", |
| 109 | + ) |
| 110 | + |
| 111 | + val notificationData = aNotificationData( |
| 112 | + content = NotificationContent.MessageLike.CallNotify(A_USER_ID_2, CallNotifyType.NOTIFY) |
| 113 | + ) |
| 114 | + val result = resolver.resolveEvent(A_SESSION_ID, notificationData) |
| 115 | + assertThat(result.getOrNull()).isEqualTo(expectedResult) |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + fun `resolve CallNotify - RING but timed out displays the same as NOTIFY`() = runTest { |
| 120 | + val room = FakeJoinedRoom( |
| 121 | + baseRoom = FakeBaseRoom( |
| 122 | + sessionId = A_SESSION_ID, |
| 123 | + roomId = A_ROOM_ID, |
| 124 | + // The call already ended |
| 125 | + initialRoomInfo = aRoomInfo(hasRoomCall = false), |
| 126 | + ) |
| 127 | + ) |
| 128 | + val client = FakeMatrixClient().apply { |
| 129 | + givenGetRoomResult(A_ROOM_ID, room) |
| 130 | + } |
| 131 | + |
| 132 | + val resolver = createDefaultNotifiableEventResolver( |
| 133 | + clientProvider = FakeMatrixClientProvider(getClient = { Result.success(client) }), |
| 134 | + ) |
| 135 | + val expectedResult = NotifiableMessageEvent( |
| 136 | + sessionId = A_SESSION_ID, |
| 137 | + roomId = A_ROOM_ID, |
| 138 | + eventId = AN_EVENT_ID, |
| 139 | + senderId = A_USER_ID_2, |
| 140 | + roomName = A_ROOM_NAME, |
| 141 | + editedEventId = null, |
| 142 | + body = "📹 Incoming call", |
| 143 | + timestamp = 567L, |
| 144 | + canBeReplaced = false, |
| 145 | + isRedacted = false, |
| 146 | + isUpdated = false, |
| 147 | + senderDisambiguatedDisplayName = A_USER_NAME_2, |
| 148 | + noisy = true, |
| 149 | + imageUriString = null, |
| 150 | + imageMimeType = null, |
| 151 | + threadId = null, |
| 152 | + type = "m.call.notify", |
| 153 | + ) |
| 154 | + |
| 155 | + val notificationData = aNotificationData( |
| 156 | + content = NotificationContent.MessageLike.CallNotify(A_USER_ID_2, CallNotifyType.RING) |
| 157 | + ) |
| 158 | + val result = resolver.resolveEvent(A_SESSION_ID, notificationData) |
| 159 | + assertThat(result.getOrNull()).isEqualTo(expectedResult) |
| 160 | + } |
| 161 | + |
| 162 | + private fun createDefaultNotifiableEventResolver( |
| 163 | + stringProvider: FakeStringProvider = FakeStringProvider(defaultResult = "\uD83D\uDCF9 Incoming call"), |
| 164 | + appForegroundStateService: FakeAppForegroundStateService = FakeAppForegroundStateService(), |
| 165 | + clientProvider: FakeMatrixClientProvider = FakeMatrixClientProvider(), |
| 166 | + ) = DefaultCallNotificationEventResolver( |
| 167 | + stringProvider = stringProvider, |
| 168 | + appForegroundStateService = appForegroundStateService, |
| 169 | + clientProvider = clientProvider, |
| 170 | + ) |
| 171 | +} |
0 commit comments