Adding unit tests for register and unregister use cases

This commit is contained in:
Maxime NATUREL 2022-11-30 16:02:55 +01:00
parent a3815d7012
commit 46ccf4d73f
10 changed files with 315 additions and 2 deletions

View File

@ -32,7 +32,6 @@ class RegisterUnifiedPushUseCase @Inject constructor(
object NeedToAskUserForDistributor : RegisterUnifiedPushResult
}
// TODO add unit tests
fun execute(distributor: String = ""): RegisterUnifiedPushResult {
if (distributor.isNotEmpty()) {
saveAndRegisterApp(distributor)

View File

@ -31,7 +31,6 @@ class UnregisterUnifiedPushUseCase @Inject constructor(
private val unifiedPushHelper: UnifiedPushHelper,
) {
// TODO add unit tests
suspend fun execute(pushersManager: PushersManager?) {
val mode = BackgroundSyncMode.FDROID_BACKGROUND_SYNC_MODE_FOR_REALTIME
vectorPreferences.setFdroidSyncBackgroundMode(mode)

View File

@ -0,0 +1,158 @@
/*
* 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.pushers
import im.vector.app.test.fakes.FakeContext
import im.vector.app.test.fakes.FakeVectorFeatures
import io.mockk.every
import io.mockk.justRun
import io.mockk.mockkStatic
import io.mockk.unmockkAll
import io.mockk.verify
import io.mockk.verifyAll
import io.mockk.verifyOrder
import kotlinx.coroutines.test.runTest
import org.amshove.kluent.shouldBe
import org.junit.After
import org.junit.Before
import org.junit.Test
import org.unifiedpush.android.connector.UnifiedPush
class RegisterUnifiedPushUseCaseTest {
private val fakeContext = FakeContext()
private val fakeVectorFeatures = FakeVectorFeatures()
private val registerUnifiedPushUseCase = RegisterUnifiedPushUseCase(
context = fakeContext.instance,
vectorFeatures = fakeVectorFeatures,
)
@Before
fun setup() {
mockkStatic(UnifiedPush::class)
}
@After
fun tearDown() {
unmockkAll()
}
@Test
fun `given non empty distributor when execute then distributor is saved and app is registered`() = runTest {
// Given
val aDistributor = "distributor"
justRun { UnifiedPush.registerApp(any()) }
justRun { UnifiedPush.saveDistributor(any(), any()) }
// When
val result = registerUnifiedPushUseCase.execute(aDistributor)
// Then
result shouldBe RegisterUnifiedPushUseCase.RegisterUnifiedPushResult.Success
verifyOrder {
UnifiedPush.saveDistributor(fakeContext.instance, aDistributor)
UnifiedPush.registerApp(fakeContext.instance)
}
}
@Test
fun `given external distributors are not allowed when execute then internal distributor is saved and app is registered`() = runTest {
// Given
val aPackageName = "packageName"
fakeContext.givenPackageName(aPackageName)
justRun { UnifiedPush.registerApp(any()) }
justRun { UnifiedPush.saveDistributor(any(), any()) }
fakeVectorFeatures.givenExternalDistributorsAreAllowed(false)
// When
val result = registerUnifiedPushUseCase.execute()
// Then
result shouldBe RegisterUnifiedPushUseCase.RegisterUnifiedPushResult.Success
verifyOrder {
UnifiedPush.saveDistributor(fakeContext.instance, aPackageName)
UnifiedPush.registerApp(fakeContext.instance)
}
}
@Test
fun `given a saved distributor and external distributors are allowed when execute then app is registered`() = runTest {
// Given
justRun { UnifiedPush.registerApp(any()) }
val aDistributor = "distributor"
every { UnifiedPush.getDistributor(any()) } returns aDistributor
fakeVectorFeatures.givenExternalDistributorsAreAllowed(true)
// When
val result = registerUnifiedPushUseCase.execute()
// Then
result shouldBe RegisterUnifiedPushUseCase.RegisterUnifiedPushResult.Success
verifyAll {
UnifiedPush.getDistributor(fakeContext.instance)
UnifiedPush.registerApp(fakeContext.instance)
}
}
@Test
fun `given no saved distributor and a unique distributor available when execute then the distributor is saved and app is registered`() = runTest {
// Given
justRun { UnifiedPush.registerApp(any()) }
justRun { UnifiedPush.saveDistributor(any(), any()) }
every { UnifiedPush.getDistributor(any()) } returns ""
fakeVectorFeatures.givenExternalDistributorsAreAllowed(true)
val aDistributor = "distributor"
every { UnifiedPush.getDistributors(any()) } returns listOf(aDistributor)
// When
val result = registerUnifiedPushUseCase.execute()
// Then
result shouldBe RegisterUnifiedPushUseCase.RegisterUnifiedPushResult.Success
verifyOrder {
UnifiedPush.getDistributor(fakeContext.instance)
UnifiedPush.getDistributors(fakeContext.instance)
UnifiedPush.saveDistributor(fakeContext.instance, aDistributor)
UnifiedPush.registerApp(fakeContext.instance)
}
}
@Test
fun `given no saved distributor and multiple distributors available when execute then result is to ask user`() = runTest {
// Given
every { UnifiedPush.getDistributor(any()) } returns ""
fakeVectorFeatures.givenExternalDistributorsAreAllowed(true)
val aDistributor1 = "distributor1"
val aDistributor2 = "distributor2"
every { UnifiedPush.getDistributors(any()) } returns listOf(aDistributor1, aDistributor2)
// When
val result = registerUnifiedPushUseCase.execute()
// Then
result shouldBe RegisterUnifiedPushUseCase.RegisterUnifiedPushResult.NeedToAskUserForDistributor
verifyOrder {
UnifiedPush.getDistributor(fakeContext.instance)
UnifiedPush.getDistributors(fakeContext.instance)
}
verify(inverse = true) {
UnifiedPush.saveDistributor(any(), any())
UnifiedPush.registerApp(any())
}
}
}

View File

@ -0,0 +1,83 @@
/*
* 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.pushers
import im.vector.app.features.settings.BackgroundSyncMode
import im.vector.app.test.fakes.FakeContext
import im.vector.app.test.fakes.FakePushersManager
import im.vector.app.test.fakes.FakeUnifiedPushHelper
import im.vector.app.test.fakes.FakeUnifiedPushStore
import im.vector.app.test.fakes.FakeVectorPreferences
import io.mockk.justRun
import io.mockk.mockkStatic
import io.mockk.unmockkAll
import io.mockk.verifyAll
import kotlinx.coroutines.test.runTest
import org.junit.After
import org.junit.Before
import org.junit.Test
import org.unifiedpush.android.connector.UnifiedPush
class UnregisterUnifiedPushUseCaseTest {
private val fakeContext = FakeContext()
private val fakeVectorPreferences = FakeVectorPreferences()
private val fakeUnifiedPushStore = FakeUnifiedPushStore()
private val fakeUnifiedPushHelper = FakeUnifiedPushHelper()
private val unregisterUnifiedPushUseCase = UnregisterUnifiedPushUseCase(
context = fakeContext.instance,
vectorPreferences = fakeVectorPreferences.instance,
unifiedPushStore = fakeUnifiedPushStore.instance,
unifiedPushHelper = fakeUnifiedPushHelper.instance,
)
@Before
fun setup() {
mockkStatic(UnifiedPush::class)
}
@After
fun tearDown() {
unmockkAll()
}
@Test
fun `given pushersManager when execute then unregister and clean everything which is needed`() = runTest {
// Given
val aEndpoint = "endpoint"
fakeUnifiedPushHelper.givenGetEndpointOrTokenReturns(aEndpoint)
val aPushersManager = FakePushersManager()
aPushersManager.givenUnregisterPusher(aEndpoint)
justRun { UnifiedPush.unregisterApp(any()) }
fakeVectorPreferences.givenSetFdroidSyncBackgroundMode(BackgroundSyncMode.FDROID_BACKGROUND_SYNC_MODE_FOR_REALTIME)
fakeUnifiedPushStore.givenStorePushGateway(null)
fakeUnifiedPushStore.givenStoreUpEndpoint(null)
// When
unregisterUnifiedPushUseCase.execute(aPushersManager.instance)
// Then
fakeVectorPreferences.verifySetFdroidSyncBackgroundMode(BackgroundSyncMode.FDROID_BACKGROUND_SYNC_MODE_FOR_REALTIME)
aPushersManager.verifyUnregisterPusher(aEndpoint)
verifyAll {
UnifiedPush.unregisterApp(fakeContext.instance)
}
fakeUnifiedPushStore.verifyStorePushGateway(null)
fakeUnifiedPushStore.verifyStoreUpEndpoint(null)
}
}

View File

@ -81,4 +81,8 @@ class FakeContext(
givenService(Context.CLIPBOARD_SERVICE, ClipboardManager::class.java, fakeClipboardManager.instance)
return fakeClipboardManager
}
fun givenPackageName(name: String) {
every { instance.packageName } returns name
}
}

View File

@ -17,6 +17,8 @@
package im.vector.app.test.fakes
import im.vector.app.core.pushers.PushersManager
import io.mockk.coJustRun
import io.mockk.coVerify
import io.mockk.every
import io.mockk.mockk
import org.matrix.android.sdk.api.session.pushers.Pusher
@ -28,4 +30,12 @@ class FakePushersManager {
fun givenGetPusherForCurrentSessionReturns(pusher: Pusher?) {
every { instance.getPusherForCurrentSession() } returns pusher
}
fun givenUnregisterPusher(pushKey: String) {
coJustRun { instance.unregisterPusher(pushKey) }
}
fun verifyUnregisterPusher(pushKey: String) {
coVerify { instance.unregisterPusher(pushKey) }
}
}

View File

@ -27,4 +27,8 @@ class FakeUnifiedPushHelper {
fun givenIsEmbeddedDistributorReturns(isEmbedded: Boolean) {
every { instance.isEmbeddedDistributor() } returns isEmbedded
}
fun givenGetEndpointOrTokenReturns(endpoint: String?) {
every { instance.getEndpointOrToken() } returns endpoint
}
}

View File

@ -0,0 +1,43 @@
/*
* 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.test.fakes
import im.vector.app.core.pushers.UnifiedPushStore
import io.mockk.justRun
import io.mockk.mockk
import io.mockk.verify
class FakeUnifiedPushStore {
val instance = mockk<UnifiedPushStore>()
fun givenStoreUpEndpoint(endpoint: String?) {
justRun { instance.storeUpEndpoint(endpoint) }
}
fun verifyStoreUpEndpoint(endpoint: String?) {
verify { instance.storeUpEndpoint(endpoint) }
}
fun givenStorePushGateway(gateway: String?) {
justRun { instance.storePushGateway(gateway) }
}
fun verifyStorePushGateway(gateway: String?) {
verify { instance.storePushGateway(gateway) }
}
}

View File

@ -54,4 +54,8 @@ class FakeVectorFeatures : VectorFeatures by spyk<DefaultVectorFeatures>() {
fun givenUnverifiedSessionsAlertEnabled(isEnabled: Boolean) {
every { isUnverifiedSessionsAlertEnabled() } returns isEnabled
}
fun givenExternalDistributorsAreAllowed(allowed: Boolean) {
every { allowExternalUnifiedPushDistributors() } returns allowed
}
}

View File

@ -16,6 +16,7 @@
package im.vector.app.test.fakes
import im.vector.app.features.settings.BackgroundSyncMode
import im.vector.app.features.settings.VectorPreferences
import io.mockk.every
import io.mockk.justRun
@ -60,4 +61,12 @@ class FakeVectorPreferences {
fun givenUnverifiedSessionsAlertLastShownMillis(lastShownMillis: Long) {
every { instance.getUnverifiedSessionsAlertLastShownMillis(any()) } returns lastShownMillis
}
fun givenSetFdroidSyncBackgroundMode(mode: BackgroundSyncMode) {
justRun { instance.setFdroidSyncBackgroundMode(mode) }
}
fun verifySetFdroidSyncBackgroundMode(mode: BackgroundSyncMode) {
verify { instance.setFdroidSyncBackgroundMode(mode) }
}
}