Add ApiInterceptor.removeListener()
This commit is contained in:
parent
f6e43a5305
commit
d85d44bf4b
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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<String>()
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue