-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(activity-lifecycle-watchdog): call lifecycle methods when first a…
…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
1 parent
f163302
commit aa0a824
Showing
2 changed files
with
81 additions
and
13 deletions.
There are no files selected for viewing
82 changes: 69 additions & 13 deletions
82
core/src/androidTest/java/com/emarsys/core/activity/ActivityLifecycleWatchdogTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) | ||
) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters