@@ -11,13 +11,15 @@ import io.sentry.SentryLevel
1111import io.sentry.SentryLevel.DEBUG
1212import io.sentry.SentryLevel.FATAL
1313import io.sentry.SentryOptions
14+ import io.sentry.Session
1415import io.sentry.android.core.cache.AndroidEnvelopeCache
1516import io.sentry.android.fragment.FragmentLifecycleIntegration
1617import io.sentry.android.timber.SentryTimberIntegration
1718import io.sentry.cache.IEnvelopeCache
1819import io.sentry.transport.NoOpEnvelopeCache
1920import io.sentry.util.StringUtils
2021import org.junit.runner.RunWith
22+ import org.mockito.Mockito
2123import org.mockito.kotlin.any
2224import org.mockito.kotlin.eq
2325import org.mockito.kotlin.mock
@@ -188,12 +190,36 @@ class SentryAndroidTest {
188190 }
189191
190192 @Test
191- fun `init starts a session if auto session tracking is enabled` () {
192- fixture.initSut { options ->
193- options.isEnableAutoSessionTracking = true
193+ fun `init starts a session if auto session tracking is enabled and app is in foreground ` () {
194+ initSentryWithForegroundImportance( true ) { session : Session ? ->
195+ assertNotNull(session)
194196 }
195- Sentry .getCurrentHub().withScope { scope ->
196- assertNotNull(scope.session)
197+ }
198+
199+ @Test
200+ fun `init does not start a session if auto session tracking is enabled but the app is in background` () {
201+ initSentryWithForegroundImportance(false ) { session: Session ? ->
202+ assertNull(session)
203+ }
204+ }
205+
206+ private fun initSentryWithForegroundImportance (inForeground : Boolean , callback : (session: Session ? ) -> Unit ) {
207+ val context = ContextUtilsTest .createMockContext()
208+
209+ Mockito .mockStatic(ContextUtils ::class .java).use { mockedContextUtils ->
210+ mockedContextUtils.`when `<Any > { ContextUtils .isForegroundImportance(context) }
211+ .thenReturn(inForeground)
212+ SentryAndroid .init (context) { options ->
213+ options.release = " prod"
214+ options.dsn
= " https://[email protected] /123" 215+ options.isEnableAutoSessionTracking = true
216+ }
217+
218+ var session: Session ? = null
219+ Sentry .getCurrentHub().configureScope { scope ->
220+ session = scope.session
221+ }
222+ callback(session)
197223 }
198224 }
199225
0 commit comments