-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix PermanentChild behaviour in test environment
- Loading branch information
1 parent
10695e5
commit d555cb2
Showing
8 changed files
with
148 additions
and
25 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
77 changes: 77 additions & 0 deletions
77
libraries/core/src/androidTest/kotlin/com/bumble/appyx/core/node/PermanentChildTest.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,77 @@ | ||
package com.bumble.appyx.core.node | ||
|
||
import android.os.Parcelable | ||
import androidx.compose.foundation.text.BasicText | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.platform.testTag | ||
import androidx.compose.ui.test.hasTestTag | ||
import com.bumble.appyx.core.AppyxTestScenario | ||
import com.bumble.appyx.core.children.nodeOrNull | ||
import com.bumble.appyx.core.modality.BuildContext | ||
import com.bumble.appyx.core.navigation.EmptyNavModel | ||
import kotlinx.parcelize.Parcelize | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Rule | ||
import org.junit.Test | ||
|
||
class PermanentChildTest { | ||
|
||
@get:Rule | ||
val rule = AppyxTestScenario { buildContext -> | ||
TestParentNode(buildContext) | ||
} | ||
|
||
@Test | ||
fun permanent_child_is_rendered() { | ||
rule.start() | ||
|
||
rule.onNode(hasTestTag(TestParentNode.NavTarget::class.java.name)).assertExists() | ||
} | ||
|
||
@Test | ||
fun permanent_child_is_reused_when_visibility_switched() { | ||
rule.start() | ||
rule.node.renderPermanentChild = false | ||
val childNodes = rule.node.children.value.values.map { it.nodeOrNull } | ||
|
||
rule.onNode(hasTestTag(TestParentNode.NavTarget::class.java.name)).assertDoesNotExist() | ||
|
||
rule.node.renderPermanentChild = true | ||
|
||
rule.onNode(hasTestTag(TestParentNode.NavTarget::class.java.name)).assertExists() | ||
assertEquals(childNodes, rule.node.children.value.values.map { it.nodeOrNull }) | ||
} | ||
|
||
class TestParentNode( | ||
buildContext: BuildContext, | ||
) : ParentNode<TestParentNode.NavTarget>( | ||
buildContext = buildContext, | ||
navModel = EmptyNavModel<NavTarget, Any>(), | ||
) { | ||
|
||
@Parcelize | ||
object NavTarget : Parcelable | ||
|
||
var renderPermanentChild by mutableStateOf(true) | ||
|
||
override fun resolve(navTarget: NavTarget, buildContext: BuildContext): Node = | ||
node(buildContext) { modifier -> | ||
BasicText( | ||
text = navTarget.toString(), | ||
modifier = modifier.testTag(NavTarget::class.java.name), | ||
) | ||
} | ||
|
||
@Composable | ||
override fun View(modifier: Modifier) { | ||
if (renderPermanentChild) { | ||
PermanentChild(NavTarget) | ||
} | ||
} | ||
} | ||
|
||
} |
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
35 changes: 35 additions & 0 deletions
35
...e/src/main/kotlin/com/bumble/appyx/core/navigation/model/permanent/operation/AddUnique.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,35 @@ | ||
package com.bumble.appyx.core.navigation.model.permanent.operation | ||
|
||
import com.bumble.appyx.core.navigation.NavElement | ||
import com.bumble.appyx.core.navigation.NavElements | ||
import com.bumble.appyx.core.navigation.NavKey | ||
import com.bumble.appyx.core.navigation.model.permanent.PermanentNavModel | ||
import kotlinx.parcelize.Parcelize | ||
import kotlinx.parcelize.RawValue | ||
|
||
@Parcelize | ||
data class AddUnique<NavTarget : Any>( | ||
private val navTarget: @RawValue NavTarget, | ||
) : PermanentOperation<NavTarget> { | ||
|
||
override fun isApplicable(elements: NavElements<NavTarget, Int>): Boolean = | ||
!elements.any { it.key == navTarget } | ||
|
||
override fun invoke( | ||
elements: NavElements<NavTarget, Int> | ||
): NavElements<NavTarget, Int> = | ||
if (elements.any { it.key.navTarget == navTarget }) { | ||
elements | ||
} else { | ||
elements + NavElement( | ||
key = NavKey(navTarget), | ||
fromState = 0, | ||
targetState = 0, | ||
operation = this, | ||
) | ||
} | ||
} | ||
|
||
fun <NavTarget : Any> PermanentNavModel<NavTarget>.addUnique(navTarget: NavTarget) { | ||
accept(AddUnique(navTarget)) | ||
} |
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