Skip to content

Instantly share code, notes, and snippets.

@dladukedev
Created August 26, 2023 03:18
Show Gist options
  • Save dladukedev/8f546b6725bf37d91ec54f92b088fdb2 to your computer and use it in GitHub Desktop.
Save dladukedev/8f546b6725bf37d91ec54f92b088fdb2 to your computer and use it in GitHub Desktop.
Show how to use the different modifying functions for SemanticMatchers
package com.dladukedev.semanticsmatchertest
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.material3.Text
import androidx.compose.ui.test.assertCountEquals
import androidx.compose.ui.test.hasAnyAncestor
import androidx.compose.ui.test.hasText
import androidx.compose.ui.test.isDialog
import androidx.compose.ui.test.isRoot
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.window.Dialog
import org.junit.Test
import org.junit.Before
import org.junit.Rule
class SemanticMatchersDemo {
@get:Rule
val composeTestRule = createComposeRule()
@Before
fun before() {
composeTestRule.setContent {
Box {
Column {
Text("Hello")
Text("Good-bye")
}
Dialog(onDismissRequest = {}) {
Column {
Text("Hello")
Text("Good-bye")
}
}
}
}
}
@Test
fun andTest() {
composeTestRule.onAllNodes(hasAnyAncestor(isDialog()) and hasText("Hello")).assertCountEquals(1)
}
@Test
fun orTest() {
composeTestRule.onAllNodes(hasText("Hello") or hasText("Good-bye")).assertCountEquals(4)
}
@Test
fun notTest() {
composeTestRule.onAllNodes(!isRoot()).assertCountEquals(5)
composeTestRule.onAllNodes(isRoot().not()).assertCountEquals(5)
}
@Test
fun combineTest() {
composeTestRule.onAllNodes(hasAnyAncestor(isDialog()).not() and hasText("Hello")).assertCountEquals(1)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment