@@ -54,18 +54,21 @@ class AndroidOptionsInitializerTest {
5454 useRealContext : Boolean = false,
5555 configureOptions : SentryAndroidOptions .() -> Unit = {},
5656 configureContext : Context .() -> Unit = {},
57- assets : AssetManager ? = null
57+ assets : AssetManager ? = null,
58+ customMockContext : Context ? = null,
59+ isFragmentAvailable : Boolean = false
5860 ) {
5961 sentryOptions.executorService = ImmediateExecutorService ()
60- mockContext = if (metadata != null ) {
61- ContextUtilsTestHelper .mockMetaData(
62- mockContext = ContextUtilsTestHelper .createMockContext(hasAppContext),
63- metaData = metadata,
64- assets = assets
65- )
66- } else {
67- ContextUtilsTestHelper .createMockContext(hasAppContext)
68- }
62+ mockContext = customMockContext
63+ ? : if (metadata != null ) {
64+ ContextUtilsTestHelper .mockMetaData(
65+ mockContext = ContextUtilsTestHelper .createMockContext(hasAppContext),
66+ metaData = metadata,
67+ assets = assets
68+ )
69+ } else {
70+ ContextUtilsTestHelper .createMockContext(hasAppContext)
71+ }
6972 whenever(mockContext.cacheDir).thenReturn(file)
7073 if (mockContext.applicationContext != null ) {
7174 whenever(mockContext.applicationContext.cacheDir).thenReturn(file)
@@ -85,7 +88,7 @@ class AndroidOptionsInitializerTest {
8588 BuildInfoProvider (AndroidLogger ()),
8689 loadClass,
8790 activityFramesTracker,
88- false ,
91+ isFragmentAvailable ,
8992 false ,
9093 false
9194 )
@@ -694,4 +697,24 @@ class AndroidOptionsInitializerTest {
694697 fixture.sentryOptions.findPersistingScopeObserver()?.setTags(mapOf (" key" to " value" ))
695698 assertFalse(File (AndroidOptionsInitializer .getCacheDir(fixture.context), PersistingScopeObserver .SCOPE_CACHE ).exists())
696699 }
700+
701+ @Test
702+ fun `When given context which is not instance of Application use applicationContext to initialize activityLifecycleCallbacks integrations` () {
703+ fixture.initSut(isFragmentAvailable = true )
704+
705+ assertNotNull(fixture.sentryOptions.integrations.firstOrNull { it is ActivityBreadcrumbsIntegration })
706+ assertNotNull(fixture.sentryOptions.integrations.firstOrNull { it is CurrentActivityIntegration })
707+ assertNotNull(fixture.sentryOptions.integrations.firstOrNull { it is UserInteractionIntegration })
708+ assertNotNull(fixture.sentryOptions.integrations.firstOrNull { it is FragmentLifecycleIntegration }) // Only when fragment is available
709+ }
710+
711+ @Test
712+ fun `When given context applicationContext is not App do not initialize activityLifecycleCallbacks integrations` () {
713+ fixture.initSut(customMockContext = mock<Context >())
714+
715+ assertNull(fixture.sentryOptions.integrations.firstOrNull { it is ActivityBreadcrumbsIntegration })
716+ assertNull(fixture.sentryOptions.integrations.firstOrNull { it is CurrentActivityIntegration })
717+ assertNull(fixture.sentryOptions.integrations.firstOrNull { it is UserInteractionIntegration })
718+ assertNull(fixture.sentryOptions.integrations.firstOrNull { it is FragmentLifecycleIntegration }) // Only when fragment is available
719+ }
697720}
0 commit comments