Update related account data event on notification method change

This commit is contained in:
Maxime NATUREL 2022-11-23 14:45:37 +01:00
parent e99dc1d163
commit 637961bbb1
12 changed files with 207 additions and 85 deletions

View File

@ -22,5 +22,5 @@ import com.squareup.moshi.JsonClass
@JsonClass(generateAdapter = true) @JsonClass(generateAdapter = true)
data class LocalNotificationSettingsContent( data class LocalNotificationSettingsContent(
@Json(name = "is_silenced") @Json(name = "is_silenced")
val isSilenced: Boolean? = false val isSilenced: Boolean?
) )

View File

@ -1,39 +0,0 @@
/*
* Copyright (c) 2022 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 im.vector.app.core.notification
import im.vector.app.features.session.coroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import org.matrix.android.sdk.api.session.Session
import javax.inject.Inject
import javax.inject.Singleton
@Singleton
class EnableNotificationsSettingUpdater @Inject constructor(
private val updateEnableNotificationsSettingOnChangeUseCase: UpdateEnableNotificationsSettingOnChangeUseCase,
) {
private var job: Job? = null
fun onSessionsStarted(session: Session) {
job?.cancel()
job = session.coroutineScope.launch {
updateEnableNotificationsSettingOnChangeUseCase.execute(session)
}
}
}

View File

@ -0,0 +1,74 @@
/*
* Copyright (c) 2022 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 im.vector.app.core.notification
import android.content.SharedPreferences.OnSharedPreferenceChangeListener
import im.vector.app.features.session.coroutineScope
import im.vector.app.features.settings.VectorPreferences
import im.vector.app.features.settings.VectorPreferences.Companion.SETTINGS_FDROID_BACKGROUND_SYNC_MODE
import im.vector.app.features.settings.devices.v2.notification.UpdateNotificationSettingsAccountDataUseCase
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import org.matrix.android.sdk.api.session.Session
import javax.inject.Inject
import javax.inject.Singleton
/**
* Listen changes in Pusher or Account Data to update the local setting for notification toggle.
* Listen changes on background sync mode preference to update the corresponding Account Data event.
*/
@Singleton
class NotificationsSettingUpdater @Inject constructor(
private val updateEnableNotificationsSettingOnChangeUseCase: UpdateEnableNotificationsSettingOnChangeUseCase,
private val vectorPreferences: VectorPreferences,
private val updateNotificationSettingsAccountDataUseCase: UpdateNotificationSettingsAccountDataUseCase,
) {
private var job: Job? = null
private var prefChangeListener: OnSharedPreferenceChangeListener? = null
// TODO add unit tests
fun onSessionsStarted(session: Session) {
updateEnableNotificationsSettingOnChange(session)
updateNotificationSettingsAccountDataOnChange(session)
}
private fun updateEnableNotificationsSettingOnChange(session: Session) {
job?.cancel()
job = session.coroutineScope.launch {
updateEnableNotificationsSettingOnChangeUseCase.execute(session)
}
}
private fun updateNotificationSettingsAccountDataOnChange(session: Session) {
prefChangeListener?.let { vectorPreferences.unsubscribeToChanges(it) }
prefChangeListener = null
prefChangeListener = createPrefListener(session).also {
vectorPreferences.subscribeToChanges(it)
}
}
private fun createPrefListener(session: Session): OnSharedPreferenceChangeListener {
return OnSharedPreferenceChangeListener { _, key ->
session.coroutineScope.launch {
if (key == SETTINGS_FDROID_BACKGROUND_SYNC_MODE) {
updateNotificationSettingsAccountDataUseCase.execute(session)
}
}
}
}
}

View File

