|
| 1 | +package dev.thor.rombutler.ui.settings |
| 2 | + |
| 3 | +import androidx.compose.ui.test.assertIsEnabled |
| 4 | +import androidx.compose.ui.test.assertIsNotEnabled |
| 5 | +import androidx.compose.ui.test.assertIsDisplayed |
| 6 | +import androidx.compose.ui.test.hasText |
| 7 | +import androidx.compose.ui.test.junit4.v2.createComposeRule |
| 8 | +import androidx.compose.ui.test.onNodeWithTag |
| 9 | +import androidx.compose.ui.test.onNodeWithText |
| 10 | +import androidx.compose.ui.test.performClick |
| 11 | +import androidx.compose.ui.test.performTextClearance |
| 12 | +import androidx.compose.ui.test.performTextInput |
| 13 | +import androidx.test.ext.junit.runners.AndroidJUnit4 |
| 14 | +import dev.thor.rombutler.domain.detection.SystemRegistry |
| 15 | +import dev.thor.rombutler.domain.model.Confidence |
| 16 | +import dev.thor.rombutler.domain.model.SystemDefinition |
| 17 | +import dev.thor.rombutler.domain.model.SystemPack |
| 18 | +import dev.thor.rombutler.ui.theme.ThorRomButlerTheme |
| 19 | +import org.junit.Assert.assertEquals |
| 20 | +import org.junit.Assert.assertTrue |
| 21 | +import org.junit.Rule |
| 22 | +import org.junit.Test |
| 23 | +import org.junit.runner.RunWith |
| 24 | + |
| 25 | +@RunWith(AndroidJUnit4::class) |
| 26 | +class SystemPackDialogsTest { |
| 27 | + |
| 28 | + @get:Rule |
| 29 | + val compose = createComposeRule() |
| 30 | + |
| 31 | + @Test |
| 32 | + fun importPreviewShowsExactPackAndConflictsBeforeConfirmation() { |
| 33 | + val registry = SystemRegistry() |
| 34 | + val custom = SystemDefinition( |
| 35 | + id = "preview", |
| 36 | + displayName = "Preview System", |
| 37 | + esdeFolder = "preview", |
| 38 | + extensions = mapOf("gba" to Confidence.CERTAIN), |
| 39 | + ) |
| 40 | + val preview = SystemPackImportPreview( |
| 41 | + pack = SystemPack(1, "user.preview", "Preview Pack", listOf(custom)), |
| 42 | + conflicts = registry.conflictsForCustomSystems(listOf(custom)), |
| 43 | + ) |
| 44 | + val registryState = registry.state.value |
| 45 | + var confirmed = false |
| 46 | + |
| 47 | + compose.setContent { |
| 48 | + ThorRomButlerTheme { |
| 49 | + SystemPackManagerDialog( |
| 50 | + state = registryState, |
| 51 | + importPreview = preview, |
| 52 | + onSave = { _, _ -> }, |
| 53 | + onDelete = {}, |
| 54 | + onRequestImport = {}, |
| 55 | + onConfirmImport = { confirmed = true }, |
| 56 | + onCancelImport = {}, |
| 57 | + onExport = {}, |
| 58 | + onDismiss = {}, |
| 59 | + ) |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + compose.onNodeWithTag(SystemPackTestTags.IMPORT_PREVIEW).assertIsDisplayed() |
| 64 | + compose.onNodeWithText("Preview Pack").assertIsDisplayed() |
| 65 | + compose.onNode(hasText(".gba", substring = true)).assertIsDisplayed() |
| 66 | + compose.onNodeWithTag(SystemPackTestTags.CONFIRM_IMPORT).performClick() |
| 67 | + compose.runOnIdle { assertTrue(confirmed) } |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + fun editorRejectsBuiltInIdAndAcceptsSafeCustomDefinition() { |
| 72 | + val registry = SystemRegistry() |
| 73 | + val registryState = registry.state.value |
| 74 | + var saved: SystemDefinition? = null |
| 75 | + |
| 76 | + compose.setContent { |
| 77 | + ThorRomButlerTheme { |
| 78 | + SystemPackManagerDialog( |
| 79 | + state = registryState, |
| 80 | + importPreview = null, |
| 81 | + onSave = { _, definition -> saved = definition }, |
| 82 | + onDelete = {}, |
| 83 | + onRequestImport = {}, |
| 84 | + onConfirmImport = {}, |
| 85 | + onCancelImport = {}, |
| 86 | + onExport = {}, |
| 87 | + onDismiss = {}, |
| 88 | + ) |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + compose.onNodeWithTag(SystemPackTestTags.ADD_SYSTEM).performClick() |
| 93 | + compose.onNodeWithTag(SystemPackTestTags.NAME_FIELD).performTextInput("Custom Handheld") |
| 94 | + compose.onNodeWithTag(SystemPackTestTags.ID_FIELD).performTextInput("gba") |
| 95 | + compose.onNodeWithTag(SystemPackTestTags.FOLDER_FIELD).performTextInput("customhandheld") |
| 96 | + compose.onNodeWithTag(SystemPackTestTags.EXTENSIONS_FIELD).performTextInput("gba") |
| 97 | + compose.onNodeWithTag(SystemPackTestTags.SAVE_SYSTEM).assertIsNotEnabled() |
| 98 | + |
| 99 | + compose.onNodeWithTag(SystemPackTestTags.ID_FIELD).performTextClearance() |
| 100 | + compose.onNodeWithTag(SystemPackTestTags.ID_FIELD).performTextInput("customhandheld") |
| 101 | + compose.onNodeWithTag(SystemPackTestTags.SAVE_SYSTEM).assertIsEnabled().performClick() |
| 102 | + |
| 103 | + compose.runOnIdle { assertEquals("customhandheld", saved?.id) } |
| 104 | + } |
| 105 | +} |
0 commit comments