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)) }