From 5864ce43488c928e5ec8905c3eb4c6a2b048bb7d Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Fri, 5 Aug 2022 13:08:21 +0100 Subject: [PATCH] adding rule to force a new session to be started for instrumentation tests --- .../java/im/vector/app/CantVerifyTest.kt | 5 ++- .../im/vector/app/ClearCurrentSessionRule.kt | 40 +++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 vector/src/androidTest/java/im/vector/app/ClearCurrentSessionRule.kt diff --git a/vector/src/androidTest/java/im/vector/app/CantVerifyTest.kt b/vector/src/androidTest/java/im/vector/app/CantVerifyTest.kt index ba844e56b7..e6b17c1e9e 100644 --- a/vector/src/androidTest/java/im/vector/app/CantVerifyTest.kt +++ b/vector/src/androidTest/java/im/vector/app/CantVerifyTest.kt @@ -27,6 +27,7 @@ import im.vector.app.features.MainActivity import im.vector.app.ui.robot.ElementRobot import org.junit.Rule import org.junit.Test +import org.junit.rules.RuleChain import org.junit.runner.RunWith import java.util.UUID @@ -35,7 +36,9 @@ import java.util.UUID class CantVerifyTest : VerificationTestBase() { @get:Rule - val activityRule = ActivityScenarioRule(MainActivity::class.java) + val testRule = RuleChain + .outerRule(ActivityScenarioRule(MainActivity::class.java)) + .around(ClearCurrentSessionRule()) private val elementRobot = ElementRobot() var userName: String = "loginTest_${UUID.randomUUID()}" diff --git a/vector/src/androidTest/java/im/vector/app/ClearCurrentSessionRule.kt b/vector/src/androidTest/java/im/vector/app/ClearCurrentSessionRule.kt new file mode 100644 index 0000000000..7ba0b63799 --- /dev/null +++ b/vector/src/androidTest/java/im/vector/app/ClearCurrentSessionRule.kt @@ -0,0 +1,40 @@ +/* + * 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 + +import androidx.datastore.preferences.preferencesDataStoreFile +import androidx.test.platform.app.InstrumentationRegistry +import kotlinx.coroutines.runBlocking +import org.junit.rules.TestWatcher +import org.junit.runner.Description +import org.junit.runners.model.Statement + +class ClearCurrentSessionRule : TestWatcher() { + override fun apply(base: Statement, description: Description): Statement { + val context = InstrumentationRegistry.getInstrumentation().targetContext + runBlocking { + 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() + } + } + return super.apply(base, description) + } +}