|
| 1 | +package com.telemetrydeck.sdk |
| 2 | + |
| 3 | +import androidx.activity.ComponentActivity |
| 4 | +import androidx.lifecycle.Lifecycle |
| 5 | +import androidx.lifecycle.ProcessLifecycleOwner |
| 6 | +import androidx.test.core.app.ActivityScenario |
| 7 | +import androidx.test.ext.junit.runners.AndroidJUnit4 |
| 8 | +import androidx.test.platform.app.InstrumentationRegistry |
| 9 | +import com.telemetrydeck.sdk.params.Calendar |
| 10 | +import com.telemetrydeck.sdk.params.SDK |
| 11 | +import com.telemetrydeck.sdk.signals.Session |
| 12 | +import org.junit.After |
| 13 | +import org.junit.Assert.assertEquals |
| 14 | +import org.junit.Assert.assertNotNull |
| 15 | +import org.junit.Assert.assertTrue |
| 16 | +import org.junit.Test |
| 17 | +import org.junit.runner.RunWith |
| 18 | +import java.io.File |
| 19 | + |
| 20 | +/** |
| 21 | + * Regression test for the launch-enrichment bug where TelemetryDeck.Session.started was emitted |
| 22 | + * before enrichment providers (EnvironmentParameterProvider, CalendarParameterProvider, etc.) had |
| 23 | + * registered, resulting in a launch signal with missing device/SDK/calendar parameters and |
| 24 | + * sometimes a duplicate signal. |
| 25 | + * |
| 26 | + * The fix registers sessionManager LAST inside installProviders(), and removes the explicit |
| 27 | + * handleOnForeground() call from SessionTrackingSignalProvider.register() — the lifecycle |
| 28 | + * observer's onStart is the sole trigger. |
| 29 | + * |
| 30 | + * This test runs on a real device so ProcessLifecycleOwner transitions naturally when an Activity |
| 31 | + * is resumed, without any reflection hacks. |
| 32 | + */ |
| 33 | +@RunWith(AndroidJUnit4::class) |
| 34 | +class SessionTrackingLaunchEnrichmentTest { |
| 35 | + |
| 36 | + private var scenario: ActivityScenario<ComponentActivity>? = null |
| 37 | + private var sdk: TelemetryDeck? = null |
| 38 | + |
| 39 | + @After |
| 40 | + fun cleanup() { |
| 41 | + InstrumentationRegistry.getInstrumentation().runOnMainSync { |
| 42 | + TelemetryDeck.stop() |
| 43 | + sdk?.let { instance -> |
| 44 | + instance.broadcastTimer?.stop() |
| 45 | + for (provider in instance.providers) { |
| 46 | + provider.stop() |
| 47 | + } |
| 48 | + instance.identityProvider.stop() |
| 49 | + instance.sessionManager?.stop() |
| 50 | + } |
| 51 | + sdk = null |
| 52 | + } |
| 53 | + scenario?.close() |
| 54 | + scenario = null |
| 55 | + deleteTrackingFile() |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + fun launch_session_signal_contains_enrichment_params_and_is_not_duplicated() { |
| 60 | + deleteTrackingFile() |
| 61 | + |
| 62 | + scenario = ActivityScenario.launch(ComponentActivity::class.java) |
| 63 | + scenario!!.moveToState(Lifecycle.State.RESUMED) |
| 64 | + |
| 65 | + InstrumentationRegistry.getInstrumentation().runOnMainSync { |
| 66 | + assertTrue( |
| 67 | + "ProcessLifecycleOwner must be at least STARTED before building SDK", |
| 68 | + ProcessLifecycleOwner.get().lifecycle.currentState.isAtLeast(Lifecycle.State.STARTED) |
| 69 | + ) |
| 70 | + } |
| 71 | + |
| 72 | + val cache = MemorySignalCache() |
| 73 | + |
| 74 | + InstrumentationRegistry.getInstrumentation().runOnMainSync { |
| 75 | + val appContext = InstrumentationRegistry.getInstrumentation().targetContext.applicationContext |
| 76 | + sdk = TelemetryDeck.Builder() |
| 77 | + .appID("32CB6574-6732-4238-879F-582FEBEB6536") |
| 78 | + .sendNewSessionBeganSignal(true) |
| 79 | + .signalCache(cache) |
| 80 | + .build(appContext) |
| 81 | + } |
| 82 | + |
| 83 | + val allSignals = cache.empty() |
| 84 | + val sessionStartedSignals = allSignals.filter { it.type == Session.Started.signalName } |
| 85 | + |
| 86 | + assertEquals( |
| 87 | + "Expected exactly one ${Session.Started.signalName} signal, but got ${sessionStartedSignals.size}. All signals: ${allSignals.map { it.type }}", |
| 88 | + 1, |
| 89 | + sessionStartedSignals.size |
| 90 | + ) |
| 91 | + |
| 92 | + val launchSignal = sessionStartedSignals.first() |
| 93 | + |
| 94 | + val sdkVersionEntry = launchSignal.payload.find { it.startsWith("${SDK.Version.paramName}:") } |
| 95 | + assertNotNull( |
| 96 | + "Launch session signal must contain '${SDK.Version.paramName}:...' — enrichment providers were not registered before sessionManager", |
| 97 | + sdkVersionEntry |
| 98 | + ) |
| 99 | + |
| 100 | + val calendarEntry = launchSignal.payload.find { it.startsWith("${Calendar.DayOfWeek.paramName}:") } |
| 101 | + assertNotNull( |
| 102 | + "Launch session signal must contain '${Calendar.DayOfWeek.paramName}:...' — CalendarParameterProvider was not registered before sessionManager", |
| 103 | + calendarEntry |
| 104 | + ) |
| 105 | + } |
| 106 | + |
| 107 | + private fun deleteTrackingFile() { |
| 108 | + val appContext = InstrumentationRegistry.getInstrumentation().targetContext.applicationContext |
| 109 | + val file = File(appContext.filesDir, "telemetrydeckstracking") |
| 110 | + if (file.exists()) { |
| 111 | + file.delete() |
| 112 | + } |
| 113 | + } |
| 114 | +} |
0 commit comments