From 7c10a4cb21a0bc0a044698ea633c5a76626bdbd6 Mon Sep 17 00:00:00 2001 From: Maxime NATUREL Date: Wed, 23 Nov 2022 15:12:51 +0100 Subject: [PATCH] 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) + } + } }