mirror of
https://github.com/element-hq/element-android
synced 2024-11-25 02:45:37 +03:00
TI: Introduce doSync method
This commit is contained in:
parent
fa821826d2
commit
ed773dbb96
1 changed files with 50 additions and 47 deletions
|
@ -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)
|
doSync<RegistrationResult> {
|
||||||
matrix.authenticationService.getRegistrationWizard().createAccount(userName, password, null, object : TestMatrixCallback<RegistrationResult>(lock) {
|
matrix.authenticationService
|
||||||
override fun onSuccess(data: RegistrationResult) {
|
.getRegistrationWizard()
|
||||||
super.onSuccess(data)
|
.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)
|
|
||||||
|
|
||||||
if (sessionTestParams.withInitialSync) {
|
|
||||||
syncSession(session!!)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return session!!
|
val session = doSync<Session> {
|
||||||
|
matrix.authenticationService
|
||||||
|
.getLoginWizard()
|
||||||
|
.login(userName, password, "myDevice", it)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sessionTestParams.withInitialSync) {
|
||||||
|
syncSession(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)
|
||||||
|
|
Loading…
Reference in a new issue