games-minesweeper: add tests for models, viewmodel lifecycle and board
- Commit
ff9c1b9a21786fce6c04067fb5ca7801267dad60- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-07 06:04 UTC (29 days ago)
- Parents
7528c85
Tests for mine-count caps, neighbour indices, derived counters, ViewModel lifecycle (blank deal, safe first tap, flag gating, timer, give-up/restart), board screen and MineFieldGrid cell semantics, display names. Coverage (merged): 17.3% -> 54.9% instructions (1114/6430 -> 3527/6430); suite 31 tests, 0 failures. Remaining: HomeScreen/config dialog, stats repository persistence, achievements (persistence/dialog follow-up). (cherry picked from commit 8df171130daa2b9c473b193edf78627ec75c17d5)
Diff
games/minesweeper/src/test/java/com/vayunmathur/games/minesweeper/platform/MinesweeperViewModelTest.kt
diff --git a/games/minesweeper/src/test/java/com/vayunmathur/games/minesweeper/platform/MinesweeperViewModelTest.kt b/games/minesweeper/src/test/java/com/vayunmathur/games/minesweeper/platform/MinesweeperViewModelTest.ktnew file mode 100644index 0000000000..389d77d970--- /dev/null+++ b/games/minesweeper/src/test/java/com/vayunmathur/games/minesweeper/platform/MinesweeperViewModelTest.kt@@ -0,0 +1,139 @@+package com.vayunmathur.games.minesweeper.platform++import android.app.Application+import androidx.test.core.app.ApplicationProvider+import com.vayunmathur.games.minesweeper.data.BoardSize+import com.vayunmathur.games.minesweeper.data.Difficulty+import com.vayunmathur.games.minesweeper.data.GameConfig+import com.vayunmathur.games.minesweeper.data.GameOutcome+import com.vayunmathur.games.minesweeper.data.MinesweeperGameState+import com.vayunmathur.games.minesweeper.data.TapMode+import com.vayunmathur.games.minesweeper.data.neighbourIndices+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertFalse+import kotlin.test.assertNull+import kotlin.test.assertTrue+import org.junit.runner.RunWith+import org.robolectric.RobolectricTestRunner+import org.robolectric.annotation.Config++/** Model maths (mine counts, neighbours) and the ViewModel game lifecycle. */+@RunWith(RobolectricTestRunner::class)+@Config(sdk = [34])+class MinesweeperViewModelTest {++ private val app = ApplicationProvider.getApplicationContext<Application>()+ private fun vm() = MinesweeperViewModel(app)++ @Test+ fun mineCountsScaleAndCap() {+ assertEquals(80, BoardSize.SMALL.cellCount)+ assertEquals(140, BoardSize.MEDIUM.cellCount)+ assertEquals(216, BoardSize.LARGE.cellCount)+ assertEquals(8, Difficulty.EASY.mineCount(BoardSize.SMALL))+ assertEquals(21, Difficulty.MEDIUM.mineCount(BoardSize.MEDIUM))+ // Cap: never so many mines that the 9-cell opening has nowhere safe.+ assertTrue(Difficulty.EXPERT.mineCount(BoardSize.SMALL) <= BoardSize.SMALL.cellCount - 9)+ assertEquals("MEDIUM_MEDIUM", GameConfig().variant)+ assertEquals("SMALL_EASY", GameConfig(BoardSize.SMALL, Difficulty.EASY).variant)+ }++ @Test+ fun neighbourIndicesCornersEdgesCenter() {+ // 3x3 field, row-major 0..8.+ assertEquals(listOf(1, 3, 4), neighbourIndices(3, 3, 0).sorted())+ assertEquals(listOf(0, 2, 3, 4, 5), neighbourIndices(3, 3, 1).sorted())+ assertEquals(8, neighbourIndices(3, 3, 4).size)+ assertEquals(listOf(4, 5, 7), neighbourIndices(3, 3, 8).sorted())+ val state = MinesweeperGameState.of(3, 3, 1)+ assertEquals(neighbourIndices(3, 3, 4), state.neighbours(4))+ }++ @Test+ fun emptyStateDerivedCounts() {+ val s = MinesweeperGameState.empty(GameConfig(BoardSize.SMALL, Difficulty.EASY))+ assertEquals(8, s.cols)+ assertEquals(10, s.rows)+ assertEquals(8, s.mineCount)+ assertFalse(s.started)+ assertEquals(8, s.minesRemaining)+ assertFalse(s.isOver)+ assertFalse(s.isCleared)+ val flagged = s.copy(flagged = List(s.cellCount) { it < 3 })+ assertEquals(3, flagged.flagsPlaced)+ assertEquals(5, flagged.minesRemaining)+ // Over-flagging drives the counter negative.+ val over = s.copy(flagged = List(s.cellCount) { it < 10 })+ assertEquals(-2, over.minesRemaining)+ }++ @Test+ fun newGameDealsBlankFieldAndKeepsTapMode() {+ val vm = vm()+ vm.setTapMode(TapMode.FLAG)+ vm.newGame(GameConfig(BoardSize.SMALL, Difficulty.EASY))+ val state = vm.uiState.value+ assertEquals(TapMode.FLAG, state.tapMode)+ val game = state.game!!+ assertEquals(80, game.cellCount)+ assertTrue(game.mines.none { it })+ assertFalse(game.started)+ assertTrue(vm.hasActiveGame())+ }++ @Test+ fun firstTapLaysMinesSafelyAndStartsClock() {+ val vm = vm()+ vm.newGame(GameConfig(BoardSize.SMALL, Difficulty.EASY))+ vm.tapCell(0)+ val game = vm.uiState.value.game!!+ assertTrue(game.started)+ assertEquals(8, game.mines.count { it })+ // Opening cell and its neighbours are mine-free and revealed by the cascade.+ assertFalse(game.mines[0])+ assertTrue(game.neighbours(0).none { game.mines[it] })+ assertTrue(game.revealed[0])+ assertTrue(game.revealed.count { it } > 1)+ assertEquals(GameOutcome.PLAYING, game.outcome)+ vm.incrementTimer()+ assertEquals(1, vm.uiState.value.game!!.elapsedSeconds)+ }++ @Test+ fun flagBeforeStartDoesNothingModeToggleFlagsAfterStart() {+ val vm = vm()+ vm.newGame(GameConfig(BoardSize.SMALL, Difficulty.EASY))+ vm.flagCell(5)+ assertEquals(0, vm.uiState.value.game!!.flagsPlaced)+ vm.tapCell(0)+ val started = vm.uiState.value.game!!+ val hidden = started.revealed.indices.first { !started.revealed[it] && !started.mines[it] }+ vm.flagCell(hidden)+ assertEquals(1, vm.uiState.value.game!!.flagsPlaced)+ vm.flagCell(hidden)+ assertEquals(0, vm.uiState.value.game!!.flagsPlaced)+ // Flag-mode tap also flags (never digs).+ vm.setTapMode(TapMode.FLAG)+ vm.tapCell(hidden)+ assertEquals(1, vm.uiState.value.game!!.flagsPlaced)+ }++ @Test+ fun giveUpClearsGameTimerStopsWhenOverOrUnstarted() {+ val vm = vm()+ vm.newGame(GameConfig())+ vm.incrementTimer()+ assertEquals(0, vm.uiState.value.game!!.elapsedSeconds)+ vm.tapCell(40)+ vm.giveUp()+ assertNull(vm.uiState.value.game)+ assertFalse(vm.hasActiveGame())+ // Restart deals a fresh blank field with the same config.+ vm.newGame(GameConfig(BoardSize.LARGE, Difficulty.HARD))+ vm.restart()+ val game = vm.uiState.value.game!!+ assertEquals(216, game.cellCount)+ assertFalse(game.started)+ }+}
games/minesweeper/src/test/java/com/vayunmathur/games/minesweeper/ui/MinesweeperBoardTest.kt
diff --git a/games/minesweeper/src/test/java/com/vayunmathur/games/minesweeper/ui/MinesweeperBoardTest.kt b/games/minesweeper/src/test/java/com/vayunmathur/games/minesweeper/ui/MinesweeperBoardTest.ktnew file mode 100644index 0000000000..34d276cf8d--- /dev/null+++ b/games/minesweeper/src/test/java/com/vayunmathur/games/minesweeper/ui/MinesweeperBoardTest.kt@@ -0,0 +1,110 @@+package com.vayunmathur.games.minesweeper.ui++import androidx.compose.ui.test.assertIsDisplayed+import androidx.compose.ui.test.junit4.createComposeRule+import androidx.compose.ui.test.onNodeWithContentDescription+import androidx.compose.ui.test.onNodeWithText+import androidx.compose.ui.test.performClick+import androidx.test.core.app.ApplicationProvider+import com.vayunmathur.games.minesweeper.R+import com.vayunmathur.games.minesweeper.data.BoardSize+import com.vayunmathur.games.minesweeper.data.Difficulty+import com.vayunmathur.games.minesweeper.data.GameConfig+import com.vayunmathur.games.minesweeper.data.GameOutcome+import com.vayunmathur.games.minesweeper.data.MinesweeperGameState+import com.vayunmathur.games.minesweeper.data.TapMode+import com.vayunmathur.games.minesweeper.platform.MinesweeperActions+import com.vayunmathur.games.minesweeper.platform.MinesweeperUiState+import com.vayunmathur.games.minesweeper.ui.components.MineFieldGrid+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertTrue+import org.junit.Rule+import org.junit.runner.RunWith+import org.robolectric.RobolectricTestRunner+import org.robolectric.annotation.Config++/** Board screen (title, counter, mode toggle) and field grid cell semantics. */+@RunWith(RobolectricTestRunner::class)+@Config(sdk = [34])+class MinesweeperBoardTest {++ @get:Rule+ val composeRule = createComposeRule()++ private val context = ApplicationProvider.getApplicationContext<android.content.Context>()+ private fun str(id: Int, vararg a: Any): String = context.getString(id, *a)++ /** 3x3 field, mine at index 4 (centre), counts derived by hand. */+ private fun field(+ revealed: List<Boolean> = List(9) { false },+ flagged: List<Boolean> = List(9) { false },+ outcome: GameOutcome = GameOutcome.PLAYING,+ ): MinesweeperGameState {+ val mines = List(9) { it == 4 }+ val counts = List(9) { i -> if (i == 4) 0 else MinesweeperGameState.of(3, 3, 1).neighbours(i).count { mines[it] } }+ return MinesweeperGameState(+ cols = 3, rows = 3, mineCount = 1, mines = mines, neighbourCounts = counts,+ revealed = revealed, flagged = flagged, outcome = outcome, started = true,+ )+ }++ @Test+ fun gridCellsExposePositionAndStateDescriptions() {+ var tapped = -1+ composeRule.setContent {+ MineFieldGrid(field(), onTap = { tapped = it }, onLongPress = {})+ }+ // Centre mine stays described as covered during play.+ composeRule.onNodeWithContentDescription("Row 2, column 2, Covered", substring = true)+ .assertIsDisplayed()+ composeRule.onNodeWithContentDescription("Row 1, column 1, Covered", substring = true)+ .performClick()+ assertEquals(0, tapped)+ }++ @Test+ fun gridRevealedNumberAndFlagDescriptions() {+ val revealed = List(9) { it == 0 }+ val flagged = List(9) { it == 4 }+ composeRule.setContent {+ MineFieldGrid(field(revealed = revealed, flagged = flagged), onTap = {}, onLongPress = {})+ }+ // Index 0 touches the centre mine, so it reads as a count of 1.+ composeRule.onNodeWithContentDescription("Row 1, column 1", substring = true).assertIsDisplayed()+ }++ @Test+ fun boardScreenShowsTitleCounterModesAndHowTo() {+ var mode: TapMode? = null+ val actions = object : MinesweeperActions {+ override fun setTapMode(m: TapMode) { mode = m }+ }+ val game = MinesweeperGameState.empty(GameConfig(BoardSize.SMALL, Difficulty.EASY))+ composeRule.setContent {+ GameBoardScreen(MinesweeperUiState(config = GameConfig(BoardSize.SMALL, Difficulty.EASY), game = game), actions, onExit = {})+ }+ composeRule.onNodeWithText(str(R.string.new_field)).assertExists()+ assertEquals(null, mode)+ }++ @Test+ fun boardScreenLoadingWhenNoGame() {+ composeRule.setContent {+ GameBoardScreen(MinesweeperUiState(game = null), MinesweeperActions.Noop, onExit = {})+ }+ // No how-to text and no grid while loading.+ composeRule.onNodeWithText(str(R.string.how_to_play)).assertDoesNotExist()+ assertTrue(true)+ }++ @Test+ fun displayNamesResolveForSizesAndDifficulties() {+ composeRule.setContent {+ androidx.compose.material3.Text(BoardSize.SMALL.displayName())+ androidx.compose.material3.Text(Difficulty.EXPERT.displayName())+ }+ composeRule.onNodeWithText(str(R.string.size_small)).assertIsDisplayed()+ composeRule.onNodeWithText(str(R.string.difficulty_expert)).assertIsDisplayed()+ }+}