Some refactorings + update unit tests

This commit is contained in:
Maxime NATUREL 2022-11-18 15:19:34 +01:00
parent 7c51174d7e
commit ab6a6b53c8
20 changed files with 300 additions and 154 deletions

View file

@ -25,6 +25,7 @@ import im.vector.app.features.call.webrtc.WebRtcCallManager
import im.vector.app.features.session.coroutineScope
import im.vector.app.features.settings.VectorPreferences
import im.vector.app.features.sync.SyncUtils
import im.vector.app.features.settings.devices.v2.notification.UpdateNotificationSettingsAccountDataUseCase
import kotlinx.coroutines.launch
import org.matrix.android.sdk.api.session.Session
import timber.log.Timber
@ -36,6 +37,7 @@ class ConfigureAndStartSessionUseCase @Inject constructor(
private val updateMatrixClientInfoUseCase: UpdateMatrixClientInfoUseCase,
private val vectorPreferences: VectorPreferences,
private val enableNotificationsSettingUpdater: EnableNotificationsSettingUpdater,
private val updateNotificationSettingsAccountDataUseCase: UpdateNotificationSettingsAccountDataUseCase,
) {
fun execute(session: Session, startSyncing: Boolean = true) {
@ -49,11 +51,24 @@ class ConfigureAndStartSessionUseCase @Inject constructor(
}
session.pushersService().refreshPushers()
webRtcCallManager.checkForProtocolsSupportIfNeeded()
updateMatrixClientInfoIfNeeded(session)
createNotificationSettingsAccountDataIfNeeded(session)
enableNotificationsSettingUpdater.onSessionsStarted(session)
}
private fun updateMatrixClientInfoIfNeeded(session: Session) {
session.coroutineScope.launch {
if (vectorPreferences.isClientInfoRecordingEnabled()) {
updateMatrixClientInfoUseCase.execute(session)
}
}
enableNotificationsSettingUpdater.onSessionsStarted(session)
}
private fun createNotificationSettingsAccountDataIfNeeded(session: Session) {
session.coroutineScope.launch {
if (vectorPreferences.isBackgroundSyncEnabled()) {
updateNotificationSettingsAccountDataUseCase.execute(session)
}
}
}
}

View file

