@@ -33,7 +33,6 @@ import com.google.firebase.sessions.testing.FakeUuidGenerator
33
33
import kotlin.time.Duration.Companion.hours
34
34
import kotlinx.coroutines.ExperimentalCoroutinesApi
35
35
import kotlinx.coroutines.asCoroutineDispatcher
36
- import kotlinx.coroutines.launch
37
36
import kotlinx.coroutines.test.runCurrent
38
37
import kotlinx.coroutines.test.runTest
39
38
import org.junit.After
@@ -135,7 +134,7 @@ class SharedSessionRepositoryTest {
135
134
}
136
135
137
136
@Test
138
- fun appForegroundSharedSessionRepo_updateSuccess () = runTest {
137
+ fun appForegroundGenerateNewSession_updateSuccess () = runTest {
139
138
val sessionFirelogPublisher =
140
139
SessionFirelogPublisherImpl (
141
140
fakeFirebaseApp.firebaseApp,
@@ -166,20 +165,22 @@ class SharedSessionRepositoryTest {
166
165
backgroundDispatcher =
167
166
TestOnlyExecutors .background().asCoroutineDispatcher() + coroutineContext
168
167
)
169
- backgroundScope.launch {
170
- fakeTimeProvider.addInterval(20 .hours)
171
- sharedSessionRepository.appForeground()
172
- }
173
168
runCurrent()
169
+
170
+ fakeTimeProvider.addInterval(20 .hours)
171
+ sharedSessionRepository.appForeground()
172
+ runCurrent()
173
+
174
174
assertThat(sharedSessionRepository.localSessionData.sessionDetails.sessionId)
175
175
.isEqualTo(SESSION_ID_1 )
176
+ assertThat(sharedSessionRepository.localSessionData.backgroundTime).isNull()
176
177
assertThat(sharedSessionRepository.previousNotificationType)
177
178
.isEqualTo(SharedSessionRepositoryImpl .NotificationType .GENERAL )
178
179
fakeDataStore.close()
179
180
}
180
181
181
182
@Test
182
- fun appForegroundSharedSessionRepo_updateFail () = runTest {
183
+ fun appForegroundGenerateNewSession_updateFail () = runTest {
183
184
val sessionFirelogPublisher =
184
185
SessionFirelogPublisherImpl (
185
186
fakeFirebaseApp.firebaseApp,
@@ -201,7 +202,6 @@ class SharedSessionRepositoryTest {
201
202
),
202
203
IllegalArgumentException (" Datastore init failed" )
203
204
)
204
- fakeDataStore.throwOnNextUpdateData(IllegalArgumentException (" Datastore update failed" ))
205
205
val sharedSessionRepository =
206
206
SharedSessionRepositoryImpl (
207
207
sessionsSettings,
@@ -212,15 +212,23 @@ class SharedSessionRepositoryTest {
212
212
backgroundDispatcher =
213
213
TestOnlyExecutors .background().asCoroutineDispatcher() + coroutineContext
214
214
)
215
+ runCurrent()
215
216
216
- backgroundScope.launch {
217
- fakeTimeProvider.addInterval(20 .hours)
218
- sharedSessionRepository.appForeground()
219
- }
217
+ // set background time first
218
+ fakeDataStore.throwOnNextUpdateData(IllegalArgumentException (" Datastore update failed" ))
219
+ sharedSessionRepository.appBackground()
220
220
runCurrent()
221
+
222
+ // foreground update session
223
+ fakeTimeProvider.addInterval(20 .hours)
224
+ fakeDataStore.throwOnNextUpdateData(IllegalArgumentException (" Datastore update failed" ))
225
+ sharedSessionRepository.appForeground()
226
+ runCurrent()
227
+
221
228
// session_2 here because session_1 is failed when try to init datastore
222
229
assertThat(sharedSessionRepository.localSessionData.sessionDetails.sessionId)
223
230
.isEqualTo(SESSION_ID_2 )
231
+ assertThat(sharedSessionRepository.localSessionData.backgroundTime).isNull()
224
232
assertThat(sharedSessionRepository.previousNotificationType)
225
233
.isEqualTo(SharedSessionRepositoryImpl .NotificationType .FALLBACK )
226
234
fakeDataStore.close()
0 commit comments