@ -19,7 +19,7 @@ package im.vector.app.core.session
import android.content.Context import android.content.Context
import dagger.hilt.android.qualifiers.ApplicationContext import dagger.hilt.android.qualifiers.ApplicationContext
import im.vector.app.core.extensions.startSyncing import im.vector.app.core.extensions.startSyncing
import im.vector.app.core.notification.EnableNotificationsSettingUpdater import im.vector.app.core.notification.NotificationsSettingUpdater
import im.vector.app.core.session.clientinfo.UpdateMatrixClientInfoUseCase import im.vector.app.core.session.clientinfo.UpdateMatrixClientInfoUseCase
import im.vector.app.features.call.webrtc.WebRtcCallManager import im.vector.app.features.call.webrtc.WebRtcCallManager
import im.vector.app.features.session.coroutineScope import im.vector.app.features.session.coroutineScope
@ -36,7 +36,7 @@ class ConfigureAndStartSessionUseCase @Inject constructor(
private val webRtcCallManager: WebRtcCallManager, private val webRtcCallManager: WebRtcCallManager,
private val updateMatrixClientInfoUseCase: UpdateMatrixClientInfoUseCase, private val updateMatrixClientInfoUseCase: UpdateMatrixClientInfoUseCase,
private val vectorPreferences: VectorPreferences, private val vectorPreferences: VectorPreferences,
private val enableNotificationsSettingUpdater: EnableNotificationsSettingUpdater, private val notificationsSettingUpdater: NotificationsSettingUpdater,
private val updateNotificationSettingsAccountDataUseCase: UpdateNotificationSettingsAccountDataUseCase, private val updateNotificationSettingsAccountDataUseCase: UpdateNotificationSettingsAccountDataUseCase,
) { ) {
@ -53,7 +53,7 @@ class ConfigureAndStartSessionUseCase @Inject constructor(
webRtcCallManager.checkForProtocolsSupportIfNeeded() webRtcCallManager.checkForProtocolsSupportIfNeeded()
updateMatrixClientInfoIfNeeded(session) updateMatrixClientInfoIfNeeded(session)
createNotificationSettingsAccountDataIfNeeded(session) createNotificationSettingsAccountDataIfNeeded(session)
enableNotificationsSettingUpdater.onSessionsStarted(session) notificationsSettingUpdater.onSessionsStarted(session)
} }
private fun updateMatrixClientInfoIfNeeded(session: Session) { private fun updateMatrixClientInfoIfNeeded(session: Session) {
@ -66,9 +66,7 @@ class ConfigureAndStartSessionUseCase @Inject constructor(
private fun createNotificationSettingsAccountDataIfNeeded(session: Session) { private fun createNotificationSettingsAccountDataIfNeeded(session: Session) {
session.coroutineScope.launch { session.coroutineScope.launch {
if (vectorPreferences.isBackgroundSyncEnabled()) {
updateNotificationSettingsAccountDataUseCase.execute(session) updateNotificationSettingsAccountDataUseCase.execute(session)
} }
} }
} }
}

View File

@ -24,15 +24,17 @@ import javax.inject.Inject
* Delete the content of any associated notification settings to the current session. * Delete the content of any associated notification settings to the current session.
*/ */
class DeleteNotificationSettingsAccountDataUseCase @Inject constructor( class DeleteNotificationSettingsAccountDataUseCase @Inject constructor(
private val getNotificationSettingsAccountDataUseCase: GetNotificationSettingsAccountDataUseCase,
private val setNotificationSettingsAccountDataUseCase: SetNotificationSettingsAccountDataUseCase, private val setNotificationSettingsAccountDataUseCase: SetNotificationSettingsAccountDataUseCase,
) { ) {
// TODO to be called when switching to push notifications method (check notification method setting)
suspend fun execute(session: Session) { suspend fun execute(session: Session) {
val deviceId = session.sessionParams.deviceId ?: return val deviceId = session.sessionParams.deviceId ?: return
if (getNotificationSettingsAccountDataUseCase.execute(session, deviceId)?.isSilenced != null) {
val emptyNotificationSettingsContent = LocalNotificationSettingsContent( val emptyNotificationSettingsContent = LocalNotificationSettingsContent(
isSilenced = null isSilenced = null
) )
setNotificationSettingsAccountDataUseCase.execute(session, deviceId, emptyNotificationSettingsContent) setNotificationSettingsAccountDataUseCase.execute(session, deviceId, emptyNotificationSettingsContent)
} }
} }
}

View File

@ -22,24 +22,37 @@ import org.matrix.android.sdk.api.session.Session
import javax.inject.Inject import javax.inject.Inject
/** /**
* Update the notification settings account data for the current session. * Update the notification settings account data for the current session depending on whether
* the background sync is enabled or not.
*/ */
class UpdateNotificationSettingsAccountDataUseCase @Inject constructor( class UpdateNotificationSettingsAccountDataUseCase @Inject constructor(
private val vectorPreferences: VectorPreferences, private val vectorPreferences: VectorPreferences,
private val getNotificationSettingsAccountDataUseCase: GetNotificationSettingsAccountDataUseCase, private val getNotificationSettingsAccountDataUseCase: GetNotificationSettingsAccountDataUseCase,
private val setNotificationSettingsAccountDataUseCase: SetNotificationSettingsAccountDataUseCase private val setNotificationSettingsAccountDataUseCase: SetNotificationSettingsAccountDataUseCase,
private val deleteNotificationSettingsAccountDataUseCase: DeleteNotificationSettingsAccountDataUseCase,
) { ) {
// TODO to be called when switching to background sync (in notification method setting)
suspend fun execute(session: Session) { suspend fun execute(session: Session) {
if (vectorPreferences.isBackgroundSyncEnabled()) {
setCurrentNotificationStatus(session)
} else {
deleteCurrentNotificationStatus(session)
}
}
private suspend fun setCurrentNotificationStatus(session: Session) {
val deviceId = session.sessionParams.deviceId ?: return val deviceId = session.sessionParams.deviceId ?: return
val isSilencedLocal = !vectorPreferences.areNotificationEnabledForDevice() val areNotificationsSilenced = !vectorPreferences.areNotificationEnabledForDevice()
val isSilencedRemote = getNotificationSettingsAccountDataUseCase.execute(session, deviceId)?.isSilenced val isSilencedAccountData = getNotificationSettingsAccountDataUseCase.execute(session, deviceId)?.isSilenced
if (isSilencedLocal != isSilencedRemote) { if (areNotificationsSilenced != isSilencedAccountData) {
val notificationSettingsContent = LocalNotificationSettingsContent( val notificationSettingsContent = LocalNotificationSettingsContent(
isSilenced = isSilencedLocal isSilenced = areNotificationsSilenced
) )
setNotificationSettingsAccountDataUseCase.execute(session, deviceId, notificationSettingsContent) setNotificationSettingsAccountDataUseCase.execute(session, deviceId, notificationSettingsContent)
} }
} }
private suspend fun deleteCurrentNotificationStatus(session: Session) {
deleteNotificationSettingsAccountDataUseCase.execute(session)
}
} }

View File

@ -22,7 +22,7 @@ import im.vector.app.features.session.coroutineScope
import im.vector.app.features.sync.SyncUtils import im.vector.app.features.sync.SyncUtils
import im.vector.app.features.settings.devices.v2.notification.UpdateNotificationSettingsAccountDataUseCase import im.vector.app.features.settings.devices.v2.notification.UpdateNotificationSettingsAccountDataUseCase
import im.vector.app.test.fakes.FakeContext import im.vector.app.test.fakes.FakeContext
import im.vector.app.test.fakes.FakeEnableNotificationsSettingUpdater import im.vector.app.test.fakes.FakeNotificationsSettingUpdater
import im.vector.app.test.fakes.FakeSession import im.vector.app.test.fakes.FakeSession
import im.vector.app.test.fakes.FakeVectorPreferences import im.vector.app.test.fakes.FakeVectorPreferences
import im.vector.app.test.fakes.FakeWebRtcCallManager import im.vector.app.test.fakes.FakeWebRtcCallManager
@ -47,7 +47,7 @@ class ConfigureAndStartSessionUseCaseTest {
private val fakeWebRtcCallManager = FakeWebRtcCallManager() private val fakeWebRtcCallManager = FakeWebRtcCallManager()
private val fakeUpdateMatrixClientInfoUseCase = mockk<UpdateMatrixClientInfoUseCase>() private val fakeUpdateMatrixClientInfoUseCase = mockk<UpdateMatrixClientInfoUseCase>()
private val fakeVectorPreferences = FakeVectorPreferences() private val fakeVectorPreferences = FakeVectorPreferences()
private val fakeEnableNotificationsSettingUpdater = FakeEnableNotificationsSettingUpdater() private val fakeNotificationsSettingUpdater = FakeNotificationsSettingUpdater()
private val fakeUpdateNotificationSettingsAccountDataUseCase = mockk<UpdateNotificationSettingsAccountDataUseCase>() private val fakeUpdateNotificationSettingsAccountDataUseCase = mockk<UpdateNotificationSettingsAccountDataUseCase>()
private val configureAndStartSessionUseCase = ConfigureAndStartSessionUseCase( private val configureAndStartSessionUseCase = ConfigureAndStartSessionUseCase(
@ -55,7 +55,7 @@ class ConfigureAndStartSessionUseCaseTest {
webRtcCallManager = fakeWebRtcCallManager.instance, webRtcCallManager = fakeWebRtcCallManager.instance,
updateMatrixClientInfoUseCase = fakeUpdateMatrixClientInfoUseCase, updateMatrixClientInfoUseCase = fakeUpdateMatrixClientInfoUseCase,
vectorPreferences = fakeVectorPreferences.instance, vectorPreferences = fakeVectorPreferences.instance,
enableNotificationsSettingUpdater = fakeEnableNotificationsSettingUpdater.instance, notificationsSettingUpdater = fakeNotificationsSettingUpdater.instance,
updateNotificationSettingsAccountDataUseCase = fakeUpdateNotificationSettingsAccountDataUseCase, updateNotificationSettingsAccountDataUseCase = fakeUpdateNotificationSettingsAccountDataUseCase,
) )
@ -71,7 +71,7 @@ class ConfigureAndStartSessionUseCaseTest {
} }
@Test @Test
fun `given start sync needed and enabled related preferences when execute then it should be configured properly`() = runTest { fun `given start sync needed and client info recording enabled when execute then it should be configured properly`() = runTest {
// Given // Given
val aSession = givenASession() val aSession = givenASession()
every { aSession.coroutineScope } returns this every { aSession.coroutineScope } returns this
@ -79,8 +79,7 @@ class ConfigureAndStartSessionUseCaseTest {
coJustRun { fakeUpdateMatrixClientInfoUseCase.execute(any()) } coJustRun { fakeUpdateMatrixClientInfoUseCase.execute(any()) }
coJustRun { fakeUpdateNotificationSettingsAccountDataUseCase.execute(any()) } coJustRun { fakeUpdateNotificationSettingsAccountDataUseCase.execute(any()) }
fakeVectorPreferences.givenIsClientInfoRecordingEnabled(isEnabled = true) fakeVectorPreferences.givenIsClientInfoRecordingEnabled(isEnabled = true)
fakeVectorPreferences.givenIsBackgroundSyncEnabled(isEnabled = true) fakeNotificationsSettingUpdater.givenOnSessionsStarted(aSession)
fakeEnableNotificationsSettingUpdater.givenOnSessionsStarted(aSession)
// When // When
configureAndStartSessionUseCase.execute(aSession, startSyncing = true) configureAndStartSessionUseCase.execute(aSession, startSyncing = true)
@ -98,14 +97,14 @@ class ConfigureAndStartSessionUseCaseTest {
} }
@Test @Test
fun `given start sync needed and disabled related preferences when execute then it should be configured properly`() = runTest { fun `given start sync needed and client info recording disabled when execute then it should be configured properly`() = runTest {
// Given // Given
val aSession = givenASession() val aSession = givenASession()
every { aSession.coroutineScope } returns this every { aSession.coroutineScope } returns this
fakeWebRtcCallManager.givenCheckForProtocolsSupportIfNeededSucceeds() fakeWebRtcCallManager.givenCheckForProtocolsSupportIfNeededSucceeds()
coJustRun { fakeUpdateNotificationSettingsAccountDataUseCase.execute(any()) }
fakeVectorPreferences.givenIsClientInfoRecordingEnabled(isEnabled = false) fakeVectorPreferences.givenIsClientInfoRecordingEnabled(isEnabled = false)
fakeVectorPreferences.givenIsBackgroundSyncEnabled(isEnabled = false) fakeNotificationsSettingUpdater.givenOnSessionsStarted(aSession)
fakeEnableNotificationsSettingUpdater.givenOnSessionsStarted(aSession)
// When // When
configureAndStartSessionUseCase.execute(aSession, startSyncing = true) configureAndStartSessionUseCase.execute(aSession, startSyncing = true)
@ -118,6 +117,8 @@ class ConfigureAndStartSessionUseCaseTest {
fakeWebRtcCallManager.verifyCheckForProtocolsSupportIfNeeded() fakeWebRtcCallManager.verifyCheckForProtocolsSupportIfNeeded()
coVerify(inverse = true) { coVerify(inverse = true) {
fakeUpdateMatrixClientInfoUseCase.execute(aSession) fakeUpdateMatrixClientInfoUseCase.execute(aSession)
}
coVerify {
fakeUpdateNotificationSettingsAccountDataUseCase.execute(aSession) fakeUpdateNotificationSettingsAccountDataUseCase.execute(aSession)
} }
} }
@ -125,27 +126,26 @@ class ConfigureAndStartSessionUseCaseTest {
@Test @Test
fun `given a session and no start sync needed when execute then it should be configured properly`() = runTest { fun `given a session and no start sync needed when execute then it should be configured properly`() = runTest {
// Given // Given
val fakeSession = givenASession() val aSession = givenASession()
every { fakeSession.coroutineScope } returns this every { aSession.coroutineScope } returns this
fakeWebRtcCallManager.givenCheckForProtocolsSupportIfNeededSucceeds() fakeWebRtcCallManager.givenCheckForProtocolsSupportIfNeededSucceeds()
coJustRun { fakeUpdateMatrixClientInfoUseCase.execute(any()) } coJustRun { fakeUpdateMatrixClientInfoUseCase.execute(any()) }
coJustRun { fakeUpdateNotificationSettingsAccountDataUseCase.execute(any()) } coJustRun { fakeUpdateNotificationSettingsAccountDataUseCase.execute(any()) }
fakeVectorPreferences.givenIsClientInfoRecordingEnabled(isEnabled = true) fakeVectorPreferences.givenIsClientInfoRecordingEnabled(isEnabled = true)
fakeVectorPreferences.givenIsBackgroundSyncEnabled(isEnabled = true) fakeNotificationsSettingUpdater.givenOnSessionsStarted(aSession)
fakeEnableNotificationsSettingUpdater.givenOnSessionsStarted(fakeSession)
// When // When
configureAndStartSessionUseCase.execute(fakeSession, startSyncing = false) configureAndStartSessionUseCase.execute(aSession, startSyncing = false)
advanceUntilIdle() advanceUntilIdle()
// Then // Then
verify(inverse = true) { fakeSession.startSyncing(fakeContext.instance) } verify(inverse = true) { aSession.startSyncing(fakeContext.instance) }
fakeSession.fakeFilterService.verifySetSyncFilter(SyncUtils.getSyncFilterBuilder()) aSession.fakeFilterService.verifySetSyncFilter(SyncUtils.getSyncFilterBuilder())
fakeSession.fakePushersService.verifyRefreshPushers() aSession.fakePushersService.verifyRefreshPushers()
fakeWebRtcCallManager.verifyCheckForProtocolsSupportIfNeeded() fakeWebRtcCallManager.verifyCheckForProtocolsSupportIfNeeded()
coVerify { coVerify {
fakeUpdateMatrixClientInfoUseCase.execute(fakeSession) fakeUpdateMatrixClientInfoUseCase.execute(aSession)
fakeUpdateNotificationSettingsAccountDataUseCase.execute(fakeSession) fakeUpdateNotificationSettingsAccountDataUseCase.execute(aSession)
} }
} }

View File

@ -19,7 +19,9 @@ package im.vector.app.features.settings.devices.v2.notification
import im.vector.app.test.fakes.FakeSession import im.vector.app.test.fakes.FakeSession
import io.mockk.coJustRun import io.mockk.coJustRun
import io.mockk.coVerify import io.mockk.coVerify
import io.mockk.every
import io.mockk.mockk import io.mockk.mockk
import io.mockk.verify
import kotlinx.coroutines.test.runTest import kotlinx.coroutines.test.runTest
import org.junit.Test import org.junit.Test
import org.matrix.android.sdk.api.account.LocalNotificationSettingsContent import org.matrix.android.sdk.api.account.LocalNotificationSettingsContent
@ -27,17 +29,22 @@ import org.matrix.android.sdk.api.account.LocalNotificationSettingsContent
class DeleteNotificationSettingsAccountDataUseCaseTest { class DeleteNotificationSettingsAccountDataUseCaseTest {
private val fakeSetNotificationSettingsAccountDataUseCase = mockk<SetNotificationSettingsAccountDataUseCase>() private val fakeSetNotificationSettingsAccountDataUseCase = mockk<SetNotificationSettingsAccountDataUseCase>()
private val fakeGetNotificationSettingsAccountDataUseCase = mockk<GetNotificationSettingsAccountDataUseCase>()
private val deleteNotificationSettingsAccountDataUseCase = DeleteNotificationSettingsAccountDataUseCase( private val deleteNotificationSettingsAccountDataUseCase = DeleteNotificationSettingsAccountDataUseCase(
setNotificationSettingsAccountDataUseCase = fakeSetNotificationSettingsAccountDataUseCase, setNotificationSettingsAccountDataUseCase = fakeSetNotificationSettingsAccountDataUseCase,
getNotificationSettingsAccountDataUseCase = fakeGetNotificationSettingsAccountDataUseCase,
) )
@Test @Test
fun `given a device id when execute then empty content is set for the account data`() = runTest { fun `given a device id and existing account data content when execute then empty content is set for the account data`() = runTest {
// Given // Given
val aDeviceId = "device-id" val aDeviceId = "device-id"
val aSession = FakeSession() val aSession = FakeSession()
aSession.givenSessionId(aDeviceId) aSession.givenSessionId(aDeviceId)
every { fakeGetNotificationSettingsAccountDataUseCase.execute(any(), any()) } returns LocalNotificationSettingsContent(
isSilenced = true,
)
coJustRun { fakeSetNotificationSettingsAccountDataUseCase.execute(any(), any(), any()) } coJustRun { fakeSetNotificationSettingsAccountDataUseCase.execute(any(), any(), any()) }
val expectedContent = LocalNotificationSettingsContent( val expectedContent = LocalNotificationSettingsContent(
isSilenced = null isSilenced = null
@ -47,6 +54,25 @@ class DeleteNotificationSettingsAccountDataUseCaseTest {
deleteNotificationSettingsAccountDataUseCase.execute(aSession) deleteNotificationSettingsAccountDataUseCase.execute(aSession)
// Then // Then
verify { fakeGetNotificationSettingsAccountDataUseCase.execute(aSession, aDeviceId) }
coVerify { fakeSetNotificationSettingsAccountDataUseCase.execute(aSession, aDeviceId, expectedContent) } coVerify { fakeSetNotificationSettingsAccountDataUseCase.execute(aSession, aDeviceId, expectedContent) }
} }
@Test
fun `given a device id and empty existing account data content when execute then nothing is done`() = runTest {
// Given
val aDeviceId = "device-id"
val aSession = FakeSession()
aSession.givenSessionId(aDeviceId)
every { fakeGetNotificationSettingsAccountDataUseCase.execute(any(), any()) } returns LocalNotificationSettingsContent(
isSilenced = null,
)
// When
deleteNotificationSettingsAccountDataUseCase.execute(aSession)
// Then
verify { fakeGetNotificationSettingsAccountDataUseCase.execute(aSession, aDeviceId) }
coVerify(inverse = true) { fakeSetNotificationSettingsAccountDataUseCase.execute(aSession, aDeviceId, any()) }
}
} }

View File

@ -32,7 +32,27 @@ class GetNotificationSettingsAccountDataUseCaseTest {
// Given // Given
val aDeviceId = "device-id" val aDeviceId = "device-id"
val aSession = FakeSession() val aSession = FakeSession()
val expectedContent = LocalNotificationSettingsContent() val expectedContent = LocalNotificationSettingsContent(isSilenced = true)
aSession
.accountDataService()
.givenGetUserAccountDataEventReturns(
type = UserAccountDataTypes.TYPE_LOCAL_NOTIFICATION_SETTINGS + aDeviceId,
content = expectedContent.toContent(),
)
// When
val result = getNotificationSettingsAccountDataUseCase.execute(aSession, aDeviceId)
// Then
result shouldBeEqualTo expectedContent
}
@Test
fun `given a device id and empty content when execute then retrieve the account data event corresponding to this id if any`() {
// Given
val aDeviceId = "device-id"
val aSession = FakeSession()
val expectedContent = LocalNotificationSettingsContent(isSilenced = null)
aSession aSession
.accountDataService() .accountDataService()
.givenGetUserAccountDataEventReturns( .givenGetUserAccountDataEventReturns(

View File

@ -31,7 +31,7 @@ class SetNotificationSettingsAccountDataUseCaseTest {
fun `given a content when execute then update local notification settings with this content`() = runTest { fun `given a content when execute then update local notification settings with this content`() = runTest {
// Given // Given
val sessionId = "a_session_id" val sessionId = "a_session_id"
val localNotificationSettingsContent = LocalNotificationSettingsContent() val localNotificationSettingsContent = LocalNotificationSettingsContent(isSilenced = true)
val fakeSession = FakeSession() val fakeSession = FakeSession()
fakeSession.accountDataService().givenUpdateUserAccountDataEventSucceeds() fakeSession.accountDataService().givenUpdateUserAccountDataEventSucceeds()

View File

@ -32,15 +32,17 @@ class UpdateNotificationSettingsAccountDataUseCaseTest {
private val fakeVectorPreferences = FakeVectorPreferences() private val fakeVectorPreferences = FakeVectorPreferences()
private val fakeGetNotificationSettingsAccountDataUseCase = mockk<GetNotificationSettingsAccountDataUseCase>() private val fakeGetNotificationSettingsAccountDataUseCase = mockk<GetNotificationSettingsAccountDataUseCase>()
private val fakeSetNotificationSettingsAccountDataUseCase = mockk<SetNotificationSettingsAccountDataUseCase>() private val fakeSetNotificationSettingsAccountDataUseCase = mockk<SetNotificationSettingsAccountDataUseCase>()
private val fakeDeleteNotificationSettingsAccountDataUseCase = mockk<DeleteNotificationSettingsAccountDataUseCase>()
private val updateNotificationSettingsAccountDataUseCase = UpdateNotificationSettingsAccountDataUseCase( private val updateNotificationSettingsAccountDataUseCase = UpdateNotificationSettingsAccountDataUseCase(
vectorPreferences = fakeVectorPreferences.instance, vectorPreferences = fakeVectorPreferences.instance,
getNotificationSettingsAccountDataUseCase = fakeGetNotificationSettingsAccountDataUseCase, getNotificationSettingsAccountDataUseCase = fakeGetNotificationSettingsAccountDataUseCase,
setNotificationSettingsAccountDataUseCase = fakeSetNotificationSettingsAccountDataUseCase, setNotificationSettingsAccountDataUseCase = fakeSetNotificationSettingsAccountDataUseCase,
deleteNotificationSettingsAccountDataUseCase = fakeDeleteNotificationSettingsAccountDataUseCase,
) )
@Test @Test
fun `given a device id and a different local setting compared to remote when execute then content is updated`() = runTest { fun `given back sync enabled, a device id and a different local setting compared to remote when execute then content is updated`() = runTest {
// Given // Given
val aDeviceId = "device-id" val aDeviceId = "device-id"
val aSession = FakeSession() val aSession = FakeSession()
@ -48,6 +50,7 @@ class UpdateNotificationSettingsAccountDataUseCaseTest {
coJustRun { fakeSetNotificationSettingsAccountDataUseCase.execute(any(), any(), any()) } coJustRun { fakeSetNotificationSettingsAccountDataUseCase.execute(any(), any(), any()) }
val areNotificationsEnabled = true val areNotificationsEnabled = true
fakeVectorPreferences.givenAreNotificationEnabled(areNotificationsEnabled) fakeVectorPreferences.givenAreNotificationEnabled(areNotificationsEnabled)
fakeVectorPreferences.givenIsBackgroundSyncEnabled(true)
every { fakeGetNotificationSettingsAccountDataUseCase.execute(any(), any()) } returns every { fakeGetNotificationSettingsAccountDataUseCase.execute(any(), any()) } returns
LocalNotificationSettingsContent( LocalNotificationSettingsContent(
isSilenced = null isSilenced = null
@ -61,14 +64,16 @@ class UpdateNotificationSettingsAccountDataUseCaseTest {
// Then // Then
verify { verify {
fakeVectorPreferences.instance.isBackgroundSyncEnabled()
fakeVectorPreferences.instance.areNotificationEnabledForDevice() fakeVectorPreferences.instance.areNotificationEnabledForDevice()
fakeGetNotificationSettingsAccountDataUseCase.execute(aSession, aDeviceId) fakeGetNotificationSettingsAccountDataUseCase.execute(aSession, aDeviceId)
} }
coVerify(inverse = true) { fakeDeleteNotificationSettingsAccountDataUseCase.execute(aSession) }
coVerify { fakeSetNotificationSettingsAccountDataUseCase.execute(aSession, aDeviceId, expectedContent) } coVerify { fakeSetNotificationSettingsAccountDataUseCase.execute(aSession, aDeviceId, expectedContent) }
} }
@Test @Test
fun `given a device id and a same local setting compared to remote when execute then content is not updated`() = runTest { fun `given back sync enabled, a device id and a same local setting compared to remote when execute then content is not updated`() = runTest {
// Given // Given
val aDeviceId = "device-id" val aDeviceId = "device-id"
val aSession = FakeSession() val aSession = FakeSession()
@ -76,6 +81,7 @@ class UpdateNotificationSettingsAccountDataUseCaseTest {
coJustRun { fakeSetNotificationSettingsAccountDataUseCase.execute(any(), any(), any()) } coJustRun { fakeSetNotificationSettingsAccountDataUseCase.execute(any(), any(), any()) }
val areNotificationsEnabled = true val areNotificationsEnabled = true
fakeVectorPreferences.givenAreNotificationEnabled(areNotificationsEnabled) fakeVectorPreferences.givenAreNotificationEnabled(areNotificationsEnabled)
fakeVectorPreferences.givenIsBackgroundSyncEnabled(true)
every { fakeGetNotificationSettingsAccountDataUseCase.execute(any(), any()) } returns every { fakeGetNotificationSettingsAccountDataUseCase.execute(any(), any()) } returns
LocalNotificationSettingsContent( LocalNotificationSettingsContent(
isSilenced = false isSilenced = false
@ -89,9 +95,31 @@ class UpdateNotificationSettingsAccountDataUseCaseTest {
// Then // Then
verify { verify {
fakeVectorPreferences.instance.isBackgroundSyncEnabled()
fakeVectorPreferences.instance.areNotificationEnabledForDevice() fakeVectorPreferences.instance.areNotificationEnabledForDevice()
fakeGetNotificationSettingsAccountDataUseCase.execute(aSession, aDeviceId) fakeGetNotificationSettingsAccountDataUseCase.execute(aSession, aDeviceId)
} }
coVerify(inverse = true) { fakeDeleteNotificationSettingsAccountDataUseCase.execute(aSession) }
coVerify(inverse = true) { fakeSetNotificationSettingsAccountDataUseCase.execute(aSession, aDeviceId, expectedContent) } coVerify(inverse = true) { fakeSetNotificationSettingsAccountDataUseCase.execute(aSession, aDeviceId, expectedContent) }
} }
@Test
fun `given back sync disabled and a device id when execute then content is deleted`() = runTest {
// Given
val aDeviceId = "device-id"
val aSession = FakeSession()
aSession.givenSessionId(aDeviceId)
coJustRun { fakeDeleteNotificationSettingsAccountDataUseCase.execute(any()) }
fakeVectorPreferences.givenIsBackgroundSyncEnabled(false)
// When
updateNotificationSettingsAccountDataUseCase.execute(aSession)
// Then
verify {
fakeVectorPreferences.instance.isBackgroundSyncEnabled()
}
coVerify { fakeDeleteNotificationSettingsAccountDataUseCase.execute(aSession) }
coVerify(inverse = true) { fakeSetNotificationSettingsAccountDataUseCase.execute(aSession, aDeviceId, any()) }
}
} }

View File

@ -16,14 +16,14 @@
package im.vector.app.test.fakes package im.vector.app.test.fakes
import im.vector.app.core.notification.EnableNotificationsSettingUpdater import im.vector.app.core.notification.NotificationsSettingUpdater
import io.mockk.justRun import io.mockk.justRun
import io.mockk.mockk import io.mockk.mockk
import org.matrix.android.sdk.api.session.Session import org.matrix.android.sdk.api.session.Session
class FakeEnableNotificationsSettingUpdater { class FakeNotificationsSettingUpdater {
val instance = mockk<EnableNotificationsSettingUpdater>() val instance = mockk<NotificationsSettingUpdater>()
fun givenOnSessionsStarted(session: Session) { fun givenOnSessionsStarted(session: Session) {
justRun { instance.onSessionsStarted(session) } justRun { instance.onSessionsStarted(session) }