documenting the rule and reflection helper

This commit is contained in:
Adam Brown 2022-08-05 17:53:36 +01:00
parent 2683e9209b
commit 20b3dbc6e6
2 changed files with 12 additions and 0 deletions

View file

@ -28,6 +28,11 @@ import org.junit.runner.Description
import org.junit.runners.model.Statement import org.junit.runners.model.Statement
import kotlin.reflect.KClass import kotlin.reflect.KClass
/**
* A TestRule to reset and clear the current Session.
* If a Session is active it will be signed out and cleared from the ActiveSessionHolder.
* The VectorPreferences and AnalyticsDatastore are also cleared in an attempt to recreate a fresh base.
*/
class ClearCurrentSessionRule : TestWatcher() { class ClearCurrentSessionRule : TestWatcher() {
override fun apply(base: Statement, description: Description): Statement { override fun apply(base: Statement, description: Description): Statement {
val context = InstrumentationRegistry.getInstrumentation().targetContext val context = InstrumentationRegistry.getInstrumentation().targetContext
@ -46,6 +51,10 @@ class ClearCurrentSessionRule : TestWatcher() {
private fun KClass<*>.asTopLevel() = Class.forName("${qualifiedName}Kt") private fun KClass<*>.asTopLevel() = Class.forName("${qualifiedName}Kt")
/**
* Fetches the top level, private [Context.dataStore] extension property from [im.vector.app.features.analytics.store.AnalyticsStore]
* via reflection to avoid exposing property to all callers.
*/
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
private fun reflectAnalyticDatastore(context: Context): DataStore<Preferences> { private fun reflectAnalyticDatastore(context: Context): DataStore<Preferences> {
val klass = AnalyticsStore::class.asTopLevel() val klass = AnalyticsStore::class.asTopLevel()

View file

@ -29,6 +29,9 @@ import kotlinx.coroutines.flow.map
import org.matrix.android.sdk.api.extensions.orFalse import org.matrix.android.sdk.api.extensions.orFalse
import javax.inject.Inject import javax.inject.Inject
/**
* Also accessed via reflection by the instrumentation tests @see [im.vector.app.ClearCurrentSessionRule].
*/
private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "vector_analytics") private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "vector_analytics")
/** /**