@@ -54,18 +54,21 @@ class AndroidOptionsInitializerTest {
54
54
useRealContext : Boolean = false,
55
55
configureOptions : SentryAndroidOptions .() -> Unit = {},
56
56
configureContext : Context .() -> Unit = {},
57
- assets : AssetManager ? = null
57
+ assets : AssetManager ? = null,
58
+ customMockContext : Context ? = null,
59
+ isFragmentAvailable : Boolean = false
58
60
) {
59
61
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
+ }
69
72
whenever(mockContext.cacheDir).thenReturn(file)
70
73
if (mockContext.applicationContext != null ) {
71
74
whenever(mockContext.applicationContext.cacheDir).thenReturn(file)
@@ -85,7 +88,7 @@ class AndroidOptionsInitializerTest {
85
88
BuildInfoProvider (AndroidLogger ()),
86
89
loadClass,
87
90
activityFramesTracker,
88
- false ,
91
+ isFragmentAvailable ,
89
92
false ,
90
93
false
91
94
)
@@ -694,4 +697,24 @@ class AndroidOptionsInitializerTest {
694
697
fixture.sentryOptions.findPersistingScopeObserver()?.setTags(mapOf (" key" to " value" ))
695
698
assertFalse(File (AndroidOptionsInitializer .getCacheDir(fixture.context), PersistingScopeObserver .SCOPE_CACHE ).exists())
696
699
}
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
+ }
697
720
}
0 commit comments