package com.vayunmathur.lint import com.android.tools.lint.checks.infrastructure.LintDetectorTest import com.android.tools.lint.checks.infrastructure.TestFile import com.android.tools.lint.checks.infrastructure.TestFiles.kotlin import com.android.tools.lint.checks.infrastructure.TestMode import com.android.tools.lint.detector.api.Detector import com.android.tools.lint.detector.api.Issue import org.junit.Test import org.junit.runner.RunWith import org.junit.runners.JUnit4 @RunWith(JUnit4::class) class DialogAsRouteDetectorTest : LintDetectorTest() { override fun getDetector(): Detector = DialogAsRouteDetector() override fun getIssues(): List = listOf(DialogAsRouteDetector.ISSUE) @Test fun gatedAlertDialogInScreenIsFlagged() { lint().files(UI_STUBS, STATE_STUBS, kotlin( "src/main/java/com/vayunmathur/example/ui/ListScreen.kt", """ package com.vayunmathur.example.ui import androidx.compose.runtime.Composable import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import com.vayunmathur.library.ui.AlertDialog @Composable fun ListScreen() { var showDeleteDialog by remember { mutableStateOf(false) } if (showDeleteDialog) { AlertDialog(onDismissRequest = { showDeleteDialog = false }) } } """.trimIndent(), )).testModes(TestMode.DEFAULT).run().expectErrorCount(1) } @Test fun nullablePendingStateLetDialogIsFlagged() { lint().files(UI_STUBS, STATE_STUBS, kotlin( "src/main/java/com/vayunmathur/example/ui/ListScreen.kt", """ package com.vayunmathur.example.ui import androidx.compose.runtime.Composable import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import com.vayunmathur.library.ui.ConfirmDialog @Composable fun ListScreen() { var pendingName by remember { mutableStateOf(null) } pendingName?.let { name -> ConfirmDialog(title = name, confirmLabel = "Delete", onConfirm = {}, onDismiss = {}) } } """.trimIndent(), )).testModes(TestMode.DEFAULT).run().expectErrorCount(1) } @Test fun customDialogComposableGatedByStateIsFlagged() { lint().files(UI_STUBS, STATE_STUBS, kotlin( "src/main/java/com/vayunmathur/example/ui/ListScreen.kt", """ package com.vayunmathur.example.ui import androidx.compose.runtime.Composable import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.rememberSaveable @Composable fun ListScreen() { var renaming by rememberSaveable { mutableStateOf(null) } renaming?.let { tag -> RenameTagDialog(tag = tag, onDismiss = { renaming = null }) } } @Composable fun RenameTagDialog(tag: String, onDismiss: () -> Unit) {} """.trimIndent(), )).testModes(TestMode.DEFAULT).run().expectErrorCount(1) } @Test fun gatedModalBottomSheetIsFlagged() { lint().files(UI_STUBS, STATE_STUBS, kotlin( "src/main/java/com/vayunmathur/example/ui/ListScreen.kt", """ package com.vayunmathur.example.ui import androidx.compose.runtime.Composable import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import com.vayunmathur.library.ui.ModalBottomSheet @Composable fun ListScreen() { var showSheet by remember { mutableStateOf(false) } if (showSheet) { ModalBottomSheet(onDismissRequest = { showSheet = false }) {} } } """.trimIndent(), )).testModes(TestMode.DEFAULT).run().expectErrorCount(1) } @Test fun dialogInsideSameFileHelperInvokedUnderGateIsFlagged() { // The helper's AlertDialog and its gated call site are both reported: // the definition does not become legal by moving one brace away. lint().files(UI_STUBS, STATE_STUBS, kotlin( "src/main/java/com/vayunmathur/example/ui/EditScreen.kt", """ package com.vayunmathur.example.ui import androidx.compose.runtime.Composable import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import com.vayunmathur.library.ui.AlertDialog @Composable fun EditScreen() { var showCustomDialog by remember { mutableStateOf(false) } if (showCustomDialog) { CustomReminderDialog(onDismiss = { showCustomDialog = false }) } } @Composable private fun CustomReminderDialog(onDismiss: () -> Unit) { AlertDialog(onDismissRequest = onDismiss) } """.trimIndent(), )).testModes(TestMode.DEFAULT).run().expectErrorCount(2) } @Test fun unconditionalDialogInRouteEntryIsClean() { lint().files(UI_STUBS, STATE_STUBS, kotlin( "src/main/java/com/vayunmathur/example/ui/DialogEntries.kt", """ package com.vayunmathur.example.ui import androidx.compose.runtime.Composable import com.vayunmathur.library.ui.ConfirmDialog @Composable fun DeleteDialogHost(onConfirm: () -> Unit, onDismiss: () -> Unit) { ConfirmDialog(title = "Delete?", confirmLabel = "Delete", onConfirm = onConfirm, onDismiss = onDismiss) } """.trimIndent(), )).testModes(TestMode.DEFAULT).run().expectClean() } @Test fun dialogDefinitionUnderDialogsPackageIsClean() { lint().files(UI_STUBS, STATE_STUBS, kotlin( "src/main/java/com/vayunmathur/example/ui/dialogs/RenameDialog.kt", """ package com.vayunmathur.example.ui.dialogs import androidx.compose.runtime.Composable import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import com.vayunmathur.library.ui.AlertDialog @Composable fun RenameDialog(onDismiss: () -> Unit) { var showHelp by remember { mutableStateOf(false) } if (showHelp) { AlertDialog(onDismissRequest = { showHelp = false }) } AlertDialog(onDismissRequest = onDismiss) } """.trimIndent(), )).testModes(TestMode.DEFAULT).run().expectClean() } @Test fun routeConstructionAndUnconditionalContentCallAreClean() { lint().files(UI_STUBS, STATE_STUBS, kotlin( "src/main/java/com/vayunmathur/example/ui/ListScreen.kt", """ package com.vayunmathur.example.ui import androidx.compose.runtime.Composable import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember @Composable fun ListScreen(openDialog: () -> Unit) { var query by remember { mutableStateOf("") } if (query.isNotEmpty()) { HelperText(query) } } @Composable fun HelperText(text: String) {} """.trimIndent(), )).testModes(TestMode.DEFAULT).run().expectClean() } @Test fun exceptionMarkerOptsOut() { lint().files(UI_STUBS, STATE_STUBS, kotlin( "src/main/java/com/vayunmathur/example/ui/ListScreen.kt", """ package com.vayunmathur.example.ui // DIALOG LOCAL EXCEPTION: multi-step transfer flow; route conversion is a separate change import androidx.compose.runtime.Composable import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import com.vayunmathur.library.ui.AlertDialog @Composable fun ListScreen() { var showDialog by remember { mutableStateOf(false) } if (showDialog) { AlertDialog(onDismissRequest = { showDialog = false }) } } """.trimIndent(), )).testModes(TestMode.DEFAULT).run().expectClean() } @Test fun libraryPackageIsExempt() { lint().files(UI_STUBS, STATE_STUBS, kotlin( "src/main/java/com/vayunmathur/library/ui/Internal.kt", """ package com.vayunmathur.library.ui import androidx.compose.runtime.Composable import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember @Composable fun Internal() { var showDialog by remember { mutableStateOf(false) } if (showDialog) { AlertDialog(onDismissRequest = { showDialog = false }) } } """.trimIndent(), )).testModes(TestMode.DEFAULT).run().expectClean() } @Test fun screenshotTestSourceIsSkipped() { lint().files(UI_STUBS, STATE_STUBS, kotlin( "src/screenshotTest/kotlin/com/vayunmathur/example/ui/Previews.kt", """ package com.vayunmathur.example.ui import androidx.compose.runtime.Composable import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import com.vayunmathur.library.ui.AlertDialog @Composable fun PreviewDialog() { var showDialog by remember { mutableStateOf(true) } if (showDialog) { AlertDialog(onDismissRequest = { showDialog = false }) } } """.trimIndent(), )).testModes(TestMode.DEFAULT).run().expectClean() } companion object { private val STATE_STUBS: TestFile = kotlin( "src/androidx/compose/runtime/StateStubs.kt", """ package androidx.compose.runtime annotation class Composable interface State { val value: T } class MutableState(override var value: T) : State fun mutableStateOf(value: T): MutableState = MutableState(value) fun remember(calculation: () -> T): T = calculation() fun rememberSaveable(calculation: () -> T): T = calculation() """.trimIndent(), ) private val UI_STUBS: TestFile = kotlin( "src/com/vayunmathur/library/ui/UiStubs.kt", """ package com.vayunmathur.library.ui import androidx.compose.runtime.Composable @Composable fun AlertDialog(onDismissRequest: () -> Unit) {} @Composable fun ConfirmDialog(title: String, confirmLabel: String, onConfirm: () -> Unit, onDismiss: () -> Unit) {} @Composable fun ModalBottomSheet(onDismissRequest: () -> Unit, content: @Composable () -> Unit) {} """.trimIndent(), ) } } @RunWith(JUnit4::class) class DialogLocalMarkerFormatTest : LintDetectorTest() { override fun getDetector(): Detector = ExceptionMarkerFormatDetector() override fun getIssues(): List = listOf(ExceptionMarkerFormatDetector.ISSUE) @Test fun wellFormedDialogMarkerIsClean() { lint().files(kotlin( """ package com.vayunmathur.example // DIALOG LOCAL EXCEPTION: multi-step flow, no route shape yet class Screen """.trimIndent(), )).testModes(TestMode.DEFAULT).run().expectClean() } @Test fun dialogMarkerWithoutReasonIsFlagged() { lint().files(kotlin( """ package com.vayunmathur.example // DIALOG LOCAL EXCEPTION: class Screen """.trimIndent(), )).testModes(TestMode.DEFAULT).run().expectErrorCount(1) } @Test fun dialogMarkerWithoutColonIsFlagged() { lint().files(kotlin( """ package com.vayunmathur.example // DIALOG LOCAL EXCEPTION multi-step flow class Screen """.trimIndent(), )).testModes(TestMode.DEFAULT).run().expectErrorCount(1) } }