Skip to content

Commit 20d793b

Browse files
romtsncodex
andcommitted
fix(replay): Preserve queued foreground startup
Check Replay registration when the foreground callback executes so AppState catch-up can start Replay after registration. Cover both callback orderings with tests. Refs JAVA-325 Co-Authored-By: Codex <noreply@openai.com>
1 parent df60eec commit 20d793b

2 files changed

Lines changed: 17 additions & 11 deletions

File tree

‎sentry-android-replay/src/main/java/io/sentry/android/replay/ReplayIntegration.kt‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,10 @@ public class ReplayIntegration(
167167
}
168168

169169
override fun onAppForegrounded(startNewSession: Boolean) {
170-
if (!isEnabled.get()) {
171-
return
172-
}
173170
enqueueOnMainThread {
171+
if (!isEnabled.get()) {
172+
return@enqueueOnMainThread
173+
}
174174
if (startNewSession) {
175175
val isFullSession = sample(options.sessionReplay.sessionSampleRate)
176176
if (!isFullSession && !options.sessionReplay.isSessionReplayForErrorsEnabled) {
@@ -187,9 +187,6 @@ public class ReplayIntegration(
187187
}
188188

189189
override fun onAppBackgrounded() {
190-
if (!isEnabled.get()) {
191-
return
192-
}
193190
enqueueOnMainThread { pauseInternal() }
194191
}
195192

‎sentry-android-replay/src/test/java/io/sentry/android/replay/ReplayIntegrationTest.kt‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -226,14 +226,23 @@ class ReplayIntegrationTest {
226226
}
227227

228228
@Test
229-
fun `lifecycle callbacks before register are not enqueued`() {
230-
val mainLooperHandler = mock<MainLooperHandler>()
231-
val replay = fixture.getSut(context, mainLooperHandler = mainLooperHandler)
229+
fun `foreground before register does nothing`() {
230+
val replay = fixture.getSut(context)
232231

233232
replay.onAppForegrounded(true)
234-
replay.onAppBackgrounded()
235233

236-
verify(mainLooperHandler, never()).post(any())
234+
assertThat(replay.isRecording).isFalse()
235+
}
236+
237+
@Test
238+
fun `foreground queued before register starts replay after register`() {
239+
val replay = fixture.getSut(context, mainLooperHandler = MainLooperHandler())
240+
241+
replay.onAppForegrounded(true)
242+
replay.register(fixture.scopes, fixture.options)
243+
shadowOf(Looper.getMainLooper()).idle()
244+
245+
assertThat(replay.isRecording).isTrue()
237246
}
238247

239248
@Test

0 commit comments

Comments
 (0)