From d87d985b9e1c2e508c29caa29e66b5734d734b2d Mon Sep 17 00:00:00 2001 From: Nikita Ogorodnikov Date: Fri, 25 Aug 2023 14:48:37 +0200 Subject: [PATCH] Unregister process lifecycle monitor in core instance stop --- .../com/datadog/android/core/DatadogCore.kt | 21 +++++++++++++------ .../datadog/android/core/DatadogCoreTest.kt | 19 +++++++++++++++++ 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/dd-sdk-android-core/src/main/kotlin/com/datadog/android/core/DatadogCore.kt b/dd-sdk-android-core/src/main/kotlin/com/datadog/android/core/DatadogCore.kt index fef1d7da5c..8b475c569d 100644 --- a/dd-sdk-android-core/src/main/kotlin/com/datadog/android/core/DatadogCore.kt +++ b/dd-sdk-android-core/src/main/kotlin/com/datadog/android/core/DatadogCore.kt @@ -87,6 +87,8 @@ internal class DatadogCore( ) } + private var processLifecycleMonitor: ProcessLifecycleMonitor? = null + // region SdkCore /** @inheritDoc */ @@ -358,12 +360,15 @@ internal class DatadogCore( private fun setupLifecycleMonitorCallback(appContext: Context) { if (appContext is Application) { - val callback = ProcessLifecycleCallback( - coreFeature.networkInfoProvider, - appContext, - internalLogger - ) - appContext.registerActivityLifecycleCallbacks(ProcessLifecycleMonitor(callback)) + processLifecycleMonitor = ProcessLifecycleMonitor( + ProcessLifecycleCallback( + coreFeature.networkInfoProvider, + appContext, + internalLogger + ) + ).apply { + appContext.registerActivityLifecycleCallbacks(this) + } } } @@ -444,6 +449,10 @@ internal class DatadogCore( } features.clear() + if (context is Application && processLifecycleMonitor != null) { + context.unregisterActivityLifecycleCallbacks(processLifecycleMonitor) + } + coreFeature.stop() } diff --git a/dd-sdk-android-core/src/test/kotlin/com/datadog/android/core/DatadogCoreTest.kt b/dd-sdk-android-core/src/test/kotlin/com/datadog/android/core/DatadogCoreTest.kt index 133d42425b..5be6efedb6 100644 --- a/dd-sdk-android-core/src/test/kotlin/com/datadog/android/core/DatadogCoreTest.kt +++ b/dd-sdk-android-core/src/test/kotlin/com/datadog/android/core/DatadogCoreTest.kt @@ -17,6 +17,7 @@ import com.datadog.android.core.configuration.Configuration import com.datadog.android.core.internal.ContextProvider import com.datadog.android.core.internal.CoreFeature import com.datadog.android.core.internal.SdkFeature +import com.datadog.android.core.internal.lifecycle.ProcessLifecycleMonitor import com.datadog.android.core.internal.net.DefaultFirstPartyHostHeaderTypeResolver import com.datadog.android.core.internal.net.info.NetworkInfoProvider import com.datadog.android.core.internal.privacy.ConsentProvider @@ -55,10 +56,12 @@ import org.mockito.Mock import org.mockito.junit.jupiter.MockitoExtension import org.mockito.junit.jupiter.MockitoSettings import org.mockito.kotlin.any +import org.mockito.kotlin.argumentCaptor import org.mockito.kotlin.doAnswer import org.mockito.kotlin.doReturn import org.mockito.kotlin.mock import org.mockito.kotlin.reset +import org.mockito.kotlin.times import org.mockito.kotlin.verify import org.mockito.kotlin.verifyNoInteractions import org.mockito.kotlin.whenever @@ -648,6 +651,22 @@ internal class DatadogCoreTest { assertThat(testedCore.features).isEmpty() } + @Test + fun `𝕄 unregister process lifecycle monitor 𝕎 stop()`() { + // Given + val expectedInvocations = if (fakeConfiguration.crashReportsEnabled) 2 else 1 + + // When + testedCore.stop() + + // Then + argumentCaptor { + verify(appContext.mockInstance, times(expectedInvocations)) + .unregisterActivityLifecycleCallbacks(capture()) + assertThat(lastValue).isInstanceOf(ProcessLifecycleMonitor::class.java) + } + } + companion object { val appContext = ApplicationContextTestConfiguration(Application::class.java)