TI: Introduce doSync method

This commit is contained in:
Benoit Marty 2020-01-08 10:20:08 +01:00
parent fa821826d2
commit ed773dbb96

View file

@ -20,6 +20,7 @@ package im.vector.matrix.android.common
import android.content.Context import android.content.Context
import android.net.Uri import android.net.Uri
import im.vector.matrix.android.api.Matrix import im.vector.matrix.android.api.Matrix
import im.vector.matrix.android.api.MatrixCallback
import im.vector.matrix.android.api.MatrixConfiguration import im.vector.matrix.android.api.MatrixConfiguration
import im.vector.matrix.android.api.auth.data.HomeServerConnectionConfig import im.vector.matrix.android.api.auth.data.HomeServerConnectionConfig
import im.vector.matrix.android.api.auth.data.LoginFlowResult import im.vector.matrix.android.api.auth.data.LoginFlowResult
@ -139,7 +140,6 @@ class CommonTestHelper(context: Context) {
* @param testParams test params about the session * @param testParams test params about the session
* @return the session associated with the newly created account * @return the session associated with the newly created account
*/ */
@Throws(InterruptedException::class)
private fun createAccount(userNamePrefix: String, private fun createAccount(userNamePrefix: String,
password: String, password: String,
testParams: SessionTestParams): Session { testParams: SessionTestParams): Session {
@ -160,7 +160,6 @@ class CommonTestHelper(context: Context) {
* @param testParams test params about the session * @param testParams test params about the session
* @return the session associated with the existing account * @return the session associated with the existing account
*/ */
@Throws(InterruptedException::class)
private fun logIntoAccount(userId: String, private fun logIntoAccount(userId: String,
password: String, password: String,
testParams: SessionTestParams): Session { testParams: SessionTestParams): Session {
@ -181,28 +180,23 @@ class CommonTestHelper(context: Context) {
sessionTestParams: SessionTestParams): Session { sessionTestParams: SessionTestParams): Session {
val hs = createHomeServerConfig() val hs = createHomeServerConfig()
var lock = CountDownLatch(1) doSync<LoginFlowResult> {
matrix.authenticationService.getLoginFlow(hs, object : TestMatrixCallback<LoginFlowResult>(lock) {}) matrix.authenticationService
await(lock) .getLoginFlow(hs, it)
}
lock = CountDownLatch(1)
matrix.authenticationService.getRegistrationWizard().createAccount(userName, password, null, object : TestMatrixCallback<RegistrationResult>(lock) { doSync<RegistrationResult> {
override fun onSuccess(data: RegistrationResult) { matrix.authenticationService
super.onSuccess(data) .getRegistrationWizard()
.createAccount(userName, password, null, it)
} }
})
await(lock)
// Preform dummy step // Preform dummy step
lock = CountDownLatch(1) val registrationResult = doSync<RegistrationResult> {
var registrationResult: RegistrationResult? = null matrix.authenticationService
matrix.authenticationService.getRegistrationWizard().dummy(object : TestMatrixCallback<RegistrationResult>(lock) { .getRegistrationWizard()
override fun onSuccess(data: RegistrationResult) { .dummy(it)
registrationResult = data
super.onSuccess(data)
} }
})
await(lock)
assertTrue(registrationResult is RegistrationResult.Success) assertTrue(registrationResult is RegistrationResult.Success)
val session = (registrationResult as RegistrationResult.Success).session val session = (registrationResult as RegistrationResult.Success).session
@ -225,27 +219,22 @@ class CommonTestHelper(context: Context) {
sessionTestParams: SessionTestParams): Session { sessionTestParams: SessionTestParams): Session {
val hs = createHomeServerConfig() val hs = createHomeServerConfig()
var lock = CountDownLatch(1) doSync<LoginFlowResult> {
matrix.authenticationService.getLoginFlow(hs, object : TestMatrixCallback<LoginFlowResult>(lock) {}) matrix.authenticationService
await(lock) .getLoginFlow(hs, it)
lock = CountDownLatch(1)
var session: Session? = null
matrix.authenticationService.getLoginWizard().login(userName, password, "myDevice", object : TestMatrixCallback<Session>(lock) {
override fun onSuccess(data: Session) {
session = data
super.onSuccess(data)
} }
})
await(lock)
assertNotNull(session) val session = doSync<Session> {
matrix.authenticationService
.getLoginWizard()
.login(userName, password, "myDevice", it)
}
if (sessionTestParams.withInitialSync) { if (sessionTestParams.withInitialSync) {
syncSession(session!!) syncSession(session)
} }
return session!! return session
} }
/** /**
@ -258,16 +247,30 @@ class CommonTestHelper(context: Context) {
assertTrue(latch.await(TestConstants.timeOutMillis, TimeUnit.MILLISECONDS)) assertTrue(latch.await(TestConstants.timeOutMillis, TimeUnit.MILLISECONDS))
} }
// Transform a method with a MatrixCallback to a synchronous method
inline fun <reified T> doSync(block: (MatrixCallback<T>) -> Unit): T {
val lock = CountDownLatch(1)
var result: T? = null
val callback = object : TestMatrixCallback<T>(lock) {
override fun onSuccess(data: T) {
result = data
super.onSuccess(data)
}
}
block.invoke(callback)
await(lock)
assertNotNull(result)
return result!!
}
/** /**
* Clear all provided sessions * Clear all provided sessions
*
* @param sessions the sessions to clear
*/ */
fun closeAllSessions(sessions: List<Session>) { fun Iterable<Session>.close() = forEach { it.close() }
for (session in sessions) {
session.close()
}
}
fun signout(session: Session) { fun signout(session: Session) {
val lock = CountDownLatch(1) val lock = CountDownLatch(1)