Skip to content

Commit

Permalink
fix(activity-lifecycle-watchdog): call lifecycle methods when first a…
Browse files Browse the repository at this point in the history
…ctivity is available

SUITEDEV-36785

Co-authored-by: davidSchuppa <[email protected]>
Co-authored-by: megamegax <[email protected]>
Co-authored-by: matusekma <[email protected]>
  • Loading branch information
4 people committed Oct 16, 2024
1 parent f163302 commit aa0a824
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -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
)
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down

0 comments on commit aa0a824

Please sign in to comment.