mirror of
https://github.com/signalapp/Signal-Android.git
synced 2026-04-19 08:09:12 +01:00
Add unit test for RegistrationUtil.
This commit is contained in:
committed by
Greyson Parrelli
parent
34eef0bf5c
commit
7d24bff134
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Copyright 2024 Signal Messenger, LLC
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
package org.thoughtcrime.securesms.testutil
|
||||
|
||||
import androidx.test.core.app.ApplicationProvider
|
||||
import io.mockk.clearMocks
|
||||
import org.junit.rules.ExternalResource
|
||||
import org.thoughtcrime.securesms.dependencies.AppDependencies
|
||||
import org.thoughtcrime.securesms.dependencies.MockApplicationDependencyProvider
|
||||
import kotlin.reflect.KVisibility
|
||||
import kotlin.reflect.full.memberProperties
|
||||
|
||||
/**
|
||||
* Facilitates mocking and clearing components of [AppDependencies]. Clearing is particularly important as the
|
||||
* mocks will be reused since [AppDependencies] is scoped to the entire test suite and stays initialized with prior
|
||||
* test runs leading to unpredictable results based on how tests are run.
|
||||
*/
|
||||
class MockAppDependenciesRule : ExternalResource() {
|
||||
|
||||
private val skipList = setOf(
|
||||
"application",
|
||||
"databaseObserver",
|
||||
"groupsV2Authorization",
|
||||
"isInitialized",
|
||||
"okHttpClient",
|
||||
"signalOkHttpClient",
|
||||
"webSocketObserver"
|
||||
)
|
||||
|
||||
private val properties = AppDependencies::class
|
||||
.memberProperties
|
||||
.filter { it.visibility == KVisibility.PUBLIC }
|
||||
.filterNot { skipList.contains(it.name) }
|
||||
|
||||
override fun before() {
|
||||
if (!AppDependencies.isInitialized) {
|
||||
AppDependencies.init(ApplicationProvider.getApplicationContext(), MockApplicationDependencyProvider())
|
||||
}
|
||||
}
|
||||
|
||||
override fun after() {
|
||||
properties
|
||||
.forEach { property ->
|
||||
property.get(AppDependencies)?.let { clearMocks(it) }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* Copyright 2024 Signal Messenger, LLC
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
package org.thoughtcrime.securesms.testutil
|
||||
|
||||
import io.mockk.every
|
||||
import io.mockk.mockk
|
||||
import io.mockk.mockkObject
|
||||
import io.mockk.unmockkObject
|
||||
import org.junit.rules.ExternalResource
|
||||
import org.thoughtcrime.securesms.keyvalue.AccountValues
|
||||
import org.thoughtcrime.securesms.keyvalue.PhoneNumberPrivacyValues
|
||||
import org.thoughtcrime.securesms.keyvalue.RegistrationValues
|
||||
import org.thoughtcrime.securesms.keyvalue.SignalStore
|
||||
import org.thoughtcrime.securesms.keyvalue.SvrValues
|
||||
import kotlin.reflect.KClass
|
||||
|
||||
/**
|
||||
* Mocks [SignalStore] to return mock versions of the various values. Mocks will default to not be relaxed (each
|
||||
* method call on them will need to be mocked) except for unit functions which will do nothing.
|
||||
*
|
||||
* Expand mocked values as necessary when needed.
|
||||
*
|
||||
* @param relaxed Set of value classes that should default to relaxed thus defaulting all methods. Useful
|
||||
* when value is not part of the input state under test but called within the under test code.
|
||||
*/
|
||||
@Suppress("MemberVisibilityCanBePrivate")
|
||||
class MockSignalStoreRule(private val relaxed: Set<KClass<*>> = emptySet()) : ExternalResource() {
|
||||
|
||||
lateinit var account: AccountValues
|
||||
private set
|
||||
|
||||
lateinit var phoneNumberPrivacy: PhoneNumberPrivacyValues
|
||||
private set
|
||||
|
||||
lateinit var registration: RegistrationValues
|
||||
private set
|
||||
|
||||
lateinit var svr: SvrValues
|
||||
private set
|
||||
|
||||
override fun before() {
|
||||
account = mockk(relaxed = relaxed.contains(AccountValues::class), relaxUnitFun = true)
|
||||
phoneNumberPrivacy = mockk(relaxed = relaxed.contains(PhoneNumberPrivacyValues::class), relaxUnitFun = true)
|
||||
registration = mockk(relaxed = relaxed.contains(RegistrationValues::class), relaxUnitFun = true)
|
||||
svr = mockk(relaxed = relaxed.contains(SvrValues::class), relaxUnitFun = true)
|
||||
|
||||
mockkObject(SignalStore)
|
||||
every { SignalStore.account } returns account
|
||||
every { SignalStore.phoneNumberPrivacy } returns phoneNumberPrivacy
|
||||
every { SignalStore.registration } returns registration
|
||||
every { SignalStore.svr } returns svr
|
||||
}
|
||||
|
||||
override fun after() {
|
||||
unmockkObject(SignalStore)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user