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 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
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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) {
|
||||||
apiResponseListenersMap.getOrPut(path) { mutableListOf() }
|
synchronized(apiResponseListenersMap) {
|
||||||
.add(listener)
|
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