Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assert Is Indeterminate for Compose Ultron #78

Merged
merged 6 commits into from
Jul 24, 2024
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
@@ -0,0 +1,41 @@
package com.atiurin.sampleapp.tests.compose

import androidx.compose.material.TriStateCheckbox
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.semantics.contentDescription
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.state.ToggleableState
import androidx.compose.ui.test.hasContentDescription
import com.atiurin.ultron.core.compose.createDefaultUltronComposeRule
import com.atiurin.ultron.core.compose.nodeinteraction.click
import com.atiurin.ultron.extensions.assertIsIndeterminate
import org.junit.Rule
import org.junit.Test

class CheckboxTest {
@get:Rule
val composeRule = createDefaultUltronComposeRule()

@Test
fun checkboxStates() {
val testTag = "checkBox"
composeRule.setContent {
val checkedState = remember { mutableStateOf(ToggleableState.Indeterminate) }
TriStateCheckbox(
state = checkedState.value,
onClick = {
if (checkedState.value == ToggleableState.Indeterminate || checkedState.value == ToggleableState.Off)
checkedState.value = ToggleableState.On
else checkedState.value = ToggleableState.Off
},
modifier = Modifier.semantics { contentDescription = testTag }
)
}

hasContentDescription(testTag).assertIsIndeterminate()
.click().assertIsOn()
.click().assertIsOff()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,15 @@ open class UltronComposeSemanticsNodeInteraction constructor(
)
}

fun assertIsIndeterminate() = apply {
executeOperation(
operationBlock = { semanticsNodeInteraction.assertIsIndeterminate() },
name = "Assert '${elementInfo.name}' is indeterminate",
type = IS_INDETERMINATE,
description = "Compose assertIsIndeterminate '${elementInfo.name}' during $timeoutMs ms",
)
}

fun assertIsOn() = apply {
executeOperation(
operationBlock = { semanticsNodeInteraction.assertIsOn() },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ enum class ComposeOperationType : UltronOperationType {
IS_SELECTED, IS_NOT_SELECTED, IS_SELECTABLE,
IS_TOGGLEABLE,
IS_FOCUSED, IS_NOT_FOCUSED,
IS_ON, IS_OFF,
IS_ON, IS_OFF, IS_INDETERMINATE,
HAS_CLICK_ACTION, HAS_NO_CLICK_ACTION,
HEIGHT_IS_AT_LEAST, HEIGHT_IS_EQUAL_TO, WIDTH_IS_AT_LEAST, WIDTH_IS_EQUAL_TO,
TEXT_EQUALS, CONTAINS_TEXT,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.atiurin.ultron.extensions

import androidx.compose.ui.test.SemanticsNodeInteraction
import androidx.compose.ui.test.assert

fun SemanticsNodeInteraction.assertIsIndeterminate(): SemanticsNodeInteraction = assert(isIndeterminate())
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.atiurin.ultron.extensions

import androidx.compose.ui.semantics.SemanticsProperties
import androidx.compose.ui.state.ToggleableState
import androidx.compose.ui.test.SemanticsMatcher

fun isIndeterminate(): SemanticsMatcher = SemanticsMatcher.expectValue(
SemanticsProperties.ToggleableState, ToggleableState.Indeterminate
)
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ fun SemanticsMatcher.assertIsNotFocused() = UltronComposeSemanticsNodeInteractio
fun SemanticsMatcher.assertIsSelected() = UltronComposeSemanticsNodeInteraction(this).assertIsSelected()
fun SemanticsMatcher.assertIsNotSelected() = UltronComposeSemanticsNodeInteraction(this).assertIsNotSelected()
fun SemanticsMatcher.assertIsSelectable() = UltronComposeSemanticsNodeInteraction(this).assertIsSelectable()
fun SemanticsMatcher.assertIsIndeterminate() = UltronComposeSemanticsNodeInteraction(this).assertIsIndeterminate()
fun SemanticsMatcher.assertIsOn() = UltronComposeSemanticsNodeInteraction(this).assertIsOn()
fun SemanticsMatcher.assertIsOff() = UltronComposeSemanticsNodeInteraction(this).assertIsOff()
fun SemanticsMatcher.assertIsToggleable() = UltronComposeSemanticsNodeInteraction(this).assertIsToggleable()
Expand Down