@ -16,20 +16,14 @@
package im.vector.app.features.settings.devices.v2.notification
import org.matrix.android.sdk.api.account.LocalNotificationSettingsContent
import org.matrix.android.sdk.api.session.Session
import org.matrix.android.sdk.api.session.accountdata.UserAccountDataTypes
import org.matrix.android.sdk.api.session.events.model.toModel
import javax.inject.Inject
class CheckIfCanToggleNotificationsViaAccountDataUseCase @Inject constructor() {
class CheckIfCanToggleNotificationsViaAccountDataUseCase @Inject constructor(
private val getNotificationSettingsAccountDataUseCase: GetNotificationSettingsAccountDataUseCase,
) {
fun execute(session: Session, deviceId: String): Boolean {
return session
.accountDataService()
.getUserAccountDataEvent(UserAccountDataTypes.TYPE_LOCAL_NOTIFICATION_SETTINGS + deviceId)
?.content
.toModel<LocalNotificationSettingsContent>()
?.isSilenced != null
return getNotificationSettingsAccountDataUseCase.execute(session, deviceId)?.isSilenced != null
}
}

View file

@ -17,17 +17,22 @@
package im.vector.app.features.settings.devices.v2.notification
import org.matrix.android.sdk.api.account.LocalNotificationSettingsContent
import org.matrix.android.sdk.api.session.Session
import javax.inject.Inject
/**
* Delete the content of any associated notification settings to the current session.
*/
class DeleteNotificationSettingsAccountDataUseCase @Inject constructor(
private val setNotificationSettingsAccountDataUseCase: SetNotificationSettingsAccountDataUseCase,
) {
// TODO to be called when switching to push notifications method
suspend fun execute(deviceId: String) {
// TODO to be called when switching to push notifications method (check notification method setting)
suspend fun execute(session: Session) {
val deviceId = session.sessionParams.deviceId ?: return
val emptyNotificationSettingsContent = LocalNotificationSettingsContent(
isSilenced = null
)
setNotificationSettingsAccountDataUseCase.execute(deviceId, emptyNotificationSettingsContent)
setNotificationSettingsAccountDataUseCase.execute(session, deviceId, emptyNotificationSettingsContent)
}
}

View file

@ -0,0 +1,34 @@
/*
* 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.features.settings.devices.v2.notification
import org.matrix.android.sdk.api.account.LocalNotificationSettingsContent
import org.matrix.android.sdk.api.session.Session
import org.matrix.android.sdk.api.session.accountdata.UserAccountDataTypes
import org.matrix.android.sdk.api.session.events.model.toModel
import javax.inject.Inject
class GetNotificationSettingsAccountDataUseCase @Inject constructor() {
fun execute(session: Session, deviceId: String): LocalNotificationSettingsContent? {
return session
.accountDataService()
.getUserAccountDataEvent(UserAccountDataTypes.TYPE_LOCAL_NOTIFICATION_SETTINGS + deviceId)
?.content
.toModel<LocalNotificationSettingsContent>()
}
}

View file

@ -16,18 +16,15 @@
package im.vector.app.features.settings.devices.v2.notification
import im.vector.app.core.di.ActiveSessionHolder
import org.matrix.android.sdk.api.account.LocalNotificationSettingsContent
import org.matrix.android.sdk.api.session.Session
import org.matrix.android.sdk.api.session.accountdata.UserAccountDataTypes
import org.matrix.android.sdk.api.session.events.model.toContent
import javax.inject.Inject
class SetNotificationSettingsAccountDataUseCase @Inject constructor(
private val activeSessionHolder: ActiveSessionHolder,
) {
class SetNotificationSettingsAccountDataUseCase @Inject constructor() {
suspend fun execute(deviceId: String, localNotificationSettingsContent: LocalNotificationSettingsContent) {
val session = activeSessionHolder.getSafeActiveSession() ?: return
suspend fun execute(session: Session, deviceId: String, localNotificationSettingsContent: LocalNotificationSettingsContent) {
session.accountDataService().updateUserAccountData(
UserAccountDataTypes.TYPE_LOCAL_NOTIFICATION_SETTINGS + deviceId,
localNotificationSettingsContent.toContent(),

View file

@ -39,7 +39,7 @@ class ToggleNotificationUseCase @Inject constructor(
if (checkIfCanToggleNotificationsViaAccountDataUseCase.execute(session, deviceId)) {
val newNotificationSettingsContent = LocalNotificationSettingsContent(isSilenced = !enabled)
setNotificationSettingsAccountDataUseCase.execute(deviceId, newNotificationSettingsContent)
setNotificationSettingsAccountDataUseCase.execute(session, deviceId, newNotificationSettingsContent)
}
}
}

View file

@ -21,18 +21,25 @@ import org.matrix.android.sdk.api.account.LocalNotificationSettingsContent
import org.matrix.android.sdk.api.session.Session
import javax.inject.Inject
class CreateNotificationSettingsAccountDataUseCase @Inject constructor(
/**
* Update the notification settings account data for the current session.
*/
class UpdateNotificationSettingsAccountDataUseCase @Inject constructor(
private val vectorPreferences: VectorPreferences,
private val getNotificationSettingsAccountDataUseCase: GetNotificationSettingsAccountDataUseCase,
private val setNotificationSettingsAccountDataUseCase: SetNotificationSettingsAccountDataUseCase
) {
// TODO to be called on session start when background sync is enabled + when switching to background sync
// TODO to be called when switching to background sync (in notification method setting)
suspend fun execute(session: Session) {
val deviceId = session.sessionParams.deviceId ?: return
val isSilenced = !vectorPreferences.areNotificationEnabledForDevice()
val notificationSettingsContent = LocalNotificationSettingsContent(
isSilenced = isSilenced
)
setNotificationSettingsAccountDataUseCase.execute(deviceId, notificationSettingsContent)
val isSilencedLocal = !vectorPreferences.areNotificationEnabledForDevice()
val isSilencedRemote = getNotificationSettingsAccountDataUseCase.execute(session, deviceId)?.isSilenced
if (isSilencedLocal != isSilencedRemote) {
val notificationSettingsContent = LocalNotificationSettingsContent(
isSilenced = isSilencedLocal
)
setNotificationSettingsAccountDataUseCase.execute(session, deviceId, notificationSettingsContent)
}
}
}

View file

@ -20,6 +20,7 @@ import im.vector.app.core.extensions.startSyncing
import im.vector.app.core.session.clientinfo.UpdateMatrixClientInfoUseCase
import im.vector.app.features.session.coroutineScope
import im.vector.app.features.sync.SyncUtils
import im.vector.app.features.settings.devices.v2.notification.UpdateNotificationSettingsAccountDataUseCase
import im.vector.app.test.fakes.FakeContext
import im.vector.app.test.fakes.FakeEnableNotificationsSettingUpdater
import im.vector.app.test.fakes.FakeSession
@ -47,6 +48,7 @@ class ConfigureAndStartSessionUseCaseTest {
private val fakeUpdateMatrixClientInfoUseCase = mockk<UpdateMatrixClientInfoUseCase>()
private val fakeVectorPreferences = FakeVectorPreferences()
private val fakeEnableNotificationsSettingUpdater = FakeEnableNotificationsSettingUpdater()
private val fakeUpdateNotificationSettingsAccountDataUseCase = mockk<UpdateNotificationSettingsAccountDataUseCase>()
private val configureAndStartSessionUseCase = ConfigureAndStartSessionUseCase(
context = fakeContext.instance,
@ -54,6 +56,7 @@ class ConfigureAndStartSessionUseCaseTest {
updateMatrixClientInfoUseCase = fakeUpdateMatrixClientInfoUseCase,
vectorPreferences = fakeVectorPreferences.instance,
enableNotificationsSettingUpdater = fakeEnableNotificationsSettingUpdater.instance,
updateNotificationSettingsAccountDataUseCase = fakeUpdateNotificationSettingsAccountDataUseCase,
)
@Before
@ -68,47 +71,55 @@ class ConfigureAndStartSessionUseCaseTest {
}
@Test
fun `given start sync needed and client info recording enabled when execute then it should be configured properly`() = runTest {
fun `given start sync needed and enabled related preferences when execute then it should be configured properly`() = runTest {
// Given
val fakeSession = givenASession()
every { fakeSession.coroutineScope } returns this
val aSession = givenASession()
every { aSession.coroutineScope } returns this
fakeWebRtcCallManager.givenCheckForProtocolsSupportIfNeededSucceeds()
coJustRun { fakeUpdateMatrixClientInfoUseCase.execute(any()) }
coJustRun { fakeUpdateNotificationSettingsAccountDataUseCase.execute(any()) }
fakeVectorPreferences.givenIsClientInfoRecordingEnabled(isEnabled = true)
fakeEnableNotificationsSettingUpdater.givenOnSessionsStarted(fakeSession)
fakeVectorPreferences.givenIsBackgroundSyncEnabled(isEnabled = true)
fakeEnableNotificationsSettingUpdater.givenOnSessionsStarted(aSession)
// When
configureAndStartSessionUseCase.execute(fakeSession, startSyncing = true)
configureAndStartSessionUseCase.execute(aSession, startSyncing = true)
advanceUntilIdle()
// Then
verify { fakeSession.startSyncing(fakeContext.instance) }
fakeSession.fakeFilterService.verifySetSyncFilter(SyncUtils.getSyncFilterBuilder())
fakeSession.fakePushersService.verifyRefreshPushers()
verify { aSession.startSyncing(fakeContext.instance) }
aSession.fakeFilterService.verifySetSyncFilter(SyncUtils.getSyncFilterBuilder())
aSession.fakePushersService.verifyRefreshPushers()
fakeWebRtcCallManager.verifyCheckForProtocolsSupportIfNeeded()
coVerify { fakeUpdateMatrixClientInfoUseCase.execute(fakeSession) }
coVerify {
fakeUpdateMatrixClientInfoUseCase.execute(aSession)
fakeUpdateNotificationSettingsAccountDataUseCase.execute(aSession)
}
}
@Test
fun `given start sync needed and client info recording disabled when execute then it should be configured properly`() = runTest {
fun `given start sync needed and disabled related preferences when execute then it should be configured properly`() = runTest {
// Given
val fakeSession = givenASession()
every { fakeSession.coroutineScope } returns this
val aSession = givenASession()
every { aSession.coroutineScope } returns this
fakeWebRtcCallManager.givenCheckForProtocolsSupportIfNeededSucceeds()
coJustRun { fakeUpdateMatrixClientInfoUseCase.execute(any()) }
fakeVectorPreferences.givenIsClientInfoRecordingEnabled(isEnabled = false)
fakeEnableNotificationsSettingUpdater.givenOnSessionsStarted(fakeSession)
fakeVectorPreferences.givenIsBackgroundSyncEnabled(isEnabled = false)
fakeEnableNotificationsSettingUpdater.givenOnSessionsStarted(aSession)
// When
configureAndStartSessionUseCase.execute(fakeSession, startSyncing = true)
configureAndStartSessionUseCase.execute(aSession, startSyncing = true)
advanceUntilIdle()
// Then
verify { fakeSession.startSyncing(fakeContext.instance) }
fakeSession.fakeFilterService.verifySetSyncFilter(SyncUtils.getSyncFilterBuilder())
fakeSession.fakePushersService.verifyRefreshPushers()
verify { aSession.startSyncing(fakeContext.instance) }
aSession.fakeFilterService.verifySetSyncFilter(SyncUtils.getSyncFilterBuilder())
aSession.fakePushersService.verifyRefreshPushers()
fakeWebRtcCallManager.verifyCheckForProtocolsSupportIfNeeded()
coVerify(inverse = true) { fakeUpdateMatrixClientInfoUseCase.execute(fakeSession) }
coVerify(inverse = true) {
fakeUpdateMatrixClientInfoUseCase.execute(aSession)
fakeUpdateNotificationSettingsAccountDataUseCase.execute(aSession)
}
}
@Test
@ -118,7 +129,9 @@ class ConfigureAndStartSessionUseCaseTest {
every { fakeSession.coroutineScope } returns this
fakeWebRtcCallManager.givenCheckForProtocolsSupportIfNeededSucceeds()
coJustRun { fakeUpdateMatrixClientInfoUseCase.execute(any()) }
coJustRun { fakeUpdateNotificationSettingsAccountDataUseCase.execute(any()) }
fakeVectorPreferences.givenIsClientInfoRecordingEnabled(isEnabled = true)
fakeVectorPreferences.givenIsBackgroundSyncEnabled(isEnabled = true)
fakeEnableNotificationsSettingUpdater.givenOnSessionsStarted(fakeSession)
// When
@ -130,7 +143,10 @@ class ConfigureAndStartSessionUseCaseTest {
fakeSession.fakeFilterService.verifySetSyncFilter(SyncUtils.getSyncFilterBuilder())
fakeSession.fakePushersService.verifyRefreshPushers()
fakeWebRtcCallManager.verifyCheckForProtocolsSupportIfNeeded()
coVerify { fakeUpdateMatrixClientInfoUseCase.execute(fakeSession) }
coVerify {
fakeUpdateMatrixClientInfoUseCase.execute(fakeSession)
fakeUpdateNotificationSettingsAccountDataUseCase.execute(fakeSession)
}
}
private fun givenASession(): FakeSession {

View file

@ -17,31 +17,29 @@
package im.vector.app.features.settings.devices.v2.notification
import im.vector.app.test.fakes.FakeSession
import io.mockk.every
import io.mockk.mockk
import org.amshove.kluent.shouldBeEqualTo
import org.junit.Test
import org.matrix.android.sdk.api.account.LocalNotificationSettingsContent
import org.matrix.android.sdk.api.session.accountdata.UserAccountDataTypes
import org.matrix.android.sdk.api.session.events.model.toContent
private const val A_DEVICE_ID = "device-id"
class CheckIfCanToggleNotificationsViaAccountDataUseCaseTest {
private val fakeGetNotificationSettingsAccountDataUseCase = mockk<GetNotificationSettingsAccountDataUseCase>()
private val fakeSession = FakeSession()
private val checkIfCanToggleNotificationsViaAccountDataUseCase =
CheckIfCanToggleNotificationsViaAccountDataUseCase()
CheckIfCanToggleNotificationsViaAccountDataUseCase(
getNotificationSettingsAccountDataUseCase = fakeGetNotificationSettingsAccountDataUseCase,
)
@Test
fun `given current session and an account data with a content for the device id when execute then result is true`() {
// Given
fakeSession
.accountDataService()
.givenGetUserAccountDataEventReturns(
type = UserAccountDataTypes.TYPE_LOCAL_NOTIFICATION_SETTINGS + A_DEVICE_ID,
content = LocalNotificationSettingsContent(isSilenced = true).toContent(),
)
val content = LocalNotificationSettingsContent(isSilenced = true)
every { fakeGetNotificationSettingsAccountDataUseCase.execute(fakeSession, A_DEVICE_ID) } returns content
// When
val result = checkIfCanToggleNotificationsViaAccountDataUseCase.execute(fakeSession, A_DEVICE_ID)
@ -53,12 +51,8 @@ class CheckIfCanToggleNotificationsViaAccountDataUseCaseTest {
@Test
fun `given current session and an account data with empty content for the device id when execute then result is false`() {
// Given
fakeSession
.accountDataService()
.givenGetUserAccountDataEventReturns(
type = UserAccountDataTypes.TYPE_LOCAL_NOTIFICATION_SETTINGS + A_DEVICE_ID,
content = mockk(),
)
val content = LocalNotificationSettingsContent(isSilenced = null)
every { fakeGetNotificationSettingsAccountDataUseCase.execute(fakeSession, A_DEVICE_ID) } returns content
// When
val result = checkIfCanToggleNotificationsViaAccountDataUseCase.execute(fakeSession, A_DEVICE_ID)
@ -70,12 +64,8 @@ class CheckIfCanToggleNotificationsViaAccountDataUseCaseTest {
@Test
fun `given current session and NO account data for the device id when execute then result is false`() {
// Given
fakeSession
.accountDataService()
.givenGetUserAccountDataEventReturns(
type = UserAccountDataTypes.TYPE_LOCAL_NOTIFICATION_SETTINGS + A_DEVICE_ID,
content = null,
)
val content = null
every { fakeGetNotificationSettingsAccountDataUseCase.execute(fakeSession, A_DEVICE_ID) } returns content
// When
val result = checkIfCanToggleNotificationsViaAccountDataUseCase.execute(fakeSession, A_DEVICE_ID)

View file

@ -1,59 +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.features.settings.devices.v2.notification
import im.vector.app.test.fakes.FakeSession
import im.vector.app.test.fakes.FakeVectorPreferences
import io.mockk.coJustRun
import io.mockk.coVerify
import io.mockk.mockk
import io.mockk.verify
import kotlinx.coroutines.test.runTest
import org.junit.Test
import org.matrix.android.sdk.api.account.LocalNotificationSettingsContent
class CreateNotificationSettingsAccountDataUseCaseTest {
private val fakeVectorPreferences = FakeVectorPreferences()
private val fakeSetNotificationSettingsAccountDataUseCase = mockk<SetNotificationSettingsAccountDataUseCase>()
private val createNotificationSettingsAccountDataUseCase = CreateNotificationSettingsAccountDataUseCase(
vectorPreferences = fakeVectorPreferences.instance,
setNotificationSettingsAccountDataUseCase = fakeSetNotificationSettingsAccountDataUseCase,
)
@Test
fun `given a device id when execute then content with the current notification preference is set for the account data`() = runTest {
// Given
val aDeviceId = "device-id"
val session = FakeSession()
session.givenSessionId(aDeviceId)
coJustRun { fakeSetNotificationSettingsAccountDataUseCase.execute(any(), any()) }
val areNotificationsEnabled = true
fakeVectorPreferences.givenAreNotificationEnabled(areNotificationsEnabled)
val expectedContent = LocalNotificationSettingsContent(
isSilenced = !areNotificationsEnabled
)
// When
createNotificationSettingsAccountDataUseCase.execute(session)
// Then
verify { fakeVectorPreferences.instance.areNotificationEnabledForDevice() }
coVerify { fakeSetNotificationSettingsAccountDataUseCase.execute(aDeviceId, expectedContent) }
}
}

View file

@ -16,6 +16,7 @@
package im.vector.app.features.settings.devices.v2.notification
import im.vector.app.test.fakes.FakeSession
import io.mockk.coJustRun
import io.mockk.coVerify
import io.mockk.mockk
@ -35,15 +36,17 @@ class DeleteNotificationSettingsAccountDataUseCaseTest {
fun `given a device id when execute then empty content is set for the account data`() = runTest {
// Given
val aDeviceId = "device-id"
coJustRun { fakeSetNotificationSettingsAccountDataUseCase.execute(any(), any()) }
val aSession = FakeSession()
aSession.givenSessionId(aDeviceId)
coJustRun { fakeSetNotificationSettingsAccountDataUseCase.execute(any(), any(), any()) }
val expectedContent = LocalNotificationSettingsContent(
isSilenced = null
)
// When
deleteNotificationSettingsAccountDataUseCase.execute(aDeviceId)
deleteNotificationSettingsAccountDataUseCase.execute(aSession)
// Then
coVerify { fakeSetNotificationSettingsAccountDataUseCase.execute(aDeviceId, expectedContent) }
coVerify { fakeSetNotificationSettingsAccountDataUseCase.execute(aSession, aDeviceId, expectedContent) }
}
}

View file

@ -0,0 +1,49 @@
/*
* 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.features.settings.devices.v2.notification
import im.vector.app.test.fakes.FakeSession
import org.amshove.kluent.shouldBeEqualTo
import org.junit.Test
import org.matrix.android.sdk.api.account.LocalNotificationSettingsContent
import org.matrix.android.sdk.api.session.accountdata.UserAccountDataTypes
import org.matrix.android.sdk.api.session.events.model.toContent
class GetNotificationSettingsAccountDataUseCaseTest {
private val getNotificationSettingsAccountDataUseCase = GetNotificationSettingsAccountDataUseCase()
@Test
fun `given a device id 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()
aSession
.accountDataService()
.givenGetUserAccountDataEventReturns(
type = UserAccountDataTypes.TYPE_LOCAL_NOTIFICATION_SETTINGS + aDeviceId,
content = expectedContent.toContent(),
)
// When
val result = getNotificationSettingsAccountDataUseCase.execute(aSession, aDeviceId)
// Then
result shouldBeEqualTo expectedContent
}
}

View file

@ -53,8 +53,8 @@ class GetNotificationsStatusUseCaseTest {
private val getNotificationsStatusUseCase =
GetNotificationsStatusUseCase(
checkIfCanTogglePushNotificationsViaAccountDataUseCase = fakeCheckIfCanToggleNotificationsViaAccountDataUseCase,
canTogglePushNotificationsViaPusherUseCase = fakeCanToggleNotificationsViaPusherUseCase,
checkIfCanToggleNotificationsViaAccountDataUseCase = fakeCheckIfCanToggleNotificationsViaAccountDataUseCase,
canToggleNotificationsViaPusherUseCase = fakeCanToggleNotificationsViaPusherUseCase,
)
@Before

View file

@ -16,7 +16,7 @@
package im.vector.app.features.settings.devices.v2.notification
import im.vector.app.test.fakes.FakeActiveSessionHolder
import im.vector.app.test.fakes.FakeSession
import kotlinx.coroutines.test.runTest
import org.junit.Test
import org.matrix.android.sdk.api.account.LocalNotificationSettingsContent
@ -25,25 +25,21 @@ import org.matrix.android.sdk.api.session.events.model.toContent
class SetNotificationSettingsAccountDataUseCaseTest {
private val activeSessionHolder = FakeActiveSessionHolder()
private val setNotificationSettingsAccountDataUseCase = SetNotificationSettingsAccountDataUseCase(
activeSessionHolder = activeSessionHolder.instance,
)
private val setNotificationSettingsAccountDataUseCase = SetNotificationSettingsAccountDataUseCase()
@Test
fun `given a content when execute then update local notification settings with this content`() = runTest {
// Given
val sessionId = "a_session_id"
val localNotificationSettingsContent = LocalNotificationSettingsContent()
val fakeSession = activeSessionHolder.fakeSession
val fakeSession = FakeSession()
fakeSession.accountDataService().givenUpdateUserAccountDataEventSucceeds()
// When
setNotificationSettingsAccountDataUseCase.execute(sessionId, localNotificationSettingsContent)
setNotificationSettingsAccountDataUseCase.execute(fakeSession, sessionId, localNotificationSettingsContent)
// Then
activeSessionHolder.fakeSession.accountDataService().verifyUpdateUserAccountDataEventSucceeds(
fakeSession.accountDataService().verifyUpdateUserAccountDataEventSucceeds(
UserAccountDataTypes.TYPE_LOCAL_NOTIFICATION_SETTINGS + sessionId,
localNotificationSettingsContent.toContent(),
)

View file

@ -39,8 +39,8 @@ class ToggleNotificationUseCaseTest {
private val toggleNotificationUseCase =
ToggleNotificationUseCase(
activeSessionHolder = activeSessionHolder.instance,
checkIfCanTogglePushNotificationsViaPusherUseCase = fakeCheckIfCanToggleNotificationsViaPusherUseCase,
checkIfCanTogglePushNotificationsViaAccountDataUseCase = fakeCheckIfCanToggleNotificationsViaAccountDataUseCase,
checkIfCanToggleNotificationsViaPusherUseCase = fakeCheckIfCanToggleNotificationsViaPusherUseCase,
checkIfCanToggleNotificationsViaAccountDataUseCase = fakeCheckIfCanToggleNotificationsViaAccountDataUseCase,
setNotificationSettingsAccountDataUseCase = fakeSetNotificationSettingsAccountDataUseCase,
)
@ -72,7 +72,7 @@ class ToggleNotificationUseCaseTest {
val fakeSession = activeSessionHolder.fakeSession
every { fakeCheckIfCanToggleNotificationsViaPusherUseCase.execute(fakeSession) } returns false
every { fakeCheckIfCanToggleNotificationsViaAccountDataUseCase.execute(fakeSession, sessionId) } returns true
coJustRun { fakeSetNotificationSettingsAccountDataUseCase.execute(any(), any()) }
coJustRun { fakeSetNotificationSettingsAccountDataUseCase.execute(any(), any(), any()) }
val expectedLocalNotificationSettingsContent = LocalNotificationSettingsContent(
isSilenced = false
)
@ -82,7 +82,7 @@ class ToggleNotificationUseCaseTest {
// Then
coVerify {
fakeSetNotificationSettingsAccountDataUseCase.execute(sessionId, expectedLocalNotificationSettingsContent)
fakeSetNotificationSettingsAccountDataUseCase.execute(fakeSession, sessionId, expectedLocalNotificationSettingsContent)
}
}
}

View file

@ -0,0 +1,97 @@
/*
* 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.features.settings.devices.v2.notification
import im.vector.app.test.fakes.FakeSession
import im.vector.app.test.fakes.FakeVectorPreferences
import io.mockk.coJustRun
import io.mockk.coVerify
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import kotlinx.coroutines.test.runTest
import org.junit.Test
import org.matrix.android.sdk.api.account.LocalNotificationSettingsContent
class UpdateNotificationSettingsAccountDataUseCaseTest {
private val fakeVectorPreferences = FakeVectorPreferences()
private val fakeGetNotificationSettingsAccountDataUseCase = mockk<GetNotificationSettingsAccountDataUseCase>()
private val fakeSetNotificationSettingsAccountDataUseCase = mockk<SetNotificationSettingsAccountDataUseCase>()
private val updateNotificationSettingsAccountDataUseCase = UpdateNotificationSettingsAccountDataUseCase(
vectorPreferences = fakeVectorPreferences.instance,
getNotificationSettingsAccountDataUseCase = fakeGetNotificationSettingsAccountDataUseCase,
setNotificationSettingsAccountDataUseCase = fakeSetNotificationSettingsAccountDataUseCase,
)
@Test
fun `given a device id and a different local setting compared to remote when execute then content is updated`() = runTest {
// Given
val aDeviceId = "device-id"
val aSession = FakeSession()
aSession.givenSessionId(aDeviceId)
coJustRun { fakeSetNotificationSettingsAccountDataUseCase.execute(any(), any(), any()) }
val areNotificationsEnabled = true
fakeVectorPreferences.givenAreNotificationEnabled(areNotificationsEnabled)
every { fakeGetNotificationSettingsAccountDataUseCase.execute(any(), any()) } returns
LocalNotificationSettingsContent(
isSilenced = null
)
val expectedContent = LocalNotificationSettingsContent(
isSilenced = !areNotificationsEnabled
)
// When
updateNotificationSettingsAccountDataUseCase.execute(aSession)
// Then
verify {
fakeVectorPreferences.instance.areNotificationEnabledForDevice()
fakeGetNotificationSettingsAccountDataUseCase.execute(aSession, aDeviceId)
}
coVerify { fakeSetNotificationSettingsAccountDataUseCase.execute(aSession, aDeviceId, expectedContent) }
}
@Test
fun `given a device id and a same local setting compared to remote when execute then content is not updated`() = runTest {
// Given
val aDeviceId = "device-id"
val aSession = FakeSession()
aSession.givenSessionId(aDeviceId)
coJustRun { fakeSetNotificationSettingsAccountDataUseCase.execute(any(), any(), any()) }
val areNotificationsEnabled = true
fakeVectorPreferences.givenAreNotificationEnabled(areNotificationsEnabled)
every { fakeGetNotificationSettingsAccountDataUseCase.execute(any(), any()) } returns
LocalNotificationSettingsContent(
isSilenced = false
)
val expectedContent = LocalNotificationSettingsContent(
isSilenced = !areNotificationsEnabled
)
// When
updateNotificationSettingsAccountDataUseCase.execute(aSession)
// Then
verify {
fakeVectorPreferences.instance.areNotificationEnabledForDevice()
fakeGetNotificationSettingsAccountDataUseCase.execute(aSession, aDeviceId)
}
coVerify(inverse = true) { fakeSetNotificationSettingsAccountDataUseCase.execute(aSession, aDeviceId, expectedContent) }
}
}

View file

@ -30,7 +30,7 @@ import im.vector.app.test.fakes.FakeActiveSessionHolder
import im.vector.app.test.fakes.FakeGetNotificationsStatusUseCase
import im.vector.app.test.fakes.FakePendingAuthHandler
import im.vector.app.test.fakes.FakeSignoutSessionsUseCase
import im.vector.app.test.fakes.FakeTogglePushNotificationUseCase
import im.vector.app.test.fakes.FakeToggleNotificationUseCase
import im.vector.app.test.fakes.FakeVectorPreferences
import im.vector.app.test.fakes.FakeVerificationService
import im.vector.app.test.test
@ -76,7 +76,7 @@ class SessionOverviewViewModelTest {
private val interceptSignoutFlowResponseUseCase = mockk<InterceptSignoutFlowResponseUseCase>()
private val fakePendingAuthHandler = FakePendingAuthHandler()
private val refreshDevicesUseCase = mockk<RefreshDevicesUseCase>(relaxed = true)
private val togglePushNotificationUseCase = FakeTogglePushNotificationUseCase()
private val toggleNotificationUseCase = FakeToggleNotificationUseCase()
private val fakeGetNotificationsStatusUseCase = FakeGetNotificationsStatusUseCase()
private val notificationsStatus = NotificationsStatus.ENABLED
private val fakeVectorPreferences = FakeVectorPreferences()
@ -91,7 +91,7 @@ class SessionOverviewViewModelTest {
pendingAuthHandler = fakePendingAuthHandler.instance,
activeSessionHolder = fakeActiveSessionHolder.instance,
refreshDevicesUseCase = refreshDevicesUseCase,
togglePushNotificationUseCase = togglePushNotificationUseCase.instance,
toggleNotificationUseCase = toggleNotificationUseCase.instance,
getNotificationsStatusUseCase = fakeGetNotificationsStatusUseCase.instance,
vectorPreferences = fakeVectorPreferences.instance,
toggleIpAddressVisibilityUseCase = toggleIpAddressVisibilityUseCase,
@ -436,7 +436,7 @@ class SessionOverviewViewModelTest {
viewModel.handle(SessionOverviewAction.TogglePushNotifications(A_SESSION_ID_1, true))
togglePushNotificationUseCase.verifyExecute(A_SESSION_ID_1, true)
toggleNotificationUseCase.verifyExecute(A_SESSION_ID_1, true)
viewModel.test().assertLatestState { state -> state.notificationsStatus == NotificationsStatus.ENABLED }.finish()
}
}

View file

@ -16,11 +16,9 @@
package im.vector.app.features.settings.notifications
import im.vector.app.core.pushers.UnregisterUnifiedPushUseCase
import im.vector.app.features.settings.devices.v2.notification.CheckIfCanToggleNotificationsViaPusherUseCase
import im.vector.app.features.settings.devices.v2.notification.ToggleNotificationUseCase
import im.vector.app.core.pushers.UnregisterUnifiedPushUseCase
import im.vector.app.features.settings.devices.v2.notification.CheckIfCanTogglePushNotificationsViaPusherUseCase
import im.vector.app.features.settings.devices.v2.notification.TogglePushNotificationUseCase
import im.vector.app.test.fakes.FakeActiveSessionHolder
import im.vector.app.test.fakes.FakePushersManager
import io.mockk.coJustRun

View file

@ -21,7 +21,7 @@ import io.mockk.coJustRun
import io.mockk.coVerify
import io.mockk.mockk
class FakeTogglePushNotificationUseCase {
class FakeToggleNotificationUseCase {
val instance = mockk<ToggleNotificationUseCase> {
coJustRun { execute(any(), any()) }

View file

@ -73,4 +73,8 @@ class FakeVectorPreferences {
fun givenAreNotificationsEnabledForDevice(notificationsEnabled: Boolean) {
every { instance.areNotificationEnabledForDevice() } returns notificationsEnabled
}
fun givenIsBackgroundSyncEnabled(isEnabled: Boolean) {
every { instance.isBackgroundSyncEnabled() } returns isEnabled
}
}