test: add dice roll result dialog tests (Closes #35) (#62)

This commit was merged in pull request #62.
This commit is contained in:
2026-03-13 13:43:32 +01:00
parent 9d530ed398
commit 413dcce433
4 changed files with 152 additions and 6 deletions

View File

@@ -0,0 +1,116 @@
package org.shahondin1624
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.test.ExperimentalTestApi
import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.assertTextContains
import androidx.compose.ui.test.onAllNodesWithTag
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.compose.ui.test.runComposeUiTest
import org.shahondin1624.lib.components.TestTags
import org.shahondin1624.lib.components.charactermodel.DiceRollResultDialog
import org.shahondin1624.lib.functions.DiceRoll
import kotlin.test.Test
import kotlin.test.assertTrue
@OptIn(ExperimentalTestApi::class)
class DiceRollResultDialogTest {
/** A deterministic DiceRoll with known results for testing. */
private fun testDiceRoll() = DiceRoll(
numberOfDice = 6,
numberOfSides = 6,
numberForSuccessOrHigher = 5,
result = listOf(1, 2, 4, 5, 5, 6),
numberOfSuccesses = 3
)
@Test
fun dialogShowsDiceCountAndSuccessCount() = runComposeUiTest {
setContent {
DiceRollResultDialog(
diceRoll = testDiceRoll(),
rollLabel = "Body",
onDismiss = {}
)
}
// Verify dialog is displayed
onNodeWithTag(TestTags.DICE_ROLL_DIALOG).assertIsDisplayed()
// Verify dice count text
onNodeWithTag(TestTags.DICE_ROLL_DICE_COUNT).assertTextContains("6 dice rolled")
// Verify success count text
onNodeWithTag(TestTags.DICE_ROLL_SUCCESS_COUNT).assertTextContains("3 successes")
}
@Test
fun dialogShowsIndividualDieChips() = runComposeUiTest {
val roll = testDiceRoll()
setContent {
DiceRollResultDialog(
diceRoll = roll,
rollLabel = "Agility",
onDismiss = {}
)
}
// Verify each die chip is displayed
for (i in roll.result.indices) {
onNodeWithTag(TestTags.dieChip(i)).assertIsDisplayed()
}
}
@Test
fun dialogShowsRollLabel() = runComposeUiTest {
setContent {
DiceRollResultDialog(
diceRoll = testDiceRoll(),
rollLabel = "Body",
onDismiss = {}
)
}
// Verify the roll label appears as title
onNodeWithText("Body").assertIsDisplayed()
}
@Test
fun dialogCanBeDismissedViaOkButton() = runComposeUiTest {
var dismissed = false
setContent {
var showDialog by remember { mutableStateOf(true) }
if (showDialog) {
DiceRollResultDialog(
diceRoll = testDiceRoll(),
rollLabel = "Body",
onDismiss = {
dismissed = true
showDialog = false
}
)
}
}
// Dialog should be visible initially
onNodeWithTag(TestTags.DICE_ROLL_DIALOG).assertIsDisplayed()
// Click dismiss button
onNodeWithTag(TestTags.DICE_ROLL_DISMISS_BUTTON).performClick()
// Verify dismiss callback was triggered
assertTrue(dismissed, "onDismiss callback should have been called")
// Dialog should no longer exist
val nodes = onAllNodesWithTag(TestTags.DICE_ROLL_DIALOG).fetchSemanticsNodes()
assertTrue(nodes.isEmpty(), "Dialog should be gone after dismiss")
}
}

View File

@@ -42,6 +42,16 @@ class TestTagsTest {
assertTrue(TestTags.PANEL_DAMAGE_MONITOR.isNotBlank())
}
@Test
fun diceRollDialogTagsAreDefined() {
assertTrue(TestTags.DICE_ROLL_DIALOG.isNotBlank())
assertTrue(TestTags.DICE_ROLL_DICE_COUNT.isNotBlank())
assertTrue(TestTags.DICE_ROLL_SUCCESS_COUNT.isNotBlank())
assertTrue(TestTags.DICE_ROLL_DISMISS_BUTTON.isNotBlank())
assertEquals("die_chip_0", TestTags.dieChip(0))
assertEquals("die_chip_5", TestTags.dieChip(5))
}
@Test
fun allTagsAreUnique() {
val staticTags = listOf(
@@ -55,7 +65,11 @@ class TestTagsTest {
TestTags.NAV_PERMANENT_DRAWER,
TestTags.TOP_APP_BAR,
TestTags.THEME_TOGGLE,
TestTags.MENU_BUTTON
TestTags.MENU_BUTTON,
TestTags.DICE_ROLL_DIALOG,
TestTags.DICE_ROLL_DICE_COUNT,
TestTags.DICE_ROLL_SUCCESS_COUNT,
TestTags.DICE_ROLL_DISMISS_BUTTON
)
assertEquals(staticTags.size, staticTags.toSet().size, "All static tags should be unique")
}