-
-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed stack animation not updating to new child component instance wh…
…en active configuration is unchanged
- Loading branch information
Showing
9 changed files
with
389 additions
and
24 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
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
121 changes: 121 additions & 0 deletions
121
...ov/decompose/extensions/compose/experimental/stack/animation/DefaultStackAnimationTest.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,121 @@ | ||
package com.arkivanov.decompose.extensions.compose.experimental.stack.animation | ||
|
||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.test.MainTestClock | ||
import androidx.compose.ui.test.junit4.createComposeRule | ||
import com.arkivanov.decompose.Child | ||
import com.arkivanov.decompose.extensions.compose.experimental.animateFloat | ||
import com.arkivanov.decompose.extensions.compose.experimental.takeSorted | ||
import com.arkivanov.decompose.router.stack.ChildStack | ||
import org.junit.Rule | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertTrue | ||
|
||
@Suppress("TestFunctionName") | ||
class DefaultStackAnimationTest { | ||
|
||
@get:Rule | ||
val composeRule = createComposeRule() | ||
|
||
@Test | ||
fun WHEN_animating_push_and_stack_popped_during_animation_THEN_animated_push_and_pop_fully() { | ||
val anim = | ||
DefaultStackAnimation<Int, Any>( | ||
disableInputDuringAnimation = true, | ||
predictiveBackParams = { null }, | ||
selector = { _, _, _ -> null }, | ||
) | ||
|
||
var stack by mutableStateOf(stack(1)) | ||
val values1 = ArrayList<Pair<Long, Float>>() | ||
val values2 = ArrayList<Pair<Long, Float>>() | ||
|
||
composeRule.setContent { | ||
anim(stack = stack, modifier = Modifier) { | ||
val value by transition.animateFloat(durationMillis = 1000) | ||
val pair = composeRule.mainClock.currentTime to value | ||
|
||
when (it.key) { | ||
1 -> values1 += pair | ||
2 -> values2 += pair | ||
} | ||
} | ||
} | ||
|
||
stack = stack(1, 2) | ||
composeRule.mainClock.advanceFramesBy(millis = 500L) | ||
stack = stack(1) | ||
composeRule.waitForIdle() | ||
|
||
val v11 = values1.takeSorted(compareByDescending { it.second }) | ||
val v21 = values2.takeSorted(compareBy { it.second }) | ||
assertTrue(v11.size > 10) | ||
assertTrue(v21.size > 10) | ||
assertEquals(1F, v11.first().second) | ||
assertEquals(0F, v11.last().second) | ||
assertEquals(0F, v21.first().second) | ||
assertEquals(1F, v21.last().second) | ||
|
||
val v12 = values1.drop(v11.size - 1).takeSorted(compareBy { it.second }) | ||
val v22 = values2.drop(v21.size - 1).takeSorted(compareByDescending { it.second }) | ||
assertTrue(v12.size > 10) | ||
assertTrue(v22.size > 10) | ||
assertEquals(0F, v12.first().second) | ||
assertEquals(1F, v12.last().second) | ||
assertEquals(1F, v22.first().second) | ||
assertEquals(0F, v22.last().second) | ||
} | ||
|
||
@Test | ||
fun WHEN_animating_push_and_stack_popped_during_animation_THEN_first_child_restarted() { | ||
val anim = | ||
DefaultStackAnimation<Int, Any>( | ||
disableInputDuringAnimation = true, | ||
predictiveBackParams = { null }, | ||
selector = { _, _, _ -> null }, | ||
) | ||
|
||
var stack by mutableStateOf(stack(1)) | ||
var counter = 0 | ||
|
||
composeRule.setContent { | ||
anim(stack = stack, modifier = Modifier) { | ||
transition.animateFloat(durationMillis = 1000) | ||
|
||
if (it.key == 1) { | ||
LaunchedEffect(Unit) { | ||
counter++ | ||
} | ||
} | ||
} | ||
} | ||
|
||
stack = stack(1, 2) | ||
composeRule.mainClock.advanceFramesBy(millis = 500L) | ||
stack = stack(1) | ||
composeRule.waitForIdle() | ||
|
||
assertEquals(counter, 2) | ||
} | ||
|
||
private fun MainTestClock.advanceFramesBy(millis: Long) { | ||
val endTime = currentTime + millis | ||
while (currentTime < endTime) { | ||
advanceTimeByFrame() | ||
} | ||
} | ||
|
||
private fun child(config: Int): Child.Created<Int, Any> = | ||
Child.Created(configuration = config, instance = Any()) | ||
|
||
private fun stack(vararg stack: Int): ChildStack<Int, Any> = | ||
ChildStack( | ||
active = child(stack.last()), | ||
backStack = stack.dropLast(1).map(::child), | ||
) | ||
} |
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
11 changes: 11 additions & 0 deletions
11
extensions-compose/src/commonTest/kotlin/com/arkivanov/decompose/extensions/compose/Utils.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,11 @@ | ||
package com.arkivanov.decompose.extensions.compose | ||
|
||
internal fun <T> List<T>.takeSorted(comparator: Comparator<T>): List<T> = | ||
takeWhileIndexed { index, item -> | ||
(index == 0) || (comparator.compare(item, get(index - 1)) >= 0) | ||
} | ||
|
||
internal fun <T> Iterable<T>.takeWhileIndexed(predicate: (Int, T) -> Boolean): List<T> = | ||
withIndex() | ||
.takeWhile { (index, item) -> predicate(index, item) } | ||
.map { it.value } |
Oops, something went wrong.