resetting the analytics datastore via reflection

This commit is contained in:
Adam Brown 2022-08-05 14:24:53 +01:00
parent 5864ce4348
commit 2683e9209b
2 changed files with 17 additions and 2 deletions

View File

@ -598,4 +598,5 @@ dependencies {
androidTestImplementation libs.mockk.mockkAndroid androidTestImplementation libs.mockk.mockkAndroid
androidTestUtil libs.androidx.orchestrator androidTestUtil libs.androidx.orchestrator
debugImplementation libs.androidx.fragmentTesting debugImplementation libs.androidx.fragmentTesting
androidTestImplementation "org.jetbrains.kotlin:kotlin-reflect:1.7.10"
} }

View File

@ -16,21 +16,26 @@
package im.vector.app package im.vector.app
import androidx.datastore.preferences.preferencesDataStoreFile import android.content.Context
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.edit
import androidx.test.platform.app.InstrumentationRegistry import androidx.test.platform.app.InstrumentationRegistry
import im.vector.app.features.analytics.store.AnalyticsStore
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import org.junit.rules.TestWatcher import org.junit.rules.TestWatcher
import org.junit.runner.Description import org.junit.runner.Description
import org.junit.runners.model.Statement import org.junit.runners.model.Statement
import kotlin.reflect.KClass
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
runBlocking { runBlocking {
reflectAnalyticDatastore(context).edit { it.clear() }
runCatching { runCatching {
val holder = (context.applicationContext as VectorApplication).activeSessionHolder val holder = (context.applicationContext as VectorApplication).activeSessionHolder
holder.getSafeActiveSession()?.signOutService()?.signOut(true) holder.getSafeActiveSession()?.signOutService()?.signOut(true)
context.preferencesDataStoreFile(name = "vector_analytics").delete()
(context.applicationContext as VectorApplication).vectorPreferences.clearPreferences() (context.applicationContext as VectorApplication).vectorPreferences.clearPreferences()
holder.clearActiveSession() holder.clearActiveSession()
} }
@ -38,3 +43,12 @@ class ClearCurrentSessionRule : TestWatcher() {
return super.apply(base, description) return super.apply(base, description)
} }
} }
private fun KClass<*>.asTopLevel() = Class.forName("${qualifiedName}Kt")
@Suppress("UNCHECKED_CAST")
private fun reflectAnalyticDatastore(context: Context): DataStore<Preferences> {
val klass = AnalyticsStore::class.asTopLevel()
val method = klass.getMethod("access\$getDataStore", Context::class.java)
return method.invoke(klass, context) as DataStore<Preferences>
}