mirror of
https://github.com/bitwarden/android.git
synced 2026-04-29 20:38:41 -05:00
Create a common FakeLifecycle and LifecycleOwner (#1471)
This commit is contained in:
committed by
Álison Fernandes
parent
cd25e2b280
commit
a2f2216df2
@@ -1,11 +1,8 @@
|
|||||||
package com.x8bit.bitwarden.data.platform.manager
|
package com.x8bit.bitwarden.data.platform.manager
|
||||||
|
|
||||||
import androidx.lifecycle.DefaultLifecycleObserver
|
|
||||||
import androidx.lifecycle.Lifecycle
|
|
||||||
import androidx.lifecycle.LifecycleObserver
|
|
||||||
import androidx.lifecycle.LifecycleOwner
|
|
||||||
import app.cash.turbine.test
|
import app.cash.turbine.test
|
||||||
import com.x8bit.bitwarden.data.platform.manager.model.AppForegroundState
|
import com.x8bit.bitwarden.data.platform.manager.model.AppForegroundState
|
||||||
|
import com.x8bit.bitwarden.data.util.FakeLifecycleOwner
|
||||||
import kotlinx.coroutines.test.runTest
|
import kotlinx.coroutines.test.runTest
|
||||||
import org.junit.jupiter.api.Assertions.assertEquals
|
import org.junit.jupiter.api.Assertions.assertEquals
|
||||||
import org.junit.jupiter.api.Test
|
import org.junit.jupiter.api.Test
|
||||||
@@ -45,43 +42,3 @@ class AppForegroundManagerTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private class FakeLifecycle(
|
|
||||||
private val lifecycleOwner: LifecycleOwner,
|
|
||||||
) : Lifecycle() {
|
|
||||||
private val observers = mutableSetOf<DefaultLifecycleObserver>()
|
|
||||||
|
|
||||||
override var currentState: State = State.INITIALIZED
|
|
||||||
|
|
||||||
override fun addObserver(observer: LifecycleObserver) {
|
|
||||||
observers += (observer as DefaultLifecycleObserver)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun removeObserver(observer: LifecycleObserver) {
|
|
||||||
observers -= (observer as DefaultLifecycleObserver)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Triggers [DefaultLifecycleObserver.onStart] calls for each registered observer.
|
|
||||||
*/
|
|
||||||
fun dispatchOnStart() {
|
|
||||||
currentState = State.STARTED
|
|
||||||
observers.forEach { observer ->
|
|
||||||
observer.onStart(lifecycleOwner)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Triggers [DefaultLifecycleObserver.onStop] calls for each registered observer.
|
|
||||||
*/
|
|
||||||
fun dispatchOnStop() {
|
|
||||||
currentState = State.CREATED
|
|
||||||
observers.forEach { observer ->
|
|
||||||
observer.onStop(lifecycleOwner)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private class FakeLifecycleOwner : LifecycleOwner {
|
|
||||||
override val lifecycle: FakeLifecycle = FakeLifecycle(this)
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -0,0 +1,45 @@
|
|||||||
|
package com.x8bit.bitwarden.data.util
|
||||||
|
|
||||||
|
import androidx.lifecycle.DefaultLifecycleObserver
|
||||||
|
import androidx.lifecycle.Lifecycle
|
||||||
|
import androidx.lifecycle.LifecycleObserver
|
||||||
|
import androidx.lifecycle.LifecycleOwner
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A fake implementation of [LifecycleOwner] and [Lifecycle] for testing purposes.
|
||||||
|
*/
|
||||||
|
class FakeLifecycle(
|
||||||
|
private val lifecycleOwner: LifecycleOwner,
|
||||||
|
) : Lifecycle() {
|
||||||
|
private val observers = mutableSetOf<DefaultLifecycleObserver>()
|
||||||
|
|
||||||
|
override var currentState: State = State.INITIALIZED
|
||||||
|
|
||||||
|
override fun addObserver(observer: LifecycleObserver) {
|
||||||
|
observers += (observer as DefaultLifecycleObserver)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun removeObserver(observer: LifecycleObserver) {
|
||||||
|
observers -= (observer as DefaultLifecycleObserver)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Triggers [DefaultLifecycleObserver.onStart] calls for each registered observer.
|
||||||
|
*/
|
||||||
|
fun dispatchOnStart() {
|
||||||
|
currentState = State.STARTED
|
||||||
|
observers.forEach { observer ->
|
||||||
|
observer.onStart(lifecycleOwner)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Triggers [DefaultLifecycleObserver.onStop] calls for each registered observer.
|
||||||
|
*/
|
||||||
|
fun dispatchOnStop() {
|
||||||
|
currentState = State.CREATED
|
||||||
|
observers.forEach { observer ->
|
||||||
|
observer.onStop(lifecycleOwner)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package com.x8bit.bitwarden.data.util
|
||||||
|
|
||||||
|
import androidx.lifecycle.LifecycleOwner
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A fake implementation of [LifecycleOwner] for testing purposes.
|
||||||
|
*/
|
||||||
|
class FakeLifecycleOwner : LifecycleOwner {
|
||||||
|
override val lifecycle: FakeLifecycle = FakeLifecycle(lifecycleOwner = this)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user