Skip to content

Commit 22bde9e

Browse files
authored
Merge pull request #2899 from element-hq/feature/bma/unitTestPush
Add unit tests in some push classes
2 parents 06d5376 + 788d9e0 commit 22bde9e

File tree

87 files changed

+3396
-285
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

87 files changed

+3396
-285
lines changed

appnav/src/test/kotlin/io/element/android/appnav/intent/IntentResolverTest.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import io.element.android.libraries.matrix.test.A_ROOM_ID
3333
import io.element.android.libraries.matrix.test.A_SESSION_ID
3434
import io.element.android.libraries.matrix.test.A_THREAD_ID
3535
import io.element.android.libraries.matrix.test.permalink.FakePermalinkParser
36+
import io.element.android.tests.testutils.lambda.lambdaError
3637
import org.junit.Assert.assertThrows
3738
import org.junit.Test
3839
import org.junit.runner.RunWith
@@ -229,7 +230,7 @@ class IntentResolverTest {
229230
}
230231

231232
private fun createIntentResolver(
232-
permalinkParserResult: () -> PermalinkData = { throw NotImplementedError() }
233+
permalinkParserResult: () -> PermalinkData = { lambdaError() }
233234
): IntentResolver {
234235
return IntentResolver(
235236
deeplinkParser = DeeplinkParser(),

features/invite/impl/src/test/kotlin/io/element/android/features/invite/impl/response/AcceptDeclineInvitePresenterTest.kt

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,11 @@ class AcceptDeclineInvitePresenterTest {
192192
cancelAndConsumeRemainingEvents()
193193
}
194194
assert(joinRoomFailure)
195-
.isCalledExactly(1)
196-
.withSequence(
197-
listOf(value(A_ROOM_ID), value(emptyList<String>()), value(JoinedRoom.Trigger.Invite))
195+
.isCalledOnce()
196+
.with(
197+
value(A_ROOM_ID),
198+
value(emptyList<String>()),
199+
value(JoinedRoom.Trigger.Invite)
198200
)
199201
}
200202

@@ -221,9 +223,11 @@ class AcceptDeclineInvitePresenterTest {
221223
cancelAndConsumeRemainingEvents()
222224
}
223225
assert(joinRoomSuccess)
224-
.isCalledExactly(1)
225-
.withSequence(
226-
listOf(value(A_ROOM_ID), value(emptyList<String>()), value(JoinedRoom.Trigger.Invite))
226+
.isCalledOnce()
227+
.with(
228+
value(A_ROOM_ID),
229+
value(emptyList<String>()),
230+
value(JoinedRoom.Trigger.Invite)
227231
)
228232
}
229233

features/preferences/impl/src/test/kotlin/io/element/android/features/preferences/impl/notifications/NotificationSettingsPresenterTests.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ package io.element.android.features.preferences.impl.notifications
1919
import app.cash.molecule.RecompositionMode
2020
import app.cash.molecule.moleculeFlow
2121
import app.cash.turbine.test
22-
import com.element.android.libraries.pushstore.test.userpushstore.FakeUserPushStoreFactory
2322
import com.google.common.truth.Truth.assertThat
2423
import io.element.android.libraries.matrix.api.room.RoomNotificationMode
2524
import io.element.android.libraries.matrix.test.A_THROWABLE
2625
import io.element.android.libraries.matrix.test.FakeMatrixClient
2726
import io.element.android.libraries.matrix.test.notificationsettings.FakeNotificationSettingsService
27+
import io.element.android.libraries.pushstore.test.userpushstore.FakeUserPushStoreFactory
2828
import io.element.android.tests.testutils.consumeItemsUntilPredicate
2929
import kotlinx.coroutines.test.runTest
3030
import org.junit.Test

features/rageshake/impl/src/test/kotlin/io/element/android/features/rageshake/impl/bugreport/BugReportPresenterTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ class BugReportPresenterTest {
136136
initialState.eventSink.invoke(BugReportEvents.ResetAll)
137137
val resetState = awaitItem()
138138
assertThat(resetState.hasCrashLogs).isFalse()
139-
logFilesRemoverLambda.assertions().isCalledExactly(1)
139+
logFilesRemoverLambda.assertions().isCalledOnce()
140140
// TODO Make it live assertThat(resetState.screenshotUri).isNull()
141141
}
142142
}

libraries/matrix/impl/src/test/kotlin/io/element/android/libraries/matrix/impl/room/join/DefaultJoinRoomTest.kt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ class DefaultJoinRoomTest {
5757
.isNeverCalled()
5858
joinRoomLambda
5959
.assertions()
60-
.isCalledExactly(1)
61-
.withSequence(
62-
listOf(value(A_ROOM_ID))
60+
.isCalledOnce()
61+
.with(
62+
value(A_ROOM_ID)
6363
)
6464
assertThat(analyticsService.capturedEvents).containsExactly(
6565
roomResult.toAnalyticsJoinedRoom(aTrigger)
@@ -88,9 +88,10 @@ class DefaultJoinRoomTest {
8888
sut.invoke(A_ROOM_ID, A_SERVER_LIST, aTrigger)
8989
joinRoomByIdOrAliasLambda
9090
.assertions()
91-
.isCalledExactly(1)
92-
.withSequence(
93-
listOf(value(A_ROOM_ID), value(A_SERVER_LIST))
91+
.isCalledOnce()
92+
.with(
93+
value(A_ROOM_ID),
94+
value(A_SERVER_LIST)
9495
)
9596
joinRoomLambda
9697
.assertions()

libraries/matrix/test/src/main/kotlin/io/element/android/libraries/matrix/test/TestData.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import io.element.android.libraries.matrix.api.room.RoomNotificationSettings
3030

3131
const val A_USER_NAME = "alice"
3232
const val A_PASSWORD = "password"
33+
const val A_SECRET = "secret"
3334

3435
val A_USER_ID = UserId("@alice:server.org")
3536
val A_USER_ID_2 = UserId("@bob:server.org")

libraries/matrix/test/src/main/kotlin/io/element/android/libraries/matrix/test/auth/FakeAuthenticationService.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ import kotlinx.coroutines.flow.flowOf
3131

3232
val A_OIDC_DATA = OidcDetails(url = "a-url")
3333

34-
class FakeAuthenticationService : MatrixAuthenticationService {
34+
class FakeAuthenticationService(
35+
private val matrixClientResult: ((SessionId) -> Result<MatrixClient>)? = null
36+
) : MatrixAuthenticationService {
3537
private val homeserver = MutableStateFlow<MatrixHomeServerDetails?>(null)
3638
private var oidcError: Throwable? = null
3739
private var oidcCancelError: Throwable? = null
@@ -48,6 +50,9 @@ class FakeAuthenticationService : MatrixAuthenticationService {
4850
override suspend fun getLatestSessionId(): SessionId? = getLatestSessionIdLambda()
4951

5052
override suspend fun restoreSession(sessionId: SessionId): Result<MatrixClient> {
53+
if (matrixClientResult != null) {
54+
return matrixClientResult.invoke(sessionId)
55+
}
5156
return if (matrixClient != null) {
5257
Result.success(matrixClient!!)
5358
} else {

libraries/matrix/test/src/main/kotlin/io/element/android/libraries/matrix/test/permalink/FakePermalinkParser.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ package io.element.android.libraries.matrix.test.permalink
1818

1919
import io.element.android.libraries.matrix.api.permalink.PermalinkData
2020
import io.element.android.libraries.matrix.api.permalink.PermalinkParser
21+
import io.element.android.tests.testutils.lambda.lambdaError
2122

2223
class FakePermalinkParser(
23-
private var result: () -> PermalinkData = { TODO("Not implemented") }
24+
private var result: () -> PermalinkData = { lambdaError() }
2425
) : PermalinkParser {
2526
fun givenResult(result: PermalinkData) {
2627
this.result = { result }

libraries/matrix/test/src/main/kotlin/io/element/android/libraries/matrix/test/pushers/FakePushersService.kt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,12 @@ package io.element.android.libraries.matrix.test.pushers
1919
import io.element.android.libraries.matrix.api.pusher.PushersService
2020
import io.element.android.libraries.matrix.api.pusher.SetHttpPusherData
2121
import io.element.android.libraries.matrix.api.pusher.UnsetHttpPusherData
22+
import io.element.android.tests.testutils.lambda.lambdaError
2223

23-
class FakePushersService : PushersService {
24-
override suspend fun setHttpPusher(setHttpPusherData: SetHttpPusherData) = Result.success(Unit)
25-
override suspend fun unsetHttpPusher(unsetHttpPusherData: UnsetHttpPusherData): Result<Unit> = Result.success(Unit)
24+
class FakePushersService(
25+
private val setHttpPusherResult: (SetHttpPusherData) -> Result<Unit> = { lambdaError() },
26+
private val unsetHttpPusherResult: (UnsetHttpPusherData) -> Result<Unit> = { lambdaError() },
27+
) : PushersService {
28+
override suspend fun setHttpPusher(setHttpPusherData: SetHttpPusherData) = setHttpPusherResult(setHttpPusherData)
29+
override suspend fun unsetHttpPusher(unsetHttpPusherData: UnsetHttpPusherData): Result<Unit> = unsetHttpPusherResult(unsetHttpPusherData)
2630
}

libraries/push/api/src/main/kotlin/io/element/android/libraries/push/api/PushService.kt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@ import io.element.android.libraries.pushproviders.api.Distributor
2121
import io.element.android.libraries.pushproviders.api.PushProvider
2222

2323
interface PushService {
24-
// TODO Move away
25-
fun notificationStyleChanged()
26-
2724
/**
2825
* Return the current push provider, or null if none.
2926
*/

libraries/push/impl/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ dependencies {
7272
testImplementation(projects.libraries.matrix.test)
7373
testImplementation(projects.libraries.push.test)
7474
testImplementation(projects.libraries.pushproviders.test)
75+
testImplementation(projects.libraries.pushstore.test)
7576
testImplementation(projects.tests.testutils)
7677
testImplementation(projects.services.appnavstate.test)
7778
testImplementation(projects.services.toolbox.impl)

libraries/push/impl/src/main/kotlin/io/element/android/libraries/push/impl/DefaultPushService.kt

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import io.element.android.libraries.di.AppScope
2121
import io.element.android.libraries.matrix.api.MatrixClient
2222
import io.element.android.libraries.push.api.GetCurrentPushProvider
2323
import io.element.android.libraries.push.api.PushService
24-
import io.element.android.libraries.push.impl.notifications.DefaultNotificationDrawerManager
24+
import io.element.android.libraries.push.impl.test.TestPush
2525
import io.element.android.libraries.pushproviders.api.Distributor
2626
import io.element.android.libraries.pushproviders.api.PushProvider
2727
import io.element.android.libraries.pushstore.api.UserPushStoreFactory
@@ -30,16 +30,11 @@ import javax.inject.Inject
3030

3131
@ContributesBinding(AppScope::class)
3232
class DefaultPushService @Inject constructor(
33-
private val defaultNotificationDrawerManager: DefaultNotificationDrawerManager,
34-
private val pushersManager: PushersManager,
33+
private val testPush: TestPush,
3534
private val userPushStoreFactory: UserPushStoreFactory,
3635
private val pushProviders: Set<@JvmSuppressWildcards PushProvider>,
3736
private val getCurrentPushProvider: GetCurrentPushProvider,
3837
) : PushService {
39-
override fun notificationStyleChanged() {
40-
defaultNotificationDrawerManager.notificationStyleChanged()
41-
}
42-
4338
override suspend fun getCurrentPushProvider(): PushProvider? {
4439
val currentPushProvider = getCurrentPushProvider.getCurrentPushProvider()
4540
return pushProviders.find { it.name == currentPushProvider }
@@ -51,9 +46,6 @@ class DefaultPushService @Inject constructor(
5146
.sortedBy { it.index }
5247
}
5348

54-
/**
55-
* Get current push provider, compare with provided one, then unregister and register if different, and store change.
56-
*/
5749
override suspend fun registerWith(
5850
matrixClient: MatrixClient,
5951
pushProvider: PushProvider,
@@ -80,7 +72,7 @@ class DefaultPushService @Inject constructor(
8072
override suspend fun testPush(): Boolean {
8173
val pushProvider = getCurrentPushProvider() ?: return false
8274
val config = pushProvider.getCurrentUserPushConfig() ?: return false
83-
pushersManager.testPush(config)
75+
testPush.execute(config)
8476
return true
8577
}
8678
}

libraries/push/impl/src/main/kotlin/io/element/android/libraries/push/impl/PushersManager.kt renamed to libraries/push/impl/src/main/kotlin/io/element/android/libraries/push/impl/DefaultPusherSubscriber.kt

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,9 @@ import io.element.android.libraries.core.log.logger.LoggerTag
2222
import io.element.android.libraries.core.meta.BuildMeta
2323
import io.element.android.libraries.di.AppScope
2424
import io.element.android.libraries.matrix.api.MatrixClient
25-
import io.element.android.libraries.matrix.api.core.EventId
26-
import io.element.android.libraries.matrix.api.core.RoomId
2725
import io.element.android.libraries.matrix.api.core.SessionId
2826
import io.element.android.libraries.matrix.api.pusher.SetHttpPusherData
2927
import io.element.android.libraries.matrix.api.pusher.UnsetHttpPusherData
30-
import io.element.android.libraries.push.impl.pushgateway.PushGatewayNotifyRequest
31-
import io.element.android.libraries.pushproviders.api.CurrentUserPushConfig
3228
import io.element.android.libraries.pushproviders.api.PusherSubscriber
3329
import io.element.android.libraries.pushstore.api.UserPushStoreFactory
3430
import io.element.android.libraries.pushstore.api.clientsecret.PushClientSecret
@@ -37,29 +33,14 @@ import javax.inject.Inject
3733

3834
internal const val DEFAULT_PUSHER_FILE_TAG = "mobile"
3935

40-
private val loggerTag = LoggerTag("PushersManager", LoggerTag.PushLoggerTag)
36+
private val loggerTag = LoggerTag("DefaultPusherSubscriber", LoggerTag.PushLoggerTag)
4137

4238
@ContributesBinding(AppScope::class)
43-
class PushersManager @Inject constructor(
44-
// private val localeProvider: LocaleProvider,
39+
class DefaultPusherSubscriber @Inject constructor(
4540
private val buildMeta: BuildMeta,
46-
// private val getDeviceInfoUseCase: GetDeviceInfoUseCase,
47-
private val pushGatewayNotifyRequest: PushGatewayNotifyRequest,
4841
private val pushClientSecret: PushClientSecret,
4942
private val userPushStoreFactory: UserPushStoreFactory,
5043
) : PusherSubscriber {
51-
suspend fun testPush(config: CurrentUserPushConfig) {
52-
pushGatewayNotifyRequest.execute(
53-
PushGatewayNotifyRequest.Params(
54-
url = config.url,
55-
appId = PushConfig.PUSHER_APP_ID,
56-
pushKey = config.pushKey,
57-
eventId = TEST_EVENT_ID,
58-
roomId = TEST_ROOM_ID,
59-
)
60-
)
61-
}
62-
6344
/**
6445
* Register a pusher to the server if not done yet.
6546
*/
@@ -131,9 +112,4 @@ class PushersManager @Inject constructor(
131112
Timber.tag(loggerTag.value).e(throwable, "Unable to unregister the pusher")
132113
}
133114
}
134-
135-
companion object {
136-
val TEST_EVENT_ID = EventId("\$THIS_IS_A_FAKE_EVENT_ID")
137-
val TEST_ROOM_ID = RoomId("!room:domain")
138-
}
139115
}
Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ package io.element.android.libraries.push.impl.notifications
1919
import android.content.Context
2020
import android.net.Uri
2121
import androidx.core.content.FileProvider
22+
import com.squareup.anvil.annotations.ContributesBinding
2223
import io.element.android.libraries.core.log.logger.LoggerTag
24+
import io.element.android.libraries.di.AppScope
2325
import io.element.android.libraries.di.ApplicationContext
2426
import io.element.android.libraries.matrix.api.MatrixClient
2527
import io.element.android.libraries.matrix.api.MatrixClientProvider
@@ -55,23 +57,28 @@ import io.element.android.services.toolbox.api.systemclock.SystemClock
5557
import timber.log.Timber
5658
import javax.inject.Inject
5759

58-
private val loggerTag = LoggerTag("NotifiableEventResolver", LoggerTag.NotificationLoggerTag)
60+
private val loggerTag = LoggerTag("DefaultNotifiableEventResolver", LoggerTag.NotificationLoggerTag)
5961

6062
/**
6163
* The notifiable event resolver is able to create a NotifiableEvent (view model for notifications) from an sdk Event.
6264
* It is used as a bridge between the Event Thread and the NotificationDrawerManager.
6365
* The NotifiableEventResolver is the only aware of session/store, the NotificationDrawerManager has no knowledge of that,
6466
* this pattern allow decoupling between the object responsible of displaying notifications and the matrix sdk.
6567
*/
66-
class NotifiableEventResolver @Inject constructor(
68+
interface NotifiableEventResolver {
69+
suspend fun resolveEvent(sessionId: SessionId, roomId: RoomId, eventId: EventId): NotifiableEvent?
70+
}
71+
72+
@ContributesBinding(AppScope::class)
73+
class DefaultNotifiableEventResolver @Inject constructor(
6774
private val stringProvider: StringProvider,
6875
private val clock: SystemClock,
6976
private val matrixClientProvider: MatrixClientProvider,
7077
private val notificationMediaRepoFactory: NotificationMediaRepo.Factory,
7178
@ApplicationContext private val context: Context,
7279
private val permalinkParser: PermalinkParser,
73-
) {
74-
suspend fun resolveEvent(sessionId: SessionId, roomId: RoomId, eventId: EventId): NotifiableEvent? {
80+
) : NotifiableEventResolver {
81+
override suspend fun resolveEvent(sessionId: SessionId, roomId: RoomId, eventId: EventId): NotifiableEvent? {
7582
// Restore session
7683
val client = matrixClientProvider.getOrRestore(sessionId).getOrNull() ?: return null
7784
val notificationService = client.notificationService()

libraries/push/impl/src/main/kotlin/io/element/android/libraries/push/impl/notifications/DefaultNotificationDrawerManager.kt

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -221,18 +221,6 @@ class DefaultNotificationDrawerManager @Inject constructor(
221221
}
222222
}
223223

224-
// TODO EAx Must be per account
225-
fun notificationStyleChanged() {
226-
updateEvents(doRender = true) {
227-
val newSettings = true // pushDataStore.useCompleteNotificationFormat()
228-
if (newSettings != useCompleteNotificationFormat) {
229-
// Settings has changed, remove all current notifications
230-
notificationRenderer.cancelAllNotifications()
231-
useCompleteNotificationFormat = newSettings
232-
}
233-
}
234-
}
235-
236224
private fun updateEvents(
237225
doRender: Boolean,
238226
action: (NotificationEventQueue) -> Unit,

0 commit comments

Comments
 (0)