From 2683e9209b3d3aaf737795ee4818b9e76443b2cc Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Fri, 5 Aug 2022 14:24:53 +0100 Subject: [PATCH] resetting the analytics datastore via reflection --- vector/build.gradle | 1 + .../im/vector/app/ClearCurrentSessionRule.kt | 18 ++++++++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/vector/build.gradle b/vector/build.gradle index dc0a2da35d..0edaf5424e 100644 --- a/vector/build.gradle +++ b/vector/build.gradle @@ -598,4 +598,5 @@ dependencies { androidTestImplementation libs.mockk.mockkAndroid androidTestUtil libs.androidx.orchestrator debugImplementation libs.androidx.fragmentTesting + androidTestImplementation "org.jetbrains.kotlin:kotlin-reflect:1.7.10" } diff --git a/vector/src/androidTest/java/im/vector/app/ClearCurrentSessionRule.kt b/vector/src/androidTest/java/im/vector/app/ClearCurrentSessionRule.kt index 7ba0b63799..68db0a9509 100644 --- a/vector/src/androidTest/java/im/vector/app/ClearCurrentSessionRule.kt +++ b/vector/src/androidTest/java/im/vector/app/ClearCurrentSessionRule.kt @@ -16,21 +16,26 @@ 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 im.vector.app.features.analytics.store.AnalyticsStore import kotlinx.coroutines.runBlocking import org.junit.rules.TestWatcher import org.junit.runner.Description import org.junit.runners.model.Statement +import kotlin.reflect.KClass class ClearCurrentSessionRule : TestWatcher() { override fun apply(base: Statement, description: Description): Statement { val context = InstrumentationRegistry.getInstrumentation().targetContext runBlocking { + reflectAnalyticDatastore(context).edit { it.clear() } runCatching { val holder = (context.applicationContext as VectorApplication).activeSessionHolder holder.getSafeActiveSession()?.signOutService()?.signOut(true) - context.preferencesDataStoreFile(name = "vector_analytics").delete() (context.applicationContext as VectorApplication).vectorPreferences.clearPreferences() holder.clearActiveSession() } @@ -38,3 +43,12 @@ class ClearCurrentSessionRule : TestWatcher() { return super.apply(base, description) } } + +private fun KClass<*>.asTopLevel() = Class.forName("${qualifiedName}Kt") + +@Suppress("UNCHECKED_CAST") +private fun reflectAnalyticDatastore(context: Context): DataStore { + val klass = AnalyticsStore::class.asTopLevel() + val method = klass.getMethod("access\$getDataStore", Context::class.java) + return method.invoke(klass, context) as DataStore +}