From aa0a824118ec0c2eb1abc8c5b3a4c24d177ea517 Mon Sep 17 00:00:00 2001 From: Andras Sarro Date: Wed, 16 Oct 2024 11:01:57 +0200 Subject: [PATCH] fix(activity-lifecycle-watchdog): call lifecycle methods when first activity is available SUITEDEV-36785 Co-authored-by: davidSchuppa <32750715+davidSchuppa@users.noreply.github.com> Co-authored-by: megamegax Co-authored-by: matusekma <36794575+matusekma@users.noreply.github.com> --- .../activity/ActivityLifecycleWatchdogTest.kt | 82 ++++++++++++++++--- .../activity/ActivityLifecycleWatchdog.kt | 12 +++ 2 files changed, 81 insertions(+), 13 deletions(-) diff --git a/core/src/androidTest/java/com/emarsys/core/activity/ActivityLifecycleWatchdogTest.kt b/core/src/androidTest/java/com/emarsys/core/activity/ActivityLifecycleWatchdogTest.kt index e78abe04..3d5a29ae 100644 --- a/core/src/androidTest/java/com/emarsys/core/activity/ActivityLifecycleWatchdogTest.kt +++ b/core/src/androidTest/java/com/emarsys/core/activity/ActivityLifecycleWatchdogTest.kt @@ -1,41 +1,97 @@ package com.emarsys.core.activity import android.app.Activity +import com.emarsys.getCurrentActivity import com.emarsys.testUtil.AnnotationSpec -import org.mockito.kotlin.mock -import org.mockito.kotlin.verify +import io.mockk.coEvery +import io.mockk.every +import io.mockk.mockk +import io.mockk.mockkStatic +import io.mockk.unmockkAll +import io.mockk.verify +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.test.StandardTestDispatcher +import kotlinx.coroutines.test.resetMain +import kotlinx.coroutines.test.runTest +import kotlinx.coroutines.test.setMain +import java.util.concurrent.CountDownLatch +@OptIn(ExperimentalCoroutinesApi::class) class ActivityLifecycleWatchdogTest : AnnotationSpec() { private lateinit var watchdog: ActivityLifecycleWatchdog private lateinit var mockRegistry: ActivityLifecycleActionRegistry private lateinit var mockActivity: Activity - @Before fun setUp() { - mockRegistry = mock() - mockActivity = mock() + Dispatchers.setMain(StandardTestDispatcher()) + mockkStatic("com.emarsys.EmarsysSdkInitializerKt") + mockRegistry = mockk(relaxed = true) + mockActivity = mockk(relaxed = true) watchdog = ActivityLifecycleWatchdog(mockRegistry) } + @After + fun tearDown() { + unmockkAll() + Dispatchers.resetMain() + } + @Test fun testOnCreate_shouldInvokeRegistry_withCreateLifecycle() { watchdog.onActivityCreated(mockActivity, null) - verify(mockRegistry).execute( - mockActivity, - listOf(ActivityLifecycleAction.ActivityLifecycle.CREATE) - ) + verify { + mockRegistry.execute( + mockActivity, + listOf(ActivityLifecycleAction.ActivityLifecycle.CREATE) + ) + } } @Test fun testOnResume_shouldInvokeRegistry_withResumeLifecycle() { watchdog.onActivityResumed(mockActivity) - verify(mockRegistry).execute( - mockActivity, - listOf(ActivityLifecycleAction.ActivityLifecycle.RESUME) - ) + verify { + mockRegistry.execute( + mockActivity, + listOf(ActivityLifecycleAction.ActivityLifecycle.RESUME) + ) + } + } + + @Test + fun testInit_shouldInvokeRegistry_withCreateAndResumeLifecycle() = runTest { + val latch = CountDownLatch(2) + coEvery { getCurrentActivity() } returns mockActivity + every { mockRegistry.execute(any(), any()) } answers { + latch.countDown() + nothing + } + + ActivityLifecycleWatchdog(mockRegistry) + + latch.await() + + verify(exactly = 1) { + mockRegistry.execute( + mockActivity, + listOf( + ActivityLifecycleAction.ActivityLifecycle.CREATE + ) + ) + } + + verify(exactly = 1) { + mockRegistry.execute( + mockActivity, + listOf( + ActivityLifecycleAction.ActivityLifecycle.RESUME + ) + ) + } } } \ No newline at end of file diff --git a/core/src/main/java/com/emarsys/core/activity/ActivityLifecycleWatchdog.kt b/core/src/main/java/com/emarsys/core/activity/ActivityLifecycleWatchdog.kt index 9f53640d..de7c1f61 100644 --- a/core/src/main/java/com/emarsys/core/activity/ActivityLifecycleWatchdog.kt +++ b/core/src/main/java/com/emarsys/core/activity/ActivityLifecycleWatchdog.kt @@ -5,12 +5,24 @@ import android.app.Application.ActivityLifecycleCallbacks import android.os.Bundle import com.emarsys.core.Mockable import com.emarsys.core.activity.ActivityLifecycleAction.ActivityLifecycle +import com.emarsys.getCurrentActivity +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.SupervisorJob +import kotlinx.coroutines.launch @Mockable class ActivityLifecycleWatchdog( val activityLifecycleActionRegistry: ActivityLifecycleActionRegistry ) : ActivityLifecycleCallbacks { + init { + CoroutineScope(Dispatchers.Default + SupervisorJob()).launch { + onActivityCreated(getCurrentActivity(), null) + onActivityResumed(getCurrentActivity()) + } + } + override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) { activityLifecycleActionRegistry.execute(activity, listOf(ActivityLifecycle.CREATE)) }