Skip to content

Commit 42ff8e9

Browse files
committed
Fix tests
1 parent d2263b8 commit 42ff8e9

File tree

2 files changed

+27
-48
lines changed

2 files changed

+27
-48
lines changed

sentry-android-core/src/test/java/io/sentry/android/core/LifecycleWatcherTest.kt

Lines changed: 23 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package io.sentry.android.core
22

3-
import androidx.lifecycle.LifecycleOwner
43
import io.sentry.Breadcrumb
54
import io.sentry.DateUtils
65
import io.sentry.IContinuousProfiler
@@ -16,10 +15,8 @@ import io.sentry.transport.ICurrentDateProvider
1615
import kotlin.test.BeforeTest
1716
import kotlin.test.Test
1817
import kotlin.test.assertEquals
19-
import kotlin.test.assertFalse
2018
import kotlin.test.assertNotNull
2119
import kotlin.test.assertNull
22-
import kotlin.test.assertTrue
2320
import org.mockito.ArgumentCaptor
2421
import org.mockito.kotlin.any
2522
import org.mockito.kotlin.check
@@ -33,7 +30,6 @@ import org.mockito.kotlin.whenever
3330

3431
class LifecycleWatcherTest {
3532
private class Fixture {
36-
val ownerMock = mock<LifecycleOwner>()
3733
val scopes = mock<IScopes>()
3834
val dateProvider = mock<ICurrentDateProvider>()
3935
val options = SentryOptions()
@@ -77,7 +73,7 @@ class LifecycleWatcherTest {
7773
@Test
7874
fun `if last started session is 0, start new session`() {
7975
val watcher = fixture.getSUT(enableAppLifecycleBreadcrumbs = false)
80-
watcher.onStart(fixture.ownerMock)
76+
watcher.onForeground()
8177
verify(fixture.scopes).startSession()
8278
verify(fixture.replayController).start()
8379
}
@@ -86,8 +82,8 @@ class LifecycleWatcherTest {
8682
fun `if last started session is after interval, start new session`() {
8783
val watcher = fixture.getSUT(enableAppLifecycleBreadcrumbs = false)
8884
whenever(fixture.dateProvider.currentTimeMillis).thenReturn(1L, 2L)
89-
watcher.onStart(fixture.ownerMock)
90-
watcher.onStart(fixture.ownerMock)
85+
watcher.onForeground()
86+
watcher.onForeground()
9187
verify(fixture.scopes, times(2)).startSession()
9288
verify(fixture.replayController, times(2)).start()
9389
}
@@ -96,17 +92,17 @@ class LifecycleWatcherTest {
9692
fun `if last started session is before interval, it should not start a new session`() {
9793
val watcher = fixture.getSUT(enableAppLifecycleBreadcrumbs = false)
9894
whenever(fixture.dateProvider.currentTimeMillis).thenReturn(2L, 1L)
99-
watcher.onStart(fixture.ownerMock)
100-
watcher.onStart(fixture.ownerMock)
95+
watcher.onForeground()
96+
watcher.onForeground()
10197
verify(fixture.scopes).startSession()
10298
verify(fixture.replayController).start()
10399
}
104100

105101
@Test
106102
fun `if app goes to background, end session after interval`() {
107103
val watcher = fixture.getSUT(enableAppLifecycleBreadcrumbs = false)
108-
watcher.onStart(fixture.ownerMock)
109-
watcher.onStop(fixture.ownerMock)
104+
watcher.onForeground()
105+
watcher.onBackground()
110106
verify(fixture.scopes, timeout(10000)).endSession()
111107
verify(fixture.replayController, timeout(10000)).stop()
112108
verify(fixture.continuousProfiler, timeout(10000)).close(eq(false))
@@ -116,12 +112,12 @@ class LifecycleWatcherTest {
116112
fun `if app goes to background and foreground again, dont end the session`() {
117113
val watcher =
118114
fixture.getSUT(sessionIntervalMillis = 30000L, enableAppLifecycleBreadcrumbs = false)
119-
watcher.onStart(fixture.ownerMock)
115+
watcher.onForeground()
120116

121-
watcher.onStop(fixture.ownerMock)
117+
watcher.onBackground()
122118
assertNotNull(watcher.timerTask)
123119

124-
watcher.onStart(fixture.ownerMock)
120+
watcher.onForeground()
125121
assertNull(watcher.timerTask)
126122

127123
verify(fixture.scopes, never()).endSession()
@@ -132,22 +128,22 @@ class LifecycleWatcherTest {
132128
fun `When session tracking is disabled, do not start session`() {
133129
val watcher =
134130
fixture.getSUT(enableAutoSessionTracking = false, enableAppLifecycleBreadcrumbs = false)
135-
watcher.onStart(fixture.ownerMock)
131+
watcher.onForeground()
136132
verify(fixture.scopes, never()).startSession()
137133
}
138134

139135
@Test
140136
fun `When session tracking is disabled, do not end session`() {
141137
val watcher =
142138
fixture.getSUT(enableAutoSessionTracking = false, enableAppLifecycleBreadcrumbs = false)
143-
watcher.onStop(fixture.ownerMock)
139+
watcher.onBackground()
144140
verify(fixture.scopes, never()).endSession()
145141
}
146142

147143
@Test
148144
fun `When app lifecycle breadcrumbs is enabled, add breadcrumb on start`() {
149145
val watcher = fixture.getSUT(enableAutoSessionTracking = false)
150-
watcher.onStart(fixture.ownerMock)
146+
watcher.onForeground()
151147
verify(fixture.scopes)
152148
.addBreadcrumb(
153149
check<Breadcrumb> {
@@ -163,14 +159,14 @@ class LifecycleWatcherTest {
163159
fun `When app lifecycle breadcrumbs is disabled, do not add breadcrumb on start`() {
164160
val watcher =
165161
fixture.getSUT(enableAutoSessionTracking = false, enableAppLifecycleBreadcrumbs = false)
166-
watcher.onStart(fixture.ownerMock)
162+
watcher.onForeground()
167163
verify(fixture.scopes, never()).addBreadcrumb(any<Breadcrumb>())
168164
}
169165

170166
@Test
171167
fun `When app lifecycle breadcrumbs is enabled, add breadcrumb on stop`() {
172168
val watcher = fixture.getSUT(enableAutoSessionTracking = false)
173-
watcher.onStop(fixture.ownerMock)
169+
watcher.onBackground()
174170
verify(fixture.scopes)
175171
.addBreadcrumb(
176172
check<Breadcrumb> {
@@ -186,7 +182,7 @@ class LifecycleWatcherTest {
186182
fun `When app lifecycle breadcrumbs is disabled, do not add breadcrumb on stop`() {
187183
val watcher =
188184
fixture.getSUT(enableAutoSessionTracking = false, enableAppLifecycleBreadcrumbs = false)
189-
watcher.onStop(fixture.ownerMock)
185+
watcher.onBackground()
190186
verify(fixture.scopes, never()).addBreadcrumb(any<Breadcrumb>())
191187
}
192188

@@ -221,7 +217,7 @@ class LifecycleWatcherTest {
221217
),
222218
)
223219

224-
watcher.onStart(fixture.ownerMock)
220+
watcher.onForeground()
225221
verify(fixture.scopes, never()).startSession()
226222
verify(fixture.replayController, never()).start()
227223
}
@@ -250,25 +246,11 @@ class LifecycleWatcherTest {
250246
),
251247
)
252248

253-
watcher.onStart(fixture.ownerMock)
249+
watcher.onForeground()
254250
verify(fixture.scopes).startSession()
255251
verify(fixture.replayController).start()
256252
}
257253

258-
@Test
259-
fun `When app goes into foreground, sets isBackground to false for AppState`() {
260-
val watcher = fixture.getSUT()
261-
watcher.onStart(fixture.ownerMock)
262-
assertFalse(AppState.getInstance().isInBackground!!)
263-
}
264-
265-
@Test
266-
fun `When app goes into background, sets isBackground to true for AppState`() {
267-
val watcher = fixture.getSUT()
268-
watcher.onStop(fixture.ownerMock)
269-
assertTrue(AppState.getInstance().isInBackground!!)
270-
}
271-
272254
@Test
273255
fun `if the hub has already a fresh session running, resumes replay to invalidate isManualPause flag`() {
274256
val watcher =
@@ -293,24 +275,24 @@ class LifecycleWatcherTest {
293275
),
294276
)
295277

296-
watcher.onStart(fixture.ownerMock)
278+
watcher.onForeground()
297279
verify(fixture.replayController).resume()
298280
}
299281

300282
@Test
301283
fun `background-foreground replay`() {
302284
whenever(fixture.dateProvider.currentTimeMillis).thenReturn(1L)
303285
val watcher = fixture.getSUT(sessionIntervalMillis = 2L, enableAppLifecycleBreadcrumbs = false)
304-
watcher.onStart(fixture.ownerMock)
286+
watcher.onForeground()
305287
verify(fixture.replayController).start()
306288

307-
watcher.onStop(fixture.ownerMock)
289+
watcher.onBackground()
308290
verify(fixture.replayController).pause()
309291

310-
watcher.onStart(fixture.ownerMock)
292+
watcher.onForeground()
311293
verify(fixture.replayController, times(2)).resume()
312294

313-
watcher.onStop(fixture.ownerMock)
295+
watcher.onBackground()
314296
verify(fixture.replayController, timeout(10000)).stop()
315297
}
316298
}

sentry-android-core/src/test/java/io/sentry/android/core/SessionTrackingIntegrationTest.kt

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import io.sentry.SentryEnvelope
1818
import io.sentry.SentryEvent
1919
import io.sentry.SentryLogEvent
2020
import io.sentry.SentryLogEvents
21-
import io.sentry.SentryOptions
2221
import io.sentry.SentryReplayEvent
2322
import io.sentry.Session
2423
import io.sentry.TraceContext
@@ -41,6 +40,7 @@ class SessionTrackingIntegrationTest {
4140

4241
@BeforeTest
4342
fun `set up`() {
43+
AppState.getInstance().resetInstance()
4444
context = ApplicationProvider.getApplicationContext()
4545
}
4646

@@ -56,7 +56,7 @@ class SessionTrackingIntegrationTest {
5656
}
5757
val client = CapturingSentryClient()
5858
Sentry.bindClient(client)
59-
val lifecycle = setupLifecycle(options)
59+
val lifecycle = setupLifecycle()
6060
val initSid = lastSessionId()
6161

6262
lifecycle.handleLifecycleEvent(ON_START)
@@ -115,12 +115,9 @@ class SessionTrackingIntegrationTest {
115115
return sid
116116
}
117117

118-
private fun setupLifecycle(options: SentryOptions): LifecycleRegistry {
118+
private fun setupLifecycle(): LifecycleRegistry {
119119
val lifecycle = LifecycleRegistry(ProcessLifecycleOwner.get())
120-
val lifecycleWatcher =
121-
(options.integrations.find { it is AppLifecycleIntegration } as AppLifecycleIntegration)
122-
.watcher
123-
lifecycle.addObserver(lifecycleWatcher!!)
120+
lifecycle.addObserver(AppState.getInstance().lifecycleObserver)
124121
return lifecycle
125122
}
126123

0 commit comments

Comments
 (0)