5
5
* Please see LICENSE files in the repository root for full details.
6
6
*/
7
7
8
+ @file:OptIn(ExperimentalCoroutinesApi ::class )
9
+
8
10
package io.element.android.appnav.loggedin
9
11
10
- import app.cash.molecule.RecompositionMode
11
- import app.cash.molecule.moleculeFlow
12
12
import app.cash.turbine.ReceiveTurbine
13
- import app.cash.turbine.test
14
13
import com.google.common.truth.Truth.assertThat
15
14
import im.vector.app.features.analytics.plan.CryptoSessionStateChange
16
15
import im.vector.app.features.analytics.plan.UserProperties
@@ -19,6 +18,7 @@ import io.element.android.libraries.matrix.api.MatrixClient
19
18
import io.element.android.libraries.matrix.api.core.SessionId
20
19
import io.element.android.libraries.matrix.api.encryption.EncryptionService
21
20
import io.element.android.libraries.matrix.api.encryption.RecoveryState
21
+ import io.element.android.libraries.matrix.api.oidc.AccountManagementAction
22
22
import io.element.android.libraries.matrix.api.roomlist.RoomListService
23
23
import io.element.android.libraries.matrix.api.sync.SlidingSyncVersion
24
24
import io.element.android.libraries.matrix.api.sync.SyncState
@@ -45,8 +45,8 @@ import io.element.android.tests.testutils.lambda.any
45
45
import io.element.android.tests.testutils.lambda.lambdaError
46
46
import io.element.android.tests.testutils.lambda.lambdaRecorder
47
47
import io.element.android.tests.testutils.lambda.value
48
+ import io.element.android.tests.testutils.test
48
49
import kotlinx.coroutines.ExperimentalCoroutinesApi
49
- import kotlinx.coroutines.test.TestScope
50
50
import kotlinx.coroutines.test.advanceUntilIdle
51
51
import kotlinx.coroutines.test.runTest
52
52
import org.junit.Rule
@@ -58,24 +58,40 @@ class LoggedInPresenterTest {
58
58
59
59
@Test
60
60
fun `present - initial state` () = runTest {
61
- val presenter = createLoggedInPresenter()
62
- moleculeFlow(RecompositionMode .Immediate ) {
63
- presenter.present()
64
- }.test {
61
+ createLoggedInPresenter().test {
65
62
val initialState = awaitItem()
66
63
assertThat(initialState.showSyncSpinner).isFalse()
67
64
assertThat(initialState.pusherRegistrationState.isUninitialized()).isTrue()
68
65
assertThat(initialState.ignoreRegistrationError).isFalse()
69
66
}
70
67
}
71
68
69
+ @Test
70
+ fun `present - ensure that account urls are preloaded` () = runTest {
71
+ val accountManagementUrlResult = lambdaRecorder<AccountManagementAction ?, Result <String ?>> { Result .success(" aUrl" ) }
72
+ val matrixClient = FakeMatrixClient (
73
+ accountManagementUrlResult = accountManagementUrlResult,
74
+ )
75
+ createLoggedInPresenter(
76
+ matrixClient = matrixClient,
77
+ ).test {
78
+ awaitItem()
79
+ advanceUntilIdle()
80
+ accountManagementUrlResult.assertions().isCalledExactly(2 )
81
+ .withSequence(
82
+ listOf (value(AccountManagementAction .Profile )),
83
+ listOf (value(AccountManagementAction .SessionsList )),
84
+ )
85
+ }
86
+ }
87
+
72
88
@Test
73
89
fun `present - show sync spinner` () = runTest {
74
90
val roomListService = FakeRoomListService ()
75
- val presenter = createLoggedInPresenter(roomListService, SyncState . Running )
76
- moleculeFlow( RecompositionMode . Immediate ) {
77
- presenter.present()
78
- } .test {
91
+ createLoggedInPresenter(
92
+ syncState = SyncState . Running ,
93
+ matrixClient = FakeMatrixClient (roomListService = roomListService),
94
+ ) .test {
79
95
val initialState = awaitItem()
80
96
assertThat(initialState.showSyncSpinner).isFalse()
81
97
roomListService.postSyncIndicator(RoomListService .SyncIndicator .Show )
@@ -92,18 +108,18 @@ class LoggedInPresenterTest {
92
108
val verificationService = FakeSessionVerificationService ()
93
109
val encryptionService = FakeEncryptionService ()
94
110
val buildMeta = aBuildMeta()
95
- val presenter = LoggedInPresenter (
96
- matrixClient = FakeMatrixClient (roomListService = roomListService, encryptionService = encryptionService),
111
+ LoggedInPresenter (
112
+ matrixClient = FakeMatrixClient (
113
+ roomListService = roomListService,
114
+ encryptionService = encryptionService,
115
+ ),
97
116
syncService = FakeSyncService (initialSyncState = SyncState .Running ),
98
117
pushService = FakePushService (),
99
118
sessionVerificationService = verificationService,
100
119
analyticsService = analyticsService,
101
120
encryptionService = encryptionService,
102
121
buildMeta = buildMeta,
103
- )
104
- moleculeFlow(RecompositionMode .Immediate ) {
105
- presenter.present()
106
- }.test {
122
+ ).test {
107
123
encryptionService.emitRecoveryState(RecoveryState .UNKNOWN )
108
124
encryptionService.emitRecoveryState(RecoveryState .INCOMPLETE )
109
125
verificationService.emitVerifiedStatus(SessionVerifiedStatus .Verified )
@@ -129,13 +145,10 @@ class LoggedInPresenterTest {
129
145
val verificationService = FakeSessionVerificationService (
130
146
initialSessionVerifiedStatus = SessionVerifiedStatus .NotVerified
131
147
)
132
- val presenter = createLoggedInPresenter(
148
+ createLoggedInPresenter(
133
149
pushService = pushService,
134
150
sessionVerificationService = verificationService,
135
- )
136
- moleculeFlow(RecompositionMode .Immediate ) {
137
- presenter.present()
138
- }.test {
151
+ ).test {
139
152
val finalState = awaitFirstItem()
140
153
assertThat(finalState.pusherRegistrationState.errorOrNull())
141
154
.isInstanceOf(PusherRegistrationFailure .AccountNotVerified ::class .java)
@@ -155,13 +168,13 @@ class LoggedInPresenterTest {
155
168
val pushService = createFakePushService(
156
169
registerWithLambda = lambda,
157
170
)
158
- val presenter = createLoggedInPresenter(
171
+ createLoggedInPresenter(
159
172
pushService = pushService,
160
173
sessionVerificationService = sessionVerificationService,
161
- )
162
- moleculeFlow( RecompositionMode . Immediate ) {
163
- presenter.present()
164
- } .test {
174
+ matrixClient = FakeMatrixClient (
175
+ accountManagementUrlResult = { Result .success( null ) },
176
+ ),
177
+ ) .test {
165
178
val finalState = awaitFirstItem()
166
179
assertThat(finalState.pusherRegistrationState.isSuccess()).isTrue()
167
180
lambda.assertions()
@@ -188,13 +201,13 @@ class LoggedInPresenterTest {
188
201
val pushService = createFakePushService(
189
202
registerWithLambda = lambda,
190
203
)
191
- val presenter = createLoggedInPresenter(
204
+ createLoggedInPresenter(
192
205
pushService = pushService,
193
206
sessionVerificationService = sessionVerificationService,
194
- )
195
- moleculeFlow( RecompositionMode . Immediate ) {
196
- presenter.present()
197
- } .test {
207
+ matrixClient = FakeMatrixClient (
208
+ accountManagementUrlResult = { Result .success( null ) },
209
+ ),
210
+ ) .test {
198
211
val finalState = awaitFirstItem()
199
212
assertThat(finalState.pusherRegistrationState.isFailure()).isTrue()
200
213
lambda.assertions()
@@ -233,13 +246,13 @@ class LoggedInPresenterTest {
233
246
currentPushProvider = { pushProvider },
234
247
registerWithLambda = lambda,
235
248
)
236
- val presenter = createLoggedInPresenter(
249
+ createLoggedInPresenter(
237
250
pushService = pushService,
238
251
sessionVerificationService = sessionVerificationService,
239
- )
240
- moleculeFlow( RecompositionMode . Immediate ) {
241
- presenter.present()
242
- } .test {
252
+ matrixClient = FakeMatrixClient (
253
+ accountManagementUrlResult = { Result .success( null ) },
254
+ ),
255
+ ) .test {
243
256
val finalState = awaitFirstItem()
244
257
assertThat(finalState.pusherRegistrationState.isSuccess()).isTrue()
245
258
lambda.assertions()
@@ -277,13 +290,13 @@ class LoggedInPresenterTest {
277
290
currentPushProvider = { pushProvider },
278
291
registerWithLambda = lambda,
279
292
)
280
- val presenter = createLoggedInPresenter(
293
+ createLoggedInPresenter(
281
294
pushService = pushService,
282
295
sessionVerificationService = sessionVerificationService,
283
- )
284
- moleculeFlow( RecompositionMode . Immediate ) {
285
- presenter.present()
286
- } .test {
296
+ matrixClient = FakeMatrixClient (
297
+ accountManagementUrlResult = { Result .success( null ) },
298
+ ),
299
+ ) .test {
287
300
val finalState = awaitFirstItem()
288
301
assertThat(finalState.pusherRegistrationState.isSuccess()).isTrue()
289
302
lambda.assertions()
@@ -317,13 +330,10 @@ class LoggedInPresenterTest {
317
330
currentPushProvider = { pushProvider },
318
331
registerWithLambda = lambda,
319
332
)
320
- val presenter = createLoggedInPresenter(
333
+ createLoggedInPresenter(
321
334
pushService = pushService,
322
335
sessionVerificationService = sessionVerificationService,
323
- )
324
- moleculeFlow(RecompositionMode .Immediate ) {
325
- presenter.present()
326
- }.test {
336
+ ).test {
327
337
val finalState = awaitFirstItem()
328
338
assertThat(finalState.pusherRegistrationState.errorOrNull())
329
339
.isInstanceOf(PusherRegistrationFailure .NoDistributorsAvailable ::class .java)
@@ -345,13 +355,10 @@ class LoggedInPresenterTest {
345
355
registerWithLambda = lambda,
346
356
setIgnoreRegistrationErrorLambda = setIgnoreRegistrationErrorLambda,
347
357
)
348
- val presenter = createLoggedInPresenter(
358
+ createLoggedInPresenter(
349
359
pushService = pushService,
350
360
sessionVerificationService = sessionVerificationService,
351
- )
352
- moleculeFlow(RecompositionMode .Immediate ) {
353
- presenter.present()
354
- }.test {
361
+ ).test {
355
362
val finalState = awaitFirstItem()
356
363
assertThat(finalState.pusherRegistrationState.errorOrNull())
357
364
.isInstanceOf(PusherRegistrationFailure .NoProvidersAvailable ::class .java)
@@ -394,13 +401,10 @@ class LoggedInPresenterTest {
394
401
registerWithLambda = lambda,
395
402
selectPushProviderLambda = selectPushProviderLambda,
396
403
)
397
- val presenter = createLoggedInPresenter(
404
+ createLoggedInPresenter(
398
405
pushService = pushService,
399
406
sessionVerificationService = sessionVerificationService,
400
- )
401
- moleculeFlow(RecompositionMode .Immediate ) {
402
- presenter.present()
403
- }.test {
407
+ ).test {
404
408
val finalState = awaitFirstItem()
405
409
assertThat(finalState.pusherRegistrationState.errorOrNull())
406
410
.isInstanceOf(PusherRegistrationFailure .NoDistributorsAvailable ::class .java)
@@ -445,13 +449,13 @@ class LoggedInPresenterTest {
445
449
pushProvider1 = pushProvider1,
446
450
registerWithLambda = lambda,
447
451
)
448
- val presenter = createLoggedInPresenter(
452
+ createLoggedInPresenter(
449
453
pushService = pushService,
450
454
sessionVerificationService = sessionVerificationService,
451
- )
452
- moleculeFlow( RecompositionMode . Immediate ) {
453
- presenter.present()
454
- } .test {
455
+ matrixClient = FakeMatrixClient (
456
+ accountManagementUrlResult = { Result .success( null ) },
457
+ ),
458
+ ) .test {
455
459
val finalState = awaitFirstItem()
456
460
assertThat(finalState.pusherRegistrationState.isSuccess()).isTrue()
457
461
lambda.assertions().isCalledOnce()
@@ -505,10 +509,9 @@ class LoggedInPresenterTest {
505
509
currentSlidingSyncVersionLambda = { Result .success(SlidingSyncVersion .Proxy ) },
506
510
availableSlidingSyncVersionsLambda = { Result .success(listOf (SlidingSyncVersion .Native )) },
507
511
)
508
- val presenter = createLoggedInPresenter(matrixClient = matrixClient)
509
- moleculeFlow(RecompositionMode .Immediate ) {
510
- presenter.present()
511
- }.test {
512
+ createLoggedInPresenter(
513
+ matrixClient = matrixClient,
514
+ ).test {
512
515
val initialState = awaitItem()
513
516
assertThat(initialState.forceNativeSlidingSyncMigration).isFalse()
514
517
@@ -525,13 +528,14 @@ class LoggedInPresenterTest {
525
528
assertThat(userInitiated).isTrue()
526
529
assertThat(ignoreSdkError).isTrue()
527
530
}
528
- val matrixClient = FakeMatrixClient ().apply {
531
+ val matrixClient = FakeMatrixClient (
532
+ accountManagementUrlResult = { Result .success(null ) },
533
+ ).apply {
529
534
this .logoutLambda = logoutLambda
530
535
}
531
- val presenter = createLoggedInPresenter(matrixClient = matrixClient)
532
- moleculeFlow(RecompositionMode .Immediate ) {
533
- presenter.present()
534
- }.test {
536
+ createLoggedInPresenter(
537
+ matrixClient = matrixClient,
538
+ ).test {
535
539
val initialState = awaitItem()
536
540
537
541
initialState.eventSink(LoggedInEvents .LogoutAndMigrateToNativeSlidingSync )
@@ -547,14 +551,15 @@ class LoggedInPresenterTest {
547
551
return awaitItem()
548
552
}
549
553
550
- private fun TestScope.createLoggedInPresenter (
551
- roomListService : RoomListService = FakeRoomListService (),
554
+ private fun createLoggedInPresenter (
552
555
syncState : SyncState = SyncState .Running ,
553
556
analyticsService : AnalyticsService = FakeAnalyticsService (),
554
557
sessionVerificationService : SessionVerificationService = FakeSessionVerificationService (),
555
558
encryptionService : EncryptionService = FakeEncryptionService (),
556
559
pushService : PushService = FakePushService (),
557
- matrixClient : MatrixClient = FakeMatrixClient (roomListService = roomListService),
560
+ matrixClient : MatrixClient = FakeMatrixClient (
561
+ accountManagementUrlResult = { Result .success(null) },
562
+ ),
558
563
buildMeta : BuildMeta = aBuildMeta(),
559
564
): LoggedInPresenter {
560
565
return LoggedInPresenter (
0 commit comments