Add test for DefaultNotificationDrawerManager
This commit is contained in:
parent
0930cd0dc6
commit
47dc03082e
5 changed files with 180 additions and 8 deletions
|
|
@ -68,6 +68,7 @@ dependencies {
|
||||||
testImplementation(libs.coil.test)
|
testImplementation(libs.coil.test)
|
||||||
testImplementation(libs.coroutines.test)
|
testImplementation(libs.coroutines.test)
|
||||||
testImplementation(projects.libraries.matrix.test)
|
testImplementation(projects.libraries.matrix.test)
|
||||||
|
testImplementation(projects.tests.testutils)
|
||||||
testImplementation(projects.services.appnavstate.test)
|
testImplementation(projects.services.appnavstate.test)
|
||||||
testImplementation(projects.services.toolbox.impl)
|
testImplementation(projects.services.toolbox.impl)
|
||||||
testImplementation(projects.services.toolbox.test)
|
testImplementation(projects.services.toolbox.test)
|
||||||
|
|
|
||||||
|
|
@ -36,6 +36,7 @@ import io.element.android.services.appnavstate.api.AppNavigationStateService
|
||||||
import io.element.android.services.appnavstate.api.NavigationState
|
import io.element.android.services.appnavstate.api.NavigationState
|
||||||
import io.element.android.services.appnavstate.api.currentSessionId
|
import io.element.android.services.appnavstate.api.currentSessionId
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
|
import kotlinx.coroutines.Job
|
||||||
import kotlinx.coroutines.delay
|
import kotlinx.coroutines.delay
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
|
|
@ -61,6 +62,8 @@ class DefaultNotificationDrawerManager @Inject constructor(
|
||||||
private val buildMeta: BuildMeta,
|
private val buildMeta: BuildMeta,
|
||||||
private val matrixClientProvider: MatrixClientProvider,
|
private val matrixClientProvider: MatrixClientProvider,
|
||||||
) : NotificationDrawerManager {
|
) : NotificationDrawerManager {
|
||||||
|
private var appNavigationStateObserver: Job? = null
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Lazily initializes the NotificationState as we rely on having a current session in order to fetch the persisted queue of events.
|
* Lazily initializes the NotificationState as we rely on having a current session in order to fetch the persisted queue of events.
|
||||||
*/
|
*/
|
||||||
|
|
@ -72,12 +75,17 @@ class DefaultNotificationDrawerManager @Inject constructor(
|
||||||
|
|
||||||
init {
|
init {
|
||||||
// Observe application state
|
// Observe application state
|
||||||
coroutineScope.launch {
|
appNavigationStateObserver = coroutineScope.launch {
|
||||||
appNavigationStateService.appNavigationState
|
appNavigationStateService.appNavigationState
|
||||||
.collect { onAppNavigationStateChange(it.navigationState) }
|
.collect { onAppNavigationStateChange(it.navigationState) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// For test only
|
||||||
|
fun destroy() {
|
||||||
|
appNavigationStateObserver?.cancel()
|
||||||
|
}
|
||||||
|
|
||||||
private var currentAppNavigationState: NavigationState? = null
|
private var currentAppNavigationState: NavigationState? = null
|
||||||
|
|
||||||
private fun onAppNavigationStateChange(navigationState: NavigationState) {
|
private fun onAppNavigationStateChange(navigationState: NavigationState) {
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,134 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2023 New Vector Ltd
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package io.element.android.libraries.push.impl.notifications
|
||||||
|
|
||||||
|
import io.element.android.libraries.matrix.test.AN_EVENT_ID
|
||||||
|
import io.element.android.libraries.matrix.test.A_ROOM_ID
|
||||||
|
import io.element.android.libraries.matrix.test.A_SESSION_ID
|
||||||
|
import io.element.android.libraries.matrix.test.A_SPACE_ID
|
||||||
|
import io.element.android.libraries.matrix.test.A_THREAD_ID
|
||||||
|
import io.element.android.libraries.matrix.test.FakeMatrixClientProvider
|
||||||
|
import io.element.android.libraries.matrix.test.core.aBuildMeta
|
||||||
|
import io.element.android.libraries.push.impl.notifications.fake.FakeAndroidNotificationFactory
|
||||||
|
import io.element.android.libraries.push.impl.notifications.fake.FakeRoomGroupMessageCreator
|
||||||
|
import io.element.android.libraries.push.impl.notifications.fake.FakeSummaryGroupMessageCreator
|
||||||
|
import io.element.android.libraries.push.impl.notifications.fixtures.aNotifiableMessageEvent
|
||||||
|
import io.element.android.libraries.push.impl.notifications.model.NotifiableEvent
|
||||||
|
import io.element.android.services.appnavstate.api.AppNavigationState
|
||||||
|
import io.element.android.services.appnavstate.api.AppNavigationStateService
|
||||||
|
import io.element.android.services.appnavstate.api.NavigationState
|
||||||
|
import io.element.android.services.appnavstate.test.FakeAppNavigationStateService
|
||||||
|
import io.element.android.services.appnavstate.test.aNavigationState
|
||||||
|
import io.element.android.tests.testutils.testCoroutineDispatchers
|
||||||
|
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||||
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
|
import kotlinx.coroutines.test.TestScope
|
||||||
|
import kotlinx.coroutines.test.runCurrent
|
||||||
|
import kotlinx.coroutines.test.runTest
|
||||||
|
import org.junit.Test
|
||||||
|
import org.junit.runner.RunWith
|
||||||
|
import org.robolectric.RobolectricTestRunner
|
||||||
|
import org.robolectric.RuntimeEnvironment
|
||||||
|
|
||||||
|
@OptIn(ExperimentalCoroutinesApi::class)
|
||||||
|
@RunWith(RobolectricTestRunner::class)
|
||||||
|
class DefaultNotificationDrawerManagerTest {
|
||||||
|
@Test
|
||||||
|
fun `clearAllEvents should have no effect when queue is empty`() = runTest {
|
||||||
|
val defaultNotificationDrawerManager = createDefaultNotificationDrawerManager()
|
||||||
|
defaultNotificationDrawerManager.clearAllEvents(A_SESSION_ID)
|
||||||
|
defaultNotificationDrawerManager.destroy()
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `cover all APIs`() = runTest {
|
||||||
|
// For now just call all the API. Later, add more valuable tests.
|
||||||
|
val defaultNotificationDrawerManager = createDefaultNotificationDrawerManager()
|
||||||
|
defaultNotificationDrawerManager.notificationStyleChanged()
|
||||||
|
defaultNotificationDrawerManager.clearAllMessagesEvents(A_SESSION_ID, doRender = true)
|
||||||
|
defaultNotificationDrawerManager.clearAllMessagesEvents(A_SESSION_ID, doRender = false)
|
||||||
|
defaultNotificationDrawerManager.clearEvent(A_SESSION_ID, AN_EVENT_ID, doRender = true)
|
||||||
|
defaultNotificationDrawerManager.clearEvent(A_SESSION_ID, AN_EVENT_ID, doRender = false)
|
||||||
|
defaultNotificationDrawerManager.clearMessagesForRoom(A_SESSION_ID, A_ROOM_ID, doRender = true)
|
||||||
|
defaultNotificationDrawerManager.clearMessagesForRoom(A_SESSION_ID, A_ROOM_ID, doRender = false)
|
||||||
|
defaultNotificationDrawerManager.clearMembershipNotificationForSession(A_SESSION_ID)
|
||||||
|
defaultNotificationDrawerManager.clearMembershipNotificationForRoom(A_SESSION_ID, A_ROOM_ID, doRender = true)
|
||||||
|
defaultNotificationDrawerManager.clearMembershipNotificationForRoom(A_SESSION_ID, A_ROOM_ID, doRender = false)
|
||||||
|
defaultNotificationDrawerManager.onNotifiableEventReceived(aNotifiableMessageEvent())
|
||||||
|
// Add the same Event again (will be ignored)
|
||||||
|
defaultNotificationDrawerManager.onNotifiableEventReceived(aNotifiableMessageEvent())
|
||||||
|
defaultNotificationDrawerManager.destroy()
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `react to applicationStateChange`() = runTest {
|
||||||
|
// For now just call all the API. Later, add more valuable tests.
|
||||||
|
val appNavigationStateFlow: MutableStateFlow<AppNavigationState> = MutableStateFlow(
|
||||||
|
AppNavigationState(
|
||||||
|
navigationState = NavigationState.Root,
|
||||||
|
isInForeground = true,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
val appNavigationStateService = FakeAppNavigationStateService(appNavigationState = appNavigationStateFlow)
|
||||||
|
val defaultNotificationDrawerManager = createDefaultNotificationDrawerManager(
|
||||||
|
appNavigationStateService = appNavigationStateService
|
||||||
|
)
|
||||||
|
appNavigationStateFlow.emit(AppNavigationState(aNavigationState(), isInForeground = true))
|
||||||
|
runCurrent()
|
||||||
|
appNavigationStateFlow.emit(AppNavigationState(aNavigationState(A_SESSION_ID), isInForeground = true))
|
||||||
|
runCurrent()
|
||||||
|
appNavigationStateFlow.emit(AppNavigationState(aNavigationState(A_SESSION_ID, A_SPACE_ID), isInForeground = true))
|
||||||
|
runCurrent()
|
||||||
|
appNavigationStateFlow.emit(AppNavigationState(aNavigationState(A_SESSION_ID, A_SPACE_ID, A_ROOM_ID), isInForeground = true))
|
||||||
|
runCurrent()
|
||||||
|
appNavigationStateFlow.emit(AppNavigationState(aNavigationState(A_SESSION_ID, A_SPACE_ID, A_ROOM_ID, A_THREAD_ID), isInForeground = true))
|
||||||
|
runCurrent()
|
||||||
|
// Like a user sign out
|
||||||
|
appNavigationStateFlow.emit(AppNavigationState(aNavigationState(), isInForeground = true))
|
||||||
|
runCurrent()
|
||||||
|
defaultNotificationDrawerManager.destroy()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun TestScope.createDefaultNotificationDrawerManager(
|
||||||
|
appNavigationStateService: AppNavigationStateService = FakeAppNavigationStateService(),
|
||||||
|
initialData: List<NotifiableEvent> = emptyList()
|
||||||
|
): DefaultNotificationDrawerManager {
|
||||||
|
val context = RuntimeEnvironment.getApplication()
|
||||||
|
return DefaultNotificationDrawerManager(
|
||||||
|
notifiableEventProcessor = NotifiableEventProcessor(
|
||||||
|
outdatedDetector = OutdatedEventDetector(),
|
||||||
|
appNavigationStateService = appNavigationStateService
|
||||||
|
),
|
||||||
|
notificationRenderer = NotificationRenderer(
|
||||||
|
NotificationIdProvider(),
|
||||||
|
NotificationDisplayer(context),
|
||||||
|
NotificationFactory(
|
||||||
|
FakeAndroidNotificationFactory().instance,
|
||||||
|
FakeRoomGroupMessageCreator().instance,
|
||||||
|
FakeSummaryGroupMessageCreator().instance,
|
||||||
|
)
|
||||||
|
),
|
||||||
|
notificationEventPersistence = InMemoryNotificationEventPersistence(initialData = initialData),
|
||||||
|
filteredEventDetector = FilteredEventDetector(),
|
||||||
|
appNavigationStateService = appNavigationStateService,
|
||||||
|
coroutineScope = this,
|
||||||
|
dispatchers = testCoroutineDispatchers(useUnconfinedTestDispatcher = true),
|
||||||
|
buildMeta = aBuildMeta(),
|
||||||
|
matrixClientProvider = FakeMatrixClientProvider(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,33 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2023 New Vector Ltd
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package io.element.android.libraries.push.impl.notifications
|
||||||
|
|
||||||
|
import io.element.android.libraries.push.impl.notifications.model.NotifiableEvent
|
||||||
|
|
||||||
|
class InMemoryNotificationEventPersistence(
|
||||||
|
initialData: List<NotifiableEvent> = emptyList()
|
||||||
|
) : NotificationEventPersistence {
|
||||||
|
private var data: List<NotifiableEvent> = initialData
|
||||||
|
|
||||||
|
override fun loadEvents(factory: (List<NotifiableEvent>) -> NotificationEventQueue): NotificationEventQueue {
|
||||||
|
return factory(data)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun persistEvents(queuedEvents: NotificationEventQueue) {
|
||||||
|
data = queuedEvents.rawEvents()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -20,23 +20,19 @@ import io.element.android.libraries.matrix.api.core.RoomId
|
||||||
import io.element.android.libraries.matrix.api.core.SessionId
|
import io.element.android.libraries.matrix.api.core.SessionId
|
||||||
import io.element.android.libraries.matrix.api.core.SpaceId
|
import io.element.android.libraries.matrix.api.core.SpaceId
|
||||||
import io.element.android.libraries.matrix.api.core.ThreadId
|
import io.element.android.libraries.matrix.api.core.ThreadId
|
||||||
import io.element.android.services.appnavstate.api.NavigationState
|
|
||||||
import io.element.android.services.appnavstate.api.AppNavigationStateService
|
|
||||||
import io.element.android.services.appnavstate.api.AppNavigationState
|
import io.element.android.services.appnavstate.api.AppNavigationState
|
||||||
|
import io.element.android.services.appnavstate.api.AppNavigationStateService
|
||||||
|
import io.element.android.services.appnavstate.api.NavigationState
|
||||||
import kotlinx.coroutines.flow.MutableStateFlow
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
import kotlinx.coroutines.flow.StateFlow
|
|
||||||
|
|
||||||
class FakeAppNavigationStateService(
|
class FakeAppNavigationStateService(
|
||||||
private val fakeAppNavigationState: MutableStateFlow<AppNavigationState> = MutableStateFlow(
|
override val appNavigationState: MutableStateFlow<AppNavigationState> = MutableStateFlow(
|
||||||
AppNavigationState(
|
AppNavigationState(
|
||||||
navigationState = NavigationState.Root,
|
navigationState = NavigationState.Root,
|
||||||
isInForeground = true,
|
isInForeground = true,
|
||||||
)
|
)
|
||||||
),
|
),
|
||||||
) : AppNavigationStateService {
|
) : AppNavigationStateService {
|
||||||
|
|
||||||
override val appNavigationState: StateFlow<AppNavigationState> = fakeAppNavigationState
|
|
||||||
|
|
||||||
override fun onNavigateToSession(owner: String, sessionId: SessionId) = Unit
|
override fun onNavigateToSession(owner: String, sessionId: SessionId) = Unit
|
||||||
override fun onLeavingSession(owner: String) = Unit
|
override fun onLeavingSession(owner: String) = Unit
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue