games/nonogram: add tests for datastore, viewmodel and UI
- Commit
cf7e3f8963435c53d67e41d67ec5665058563450- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-07 05:47 UTC (29 days ago)
- Parents
da651dd
Coverage 11.9% -> 88.6% (merged, instructions 7263/8197). New Robolectric/Compose tests: NonogramDataStoreTest (mode round-trips, saveLevel wipe, reveal-once heart charge, daily rollover, clearMarks independence), NonogramViewModelTest (tap/cross/long-press judging, hearts, nextLevel, daily mode, casual+daily win recording, reminders, contract defaults), NonogramScreensTest + NonogramExpandedScreenTest (loading, generation-failed retry, won/failed/daily states, restart confirmation, board cell descriptions/tap/long-press, empty-clue and 15x15 boards, settings) and MainActivityTest (launch, Route serialization). No production changes; no bugs found. (cherry picked from commit 5c72127a295a0b4a8518808c24ef70ea37047337)
Diff
games/nonogram/src/test/java/com/vayunmathur/games/nonogram/MainActivityTest.kt
diff --git a/games/nonogram/src/test/java/com/vayunmathur/games/nonogram/MainActivityTest.kt b/games/nonogram/src/test/java/com/vayunmathur/games/nonogram/MainActivityTest.ktnew file mode 100644index 0000000000..08b29f10cf--- /dev/null+++ b/games/nonogram/src/test/java/com/vayunmathur/games/nonogram/MainActivityTest.kt@@ -0,0 +1,33 @@+package com.vayunmathur.games.nonogram++import org.junit.runner.RunWith+import org.robolectric.Robolectric+import org.robolectric.RobolectricTestRunner+import org.robolectric.annotation.Config+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertNotNull+import kotlin.test.assertTrue++@RunWith(RobolectricTestRunner::class)+class MainActivityTest {+ @Test fun routesSerializeRoundTrip() {+ val json = kotlinx.serialization.json.Json+ for (route in listOf(Route.Game, Route.GameCenter, Route.Settings)) {+ val text = json.encodeToString(Route.serializer(), route)+ assertEquals(route, json.decodeFromString(Route.serializer(), text))+ }+ }++ @Test fun routesAreDistinctObjects() {+ assertEquals(Route.Game, Route.Game)+ assertTrue(Route.Game != Route.Settings)+ assertTrue(Route.GameCenter != Route.Settings)+ }++ @Test @Config(sdk = [34]) fun activityLaunchesAndShowsContent() {+ val controller = Robolectric.buildActivity(MainActivity::class.java).setup()+ val activity = controller.get()+ assertNotNull(activity)+ }+}
games/nonogram/src/test/java/com/vayunmathur/games/nonogram/data/NonogramDataStoreTest.kt
diff --git a/games/nonogram/src/test/java/com/vayunmathur/games/nonogram/data/NonogramDataStoreTest.kt b/games/nonogram/src/test/java/com/vayunmathur/games/nonogram/data/NonogramDataStoreTest.ktnew file mode 100644index 0000000000..4528844ee8--- /dev/null+++ b/games/nonogram/src/test/java/com/vayunmathur/games/nonogram/data/NonogramDataStoreTest.kt@@ -0,0 +1,99 @@+package com.vayunmathur.games.nonogram.data++import androidx.test.core.app.ApplicationProvider+import android.content.Context+import kotlinx.coroutines.flow.first+import kotlinx.coroutines.runBlocking+import org.junit.runner.RunWith+import org.robolectric.RobolectricTestRunner+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertTrue++@RunWith(RobolectricTestRunner::class)+class NonogramDataStoreTest {+ private val ctx get() = ApplicationProvider.getApplicationContext<Context>()+ private fun store() = NonogramDataStore(ctx)++ @Test fun defaultsAndModeRoundTrips() = runBlocking {+ val s = store()+ s.saveLevel(1)+ assertEquals(1, s.currentLevel.first())+ assertEquals(STARTING_HEARTS, s.hearts.first())+ assertEquals(emptySet(), s.filledCells.first())+ assertEquals(GameMode.CASUAL, s.gameMode.first())+ assertEquals(MarkMode.FILL, s.markMode.first())+ s.setGameMode(GameMode.DAILY); assertEquals(GameMode.DAILY, s.gameMode.first())+ s.setGameMode(GameMode.CASUAL)+ s.setMarkMode(MarkMode.CROSS); assertEquals(MarkMode.CROSS, s.markMode.first())+ s.setMarkMode(MarkMode.FILL)+ }++ @Test fun saveLevelWipesBoardAndRestoresHearts() = runBlocking {+ val s = store()+ s.fillCell(3, daily = false); s.setNote(4, true, daily = false); s.revealBlank(5, daily = false)+ assertEquals(STARTING_HEARTS - 1, s.hearts.first())+ s.saveLevel(7)+ assertEquals(7, s.currentLevel.first())+ assertEquals(emptySet(), s.filledCells.first())+ assertEquals(emptySet(), s.crossedCells.first())+ assertEquals(emptySet(), s.revealedBlanks.first())+ assertEquals(STARTING_HEARTS, s.hearts.first())+ }++ @Test fun fillRemovesNoteAndNotesToggle() = runBlocking {+ val s = store(); s.saveLevel(2)+ s.setNote(10, true, daily = false); assertEquals(setOf(10), s.crossedCells.first())+ s.fillCell(10, daily = false)+ assertEquals(setOf(10), s.filledCells.first()); assertEquals(emptySet(), s.crossedCells.first())+ s.setNote(11, true, daily = false); s.setNote(11, false, daily = false)+ assertEquals(emptySet(), s.crossedCells.first())+ }++ @Test fun revealBlankChargesOnceAndClearsNote() = runBlocking {+ val s = store(); s.saveLevel(3)+ s.setNote(20, true, daily = false)+ s.revealBlank(20, daily = false)+ assertEquals(setOf(20), s.revealedBlanks.first()); assertEquals(emptySet(), s.crossedCells.first())+ assertEquals(STARTING_HEARTS - 1, s.hearts.first())+ s.revealBlank(20, daily = false)+ assertEquals(STARTING_HEARTS - 1, s.hearts.first(), "second reveal must not charge again")+ s.revealBlank(21, daily = false); s.revealBlank(22, daily = false)+ assertEquals(0, s.hearts.first())+ s.revealBlank(23, daily = false); assertEquals(0, s.hearts.first(), "hearts floor at zero")+ }++ @Test fun clearMarksCasualAndDailyAreIndependent() = runBlocking {+ val s = store(); s.saveLevel(4); s.ensureDailyDay(19_000L)+ s.fillCell(1, daily = false); s.fillCell(2, daily = true)+ s.clearMarks(daily = false)+ assertEquals(emptySet(), s.filledCells.first()); assertEquals(setOf(2), s.dailyFilledCells.first())+ s.clearMarks(daily = true)+ assertEquals(emptySet(), s.dailyFilledCells.first()); assertEquals(STARTING_HEARTS, s.dailyHearts.first())+ }++ @Test fun recordCompletedIncrements() = runBlocking {+ val s = store()+ val a = s.recordCompleted(); val b = s.recordCompleted()+ assertEquals(a + 1, b); assertEquals(b, s.puzzlesCompleted.first())+ }++ @Test fun dailyRolloverClearsOnlyOnDayChange() = runBlocking {+ val s = store()+ s.ensureDailyDay(20_000L); s.fillCell(5, daily = true); s.revealBlank(6, daily = true)+ s.ensureDailyDay(20_000L)+ assertEquals(setOf(5), s.dailyFilledCells.first(), "same day must keep work")+ s.ensureDailyDay(20_001L)+ assertEquals(20_001L, s.dailyDay.first())+ assertEquals(emptySet(), s.dailyFilledCells.first())+ assertEquals(emptySet(), s.dailyRevealedBlanks.first())+ assertEquals(STARTING_HEARTS, s.dailyHearts.first())+ assertTrue(s.dailyCrossedCells.first().isEmpty())+ }++ @Test fun puzzleFilledCountAndClues() {+ val p = NonogramPuzzle.from(3, listOf(true,false,true, false,true,false, true,false,true))+ assertEquals(5, p.filledCount); assertEquals(3, p.size)+ assertEquals(listOf(1,1), p.rowClues[0]); assertEquals(emptyList<Int>(), NonogramPuzzle.cluesFor(listOf(false,false)))+ }+}
games/nonogram/src/test/java/com/vayunmathur/games/nonogram/platform/NonogramViewModelTest.kt
diff --git a/games/nonogram/src/test/java/com/vayunmathur/games/nonogram/platform/NonogramViewModelTest.kt b/games/nonogram/src/test/java/com/vayunmathur/games/nonogram/platform/NonogramViewModelTest.ktnew file mode 100644index 0000000000..a092b643df--- /dev/null+++ b/games/nonogram/src/test/java/com/vayunmathur/games/nonogram/platform/NonogramViewModelTest.kt@@ -0,0 +1,160 @@+package com.vayunmathur.games.nonogram.platform++import android.app.Application+import androidx.test.core.app.ApplicationProvider+import com.vayunmathur.games.nonogram.data.GameMode+import com.vayunmathur.games.nonogram.data.MarkMode+import com.vayunmathur.games.nonogram.data.STARTING_HEARTS+import kotlinx.coroutines.Dispatchers+import kotlinx.coroutines.ExperimentalCoroutinesApi+import kotlinx.coroutines.flow.first+import kotlinx.coroutines.launch+import kotlinx.coroutines.runBlocking+import kotlinx.coroutines.test.resetMain+import kotlinx.coroutines.test.setMain+import org.junit.runner.RunWith+import org.robolectric.RobolectricTestRunner+import kotlin.test.AfterTest+import kotlin.test.BeforeTest+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertFalse+import kotlin.test.assertNotNull+import kotlin.test.assertTrue++@OptIn(ExperimentalCoroutinesApi::class)+@RunWith(RobolectricTestRunner::class)+class NonogramViewModelTest {+ private val app get() = ApplicationProvider.getApplicationContext<Application>()+ @BeforeTest fun setUp() { Dispatchers.setMain(Dispatchers.Unconfined) }+ @AfterTest fun tearDown() { Dispatchers.resetMain() }++ private fun await(desc: String, cond: () -> Boolean) {+ val deadline = System.currentTimeMillis() + 20_000+ while (System.currentTimeMillis() < deadline) { if (cond()) return; Thread.sleep(25) }+ assertTrue(cond(), "timed out: $desc")+ }+ private fun loadedVm(): NonogramViewModel {+ val vm = NonogramViewModel(app)+ runBlocking { vm.dataStore.saveLevel(1); vm.dataStore.setGameMode(GameMode.CASUAL); vm.dataStore.setMarkMode(MarkMode.FILL) }+ await("game loads") { vm.uiState.value.game != null && !vm.uiState.value.generating }+ return vm+ }++ @Test fun loadsCasualPuzzleAndDefaults() {+ val vm = loadedVm()+ val s = vm.uiState.value+ assertEquals(GameMode.CASUAL, s.mode); assertEquals(1, s.level); assertEquals(MarkMode.FILL, s.markMode)+ val g = assertNotNull(s.game); assertEquals(5, g.size); assertEquals(STARTING_HEARTS, g.hearts)+ assertFalse(g.isWon); assertNotNull(vm.achievementsManager)+ assertEquals(NonogramViewModel.DAILY_KEY_PREFIX, "nonogram_daily")+ vm.dismissAchievementNotification()+ }++ @Test fun tapCorrectFillsWrongCostsHeartCrossModeNotesFree() {+ val vm = loadedVm()+ val g = vm.uiState.value.game!!+ val correct = g.puzzle.solution.indices.first { g.puzzle.solution[it] }+ val wrong = g.puzzle.solution.indices.first { !g.puzzle.solution[it] }+ vm.tapCell(correct)+ await("fill lands") { vm.uiState.value.game?.filled?.contains(correct) == true }+ vm.tapCell(correct) // locked: no-op+ vm.tapCell(wrong)+ await("heart spent") { vm.uiState.value.game?.hearts == STARTING_HEARTS - 1 }+ assertTrue(vm.uiState.value.game!!.revealedBlanks.contains(wrong))+ // Cross mode: tap only notes, even on a picture cell, never costs.+ vm.setMarkMode(MarkMode.CROSS)+ await("cross mode") { vm.uiState.value.markMode == MarkMode.CROSS }+ val noteCell = vm.uiState.value.game!!.puzzle.solution.indices.first { vm.uiState.value.game!!.puzzle.solution[it] && it !in vm.uiState.value.game!!.filled }+ vm.tapCell(noteCell)+ await("note placed") { vm.uiState.value.game?.crossed?.contains(noteCell) == true }+ assertEquals(STARTING_HEARTS - 1, vm.uiState.value.game!!.hearts)+ vm.tapCell(noteCell)+ await("note removed") { vm.uiState.value.game?.crossed?.contains(noteCell) == false }+ // Long-press in cross mode performs the fill action.+ vm.crossCell(noteCell)+ await("long press fills") { vm.uiState.value.game?.filled?.contains(noteCell) == true }+ }++ @Test fun longPressInFillModeNotesAndRestartClears() {+ val vm = loadedVm()+ val g = vm.uiState.value.game!!+ val cell = g.puzzle.solution.indices.first { !g.puzzle.solution[it] }+ vm.crossCell(cell)+ await("long-press note") { vm.uiState.value.game?.crossed?.contains(cell) == true }+ assertEquals(STARTING_HEARTS, vm.uiState.value.game!!.hearts)+ vm.restartLevel()+ await("restart clears") { vm.uiState.value.game?.crossed?.isEmpty() == true }+ }++ @Test fun nextLevelAdvancesAndDailyModeLoads() {+ val vm = loadedVm()+ vm.nextLevel()+ await("level 2") { vm.uiState.value.level == 2 && vm.uiState.value.game != null && !vm.uiState.value.generating }+ vm.setGameMode(GameMode.DAILY)+ await("daily loads") { vm.uiState.value.mode == GameMode.DAILY && vm.uiState.value.game?.size == 10 }+ // nextLevel is a no-op in daily mode.+ vm.nextLevel()+ assertEquals(GameMode.DAILY, vm.uiState.value.mode)+ vm.setGameMode(GameMode.CASUAL)+ await("back to casual") { vm.uiState.value.mode == GameMode.CASUAL && vm.uiState.value.game != null }+ }++ @Test fun winningBoardRecordsCompletionOnce() {+ val vm = loadedVm()+ val before = runBlocking { vm.dataStore.puzzlesCompleted.first() }+ val g = vm.uiState.value.game!!+ for (i in g.puzzle.solution.indices.filter { g.puzzle.solution[it] }) vm.tapCell(i)+ await("won") { vm.uiState.value.game?.isWon == true }+ await("completion recorded") { runBlocking { vm.dataStore.puzzlesCompleted.first() } == before + 1 }+ // Taps after game over are ignored.+ val hearts = vm.uiState.value.game!!.hearts+ vm.tapCell(g.puzzle.solution.indices.first { !g.puzzle.solution[it] })+ assertEquals(hearts, vm.uiState.value.game!!.hearts)+ }++ @Test fun winningDailyRecordsDailyWin() {+ val vm = loadedVm()+ vm.setGameMode(GameMode.DAILY)+ await("daily ready") { vm.uiState.value.mode == GameMode.DAILY && vm.uiState.value.game != null && !vm.uiState.value.generating }+ val before = runBlocking { vm.dataStore.puzzlesCompleted.first() }+ val g = vm.uiState.value.game!!+ for (i in g.puzzle.solution.indices.filter { g.puzzle.solution[it] }) vm.tapCell(i)+ await("daily won") { vm.uiState.value.game?.isWon == true }+ await("daily completion recorded") { runBlocking { vm.dataStore.puzzlesCompleted.first() } > before }+ }++ @Test fun reminderActionsUpdateState() {+ val vm = loadedVm()+ val enabledSeen = java.util.Collections.synchronizedList(mutableListOf<Boolean>())+ val minutesSeen = java.util.Collections.synchronizedList(mutableListOf<Long>())+ val scope = kotlinx.coroutines.CoroutineScope(Dispatchers.Unconfined)+ val j1 = scope.launch { vm.reminderEnabled.collect { enabledSeen += it } }+ val j2 = scope.launch { vm.reminderMinutesOfDay.collect { minutesSeen += it } }+ vm.setReminderEnabled(true)+ await("reminder on") { enabledSeen.contains(true) }+ vm.setReminderTime(7, 30)+ await("reminder time") { minutesSeen.contains(450L) }+ vm.setReminderEnabled(false)+ await("reminder off") { enabledSeen.lastOrNull() == false }+ j1.cancel(); j2.cancel()+ }++ @Test fun contractDefaultsAndNoop() {+ val s = NonogramUiState()+ assertEquals(null, s.game); assertEquals(GameMode.CASUAL, s.mode); assertEquals(1, s.level); assertFalse(s.generating)+ val ss = SettingsUiState(); assertFalse(ss.reminderEnabled); assertEquals(20, ss.reminderHour)+ NonogramGameActions.Noop.tapCell(0); NonogramGameActions.Noop.crossCell(0)+ NonogramGameActions.Noop.setMarkMode(MarkMode.CROSS); NonogramGameActions.Noop.nextLevel()+ NonogramGameActions.Noop.restartLevel(); NonogramGameActions.Noop.setGameMode(GameMode.DAILY)+ SettingsActions.Noop.setReminderEnabled(true); SettingsActions.Noop.setReminderTime(1, 2)+ assertEquals(listOf("settings", "datastore_default"), AppBackupAgent().datastoreNames)+ }++ @Test fun achievementsManagerBackfillDoesNotCrash() {+ val vm = loadedVm()+ vm.achievementsManager.checkExistingAchievements()+ assertTrue(vm.achievementsManager.achievements.any { it.id == "first_puzzle" })+ assertTrue(vm.achievementsManager.achievements.any { it.id == "daily_streak_30" })+ }+}
games/nonogram/src/test/java/com/vayunmathur/games/nonogram/ui/NonogramExpandedScreenTest.kt
diff --git a/games/nonogram/src/test/java/com/vayunmathur/games/nonogram/ui/NonogramExpandedScreenTest.kt b/games/nonogram/src/test/java/com/vayunmathur/games/nonogram/ui/NonogramExpandedScreenTest.ktnew file mode 100644index 0000000000..4c04159661--- /dev/null+++ b/games/nonogram/src/test/java/com/vayunmathur/games/nonogram/ui/NonogramExpandedScreenTest.kt@@ -0,0 +1,51 @@+package com.vayunmathur.games.nonogram.ui++import androidx.compose.ui.semantics.SemanticsActions+import androidx.compose.ui.test.junit4.createComposeRule+import androidx.compose.ui.test.onNodeWithText+import androidx.compose.ui.test.performSemanticsAction+import androidx.test.core.app.ApplicationProvider+import android.content.Context+import com.vayunmathur.games.nonogram.R+import com.vayunmathur.games.nonogram.data.GameMode+import com.vayunmathur.games.nonogram.data.MarkMode+import com.vayunmathur.games.nonogram.data.NonogramGameState+import com.vayunmathur.games.nonogram.data.NonogramPuzzle+import com.vayunmathur.games.nonogram.data.STARTING_HEARTS+import com.vayunmathur.games.nonogram.platform.NonogramUiState+import org.junit.Rule+import org.junit.runner.RunWith+import org.robolectric.RobolectricTestRunner+import org.robolectric.annotation.Config+import kotlin.test.Test+import kotlin.test.assertEquals++@RunWith(RobolectricTestRunner::class)+@Config(qualifiers = "w1280dp-h900dp")+class NonogramExpandedScreenTest {+ @get:Rule val rule = createComposeRule()+ private val ctx get() = ApplicationProvider.getApplicationContext<Context>()+ private fun str(id: Int, vararg args: Any) = if (args.isEmpty()) ctx.getString(id) else ctx.getString(id, *args)++ @Test fun expandedLayoutShowsSidePanelControls() {+ val a = NonogramScreensTest.Recording()+ val puzzle = NonogramPuzzle.from(3, listOf(false,true,false, true,true,true, false,true,false))+ val game = NonogramGameState(puzzle, emptySet(), emptySet(), emptySet(), STARTING_HEARTS, GameMode.CASUAL, 2)+ rule.setContent {+ NonogramGameScreen(NonogramUiState(game = game, level = 2, markMode = MarkMode.FILL), a, {}, {})+ }+ rule.onNodeWithText(str(R.string.how_to_play)).assertExists()+ rule.onNodeWithText(str(R.string.mode_cross)).performSemanticsAction(SemanticsActions.OnClick)+ assertEquals(listOf(MarkMode.CROSS), a.modes)+ }++ @Test fun expandedDailyInProgressShowsControls() {+ val a = NonogramScreensTest.Recording()+ val puzzle = NonogramPuzzle.from(3, listOf(true,false,true, false,true,false, true,false,true))+ val game = NonogramGameState(puzzle, setOf(0), emptySet(), emptySet(), 2, GameMode.DAILY, 1)+ rule.setContent {+ NonogramGameScreen(NonogramUiState(game = game, mode = GameMode.DAILY, dailyStreak = 4), a, {}, {})+ }+ rule.onNodeWithText(str(R.string.mode_daily)).assertExists()+ }+}
games/nonogram/src/test/java/com/vayunmathur/games/nonogram/ui/NonogramScreensTest.kt
diff --git a/games/nonogram/src/test/java/com/vayunmathur/games/nonogram/ui/NonogramScreensTest.kt b/games/nonogram/src/test/java/com/vayunmathur/games/nonogram/ui/NonogramScreensTest.ktnew file mode 100644index 0000000000..7c22610fbc--- /dev/null+++ b/games/nonogram/src/test/java/com/vayunmathur/games/nonogram/ui/NonogramScreensTest.kt@@ -0,0 +1,173 @@+package com.vayunmathur.games.nonogram.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.compose.ui.test.performTouchInput+import androidx.compose.ui.semantics.SemanticsActions+import androidx.compose.ui.test.performSemanticsAction+import androidx.compose.ui.test.longClick+import androidx.test.core.app.ApplicationProvider+import android.content.Context+import com.vayunmathur.games.nonogram.R+import com.vayunmathur.games.nonogram.data.GameMode+import com.vayunmathur.games.nonogram.data.MarkMode+import com.vayunmathur.games.nonogram.data.NonogramGameState+import com.vayunmathur.games.nonogram.data.NonogramPuzzle+import com.vayunmathur.games.nonogram.data.STARTING_HEARTS+import com.vayunmathur.games.nonogram.platform.NonogramGameActions+import com.vayunmathur.games.nonogram.platform.NonogramUiState+import com.vayunmathur.games.nonogram.platform.SettingsActions+import com.vayunmathur.games.nonogram.platform.SettingsUiState+import com.vayunmathur.games.nonogram.ui.components.NonogramBoard+import com.vayunmathur.library.ui.R as UiR+import org.junit.Rule+import org.junit.runner.RunWith+import org.robolectric.RobolectricTestRunner+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertTrue++@RunWith(RobolectricTestRunner::class)+class NonogramScreensTest {+ @get:Rule val rule = createComposeRule()+ private val ctx get() = ApplicationProvider.getApplicationContext<Context>()+ private fun str(id: Int, vararg args: Any) = if (args.isEmpty()) ctx.getString(id) else ctx.getString(id, *args)++ class Recording : NonogramGameActions {+ val taps = mutableListOf<Int>(); val crosses = mutableListOf<Int>()+ val modes = mutableListOf<MarkMode>(); val gameModes = mutableListOf<GameMode>()+ var next = 0; var restarts = 0+ override fun tapCell(index: Int) { taps += index }+ override fun crossCell(index: Int) { crosses += index }+ override fun setMarkMode(mode: MarkMode) { modes += mode }+ override fun nextLevel() { next++ }+ override fun restartLevel() { restarts++ }+ override fun setGameMode(mode: GameMode) { gameModes += mode }+ }++ private fun puzzle(): NonogramPuzzle {+ // 3x3 cross+ return NonogramPuzzle.from(3, listOf(false,true,false, true,true,true, false,true,false))+ }+ private fun game(filled: Set<Int> = emptySet(), crossed: Set<Int> = emptySet(), revealed: Set<Int> = emptySet(), hearts: Int = STARTING_HEARTS, mode: GameMode = GameMode.CASUAL) =+ NonogramGameState(puzzle(), filled, crossed, revealed, hearts, mode, level = 2)+ private fun answer() = puzzle().solution.indices.filter { puzzle().solution[it] }.toSet()++ @Test fun loadingStateShowsBoardChrome() {+ val a = Recording()+ rule.setContent { NonogramGameScreen(NonogramUiState(generating = true), a, {}, {}) }+ rule.onNodeWithText(str(R.string.level_title, 1)).assertExists()+ }++ @Test fun generationFailedOffersRetry() {+ val a = Recording()+ rule.setContent { NonogramGameScreen(NonogramUiState(generationFailed = true), a, {}, {}) }+ rule.onNodeWithText(str(R.string.generation_failed)).assertIsDisplayed()+ rule.onNodeWithText(str(R.string.try_again)).performClick()+ assertEquals(1, a.restarts)+ }++ @Test fun inProgressShowsHintToggleAndRestartWithoutConfirm() {+ val a = Recording()+ rule.setContent { NonogramGameScreen(NonogramUiState(game = game(), mode = GameMode.CASUAL, markMode = MarkMode.FILL, level = 2), a, {}, {}) }+ rule.onNodeWithText(str(R.string.how_to_play)).assertExists()+ rule.onNodeWithText(str(R.string.mode_cross)).performSemanticsAction(SemanticsActions.OnClick)+ assertEquals(listOf(MarkMode.CROSS), a.modes)+ rule.onNodeWithText(str(UiR.string.restart)).performSemanticsAction(SemanticsActions.OnClick)+ assertEquals(1, a.restarts, "untouched board restarts without confirmation")+ }++ @Test fun restartWithProgressAsksConfirmation() {+ val a = Recording()+ rule.setContent { NonogramGameScreen(NonogramUiState(game = game(filled = setOf(1)), level = 2), a, {}, {}) }+ rule.onNodeWithText(str(UiR.string.restart)).performSemanticsAction(SemanticsActions.OnClick)+ rule.onNodeWithText(str(R.string.confirm_restart_title)).assertExists()+ rule.onNodeWithText(str(UiR.string.cancel)).performClick()+ assertEquals(0, a.restarts)+ rule.onNodeWithText(str(UiR.string.restart)).performSemanticsAction(SemanticsActions.OnClick)+ rule.onNodeWithText(str(R.string.confirm_restart_title)).assertExists()+ // Two Restart nodes now exist (screen + dialog); the dialog's is last.+ val nodes = rule.onAllNodes(androidx.compose.ui.test.hasText(str(UiR.string.restart)))+ nodes.fetchSemanticsNodes().size.let { assertTrue(it >= 2) }+ nodes[1].performClick()+ assertEquals(1, a.restarts)+ }++ @Test fun wonCasualShowsNextLevel() {+ val a = Recording()+ rule.setContent { NonogramGameScreen(NonogramUiState(game = game(filled = answer()), level = 2), a, {}, {}) }+ rule.onNodeWithText(str(R.string.solved)).assertExists()+ rule.onNodeWithText(str(UiR.string.next_level)).performClick()+ assertEquals(1, a.next)+ }++ @Test fun wonDailyShowsComeBack() {+ val a = Recording()+ rule.setContent { NonogramGameScreen(NonogramUiState(game = game(filled = answer(), mode = GameMode.DAILY), mode = GameMode.DAILY, dailyStreak = 3), a, {}, {}) }+ rule.onNodeWithText(str(R.string.daily_come_back_tomorrow)).assertExists()+ }++ @Test fun failedShowsTryAgain() {+ val a = Recording()+ rule.setContent { NonogramGameScreen(NonogramUiState(game = game(hearts = 0), level = 2), a, {}, {}) }+ rule.onNodeWithText(str(R.string.out_of_hearts)).assertExists()+ rule.onNodeWithText(str(R.string.try_again)).performClick()+ assertEquals(1, a.restarts)+ }++ @Test fun boardCellsReportDescriptionsAndTaps() {+ val taps = mutableListOf<Int>()+ rule.setContent { NonogramBoard(game = game(filled = setOf(1), crossed = setOf(0)), onTap = { taps += it }, onLongPress = {}) }+ rule.onNodeWithContentDescription(str(R.string.cd_cell, 1, 2, str(R.string.cd_filled))).assertExists()+ rule.onNodeWithContentDescription(str(R.string.cd_cell, 1, 1, str(R.string.cd_crossed))).assertExists()+ rule.onNodeWithContentDescription(str(R.string.cd_cell, 1, 3, str(R.string.cd_blank))).performClick()+ assertEquals(listOf(2), taps)+ }++ @Test fun boardLongPressFiresCross() {+ val crosses = mutableListOf<Int>()+ rule.setContent { NonogramBoard(game = game(), onTap = {}, onLongPress = { crosses += it }) }+ rule.onNodeWithContentDescription(str(R.string.cd_cell, 2, 2, str(R.string.cd_blank)))+ .performTouchInput { longClick() }+ assertEquals(listOf(4), crosses)+ }++ @Test fun boardWithEmptyLinesRendersZeroClues() {+ val p = com.vayunmathur.games.nonogram.data.NonogramPuzzle.from(4, listOf(+ true,true,false,false, false,false,false,false, false,false,true,false, false,false,false,true))+ val g = NonogramGameState(p, emptySet(), emptySet(), emptySet(), STARTING_HEARTS, GameMode.CASUAL, 1)+ rule.setContent { NonogramBoard(game = g, onTap = {}, onLongPress = {}) }+ rule.onNodeWithContentDescription(str(R.string.cd_cell, 2, 1, str(R.string.cd_blank))).assertExists()+ rule.onNodeWithText("0").assertExists()+ }++ @Test fun boardLargeGeneratedPuzzleRenders() {+ val p = requireNotNull(com.vayunmathur.games.nonogram.domain.NonogramGenerator.generateSeeded(15, 99L))+ val g = NonogramGameState(p, setOf(0), setOf(1), emptySet(), 2, GameMode.CASUAL, 25)+ rule.setContent { NonogramBoard(game = g, onTap = {}, onLongPress = {}) }+ rule.onNodeWithContentDescription(str(R.string.cd_cell, 15, 15, str(R.string.cd_blank))).assertExists()+ }++ @Test fun settingsPageWithRealViewModelRenders() {+ val vm = com.vayunmathur.games.nonogram.platform.NonogramViewModel(+ androidx.test.core.app.ApplicationProvider.getApplicationContext<android.app.Application>())+ var backed = false+ rule.setContent { SettingsPage(vm, onBack = { backed = true }) }+ rule.onNodeWithText(str(UiR.string.settings)).assertExists()+ assertTrue(!backed)+ }++ @Test fun settingsScreenRendersAndToggles() {+ var enabled: Boolean? = null; var time: Pair<Int, Int>? = null+ val actions = object : SettingsActions {+ override fun setReminderEnabled(e: Boolean) { enabled = e }+ override fun setReminderTime(hour: Int, minute: Int) { time = hour to minute }+ }+ rule.setContent { SettingsScreen(SettingsUiState(false, 20, 0), actions, {}) }+ rule.onNodeWithText(str(UiR.string.settings)).assertExists()+ assertTrue(enabled == null && time == null)+ }+}