From 1858fcd2da401c3f5c40ceb2ce73c787ebbc0101 Mon Sep 17 00:00:00 2001 From: thiago Date: Thu, 28 Nov 2024 07:01:52 -0300 Subject: [PATCH] chore: add unit tests and fix Processor classes modules --- .../com/posthog/android/PostHogAndroid.kt | 2 +- .../com/posthog}/PostHogScreenProcessor.kt | 6 +- .../main/java/com/posthog}/ScreenTracker.kt | 2 +- .../src/test/java/com/posthog/PostHogTest.kt | 99 +++++++++++-------- 4 files changed, 64 insertions(+), 45 deletions(-) rename {posthog-android/src/main/java/com/posthog/android/internal => posthog/src/main/java/com/posthog}/PostHogScreenProcessor.kt (68%) rename {posthog-android/src/main/java/com/posthog/android/internal => posthog/src/main/java/com/posthog}/ScreenTracker.kt (87%) diff --git a/posthog-android/src/main/java/com/posthog/android/PostHogAndroid.kt b/posthog-android/src/main/java/com/posthog/android/PostHogAndroid.kt index 2c08f2a3..c1e64711 100644 --- a/posthog-android/src/main/java/com/posthog/android/PostHogAndroid.kt +++ b/posthog-android/src/main/java/com/posthog/android/PostHogAndroid.kt @@ -4,6 +4,7 @@ import android.app.Application import android.content.Context import com.posthog.PostHog import com.posthog.PostHogInterface +import com.posthog.PostHogScreenProcessor import com.posthog.android.internal.MainHandler import com.posthog.android.internal.PostHogActivityLifecycleCallbackIntegration import com.posthog.android.internal.PostHogAndroidContext @@ -11,7 +12,6 @@ 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.PostHogScreenProcessor import com.posthog.android.internal.PostHogSharedPreferences import com.posthog.android.internal.appContext import com.posthog.android.replay.PostHogReplayIntegration diff --git a/posthog-android/src/main/java/com/posthog/android/internal/PostHogScreenProcessor.kt b/posthog/src/main/java/com/posthog/PostHogScreenProcessor.kt similarity index 68% rename from posthog-android/src/main/java/com/posthog/android/internal/PostHogScreenProcessor.kt rename to posthog/src/main/java/com/posthog/PostHogScreenProcessor.kt index 2bca34fc..4b07d30b 100644 --- a/posthog-android/src/main/java/com/posthog/android/internal/PostHogScreenProcessor.kt +++ b/posthog/src/main/java/com/posthog/PostHogScreenProcessor.kt @@ -1,8 +1,6 @@ -package com.posthog.android.internal +package com.posthog -import com.posthog.PostHogPropertiesProcessor - -internal class PostHogScreenProcessor : PostHogPropertiesProcessor { +public class PostHogScreenProcessor : PostHogPropertiesProcessor { override fun process(properties: MutableMap): Map { if (properties.containsKey("\$screen_name")) { return properties diff --git a/posthog-android/src/main/java/com/posthog/android/internal/ScreenTracker.kt b/posthog/src/main/java/com/posthog/ScreenTracker.kt similarity index 87% rename from posthog-android/src/main/java/com/posthog/android/internal/ScreenTracker.kt rename to posthog/src/main/java/com/posthog/ScreenTracker.kt index 41ba9f76..1ae4e8d8 100644 --- a/posthog-android/src/main/java/com/posthog/android/internal/ScreenTracker.kt +++ b/posthog/src/main/java/com/posthog/ScreenTracker.kt @@ -1,4 +1,4 @@ -package com.posthog.android.internal +package com.posthog public object ScreenTracker { @Volatile private lateinit var currentScreen: String diff --git a/posthog/src/test/java/com/posthog/PostHogTest.kt b/posthog/src/test/java/com/posthog/PostHogTest.kt index ef4d9c3e..2e65b80c 100644 --- a/posthog/src/test/java/com/posthog/PostHogTest.kt +++ b/posthog/src/test/java/com/posthog/PostHogTest.kt @@ -45,6 +45,7 @@ internal class PostHogTest { reloadFeatureFlags: Boolean = true, sendFeatureFlagEvent: Boolean = true, integration: PostHogIntegration? = null, + processor: PostHogPropertiesProcessor? = null, cachePreferences: PostHogMemoryPreferences = PostHogMemoryPreferences(), propertiesSanitizer: PostHogPropertiesSanitizer? = null, ): PostHogInterface { @@ -58,6 +59,9 @@ internal class PostHogTest { if (integration != null) { addIntegration(integration) } + if (processor != null) { + addProcessor(processor) + } this.sendFeatureFlagEvent = sendFeatureFlagEvent this.cachePreferences = cachePreferences this.propertiesSanitizer = propertiesSanitizer @@ -1044,45 +1048,6 @@ internal class PostHogTest { sut.close() } - @Test - fun `captures screen event and alias event with screen_name`() { - val http = mockHttp() - val url = http.url("/") - - val sut = getSut(url.toString(), preloadFeatureFlags = false) - - val screenName = "HomeScreen" - - val alias = "UserAlias" - sut.screen(screenName) - - sut.alias(alias) - - queueExecutor.shutdownAndAwaitTermination() - - var request = http.takeRequest() - - assertEquals(2, http.requestCount) - - var content = request.body.unGzip() - var batch = serializer.deserialize(content.reader()) - - var theEvent = batch.batch.first() - - assertEquals(screenName, theEvent.properties!!["\$screen_name"]) - - request = http.takeRequest() - - assertEquals(2, http.requestCount) - - content = request.body.unGzip() - batch = serializer.deserialize(content.reader()) - - theEvent = batch.batch.first() - assertEquals(alias, theEvent.properties!!["alias"]) - assertEquals(screenName, theEvent.properties!!["\$screen_name"]) - sut.close() - } @Test fun `reset session id when reset is called`() { @@ -1168,4 +1133,60 @@ internal class PostHogTest { sut.close() } + + @Test + fun `processor does not override existing screen_name in event properties`() { + val http = mockHttp() + val url = http.url("/") + + val sut = getSut(url.toString(), preloadFeatureFlags = false, reloadFeatureFlags = false, processor = PostHogScreenProcessor()) + + ScreenTracker.setCurrentScreen("CurrentScreen") + + sut.capture( + "Test Event", + properties = mapOf( + "test_prop" to "test_value", + "\$screen_name" to "ProvidedScreen" + ) + ) + + queueExecutor.shutdownAndAwaitTermination() + + val request = http.takeRequest() + + val content = request.body.unGzip() + val batch = serializer.deserialize(content.reader()) + + val theEvent = batch.batch.first() + + assertEquals("ProvidedScreen", theEvent.properties?.get("\$screen_name")) + + sut.close() + } + + @Test + fun `processor adds screen_name to event properties`() { + val http = mockHttp() + val url = http.url("/") + + val sut = getSut(url.toString(), preloadFeatureFlags = false, reloadFeatureFlags = false, processor = PostHogScreenProcessor()) + + ScreenTracker.setCurrentScreen("TestScreen") + + sut.capture("Test Event", properties = mapOf("test_prop" to "test_value")) + + queueExecutor.shutdownAndAwaitTermination() + + val request = http.takeRequest() + + val content = request.body.unGzip() + val batch = serializer.deserialize(content.reader()) + + val theEvent = batch.batch.first() + + assertEquals("TestScreen", theEvent.properties?.get("\$screen_name")) + + sut.close() + } }