-
Couldn't load subscription status.
- Fork 90
Added tests for binder lifecycle extensions #178
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,14 @@ | ||
| config: | ||
| warningsAsErrors: true | ||
|
|
||
| style: | ||
| ForbiddenComment: | ||
| allowedPatterns: 'https://github.com/badoo/MVICore/issues/*' | ||
| MagicNumber: | ||
| excludes: [ '**/test/**', '**/*Test.kt' ] | ||
| MaxLineLength: | ||
| excludes: [ '**/test/**', '**/*.Test.kt', '**/*.Spec.kt' ] | ||
| excludeCommentStatements: true | ||
| WildcardImport: | ||
| active: true | ||
| excludeImports: [ ] | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,243 @@ | ||
| package com.badoo.mvicore.android.lifecycle | ||
|
|
||
| import androidx.arch.core.executor.ArchTaskExecutor | ||
| import androidx.arch.core.executor.TaskExecutor | ||
| import androidx.lifecycle.Lifecycle | ||
| import io.reactivex.functions.Consumer | ||
| import io.reactivex.subjects.PublishSubject | ||
| import org.junit.After | ||
| import org.junit.Before | ||
| import org.junit.Test | ||
| import kotlin.test.assertEquals | ||
|
|
||
| class LifecycleExtensionsTest { | ||
| private val subject = PublishSubject.create<Unit>() | ||
| private val consumerTester = ConsumerTester() | ||
| private val testLifecycleOwner = TestLifecycleOwner() | ||
|
|
||
| @Before | ||
| fun setup() { | ||
| ArchTaskExecutor.getInstance() | ||
| .setDelegate(object : TaskExecutor() { | ||
| override fun executeOnDiskIO(runnable: Runnable) = runnable.run() | ||
| override fun postToMainThread(runnable: Runnable) = runnable.run() | ||
| override fun isMainThread(): Boolean = true | ||
| }) | ||
| } | ||
|
|
||
| @After | ||
| fun teardown() { | ||
| ArchTaskExecutor.getInstance().setDelegate(null) | ||
| } | ||
|
|
||
| @Test | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about converting these tests to a parametrised one? I think it would be easier to read and add new cases There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As discussed, I'm happy for you to do this in a follow up PR using junit 5 |
||
| fun `GIVEN initial lifecycle not set AND createDestroy WHEN event emitted THEN consumer not invoked`() { | ||
| testLifecycleOwner.lifecycle.createDestroy { | ||
| bind(subject to consumerTester) | ||
| } | ||
|
|
||
| subject.onNext(Unit) | ||
|
|
||
| consumerTester.verifyNotInvoked() | ||
| assertEquals(false, subject.hasObservers()) | ||
| } | ||
|
|
||
| @Test | ||
| fun `GIVEN initial lifecycle is created AND createDestroy WHEN event emitted THEN consumer invoked AND observers exist`() { | ||
| testLifecycleOwner.lifecycle.createDestroy { | ||
| bind(subject to consumerTester) | ||
| } | ||
| testLifecycleOwner.state = Lifecycle.State.CREATED | ||
|
|
||
| subject.onNext(Unit) | ||
|
|
||
| consumerTester.verifyInvoked() | ||
| assertEquals(true, subject.hasObservers()) | ||
| } | ||
|
|
||
| @Test | ||
| fun `GIVEN initial lifecycle is started AND createDestroy WHEN event emitted THEN consumer invoked AND observers exist`() { | ||
| testLifecycleOwner.lifecycle.createDestroy { | ||
| bind(subject to consumerTester) | ||
| } | ||
| testLifecycleOwner.state = Lifecycle.State.STARTED | ||
|
|
||
| subject.onNext(Unit) | ||
|
|
||
| consumerTester.verifyInvoked() | ||
| assertEquals(true, subject.hasObservers()) | ||
| } | ||
|
|
||
| @Test | ||
| fun `GIVEN initial lifecycle is resumed AND createDestroy WHEN event emitted THEN consumer invoked AND observers exist`() { | ||
| testLifecycleOwner.lifecycle.createDestroy { | ||
| bind(subject to consumerTester) | ||
| } | ||
| testLifecycleOwner.state = Lifecycle.State.RESUMED | ||
|
|
||
| subject.onNext(Unit) | ||
|
|
||
| consumerTester.verifyInvoked() | ||
| assertEquals(true, subject.hasObservers()) | ||
| } | ||
|
|
||
| @Test | ||
| fun `GIVEN initial lifecycle is created AND createDestroy AND lifecycle moved to destroyed WHEN event emitted THEN consumer not invoked AND no observers`() { | ||
| testLifecycleOwner.lifecycle.createDestroy { | ||
| bind(subject to consumerTester) | ||
| } | ||
| testLifecycleOwner.state = Lifecycle.State.CREATED | ||
| testLifecycleOwner.state = Lifecycle.State.DESTROYED | ||
|
|
||
| subject.onNext(Unit) | ||
|
|
||
| consumerTester.verifyNotInvoked() | ||
| assertEquals(false, subject.hasObservers()) | ||
| } | ||
|
|
||
| @Test | ||
| fun `GIVEN initial lifecycle not set AND startStop WHEN event emitted THEN consumer not invoked AND no observers`() { | ||
| testLifecycleOwner.lifecycle.startStop { | ||
| bind(subject to consumerTester) | ||
| } | ||
|
|
||
| subject.onNext(Unit) | ||
|
|
||
| consumerTester.verifyNotInvoked() | ||
| assertEquals(false, subject.hasObservers()) | ||
| } | ||
|
|
||
| @Test | ||
| fun `GIVEN initial lifecycle is created AND startStop WHEN event emitted THEN consumer not invoked AND no observers`() { | ||
| testLifecycleOwner.lifecycle.startStop { | ||
| bind(subject to consumerTester) | ||
| } | ||
| testLifecycleOwner.state = Lifecycle.State.CREATED | ||
|
|
||
| subject.onNext(Unit) | ||
|
|
||
| consumerTester.verifyNotInvoked() | ||
| assertEquals(false, subject.hasObservers()) | ||
| } | ||
|
|
||
| @Test | ||
| fun `GIVEN initial lifecycle is started AND startStop WHEN event emitted THEN consumer invoked AND observers exist`() { | ||
| testLifecycleOwner.lifecycle.startStop { | ||
| bind(subject to consumerTester) | ||
| } | ||
| testLifecycleOwner.state = Lifecycle.State.STARTED | ||
|
|
||
| subject.onNext(Unit) | ||
|
|
||
| consumerTester.verifyInvoked() | ||
| assertEquals(true, subject.hasObservers()) | ||
| } | ||
|
|
||
| @Test | ||
| fun `GIVEN initial lifecycle is resumed AND startStop WHEN event emitted THEN consumer invoked AND observers exist`() { | ||
| testLifecycleOwner.lifecycle.startStop { | ||
| bind(subject to consumerTester) | ||
| } | ||
| testLifecycleOwner.state = Lifecycle.State.RESUMED | ||
|
|
||
| subject.onNext(Unit) | ||
|
|
||
| consumerTester.verifyInvoked() | ||
| assertEquals(true, subject.hasObservers()) | ||
| } | ||
|
|
||
| @Test | ||
| fun `GIVEN initial lifecycle is created AND startStop AND lifecycle moved to destroyed WHEN event emitted THEN consumer not invoked AND no observers`() { | ||
| testLifecycleOwner.lifecycle.startStop { | ||
| bind(subject to consumerTester) | ||
| } | ||
| testLifecycleOwner.state = Lifecycle.State.CREATED | ||
| testLifecycleOwner.state = Lifecycle.State.DESTROYED | ||
|
|
||
| subject.onNext(Unit) | ||
|
|
||
| consumerTester.verifyNotInvoked() | ||
| assertEquals(false, subject.hasObservers()) | ||
| } | ||
|
|
||
| @Test | ||
| fun `GIVEN initial lifecycle not set AND resumePause WHEN event emitted THEN consumer not invoked AND no observers`() { | ||
| testLifecycleOwner.lifecycle.resumePause { | ||
| bind(subject to consumerTester) | ||
| } | ||
|
|
||
| subject.onNext(Unit) | ||
|
|
||
| consumerTester.verifyNotInvoked() | ||
| assertEquals(false, subject.hasObservers()) | ||
| } | ||
|
|
||
| @Test | ||
| fun `GIVEN initial lifecycle is created AND resumePause WHEN event emitted THEN consumer not invoked AND no observers`() { | ||
| testLifecycleOwner.lifecycle.resumePause { | ||
| bind(subject to consumerTester) | ||
| } | ||
| testLifecycleOwner.state = Lifecycle.State.CREATED | ||
|
|
||
| subject.onNext(Unit) | ||
|
|
||
| consumerTester.verifyNotInvoked() | ||
| assertEquals(false, subject.hasObservers()) | ||
| } | ||
|
|
||
| @Test | ||
| fun `GIVEN initial lifecycle is started AND resumePause WHEN event emitted THEN consumer not invoked AND no observers`() { | ||
| testLifecycleOwner.lifecycle.resumePause { | ||
| bind(subject to consumerTester) | ||
| } | ||
| testLifecycleOwner.state = Lifecycle.State.STARTED | ||
|
|
||
| subject.onNext(Unit) | ||
|
|
||
| consumerTester.verifyNotInvoked() | ||
| assertEquals(false, subject.hasObservers()) | ||
| } | ||
|
|
||
| @Test | ||
| fun `GIVEN initial lifecycle is resumed AND resumePause WHEN event emitted THEN consumer invoked AND observers exist`() { | ||
| testLifecycleOwner.lifecycle.resumePause { | ||
| bind(subject to consumerTester) | ||
| } | ||
| testLifecycleOwner.state = Lifecycle.State.RESUMED | ||
|
|
||
| subject.onNext(Unit) | ||
|
|
||
| consumerTester.verifyInvoked() | ||
| assertEquals(true, subject.hasObservers()) | ||
| } | ||
|
|
||
| @Test | ||
| fun `GIVEN initial lifecycle is created AND resumePause AND lifecycle moved to destroyed WHEN event emitted THEN consumer not invoked AND no observers`() { | ||
| testLifecycleOwner.lifecycle.resumePause { | ||
| bind(subject to consumerTester) | ||
| } | ||
| testLifecycleOwner.state = Lifecycle.State.CREATED | ||
| testLifecycleOwner.state = Lifecycle.State.DESTROYED | ||
|
|
||
| subject.onNext(Unit) | ||
|
|
||
| consumerTester.verifyNotInvoked() | ||
| assertEquals(false, subject.hasObservers()) | ||
| } | ||
|
|
||
| private class ConsumerTester : Consumer<Unit> { | ||
| private var wasCalled: Boolean = false | ||
|
|
||
| override fun accept(t: Unit?) { | ||
| wasCalled = true | ||
| } | ||
|
|
||
| fun verifyInvoked() { | ||
| assertEquals(true, wasCalled) | ||
| } | ||
|
|
||
| fun verifyNotInvoked() { | ||
| assertEquals(false, wasCalled) | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package com.badoo.mvicore.android.lifecycle | ||
|
|
||
| import androidx.lifecycle.Lifecycle | ||
| import androidx.lifecycle.LifecycleOwner | ||
| import androidx.lifecycle.LifecycleRegistry | ||
|
|
||
| class TestLifecycleOwner : LifecycleOwner { | ||
| private val registry = LifecycleRegistry(this) | ||
|
|
||
| var state: Lifecycle.State | ||
| get() = registry.currentState | ||
| set(value) { | ||
| registry.currentState = value | ||
| } | ||
|
|
||
| override fun getLifecycle(): Lifecycle = registry | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Copied from Appyx. Some of the test descriptions were too long, but the descriptiveness is required