Add ApiInterceptor.removeListener()

This commit is contained in:
Benoit Marty 2021-03-22 18:09:42 +01:00
parent f6e43a5305
commit d85d44bf4b
5 changed files with 35 additions and 13 deletions

View File

@ -20,7 +20,6 @@ import android.content.Context
import androidx.test.core.app.ApplicationProvider import androidx.test.core.app.ApplicationProvider
import org.matrix.android.sdk.test.shared.createTimberTestRule import org.matrix.android.sdk.test.shared.createTimberTestRule
import org.junit.Rule import org.junit.Rule
import java.io.File
interface InstrumentedTest { interface InstrumentedTest {
@ -30,8 +29,4 @@ interface InstrumentedTest {
fun context(): Context { fun context(): Context {
return ApplicationProvider.getApplicationContext() return ApplicationProvider.getApplicationContext()
} }
fun cacheDir(): File {
return context().cacheDir
}
} }

View File

@ -87,6 +87,10 @@ class Matrix private constructor(context: Context, matrixConfiguration: MatrixCo
apiInterceptor.addListener(path, listener) apiInterceptor.addListener(path, listener)
} }
fun unregisterApiInterceptorListener(path: ApiPath, listener: ApiInterceptorListener) {
apiInterceptor.removeListener(path, listener)
}
companion object { companion object {
private lateinit var instance: Matrix private lateinit var instance: Matrix

View File

@ -35,19 +35,24 @@ class ApiInterceptorTest : InstrumentedTest {
private val commonTestHelper = CommonTestHelper(context()) private val commonTestHelper = CommonTestHelper(context())
@Test @Test
fun createAccountTest() { fun apiInterceptorTest() {
var counter = 0 val responses = mutableListOf<String>()
commonTestHelper.matrix.registerApiInterceptorListener(ApiPath.REGISTER, object : ApiInterceptorListener {
val listener = object : ApiInterceptorListener {
override fun onApiResponse(path: ApiPath, response: String) { override fun onApiResponse(path: ApiPath, response: String) {
Timber.w("onApiResponse($path): $response") 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)) val session = commonTestHelper.createAccount(TestConstants.USER_ALICE, SessionTestParams(withInitialSync = true))
commonTestHelper.signOutAndClose(session) commonTestHelper.signOutAndClose(session)
counter shouldBeEqualTo 2 commonTestHelper.matrix.unregisterApiInterceptorListener(ApiPath.REGISTER, listener)
responses.size shouldBeEqualTo 2
} }
} }

View File

@ -81,6 +81,10 @@ class Matrix private constructor(context: Context, matrixConfiguration: MatrixCo
apiInterceptor.addListener(path, listener) apiInterceptor.addListener(path, listener)
} }
fun unregisterApiInterceptorListener(path: ApiPath, listener: ApiInterceptorListener) {
apiInterceptor.removeListener(path, listener)
}
companion object { companion object {
private lateinit var instance: Matrix private lateinit var instance: Matrix

View File

@ -80,7 +80,21 @@ internal class ApiInterceptor @Inject constructor() : Interceptor {
* Adds listener to send intercepted api responses through. * Adds listener to send intercepted api responses through.
*/ */
fun addListener(path: ApiPath, listener: ApiInterceptorListener) { fun addListener(path: ApiPath, listener: ApiInterceptorListener) {
synchronized(apiResponseListenersMap) {
apiResponseListenersMap.getOrPut(path) { mutableListOf() } apiResponseListenersMap.getOrPut(path) { mutableListOf() }
.add(listener) .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)
}
}
}
} }