diff --git a/matrix-sdk-android/src/androidTest/java/org/matrix/android/sdk/InstrumentedTest.kt b/matrix-sdk-android/src/androidTest/java/org/matrix/android/sdk/InstrumentedTest.kt index b784884363..583406346e 100644 --- a/matrix-sdk-android/src/androidTest/java/org/matrix/android/sdk/InstrumentedTest.kt +++ b/matrix-sdk-android/src/androidTest/java/org/matrix/android/sdk/InstrumentedTest.kt @@ -20,7 +20,6 @@ import android.content.Context import androidx.test.core.app.ApplicationProvider import org.matrix.android.sdk.test.shared.createTimberTestRule import org.junit.Rule -import java.io.File interface InstrumentedTest { @@ -30,8 +29,4 @@ interface InstrumentedTest { fun context(): Context { return ApplicationProvider.getApplicationContext() } - - fun cacheDir(): File { - return context().cacheDir - } } diff --git a/matrix-sdk-android/src/androidTest/java/org/matrix/android/sdk/api/Matrix.kt b/matrix-sdk-android/src/androidTest/java/org/matrix/android/sdk/api/Matrix.kt index 11251b8a5d..c439da8407 100644 --- a/matrix-sdk-android/src/androidTest/java/org/matrix/android/sdk/api/Matrix.kt +++ b/matrix-sdk-android/src/androidTest/java/org/matrix/android/sdk/api/Matrix.kt @@ -87,6 +87,10 @@ class Matrix private constructor(context: Context, matrixConfiguration: MatrixCo apiInterceptor.addListener(path, listener) } + fun unregisterApiInterceptorListener(path: ApiPath, listener: ApiInterceptorListener) { + apiInterceptor.removeListener(path, listener) + } + companion object { private lateinit var instance: Matrix diff --git a/matrix-sdk-android/src/androidTest/java/org/matrix/android/sdk/api/network/ApiInterceptorTest.kt b/matrix-sdk-android/src/androidTest/java/org/matrix/android/sdk/api/network/ApiInterceptorTest.kt index 93fdccfd48..149acbc18a 100644 --- a/matrix-sdk-android/src/androidTest/java/org/matrix/android/sdk/api/network/ApiInterceptorTest.kt +++ b/matrix-sdk-android/src/androidTest/java/org/matrix/android/sdk/api/network/ApiInterceptorTest.kt @@ -35,19 +35,24 @@ class ApiInterceptorTest : InstrumentedTest { private val commonTestHelper = CommonTestHelper(context()) @Test - fun createAccountTest() { - var counter = 0 - commonTestHelper.matrix.registerApiInterceptorListener(ApiPath.REGISTER, object : ApiInterceptorListener { + fun apiInterceptorTest() { + val responses = mutableListOf() + + val listener = object : ApiInterceptorListener { override fun onApiResponse(path: ApiPath, response: String) { Timber.w("onApiResponse($path): $response") - counter++ + responses.add(response) } - }) + } + + commonTestHelper.matrix.registerApiInterceptorListener(ApiPath.REGISTER, listener) val session = commonTestHelper.createAccount(TestConstants.USER_ALICE, SessionTestParams(withInitialSync = true)) commonTestHelper.signOutAndClose(session) - counter shouldBeEqualTo 2 + commonTestHelper.matrix.unregisterApiInterceptorListener(ApiPath.REGISTER, listener) + + responses.size shouldBeEqualTo 2 } } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/Matrix.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/Matrix.kt index 8a27f364d2..9980259266 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/Matrix.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/Matrix.kt @@ -81,6 +81,10 @@ class Matrix private constructor(context: Context, matrixConfiguration: MatrixCo apiInterceptor.addListener(path, listener) } + fun unregisterApiInterceptorListener(path: ApiPath, listener: ApiInterceptorListener) { + apiInterceptor.removeListener(path, listener) + } + companion object { private lateinit var instance: Matrix diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/network/ApiInterceptor.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/network/ApiInterceptor.kt index 8b188910a1..5d73264d11 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/network/ApiInterceptor.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/network/ApiInterceptor.kt @@ -80,7 +80,21 @@ internal class ApiInterceptor @Inject constructor() : Interceptor { * Adds listener to send intercepted api responses through. */ fun addListener(path: ApiPath, listener: ApiInterceptorListener) { - apiResponseListenersMap.getOrPut(path) { mutableListOf() } - .add(listener) + synchronized(apiResponseListenersMap) { + apiResponseListenersMap.getOrPut(path) { mutableListOf() } + .add(listener) + } + } + + /** + * Remove listener to send intercepted api responses through. + */ + fun removeListener(path: ApiPath, listener: ApiInterceptorListener) { + synchronized(apiResponseListenersMap) { + apiResponseListenersMap[path]?.remove(listener) + if (apiResponseListenersMap[path]?.isEmpty() == true) { + apiResponseListenersMap.remove(path) + } + } } }