|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 New Vector Ltd |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "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://www.apache.org/licenses/LICENSE-2.0 |
| 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.element.android.libraries.push.impl.push |
| 18 | + |
| 19 | +import io.element.android.libraries.featureflag.api.FeatureFlags |
| 20 | +import io.element.android.libraries.featureflag.test.FakeFeatureFlagService |
| 21 | +import io.element.android.libraries.matrix.api.MatrixClient |
| 22 | +import io.element.android.libraries.matrix.api.sync.SyncState |
| 23 | +import io.element.android.libraries.matrix.api.timeline.MatrixTimelineItem |
| 24 | +import io.element.android.libraries.matrix.test.A_ROOM_ID |
| 25 | +import io.element.android.libraries.matrix.test.A_UNIQUE_ID |
| 26 | +import io.element.android.libraries.matrix.test.FakeMatrixClient |
| 27 | +import io.element.android.libraries.matrix.test.FakeMatrixClientProvider |
| 28 | +import io.element.android.libraries.matrix.test.room.FakeMatrixRoom |
| 29 | +import io.element.android.libraries.matrix.test.sync.FakeSyncService |
| 30 | +import io.element.android.libraries.matrix.test.timeline.FakeTimeline |
| 31 | +import io.element.android.libraries.matrix.test.timeline.anEventTimelineItem |
| 32 | +import io.element.android.libraries.push.impl.notifications.fixtures.aNotifiableMessageEvent |
| 33 | +import io.element.android.services.appnavstate.test.FakeAppForegroundStateService |
| 34 | +import io.element.android.tests.testutils.lambda.assert |
| 35 | +import io.element.android.tests.testutils.lambda.lambdaRecorder |
| 36 | +import io.element.android.tests.testutils.testCoroutineDispatchers |
| 37 | +import kotlinx.coroutines.coroutineScope |
| 38 | +import kotlinx.coroutines.delay |
| 39 | +import kotlinx.coroutines.flow.MutableStateFlow |
| 40 | +import kotlinx.coroutines.launch |
| 41 | +import kotlinx.coroutines.test.TestScope |
| 42 | +import kotlinx.coroutines.test.runTest |
| 43 | +import org.junit.Test |
| 44 | + |
| 45 | +class SyncOnNotifiableEventTest { |
| 46 | + private val timelineItems = MutableStateFlow<List<MatrixTimelineItem>>(emptyList()) |
| 47 | + private val syncStateFlow = MutableStateFlow(SyncState.Idle) |
| 48 | + private val startSyncLambda = lambdaRecorder<Result<Unit>> { Result.success(Unit) } |
| 49 | + private val stopSyncLambda = lambdaRecorder<Result<Unit>> { Result.success(Unit) } |
| 50 | + private val subscribeToSyncLambda = lambdaRecorder<Unit> { } |
| 51 | + |
| 52 | + private val liveTimeline = FakeTimeline( |
| 53 | + timelineItems = timelineItems, |
| 54 | + ) |
| 55 | + private val room = FakeMatrixRoom( |
| 56 | + roomId = A_ROOM_ID, |
| 57 | + liveTimeline = liveTimeline, |
| 58 | + subscribeToSyncLambda = subscribeToSyncLambda |
| 59 | + ) |
| 60 | + private val syncService = FakeSyncService(syncStateFlow).also { |
| 61 | + it.startSyncLambda = startSyncLambda |
| 62 | + it.stopSyncLambda = stopSyncLambda |
| 63 | + } |
| 64 | + |
| 65 | + private val client = FakeMatrixClient( |
| 66 | + syncService = syncService, |
| 67 | + ).apply { |
| 68 | + givenGetRoomResult(A_ROOM_ID, room) |
| 69 | + } |
| 70 | + |
| 71 | + private val notifiableEvent = aNotifiableMessageEvent() |
| 72 | + |
| 73 | + @Test |
| 74 | + fun `when feature flag is disabled, nothing happens`() = runTest { |
| 75 | + val sut = createSyncOnNotifiableEvent(client = client, isSyncOnPushEnabled = false) |
| 76 | + |
| 77 | + sut(notifiableEvent) |
| 78 | + |
| 79 | + assert(startSyncLambda).isNeverCalled() |
| 80 | + assert(stopSyncLambda).isNeverCalled() |
| 81 | + assert(subscribeToSyncLambda).isNeverCalled() |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + fun `when feature flag is enabled and app is in foreground, sync is not started`() = runTest { |
| 86 | + val sut = createSyncOnNotifiableEvent(client = client, isAppInForeground = true, isSyncOnPushEnabled = true) |
| 87 | + |
| 88 | + sut(notifiableEvent) |
| 89 | + |
| 90 | + assert(startSyncLambda).isNeverCalled() |
| 91 | + assert(stopSyncLambda).isNeverCalled() |
| 92 | + assert(subscribeToSyncLambda).isCalledOnce() |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + fun `when feature flag is enabled and app is in background, sync is started and stopped`() = runTest { |
| 97 | + val sut = createSyncOnNotifiableEvent(client = client, isAppInForeground = false, isSyncOnPushEnabled = true) |
| 98 | + |
| 99 | + timelineItems.emit( |
| 100 | + listOf(MatrixTimelineItem.Event(A_UNIQUE_ID, anEventTimelineItem())) |
| 101 | + ) |
| 102 | + syncStateFlow.emit(SyncState.Running) |
| 103 | + sut(notifiableEvent) |
| 104 | + |
| 105 | + assert(startSyncLambda).isCalledOnce() |
| 106 | + assert(stopSyncLambda).isCalledOnce() |
| 107 | + assert(subscribeToSyncLambda).isCalledOnce() |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + fun `when feature flag is enabled and app is in background, running multiple time only call once`() = runTest { |
| 112 | + val sut = createSyncOnNotifiableEvent(client = client, isAppInForeground = false, isSyncOnPushEnabled = true) |
| 113 | + |
| 114 | + coroutineScope { |
| 115 | + launch { sut(notifiableEvent) } |
| 116 | + launch { sut(notifiableEvent) } |
| 117 | + launch { |
| 118 | + delay(1) |
| 119 | + timelineItems.emit( |
| 120 | + listOf(MatrixTimelineItem.Event(A_UNIQUE_ID, anEventTimelineItem())) |
| 121 | + ) |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + assert(startSyncLambda).isCalledOnce() |
| 126 | + assert(stopSyncLambda).isCalledOnce() |
| 127 | + assert(subscribeToSyncLambda).isCalledExactly(2) |
| 128 | + } |
| 129 | + |
| 130 | + private fun TestScope.createSyncOnNotifiableEvent( |
| 131 | + client: MatrixClient = FakeMatrixClient(), |
| 132 | + isSyncOnPushEnabled: Boolean = true, |
| 133 | + isAppInForeground: Boolean = true, |
| 134 | + ): SyncOnNotifiableEvent { |
| 135 | + val featureFlagService = FakeFeatureFlagService( |
| 136 | + initialState = mapOf( |
| 137 | + FeatureFlags.SyncOnPush.key to isSyncOnPushEnabled |
| 138 | + ) |
| 139 | + ) |
| 140 | + val appForegroundStateService = FakeAppForegroundStateService( |
| 141 | + initialValue = isAppInForeground |
| 142 | + ) |
| 143 | + val matrixClientProvider = FakeMatrixClientProvider { Result.success(client) } |
| 144 | + return SyncOnNotifiableEvent( |
| 145 | + matrixClientProvider = matrixClientProvider, |
| 146 | + featureFlagService = featureFlagService, |
| 147 | + appForegroundStateService = appForegroundStateService, |
| 148 | + dispatchers = testCoroutineDispatchers(), |
| 149 | + ) |
| 150 | + } |
| 151 | +} |
0 commit comments