-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #163 from nhaarman/listenerinvocationsordering
Invoke Scene.onStart after listener notification
- Loading branch information
Showing
8 changed files
with
379 additions
and
13 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
plugins { | ||
id("org.jetbrains.kotlin.jvm") | ||
id("org.gradle.java-library") | ||
} | ||
|
||
dependencies { | ||
implementation "org.jetbrains.kotlin:kotlin-stdlib" | ||
|
||
testImplementation project(":ext-acorn-testing") | ||
testImplementation "com.nhaarman:expect.kt" | ||
testImplementation "com.nhaarman.mockitokotlin2:mockito-kotlin" | ||
testImplementation "org.junit.jupiter:junit-jupiter-api" | ||
testRuntime "org.junit.jupiter:junit-jupiter-engine" | ||
} |
94 changes: 94 additions & 0 deletions
94
...ontests/ReplacingNavigator tests for scenes that cause a transition in their onStart().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 |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* Copyright 2018 Niek Haarman | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.nhaarman.acorn.integrationtests | ||
|
||
import com.nhaarman.acorn.navigation.Navigator | ||
import com.nhaarman.acorn.navigation.ReplacingNavigator | ||
import com.nhaarman.acorn.presentation.Container | ||
import com.nhaarman.acorn.presentation.Scene | ||
import com.nhaarman.acorn.state.SceneState | ||
import com.nhaarman.acorn.testing.ContainerProvider | ||
import com.nhaarman.acorn.testing.TestContext | ||
import com.nhaarman.acorn.testing.TestContext.Companion.testWith | ||
import com.nhaarman.mockitokotlin2.any | ||
import com.nhaarman.mockitokotlin2.anyOrNull | ||
import com.nhaarman.mockitokotlin2.inOrder | ||
import com.nhaarman.mockitokotlin2.mock | ||
import kotlin.reflect.KClass | ||
import org.junit.jupiter.api.Test | ||
|
||
/** | ||
* When a Scene (A) _immediately_ causes another transition to | ||
* another Scene (B) in its `onStart` method, a wrong order of | ||
* scene notifications can occur if the listener invocation happens | ||
* after starting the scene: First B is reported and only then A. | ||
* | ||
* These tests form an integration test to ensure proper behavior. | ||
*/ | ||
class `ReplacingNavigator tests for scenes that cause a transition in their onStart()` { | ||
|
||
private val navigator = MyNavigator() | ||
private val navigatorListener = mock<Navigator.Events>() | ||
|
||
private val immediatelyFinishingScene = ImmediatelyFinishingScene { navigator.finish() } | ||
|
||
private val context = TestContext.create( | ||
navigator, | ||
object : ContainerProvider { | ||
override fun containerFor(scene: Scene<*>): Container { | ||
return mock() | ||
} | ||
} | ||
) | ||
|
||
@Test | ||
fun `replacing an immediately finishing scene results in proper navigator notifications`() = testWith(context) { | ||
/* Given */ | ||
navigator.addNavigatorEventsListener(navigatorListener) | ||
|
||
/* When */ | ||
navigator.replace(immediatelyFinishingScene) | ||
|
||
/* Then */ | ||
inOrder(navigatorListener) { | ||
verify(navigatorListener).scene(any<InitialScene>(), anyOrNull()) | ||
verify(navigatorListener).scene(any<ImmediatelyFinishingScene>(), anyOrNull()) | ||
verify(navigatorListener).finished() | ||
verifyNoMoreInteractions() | ||
} | ||
} | ||
|
||
private class InitialScene : Scene<Container> | ||
|
||
private class ImmediatelyFinishingScene(private val onFinish: () -> Unit) : Scene<Container> { | ||
|
||
override fun onStart() { | ||
onFinish() | ||
} | ||
} | ||
|
||
private class MyNavigator : ReplacingNavigator(null) { | ||
|
||
override fun initialScene(): Scene<out Container> { | ||
return InitialScene() | ||
} | ||
|
||
override fun instantiateScene(sceneClass: KClass<out Scene<*>>, state: SceneState?): Scene<out Container> { | ||
error("Not used") | ||
} | ||
} | ||
} |
111 changes: 111 additions & 0 deletions
111
...rationtests/StackNavigator tests for scenes that cause a transition in their onStart().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 |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* | ||
* Copyright 2018 Niek Haarman | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.nhaarman.acorn.integrationtests | ||
|
||
import com.nhaarman.acorn.navigation.Navigator | ||
import com.nhaarman.acorn.navigation.StackNavigator | ||
import com.nhaarman.acorn.presentation.Container | ||
import com.nhaarman.acorn.presentation.Scene | ||
import com.nhaarman.acorn.state.SceneState | ||
import com.nhaarman.acorn.testing.ContainerProvider | ||
import com.nhaarman.acorn.testing.TestContext | ||
import com.nhaarman.acorn.testing.TestContext.Companion.testWith | ||
import com.nhaarman.mockitokotlin2.any | ||
import com.nhaarman.mockitokotlin2.anyOrNull | ||
import com.nhaarman.mockitokotlin2.inOrder | ||
import com.nhaarman.mockitokotlin2.mock | ||
import kotlin.reflect.KClass | ||
import org.junit.jupiter.api.Test | ||
|
||
/** | ||
* When a Scene (A) _immediately_ causes another transition to | ||
* another Scene (B) in its `onStart` method, a wrong order of | ||
* scene notifications can occur if the listener invocation happens | ||
* after starting the scene: First B is reported and only then A. | ||
* | ||
* These tests form an integration test to ensure proper behavior. | ||
*/ | ||
class `StackNavigator tests for scenes that cause a transition in their onStart()` { | ||
|
||
private val navigator = MyNavigator() | ||
private val navigatorListener = mock<Navigator.Events>() | ||
|
||
private val immediatelyFinishingScene = ImmediatelyFinishingScene { navigator.pop() } | ||
|
||
private val context = TestContext.create( | ||
navigator, | ||
object : ContainerProvider { | ||
override fun containerFor(scene: Scene<*>): Container { | ||
return mock() | ||
} | ||
} | ||
) | ||
|
||
@Test | ||
fun `pushing an immediately finishing scene results in proper navigator notifications`() = testWith(context) { | ||
/* Given */ | ||
navigator.addNavigatorEventsListener(navigatorListener) | ||
|
||
/* When */ | ||
navigator.push(immediatelyFinishingScene) | ||
|
||
/* Then */ | ||
inOrder(navigatorListener) { | ||
verify(navigatorListener).scene(any<InitialScene>(), anyOrNull()) | ||
verify(navigatorListener).scene(any<ImmediatelyFinishingScene>(), anyOrNull()) | ||
verify(navigatorListener).scene(any<InitialScene>(), anyOrNull()) | ||
verifyNoMoreInteractions() | ||
} | ||
} | ||
|
||
@Test | ||
fun `replacing an immediately finishing scene results in proper navigator notifications`() = testWith(context) { | ||
/* Given */ | ||
navigator.addNavigatorEventsListener(navigatorListener) | ||
|
||
/* When */ | ||
navigator.replace(immediatelyFinishingScene) | ||
|
||
/* Then */ | ||
inOrder(navigatorListener) { | ||
verify(navigatorListener).scene(any<InitialScene>(), anyOrNull()) | ||
verify(navigatorListener).scene(any<ImmediatelyFinishingScene>(), anyOrNull()) | ||
verify(navigatorListener).finished() | ||
verifyNoMoreInteractions() | ||
} | ||
} | ||
|
||
private class InitialScene : Scene<Container> | ||
|
||
private class ImmediatelyFinishingScene(private val onFinish: () -> Unit) : Scene<Container> { | ||
|
||
override fun onStart() { | ||
onFinish() | ||
} | ||
} | ||
|
||
private class MyNavigator : StackNavigator(null) { | ||
|
||
override fun initialStack(): List<Scene<out Container>> { | ||
return listOf(InitialScene()) | ||
} | ||
|
||
override fun instantiateScene(sceneClass: KClass<out Scene<*>>, state: SceneState?): Scene<out Container> { | ||
error("Not used") | ||
} | ||
} | ||
} |
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
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
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
Oops, something went wrong.