Adding tests for notifications setting updater
This commit is contained in:
parent
637961bbb1
commit
7c10a4cb21
|
@ -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 {
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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<UpdateEnableNotificationsSettingOnChangeUseCase>()
|
||||
private val fakeVectorPreferences = FakeVectorPreferences()
|
||||
private val fakeUpdateNotificationSettingsAccountDataUseCase = mockk<UpdateNotificationSettingsAccountDataUseCase>()
|
||||
|
||||
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) }
|
||||
}
|
||||
}
|
|
@ -26,6 +26,6 @@ class FakeNotificationsSettingUpdater {
|
|||
val instance = mockk<NotificationsSettingUpdater>()
|
||||
|
||||
fun givenOnSessionsStarted(session: Session) {
|
||||
justRun { instance.onSessionsStarted(session) }
|
||||
justRun { instance.onSessionStarted(session) }
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<OnSharedPreferenceChangeListener>().onSharedPreferenceChanged(mockk(), key)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue