Add test on UnifiedPushProvider

This commit is contained in:
Benoit Marty 2024-05-22 10:31:08 +02:00 committed by Benoit Marty
parent 86eceb3cbc
commit 1bf38e96ae
10 changed files with 502 additions and 14 deletions

View file

@ -17,6 +17,8 @@
package io.element.android.libraries.pushproviders.unifiedpush
import android.content.Context
import com.squareup.anvil.annotations.ContributesBinding
import io.element.android.libraries.di.AppScope
import io.element.android.libraries.di.ApplicationContext
import io.element.android.libraries.pushproviders.api.Distributor
import io.element.android.libraries.pushproviders.unifiedpush.registration.EndpointRegistrationHandler
@ -30,12 +32,17 @@ import org.unifiedpush.android.connector.UnifiedPush
import javax.inject.Inject
import kotlin.time.Duration.Companion.seconds
class RegisterUnifiedPushUseCase @Inject constructor(
interface RegisterUnifiedPushUseCase {
suspend fun execute(distributor: Distributor, clientSecret: String): Result<Unit>
}
@ContributesBinding(AppScope::class)
class DefaultRegisterUnifiedPushUseCase @Inject constructor(
@ApplicationContext private val context: Context,
private val endpointRegistrationHandler: EndpointRegistrationHandler,
private val coroutineScope: CoroutineScope,
) {
suspend fun execute(distributor: Distributor, clientSecret: String): Result<Unit> {
) : RegisterUnifiedPushUseCase {
override suspend fun execute(distributor: Distributor, clientSecret: String): Result<Unit> {
UnifiedPush.saveDistributor(context, distributor.value)
val completable = CompletableDeferred<Result<Unit>>()
val job = coroutineScope.launch {

View file

@ -19,22 +19,34 @@ package io.element.android.libraries.pushproviders.unifiedpush
import android.content.Context
import android.content.SharedPreferences
import androidx.core.content.edit
import com.squareup.anvil.annotations.ContributesBinding
import io.element.android.libraries.di.AppScope
import io.element.android.libraries.di.ApplicationContext
import io.element.android.libraries.di.DefaultPreferences
import io.element.android.libraries.matrix.api.core.UserId
import javax.inject.Inject
class UnifiedPushStore @Inject constructor(
interface UnifiedPushStore {
fun getEndpoint(clientSecret: String): String?
fun storeUpEndpoint(endpoint: String?, clientSecret: String)
fun getPushGateway(clientSecret: String): String?
fun storePushGateway(gateway: String?, clientSecret: String)
fun getDistributorValue(userId: UserId): String?
fun setDistributorValue(userId: UserId, value: String)
}
@ContributesBinding(AppScope::class)
class DefaultUnifiedPushStore @Inject constructor(
@ApplicationContext val context: Context,
@DefaultPreferences private val defaultPrefs: SharedPreferences,
) {
) : UnifiedPushStore {
/**
* Retrieves the UnifiedPush Endpoint.
*
* @param clientSecret the client secret, to identify the session
* @return the UnifiedPush Endpoint or null if not received
*/
fun getEndpoint(clientSecret: String): String? {
override fun getEndpoint(clientSecret: String): String? {
return defaultPrefs.getString(PREFS_ENDPOINT_OR_TOKEN + clientSecret, null)
}
@ -44,7 +56,7 @@ class UnifiedPushStore @Inject constructor(
* @param endpoint the endpoint to store
* @param clientSecret the client secret, to identify the session
*/
fun storeUpEndpoint(endpoint: String?, clientSecret: String) {
override fun storeUpEndpoint(endpoint: String?, clientSecret: String) {
defaultPrefs.edit {
putString(PREFS_ENDPOINT_OR_TOKEN + clientSecret, endpoint)
}
@ -56,7 +68,7 @@ class UnifiedPushStore @Inject constructor(
* @param clientSecret the client secret, to identify the session
* @return the Push Gateway or null if not defined
*/
fun getPushGateway(clientSecret: String): String? {
override fun getPushGateway(clientSecret: String): String? {
return defaultPrefs.getString(PREFS_PUSH_GATEWAY + clientSecret, null)
}
@ -66,17 +78,17 @@ class UnifiedPushStore @Inject constructor(
* @param gateway the push gateway to store
* @param clientSecret the client secret, to identify the session
*/
fun storePushGateway(gateway: String?, clientSecret: String) {
override fun storePushGateway(gateway: String?, clientSecret: String) {
defaultPrefs.edit {
putString(PREFS_PUSH_GATEWAY + clientSecret, gateway)
}
}
fun getDistributorValue(userId: UserId): String? {
override fun getDistributorValue(userId: UserId): String? {
return defaultPrefs.getString(PREFS_DISTRIBUTOR + userId, null)
}
fun setDistributorValue(userId: UserId, value: String) {
override fun setDistributorValue(userId: UserId, value: String) {
defaultPrefs.edit {
putString(PREFS_DISTRIBUTOR + userId, value)
}

View file

@ -17,18 +17,25 @@
package io.element.android.libraries.pushproviders.unifiedpush
import android.content.Context
import com.squareup.anvil.annotations.ContributesBinding
import io.element.android.libraries.di.AppScope
import io.element.android.libraries.di.ApplicationContext
import io.element.android.libraries.matrix.api.MatrixClient
import io.element.android.libraries.pushproviders.api.PusherSubscriber
import org.unifiedpush.android.connector.UnifiedPush
import javax.inject.Inject
class UnregisterUnifiedPushUseCase @Inject constructor(
interface UnregisterUnifiedPushUseCase {
suspend fun execute(matrixClient: MatrixClient, clientSecret: String): Result<Unit>
}
@ContributesBinding(AppScope::class)
class DefaultUnregisterUnifiedPushUseCase @Inject constructor(
@ApplicationContext private val context: Context,
private val unifiedPushStore: UnifiedPushStore,
private val pusherSubscriber: PusherSubscriber,
) {
suspend fun execute(matrixClient: MatrixClient, clientSecret: String): Result<Unit> {
) : UnregisterUnifiedPushUseCase {
override suspend fun execute(matrixClient: MatrixClient, clientSecret: String): Result<Unit> {
val endpoint = unifiedPushStore.getEndpoint(clientSecret)
val gateway = unifiedPushStore.getPushGateway(clientSecret)
if (endpoint == null || gateway == null) {