Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,30 @@ class Trait {
@IdRes
val viewId: Int
val text: String?
val tag: String?

constructor(@IdRes viewId: Int) {
this.viewId = viewId
text = null
tag = null
}

constructor(text: String) {
viewId = -1
this.text = text
tag = null
}

constructor(@IdRes viewId: Int, text: String) {
this.viewId = viewId
this.text = text
tag = null
}

constructor(text: String, tag: String) {
this.text = text
this.tag = tag
this.viewId = -1
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package com.progressive.kherkin.compose.steps.setup

import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.hasTestTag
import androidx.compose.ui.test.hasText
import androidx.compose.ui.test.junit4.ComposeTestRule
import androidx.compose.ui.test.onAllNodesWithTag
import androidx.compose.ui.test.onAllNodesWithText
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.onNodeWithText
import com.progressive.kherkin.common.screen.IScreen
import com.progressive.kherkin.common.screen.Screen
Expand All @@ -18,24 +22,51 @@ import com.progressive.kherkin.common.testcore.IntegrationTestLogger
object TraitVerifier {

@JvmStatic
fun verifyTrait(screen: IScreen, composeTestRule: ComposeTestRule, timeoutInMillis: Long = 2000) {
fun verifyTrait(
screen: IScreen,
composeTestRule: ComposeTestRule,
timeoutInMillis: Long = 2000
) {
val trait = screen.trait
if (trait.text == null) {
val logger = IntegrationTestLogger()
logger.info("ScreenActivityName: $screen")
throw RuntimeException("Timed out waiting for activity: $screen, no trait text found.")
}
trait.text.let {
if (it == null) {
return
val tag = screen.trait.tag
val text = screen.trait.text
when {
tag != null && text != null -> {
val tag = trait.tag ?: return
val text = trait.text ?: return
composeTestRule.waitUntil(timeoutInMillis) {
composeTestRule
.onAllNodes(hasTestTag(tag).and(hasText(text)))
.fetchSemanticsNodes().size == 1
}
ComposeTestLogger().info("${::verifyTrait.name}: onNode(hasTestTag($tag).and(hasText($text))).assertIsDisplayed()")
composeTestRule.onNode(hasTestTag(tag).and(hasText(text))).assertIsDisplayed()
}

text != null -> {
composeTestRule.waitUntil(timeoutInMillis) {
composeTestRule
.onAllNodesWithText(text)
.fetchSemanticsNodes().size == 1
}
ComposeTestLogger().info("${::verifyTrait.name}: onNodeWithText($text).assertIsDisplayed()")
composeTestRule.onNodeWithText(text).assertIsDisplayed()
}
composeTestRule.waitUntil(timeoutInMillis) {
composeTestRule
.onAllNodesWithText(it)
.fetchSemanticsNodes().size == 1

tag != null -> {
composeTestRule.waitUntil(timeoutInMillis) {
composeTestRule
.onAllNodesWithTag(tag)
.fetchSemanticsNodes().size == 1
}
ComposeTestLogger().info("${::verifyTrait.name}: onNodeWithTag($tag).assertIsDisplayed()")
composeTestRule.onNodeWithTag(tag).assertIsDisplayed()
}

else -> {
IntegrationTestLogger().info("ScreenActivityName: $screen")
throw RuntimeException("Timed out waiting for activity: $screen, no traits found.")
}
ComposeTestLogger().info("${::verifyTrait.name}: onNodeWithText($it).assertIsDisplayed()")
composeTestRule.onNodeWithText(it).assertIsDisplayed()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import com.progressive.sampleapp.activities.compose.FinalComposeActivity
class FinalComposeScreen : Screen(), ComposeNavigable {

override lateinit var activityScenario: ActivityScenario<FinalComposeActivity>
override val trait: Trait = Trait("Final Compose Activity")
override val trait: Trait = Trait("Final Compose Activity", "Final Compose Title Tag")
override fun screenActivityClass(): Class<out Activity> = FinalComposeActivity::class.java

override fun startMyActivity() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ private fun SetupFinalComposeActivityContent() {
@Composable
private fun Greeting() {
Text(text = stringResource(id = R.string.final_title),
modifier = Modifier.padding(24.dp))
modifier = Modifier.padding(24.dp)
.testTag("Final Compose Title Tag"))
}

@Composable
Expand Down
Loading