From 976cc836378ef82f6079641342e1d69d487c430a Mon Sep 17 00:00:00 2001 From: Manoel Aranda Neto Date: Thu, 28 Sep 2023 10:49:39 +0200 Subject: [PATCH] add tests for lifecycle observer --- .../PostHogLifecycleObserverIntegration.kt | 8 +- .../java/com/posthog/android/FakeLifecycle.kt | 15 +++ ...PostHogLifecycleObserverIntegrationTest.kt | 93 +++++++++++++++++++ 3 files changed, 114 insertions(+), 2 deletions(-) create mode 100644 posthog-android/src/test/java/com/posthog/android/FakeLifecycle.kt create mode 100644 posthog-android/src/test/java/com/posthog/android/internal/PostHogLifecycleObserverIntegrationTest.kt diff --git a/posthog-android/src/main/java/com/posthog/android/internal/PostHogLifecycleObserverIntegration.kt b/posthog-android/src/main/java/com/posthog/android/internal/PostHogLifecycleObserverIntegration.kt index 37fa0c47..3da4204b 100644 --- a/posthog-android/src/main/java/com/posthog/android/internal/PostHogLifecycleObserverIntegration.kt +++ b/posthog-android/src/main/java/com/posthog/android/internal/PostHogLifecycleObserverIntegration.kt @@ -4,10 +4,12 @@ import android.content.Context import android.os.Handler import android.os.Looper import androidx.lifecycle.DefaultLifecycleObserver +import androidx.lifecycle.Lifecycle import androidx.lifecycle.LifecycleOwner import androidx.lifecycle.ProcessLifecycleOwner import com.posthog.PostHog import com.posthog.PostHogIntegration +import com.posthog.PostHogVisibleForTesting import com.posthog.android.PostHogAndroidConfig /** @@ -18,6 +20,8 @@ import com.posthog.android.PostHogAndroidConfig internal class PostHogLifecycleObserverIntegration( private val context: Context, private val config: PostHogAndroidConfig, + @PostHogVisibleForTesting + private val lifecycle: Lifecycle = ProcessLifecycleOwner.get().lifecycle, ) : DefaultLifecycleObserver, PostHogIntegration { private val handler = Handler(Looper.getMainLooper()) @@ -50,7 +54,7 @@ internal class PostHogLifecycleObserverIntegration( } private fun add() { - ProcessLifecycleOwner.get().lifecycle.addObserver(this) + lifecycle.addObserver(this) } override fun install() { @@ -68,7 +72,7 @@ internal class PostHogLifecycleObserverIntegration( } private fun remove() { - ProcessLifecycleOwner.get().lifecycle.removeObserver(this) + lifecycle.removeObserver(this) } override fun uninstall() { diff --git a/posthog-android/src/test/java/com/posthog/android/FakeLifecycle.kt b/posthog-android/src/test/java/com/posthog/android/FakeLifecycle.kt new file mode 100644 index 00000000..26443169 --- /dev/null +++ b/posthog-android/src/test/java/com/posthog/android/FakeLifecycle.kt @@ -0,0 +1,15 @@ +package com.posthog.android + +import androidx.lifecycle.Lifecycle +import androidx.lifecycle.LifecycleObserver + +internal class FakeLifecycle(override val currentState: State) : Lifecycle() { + var observers = 0 + override fun addObserver(observer: LifecycleObserver) { + observers++ + } + + override fun removeObserver(observer: LifecycleObserver) { + observers-- + } +} diff --git a/posthog-android/src/test/java/com/posthog/android/internal/PostHogLifecycleObserverIntegrationTest.kt b/posthog-android/src/test/java/com/posthog/android/internal/PostHogLifecycleObserverIntegrationTest.kt new file mode 100644 index 00000000..5b4e1f6c --- /dev/null +++ b/posthog-android/src/test/java/com/posthog/android/internal/PostHogLifecycleObserverIntegrationTest.kt @@ -0,0 +1,93 @@ +package com.posthog.android.internal + +import android.content.Context +import androidx.lifecycle.Lifecycle +import androidx.lifecycle.ProcessLifecycleOwner +import androidx.test.ext.junit.runners.AndroidJUnit4 +import com.posthog.PostHog +import com.posthog.android.FakeLifecycle +import com.posthog.android.PostHogAndroidConfig +import com.posthog.android.apiKey +import com.posthog.android.createPostHogFake +import com.posthog.android.mockPackageInfo +import org.junit.runner.RunWith +import org.mockito.kotlin.mock +import kotlin.test.BeforeTest +import kotlin.test.Test +import kotlin.test.assertEquals + +@RunWith(AndroidJUnit4::class) +internal class PostHogLifecycleObserverIntegrationTest { + private val context = mock() + private val fakeLifecycle = FakeLifecycle(Lifecycle.State.CREATED) + + private fun getSut(): PostHogLifecycleObserverIntegration { + val config = PostHogAndroidConfig(apiKey) + return PostHogLifecycleObserverIntegration(context, config, lifecycle = fakeLifecycle) + } + + @BeforeTest + fun `set up`() { + PostHog.resetSharedInstance() + } + + @Test + fun `install adds the observer`() { + val sut = getSut() + + sut.install() + + assertEquals(1, fakeLifecycle.observers) + } + + @Test + fun `uninstall removes the observer`() { + val sut = getSut() + + sut.install() + sut.uninstall() + + assertEquals(0, fakeLifecycle.observers) + } + + @Test + fun `onStart captures app opened - cold state`() { + val sut = getSut() + + val fake = createPostHogFake() + mockPackageInfo(context, "1.0.0", 1) + + sut.onStart(ProcessLifecycleOwner.get()) + + assertEquals("Application Opened", fake.event) + assertEquals("1.0.0", fake.properties?.get("version")) + assertEquals(1L, fake.properties?.get("build")) + assertEquals(false, fake.properties?.get("from_background")) + } + + @Test + fun `onStart captures app opened - warm state`() { + val sut = getSut() + + val fake = createPostHogFake() + mockPackageInfo(context, "1.0.0", 1) + + sut.onStart(ProcessLifecycleOwner.get()) + sut.onStart(ProcessLifecycleOwner.get()) + + assertEquals("Application Opened", fake.event) + assertEquals(true, fake.properties?.get("from_background")) + } + + @Test + fun `onStart captures app backgrounded`() { + val sut = getSut() + + val fake = createPostHogFake() + + sut.onStart(ProcessLifecycleOwner.get()) + sut.onStop(ProcessLifecycleOwner.get()) + + assertEquals("Application Backgrounded", fake.event) + } +}