From 0c6781e9ef1ca7784539f645a8ce7b2b508c918e Mon Sep 17 00:00:00 2001 From: Maxime NATUREL Date: Wed, 16 Nov 2022 14:37:48 +0100 Subject: [PATCH 01/21] Adding changelog entry --- changelog.d/7596.feature | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/7596.feature diff --git a/changelog.d/7596.feature b/changelog.d/7596.feature new file mode 100644 index 0000000000..022d86342b --- /dev/null +++ b/changelog.d/7596.feature @@ -0,0 +1 @@ +Save m.local_notification_settings. event in account_data From 9d684bc021b5af66b8309d24dd398f61bdbad7f6 Mon Sep 17 00:00:00 2001 From: Maxime NATUREL Date: Wed, 16 Nov 2022 16:31:00 +0100 Subject: [PATCH 02/21] Check if account data has content to decide if push notifications can be toggled using account data --- ...ePushNotificationsViaAccountDataUseCase.kt | 7 +++++- ...hNotificationsViaAccountDataUseCaseTest.kt | 23 +++++++++++++++++-- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/CheckIfCanTogglePushNotificationsViaAccountDataUseCase.kt b/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/CheckIfCanTogglePushNotificationsViaAccountDataUseCase.kt index 194a2aebbf..a1b87bc396 100644 --- a/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/CheckIfCanTogglePushNotificationsViaAccountDataUseCase.kt +++ b/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/CheckIfCanTogglePushNotificationsViaAccountDataUseCase.kt @@ -16,8 +16,10 @@ 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 CheckIfCanTogglePushNotificationsViaAccountDataUseCase @Inject constructor() { @@ -25,6 +27,9 @@ class CheckIfCanTogglePushNotificationsViaAccountDataUseCase @Inject constructor fun execute(session: Session, deviceId: String): Boolean { return session .accountDataService() - .getUserAccountDataEvent(UserAccountDataTypes.TYPE_LOCAL_NOTIFICATION_SETTINGS + deviceId) != null + .getUserAccountDataEvent(UserAccountDataTypes.TYPE_LOCAL_NOTIFICATION_SETTINGS + deviceId) + ?.content + .toModel() + ?.isSilenced != null } } diff --git a/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/CheckIfCanTogglePushNotificationsViaAccountDataUseCaseTest.kt b/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/CheckIfCanTogglePushNotificationsViaAccountDataUseCaseTest.kt index 37433364e8..94f142cbe6 100644 --- a/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/CheckIfCanTogglePushNotificationsViaAccountDataUseCaseTest.kt +++ b/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/CheckIfCanTogglePushNotificationsViaAccountDataUseCaseTest.kt @@ -20,7 +20,9 @@ import im.vector.app.test.fakes.FakeSession 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" @@ -32,7 +34,24 @@ class CheckIfCanTogglePushNotificationsViaAccountDataUseCaseTest { CheckIfCanTogglePushNotificationsViaAccountDataUseCase() @Test - fun `given current session and an account data for the device id when execute then result is true`() { + 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(), + ) + + // When + val result = checkIfCanTogglePushNotificationsViaAccountDataUseCase.execute(fakeSession, A_DEVICE_ID) + + // Then + result shouldBeEqualTo true + } + + @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() @@ -45,7 +64,7 @@ class CheckIfCanTogglePushNotificationsViaAccountDataUseCaseTest { val result = checkIfCanTogglePushNotificationsViaAccountDataUseCase.execute(fakeSession, A_DEVICE_ID) // Then - result shouldBeEqualTo true + result shouldBeEqualTo false } @Test From c56eb331db5cfe652d70ba4d3f293dbb118dd55c Mon Sep 17 00:00:00 2001 From: Maxime NATUREL Date: Wed, 16 Nov 2022 16:38:46 +0100 Subject: [PATCH 03/21] Update use cases to enable/disable push notifications for the current session --- .../DisableNotificationsForCurrentSessionUseCase.kt | 5 ++--- .../DisableNotificationsForCurrentSessionUseCaseTest.kt | 2 ++ 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/vector/src/main/java/im/vector/app/features/settings/notifications/DisableNotificationsForCurrentSessionUseCase.kt b/vector/src/main/java/im/vector/app/features/settings/notifications/DisableNotificationsForCurrentSessionUseCase.kt index 84d92c4291..4d890ca678 100644 --- a/vector/src/main/java/im/vector/app/features/settings/notifications/DisableNotificationsForCurrentSessionUseCase.kt +++ b/vector/src/main/java/im/vector/app/features/settings/notifications/DisableNotificationsForCurrentSessionUseCase.kt @@ -34,9 +34,8 @@ class DisableNotificationsForCurrentSessionUseCase @Inject constructor( suspend fun execute() { val session = activeSessionHolder.getSafeActiveSession() ?: return val deviceId = session.sessionParams.deviceId ?: return - if (checkIfCanTogglePushNotificationsViaPusherUseCase.execute(session)) { - togglePushNotificationUseCase.execute(deviceId, enabled = false) - } else { + togglePushNotificationUseCase.execute(deviceId, enabled = false) + if (!checkIfCanTogglePushNotificationsViaPusherUseCase.execute(session)) { unregisterUnifiedPushUseCase.execute(pushersManager) } } diff --git a/vector/src/test/java/im/vector/app/features/settings/notifications/DisableNotificationsForCurrentSessionUseCaseTest.kt b/vector/src/test/java/im/vector/app/features/settings/notifications/DisableNotificationsForCurrentSessionUseCaseTest.kt index 386e68ee3a..a84aa4b055 100644 --- a/vector/src/test/java/im/vector/app/features/settings/notifications/DisableNotificationsForCurrentSessionUseCaseTest.kt +++ b/vector/src/test/java/im/vector/app/features/settings/notifications/DisableNotificationsForCurrentSessionUseCaseTest.kt @@ -67,6 +67,7 @@ class DisableNotificationsForCurrentSessionUseCaseTest { val fakeSession = fakeActiveSessionHolder.fakeSession fakeSession.givenSessionId(A_SESSION_ID) every { fakeCheckIfCanTogglePushNotificationsViaPusherUseCase.execute(fakeSession) } returns false + coJustRun { fakeTogglePushNotificationUseCase.execute(A_SESSION_ID, any()) } coJustRun { fakeUnregisterUnifiedPushUseCase.execute(any()) } // When @@ -74,6 +75,7 @@ class DisableNotificationsForCurrentSessionUseCaseTest { // Then coVerify { + fakeTogglePushNotificationUseCase.execute(A_SESSION_ID, false) fakeUnregisterUnifiedPushUseCase.execute(fakePushersManager.instance) } } From 81c64503f2f5956c3ef331128b251538c16c9b22 Mon Sep 17 00:00:00 2001 From: Maxime NATUREL Date: Wed, 16 Nov 2022 18:02:59 +0100 Subject: [PATCH 04/21] Adding SetNotificationSettingsAccountDataUseCase --- ...tNotificationSettingsAccountDataUseCase.kt | 36 +++++++++++++ ...ificationSettingsAccountDataUseCaseTest.kt | 51 +++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/SetNotificationSettingsAccountDataUseCase.kt create mode 100644 vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/SetNotificationSettingsAccountDataUseCaseTest.kt diff --git a/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/SetNotificationSettingsAccountDataUseCase.kt b/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/SetNotificationSettingsAccountDataUseCase.kt new file mode 100644 index 0000000000..f0ec9d5ddc --- /dev/null +++ b/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/SetNotificationSettingsAccountDataUseCase.kt @@ -0,0 +1,36 @@ +/* + * 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.core.di.ActiveSessionHolder +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 +import javax.inject.Inject + +class SetNotificationSettingsAccountDataUseCase @Inject constructor( + private val activeSessionHolder: ActiveSessionHolder, +) { + + suspend fun execute(deviceId: String, localNotificationSettingsContent: LocalNotificationSettingsContent) { + val session = activeSessionHolder.getSafeActiveSession() ?: return + session.accountDataService().updateUserAccountData( + UserAccountDataTypes.TYPE_LOCAL_NOTIFICATION_SETTINGS + deviceId, + localNotificationSettingsContent.toContent(), + ) + } +} diff --git a/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/SetNotificationSettingsAccountDataUseCaseTest.kt b/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/SetNotificationSettingsAccountDataUseCaseTest.kt new file mode 100644 index 0000000000..8f72f0946f --- /dev/null +++ b/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/SetNotificationSettingsAccountDataUseCaseTest.kt @@ -0,0 +1,51 @@ +/* + * 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.FakeActiveSessionHolder +import kotlinx.coroutines.test.runTest +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 SetNotificationSettingsAccountDataUseCaseTest { + + private val activeSessionHolder = FakeActiveSessionHolder() + + private val setNotificationSettingsAccountDataUseCase = SetNotificationSettingsAccountDataUseCase( + activeSessionHolder = activeSessionHolder.instance, + ) + + @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 + fakeSession.accountDataService().givenUpdateUserAccountDataEventSucceeds() + + // When + setNotificationSettingsAccountDataUseCase.execute(sessionId, localNotificationSettingsContent) + + // Then + activeSessionHolder.fakeSession.accountDataService().verifyUpdateUserAccountDataEventSucceeds( + UserAccountDataTypes.TYPE_LOCAL_NOTIFICATION_SETTINGS + sessionId, + localNotificationSettingsContent.toContent(), + ) + } +} From b163b42d3d54f7c296d723b491b2e7d067f71ff5 Mon Sep 17 00:00:00 2001 From: Maxime NATUREL Date: Thu, 17 Nov 2022 10:14:51 +0100 Subject: [PATCH 05/21] Use new sub usecase in the TogglePushNotificationUseCase --- .../TogglePushNotificationUseCase.kt | 9 +++---- .../TogglePushNotificationUseCaseTest.kt | 27 +++++++++---------- 2 files changed, 15 insertions(+), 21 deletions(-) diff --git a/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/TogglePushNotificationUseCase.kt b/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/TogglePushNotificationUseCase.kt index 7969bbbe9b..b8a6d7343b 100644 --- a/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/TogglePushNotificationUseCase.kt +++ b/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/TogglePushNotificationUseCase.kt @@ -18,14 +18,14 @@ 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.accountdata.UserAccountDataTypes -import org.matrix.android.sdk.api.session.events.model.toContent import javax.inject.Inject +// TODO rename into ToggleNotificationsUseCase class TogglePushNotificationUseCase @Inject constructor( private val activeSessionHolder: ActiveSessionHolder, private val checkIfCanTogglePushNotificationsViaPusherUseCase: CheckIfCanTogglePushNotificationsViaPusherUseCase, private val checkIfCanTogglePushNotificationsViaAccountDataUseCase: CheckIfCanTogglePushNotificationsViaAccountDataUseCase, + private val setNotificationSettingsAccountDataUseCase: SetNotificationSettingsAccountDataUseCase, ) { suspend fun execute(deviceId: String, enabled: Boolean) { @@ -40,10 +40,7 @@ class TogglePushNotificationUseCase @Inject constructor( if (checkIfCanTogglePushNotificationsViaAccountDataUseCase.execute(session, deviceId)) { val newNotificationSettingsContent = LocalNotificationSettingsContent(isSilenced = !enabled) - session.accountDataService().updateUserAccountData( - UserAccountDataTypes.TYPE_LOCAL_NOTIFICATION_SETTINGS + deviceId, - newNotificationSettingsContent.toContent(), - ) + setNotificationSettingsAccountDataUseCase.execute(deviceId, newNotificationSettingsContent) } } } diff --git a/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/TogglePushNotificationUseCaseTest.kt b/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/TogglePushNotificationUseCaseTest.kt index 35c5979e53..e443dc3e94 100644 --- a/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/TogglePushNotificationUseCaseTest.kt +++ b/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/TogglePushNotificationUseCaseTest.kt @@ -18,13 +18,13 @@ package im.vector.app.features.settings.devices.v2.notification import im.vector.app.test.fakes.FakeActiveSessionHolder import im.vector.app.test.fixtures.PusherFixture +import io.mockk.coJustRun +import io.mockk.coVerify import io.mockk.every import io.mockk.mockk import kotlinx.coroutines.test.runTest 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 TogglePushNotificationUseCaseTest { @@ -33,12 +33,15 @@ class TogglePushNotificationUseCaseTest { mockk() private val fakeCheckIfCanTogglePushNotificationsViaAccountDataUseCase = mockk() + private val fakeSetNotificationSettingsAccountDataUseCase = + mockk() private val togglePushNotificationUseCase = TogglePushNotificationUseCase( activeSessionHolder = activeSessionHolder.instance, checkIfCanTogglePushNotificationsViaPusherUseCase = fakeCheckIfCanTogglePushNotificationsViaPusherUseCase, checkIfCanTogglePushNotificationsViaAccountDataUseCase = fakeCheckIfCanTogglePushNotificationsViaAccountDataUseCase, + setNotificationSettingsAccountDataUseCase = fakeSetNotificationSettingsAccountDataUseCase, ) @Test @@ -66,26 +69,20 @@ class TogglePushNotificationUseCaseTest { fun `when execute, then toggle local notification settings`() = runTest { // Given val sessionId = "a_session_id" - val pushers = listOf( - PusherFixture.aPusher(deviceId = sessionId, enabled = false), - PusherFixture.aPusher(deviceId = "another id", enabled = false) - ) val fakeSession = activeSessionHolder.fakeSession - fakeSession.pushersService().givenPushersLive(pushers) - fakeSession.accountDataService().givenGetUserAccountDataEventReturns( - UserAccountDataTypes.TYPE_LOCAL_NOTIFICATION_SETTINGS + sessionId, - LocalNotificationSettingsContent(isSilenced = true).toContent() - ) every { fakeCheckIfCanTogglePushNotificationsViaPusherUseCase.execute(fakeSession) } returns false every { fakeCheckIfCanTogglePushNotificationsViaAccountDataUseCase.execute(fakeSession, sessionId) } returns true + coJustRun { fakeSetNotificationSettingsAccountDataUseCase.execute(any(), any()) } + val expectedLocalNotificationSettingsContent = LocalNotificationSettingsContent( + isSilenced = false + ) // When togglePushNotificationUseCase.execute(sessionId, true) // Then - activeSessionHolder.fakeSession.accountDataService().verifyUpdateUserAccountDataEventSucceeds( - UserAccountDataTypes.TYPE_LOCAL_NOTIFICATION_SETTINGS + sessionId, - LocalNotificationSettingsContent(isSilenced = false).toContent(), - ) + coVerify { + fakeSetNotificationSettingsAccountDataUseCase.execute(sessionId, expectedLocalNotificationSettingsContent) + } } } From 14b21dc0397c5b4e77a5ad5c94e220847b82cb14 Mon Sep 17 00:00:00 2001 From: Maxime NATUREL Date: Thu, 17 Nov 2022 10:34:11 +0100 Subject: [PATCH 06/21] Adding use cases to create and delete notifications settings in account data --- .../LocalNotificationSettingsContent.kt | 3 +- ...eNotificationSettingsAccountDataUseCase.kt | 38 ++++++++++++ ...eNotificationSettingsAccountDataUseCase.kt | 33 +++++++++++ ...ificationSettingsAccountDataUseCaseTest.kt | 59 +++++++++++++++++++ ...ificationSettingsAccountDataUseCaseTest.kt | 49 +++++++++++++++ 5 files changed, 181 insertions(+), 1 deletion(-) create mode 100644 vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/CreateNotificationSettingsAccountDataUseCase.kt create mode 100644 vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/DeleteNotificationSettingsAccountDataUseCase.kt create mode 100644 vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/CreateNotificationSettingsAccountDataUseCaseTest.kt create mode 100644 vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/DeleteNotificationSettingsAccountDataUseCaseTest.kt diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/account/LocalNotificationSettingsContent.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/account/LocalNotificationSettingsContent.kt index 2a95ccce7a..6998d9dcf2 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/account/LocalNotificationSettingsContent.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/account/LocalNotificationSettingsContent.kt @@ -21,5 +21,6 @@ import com.squareup.moshi.JsonClass @JsonClass(generateAdapter = true) data class LocalNotificationSettingsContent( - @Json(name = "is_silenced") val isSilenced: Boolean = false + @Json(name = "is_silenced") + val isSilenced: Boolean? = false ) diff --git a/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/CreateNotificationSettingsAccountDataUseCase.kt b/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/CreateNotificationSettingsAccountDataUseCase.kt new file mode 100644 index 0000000000..e2ee19e5cd --- /dev/null +++ b/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/CreateNotificationSettingsAccountDataUseCase.kt @@ -0,0 +1,38 @@ +/* + * 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.features.settings.VectorPreferences +import org.matrix.android.sdk.api.account.LocalNotificationSettingsContent +import org.matrix.android.sdk.api.session.Session +import javax.inject.Inject + +class CreateNotificationSettingsAccountDataUseCase @Inject constructor( + private val vectorPreferences: VectorPreferences, + private val setNotificationSettingsAccountDataUseCase: SetNotificationSettingsAccountDataUseCase +) { + + // TODO to be called on session start when background sync is enabled + when switching to background sync + suspend fun execute(session: Session) { + val deviceId = session.sessionParams.deviceId ?: return + val isSilenced = !vectorPreferences.areNotificationEnabledForDevice() + val notificationSettingsContent = LocalNotificationSettingsContent( + isSilenced = isSilenced + ) + setNotificationSettingsAccountDataUseCase.execute(deviceId, notificationSettingsContent) + } +} diff --git a/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/DeleteNotificationSettingsAccountDataUseCase.kt b/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/DeleteNotificationSettingsAccountDataUseCase.kt new file mode 100644 index 0000000000..51c24e500c --- /dev/null +++ b/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/DeleteNotificationSettingsAccountDataUseCase.kt @@ -0,0 +1,33 @@ +/* + * 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 javax.inject.Inject + +class DeleteNotificationSettingsAccountDataUseCase @Inject constructor( + private val setNotificationSettingsAccountDataUseCase: SetNotificationSettingsAccountDataUseCase, +) { + + // TODO to be called when switching to push notifications method + suspend fun execute(deviceId: String) { + val emptyNotificationSettingsContent = LocalNotificationSettingsContent( + isSilenced = null + ) + setNotificationSettingsAccountDataUseCase.execute(deviceId, emptyNotificationSettingsContent) + } +} diff --git a/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/CreateNotificationSettingsAccountDataUseCaseTest.kt b/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/CreateNotificationSettingsAccountDataUseCaseTest.kt new file mode 100644 index 0000000000..e4cadaa005 --- /dev/null +++ b/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/CreateNotificationSettingsAccountDataUseCaseTest.kt @@ -0,0 +1,59 @@ +/* + * 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() + + 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) } + } +} diff --git a/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/DeleteNotificationSettingsAccountDataUseCaseTest.kt b/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/DeleteNotificationSettingsAccountDataUseCaseTest.kt new file mode 100644 index 0000000000..038a1f436a --- /dev/null +++ b/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/DeleteNotificationSettingsAccountDataUseCaseTest.kt @@ -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 io.mockk.coJustRun +import io.mockk.coVerify +import io.mockk.mockk +import kotlinx.coroutines.test.runTest +import org.junit.Test +import org.matrix.android.sdk.api.account.LocalNotificationSettingsContent + +class DeleteNotificationSettingsAccountDataUseCaseTest { + + private val fakeSetNotificationSettingsAccountDataUseCase = mockk() + + private val deleteNotificationSettingsAccountDataUseCase = DeleteNotificationSettingsAccountDataUseCase( + setNotificationSettingsAccountDataUseCase = fakeSetNotificationSettingsAccountDataUseCase, + ) + + @Test + 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 expectedContent = LocalNotificationSettingsContent( + isSilenced = null + ) + + // When + deleteNotificationSettingsAccountDataUseCase.execute(aDeviceId) + + // Then + coVerify { fakeSetNotificationSettingsAccountDataUseCase.execute(aDeviceId, expectedContent) } + } +} From 7c51174d7ef3940cbb550dcbd89dcce942ae4bec Mon Sep 17 00:00:00 2001 From: Maxime NATUREL Date: Thu, 17 Nov 2022 11:04:21 +0100 Subject: [PATCH 07/21] Renaming some use cases to be consistent --- ...CanToggleNotificationsViaPusherUseCase.kt} | 2 +- ...ggleNotificationsViaAccountDataUseCase.kt} | 2 +- ...CanToggleNotificationsViaPusherUseCase.kt} | 2 +- .../GetNotificationsStatusUseCase.kt | 8 ++--- ...seCase.kt => ToggleNotificationUseCase.kt} | 11 ++++--- .../v2/overview/SessionOverviewViewModel.kt | 6 ++-- ...leNotificationsForCurrentSessionUseCase.kt | 12 ++++---- ...leNotificationsForCurrentSessionUseCase.kt | 6 ++-- ...oggleNotificationsViaPusherUseCaseTest.kt} | 8 ++--- ...NotificationsViaAccountDataUseCaseTest.kt} | 12 ++++---- ...oggleNotificationsViaPusherUseCaseTest.kt} | 8 ++--- .../GetNotificationsStatusUseCaseTest.kt | 28 ++++++++--------- ...st.kt => ToggleNotificationUseCaseTest.kt} | 30 +++++++++---------- ...tificationsForCurrentSessionUseCaseTest.kt | 22 +++++++------- ...tificationsForCurrentSessionUseCaseTest.kt | 8 ++--- .../FakeTogglePushNotificationUseCase.kt | 4 +-- 16 files changed, 85 insertions(+), 84 deletions(-) rename vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/{CanTogglePushNotificationsViaPusherUseCase.kt => CanToggleNotificationsViaPusherUseCase.kt} (94%) rename vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/{CheckIfCanTogglePushNotificationsViaAccountDataUseCase.kt => CheckIfCanToggleNotificationsViaAccountDataUseCase.kt} (93%) rename vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/{CheckIfCanTogglePushNotificationsViaPusherUseCase.kt => CheckIfCanToggleNotificationsViaPusherUseCase.kt} (92%) rename vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/{TogglePushNotificationUseCase.kt => ToggleNotificationUseCase.kt} (74%) rename vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/{CanTogglePushNotificationsViaPusherUseCaseTest.kt => CanToggleNotificationsViaPusherUseCaseTest.kt} (87%) rename vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/{CheckIfCanTogglePushNotificationsViaAccountDataUseCaseTest.kt => CheckIfCanToggleNotificationsViaAccountDataUseCaseTest.kt} (83%) rename vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/{CheckIfCanTogglePushNotificationsViaPusherUseCaseTest.kt => CheckIfCanToggleNotificationsViaPusherUseCaseTest.kt} (82%) rename vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/{TogglePushNotificationUseCaseTest.kt => ToggleNotificationUseCaseTest.kt} (70%) diff --git a/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/CanTogglePushNotificationsViaPusherUseCase.kt b/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/CanToggleNotificationsViaPusherUseCase.kt similarity index 94% rename from vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/CanTogglePushNotificationsViaPusherUseCase.kt rename to vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/CanToggleNotificationsViaPusherUseCase.kt index 0125d92ba6..96521ec78c 100644 --- a/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/CanTogglePushNotificationsViaPusherUseCase.kt +++ b/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/CanToggleNotificationsViaPusherUseCase.kt @@ -24,7 +24,7 @@ import org.matrix.android.sdk.api.session.Session import org.matrix.android.sdk.flow.unwrap import javax.inject.Inject -class CanTogglePushNotificationsViaPusherUseCase @Inject constructor() { +class CanToggleNotificationsViaPusherUseCase @Inject constructor() { fun execute(session: Session): Flow { return session diff --git a/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/CheckIfCanTogglePushNotificationsViaAccountDataUseCase.kt b/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/CheckIfCanToggleNotificationsViaAccountDataUseCase.kt similarity index 93% rename from vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/CheckIfCanTogglePushNotificationsViaAccountDataUseCase.kt rename to vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/CheckIfCanToggleNotificationsViaAccountDataUseCase.kt index a1b87bc396..b006e3da45 100644 --- a/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/CheckIfCanTogglePushNotificationsViaAccountDataUseCase.kt +++ b/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/CheckIfCanToggleNotificationsViaAccountDataUseCase.kt @@ -22,7 +22,7 @@ import org.matrix.android.sdk.api.session.accountdata.UserAccountDataTypes import org.matrix.android.sdk.api.session.events.model.toModel import javax.inject.Inject -class CheckIfCanTogglePushNotificationsViaAccountDataUseCase @Inject constructor() { +class CheckIfCanToggleNotificationsViaAccountDataUseCase @Inject constructor() { fun execute(session: Session, deviceId: String): Boolean { return session diff --git a/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/CheckIfCanTogglePushNotificationsViaPusherUseCase.kt b/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/CheckIfCanToggleNotificationsViaPusherUseCase.kt similarity index 92% rename from vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/CheckIfCanTogglePushNotificationsViaPusherUseCase.kt rename to vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/CheckIfCanToggleNotificationsViaPusherUseCase.kt index ca314bf145..1dc186be7c 100644 --- a/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/CheckIfCanTogglePushNotificationsViaPusherUseCase.kt +++ b/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/CheckIfCanToggleNotificationsViaPusherUseCase.kt @@ -19,7 +19,7 @@ package im.vector.app.features.settings.devices.v2.notification import org.matrix.android.sdk.api.session.Session import javax.inject.Inject -class CheckIfCanTogglePushNotificationsViaPusherUseCase @Inject constructor() { +class CheckIfCanToggleNotificationsViaPusherUseCase @Inject constructor() { fun execute(session: Session): Boolean { return session diff --git a/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/GetNotificationsStatusUseCase.kt b/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/GetNotificationsStatusUseCase.kt index 03e4e31f2e..f98fd63efb 100644 --- a/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/GetNotificationsStatusUseCase.kt +++ b/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/GetNotificationsStatusUseCase.kt @@ -30,13 +30,13 @@ import org.matrix.android.sdk.flow.unwrap import javax.inject.Inject class GetNotificationsStatusUseCase @Inject constructor( - private val canTogglePushNotificationsViaPusherUseCase: CanTogglePushNotificationsViaPusherUseCase, - private val checkIfCanTogglePushNotificationsViaAccountDataUseCase: CheckIfCanTogglePushNotificationsViaAccountDataUseCase, + private val canToggleNotificationsViaPusherUseCase: CanToggleNotificationsViaPusherUseCase, + private val checkIfCanToggleNotificationsViaAccountDataUseCase: CheckIfCanToggleNotificationsViaAccountDataUseCase, ) { fun execute(session: Session, deviceId: String): Flow { return when { - checkIfCanTogglePushNotificationsViaAccountDataUseCase.execute(session, deviceId) -> { + checkIfCanToggleNotificationsViaAccountDataUseCase.execute(session, deviceId) -> { session.flow() .liveUserAccountData(UserAccountDataTypes.TYPE_LOCAL_NOTIFICATION_SETTINGS + deviceId) .unwrap() @@ -44,7 +44,7 @@ class GetNotificationsStatusUseCase @Inject constructor( .map { if (it == true) NotificationsStatus.ENABLED else NotificationsStatus.DISABLED } .distinctUntilChanged() } - else -> canTogglePushNotificationsViaPusherUseCase.execute(session) + else -> canToggleNotificationsViaPusherUseCase.execute(session) .flatMapLatest { canToggle -> if (canToggle) { session.flow() diff --git a/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/TogglePushNotificationUseCase.kt b/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/ToggleNotificationUseCase.kt similarity index 74% rename from vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/TogglePushNotificationUseCase.kt rename to vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/ToggleNotificationUseCase.kt index b8a6d7343b..d0e1ea2a7a 100644 --- a/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/TogglePushNotificationUseCase.kt +++ b/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/ToggleNotificationUseCase.kt @@ -20,25 +20,24 @@ import im.vector.app.core.di.ActiveSessionHolder import org.matrix.android.sdk.api.account.LocalNotificationSettingsContent import javax.inject.Inject -// TODO rename into ToggleNotificationsUseCase -class TogglePushNotificationUseCase @Inject constructor( +class ToggleNotificationUseCase @Inject constructor( private val activeSessionHolder: ActiveSessionHolder, - private val checkIfCanTogglePushNotificationsViaPusherUseCase: CheckIfCanTogglePushNotificationsViaPusherUseCase, - private val checkIfCanTogglePushNotificationsViaAccountDataUseCase: CheckIfCanTogglePushNotificationsViaAccountDataUseCase, + private val checkIfCanToggleNotificationsViaPusherUseCase: CheckIfCanToggleNotificationsViaPusherUseCase, + private val checkIfCanToggleNotificationsViaAccountDataUseCase: CheckIfCanToggleNotificationsViaAccountDataUseCase, private val setNotificationSettingsAccountDataUseCase: SetNotificationSettingsAccountDataUseCase, ) { suspend fun execute(deviceId: String, enabled: Boolean) { val session = activeSessionHolder.getSafeActiveSession() ?: return - if (checkIfCanTogglePushNotificationsViaPusherUseCase.execute(session)) { + if (checkIfCanToggleNotificationsViaPusherUseCase.execute(session)) { val devicePusher = session.pushersService().getPushers().firstOrNull { it.deviceId == deviceId } devicePusher?.let { pusher -> session.pushersService().togglePusher(pusher, enabled) } } - if (checkIfCanTogglePushNotificationsViaAccountDataUseCase.execute(session, deviceId)) { + if (checkIfCanToggleNotificationsViaAccountDataUseCase.execute(session, deviceId)) { val newNotificationSettingsContent = LocalNotificationSettingsContent(isSilenced = !enabled) setNotificationSettingsAccountDataUseCase.execute(deviceId, newNotificationSettingsContent) } diff --git a/vector/src/main/java/im/vector/app/features/settings/devices/v2/overview/SessionOverviewViewModel.kt b/vector/src/main/java/im/vector/app/features/settings/devices/v2/overview/SessionOverviewViewModel.kt index 472e0a4269..0ddf688514 100644 --- a/vector/src/main/java/im/vector/app/features/settings/devices/v2/overview/SessionOverviewViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/settings/devices/v2/overview/SessionOverviewViewModel.kt @@ -31,7 +31,7 @@ import im.vector.app.features.settings.devices.v2.RefreshDevicesUseCase import im.vector.app.features.settings.devices.v2.ToggleIpAddressVisibilityUseCase import im.vector.app.features.settings.devices.v2.VectorSessionsListViewModel import im.vector.app.features.settings.devices.v2.notification.GetNotificationsStatusUseCase -import im.vector.app.features.settings.devices.v2.notification.TogglePushNotificationUseCase +import im.vector.app.features.settings.devices.v2.notification.ToggleNotificationUseCase import im.vector.app.features.settings.devices.v2.signout.InterceptSignoutFlowResponseUseCase import im.vector.app.features.settings.devices.v2.signout.SignoutSessionsReAuthNeeded import im.vector.app.features.settings.devices.v2.signout.SignoutSessionsUseCase @@ -54,7 +54,7 @@ class SessionOverviewViewModel @AssistedInject constructor( private val interceptSignoutFlowResponseUseCase: InterceptSignoutFlowResponseUseCase, private val pendingAuthHandler: PendingAuthHandler, private val activeSessionHolder: ActiveSessionHolder, - private val togglePushNotificationUseCase: TogglePushNotificationUseCase, + private val toggleNotificationUseCase: ToggleNotificationUseCase, private val getNotificationsStatusUseCase: GetNotificationsStatusUseCase, refreshDevicesUseCase: RefreshDevicesUseCase, private val vectorPreferences: VectorPreferences, @@ -228,7 +228,7 @@ class SessionOverviewViewModel @AssistedInject constructor( private fun handleTogglePusherAction(action: SessionOverviewAction.TogglePushNotifications) { viewModelScope.launch { - togglePushNotificationUseCase.execute(action.deviceId, action.enabled) + toggleNotificationUseCase.execute(action.deviceId, action.enabled) } } } diff --git a/vector/src/main/java/im/vector/app/features/settings/notifications/DisableNotificationsForCurrentSessionUseCase.kt b/vector/src/main/java/im/vector/app/features/settings/notifications/DisableNotificationsForCurrentSessionUseCase.kt index 4d890ca678..380c3b5e4e 100644 --- a/vector/src/main/java/im/vector/app/features/settings/notifications/DisableNotificationsForCurrentSessionUseCase.kt +++ b/vector/src/main/java/im/vector/app/features/settings/notifications/DisableNotificationsForCurrentSessionUseCase.kt @@ -19,23 +19,23 @@ package im.vector.app.features.settings.notifications import im.vector.app.core.di.ActiveSessionHolder import im.vector.app.core.pushers.PushersManager 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.features.settings.devices.v2.notification.CheckIfCanToggleNotificationsViaPusherUseCase +import im.vector.app.features.settings.devices.v2.notification.ToggleNotificationUseCase import javax.inject.Inject class DisableNotificationsForCurrentSessionUseCase @Inject constructor( private val activeSessionHolder: ActiveSessionHolder, private val pushersManager: PushersManager, - private val checkIfCanTogglePushNotificationsViaPusherUseCase: CheckIfCanTogglePushNotificationsViaPusherUseCase, - private val togglePushNotificationUseCase: TogglePushNotificationUseCase, + private val checkIfCanToggleNotificationsViaPusherUseCase: CheckIfCanToggleNotificationsViaPusherUseCase, + private val toggleNotificationUseCase: ToggleNotificationUseCase, private val unregisterUnifiedPushUseCase: UnregisterUnifiedPushUseCase, ) { suspend fun execute() { val session = activeSessionHolder.getSafeActiveSession() ?: return val deviceId = session.sessionParams.deviceId ?: return - togglePushNotificationUseCase.execute(deviceId, enabled = false) - if (!checkIfCanTogglePushNotificationsViaPusherUseCase.execute(session)) { + toggleNotificationUseCase.execute(deviceId, enabled = false) + if (!checkIfCanToggleNotificationsViaPusherUseCase.execute(session)) { unregisterUnifiedPushUseCase.execute(pushersManager) } } diff --git a/vector/src/main/java/im/vector/app/features/settings/notifications/EnableNotificationsForCurrentSessionUseCase.kt b/vector/src/main/java/im/vector/app/features/settings/notifications/EnableNotificationsForCurrentSessionUseCase.kt index 99fb249384..89633a10c2 100644 --- a/vector/src/main/java/im/vector/app/features/settings/notifications/EnableNotificationsForCurrentSessionUseCase.kt +++ b/vector/src/main/java/im/vector/app/features/settings/notifications/EnableNotificationsForCurrentSessionUseCase.kt @@ -20,13 +20,13 @@ import im.vector.app.core.di.ActiveSessionHolder import im.vector.app.core.pushers.EnsureFcmTokenIsRetrievedUseCase import im.vector.app.core.pushers.PushersManager import im.vector.app.core.pushers.RegisterUnifiedPushUseCase -import im.vector.app.features.settings.devices.v2.notification.TogglePushNotificationUseCase +import im.vector.app.features.settings.devices.v2.notification.ToggleNotificationUseCase import javax.inject.Inject class EnableNotificationsForCurrentSessionUseCase @Inject constructor( private val activeSessionHolder: ActiveSessionHolder, private val pushersManager: PushersManager, - private val togglePushNotificationUseCase: TogglePushNotificationUseCase, + private val toggleNotificationUseCase: ToggleNotificationUseCase, private val registerUnifiedPushUseCase: RegisterUnifiedPushUseCase, private val ensureFcmTokenIsRetrievedUseCase: EnsureFcmTokenIsRetrievedUseCase, ) { @@ -52,7 +52,7 @@ class EnableNotificationsForCurrentSessionUseCase @Inject constructor( val session = activeSessionHolder.getSafeActiveSession() ?: return EnableNotificationsResult.Failure val deviceId = session.sessionParams.deviceId ?: return EnableNotificationsResult.Failure - togglePushNotificationUseCase.execute(deviceId, enabled = true) + toggleNotificationUseCase.execute(deviceId, enabled = true) return EnableNotificationsResult.Success } diff --git a/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/CanTogglePushNotificationsViaPusherUseCaseTest.kt b/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/CanToggleNotificationsViaPusherUseCaseTest.kt similarity index 87% rename from vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/CanTogglePushNotificationsViaPusherUseCaseTest.kt rename to vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/CanToggleNotificationsViaPusherUseCaseTest.kt index 997fa827f5..3284adb32d 100644 --- a/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/CanTogglePushNotificationsViaPusherUseCaseTest.kt +++ b/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/CanToggleNotificationsViaPusherUseCaseTest.kt @@ -30,13 +30,13 @@ import org.junit.Test private val A_HOMESERVER_CAPABILITIES = aHomeServerCapabilities(canRemotelyTogglePushNotificationsOfDevices = true) -class CanTogglePushNotificationsViaPusherUseCaseTest { +class CanToggleNotificationsViaPusherUseCaseTest { private val fakeSession = FakeSession() private val fakeFlowLiveDataConversions = FakeFlowLiveDataConversions() - private val canTogglePushNotificationsViaPusherUseCase = - CanTogglePushNotificationsViaPusherUseCase() + private val canToggleNotificationsViaPusherUseCase = + CanToggleNotificationsViaPusherUseCase() @Before fun setUp() { @@ -57,7 +57,7 @@ class CanTogglePushNotificationsViaPusherUseCaseTest { .givenAsFlow() // When - val result = canTogglePushNotificationsViaPusherUseCase.execute(fakeSession).firstOrNull() + val result = canToggleNotificationsViaPusherUseCase.execute(fakeSession).firstOrNull() // Then result shouldBeEqualTo A_HOMESERVER_CAPABILITIES.canRemotelyTogglePushNotificationsOfDevices diff --git a/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/CheckIfCanTogglePushNotificationsViaAccountDataUseCaseTest.kt b/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/CheckIfCanToggleNotificationsViaAccountDataUseCaseTest.kt similarity index 83% rename from vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/CheckIfCanTogglePushNotificationsViaAccountDataUseCaseTest.kt rename to vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/CheckIfCanToggleNotificationsViaAccountDataUseCaseTest.kt index 94f142cbe6..de225d36ac 100644 --- a/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/CheckIfCanTogglePushNotificationsViaAccountDataUseCaseTest.kt +++ b/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/CheckIfCanToggleNotificationsViaAccountDataUseCaseTest.kt @@ -26,12 +26,12 @@ import org.matrix.android.sdk.api.session.events.model.toContent private const val A_DEVICE_ID = "device-id" -class CheckIfCanTogglePushNotificationsViaAccountDataUseCaseTest { +class CheckIfCanToggleNotificationsViaAccountDataUseCaseTest { private val fakeSession = FakeSession() - private val checkIfCanTogglePushNotificationsViaAccountDataUseCase = - CheckIfCanTogglePushNotificationsViaAccountDataUseCase() + private val checkIfCanToggleNotificationsViaAccountDataUseCase = + CheckIfCanToggleNotificationsViaAccountDataUseCase() @Test fun `given current session and an account data with a content for the device id when execute then result is true`() { @@ -44,7 +44,7 @@ class CheckIfCanTogglePushNotificationsViaAccountDataUseCaseTest { ) // When - val result = checkIfCanTogglePushNotificationsViaAccountDataUseCase.execute(fakeSession, A_DEVICE_ID) + val result = checkIfCanToggleNotificationsViaAccountDataUseCase.execute(fakeSession, A_DEVICE_ID) // Then result shouldBeEqualTo true @@ -61,7 +61,7 @@ class CheckIfCanTogglePushNotificationsViaAccountDataUseCaseTest { ) // When - val result = checkIfCanTogglePushNotificationsViaAccountDataUseCase.execute(fakeSession, A_DEVICE_ID) + val result = checkIfCanToggleNotificationsViaAccountDataUseCase.execute(fakeSession, A_DEVICE_ID) // Then result shouldBeEqualTo false @@ -78,7 +78,7 @@ class CheckIfCanTogglePushNotificationsViaAccountDataUseCaseTest { ) // When - val result = checkIfCanTogglePushNotificationsViaAccountDataUseCase.execute(fakeSession, A_DEVICE_ID) + val result = checkIfCanToggleNotificationsViaAccountDataUseCase.execute(fakeSession, A_DEVICE_ID) // Then result shouldBeEqualTo false diff --git a/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/CheckIfCanTogglePushNotificationsViaPusherUseCaseTest.kt b/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/CheckIfCanToggleNotificationsViaPusherUseCaseTest.kt similarity index 82% rename from vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/CheckIfCanTogglePushNotificationsViaPusherUseCaseTest.kt rename to vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/CheckIfCanToggleNotificationsViaPusherUseCaseTest.kt index 508a05acd6..64beb7b8e2 100644 --- a/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/CheckIfCanTogglePushNotificationsViaPusherUseCaseTest.kt +++ b/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/CheckIfCanToggleNotificationsViaPusherUseCaseTest.kt @@ -23,12 +23,12 @@ import org.junit.Test private val A_HOMESERVER_CAPABILITIES = aHomeServerCapabilities(canRemotelyTogglePushNotificationsOfDevices = true) -class CheckIfCanTogglePushNotificationsViaPusherUseCaseTest { +class CheckIfCanToggleNotificationsViaPusherUseCaseTest { private val fakeSession = FakeSession() - private val checkIfCanTogglePushNotificationsViaPusherUseCase = - CheckIfCanTogglePushNotificationsViaPusherUseCase() + private val checkIfCanToggleNotificationsViaPusherUseCase = + CheckIfCanToggleNotificationsViaPusherUseCase() @Test fun `given current session when execute then toggle capability is returned`() { @@ -38,7 +38,7 @@ class CheckIfCanTogglePushNotificationsViaPusherUseCaseTest { .givenCapabilities(A_HOMESERVER_CAPABILITIES) // When - val result = checkIfCanTogglePushNotificationsViaPusherUseCase.execute(fakeSession) + val result = checkIfCanToggleNotificationsViaPusherUseCase.execute(fakeSession) // Then result shouldBeEqualTo A_HOMESERVER_CAPABILITIES.canRemotelyTogglePushNotificationsOfDevices diff --git a/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/GetNotificationsStatusUseCaseTest.kt b/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/GetNotificationsStatusUseCaseTest.kt index b38367b098..7c0b0a8693 100644 --- a/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/GetNotificationsStatusUseCaseTest.kt +++ b/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/GetNotificationsStatusUseCaseTest.kt @@ -46,15 +46,15 @@ class GetNotificationsStatusUseCaseTest { val instantTaskExecutorRule = InstantTaskExecutorRule() private val fakeSession = FakeSession() - private val fakeCheckIfCanTogglePushNotificationsViaAccountDataUseCase = - mockk() - private val fakeCanTogglePushNotificationsViaPusherUseCase = - mockk() + private val fakeCheckIfCanToggleNotificationsViaAccountDataUseCase = + mockk() + private val fakeCanToggleNotificationsViaPusherUseCase = + mockk() private val getNotificationsStatusUseCase = GetNotificationsStatusUseCase( - checkIfCanTogglePushNotificationsViaAccountDataUseCase = fakeCheckIfCanTogglePushNotificationsViaAccountDataUseCase, - canTogglePushNotificationsViaPusherUseCase = fakeCanTogglePushNotificationsViaPusherUseCase, + checkIfCanTogglePushNotificationsViaAccountDataUseCase = fakeCheckIfCanToggleNotificationsViaAccountDataUseCase, + canTogglePushNotificationsViaPusherUseCase = fakeCanToggleNotificationsViaPusherUseCase, ) @Before @@ -70,8 +70,8 @@ class GetNotificationsStatusUseCaseTest { @Test fun `given current session and toggle is not supported when execute then resulting flow contains NOT_SUPPORTED value`() = runTest { // Given - every { fakeCheckIfCanTogglePushNotificationsViaAccountDataUseCase.execute(fakeSession, A_DEVICE_ID) } returns false - every { fakeCanTogglePushNotificationsViaPusherUseCase.execute(fakeSession) } returns flowOf(false) + every { fakeCheckIfCanToggleNotificationsViaAccountDataUseCase.execute(fakeSession, A_DEVICE_ID) } returns false + every { fakeCanToggleNotificationsViaPusherUseCase.execute(fakeSession) } returns flowOf(false) // When val result = getNotificationsStatusUseCase.execute(fakeSession, A_DEVICE_ID) @@ -80,8 +80,8 @@ class GetNotificationsStatusUseCaseTest { result.firstOrNull() shouldBeEqualTo NotificationsStatus.NOT_SUPPORTED verifyOrder { // we should first check account data - fakeCheckIfCanTogglePushNotificationsViaAccountDataUseCase.execute(fakeSession, A_DEVICE_ID) - fakeCanTogglePushNotificationsViaPusherUseCase.execute(fakeSession) + fakeCheckIfCanToggleNotificationsViaAccountDataUseCase.execute(fakeSession, A_DEVICE_ID) + fakeCanToggleNotificationsViaPusherUseCase.execute(fakeSession) } } @@ -95,8 +95,8 @@ class GetNotificationsStatusUseCaseTest { ) ) fakeSession.pushersService().givenPushersLive(pushers) - every { fakeCheckIfCanTogglePushNotificationsViaAccountDataUseCase.execute(fakeSession, A_DEVICE_ID) } returns false - every { fakeCanTogglePushNotificationsViaPusherUseCase.execute(fakeSession) } returns flowOf(true) + every { fakeCheckIfCanToggleNotificationsViaAccountDataUseCase.execute(fakeSession, A_DEVICE_ID) } returns false + every { fakeCanToggleNotificationsViaPusherUseCase.execute(fakeSession) } returns flowOf(true) // When val result = getNotificationsStatusUseCase.execute(fakeSession, A_DEVICE_ID) @@ -116,8 +116,8 @@ class GetNotificationsStatusUseCaseTest { isSilenced = false ).toContent(), ) - every { fakeCheckIfCanTogglePushNotificationsViaAccountDataUseCase.execute(fakeSession, A_DEVICE_ID) } returns true - every { fakeCanTogglePushNotificationsViaPusherUseCase.execute(fakeSession) } returns flowOf(false) + every { fakeCheckIfCanToggleNotificationsViaAccountDataUseCase.execute(fakeSession, A_DEVICE_ID) } returns true + every { fakeCanToggleNotificationsViaPusherUseCase.execute(fakeSession) } returns flowOf(false) // When val result = getNotificationsStatusUseCase.execute(fakeSession, A_DEVICE_ID) diff --git a/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/TogglePushNotificationUseCaseTest.kt b/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/ToggleNotificationUseCaseTest.kt similarity index 70% rename from vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/TogglePushNotificationUseCaseTest.kt rename to vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/ToggleNotificationUseCaseTest.kt index e443dc3e94..99be7c7ea9 100644 --- a/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/TogglePushNotificationUseCaseTest.kt +++ b/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/ToggleNotificationUseCaseTest.kt @@ -26,21 +26,21 @@ import kotlinx.coroutines.test.runTest import org.junit.Test import org.matrix.android.sdk.api.account.LocalNotificationSettingsContent -class TogglePushNotificationUseCaseTest { +class ToggleNotificationUseCaseTest { private val activeSessionHolder = FakeActiveSessionHolder() - private val fakeCheckIfCanTogglePushNotificationsViaPusherUseCase = - mockk() - private val fakeCheckIfCanTogglePushNotificationsViaAccountDataUseCase = - mockk() + private val fakeCheckIfCanToggleNotificationsViaPusherUseCase = + mockk() + private val fakeCheckIfCanToggleNotificationsViaAccountDataUseCase = + mockk() private val fakeSetNotificationSettingsAccountDataUseCase = mockk() - private val togglePushNotificationUseCase = - TogglePushNotificationUseCase( + private val toggleNotificationUseCase = + ToggleNotificationUseCase( activeSessionHolder = activeSessionHolder.instance, - checkIfCanTogglePushNotificationsViaPusherUseCase = fakeCheckIfCanTogglePushNotificationsViaPusherUseCase, - checkIfCanTogglePushNotificationsViaAccountDataUseCase = fakeCheckIfCanTogglePushNotificationsViaAccountDataUseCase, + checkIfCanTogglePushNotificationsViaPusherUseCase = fakeCheckIfCanToggleNotificationsViaPusherUseCase, + checkIfCanTogglePushNotificationsViaAccountDataUseCase = fakeCheckIfCanToggleNotificationsViaAccountDataUseCase, setNotificationSettingsAccountDataUseCase = fakeSetNotificationSettingsAccountDataUseCase, ) @@ -55,11 +55,11 @@ class TogglePushNotificationUseCaseTest { val fakeSession = activeSessionHolder.fakeSession fakeSession.pushersService().givenPushersLive(pushers) fakeSession.pushersService().givenGetPushers(pushers) - every { fakeCheckIfCanTogglePushNotificationsViaPusherUseCase.execute(fakeSession) } returns true - every { fakeCheckIfCanTogglePushNotificationsViaAccountDataUseCase.execute(fakeSession, sessionId) } returns false + every { fakeCheckIfCanToggleNotificationsViaPusherUseCase.execute(fakeSession) } returns true + every { fakeCheckIfCanToggleNotificationsViaAccountDataUseCase.execute(fakeSession, sessionId) } returns false // When - togglePushNotificationUseCase.execute(sessionId, true) + toggleNotificationUseCase.execute(sessionId, true) // Then activeSessionHolder.fakeSession.pushersService().verifyTogglePusherCalled(pushers.first(), true) @@ -70,15 +70,15 @@ class TogglePushNotificationUseCaseTest { // Given val sessionId = "a_session_id" val fakeSession = activeSessionHolder.fakeSession - every { fakeCheckIfCanTogglePushNotificationsViaPusherUseCase.execute(fakeSession) } returns false - every { fakeCheckIfCanTogglePushNotificationsViaAccountDataUseCase.execute(fakeSession, sessionId) } returns true + every { fakeCheckIfCanToggleNotificationsViaPusherUseCase.execute(fakeSession) } returns false + every { fakeCheckIfCanToggleNotificationsViaAccountDataUseCase.execute(fakeSession, sessionId) } returns true coJustRun { fakeSetNotificationSettingsAccountDataUseCase.execute(any(), any()) } val expectedLocalNotificationSettingsContent = LocalNotificationSettingsContent( isSilenced = false ) // When - togglePushNotificationUseCase.execute(sessionId, true) + toggleNotificationUseCase.execute(sessionId, true) // Then coVerify { diff --git a/vector/src/test/java/im/vector/app/features/settings/notifications/DisableNotificationsForCurrentSessionUseCaseTest.kt b/vector/src/test/java/im/vector/app/features/settings/notifications/DisableNotificationsForCurrentSessionUseCaseTest.kt index a84aa4b055..153b79f1a8 100644 --- a/vector/src/test/java/im/vector/app/features/settings/notifications/DisableNotificationsForCurrentSessionUseCaseTest.kt +++ b/vector/src/test/java/im/vector/app/features/settings/notifications/DisableNotificationsForCurrentSessionUseCaseTest.kt @@ -16,6 +16,8 @@ package im.vector.app.features.settings.notifications +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 @@ -34,15 +36,15 @@ class DisableNotificationsForCurrentSessionUseCaseTest { private val fakeActiveSessionHolder = FakeActiveSessionHolder() private val fakePushersManager = FakePushersManager() - private val fakeCheckIfCanTogglePushNotificationsViaPusherUseCase = mockk() - private val fakeTogglePushNotificationUseCase = mockk() + private val fakeCheckIfCanToggleNotificationsViaPusherUseCase = mockk() + private val fakeToggleNotificationUseCase = mockk() private val fakeUnregisterUnifiedPushUseCase = mockk() private val disableNotificationsForCurrentSessionUseCase = DisableNotificationsForCurrentSessionUseCase( activeSessionHolder = fakeActiveSessionHolder.instance, pushersManager = fakePushersManager.instance, - checkIfCanTogglePushNotificationsViaPusherUseCase = fakeCheckIfCanTogglePushNotificationsViaPusherUseCase, - togglePushNotificationUseCase = fakeTogglePushNotificationUseCase, + checkIfCanToggleNotificationsViaPusherUseCase = fakeCheckIfCanToggleNotificationsViaPusherUseCase, + toggleNotificationUseCase = fakeToggleNotificationUseCase, unregisterUnifiedPushUseCase = fakeUnregisterUnifiedPushUseCase, ) @@ -51,14 +53,14 @@ class DisableNotificationsForCurrentSessionUseCaseTest { // Given val fakeSession = fakeActiveSessionHolder.fakeSession fakeSession.givenSessionId(A_SESSION_ID) - every { fakeCheckIfCanTogglePushNotificationsViaPusherUseCase.execute(fakeSession) } returns true - coJustRun { fakeTogglePushNotificationUseCase.execute(A_SESSION_ID, any()) } + every { fakeCheckIfCanToggleNotificationsViaPusherUseCase.execute(fakeSession) } returns true + coJustRun { fakeToggleNotificationUseCase.execute(A_SESSION_ID, any()) } // When disableNotificationsForCurrentSessionUseCase.execute() // Then - coVerify { fakeTogglePushNotificationUseCase.execute(A_SESSION_ID, false) } + coVerify { fakeToggleNotificationUseCase.execute(A_SESSION_ID, false) } } @Test @@ -66,8 +68,8 @@ class DisableNotificationsForCurrentSessionUseCaseTest { // Given val fakeSession = fakeActiveSessionHolder.fakeSession fakeSession.givenSessionId(A_SESSION_ID) - every { fakeCheckIfCanTogglePushNotificationsViaPusherUseCase.execute(fakeSession) } returns false - coJustRun { fakeTogglePushNotificationUseCase.execute(A_SESSION_ID, any()) } + every { fakeCheckIfCanToggleNotificationsViaPusherUseCase.execute(fakeSession) } returns false + coJustRun { fakeToggleNotificationUseCase.execute(A_SESSION_ID, any()) } coJustRun { fakeUnregisterUnifiedPushUseCase.execute(any()) } // When @@ -75,7 +77,7 @@ class DisableNotificationsForCurrentSessionUseCaseTest { // Then coVerify { - fakeTogglePushNotificationUseCase.execute(A_SESSION_ID, false) + fakeToggleNotificationUseCase.execute(A_SESSION_ID, false) fakeUnregisterUnifiedPushUseCase.execute(fakePushersManager.instance) } } diff --git a/vector/src/test/java/im/vector/app/features/settings/notifications/EnableNotificationsForCurrentSessionUseCaseTest.kt b/vector/src/test/java/im/vector/app/features/settings/notifications/EnableNotificationsForCurrentSessionUseCaseTest.kt index c923f0c7d6..f10b0777cb 100644 --- a/vector/src/test/java/im/vector/app/features/settings/notifications/EnableNotificationsForCurrentSessionUseCaseTest.kt +++ b/vector/src/test/java/im/vector/app/features/settings/notifications/EnableNotificationsForCurrentSessionUseCaseTest.kt @@ -18,7 +18,7 @@ package im.vector.app.features.settings.notifications import im.vector.app.core.pushers.EnsureFcmTokenIsRetrievedUseCase import im.vector.app.core.pushers.RegisterUnifiedPushUseCase -import im.vector.app.features.settings.devices.v2.notification.TogglePushNotificationUseCase +import im.vector.app.features.settings.devices.v2.notification.ToggleNotificationUseCase import im.vector.app.test.fakes.FakeActiveSessionHolder import im.vector.app.test.fakes.FakePushersManager import io.mockk.coJustRun @@ -36,14 +36,14 @@ class EnableNotificationsForCurrentSessionUseCaseTest { private val fakeActiveSessionHolder = FakeActiveSessionHolder() private val fakePushersManager = FakePushersManager() - private val fakeTogglePushNotificationUseCase = mockk() + private val fakeToggleNotificationUseCase = mockk() private val fakeRegisterUnifiedPushUseCase = mockk() private val fakeEnsureFcmTokenIsRetrievedUseCase = mockk() private val enableNotificationsForCurrentSessionUseCase = EnableNotificationsForCurrentSessionUseCase( activeSessionHolder = fakeActiveSessionHolder.instance, pushersManager = fakePushersManager.instance, - togglePushNotificationUseCase = fakeTogglePushNotificationUseCase, + toggleNotificationUseCase = fakeToggleNotificationUseCase, registerUnifiedPushUseCase = fakeRegisterUnifiedPushUseCase, ensureFcmTokenIsRetrievedUseCase = fakeEnsureFcmTokenIsRetrievedUseCase, ) @@ -57,7 +57,7 @@ class EnableNotificationsForCurrentSessionUseCaseTest { fakePushersManager.givenGetPusherForCurrentSessionReturns(null) every { fakeRegisterUnifiedPushUseCase.execute(any()) } returns RegisterUnifiedPushUseCase.RegisterUnifiedPushResult.Success justRun { fakeEnsureFcmTokenIsRetrievedUseCase.execute(any(), any()) } - coJustRun { fakeTogglePushNotificationUseCase.execute(A_SESSION_ID, any()) } + coJustRun { fakeToggleNotificationUseCase.execute(A_SESSION_ID, any()) } // When val result = enableNotificationsForCurrentSessionUseCase.execute(aDistributor) diff --git a/vector/src/test/java/im/vector/app/test/fakes/FakeTogglePushNotificationUseCase.kt b/vector/src/test/java/im/vector/app/test/fakes/FakeTogglePushNotificationUseCase.kt index bfbbb87705..cc42193fe1 100644 --- a/vector/src/test/java/im/vector/app/test/fakes/FakeTogglePushNotificationUseCase.kt +++ b/vector/src/test/java/im/vector/app/test/fakes/FakeTogglePushNotificationUseCase.kt @@ -16,14 +16,14 @@ package im.vector.app.test.fakes -import im.vector.app.features.settings.devices.v2.notification.TogglePushNotificationUseCase +import im.vector.app.features.settings.devices.v2.notification.ToggleNotificationUseCase import io.mockk.coJustRun import io.mockk.coVerify import io.mockk.mockk class FakeTogglePushNotificationUseCase { - val instance = mockk { + val instance = mockk { coJustRun { execute(any(), any()) } } From ab6a6b53c88b4336bb3b96edced8beb852399c1f Mon Sep 17 00:00:00 2001 From: Maxime NATUREL Date: Fri, 18 Nov 2022 15:19:34 +0100 Subject: [PATCH 08/21] Some refactorings + update unit tests --- .../ConfigureAndStartSessionUseCase.kt | 17 +++- ...oggleNotificationsViaAccountDataUseCase.kt | 14 +-- ...eNotificationSettingsAccountDataUseCase.kt | 11 ++- ...tNotificationSettingsAccountDataUseCase.kt | 34 +++++++ ...tNotificationSettingsAccountDataUseCase.kt | 9 +- .../notification/ToggleNotificationUseCase.kt | 2 +- ...NotificationSettingsAccountDataUseCase.kt} | 21 ++-- .../ConfigureAndStartSessionUseCaseTest.kt | 56 +++++++---- ...eNotificationsViaAccountDataUseCaseTest.kt | 32 +++--- ...ificationSettingsAccountDataUseCaseTest.kt | 59 ----------- ...ificationSettingsAccountDataUseCaseTest.kt | 9 +- ...ificationSettingsAccountDataUseCaseTest.kt | 49 ++++++++++ .../GetNotificationsStatusUseCaseTest.kt | 4 +- ...ificationSettingsAccountDataUseCaseTest.kt | 14 +-- .../ToggleNotificationUseCaseTest.kt | 8 +- ...ificationSettingsAccountDataUseCaseTest.kt | 97 +++++++++++++++++++ .../overview/SessionOverviewViewModelTest.kt | 8 +- ...tificationsForCurrentSessionUseCaseTest.kt | 4 +- ...se.kt => FakeToggleNotificationUseCase.kt} | 2 +- .../app/test/fakes/FakeVectorPreferences.kt | 4 + 20 files changed, 300 insertions(+), 154 deletions(-) create mode 100644 vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/GetNotificationSettingsAccountDataUseCase.kt rename vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/{CreateNotificationSettingsAccountDataUseCase.kt => UpdateNotificationSettingsAccountDataUseCase.kt} (57%) delete mode 100644 vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/CreateNotificationSettingsAccountDataUseCaseTest.kt create mode 100644 vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/GetNotificationSettingsAccountDataUseCaseTest.kt create mode 100644 vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/UpdateNotificationSettingsAccountDataUseCaseTest.kt rename vector/src/test/java/im/vector/app/test/fakes/{FakeTogglePushNotificationUseCase.kt => FakeToggleNotificationUseCase.kt} (96%) diff --git a/vector/src/main/java/im/vector/app/core/session/ConfigureAndStartSessionUseCase.kt b/vector/src/main/java/im/vector/app/core/session/ConfigureAndStartSessionUseCase.kt index 96c3f8a6ce..d9688a45ed 100644 --- a/vector/src/main/java/im/vector/app/core/session/ConfigureAndStartSessionUseCase.kt +++ b/vector/src/main/java/im/vector/app/core/session/ConfigureAndStartSessionUseCase.kt @@ -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) + } + } } } diff --git a/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/CheckIfCanToggleNotificationsViaAccountDataUseCase.kt b/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/CheckIfCanToggleNotificationsViaAccountDataUseCase.kt index b006e3da45..58289495a4 100644 --- a/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/CheckIfCanToggleNotificationsViaAccountDataUseCase.kt +++ b/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/CheckIfCanToggleNotificationsViaAccountDataUseCase.kt @@ -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() - ?.isSilenced != null + return getNotificationSettingsAccountDataUseCase.execute(session, deviceId)?.isSilenced != null } } diff --git a/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/DeleteNotificationSettingsAccountDataUseCase.kt b/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/DeleteNotificationSettingsAccountDataUseCase.kt index 51c24e500c..d71eebdf8a 100644 --- a/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/DeleteNotificationSettingsAccountDataUseCase.kt +++ b/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/DeleteNotificationSettingsAccountDataUseCase.kt @@ -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) } } diff --git a/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/GetNotificationSettingsAccountDataUseCase.kt b/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/GetNotificationSettingsAccountDataUseCase.kt new file mode 100644 index 0000000000..5517fa0978 --- /dev/null +++ b/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/GetNotificationSettingsAccountDataUseCase.kt @@ -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() + } +} diff --git a/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/SetNotificationSettingsAccountDataUseCase.kt b/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/SetNotificationSettingsAccountDataUseCase.kt index f0ec9d5ddc..7306794f16 100644 --- a/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/SetNotificationSettingsAccountDataUseCase.kt +++ b/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/SetNotificationSettingsAccountDataUseCase.kt @@ -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(), diff --git a/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/ToggleNotificationUseCase.kt b/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/ToggleNotificationUseCase.kt index d0e1ea2a7a..73a81a6de1 100644 --- a/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/ToggleNotificationUseCase.kt +++ b/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/ToggleNotificationUseCase.kt @@ -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) } } } diff --git a/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/CreateNotificationSettingsAccountDataUseCase.kt b/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/UpdateNotificationSettingsAccountDataUseCase.kt similarity index 57% rename from vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/CreateNotificationSettingsAccountDataUseCase.kt rename to vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/UpdateNotificationSettingsAccountDataUseCase.kt index e2ee19e5cd..596be90abb 100644 --- a/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/CreateNotificationSettingsAccountDataUseCase.kt +++ b/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/UpdateNotificationSettingsAccountDataUseCase.kt @@ -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) + } } } diff --git a/vector/src/test/java/im/vector/app/core/session/ConfigureAndStartSessionUseCaseTest.kt b/vector/src/test/java/im/vector/app/core/session/ConfigureAndStartSessionUseCaseTest.kt index 01596e796d..23a3629efe 100644 --- a/vector/src/test/java/im/vector/app/core/session/ConfigureAndStartSessionUseCaseTest.kt +++ b/vector/src/test/java/im/vector/app/core/session/ConfigureAndStartSessionUseCaseTest.kt @@ -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() private val fakeVectorPreferences = FakeVectorPreferences() private val fakeEnableNotificationsSettingUpdater = FakeEnableNotificationsSettingUpdater() + private val fakeUpdateNotificationSettingsAccountDataUseCase = mockk() 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 { diff --git a/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/CheckIfCanToggleNotificationsViaAccountDataUseCaseTest.kt b/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/CheckIfCanToggleNotificationsViaAccountDataUseCaseTest.kt index de225d36ac..f97e326a02 100644 --- a/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/CheckIfCanToggleNotificationsViaAccountDataUseCaseTest.kt +++ b/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/CheckIfCanToggleNotificationsViaAccountDataUseCaseTest.kt @@ -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() 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) diff --git a/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/CreateNotificationSettingsAccountDataUseCaseTest.kt b/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/CreateNotificationSettingsAccountDataUseCaseTest.kt deleted file mode 100644 index e4cadaa005..0000000000 --- a/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/CreateNotificationSettingsAccountDataUseCaseTest.kt +++ /dev/null @@ -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() - - 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) } - } -} diff --git a/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/DeleteNotificationSettingsAccountDataUseCaseTest.kt b/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/DeleteNotificationSettingsAccountDataUseCaseTest.kt index 038a1f436a..d84ff8c6ac 100644 --- a/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/DeleteNotificationSettingsAccountDataUseCaseTest.kt +++ b/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/DeleteNotificationSettingsAccountDataUseCaseTest.kt @@ -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) } } } diff --git a/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/GetNotificationSettingsAccountDataUseCaseTest.kt b/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/GetNotificationSettingsAccountDataUseCaseTest.kt new file mode 100644 index 0000000000..75179b5679 --- /dev/null +++ b/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/GetNotificationSettingsAccountDataUseCaseTest.kt @@ -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 + } +} diff --git a/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/GetNotificationsStatusUseCaseTest.kt b/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/GetNotificationsStatusUseCaseTest.kt index 7c0b0a8693..d4c3aa5788 100644 --- a/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/GetNotificationsStatusUseCaseTest.kt +++ b/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/GetNotificationsStatusUseCaseTest.kt @@ -53,8 +53,8 @@ class GetNotificationsStatusUseCaseTest { private val getNotificationsStatusUseCase = GetNotificationsStatusUseCase( - checkIfCanTogglePushNotificationsViaAccountDataUseCase = fakeCheckIfCanToggleNotificationsViaAccountDataUseCase, - canTogglePushNotificationsViaPusherUseCase = fakeCanToggleNotificationsViaPusherUseCase, + checkIfCanToggleNotificationsViaAccountDataUseCase = fakeCheckIfCanToggleNotificationsViaAccountDataUseCase, + canToggleNotificationsViaPusherUseCase = fakeCanToggleNotificationsViaPusherUseCase, ) @Before diff --git a/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/SetNotificationSettingsAccountDataUseCaseTest.kt b/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/SetNotificationSettingsAccountDataUseCaseTest.kt index 8f72f0946f..d26271e59d 100644 --- a/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/SetNotificationSettingsAccountDataUseCaseTest.kt +++ b/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/SetNotificationSettingsAccountDataUseCaseTest.kt @@ -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(), ) diff --git a/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/ToggleNotificationUseCaseTest.kt b/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/ToggleNotificationUseCaseTest.kt index 99be7c7ea9..1e3517c776 100644 --- a/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/ToggleNotificationUseCaseTest.kt +++ b/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/ToggleNotificationUseCaseTest.kt @@ -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) } } } diff --git a/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/UpdateNotificationSettingsAccountDataUseCaseTest.kt b/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/UpdateNotificationSettingsAccountDataUseCaseTest.kt new file mode 100644 index 0000000000..41c5ab9081 --- /dev/null +++ b/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/UpdateNotificationSettingsAccountDataUseCaseTest.kt @@ -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() + private val fakeSetNotificationSettingsAccountDataUseCase = mockk() + + 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) } + } +} diff --git a/vector/src/test/java/im/vector/app/features/settings/devices/v2/overview/SessionOverviewViewModelTest.kt b/vector/src/test/java/im/vector/app/features/settings/devices/v2/overview/SessionOverviewViewModelTest.kt index 287bdd159c..901c0331c5 100644 --- a/vector/src/test/java/im/vector/app/features/settings/devices/v2/overview/SessionOverviewViewModelTest.kt +++ b/vector/src/test/java/im/vector/app/features/settings/devices/v2/overview/SessionOverviewViewModelTest.kt @@ -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() private val fakePendingAuthHandler = FakePendingAuthHandler() private val refreshDevicesUseCase = mockk(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() } } diff --git a/vector/src/test/java/im/vector/app/features/settings/notifications/DisableNotificationsForCurrentSessionUseCaseTest.kt b/vector/src/test/java/im/vector/app/features/settings/notifications/DisableNotificationsForCurrentSessionUseCaseTest.kt index 153b79f1a8..e53874858a 100644 --- a/vector/src/test/java/im/vector/app/features/settings/notifications/DisableNotificationsForCurrentSessionUseCaseTest.kt +++ b/vector/src/test/java/im/vector/app/features/settings/notifications/DisableNotificationsForCurrentSessionUseCaseTest.kt @@ -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 diff --git a/vector/src/test/java/im/vector/app/test/fakes/FakeTogglePushNotificationUseCase.kt b/vector/src/test/java/im/vector/app/test/fakes/FakeToggleNotificationUseCase.kt similarity index 96% rename from vector/src/test/java/im/vector/app/test/fakes/FakeTogglePushNotificationUseCase.kt rename to vector/src/test/java/im/vector/app/test/fakes/FakeToggleNotificationUseCase.kt index cc42193fe1..527625144e 100644 --- a/vector/src/test/java/im/vector/app/test/fakes/FakeTogglePushNotificationUseCase.kt +++ b/vector/src/test/java/im/vector/app/test/fakes/FakeToggleNotificationUseCase.kt @@ -21,7 +21,7 @@ import io.mockk.coJustRun import io.mockk.coVerify import io.mockk.mockk -class FakeTogglePushNotificationUseCase { +class FakeToggleNotificationUseCase { val instance = mockk { coJustRun { execute(any(), any()) } diff --git a/vector/src/test/java/im/vector/app/test/fakes/FakeVectorPreferences.kt b/vector/src/test/java/im/vector/app/test/fakes/FakeVectorPreferences.kt index 06efca1bf7..58bc1a18b8 100644 --- a/vector/src/test/java/im/vector/app/test/fakes/FakeVectorPreferences.kt +++ b/vector/src/test/java/im/vector/app/test/fakes/FakeVectorPreferences.kt @@ -73,4 +73,8 @@ class FakeVectorPreferences { fun givenAreNotificationsEnabledForDevice(notificationsEnabled: Boolean) { every { instance.areNotificationEnabledForDevice() } returns notificationsEnabled } + + fun givenIsBackgroundSyncEnabled(isEnabled: Boolean) { + every { instance.isBackgroundSyncEnabled() } returns isEnabled + } } From e99dc1d163008123520b5e9cfb58d7ac7795fef9 Mon Sep 17 00:00:00 2001 From: Maxime NATUREL Date: Fri, 18 Nov 2022 17:17:05 +0100 Subject: [PATCH 09/21] Remove unused parameters from some ViewModel --- .../vector/app/features/settings/devices/v2/DevicesViewModel.kt | 1 - .../settings/devices/v2/overview/SessionOverviewViewModel.kt | 1 - .../app/features/settings/devices/v2/DevicesViewModelTest.kt | 2 -- .../devices/v2/overview/SessionOverviewViewModelTest.kt | 2 -- 4 files changed, 6 deletions(-) diff --git a/vector/src/main/java/im/vector/app/features/settings/devices/v2/DevicesViewModel.kt b/vector/src/main/java/im/vector/app/features/settings/devices/v2/DevicesViewModel.kt index f42d5af398..bfccd2f9d3 100644 --- a/vector/src/main/java/im/vector/app/features/settings/devices/v2/DevicesViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/settings/devices/v2/DevicesViewModel.kt @@ -48,7 +48,6 @@ class DevicesViewModel @AssistedInject constructor( private val refreshDevicesOnCryptoDevicesChangeUseCase: RefreshDevicesOnCryptoDevicesChangeUseCase, private val checkIfCurrentSessionCanBeVerifiedUseCase: CheckIfCurrentSessionCanBeVerifiedUseCase, private val signoutSessionsUseCase: SignoutSessionsUseCase, - private val interceptSignoutFlowResponseUseCase: InterceptSignoutFlowResponseUseCase, private val pendingAuthHandler: PendingAuthHandler, refreshDevicesUseCase: RefreshDevicesUseCase, private val vectorPreferences: VectorPreferences, diff --git a/vector/src/main/java/im/vector/app/features/settings/devices/v2/overview/SessionOverviewViewModel.kt b/vector/src/main/java/im/vector/app/features/settings/devices/v2/overview/SessionOverviewViewModel.kt index 0ddf688514..74f962b464 100644 --- a/vector/src/main/java/im/vector/app/features/settings/devices/v2/overview/SessionOverviewViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/settings/devices/v2/overview/SessionOverviewViewModel.kt @@ -51,7 +51,6 @@ class SessionOverviewViewModel @AssistedInject constructor( private val getDeviceFullInfoUseCase: GetDeviceFullInfoUseCase, private val checkIfCurrentSessionCanBeVerifiedUseCase: CheckIfCurrentSessionCanBeVerifiedUseCase, private val signoutSessionsUseCase: SignoutSessionsUseCase, - private val interceptSignoutFlowResponseUseCase: InterceptSignoutFlowResponseUseCase, private val pendingAuthHandler: PendingAuthHandler, private val activeSessionHolder: ActiveSessionHolder, private val toggleNotificationUseCase: ToggleNotificationUseCase, diff --git a/vector/src/test/java/im/vector/app/features/settings/devices/v2/DevicesViewModelTest.kt b/vector/src/test/java/im/vector/app/features/settings/devices/v2/DevicesViewModelTest.kt index 03177aac47..aa5ebd73eb 100644 --- a/vector/src/test/java/im/vector/app/features/settings/devices/v2/DevicesViewModelTest.kt +++ b/vector/src/test/java/im/vector/app/features/settings/devices/v2/DevicesViewModelTest.kt @@ -72,7 +72,6 @@ class DevicesViewModelTest { private val refreshDevicesOnCryptoDevicesChangeUseCase = mockk(relaxed = true) private val checkIfCurrentSessionCanBeVerifiedUseCase = mockk() private val fakeSignoutSessionsUseCase = FakeSignoutSessionsUseCase() - private val fakeInterceptSignoutFlowResponseUseCase = mockk() private val fakePendingAuthHandler = FakePendingAuthHandler() private val fakeRefreshDevicesUseCase = mockk(relaxUnitFun = true) private val fakeVectorPreferences = FakeVectorPreferences() @@ -87,7 +86,6 @@ class DevicesViewModelTest { refreshDevicesOnCryptoDevicesChangeUseCase = refreshDevicesOnCryptoDevicesChangeUseCase, checkIfCurrentSessionCanBeVerifiedUseCase = checkIfCurrentSessionCanBeVerifiedUseCase, signoutSessionsUseCase = fakeSignoutSessionsUseCase.instance, - interceptSignoutFlowResponseUseCase = fakeInterceptSignoutFlowResponseUseCase, pendingAuthHandler = fakePendingAuthHandler.instance, refreshDevicesUseCase = fakeRefreshDevicesUseCase, vectorPreferences = fakeVectorPreferences.instance, diff --git a/vector/src/test/java/im/vector/app/features/settings/devices/v2/overview/SessionOverviewViewModelTest.kt b/vector/src/test/java/im/vector/app/features/settings/devices/v2/overview/SessionOverviewViewModelTest.kt index 901c0331c5..2ddc91cdac 100644 --- a/vector/src/test/java/im/vector/app/features/settings/devices/v2/overview/SessionOverviewViewModelTest.kt +++ b/vector/src/test/java/im/vector/app/features/settings/devices/v2/overview/SessionOverviewViewModelTest.kt @@ -73,7 +73,6 @@ class SessionOverviewViewModelTest { private val fakeActiveSessionHolder = FakeActiveSessionHolder() private val checkIfCurrentSessionCanBeVerifiedUseCase = mockk() private val fakeSignoutSessionsUseCase = FakeSignoutSessionsUseCase() - private val interceptSignoutFlowResponseUseCase = mockk() private val fakePendingAuthHandler = FakePendingAuthHandler() private val refreshDevicesUseCase = mockk(relaxed = true) private val toggleNotificationUseCase = FakeToggleNotificationUseCase() @@ -87,7 +86,6 @@ class SessionOverviewViewModelTest { getDeviceFullInfoUseCase = getDeviceFullInfoUseCase, checkIfCurrentSessionCanBeVerifiedUseCase = checkIfCurrentSessionCanBeVerifiedUseCase, signoutSessionsUseCase = fakeSignoutSessionsUseCase.instance, - interceptSignoutFlowResponseUseCase = interceptSignoutFlowResponseUseCase, pendingAuthHandler = fakePendingAuthHandler.instance, activeSessionHolder = fakeActiveSessionHolder.instance, refreshDevicesUseCase = refreshDevicesUseCase, From 637961bbb1043fd39ac0844d09eeb65dc7b30f0a Mon Sep 17 00:00:00 2001 From: Maxime NATUREL Date: Wed, 23 Nov 2022 14:45:37 +0100 Subject: [PATCH 10/21] Update related account data event on notification method change --- .../LocalNotificationSettingsContent.kt | 2 +- .../EnableNotificationsSettingUpdater.kt | 39 ---------- .../NotificationsSettingUpdater.kt | 74 +++++++++++++++++++ .../ConfigureAndStartSessionUseCase.kt | 10 +-- ...eNotificationSettingsAccountDataUseCase.kt | 12 +-- ...eNotificationSettingsAccountDataUseCase.kt | 27 +++++-- .../ConfigureAndStartSessionUseCaseTest.kt | 38 +++++----- ...ificationSettingsAccountDataUseCaseTest.kt | 28 ++++++- ...ificationSettingsAccountDataUseCaseTest.kt | 22 +++++- ...ificationSettingsAccountDataUseCaseTest.kt | 2 +- ...ificationSettingsAccountDataUseCaseTest.kt | 32 +++++++- ....kt => FakeNotificationsSettingUpdater.kt} | 6 +- 12 files changed, 207 insertions(+), 85 deletions(-) delete mode 100644 vector/src/main/java/im/vector/app/core/notification/EnableNotificationsSettingUpdater.kt create mode 100644 vector/src/main/java/im/vector/app/core/notification/NotificationsSettingUpdater.kt rename vector/src/test/java/im/vector/app/test/fakes/{FakeEnableNotificationsSettingUpdater.kt => FakeNotificationsSettingUpdater.kt} (82%) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/account/LocalNotificationSettingsContent.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/account/LocalNotificationSettingsContent.kt index 6998d9dcf2..75d04f340a 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/account/LocalNotificationSettingsContent.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/account/LocalNotificationSettingsContent.kt @@ -22,5 +22,5 @@ import com.squareup.moshi.JsonClass @JsonClass(generateAdapter = true) data class LocalNotificationSettingsContent( @Json(name = "is_silenced") - val isSilenced: Boolean? = false + val isSilenced: Boolean? ) diff --git a/vector/src/main/java/im/vector/app/core/notification/EnableNotificationsSettingUpdater.kt b/vector/src/main/java/im/vector/app/core/notification/EnableNotificationsSettingUpdater.kt deleted file mode 100644 index 81b524cde9..0000000000 --- a/vector/src/main/java/im/vector/app/core/notification/EnableNotificationsSettingUpdater.kt +++ /dev/null @@ -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) - } - } -} diff --git a/vector/src/main/java/im/vector/app/core/notification/NotificationsSettingUpdater.kt b/vector/src/main/java/im/vector/app/core/notification/NotificationsSettingUpdater.kt new file mode 100644 index 0000000000..c6738edddb --- /dev/null +++ b/vector/src/main/java/im/vector/app/core/notification/NotificationsSettingUpdater.kt @@ -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) + } + } + } + } +} diff --git a/vector/src/main/java/im/vector/app/core/session/ConfigureAndStartSessionUseCase.kt b/vector/src/main/java/im/vector/app/core/session/ConfigureAndStartSessionUseCase.kt index d9688a45ed..623f7d83a9 100644 --- a/vector/src/main/java/im/vector/app/core/session/ConfigureAndStartSessionUseCase.kt +++ b/vector/src/main/java/im/vector/app/core/session/ConfigureAndStartSessionUseCase.kt @@ -19,7 +19,7 @@ package im.vector.app.core.session import android.content.Context import dagger.hilt.android.qualifiers.ApplicationContext 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.features.call.webrtc.WebRtcCallManager import im.vector.app.features.session.coroutineScope @@ -36,7 +36,7 @@ class ConfigureAndStartSessionUseCase @Inject constructor( private val webRtcCallManager: WebRtcCallManager, private val updateMatrixClientInfoUseCase: UpdateMatrixClientInfoUseCase, private val vectorPreferences: VectorPreferences, - private val enableNotificationsSettingUpdater: EnableNotificationsSettingUpdater, + private val notificationsSettingUpdater: NotificationsSettingUpdater, private val updateNotificationSettingsAccountDataUseCase: UpdateNotificationSettingsAccountDataUseCase, ) { @@ -53,7 +53,7 @@ class ConfigureAndStartSessionUseCase @Inject constructor( webRtcCallManager.checkForProtocolsSupportIfNeeded() updateMatrixClientInfoIfNeeded(session) createNotificationSettingsAccountDataIfNeeded(session) - enableNotificationsSettingUpdater.onSessionsStarted(session) + notificationsSettingUpdater.onSessionsStarted(session) } private fun updateMatrixClientInfoIfNeeded(session: Session) { @@ -66,9 +66,7 @@ class ConfigureAndStartSessionUseCase @Inject constructor( private fun createNotificationSettingsAccountDataIfNeeded(session: Session) { session.coroutineScope.launch { - if (vectorPreferences.isBackgroundSyncEnabled()) { - updateNotificationSettingsAccountDataUseCase.execute(session) - } + updateNotificationSettingsAccountDataUseCase.execute(session) } } } diff --git a/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/DeleteNotificationSettingsAccountDataUseCase.kt b/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/DeleteNotificationSettingsAccountDataUseCase.kt index d71eebdf8a..3c086fe111 100644 --- a/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/DeleteNotificationSettingsAccountDataUseCase.kt +++ b/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/DeleteNotificationSettingsAccountDataUseCase.kt @@ -24,15 +24,17 @@ import javax.inject.Inject * Delete the content of any associated notification settings to the current session. */ class DeleteNotificationSettingsAccountDataUseCase @Inject constructor( + private val getNotificationSettingsAccountDataUseCase: GetNotificationSettingsAccountDataUseCase, private val setNotificationSettingsAccountDataUseCase: SetNotificationSettingsAccountDataUseCase, ) { - // 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(session, deviceId, emptyNotificationSettingsContent) + if (getNotificationSettingsAccountDataUseCase.execute(session, deviceId)?.isSilenced != null) { + val emptyNotificationSettingsContent = LocalNotificationSettingsContent( + isSilenced = null + ) + setNotificationSettingsAccountDataUseCase.execute(session, deviceId, emptyNotificationSettingsContent) + } } } diff --git a/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/UpdateNotificationSettingsAccountDataUseCase.kt b/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/UpdateNotificationSettingsAccountDataUseCase.kt index 596be90abb..7791c1dd4b 100644 --- a/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/UpdateNotificationSettingsAccountDataUseCase.kt +++ b/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/UpdateNotificationSettingsAccountDataUseCase.kt @@ -22,24 +22,37 @@ import org.matrix.android.sdk.api.session.Session 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( private val vectorPreferences: VectorPreferences, 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) { + if (vectorPreferences.isBackgroundSyncEnabled()) { + setCurrentNotificationStatus(session) + } else { + deleteCurrentNotificationStatus(session) + } + } + + private suspend fun setCurrentNotificationStatus(session: Session) { val deviceId = session.sessionParams.deviceId ?: return - val isSilencedLocal = !vectorPreferences.areNotificationEnabledForDevice() - val isSilencedRemote = getNotificationSettingsAccountDataUseCase.execute(session, deviceId)?.isSilenced - if (isSilencedLocal != isSilencedRemote) { + val areNotificationsSilenced = !vectorPreferences.areNotificationEnabledForDevice() + val isSilencedAccountData = getNotificationSettingsAccountDataUseCase.execute(session, deviceId)?.isSilenced + if (areNotificationsSilenced != isSilencedAccountData) { val notificationSettingsContent = LocalNotificationSettingsContent( - isSilenced = isSilencedLocal + isSilenced = areNotificationsSilenced ) setNotificationSettingsAccountDataUseCase.execute(session, deviceId, notificationSettingsContent) } } + + private suspend fun deleteCurrentNotificationStatus(session: Session) { + deleteNotificationSettingsAccountDataUseCase.execute(session) + } } diff --git a/vector/src/test/java/im/vector/app/core/session/ConfigureAndStartSessionUseCaseTest.kt b/vector/src/test/java/im/vector/app/core/session/ConfigureAndStartSessionUseCaseTest.kt index 23a3629efe..4071afaf3f 100644 --- a/vector/src/test/java/im/vector/app/core/session/ConfigureAndStartSessionUseCaseTest.kt +++ b/vector/src/test/java/im/vector/app/core/session/ConfigureAndStartSessionUseCaseTest.kt @@ -22,7 +22,7 @@ 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.FakeNotificationsSettingUpdater import im.vector.app.test.fakes.FakeSession import im.vector.app.test.fakes.FakeVectorPreferences import im.vector.app.test.fakes.FakeWebRtcCallManager @@ -47,7 +47,7 @@ class ConfigureAndStartSessionUseCaseTest { private val fakeWebRtcCallManager = FakeWebRtcCallManager() private val fakeUpdateMatrixClientInfoUseCase = mockk() private val fakeVectorPreferences = FakeVectorPreferences() - private val fakeEnableNotificationsSettingUpdater = FakeEnableNotificationsSettingUpdater() + private val fakeNotificationsSettingUpdater = FakeNotificationsSettingUpdater() private val fakeUpdateNotificationSettingsAccountDataUseCase = mockk() private val configureAndStartSessionUseCase = ConfigureAndStartSessionUseCase( @@ -55,7 +55,7 @@ class ConfigureAndStartSessionUseCaseTest { webRtcCallManager = fakeWebRtcCallManager.instance, updateMatrixClientInfoUseCase = fakeUpdateMatrixClientInfoUseCase, vectorPreferences = fakeVectorPreferences.instance, - enableNotificationsSettingUpdater = fakeEnableNotificationsSettingUpdater.instance, + notificationsSettingUpdater = fakeNotificationsSettingUpdater.instance, updateNotificationSettingsAccountDataUseCase = fakeUpdateNotificationSettingsAccountDataUseCase, ) @@ -71,7 +71,7 @@ class ConfigureAndStartSessionUseCaseTest { } @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 val aSession = givenASession() every { aSession.coroutineScope } returns this @@ -79,8 +79,7 @@ class ConfigureAndStartSessionUseCaseTest { coJustRun { fakeUpdateMatrixClientInfoUseCase.execute(any()) } coJustRun { fakeUpdateNotificationSettingsAccountDataUseCase.execute(any()) } fakeVectorPreferences.givenIsClientInfoRecordingEnabled(isEnabled = true) - fakeVectorPreferences.givenIsBackgroundSyncEnabled(isEnabled = true) - fakeEnableNotificationsSettingUpdater.givenOnSessionsStarted(aSession) + fakeNotificationsSettingUpdater.givenOnSessionsStarted(aSession) // When configureAndStartSessionUseCase.execute(aSession, startSyncing = true) @@ -98,14 +97,14 @@ class ConfigureAndStartSessionUseCaseTest { } @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 val aSession = givenASession() every { aSession.coroutineScope } returns this fakeWebRtcCallManager.givenCheckForProtocolsSupportIfNeededSucceeds() + coJustRun { fakeUpdateNotificationSettingsAccountDataUseCase.execute(any()) } fakeVectorPreferences.givenIsClientInfoRecordingEnabled(isEnabled = false) - fakeVectorPreferences.givenIsBackgroundSyncEnabled(isEnabled = false) - fakeEnableNotificationsSettingUpdater.givenOnSessionsStarted(aSession) + fakeNotificationsSettingUpdater.givenOnSessionsStarted(aSession) // When configureAndStartSessionUseCase.execute(aSession, startSyncing = true) @@ -118,6 +117,8 @@ class ConfigureAndStartSessionUseCaseTest { fakeWebRtcCallManager.verifyCheckForProtocolsSupportIfNeeded() coVerify(inverse = true) { fakeUpdateMatrixClientInfoUseCase.execute(aSession) + } + coVerify { fakeUpdateNotificationSettingsAccountDataUseCase.execute(aSession) } } @@ -125,27 +126,26 @@ class ConfigureAndStartSessionUseCaseTest { @Test fun `given a session and no start sync needed 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) - fakeVectorPreferences.givenIsBackgroundSyncEnabled(isEnabled = true) - fakeEnableNotificationsSettingUpdater.givenOnSessionsStarted(fakeSession) + fakeNotificationsSettingUpdater.givenOnSessionsStarted(aSession) // When - configureAndStartSessionUseCase.execute(fakeSession, startSyncing = false) + configureAndStartSessionUseCase.execute(aSession, startSyncing = false) advanceUntilIdle() // Then - verify(inverse = true) { fakeSession.startSyncing(fakeContext.instance) } - fakeSession.fakeFilterService.verifySetSyncFilter(SyncUtils.getSyncFilterBuilder()) - fakeSession.fakePushersService.verifyRefreshPushers() + verify(inverse = true) { aSession.startSyncing(fakeContext.instance) } + aSession.fakeFilterService.verifySetSyncFilter(SyncUtils.getSyncFilterBuilder()) + aSession.fakePushersService.verifyRefreshPushers() fakeWebRtcCallManager.verifyCheckForProtocolsSupportIfNeeded() coVerify { - fakeUpdateMatrixClientInfoUseCase.execute(fakeSession) - fakeUpdateNotificationSettingsAccountDataUseCase.execute(fakeSession) + fakeUpdateMatrixClientInfoUseCase.execute(aSession) + fakeUpdateNotificationSettingsAccountDataUseCase.execute(aSession) } } diff --git a/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/DeleteNotificationSettingsAccountDataUseCaseTest.kt b/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/DeleteNotificationSettingsAccountDataUseCaseTest.kt index d84ff8c6ac..600ba2ba48 100644 --- a/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/DeleteNotificationSettingsAccountDataUseCaseTest.kt +++ b/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/DeleteNotificationSettingsAccountDataUseCaseTest.kt @@ -19,7 +19,9 @@ 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.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 @@ -27,17 +29,22 @@ import org.matrix.android.sdk.api.account.LocalNotificationSettingsContent class DeleteNotificationSettingsAccountDataUseCaseTest { private val fakeSetNotificationSettingsAccountDataUseCase = mockk() + private val fakeGetNotificationSettingsAccountDataUseCase = mockk() private val deleteNotificationSettingsAccountDataUseCase = DeleteNotificationSettingsAccountDataUseCase( setNotificationSettingsAccountDataUseCase = fakeSetNotificationSettingsAccountDataUseCase, + getNotificationSettingsAccountDataUseCase = fakeGetNotificationSettingsAccountDataUseCase, ) @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 val aDeviceId = "device-id" val aSession = FakeSession() aSession.givenSessionId(aDeviceId) + every { fakeGetNotificationSettingsAccountDataUseCase.execute(any(), any()) } returns LocalNotificationSettingsContent( + isSilenced = true, + ) coJustRun { fakeSetNotificationSettingsAccountDataUseCase.execute(any(), any(), any()) } val expectedContent = LocalNotificationSettingsContent( isSilenced = null @@ -47,6 +54,25 @@ class DeleteNotificationSettingsAccountDataUseCaseTest { deleteNotificationSettingsAccountDataUseCase.execute(aSession) // Then + verify { fakeGetNotificationSettingsAccountDataUseCase.execute(aSession, aDeviceId) } 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()) } + } } diff --git a/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/GetNotificationSettingsAccountDataUseCaseTest.kt b/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/GetNotificationSettingsAccountDataUseCaseTest.kt index 75179b5679..2adb0d8599 100644 --- a/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/GetNotificationSettingsAccountDataUseCaseTest.kt +++ b/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/GetNotificationSettingsAccountDataUseCaseTest.kt @@ -32,7 +32,27 @@ class GetNotificationSettingsAccountDataUseCaseTest { // Given val aDeviceId = "device-id" 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 .accountDataService() .givenGetUserAccountDataEventReturns( diff --git a/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/SetNotificationSettingsAccountDataUseCaseTest.kt b/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/SetNotificationSettingsAccountDataUseCaseTest.kt index d26271e59d..89fcd5e512 100644 --- a/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/SetNotificationSettingsAccountDataUseCaseTest.kt +++ b/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/SetNotificationSettingsAccountDataUseCaseTest.kt @@ -31,7 +31,7 @@ class SetNotificationSettingsAccountDataUseCaseTest { 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 localNotificationSettingsContent = LocalNotificationSettingsContent(isSilenced = true) val fakeSession = FakeSession() fakeSession.accountDataService().givenUpdateUserAccountDataEventSucceeds() diff --git a/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/UpdateNotificationSettingsAccountDataUseCaseTest.kt b/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/UpdateNotificationSettingsAccountDataUseCaseTest.kt index 41c5ab9081..3bca0da84e 100644 --- a/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/UpdateNotificationSettingsAccountDataUseCaseTest.kt +++ b/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/UpdateNotificationSettingsAccountDataUseCaseTest.kt @@ -32,15 +32,17 @@ class UpdateNotificationSettingsAccountDataUseCaseTest { private val fakeVectorPreferences = FakeVectorPreferences() private val fakeGetNotificationSettingsAccountDataUseCase = mockk() private val fakeSetNotificationSettingsAccountDataUseCase = mockk() + private val fakeDeleteNotificationSettingsAccountDataUseCase = mockk() private val updateNotificationSettingsAccountDataUseCase = UpdateNotificationSettingsAccountDataUseCase( vectorPreferences = fakeVectorPreferences.instance, getNotificationSettingsAccountDataUseCase = fakeGetNotificationSettingsAccountDataUseCase, setNotificationSettingsAccountDataUseCase = fakeSetNotificationSettingsAccountDataUseCase, + deleteNotificationSettingsAccountDataUseCase = fakeDeleteNotificationSettingsAccountDataUseCase, ) @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 val aDeviceId = "device-id" val aSession = FakeSession() @@ -48,6 +50,7 @@ class UpdateNotificationSettingsAccountDataUseCaseTest { coJustRun { fakeSetNotificationSettingsAccountDataUseCase.execute(any(), any(), any()) } val areNotificationsEnabled = true fakeVectorPreferences.givenAreNotificationEnabled(areNotificationsEnabled) + fakeVectorPreferences.givenIsBackgroundSyncEnabled(true) every { fakeGetNotificationSettingsAccountDataUseCase.execute(any(), any()) } returns LocalNotificationSettingsContent( isSilenced = null @@ -61,14 +64,16 @@ class UpdateNotificationSettingsAccountDataUseCaseTest { // Then verify { + fakeVectorPreferences.instance.isBackgroundSyncEnabled() fakeVectorPreferences.instance.areNotificationEnabledForDevice() fakeGetNotificationSettingsAccountDataUseCase.execute(aSession, aDeviceId) } + coVerify(inverse = true) { fakeDeleteNotificationSettingsAccountDataUseCase.execute(aSession) } 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 { + 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 val aDeviceId = "device-id" val aSession = FakeSession() @@ -76,6 +81,7 @@ class UpdateNotificationSettingsAccountDataUseCaseTest { coJustRun { fakeSetNotificationSettingsAccountDataUseCase.execute(any(), any(), any()) } val areNotificationsEnabled = true fakeVectorPreferences.givenAreNotificationEnabled(areNotificationsEnabled) + fakeVectorPreferences.givenIsBackgroundSyncEnabled(true) every { fakeGetNotificationSettingsAccountDataUseCase.execute(any(), any()) } returns LocalNotificationSettingsContent( isSilenced = false @@ -89,9 +95,31 @@ class UpdateNotificationSettingsAccountDataUseCaseTest { // Then verify { + fakeVectorPreferences.instance.isBackgroundSyncEnabled() fakeVectorPreferences.instance.areNotificationEnabledForDevice() fakeGetNotificationSettingsAccountDataUseCase.execute(aSession, aDeviceId) } + coVerify(inverse = true) { fakeDeleteNotificationSettingsAccountDataUseCase.execute(aSession) } 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()) } + } } diff --git a/vector/src/test/java/im/vector/app/test/fakes/FakeEnableNotificationsSettingUpdater.kt b/vector/src/test/java/im/vector/app/test/fakes/FakeNotificationsSettingUpdater.kt similarity index 82% rename from vector/src/test/java/im/vector/app/test/fakes/FakeEnableNotificationsSettingUpdater.kt rename to vector/src/test/java/im/vector/app/test/fakes/FakeNotificationsSettingUpdater.kt index a78dd1a34b..f9f38e6c2a 100644 --- a/vector/src/test/java/im/vector/app/test/fakes/FakeEnableNotificationsSettingUpdater.kt +++ b/vector/src/test/java/im/vector/app/test/fakes/FakeNotificationsSettingUpdater.kt @@ -16,14 +16,14 @@ 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.mockk import org.matrix.android.sdk.api.session.Session -class FakeEnableNotificationsSettingUpdater { +class FakeNotificationsSettingUpdater { - val instance = mockk() + val instance = mockk() fun givenOnSessionsStarted(session: Session) { justRun { instance.onSessionsStarted(session) } From 7c10a4cb21a0bc0a044698ea633c5a76626bdbd6 Mon Sep 17 00:00:00 2001 From: Maxime NATUREL Date: Wed, 23 Nov 2022 15:12:51 +0100 Subject: [PATCH 11/21] Adding tests for notifications setting updater --- .../NotificationsSettingUpdater.kt | 7 +- .../ConfigureAndStartSessionUseCase.kt | 2 +- .../NotificationsSettingUpdaterTest.kt | 106 ++++++++++++++++++ .../fakes/FakeNotificationsSettingUpdater.kt | 2 +- .../app/test/fakes/FakeVectorPreferences.kt | 7 ++ 5 files changed, 118 insertions(+), 6 deletions(-) create mode 100644 vector/src/test/java/im/vector/app/core/notification/NotificationsSettingUpdaterTest.kt diff --git a/vector/src/main/java/im/vector/app/core/notification/NotificationsSettingUpdater.kt b/vector/src/main/java/im/vector/app/core/notification/NotificationsSettingUpdater.kt index c6738edddb..4a16f37cfe 100644 --- a/vector/src/main/java/im/vector/app/core/notification/NotificationsSettingUpdater.kt +++ b/vector/src/main/java/im/vector/app/core/notification/NotificationsSettingUpdater.kt @@ -41,10 +41,9 @@ class NotificationsSettingUpdater @Inject constructor( private var job: Job? = null private var prefChangeListener: OnSharedPreferenceChangeListener? = null - // TODO add unit tests - fun onSessionsStarted(session: Session) { + fun onSessionStarted(session: Session) { updateEnableNotificationsSettingOnChange(session) - updateNotificationSettingsAccountDataOnChange(session) + updateAccountDataOnBackgroundSyncChange(session) } private fun updateEnableNotificationsSettingOnChange(session: Session) { @@ -54,7 +53,7 @@ class NotificationsSettingUpdater @Inject constructor( } } - private fun updateNotificationSettingsAccountDataOnChange(session: Session) { + private fun updateAccountDataOnBackgroundSyncChange(session: Session) { prefChangeListener?.let { vectorPreferences.unsubscribeToChanges(it) } prefChangeListener = null prefChangeListener = createPrefListener(session).also { diff --git a/vector/src/main/java/im/vector/app/core/session/ConfigureAndStartSessionUseCase.kt b/vector/src/main/java/im/vector/app/core/session/ConfigureAndStartSessionUseCase.kt index 623f7d83a9..d167b02d05 100644 --- a/vector/src/main/java/im/vector/app/core/session/ConfigureAndStartSessionUseCase.kt +++ b/vector/src/main/java/im/vector/app/core/session/ConfigureAndStartSessionUseCase.kt @@ -53,7 +53,7 @@ class ConfigureAndStartSessionUseCase @Inject constructor( webRtcCallManager.checkForProtocolsSupportIfNeeded() updateMatrixClientInfoIfNeeded(session) createNotificationSettingsAccountDataIfNeeded(session) - notificationsSettingUpdater.onSessionsStarted(session) + notificationsSettingUpdater.onSessionStarted(session) } private fun updateMatrixClientInfoIfNeeded(session: Session) { diff --git a/vector/src/test/java/im/vector/app/core/notification/NotificationsSettingUpdaterTest.kt b/vector/src/test/java/im/vector/app/core/notification/NotificationsSettingUpdaterTest.kt new file mode 100644 index 0000000000..386b52e61e --- /dev/null +++ b/vector/src/test/java/im/vector/app/core/notification/NotificationsSettingUpdaterTest.kt @@ -0,0 +1,106 @@ +/* + * 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 im.vector.app.features.settings.VectorPreferences +import im.vector.app.features.settings.devices.v2.notification.UpdateNotificationSettingsAccountDataUseCase +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.mockkStatic +import io.mockk.unmockkAll +import kotlinx.coroutines.test.advanceUntilIdle +import kotlinx.coroutines.test.runTest +import org.junit.After +import org.junit.Before +import org.junit.Test + +class NotificationsSettingUpdaterTest { + + private val fakeUpdateEnableNotificationsSettingOnChangeUseCase = mockk() + private val fakeVectorPreferences = FakeVectorPreferences() + private val fakeUpdateNotificationSettingsAccountDataUseCase = mockk() + + private val notificationsSettingUpdater = NotificationsSettingUpdater( + updateEnableNotificationsSettingOnChangeUseCase = fakeUpdateEnableNotificationsSettingOnChangeUseCase, + vectorPreferences = fakeVectorPreferences.instance, + updateNotificationSettingsAccountDataUseCase = fakeUpdateNotificationSettingsAccountDataUseCase, + ) + + @Before + fun setup() { + mockkStatic("im.vector.app.features.session.SessionCoroutineScopesKt") + } + + @After + fun tearDown() { + unmockkAll() + } + + @Test + fun `given a session when calling onSessionStarted then update enable notification on change`() = runTest { + // Given + val aSession = FakeSession() + every { aSession.coroutineScope } returns this + coJustRun { fakeUpdateEnableNotificationsSettingOnChangeUseCase.execute(any()) } + + // When + notificationsSettingUpdater.onSessionStarted(aSession) + advanceUntilIdle() + + // Then + coVerify { fakeUpdateEnableNotificationsSettingOnChangeUseCase.execute(aSession) } + } + + @Test + fun `given a session when calling onSessionStarted then update account data on background sync preference change`() = runTest { + // Given + val aSession = FakeSession() + every { aSession.coroutineScope } returns this + coJustRun { fakeUpdateEnableNotificationsSettingOnChangeUseCase.execute(any()) } + coJustRun { fakeUpdateNotificationSettingsAccountDataUseCase.execute(any()) } + fakeVectorPreferences.givenChangeOnPreference(VectorPreferences.SETTINGS_FDROID_BACKGROUND_SYNC_MODE) + + // When + notificationsSettingUpdater.onSessionStarted(aSession) + advanceUntilIdle() + + // Then + coVerify { fakeUpdateNotificationSettingsAccountDataUseCase.execute(aSession) } + } + + @Test + fun `given a session when calling onSessionStarted then account data is not updated on other preference change`() = runTest { + // Given + val aSession = FakeSession() + every { aSession.coroutineScope } returns this + coJustRun { fakeUpdateEnableNotificationsSettingOnChangeUseCase.execute(any()) } + coJustRun { fakeUpdateNotificationSettingsAccountDataUseCase.execute(any()) } + fakeVectorPreferences.givenChangeOnPreference("key") + + // When + notificationsSettingUpdater.onSessionStarted(aSession) + advanceUntilIdle() + + // Then + coVerify(inverse = true) { fakeUpdateNotificationSettingsAccountDataUseCase.execute(aSession) } + } +} diff --git a/vector/src/test/java/im/vector/app/test/fakes/FakeNotificationsSettingUpdater.kt b/vector/src/test/java/im/vector/app/test/fakes/FakeNotificationsSettingUpdater.kt index f9f38e6c2a..2e397763f8 100644 --- a/vector/src/test/java/im/vector/app/test/fakes/FakeNotificationsSettingUpdater.kt +++ b/vector/src/test/java/im/vector/app/test/fakes/FakeNotificationsSettingUpdater.kt @@ -26,6 +26,6 @@ class FakeNotificationsSettingUpdater { val instance = mockk() fun givenOnSessionsStarted(session: Session) { - justRun { instance.onSessionsStarted(session) } + justRun { instance.onSessionStarted(session) } } } diff --git a/vector/src/test/java/im/vector/app/test/fakes/FakeVectorPreferences.kt b/vector/src/test/java/im/vector/app/test/fakes/FakeVectorPreferences.kt index 58bc1a18b8..94bc0966c5 100644 --- a/vector/src/test/java/im/vector/app/test/fakes/FakeVectorPreferences.kt +++ b/vector/src/test/java/im/vector/app/test/fakes/FakeVectorPreferences.kt @@ -17,6 +17,7 @@ package im.vector.app.test.fakes import im.vector.app.features.settings.BackgroundSyncMode +import android.content.SharedPreferences.OnSharedPreferenceChangeListener import im.vector.app.features.settings.VectorPreferences import io.mockk.every import io.mockk.justRun @@ -77,4 +78,10 @@ class FakeVectorPreferences { fun givenIsBackgroundSyncEnabled(isEnabled: Boolean) { every { instance.isBackgroundSyncEnabled() } returns isEnabled } + + fun givenChangeOnPreference(key: String) { + every { instance.subscribeToChanges(any()) } answers { + firstArg().onSharedPreferenceChanged(mockk(), key) + } + } } From a2ae3af69d5c187930e0e0f6e4884eb3b4f29de2 Mon Sep 17 00:00:00 2001 From: Maxime NATUREL Date: Wed, 23 Nov 2022 15:23:31 +0100 Subject: [PATCH 12/21] Removing unused imports --- .../vector/app/features/settings/devices/v2/DevicesViewModel.kt | 1 - .../settings/devices/v2/overview/SessionOverviewViewModel.kt | 1 - .../app/features/settings/devices/v2/DevicesViewModelTest.kt | 1 - .../settings/devices/v2/overview/SessionOverviewViewModelTest.kt | 1 - 4 files changed, 4 deletions(-) diff --git a/vector/src/main/java/im/vector/app/features/settings/devices/v2/DevicesViewModel.kt b/vector/src/main/java/im/vector/app/features/settings/devices/v2/DevicesViewModel.kt index bfccd2f9d3..b7a6c5df30 100644 --- a/vector/src/main/java/im/vector/app/features/settings/devices/v2/DevicesViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/settings/devices/v2/DevicesViewModel.kt @@ -28,7 +28,6 @@ import im.vector.app.core.di.hiltMavericksViewModelFactory import im.vector.app.features.auth.PendingAuthHandler import im.vector.app.features.settings.VectorPreferences import im.vector.app.features.settings.devices.v2.filter.DeviceManagerFilterType -import im.vector.app.features.settings.devices.v2.signout.InterceptSignoutFlowResponseUseCase import im.vector.app.features.settings.devices.v2.signout.SignoutSessionsReAuthNeeded import im.vector.app.features.settings.devices.v2.signout.SignoutSessionsUseCase import im.vector.app.features.settings.devices.v2.verification.CheckIfCurrentSessionCanBeVerifiedUseCase diff --git a/vector/src/main/java/im/vector/app/features/settings/devices/v2/overview/SessionOverviewViewModel.kt b/vector/src/main/java/im/vector/app/features/settings/devices/v2/overview/SessionOverviewViewModel.kt index 74f962b464..f598c397de 100644 --- a/vector/src/main/java/im/vector/app/features/settings/devices/v2/overview/SessionOverviewViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/settings/devices/v2/overview/SessionOverviewViewModel.kt @@ -32,7 +32,6 @@ import im.vector.app.features.settings.devices.v2.ToggleIpAddressVisibilityUseCa import im.vector.app.features.settings.devices.v2.VectorSessionsListViewModel import im.vector.app.features.settings.devices.v2.notification.GetNotificationsStatusUseCase import im.vector.app.features.settings.devices.v2.notification.ToggleNotificationUseCase -import im.vector.app.features.settings.devices.v2.signout.InterceptSignoutFlowResponseUseCase import im.vector.app.features.settings.devices.v2.signout.SignoutSessionsReAuthNeeded import im.vector.app.features.settings.devices.v2.signout.SignoutSessionsUseCase import im.vector.app.features.settings.devices.v2.verification.CheckIfCurrentSessionCanBeVerifiedUseCase diff --git a/vector/src/test/java/im/vector/app/features/settings/devices/v2/DevicesViewModelTest.kt b/vector/src/test/java/im/vector/app/features/settings/devices/v2/DevicesViewModelTest.kt index aa5ebd73eb..4bfd5c4496 100644 --- a/vector/src/test/java/im/vector/app/features/settings/devices/v2/DevicesViewModelTest.kt +++ b/vector/src/test/java/im/vector/app/features/settings/devices/v2/DevicesViewModelTest.kt @@ -22,7 +22,6 @@ import com.airbnb.mvrx.test.MavericksTestRule import im.vector.app.core.session.clientinfo.MatrixClientInfoContent import im.vector.app.features.settings.devices.v2.details.extended.DeviceExtendedInfo import im.vector.app.features.settings.devices.v2.list.DeviceType -import im.vector.app.features.settings.devices.v2.signout.InterceptSignoutFlowResponseUseCase import im.vector.app.features.settings.devices.v2.verification.CheckIfCurrentSessionCanBeVerifiedUseCase import im.vector.app.features.settings.devices.v2.verification.CurrentSessionCrossSigningInfo import im.vector.app.features.settings.devices.v2.verification.GetCurrentSessionCrossSigningInfoUseCase diff --git a/vector/src/test/java/im/vector/app/features/settings/devices/v2/overview/SessionOverviewViewModelTest.kt b/vector/src/test/java/im/vector/app/features/settings/devices/v2/overview/SessionOverviewViewModelTest.kt index 2ddc91cdac..6018152176 100644 --- a/vector/src/test/java/im/vector/app/features/settings/devices/v2/overview/SessionOverviewViewModelTest.kt +++ b/vector/src/test/java/im/vector/app/features/settings/devices/v2/overview/SessionOverviewViewModelTest.kt @@ -24,7 +24,6 @@ import im.vector.app.features.settings.devices.v2.DeviceFullInfo import im.vector.app.features.settings.devices.v2.RefreshDevicesUseCase import im.vector.app.features.settings.devices.v2.ToggleIpAddressVisibilityUseCase import im.vector.app.features.settings.devices.v2.notification.NotificationsStatus -import im.vector.app.features.settings.devices.v2.signout.InterceptSignoutFlowResponseUseCase import im.vector.app.features.settings.devices.v2.verification.CheckIfCurrentSessionCanBeVerifiedUseCase import im.vector.app.test.fakes.FakeActiveSessionHolder import im.vector.app.test.fakes.FakeGetNotificationsStatusUseCase From 68d00e00d16b7082f6f86e2c73120387b0eb762e Mon Sep 17 00:00:00 2001 From: Maxime NATUREL Date: Thu, 24 Nov 2022 18:01:34 +0100 Subject: [PATCH 13/21] Fix method used to check if background sync is enabled --- ...pdateNotificationSettingsAccountDataUseCase.kt | 4 +++- ...eNotificationSettingsAccountDataUseCaseTest.kt | 15 +++++++++------ .../app/test/fakes/FakeUnifiedPushHelper.kt | 4 ++++ 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/UpdateNotificationSettingsAccountDataUseCase.kt b/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/UpdateNotificationSettingsAccountDataUseCase.kt index 7791c1dd4b..9296bcd912 100644 --- a/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/UpdateNotificationSettingsAccountDataUseCase.kt +++ b/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/UpdateNotificationSettingsAccountDataUseCase.kt @@ -16,6 +16,7 @@ package im.vector.app.features.settings.devices.v2.notification +import im.vector.app.core.pushers.UnifiedPushHelper import im.vector.app.features.settings.VectorPreferences import org.matrix.android.sdk.api.account.LocalNotificationSettingsContent import org.matrix.android.sdk.api.session.Session @@ -27,13 +28,14 @@ import javax.inject.Inject */ class UpdateNotificationSettingsAccountDataUseCase @Inject constructor( private val vectorPreferences: VectorPreferences, + private val unifiedPushHelper: UnifiedPushHelper, private val getNotificationSettingsAccountDataUseCase: GetNotificationSettingsAccountDataUseCase, private val setNotificationSettingsAccountDataUseCase: SetNotificationSettingsAccountDataUseCase, private val deleteNotificationSettingsAccountDataUseCase: DeleteNotificationSettingsAccountDataUseCase, ) { suspend fun execute(session: Session) { - if (vectorPreferences.isBackgroundSyncEnabled()) { + if (unifiedPushHelper.isBackgroundSync()) { setCurrentNotificationStatus(session) } else { deleteCurrentNotificationStatus(session) diff --git a/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/UpdateNotificationSettingsAccountDataUseCaseTest.kt b/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/UpdateNotificationSettingsAccountDataUseCaseTest.kt index 3bca0da84e..f82663f6e4 100644 --- a/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/UpdateNotificationSettingsAccountDataUseCaseTest.kt +++ b/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/UpdateNotificationSettingsAccountDataUseCaseTest.kt @@ -17,6 +17,7 @@ package im.vector.app.features.settings.devices.v2.notification import im.vector.app.test.fakes.FakeSession +import im.vector.app.test.fakes.FakeUnifiedPushHelper import im.vector.app.test.fakes.FakeVectorPreferences import io.mockk.coJustRun import io.mockk.coVerify @@ -30,12 +31,14 @@ import org.matrix.android.sdk.api.account.LocalNotificationSettingsContent class UpdateNotificationSettingsAccountDataUseCaseTest { private val fakeVectorPreferences = FakeVectorPreferences() + private val fakeUnifiedPushHelper = FakeUnifiedPushHelper() private val fakeGetNotificationSettingsAccountDataUseCase = mockk() private val fakeSetNotificationSettingsAccountDataUseCase = mockk() private val fakeDeleteNotificationSettingsAccountDataUseCase = mockk() private val updateNotificationSettingsAccountDataUseCase = UpdateNotificationSettingsAccountDataUseCase( vectorPreferences = fakeVectorPreferences.instance, + unifiedPushHelper = fakeUnifiedPushHelper.instance, getNotificationSettingsAccountDataUseCase = fakeGetNotificationSettingsAccountDataUseCase, setNotificationSettingsAccountDataUseCase = fakeSetNotificationSettingsAccountDataUseCase, deleteNotificationSettingsAccountDataUseCase = fakeDeleteNotificationSettingsAccountDataUseCase, @@ -50,7 +53,7 @@ class UpdateNotificationSettingsAccountDataUseCaseTest { coJustRun { fakeSetNotificationSettingsAccountDataUseCase.execute(any(), any(), any()) } val areNotificationsEnabled = true fakeVectorPreferences.givenAreNotificationEnabled(areNotificationsEnabled) - fakeVectorPreferences.givenIsBackgroundSyncEnabled(true) + fakeUnifiedPushHelper.givenIsBackgroundSyncReturns(true) every { fakeGetNotificationSettingsAccountDataUseCase.execute(any(), any()) } returns LocalNotificationSettingsContent( isSilenced = null @@ -64,7 +67,7 @@ class UpdateNotificationSettingsAccountDataUseCaseTest { // Then verify { - fakeVectorPreferences.instance.isBackgroundSyncEnabled() + fakeUnifiedPushHelper.instance.isBackgroundSync() fakeVectorPreferences.instance.areNotificationEnabledForDevice() fakeGetNotificationSettingsAccountDataUseCase.execute(aSession, aDeviceId) } @@ -81,7 +84,7 @@ class UpdateNotificationSettingsAccountDataUseCaseTest { coJustRun { fakeSetNotificationSettingsAccountDataUseCase.execute(any(), any(), any()) } val areNotificationsEnabled = true fakeVectorPreferences.givenAreNotificationEnabled(areNotificationsEnabled) - fakeVectorPreferences.givenIsBackgroundSyncEnabled(true) + fakeUnifiedPushHelper.givenIsBackgroundSyncReturns(true) every { fakeGetNotificationSettingsAccountDataUseCase.execute(any(), any()) } returns LocalNotificationSettingsContent( isSilenced = false @@ -95,7 +98,7 @@ class UpdateNotificationSettingsAccountDataUseCaseTest { // Then verify { - fakeVectorPreferences.instance.isBackgroundSyncEnabled() + fakeUnifiedPushHelper.instance.isBackgroundSync() fakeVectorPreferences.instance.areNotificationEnabledForDevice() fakeGetNotificationSettingsAccountDataUseCase.execute(aSession, aDeviceId) } @@ -110,14 +113,14 @@ class UpdateNotificationSettingsAccountDataUseCaseTest { val aSession = FakeSession() aSession.givenSessionId(aDeviceId) coJustRun { fakeDeleteNotificationSettingsAccountDataUseCase.execute(any()) } - fakeVectorPreferences.givenIsBackgroundSyncEnabled(false) + fakeUnifiedPushHelper.givenIsBackgroundSyncReturns(false) // When updateNotificationSettingsAccountDataUseCase.execute(aSession) // Then verify { - fakeVectorPreferences.instance.isBackgroundSyncEnabled() + fakeUnifiedPushHelper.instance.isBackgroundSync() } coVerify { fakeDeleteNotificationSettingsAccountDataUseCase.execute(aSession) } coVerify(inverse = true) { fakeSetNotificationSettingsAccountDataUseCase.execute(aSession, aDeviceId, any()) } diff --git a/vector/src/test/java/im/vector/app/test/fakes/FakeUnifiedPushHelper.kt b/vector/src/test/java/im/vector/app/test/fakes/FakeUnifiedPushHelper.kt index 99b5b75874..1a09783fad 100644 --- a/vector/src/test/java/im/vector/app/test/fakes/FakeUnifiedPushHelper.kt +++ b/vector/src/test/java/im/vector/app/test/fakes/FakeUnifiedPushHelper.kt @@ -31,4 +31,8 @@ class FakeUnifiedPushHelper { fun givenGetEndpointOrTokenReturns(endpoint: String?) { every { instance.getEndpointOrToken() } returns endpoint } + + fun givenIsBackgroundSyncReturns(enabled: Boolean) { + every { instance.isBackgroundSync() } returns enabled + } } From 9dff4ff949672207112d29eab807e8f1c0a8d0d6 Mon Sep 17 00:00:00 2001 From: Maxime NATUREL Date: Thu, 1 Dec 2022 10:30:09 +0100 Subject: [PATCH 14/21] Fixing import order after rebase --- .../vector/app/core/session/ConfigureAndStartSessionUseCase.kt | 2 +- .../app/core/session/ConfigureAndStartSessionUseCaseTest.kt | 2 +- .../test/java/im/vector/app/test/fakes/FakeVectorPreferences.kt | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/vector/src/main/java/im/vector/app/core/session/ConfigureAndStartSessionUseCase.kt b/vector/src/main/java/im/vector/app/core/session/ConfigureAndStartSessionUseCase.kt index d167b02d05..fbf89b76a4 100644 --- a/vector/src/main/java/im/vector/app/core/session/ConfigureAndStartSessionUseCase.kt +++ b/vector/src/main/java/im/vector/app/core/session/ConfigureAndStartSessionUseCase.kt @@ -24,8 +24,8 @@ import im.vector.app.core.session.clientinfo.UpdateMatrixClientInfoUseCase 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 im.vector.app.features.sync.SyncUtils import kotlinx.coroutines.launch import org.matrix.android.sdk.api.session.Session import timber.log.Timber diff --git a/vector/src/test/java/im/vector/app/core/session/ConfigureAndStartSessionUseCaseTest.kt b/vector/src/test/java/im/vector/app/core/session/ConfigureAndStartSessionUseCaseTest.kt index 4071afaf3f..3fb128c759 100644 --- a/vector/src/test/java/im/vector/app/core/session/ConfigureAndStartSessionUseCaseTest.kt +++ b/vector/src/test/java/im/vector/app/core/session/ConfigureAndStartSessionUseCaseTest.kt @@ -19,8 +19,8 @@ package im.vector.app.core.session 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.features.sync.SyncUtils import im.vector.app.test.fakes.FakeContext import im.vector.app.test.fakes.FakeNotificationsSettingUpdater import im.vector.app.test.fakes.FakeSession diff --git a/vector/src/test/java/im/vector/app/test/fakes/FakeVectorPreferences.kt b/vector/src/test/java/im/vector/app/test/fakes/FakeVectorPreferences.kt index 94bc0966c5..3d7de662bd 100644 --- a/vector/src/test/java/im/vector/app/test/fakes/FakeVectorPreferences.kt +++ b/vector/src/test/java/im/vector/app/test/fakes/FakeVectorPreferences.kt @@ -16,8 +16,8 @@ package im.vector.app.test.fakes -import im.vector.app.features.settings.BackgroundSyncMode import android.content.SharedPreferences.OnSharedPreferenceChangeListener +import im.vector.app.features.settings.BackgroundSyncMode import im.vector.app.features.settings.VectorPreferences import io.mockk.every import io.mockk.justRun From 8973f3892a3fe10df8a784f6d3042e06453430e9 Mon Sep 17 00:00:00 2001 From: Maxime NATUREL Date: Thu, 1 Dec 2022 11:34:30 +0100 Subject: [PATCH 15/21] Fixing unit tests after rebase --- .../UpdateNotificationSettingsAccountDataUseCaseTest.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/UpdateNotificationSettingsAccountDataUseCaseTest.kt b/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/UpdateNotificationSettingsAccountDataUseCaseTest.kt index f82663f6e4..0075be02d2 100644 --- a/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/UpdateNotificationSettingsAccountDataUseCaseTest.kt +++ b/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/UpdateNotificationSettingsAccountDataUseCaseTest.kt @@ -52,7 +52,7 @@ class UpdateNotificationSettingsAccountDataUseCaseTest { aSession.givenSessionId(aDeviceId) coJustRun { fakeSetNotificationSettingsAccountDataUseCase.execute(any(), any(), any()) } val areNotificationsEnabled = true - fakeVectorPreferences.givenAreNotificationEnabled(areNotificationsEnabled) + fakeVectorPreferences.givenAreNotificationsEnabledForDevice(areNotificationsEnabled) fakeUnifiedPushHelper.givenIsBackgroundSyncReturns(true) every { fakeGetNotificationSettingsAccountDataUseCase.execute(any(), any()) } returns LocalNotificationSettingsContent( @@ -83,7 +83,7 @@ class UpdateNotificationSettingsAccountDataUseCaseTest { aSession.givenSessionId(aDeviceId) coJustRun { fakeSetNotificationSettingsAccountDataUseCase.execute(any(), any(), any()) } val areNotificationsEnabled = true - fakeVectorPreferences.givenAreNotificationEnabled(areNotificationsEnabled) + fakeVectorPreferences.givenAreNotificationsEnabledForDevice(areNotificationsEnabled) fakeUnifiedPushHelper.givenIsBackgroundSyncReturns(true) every { fakeGetNotificationSettingsAccountDataUseCase.execute(any(), any()) } returns LocalNotificationSettingsContent( From 3f5147ddcef841caa40c56cd092916020bb06f21 Mon Sep 17 00:00:00 2001 From: Maxime NATUREL Date: Thu, 1 Dec 2022 16:47:10 +0100 Subject: [PATCH 16/21] Fixing the toggle notifications use case for current session --- ...eCase.kt => ToggleNotificationsUseCase.kt} | 2 +- .../v2/overview/SessionOverviewViewModel.kt | 6 +- ...leNotificationsForCurrentSessionUseCase.kt | 9 +-- ...leNotificationsForCurrentSessionUseCase.kt | 10 +--- ...leNotificationsForCurrentSessionUseCase.kt | 57 +++++++++++++++++++ ...SettingsNotificationPreferenceViewModel.kt | 6 +- ...t.kt => ToggleNotificationsUseCaseTest.kt} | 10 ++-- ...tificationsForCurrentSessionUseCaseTest.kt | 14 ++--- ...tificationsForCurrentSessionUseCaseTest.kt | 8 +-- .../fakes/FakeToggleNotificationUseCase.kt | 4 +- 10 files changed, 92 insertions(+), 34 deletions(-) rename vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/{ToggleNotificationUseCase.kt => ToggleNotificationsUseCase.kt} (97%) create mode 100644 vector/src/main/java/im/vector/app/features/settings/notifications/ToggleNotificationsForCurrentSessionUseCase.kt rename vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/{ToggleNotificationUseCaseTest.kt => ToggleNotificationsUseCaseTest.kt} (93%) diff --git a/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/ToggleNotificationUseCase.kt b/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/ToggleNotificationsUseCase.kt similarity index 97% rename from vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/ToggleNotificationUseCase.kt rename to vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/ToggleNotificationsUseCase.kt index 73a81a6de1..77195ea950 100644 --- a/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/ToggleNotificationUseCase.kt +++ b/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/ToggleNotificationsUseCase.kt @@ -20,7 +20,7 @@ import im.vector.app.core.di.ActiveSessionHolder import org.matrix.android.sdk.api.account.LocalNotificationSettingsContent import javax.inject.Inject -class ToggleNotificationUseCase @Inject constructor( +class ToggleNotificationsUseCase @Inject constructor( private val activeSessionHolder: ActiveSessionHolder, private val checkIfCanToggleNotificationsViaPusherUseCase: CheckIfCanToggleNotificationsViaPusherUseCase, private val checkIfCanToggleNotificationsViaAccountDataUseCase: CheckIfCanToggleNotificationsViaAccountDataUseCase, diff --git a/vector/src/main/java/im/vector/app/features/settings/devices/v2/overview/SessionOverviewViewModel.kt b/vector/src/main/java/im/vector/app/features/settings/devices/v2/overview/SessionOverviewViewModel.kt index f598c397de..55866cb8c4 100644 --- a/vector/src/main/java/im/vector/app/features/settings/devices/v2/overview/SessionOverviewViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/settings/devices/v2/overview/SessionOverviewViewModel.kt @@ -31,7 +31,7 @@ import im.vector.app.features.settings.devices.v2.RefreshDevicesUseCase import im.vector.app.features.settings.devices.v2.ToggleIpAddressVisibilityUseCase import im.vector.app.features.settings.devices.v2.VectorSessionsListViewModel import im.vector.app.features.settings.devices.v2.notification.GetNotificationsStatusUseCase -import im.vector.app.features.settings.devices.v2.notification.ToggleNotificationUseCase +import im.vector.app.features.settings.devices.v2.notification.ToggleNotificationsUseCase import im.vector.app.features.settings.devices.v2.signout.SignoutSessionsReAuthNeeded import im.vector.app.features.settings.devices.v2.signout.SignoutSessionsUseCase import im.vector.app.features.settings.devices.v2.verification.CheckIfCurrentSessionCanBeVerifiedUseCase @@ -52,7 +52,7 @@ class SessionOverviewViewModel @AssistedInject constructor( private val signoutSessionsUseCase: SignoutSessionsUseCase, private val pendingAuthHandler: PendingAuthHandler, private val activeSessionHolder: ActiveSessionHolder, - private val toggleNotificationUseCase: ToggleNotificationUseCase, + private val toggleNotificationsUseCase: ToggleNotificationsUseCase, private val getNotificationsStatusUseCase: GetNotificationsStatusUseCase, refreshDevicesUseCase: RefreshDevicesUseCase, private val vectorPreferences: VectorPreferences, @@ -226,7 +226,7 @@ class SessionOverviewViewModel @AssistedInject constructor( private fun handleTogglePusherAction(action: SessionOverviewAction.TogglePushNotifications) { viewModelScope.launch { - toggleNotificationUseCase.execute(action.deviceId, action.enabled) + toggleNotificationsUseCase.execute(action.deviceId, action.enabled) } } } diff --git a/vector/src/main/java/im/vector/app/features/settings/notifications/DisableNotificationsForCurrentSessionUseCase.kt b/vector/src/main/java/im/vector/app/features/settings/notifications/DisableNotificationsForCurrentSessionUseCase.kt index 380c3b5e4e..9173107018 100644 --- a/vector/src/main/java/im/vector/app/features/settings/notifications/DisableNotificationsForCurrentSessionUseCase.kt +++ b/vector/src/main/java/im/vector/app/features/settings/notifications/DisableNotificationsForCurrentSessionUseCase.kt @@ -20,21 +20,22 @@ import im.vector.app.core.di.ActiveSessionHolder import im.vector.app.core.pushers.PushersManager 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 javax.inject.Inject class DisableNotificationsForCurrentSessionUseCase @Inject constructor( private val activeSessionHolder: ActiveSessionHolder, private val pushersManager: PushersManager, private val checkIfCanToggleNotificationsViaPusherUseCase: CheckIfCanToggleNotificationsViaPusherUseCase, - private val toggleNotificationUseCase: ToggleNotificationUseCase, + private val toggleNotificationsForCurrentSessionUseCase: ToggleNotificationsForCurrentSessionUseCase, private val unregisterUnifiedPushUseCase: UnregisterUnifiedPushUseCase, ) { + // TODO update unit tests suspend fun execute() { val session = activeSessionHolder.getSafeActiveSession() ?: return - val deviceId = session.sessionParams.deviceId ?: return - toggleNotificationUseCase.execute(deviceId, enabled = false) + toggleNotificationsForCurrentSessionUseCase.execute(enabled = false) + + // handle case when server does not support toggle of pusher if (!checkIfCanToggleNotificationsViaPusherUseCase.execute(session)) { unregisterUnifiedPushUseCase.execute(pushersManager) } diff --git a/vector/src/main/java/im/vector/app/features/settings/notifications/EnableNotificationsForCurrentSessionUseCase.kt b/vector/src/main/java/im/vector/app/features/settings/notifications/EnableNotificationsForCurrentSessionUseCase.kt index 89633a10c2..663c5004e8 100644 --- a/vector/src/main/java/im/vector/app/features/settings/notifications/EnableNotificationsForCurrentSessionUseCase.kt +++ b/vector/src/main/java/im/vector/app/features/settings/notifications/EnableNotificationsForCurrentSessionUseCase.kt @@ -16,17 +16,14 @@ package im.vector.app.features.settings.notifications -import im.vector.app.core.di.ActiveSessionHolder import im.vector.app.core.pushers.EnsureFcmTokenIsRetrievedUseCase import im.vector.app.core.pushers.PushersManager import im.vector.app.core.pushers.RegisterUnifiedPushUseCase -import im.vector.app.features.settings.devices.v2.notification.ToggleNotificationUseCase import javax.inject.Inject class EnableNotificationsForCurrentSessionUseCase @Inject constructor( - private val activeSessionHolder: ActiveSessionHolder, private val pushersManager: PushersManager, - private val toggleNotificationUseCase: ToggleNotificationUseCase, + private val toggleNotificationsForCurrentSessionUseCase: ToggleNotificationsForCurrentSessionUseCase, private val registerUnifiedPushUseCase: RegisterUnifiedPushUseCase, private val ensureFcmTokenIsRetrievedUseCase: EnsureFcmTokenIsRetrievedUseCase, ) { @@ -37,6 +34,7 @@ class EnableNotificationsForCurrentSessionUseCase @Inject constructor( object NeedToAskUserForDistributor : EnableNotificationsResult } + // TODO update unit tests suspend fun execute(distributor: String = ""): EnableNotificationsResult { val pusherForCurrentSession = pushersManager.getPusherForCurrentSession() if (pusherForCurrentSession == null) { @@ -50,9 +48,7 @@ class EnableNotificationsForCurrentSessionUseCase @Inject constructor( } } - val session = activeSessionHolder.getSafeActiveSession() ?: return EnableNotificationsResult.Failure - val deviceId = session.sessionParams.deviceId ?: return EnableNotificationsResult.Failure - toggleNotificationUseCase.execute(deviceId, enabled = true) + toggleNotificationsForCurrentSessionUseCase.execute(enabled = true) return EnableNotificationsResult.Success } diff --git a/vector/src/main/java/im/vector/app/features/settings/notifications/ToggleNotificationsForCurrentSessionUseCase.kt b/vector/src/main/java/im/vector/app/features/settings/notifications/ToggleNotificationsForCurrentSessionUseCase.kt new file mode 100644 index 0000000000..64b4b1bb89 --- /dev/null +++ b/vector/src/main/java/im/vector/app/features/settings/notifications/ToggleNotificationsForCurrentSessionUseCase.kt @@ -0,0 +1,57 @@ +/* + * 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.notifications + +import im.vector.app.core.di.ActiveSessionHolder +import im.vector.app.core.pushers.UnifiedPushHelper +import im.vector.app.features.settings.devices.v2.notification.CheckIfCanToggleNotificationsViaPusherUseCase +import im.vector.app.features.settings.devices.v2.notification.DeleteNotificationSettingsAccountDataUseCase +import im.vector.app.features.settings.devices.v2.notification.SetNotificationSettingsAccountDataUseCase +import org.matrix.android.sdk.api.account.LocalNotificationSettingsContent +import timber.log.Timber +import javax.inject.Inject + +class ToggleNotificationsForCurrentSessionUseCase @Inject constructor( + private val activeSessionHolder: ActiveSessionHolder, + private val unifiedPushHelper: UnifiedPushHelper, + private val checkIfCanToggleNotificationsViaPusherUseCase: CheckIfCanToggleNotificationsViaPusherUseCase, + private val setNotificationSettingsAccountDataUseCase: SetNotificationSettingsAccountDataUseCase, + private val deleteNotificationSettingsAccountDataUseCase: DeleteNotificationSettingsAccountDataUseCase, +) { + + // TODO add unit tests + suspend fun execute(enabled: Boolean) { + val session = activeSessionHolder.getSafeActiveSession() ?: return + val deviceId = session.sessionParams.deviceId ?: return + + if (unifiedPushHelper.isBackgroundSync()) { + Timber.d("background sync is enabled, setting account data event") + val newNotificationSettingsContent = LocalNotificationSettingsContent(isSilenced = !enabled) + setNotificationSettingsAccountDataUseCase.execute(session, deviceId, newNotificationSettingsContent) + } else { + Timber.d("push notif is enabled, deleting any account data and updating pusher") + deleteNotificationSettingsAccountDataUseCase.execute(session) + + if (checkIfCanToggleNotificationsViaPusherUseCase.execute(session)) { + val devicePusher = session.pushersService().getPushers().firstOrNull { it.deviceId == deviceId } + devicePusher?.let { pusher -> + session.pushersService().togglePusher(pusher, enabled) + } + } + } + } +} diff --git a/vector/src/main/java/im/vector/app/features/settings/notifications/VectorSettingsNotificationPreferenceViewModel.kt b/vector/src/main/java/im/vector/app/features/settings/notifications/VectorSettingsNotificationPreferenceViewModel.kt index d6a9c621f2..357f6458f2 100644 --- a/vector/src/main/java/im/vector/app/features/settings/notifications/VectorSettingsNotificationPreferenceViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/settings/notifications/VectorSettingsNotificationPreferenceViewModel.kt @@ -40,6 +40,7 @@ class VectorSettingsNotificationPreferenceViewModel @AssistedInject constructor( private val unregisterUnifiedPushUseCase: UnregisterUnifiedPushUseCase, private val registerUnifiedPushUseCase: RegisterUnifiedPushUseCase, private val ensureFcmTokenIsRetrievedUseCase: EnsureFcmTokenIsRetrievedUseCase, + private val toggleNotificationsForCurrentSessionUseCase: ToggleNotificationsForCurrentSessionUseCase, ) : VectorViewModel(initialState) { @AssistedFactory @@ -80,6 +81,7 @@ class VectorSettingsNotificationPreferenceViewModel @AssistedInject constructor( } } + // TODO update unit tests private fun handleRegisterPushDistributor(distributor: String) { viewModelScope.launch { unregisterUnifiedPushUseCase.execute(pushersManager) @@ -88,7 +90,9 @@ class VectorSettingsNotificationPreferenceViewModel @AssistedInject constructor( _viewEvents.post(VectorSettingsNotificationPreferenceViewEvent.AskUserForPushDistributor) } RegisterUnifiedPushUseCase.RegisterUnifiedPushResult.Success -> { - ensureFcmTokenIsRetrievedUseCase.execute(pushersManager, registerPusher = vectorPreferences.areNotificationEnabledForDevice()) + val areNotificationsEnabled = vectorPreferences.areNotificationEnabledForDevice() + ensureFcmTokenIsRetrievedUseCase.execute(pushersManager, registerPusher = areNotificationsEnabled) + toggleNotificationsForCurrentSessionUseCase.execute(enabled = areNotificationsEnabled) _viewEvents.post(VectorSettingsNotificationPreferenceViewEvent.NotificationMethodChanged) } } diff --git a/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/ToggleNotificationUseCaseTest.kt b/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/ToggleNotificationsUseCaseTest.kt similarity index 93% rename from vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/ToggleNotificationUseCaseTest.kt rename to vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/ToggleNotificationsUseCaseTest.kt index 1e3517c776..90afbe9045 100644 --- a/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/ToggleNotificationUseCaseTest.kt +++ b/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/ToggleNotificationsUseCaseTest.kt @@ -26,7 +26,7 @@ import kotlinx.coroutines.test.runTest import org.junit.Test import org.matrix.android.sdk.api.account.LocalNotificationSettingsContent -class ToggleNotificationUseCaseTest { +class ToggleNotificationsUseCaseTest { private val activeSessionHolder = FakeActiveSessionHolder() private val fakeCheckIfCanToggleNotificationsViaPusherUseCase = @@ -36,8 +36,8 @@ class ToggleNotificationUseCaseTest { private val fakeSetNotificationSettingsAccountDataUseCase = mockk() - private val toggleNotificationUseCase = - ToggleNotificationUseCase( + private val toggleNotificationsUseCase = + ToggleNotificationsUseCase( activeSessionHolder = activeSessionHolder.instance, checkIfCanToggleNotificationsViaPusherUseCase = fakeCheckIfCanToggleNotificationsViaPusherUseCase, checkIfCanToggleNotificationsViaAccountDataUseCase = fakeCheckIfCanToggleNotificationsViaAccountDataUseCase, @@ -59,7 +59,7 @@ class ToggleNotificationUseCaseTest { every { fakeCheckIfCanToggleNotificationsViaAccountDataUseCase.execute(fakeSession, sessionId) } returns false // When - toggleNotificationUseCase.execute(sessionId, true) + toggleNotificationsUseCase.execute(sessionId, true) // Then activeSessionHolder.fakeSession.pushersService().verifyTogglePusherCalled(pushers.first(), true) @@ -78,7 +78,7 @@ class ToggleNotificationUseCaseTest { ) // When - toggleNotificationUseCase.execute(sessionId, true) + toggleNotificationsUseCase.execute(sessionId, true) // Then coVerify { diff --git a/vector/src/test/java/im/vector/app/features/settings/notifications/DisableNotificationsForCurrentSessionUseCaseTest.kt b/vector/src/test/java/im/vector/app/features/settings/notifications/DisableNotificationsForCurrentSessionUseCaseTest.kt index e53874858a..7e2a118e0e 100644 --- a/vector/src/test/java/im/vector/app/features/settings/notifications/DisableNotificationsForCurrentSessionUseCaseTest.kt +++ b/vector/src/test/java/im/vector/app/features/settings/notifications/DisableNotificationsForCurrentSessionUseCaseTest.kt @@ -18,7 +18,7 @@ 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.features.settings.devices.v2.notification.ToggleNotificationsUseCase import im.vector.app.test.fakes.FakeActiveSessionHolder import im.vector.app.test.fakes.FakePushersManager import io.mockk.coJustRun @@ -35,14 +35,14 @@ class DisableNotificationsForCurrentSessionUseCaseTest { private val fakeActiveSessionHolder = FakeActiveSessionHolder() private val fakePushersManager = FakePushersManager() private val fakeCheckIfCanToggleNotificationsViaPusherUseCase = mockk() - private val fakeToggleNotificationUseCase = mockk() + private val fakeToggleNotificationsUseCase = mockk() private val fakeUnregisterUnifiedPushUseCase = mockk() private val disableNotificationsForCurrentSessionUseCase = DisableNotificationsForCurrentSessionUseCase( activeSessionHolder = fakeActiveSessionHolder.instance, pushersManager = fakePushersManager.instance, checkIfCanToggleNotificationsViaPusherUseCase = fakeCheckIfCanToggleNotificationsViaPusherUseCase, - toggleNotificationUseCase = fakeToggleNotificationUseCase, + toggleNotificationUseCase = fakeToggleNotificationsUseCase, unregisterUnifiedPushUseCase = fakeUnregisterUnifiedPushUseCase, ) @@ -52,13 +52,13 @@ class DisableNotificationsForCurrentSessionUseCaseTest { val fakeSession = fakeActiveSessionHolder.fakeSession fakeSession.givenSessionId(A_SESSION_ID) every { fakeCheckIfCanToggleNotificationsViaPusherUseCase.execute(fakeSession) } returns true - coJustRun { fakeToggleNotificationUseCase.execute(A_SESSION_ID, any()) } + coJustRun { fakeToggleNotificationsUseCase.execute(A_SESSION_ID, any()) } // When disableNotificationsForCurrentSessionUseCase.execute() // Then - coVerify { fakeToggleNotificationUseCase.execute(A_SESSION_ID, false) } + coVerify { fakeToggleNotificationsUseCase.execute(A_SESSION_ID, false) } } @Test @@ -67,7 +67,7 @@ class DisableNotificationsForCurrentSessionUseCaseTest { val fakeSession = fakeActiveSessionHolder.fakeSession fakeSession.givenSessionId(A_SESSION_ID) every { fakeCheckIfCanToggleNotificationsViaPusherUseCase.execute(fakeSession) } returns false - coJustRun { fakeToggleNotificationUseCase.execute(A_SESSION_ID, any()) } + coJustRun { fakeToggleNotificationsUseCase.execute(A_SESSION_ID, any()) } coJustRun { fakeUnregisterUnifiedPushUseCase.execute(any()) } // When @@ -75,7 +75,7 @@ class DisableNotificationsForCurrentSessionUseCaseTest { // Then coVerify { - fakeToggleNotificationUseCase.execute(A_SESSION_ID, false) + fakeToggleNotificationsUseCase.execute(A_SESSION_ID, false) fakeUnregisterUnifiedPushUseCase.execute(fakePushersManager.instance) } } diff --git a/vector/src/test/java/im/vector/app/features/settings/notifications/EnableNotificationsForCurrentSessionUseCaseTest.kt b/vector/src/test/java/im/vector/app/features/settings/notifications/EnableNotificationsForCurrentSessionUseCaseTest.kt index f10b0777cb..7beab170f2 100644 --- a/vector/src/test/java/im/vector/app/features/settings/notifications/EnableNotificationsForCurrentSessionUseCaseTest.kt +++ b/vector/src/test/java/im/vector/app/features/settings/notifications/EnableNotificationsForCurrentSessionUseCaseTest.kt @@ -18,7 +18,7 @@ package im.vector.app.features.settings.notifications import im.vector.app.core.pushers.EnsureFcmTokenIsRetrievedUseCase import im.vector.app.core.pushers.RegisterUnifiedPushUseCase -import im.vector.app.features.settings.devices.v2.notification.ToggleNotificationUseCase +import im.vector.app.features.settings.devices.v2.notification.ToggleNotificationsUseCase import im.vector.app.test.fakes.FakeActiveSessionHolder import im.vector.app.test.fakes.FakePushersManager import io.mockk.coJustRun @@ -36,14 +36,14 @@ class EnableNotificationsForCurrentSessionUseCaseTest { private val fakeActiveSessionHolder = FakeActiveSessionHolder() private val fakePushersManager = FakePushersManager() - private val fakeToggleNotificationUseCase = mockk() + private val fakeToggleNotificationsUseCase = mockk() private val fakeRegisterUnifiedPushUseCase = mockk() private val fakeEnsureFcmTokenIsRetrievedUseCase = mockk() private val enableNotificationsForCurrentSessionUseCase = EnableNotificationsForCurrentSessionUseCase( activeSessionHolder = fakeActiveSessionHolder.instance, pushersManager = fakePushersManager.instance, - toggleNotificationUseCase = fakeToggleNotificationUseCase, + toggleNotificationUseCase = fakeToggleNotificationsUseCase, registerUnifiedPushUseCase = fakeRegisterUnifiedPushUseCase, ensureFcmTokenIsRetrievedUseCase = fakeEnsureFcmTokenIsRetrievedUseCase, ) @@ -57,7 +57,7 @@ class EnableNotificationsForCurrentSessionUseCaseTest { fakePushersManager.givenGetPusherForCurrentSessionReturns(null) every { fakeRegisterUnifiedPushUseCase.execute(any()) } returns RegisterUnifiedPushUseCase.RegisterUnifiedPushResult.Success justRun { fakeEnsureFcmTokenIsRetrievedUseCase.execute(any(), any()) } - coJustRun { fakeToggleNotificationUseCase.execute(A_SESSION_ID, any()) } + coJustRun { fakeToggleNotificationsUseCase.execute(A_SESSION_ID, any()) } // When val result = enableNotificationsForCurrentSessionUseCase.execute(aDistributor) diff --git a/vector/src/test/java/im/vector/app/test/fakes/FakeToggleNotificationUseCase.kt b/vector/src/test/java/im/vector/app/test/fakes/FakeToggleNotificationUseCase.kt index 527625144e..3d2179bc2d 100644 --- a/vector/src/test/java/im/vector/app/test/fakes/FakeToggleNotificationUseCase.kt +++ b/vector/src/test/java/im/vector/app/test/fakes/FakeToggleNotificationUseCase.kt @@ -16,14 +16,14 @@ package im.vector.app.test.fakes -import im.vector.app.features.settings.devices.v2.notification.ToggleNotificationUseCase +import im.vector.app.features.settings.devices.v2.notification.ToggleNotificationsUseCase import io.mockk.coJustRun import io.mockk.coVerify import io.mockk.mockk class FakeToggleNotificationUseCase { - val instance = mockk { + val instance = mockk { coJustRun { execute(any(), any()) } } From 06681fd115596ee9b14a30aef94f73d3a62de79b Mon Sep 17 00:00:00 2001 From: Maxime NATUREL Date: Thu, 1 Dec 2022 17:12:09 +0100 Subject: [PATCH 17/21] Removing listening on background sync preference --- .../NotificationsSettingUpdater.kt | 27 ------------ .../NotificationsSettingUpdaterTest.kt | 41 ------------------- 2 files changed, 68 deletions(-) diff --git a/vector/src/main/java/im/vector/app/core/notification/NotificationsSettingUpdater.kt b/vector/src/main/java/im/vector/app/core/notification/NotificationsSettingUpdater.kt index 4a16f37cfe..a4d18baa64 100644 --- a/vector/src/main/java/im/vector/app/core/notification/NotificationsSettingUpdater.kt +++ b/vector/src/main/java/im/vector/app/core/notification/NotificationsSettingUpdater.kt @@ -16,11 +16,7 @@ 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 @@ -29,21 +25,16 @@ 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 fun onSessionStarted(session: Session) { updateEnableNotificationsSettingOnChange(session) - updateAccountDataOnBackgroundSyncChange(session) } private fun updateEnableNotificationsSettingOnChange(session: Session) { @@ -52,22 +43,4 @@ class NotificationsSettingUpdater @Inject constructor( updateEnableNotificationsSettingOnChangeUseCase.execute(session) } } - - private fun updateAccountDataOnBackgroundSyncChange(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) - } - } - } - } } diff --git a/vector/src/test/java/im/vector/app/core/notification/NotificationsSettingUpdaterTest.kt b/vector/src/test/java/im/vector/app/core/notification/NotificationsSettingUpdaterTest.kt index 386b52e61e..0920ee4716 100644 --- a/vector/src/test/java/im/vector/app/core/notification/NotificationsSettingUpdaterTest.kt +++ b/vector/src/test/java/im/vector/app/core/notification/NotificationsSettingUpdaterTest.kt @@ -17,10 +17,7 @@ package im.vector.app.core.notification import im.vector.app.features.session.coroutineScope -import im.vector.app.features.settings.VectorPreferences -import im.vector.app.features.settings.devices.v2.notification.UpdateNotificationSettingsAccountDataUseCase 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 @@ -36,13 +33,9 @@ import org.junit.Test class NotificationsSettingUpdaterTest { private val fakeUpdateEnableNotificationsSettingOnChangeUseCase = mockk() - private val fakeVectorPreferences = FakeVectorPreferences() - private val fakeUpdateNotificationSettingsAccountDataUseCase = mockk() private val notificationsSettingUpdater = NotificationsSettingUpdater( updateEnableNotificationsSettingOnChangeUseCase = fakeUpdateEnableNotificationsSettingOnChangeUseCase, - vectorPreferences = fakeVectorPreferences.instance, - updateNotificationSettingsAccountDataUseCase = fakeUpdateNotificationSettingsAccountDataUseCase, ) @Before @@ -69,38 +62,4 @@ class NotificationsSettingUpdaterTest { // Then coVerify { fakeUpdateEnableNotificationsSettingOnChangeUseCase.execute(aSession) } } - - @Test - fun `given a session when calling onSessionStarted then update account data on background sync preference change`() = runTest { - // Given - val aSession = FakeSession() - every { aSession.coroutineScope } returns this - coJustRun { fakeUpdateEnableNotificationsSettingOnChangeUseCase.execute(any()) } - coJustRun { fakeUpdateNotificationSettingsAccountDataUseCase.execute(any()) } - fakeVectorPreferences.givenChangeOnPreference(VectorPreferences.SETTINGS_FDROID_BACKGROUND_SYNC_MODE) - - // When - notificationsSettingUpdater.onSessionStarted(aSession) - advanceUntilIdle() - - // Then - coVerify { fakeUpdateNotificationSettingsAccountDataUseCase.execute(aSession) } - } - - @Test - fun `given a session when calling onSessionStarted then account data is not updated on other preference change`() = runTest { - // Given - val aSession = FakeSession() - every { aSession.coroutineScope } returns this - coJustRun { fakeUpdateEnableNotificationsSettingOnChangeUseCase.execute(any()) } - coJustRun { fakeUpdateNotificationSettingsAccountDataUseCase.execute(any()) } - fakeVectorPreferences.givenChangeOnPreference("key") - - // When - notificationsSettingUpdater.onSessionStarted(aSession) - advanceUntilIdle() - - // Then - coVerify(inverse = true) { fakeUpdateNotificationSettingsAccountDataUseCase.execute(aSession) } - } } From 5248a69fe2bd498a892f5484fff96b60ccec0154 Mon Sep 17 00:00:00 2001 From: Maxime NATUREL Date: Thu, 1 Dec 2022 17:33:27 +0100 Subject: [PATCH 18/21] Updating existing unit tests --- ...leNotificationsForCurrentSessionUseCase.kt | 1 - ...leNotificationsForCurrentSessionUseCase.kt | 2 -- ...rSettingsNotificationPreferenceFragment.kt | 1 - ...SettingsNotificationPreferenceViewEvent.kt | 1 - ...SettingsNotificationPreferenceViewModel.kt | 4 --- .../overview/SessionOverviewViewModelTest.kt | 2 +- ...tificationsForCurrentSessionUseCaseTest.kt | 20 +++++------ ...tificationsForCurrentSessionUseCaseTest.kt | 34 ++++--------------- ...ingsNotificationPreferenceViewModelTest.kt | 27 +++------------ 9 files changed, 21 insertions(+), 71 deletions(-) diff --git a/vector/src/main/java/im/vector/app/features/settings/notifications/DisableNotificationsForCurrentSessionUseCase.kt b/vector/src/main/java/im/vector/app/features/settings/notifications/DisableNotificationsForCurrentSessionUseCase.kt index 9173107018..daa58578d6 100644 --- a/vector/src/main/java/im/vector/app/features/settings/notifications/DisableNotificationsForCurrentSessionUseCase.kt +++ b/vector/src/main/java/im/vector/app/features/settings/notifications/DisableNotificationsForCurrentSessionUseCase.kt @@ -30,7 +30,6 @@ class DisableNotificationsForCurrentSessionUseCase @Inject constructor( private val unregisterUnifiedPushUseCase: UnregisterUnifiedPushUseCase, ) { - // TODO update unit tests suspend fun execute() { val session = activeSessionHolder.getSafeActiveSession() ?: return toggleNotificationsForCurrentSessionUseCase.execute(enabled = false) diff --git a/vector/src/main/java/im/vector/app/features/settings/notifications/EnableNotificationsForCurrentSessionUseCase.kt b/vector/src/main/java/im/vector/app/features/settings/notifications/EnableNotificationsForCurrentSessionUseCase.kt index 663c5004e8..daf3890e33 100644 --- a/vector/src/main/java/im/vector/app/features/settings/notifications/EnableNotificationsForCurrentSessionUseCase.kt +++ b/vector/src/main/java/im/vector/app/features/settings/notifications/EnableNotificationsForCurrentSessionUseCase.kt @@ -30,11 +30,9 @@ class EnableNotificationsForCurrentSessionUseCase @Inject constructor( sealed interface EnableNotificationsResult { object Success : EnableNotificationsResult - object Failure : EnableNotificationsResult object NeedToAskUserForDistributor : EnableNotificationsResult } - // TODO update unit tests suspend fun execute(distributor: String = ""): EnableNotificationsResult { val pusherForCurrentSession = pushersManager.getPusherForCurrentSession() if (pusherForCurrentSession == null) { diff --git a/vector/src/main/java/im/vector/app/features/settings/notifications/VectorSettingsNotificationPreferenceFragment.kt b/vector/src/main/java/im/vector/app/features/settings/notifications/VectorSettingsNotificationPreferenceFragment.kt index 238ed4218c..490a47ef61 100644 --- a/vector/src/main/java/im/vector/app/features/settings/notifications/VectorSettingsNotificationPreferenceFragment.kt +++ b/vector/src/main/java/im/vector/app/features/settings/notifications/VectorSettingsNotificationPreferenceFragment.kt @@ -119,7 +119,6 @@ class VectorSettingsNotificationPreferenceFragment : VectorSettingsNotificationPreferenceViewEvent.NotificationsForDeviceEnabled -> onNotificationsForDeviceEnabled() VectorSettingsNotificationPreferenceViewEvent.NotificationsForDeviceDisabled -> onNotificationsForDeviceDisabled() is VectorSettingsNotificationPreferenceViewEvent.AskUserForPushDistributor -> askUserToSelectPushDistributor() - VectorSettingsNotificationPreferenceViewEvent.EnableNotificationForDeviceFailure -> displayErrorDialog(throwable = null) VectorSettingsNotificationPreferenceViewEvent.NotificationMethodChanged -> onNotificationMethodChanged() } } diff --git a/vector/src/main/java/im/vector/app/features/settings/notifications/VectorSettingsNotificationPreferenceViewEvent.kt b/vector/src/main/java/im/vector/app/features/settings/notifications/VectorSettingsNotificationPreferenceViewEvent.kt index e4cf8e1973..b0ee107769 100644 --- a/vector/src/main/java/im/vector/app/features/settings/notifications/VectorSettingsNotificationPreferenceViewEvent.kt +++ b/vector/src/main/java/im/vector/app/features/settings/notifications/VectorSettingsNotificationPreferenceViewEvent.kt @@ -20,7 +20,6 @@ import im.vector.app.core.platform.VectorViewEvents sealed interface VectorSettingsNotificationPreferenceViewEvent : VectorViewEvents { object NotificationsForDeviceEnabled : VectorSettingsNotificationPreferenceViewEvent - object EnableNotificationForDeviceFailure : VectorSettingsNotificationPreferenceViewEvent object NotificationsForDeviceDisabled : VectorSettingsNotificationPreferenceViewEvent object AskUserForPushDistributor : VectorSettingsNotificationPreferenceViewEvent object NotificationMethodChanged : VectorSettingsNotificationPreferenceViewEvent diff --git a/vector/src/main/java/im/vector/app/features/settings/notifications/VectorSettingsNotificationPreferenceViewModel.kt b/vector/src/main/java/im/vector/app/features/settings/notifications/VectorSettingsNotificationPreferenceViewModel.kt index 357f6458f2..48e82b35e8 100644 --- a/vector/src/main/java/im/vector/app/features/settings/notifications/VectorSettingsNotificationPreferenceViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/settings/notifications/VectorSettingsNotificationPreferenceViewModel.kt @@ -68,9 +68,6 @@ class VectorSettingsNotificationPreferenceViewModel @AssistedInject constructor( private fun handleEnableNotificationsForDevice(distributor: String) { viewModelScope.launch { when (enableNotificationsForCurrentSessionUseCase.execute(distributor)) { - EnableNotificationsForCurrentSessionUseCase.EnableNotificationsResult.Failure -> { - _viewEvents.post(VectorSettingsNotificationPreferenceViewEvent.EnableNotificationForDeviceFailure) - } is EnableNotificationsForCurrentSessionUseCase.EnableNotificationsResult.NeedToAskUserForDistributor -> { _viewEvents.post(VectorSettingsNotificationPreferenceViewEvent.AskUserForPushDistributor) } @@ -81,7 +78,6 @@ class VectorSettingsNotificationPreferenceViewModel @AssistedInject constructor( } } - // TODO update unit tests private fun handleRegisterPushDistributor(distributor: String) { viewModelScope.launch { unregisterUnifiedPushUseCase.execute(pushersManager) diff --git a/vector/src/test/java/im/vector/app/features/settings/devices/v2/overview/SessionOverviewViewModelTest.kt b/vector/src/test/java/im/vector/app/features/settings/devices/v2/overview/SessionOverviewViewModelTest.kt index 6018152176..b0f7a774f2 100644 --- a/vector/src/test/java/im/vector/app/features/settings/devices/v2/overview/SessionOverviewViewModelTest.kt +++ b/vector/src/test/java/im/vector/app/features/settings/devices/v2/overview/SessionOverviewViewModelTest.kt @@ -88,7 +88,7 @@ class SessionOverviewViewModelTest { pendingAuthHandler = fakePendingAuthHandler.instance, activeSessionHolder = fakeActiveSessionHolder.instance, refreshDevicesUseCase = refreshDevicesUseCase, - toggleNotificationUseCase = toggleNotificationUseCase.instance, + toggleNotificationsUseCase = toggleNotificationUseCase.instance, getNotificationsStatusUseCase = fakeGetNotificationsStatusUseCase.instance, vectorPreferences = fakeVectorPreferences.instance, toggleIpAddressVisibilityUseCase = toggleIpAddressVisibilityUseCase, diff --git a/vector/src/test/java/im/vector/app/features/settings/notifications/DisableNotificationsForCurrentSessionUseCaseTest.kt b/vector/src/test/java/im/vector/app/features/settings/notifications/DisableNotificationsForCurrentSessionUseCaseTest.kt index 7e2a118e0e..b7749d0252 100644 --- a/vector/src/test/java/im/vector/app/features/settings/notifications/DisableNotificationsForCurrentSessionUseCaseTest.kt +++ b/vector/src/test/java/im/vector/app/features/settings/notifications/DisableNotificationsForCurrentSessionUseCaseTest.kt @@ -18,7 +18,6 @@ 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.ToggleNotificationsUseCase import im.vector.app.test.fakes.FakeActiveSessionHolder import im.vector.app.test.fakes.FakePushersManager import io.mockk.coJustRun @@ -28,21 +27,19 @@ import io.mockk.mockk import kotlinx.coroutines.test.runTest import org.junit.Test -private const val A_SESSION_ID = "session-id" - class DisableNotificationsForCurrentSessionUseCaseTest { private val fakeActiveSessionHolder = FakeActiveSessionHolder() private val fakePushersManager = FakePushersManager() private val fakeCheckIfCanToggleNotificationsViaPusherUseCase = mockk() - private val fakeToggleNotificationsUseCase = mockk() + private val fakeToggleNotificationsForCurrentSessionUseCase = mockk() private val fakeUnregisterUnifiedPushUseCase = mockk() private val disableNotificationsForCurrentSessionUseCase = DisableNotificationsForCurrentSessionUseCase( activeSessionHolder = fakeActiveSessionHolder.instance, pushersManager = fakePushersManager.instance, checkIfCanToggleNotificationsViaPusherUseCase = fakeCheckIfCanToggleNotificationsViaPusherUseCase, - toggleNotificationUseCase = fakeToggleNotificationsUseCase, + toggleNotificationsForCurrentSessionUseCase = fakeToggleNotificationsForCurrentSessionUseCase, unregisterUnifiedPushUseCase = fakeUnregisterUnifiedPushUseCase, ) @@ -50,24 +47,25 @@ class DisableNotificationsForCurrentSessionUseCaseTest { fun `given toggle via pusher is possible when execute then disable notification via toggle of existing pusher`() = runTest { // Given val fakeSession = fakeActiveSessionHolder.fakeSession - fakeSession.givenSessionId(A_SESSION_ID) every { fakeCheckIfCanToggleNotificationsViaPusherUseCase.execute(fakeSession) } returns true - coJustRun { fakeToggleNotificationsUseCase.execute(A_SESSION_ID, any()) } + coJustRun { fakeToggleNotificationsForCurrentSessionUseCase.execute(any()) } // When disableNotificationsForCurrentSessionUseCase.execute() // Then - coVerify { fakeToggleNotificationsUseCase.execute(A_SESSION_ID, false) } + coVerify { fakeToggleNotificationsForCurrentSessionUseCase.execute(false) } + coVerify(inverse = true) { + fakeUnregisterUnifiedPushUseCase.execute(any()) + } } @Test fun `given toggle via pusher is NOT possible when execute then disable notification by unregistering the pusher`() = runTest { // Given val fakeSession = fakeActiveSessionHolder.fakeSession - fakeSession.givenSessionId(A_SESSION_ID) every { fakeCheckIfCanToggleNotificationsViaPusherUseCase.execute(fakeSession) } returns false - coJustRun { fakeToggleNotificationsUseCase.execute(A_SESSION_ID, any()) } + coJustRun { fakeToggleNotificationsForCurrentSessionUseCase.execute(any()) } coJustRun { fakeUnregisterUnifiedPushUseCase.execute(any()) } // When @@ -75,7 +73,7 @@ class DisableNotificationsForCurrentSessionUseCaseTest { // Then coVerify { - fakeToggleNotificationsUseCase.execute(A_SESSION_ID, false) + fakeToggleNotificationsForCurrentSessionUseCase.execute(false) fakeUnregisterUnifiedPushUseCase.execute(fakePushersManager.instance) } } diff --git a/vector/src/test/java/im/vector/app/features/settings/notifications/EnableNotificationsForCurrentSessionUseCaseTest.kt b/vector/src/test/java/im/vector/app/features/settings/notifications/EnableNotificationsForCurrentSessionUseCaseTest.kt index 7beab170f2..d58ba7645c 100644 --- a/vector/src/test/java/im/vector/app/features/settings/notifications/EnableNotificationsForCurrentSessionUseCaseTest.kt +++ b/vector/src/test/java/im/vector/app/features/settings/notifications/EnableNotificationsForCurrentSessionUseCaseTest.kt @@ -18,10 +18,9 @@ package im.vector.app.features.settings.notifications import im.vector.app.core.pushers.EnsureFcmTokenIsRetrievedUseCase import im.vector.app.core.pushers.RegisterUnifiedPushUseCase -import im.vector.app.features.settings.devices.v2.notification.ToggleNotificationsUseCase -import im.vector.app.test.fakes.FakeActiveSessionHolder import im.vector.app.test.fakes.FakePushersManager import io.mockk.coJustRun +import io.mockk.coVerify import io.mockk.every import io.mockk.justRun import io.mockk.mockk @@ -30,20 +29,16 @@ import kotlinx.coroutines.test.runTest import org.amshove.kluent.shouldBe import org.junit.Test -private const val A_SESSION_ID = "session-id" - class EnableNotificationsForCurrentSessionUseCaseTest { - private val fakeActiveSessionHolder = FakeActiveSessionHolder() private val fakePushersManager = FakePushersManager() - private val fakeToggleNotificationsUseCase = mockk() + private val fakeToggleNotificationsForCurrentSessionUseCase = mockk() private val fakeRegisterUnifiedPushUseCase = mockk() private val fakeEnsureFcmTokenIsRetrievedUseCase = mockk() private val enableNotificationsForCurrentSessionUseCase = EnableNotificationsForCurrentSessionUseCase( - activeSessionHolder = fakeActiveSessionHolder.instance, pushersManager = fakePushersManager.instance, - toggleNotificationUseCase = fakeToggleNotificationsUseCase, + toggleNotificationsForCurrentSessionUseCase = fakeToggleNotificationsForCurrentSessionUseCase, registerUnifiedPushUseCase = fakeRegisterUnifiedPushUseCase, ensureFcmTokenIsRetrievedUseCase = fakeEnsureFcmTokenIsRetrievedUseCase, ) @@ -52,12 +47,10 @@ class EnableNotificationsForCurrentSessionUseCaseTest { fun `given no existing pusher and a registered distributor when execute then a new pusher is registered and result is success`() = runTest { // Given val aDistributor = "distributor" - val fakeSession = fakeActiveSessionHolder.fakeSession - fakeSession.givenSessionId(A_SESSION_ID) fakePushersManager.givenGetPusherForCurrentSessionReturns(null) every { fakeRegisterUnifiedPushUseCase.execute(any()) } returns RegisterUnifiedPushUseCase.RegisterUnifiedPushResult.Success justRun { fakeEnsureFcmTokenIsRetrievedUseCase.execute(any(), any()) } - coJustRun { fakeToggleNotificationsUseCase.execute(A_SESSION_ID, any()) } + coJustRun { fakeToggleNotificationsForCurrentSessionUseCase.execute(any()) } // When val result = enableNotificationsForCurrentSessionUseCase.execute(aDistributor) @@ -68,6 +61,9 @@ class EnableNotificationsForCurrentSessionUseCaseTest { fakeRegisterUnifiedPushUseCase.execute(aDistributor) fakeEnsureFcmTokenIsRetrievedUseCase.execute(fakePushersManager.instance, registerPusher = true) } + coVerify { + fakeToggleNotificationsForCurrentSessionUseCase.execute(enabled = true) + } } @Test @@ -86,20 +82,4 @@ class EnableNotificationsForCurrentSessionUseCaseTest { fakeRegisterUnifiedPushUseCase.execute(aDistributor) } } - - @Test - fun `given no deviceId for current session when execute then result is failure`() = runTest { - // Given - val aDistributor = "distributor" - val fakeSession = fakeActiveSessionHolder.fakeSession - fakeSession.givenSessionId(null) - fakePushersManager.givenGetPusherForCurrentSessionReturns(mockk()) - every { fakeRegisterUnifiedPushUseCase.execute(any()) } returns RegisterUnifiedPushUseCase.RegisterUnifiedPushResult.NeedToAskUserForDistributor - - // When - val result = enableNotificationsForCurrentSessionUseCase.execute(aDistributor) - - // Then - result shouldBe EnableNotificationsForCurrentSessionUseCase.EnableNotificationsResult.Failure - } } diff --git a/vector/src/test/java/im/vector/app/features/settings/notifications/VectorSettingsNotificationPreferenceViewModelTest.kt b/vector/src/test/java/im/vector/app/features/settings/notifications/VectorSettingsNotificationPreferenceViewModelTest.kt index f9d7527316..270447c461 100644 --- a/vector/src/test/java/im/vector/app/features/settings/notifications/VectorSettingsNotificationPreferenceViewModelTest.kt +++ b/vector/src/test/java/im/vector/app/features/settings/notifications/VectorSettingsNotificationPreferenceViewModelTest.kt @@ -46,6 +46,7 @@ class VectorSettingsNotificationPreferenceViewModelTest { private val fakeUnregisterUnifiedPushUseCase = mockk() private val fakeRegisterUnifiedPushUseCase = mockk() private val fakeEnsureFcmTokenIsRetrievedUseCase = mockk() + private val fakeToggleNotificationsForCurrentSessionUseCase = mockk() private fun createViewModel() = VectorSettingsNotificationPreferenceViewModel( initialState = VectorDummyViewState(), @@ -56,6 +57,7 @@ class VectorSettingsNotificationPreferenceViewModelTest { unregisterUnifiedPushUseCase = fakeUnregisterUnifiedPushUseCase, registerUnifiedPushUseCase = fakeRegisterUnifiedPushUseCase, ensureFcmTokenIsRetrievedUseCase = fakeEnsureFcmTokenIsRetrievedUseCase, + toggleNotificationsForCurrentSessionUseCase = fakeToggleNotificationsForCurrentSessionUseCase, ) @Test @@ -125,29 +127,6 @@ class VectorSettingsNotificationPreferenceViewModelTest { } } - @Test - fun `given EnableNotificationsForDevice action and enable failure when handling action then enable use case is called`() { - // Given - val viewModel = createViewModel() - val aDistributor = "aDistributor" - val action = VectorSettingsNotificationPreferenceViewAction.EnableNotificationsForDevice(aDistributor) - coEvery { fakeEnableNotificationsForCurrentSessionUseCase.execute(any()) } returns - EnableNotificationsForCurrentSessionUseCase.EnableNotificationsResult.Failure - val expectedEvent = VectorSettingsNotificationPreferenceViewEvent.EnableNotificationForDeviceFailure - - // When - val viewModelTest = viewModel.test() - viewModel.handle(action) - - // Then - viewModelTest - .assertEvent { event -> event == expectedEvent } - .finish() - coVerify { - fakeEnableNotificationsForCurrentSessionUseCase.execute(aDistributor) - } - } - @Test fun `given RegisterPushDistributor action and register success when handling action then register use case is called`() { // Given @@ -158,6 +137,7 @@ class VectorSettingsNotificationPreferenceViewModelTest { coJustRun { fakeUnregisterUnifiedPushUseCase.execute(any()) } val areNotificationsEnabled = true fakeVectorPreferences.givenAreNotificationsEnabledForDevice(areNotificationsEnabled) + coJustRun { fakeToggleNotificationsForCurrentSessionUseCase.execute(any()) } justRun { fakeEnsureFcmTokenIsRetrievedUseCase.execute(any(), any()) } val expectedEvent = VectorSettingsNotificationPreferenceViewEvent.NotificationMethodChanged @@ -173,6 +153,7 @@ class VectorSettingsNotificationPreferenceViewModelTest { fakeUnregisterUnifiedPushUseCase.execute(fakePushersManager.instance) fakeRegisterUnifiedPushUseCase.execute(aDistributor) fakeEnsureFcmTokenIsRetrievedUseCase.execute(fakePushersManager.instance, registerPusher = areNotificationsEnabled) + fakeToggleNotificationsForCurrentSessionUseCase.execute(enabled = areNotificationsEnabled) } } From b78de152286cc75f350a444863e6625d79144752 Mon Sep 17 00:00:00 2001 From: Maxime NATUREL Date: Thu, 1 Dec 2022 17:55:30 +0100 Subject: [PATCH 19/21] Adding unit tests for new toggle notification for current session use case --- ...leNotificationsForCurrentSessionUseCase.kt | 1 - ...tificationsForCurrentSessionUseCaseTest.kt | 117 ++++++++++++++++++ 2 files changed, 117 insertions(+), 1 deletion(-) create mode 100644 vector/src/test/java/im/vector/app/features/settings/notifications/ToggleNotificationsForCurrentSessionUseCaseTest.kt diff --git a/vector/src/main/java/im/vector/app/features/settings/notifications/ToggleNotificationsForCurrentSessionUseCase.kt b/vector/src/main/java/im/vector/app/features/settings/notifications/ToggleNotificationsForCurrentSessionUseCase.kt index 64b4b1bb89..3dc73f0a31 100644 --- a/vector/src/main/java/im/vector/app/features/settings/notifications/ToggleNotificationsForCurrentSessionUseCase.kt +++ b/vector/src/main/java/im/vector/app/features/settings/notifications/ToggleNotificationsForCurrentSessionUseCase.kt @@ -33,7 +33,6 @@ class ToggleNotificationsForCurrentSessionUseCase @Inject constructor( private val deleteNotificationSettingsAccountDataUseCase: DeleteNotificationSettingsAccountDataUseCase, ) { - // TODO add unit tests suspend fun execute(enabled: Boolean) { val session = activeSessionHolder.getSafeActiveSession() ?: return val deviceId = session.sessionParams.deviceId ?: return diff --git a/vector/src/test/java/im/vector/app/features/settings/notifications/ToggleNotificationsForCurrentSessionUseCaseTest.kt b/vector/src/test/java/im/vector/app/features/settings/notifications/ToggleNotificationsForCurrentSessionUseCaseTest.kt new file mode 100644 index 0000000000..f49aafab8a --- /dev/null +++ b/vector/src/test/java/im/vector/app/features/settings/notifications/ToggleNotificationsForCurrentSessionUseCaseTest.kt @@ -0,0 +1,117 @@ +/* + * 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.notifications + +import im.vector.app.features.settings.devices.v2.notification.CheckIfCanToggleNotificationsViaPusherUseCase +import im.vector.app.features.settings.devices.v2.notification.DeleteNotificationSettingsAccountDataUseCase +import im.vector.app.features.settings.devices.v2.notification.SetNotificationSettingsAccountDataUseCase +import im.vector.app.test.fakes.FakeActiveSessionHolder +import im.vector.app.test.fakes.FakeUnifiedPushHelper +import im.vector.app.test.fixtures.PusherFixture +import io.mockk.coJustRun +import io.mockk.coVerify +import io.mockk.every +import io.mockk.mockk +import kotlinx.coroutines.test.runTest +import org.junit.Test +import org.matrix.android.sdk.api.account.LocalNotificationSettingsContent + +class ToggleNotificationsForCurrentSessionUseCaseTest { + + private val fakeActiveSessionHolder = FakeActiveSessionHolder() + private val fakeUnifiedPushHelper = FakeUnifiedPushHelper() + private val fakeCheckIfCanToggleNotificationsViaPusherUseCase = mockk() + private val fakeSetNotificationSettingsAccountDataUseCase = mockk() + private val fakeDeleteNotificationSettingsAccountDataUseCase = mockk() + + private val toggleNotificationsForCurrentSessionUseCase = ToggleNotificationsForCurrentSessionUseCase( + activeSessionHolder = fakeActiveSessionHolder.instance, + unifiedPushHelper = fakeUnifiedPushHelper.instance, + checkIfCanToggleNotificationsViaPusherUseCase = fakeCheckIfCanToggleNotificationsViaPusherUseCase, + setNotificationSettingsAccountDataUseCase = fakeSetNotificationSettingsAccountDataUseCase, + deleteNotificationSettingsAccountDataUseCase = fakeDeleteNotificationSettingsAccountDataUseCase, + ) + + @Test + fun `given background sync is enabled when execute then set the related account data with correct value`() = runTest { + // Given + val enabled = true + val aDeviceId = "deviceId" + fakeUnifiedPushHelper.givenIsBackgroundSyncReturns(true) + fakeActiveSessionHolder.fakeSession.givenSessionId(aDeviceId) + coJustRun { fakeSetNotificationSettingsAccountDataUseCase.execute(any(), any(), any()) } + val expectedNotificationContent = LocalNotificationSettingsContent(isSilenced = !enabled) + + // When + toggleNotificationsForCurrentSessionUseCase.execute(enabled) + + // Then + coVerify { + fakeSetNotificationSettingsAccountDataUseCase.execute( + fakeActiveSessionHolder.fakeSession, + aDeviceId, + expectedNotificationContent + ) + } + } + + @Test + fun `given background sync is not enabled and toggle pusher is possible when execute then delete any related account data and toggle pusher`() = runTest { + // Given + val enabled = true + val aDeviceId = "deviceId" + fakeUnifiedPushHelper.givenIsBackgroundSyncReturns(false) + fakeActiveSessionHolder.fakeSession.givenSessionId(aDeviceId) + coJustRun { fakeDeleteNotificationSettingsAccountDataUseCase.execute(any()) } + every { fakeCheckIfCanToggleNotificationsViaPusherUseCase.execute(any()) } returns true + val aPusher = PusherFixture.aPusher(deviceId = aDeviceId) + fakeActiveSessionHolder.fakeSession.fakePushersService.givenGetPushers(listOf(aPusher)) + + // When + toggleNotificationsForCurrentSessionUseCase.execute(enabled) + + // Then + coVerify { + fakeDeleteNotificationSettingsAccountDataUseCase.execute(fakeActiveSessionHolder.fakeSession) + fakeCheckIfCanToggleNotificationsViaPusherUseCase.execute(fakeActiveSessionHolder.fakeSession) + } + fakeActiveSessionHolder.fakeSession.fakePushersService.verifyTogglePusherCalled(aPusher, enabled) + } + + @Test + fun `given background sync is not enabled and toggle pusher is not possible when execute then only delete any related account data`() = runTest { + // Given + val enabled = true + val aDeviceId = "deviceId" + fakeUnifiedPushHelper.givenIsBackgroundSyncReturns(false) + fakeActiveSessionHolder.fakeSession.givenSessionId(aDeviceId) + coJustRun { fakeDeleteNotificationSettingsAccountDataUseCase.execute(any()) } + every { fakeCheckIfCanToggleNotificationsViaPusherUseCase.execute(any()) } returns false + + // When + toggleNotificationsForCurrentSessionUseCase.execute(enabled) + + // Then + coVerify { + fakeDeleteNotificationSettingsAccountDataUseCase.execute(fakeActiveSessionHolder.fakeSession) + fakeCheckIfCanToggleNotificationsViaPusherUseCase.execute(fakeActiveSessionHolder.fakeSession) + } + coVerify(inverse = true) { + fakeActiveSessionHolder.fakeSession.fakePushersService.togglePusher(any(), any()) + } + } +} From e09b9a2ce0e245e1311374135807c835bf14e419 Mon Sep 17 00:00:00 2001 From: Maxime NATUREL Date: Fri, 2 Dec 2022 10:03:37 +0100 Subject: [PATCH 20/21] Fixing wrong notification status when no registered pusher for the session --- .../notification/GetNotificationsStatusUseCase.kt | 8 +++++++- .../GetNotificationsStatusUseCaseTest.kt | 14 ++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/GetNotificationsStatusUseCase.kt b/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/GetNotificationsStatusUseCase.kt index f98fd63efb..ae7e859573 100644 --- a/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/GetNotificationsStatusUseCase.kt +++ b/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/GetNotificationsStatusUseCase.kt @@ -51,7 +51,13 @@ class GetNotificationsStatusUseCase @Inject constructor( .livePushers() .map { it.filter { pusher -> pusher.deviceId == deviceId } } .map { it.takeIf { it.isNotEmpty() }?.any { pusher -> pusher.enabled } } - .map { if (it == true) NotificationsStatus.ENABLED else NotificationsStatus.DISABLED } + .map { + when (it) { + true -> NotificationsStatus.ENABLED + false -> NotificationsStatus.DISABLED + else -> NotificationsStatus.NOT_SUPPORTED + } + } .distinctUntilChanged() } else { flowOf(NotificationsStatus.NOT_SUPPORTED) diff --git a/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/GetNotificationsStatusUseCaseTest.kt b/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/GetNotificationsStatusUseCaseTest.kt index d4c3aa5788..3c454f7965 100644 --- a/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/GetNotificationsStatusUseCaseTest.kt +++ b/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/GetNotificationsStatusUseCaseTest.kt @@ -105,6 +105,20 @@ class GetNotificationsStatusUseCaseTest { result.firstOrNull() shouldBeEqualTo NotificationsStatus.ENABLED } + @Test + fun `given toggle via pusher is supported and no registered pusher when execute then resulting flow contains NOT_SUPPORTED value`() = runTest { + // Given + fakeSession.pushersService().givenPushersLive(emptyList()) + every { fakeCheckIfCanToggleNotificationsViaAccountDataUseCase.execute(fakeSession, A_DEVICE_ID) } returns false + every { fakeCanToggleNotificationsViaPusherUseCase.execute(fakeSession) } returns flowOf(true) + + // When + val result = getNotificationsStatusUseCase.execute(fakeSession, A_DEVICE_ID) + + // Then + result.firstOrNull() shouldBeEqualTo NotificationsStatus.NOT_SUPPORTED + } + @Test fun `given current session and toggle via account data is supported when execute then resulting flow contains status based on settings value`() = runTest { // Given From 635f975b6cb5bbb8170a982e4b713e432861e9d4 Mon Sep 17 00:00:00 2001 From: Maxime NATUREL Date: Fri, 2 Dec 2022 15:13:10 +0100 Subject: [PATCH 21/21] Fix missing unregister of pusher when notifications are disabled --- .../features/home/HomeActivityViewModel.kt | 10 +++++++ ...leNotificationsForCurrentSessionUseCase.kt | 11 +------- ...tificationsForCurrentSessionUseCaseTest.kt | 28 +------------------ 3 files changed, 12 insertions(+), 37 deletions(-) diff --git a/vector/src/main/java/im/vector/app/features/home/HomeActivityViewModel.kt b/vector/src/main/java/im/vector/app/features/home/HomeActivityViewModel.kt index 26034fc09c..a54ce2cff3 100644 --- a/vector/src/main/java/im/vector/app/features/home/HomeActivityViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/home/HomeActivityViewModel.kt @@ -29,6 +29,7 @@ import im.vector.app.core.platform.VectorViewModel import im.vector.app.core.pushers.EnsureFcmTokenIsRetrievedUseCase import im.vector.app.core.pushers.PushersManager import im.vector.app.core.pushers.RegisterUnifiedPushUseCase +import im.vector.app.core.pushers.UnregisterUnifiedPushUseCase import im.vector.app.features.analytics.AnalyticsConfig import im.vector.app.features.analytics.AnalyticsTracker import im.vector.app.features.analytics.extensions.toAnalyticsType @@ -92,6 +93,7 @@ class HomeActivityViewModel @AssistedInject constructor( private val stopOngoingVoiceBroadcastUseCase: StopOngoingVoiceBroadcastUseCase, private val pushersManager: PushersManager, private val registerUnifiedPushUseCase: RegisterUnifiedPushUseCase, + private val unregisterUnifiedPushUseCase: UnregisterUnifiedPushUseCase, private val ensureFcmTokenIsRetrievedUseCase: EnsureFcmTokenIsRetrievedUseCase, ) : VectorViewModel(initialState) { @@ -130,6 +132,8 @@ class HomeActivityViewModel @AssistedInject constructor( private fun registerUnifiedPushIfNeeded() { if (vectorPreferences.areNotificationEnabledForDevice()) { registerUnifiedPush(distributor = "") + } else { + unregisterUnifiedPush() } } @@ -146,6 +150,12 @@ class HomeActivityViewModel @AssistedInject constructor( } } + private fun unregisterUnifiedPush() { + viewModelScope.launch { + unregisterUnifiedPushUseCase.execute(pushersManager) + } + } + private fun observeReleaseNotes() = withState { state -> if (vectorPreferences.isNewAppLayoutEnabled()) { // we don't want to show release notes for new users or after relogin diff --git a/vector/src/main/java/im/vector/app/features/settings/notifications/DisableNotificationsForCurrentSessionUseCase.kt b/vector/src/main/java/im/vector/app/features/settings/notifications/DisableNotificationsForCurrentSessionUseCase.kt index daa58578d6..0c50a296f3 100644 --- a/vector/src/main/java/im/vector/app/features/settings/notifications/DisableNotificationsForCurrentSessionUseCase.kt +++ b/vector/src/main/java/im/vector/app/features/settings/notifications/DisableNotificationsForCurrentSessionUseCase.kt @@ -16,27 +16,18 @@ package im.vector.app.features.settings.notifications -import im.vector.app.core.di.ActiveSessionHolder import im.vector.app.core.pushers.PushersManager import im.vector.app.core.pushers.UnregisterUnifiedPushUseCase -import im.vector.app.features.settings.devices.v2.notification.CheckIfCanToggleNotificationsViaPusherUseCase import javax.inject.Inject class DisableNotificationsForCurrentSessionUseCase @Inject constructor( - private val activeSessionHolder: ActiveSessionHolder, private val pushersManager: PushersManager, - private val checkIfCanToggleNotificationsViaPusherUseCase: CheckIfCanToggleNotificationsViaPusherUseCase, private val toggleNotificationsForCurrentSessionUseCase: ToggleNotificationsForCurrentSessionUseCase, private val unregisterUnifiedPushUseCase: UnregisterUnifiedPushUseCase, ) { suspend fun execute() { - val session = activeSessionHolder.getSafeActiveSession() ?: return toggleNotificationsForCurrentSessionUseCase.execute(enabled = false) - - // handle case when server does not support toggle of pusher - if (!checkIfCanToggleNotificationsViaPusherUseCase.execute(session)) { - unregisterUnifiedPushUseCase.execute(pushersManager) - } + unregisterUnifiedPushUseCase.execute(pushersManager) } } diff --git a/vector/src/test/java/im/vector/app/features/settings/notifications/DisableNotificationsForCurrentSessionUseCaseTest.kt b/vector/src/test/java/im/vector/app/features/settings/notifications/DisableNotificationsForCurrentSessionUseCaseTest.kt index b7749d0252..669b20fc1a 100644 --- a/vector/src/test/java/im/vector/app/features/settings/notifications/DisableNotificationsForCurrentSessionUseCaseTest.kt +++ b/vector/src/test/java/im/vector/app/features/settings/notifications/DisableNotificationsForCurrentSessionUseCaseTest.kt @@ -17,54 +17,28 @@ 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.test.fakes.FakeActiveSessionHolder import im.vector.app.test.fakes.FakePushersManager import io.mockk.coJustRun import io.mockk.coVerify -import io.mockk.every import io.mockk.mockk import kotlinx.coroutines.test.runTest import org.junit.Test class DisableNotificationsForCurrentSessionUseCaseTest { - private val fakeActiveSessionHolder = FakeActiveSessionHolder() private val fakePushersManager = FakePushersManager() - private val fakeCheckIfCanToggleNotificationsViaPusherUseCase = mockk() private val fakeToggleNotificationsForCurrentSessionUseCase = mockk() private val fakeUnregisterUnifiedPushUseCase = mockk() private val disableNotificationsForCurrentSessionUseCase = DisableNotificationsForCurrentSessionUseCase( - activeSessionHolder = fakeActiveSessionHolder.instance, pushersManager = fakePushersManager.instance, - checkIfCanToggleNotificationsViaPusherUseCase = fakeCheckIfCanToggleNotificationsViaPusherUseCase, toggleNotificationsForCurrentSessionUseCase = fakeToggleNotificationsForCurrentSessionUseCase, unregisterUnifiedPushUseCase = fakeUnregisterUnifiedPushUseCase, ) @Test - fun `given toggle via pusher is possible when execute then disable notification via toggle of existing pusher`() = runTest { + fun `when execute then disable notifications and unregister the pusher`() = runTest { // Given - val fakeSession = fakeActiveSessionHolder.fakeSession - every { fakeCheckIfCanToggleNotificationsViaPusherUseCase.execute(fakeSession) } returns true - coJustRun { fakeToggleNotificationsForCurrentSessionUseCase.execute(any()) } - - // When - disableNotificationsForCurrentSessionUseCase.execute() - - // Then - coVerify { fakeToggleNotificationsForCurrentSessionUseCase.execute(false) } - coVerify(inverse = true) { - fakeUnregisterUnifiedPushUseCase.execute(any()) - } - } - - @Test - fun `given toggle via pusher is NOT possible when execute then disable notification by unregistering the pusher`() = runTest { - // Given - val fakeSession = fakeActiveSessionHolder.fakeSession - every { fakeCheckIfCanToggleNotificationsViaPusherUseCase.execute(fakeSession) } returns false coJustRun { fakeToggleNotificationsForCurrentSessionUseCase.execute(any()) } coJustRun { fakeUnregisterUnifiedPushUseCase.execute(any()) }