diff --git a/posthog-android/src/test/java/com/posthog/android/PostHogAndroidTest.kt b/posthog-android/src/test/java/com/posthog/android/PostHogAndroidTest.kt new file mode 100644 index 00000000..13e5164d --- /dev/null +++ b/posthog-android/src/test/java/com/posthog/android/PostHogAndroidTest.kt @@ -0,0 +1,224 @@ +package com.posthog.android + +import android.content.Context +import androidx.test.ext.junit.runners.AndroidJUnit4 +import com.posthog.PostHog +import com.posthog.android.internal.PostHogActivityLifecycleCallbackIntegration +import com.posthog.android.internal.PostHogAndroidContext +import com.posthog.android.internal.PostHogAndroidLogger +import com.posthog.android.internal.PostHogAndroidNetworkStatus +import com.posthog.android.internal.PostHogAppInstallIntegration +import com.posthog.android.internal.PostHogLifecycleObserverIntegration +import com.posthog.android.internal.PostHogSharedPreferences +import org.junit.Rule +import org.junit.rules.TemporaryFolder +import org.junit.runner.RunWith +import org.mockito.kotlin.mock +import kotlin.test.AfterTest +import kotlin.test.BeforeTest +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertNotNull +import kotlin.test.assertNull +import kotlin.test.assertTrue + +@RunWith(AndroidJUnit4::class) +internal class PostHogAndroidTest { + + private val context = mock() + + @get:Rule + val tmpDir = TemporaryFolder() + + @BeforeTest + fun `set up`() { + PostHog.close() + } + + @AfterTest + fun `set down`() { + tmpDir.root.deleteRecursively() + } + + @Test + fun `sets Android Logger if System logger`() { + val config = PostHogAndroidConfig(apiKey) + + mockContextAppStart(context, tmpDir) + + PostHogAndroid.setup(context, config) + + assertTrue(config.logger is PostHogAndroidLogger) + } + + @Test + fun `sets Android context`() { + val config = PostHogAndroidConfig(apiKey) + + mockContextAppStart(context, tmpDir) + + PostHogAndroid.setup(context, config) + + assertTrue(config.context is PostHogAndroidContext) + } + + @Test + fun `sets legacy storage path`() { + val config = PostHogAndroidConfig(apiKey) + + mockContextAppStart(context, tmpDir) + + PostHogAndroid.setup(context, config) + + assertNotNull(config.legacyStoragePrefix) + } + + @Test + fun `sets storage path`() { + val config = PostHogAndroidConfig(apiKey) + + mockContextAppStart(context, tmpDir) + + PostHogAndroid.setup(context, config) + + assertNotNull(config.storagePrefix) + } + + @Test + fun `sets Android cache preferences`() { + val config = PostHogAndroidConfig(apiKey) + + mockContextAppStart(context, tmpDir) + + PostHogAndroid.setup(context, config) + + assertTrue(config.cachePreferences is PostHogSharedPreferences) + } + + @Test + fun `sets Android network checker`() { + val config = PostHogAndroidConfig(apiKey) + + mockContextAppStart(context, tmpDir) + + PostHogAndroid.setup(context, config) + + assertTrue(config.networkStatus is PostHogAndroidNetworkStatus) + } + + @Test + fun `sets Android SDK version`() { + val config = PostHogAndroidConfig(apiKey) + + mockContextAppStart(context, tmpDir) + + PostHogAndroid.setup(context, config) + + assertEquals(BuildConfig.VERSION_NAME, config.sdkVersion) + } + + @Test + fun `adds captureApplicationLifecycleEvents integrations`() { + val config = PostHogAndroidConfig(apiKey) + + mockContextAppStart(context, tmpDir) + + PostHogAndroid.setup(context, config) + + assertNotNull( + config.integrations.find { + it is PostHogAppInstallIntegration + }, + ) + assertNotNull( + config.integrations.find { + it is PostHogLifecycleObserverIntegration + }, + ) + } + + @Test + fun `does not add captureApplicationLifecycleEvents integrations if disabled`() { + val config = PostHogAndroidConfig(apiKey).apply { + captureApplicationLifecycleEvents = false + } + + mockContextAppStart(context, tmpDir) + + PostHogAndroid.setup(context, config) + + assertNull( + config.integrations.find { + it is PostHogAppInstallIntegration + }, + ) + assertNull( + config.integrations.find { + it is PostHogLifecycleObserverIntegration + }, + ) + } + + @Test + fun `adds captureDeepLinks integration`() { + val config = PostHogAndroidConfig(apiKey).apply { + captureScreenViews = false + } + + mockContextAppStart(context, tmpDir) + + PostHogAndroid.setup(context, config) + + assertNotNull( + config.integrations.find { + it is PostHogActivityLifecycleCallbackIntegration + }, + ) + } + + @Test + fun `adds captureScreenViews integration`() { + val config = PostHogAndroidConfig(apiKey).apply { + captureDeepLinks = false + } + + mockContextAppStart(context, tmpDir) + + PostHogAndroid.setup(context, config) + + assertNotNull( + config.integrations.find { + it is PostHogActivityLifecycleCallbackIntegration + }, + ) + } + + @Test + fun `does not add captureDeepLinks and captureScreenViews integration if disabled`() { + val config = PostHogAndroidConfig(apiKey).apply { + captureDeepLinks = false + captureScreenViews = false + } + + mockContextAppStart(context, tmpDir) + + PostHogAndroid.setup(context, config) + + assertNull( + config.integrations.find { + it is PostHogActivityLifecycleCallbackIntegration + }, + ) + } + + @Test + fun `with creates new single instance`() { + val config = PostHogAndroidConfig(apiKey) + + mockContextAppStart(context, tmpDir) + + val postHog = PostHogAndroid.with(context, config) + + assertNotNull(postHog) + } +} diff --git a/posthog-android/src/test/java/com/posthog/android/Utils.kt b/posthog-android/src/test/java/com/posthog/android/Utils.kt index d502b959..17a29cc0 100644 --- a/posthog-android/src/test/java/com/posthog/android/Utils.kt +++ b/posthog-android/src/test/java/com/posthog/android/Utils.kt @@ -1,14 +1,17 @@ package com.posthog.android import android.app.Activity +import android.app.Application import android.content.ComponentName import android.content.Context import android.content.Intent +import android.content.SharedPreferences import android.content.pm.ActivityInfo import android.content.pm.PackageInfo import android.content.pm.PackageManager import android.net.Uri import com.posthog.PostHog +import org.junit.rules.TemporaryFolder import org.mockito.kotlin.any import org.mockito.kotlin.mock import org.mockito.kotlin.whenever @@ -52,6 +55,15 @@ public fun Context.mockPackageInfo(name: String = "1.0.0", code: Int = 1) { whenever(pm.getPackageInfo(any(), any())).thenReturn(pi) } +public fun mockContextAppStart(context: Context, tmpDir: TemporaryFolder) { + val app = mock() + whenever(context.applicationContext).thenReturn(app) + whenever(app.getDir(any(), any())).thenReturn(tmpDir.newFolder()) + whenever(app.cacheDir).thenReturn(tmpDir.newFolder()) + val sharedPreferences = mock() + whenever(app.getSharedPreferences(any(), any())).thenReturn(sharedPreferences) +} + public fun createPostHogFake(): PostHogFake { val fake = PostHogFake() PostHog.overrideSharedInstance(fake)