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 NoDeadClickDetectorTest : LintDetectorTest() { override fun getDetector(): Detector = NoDeadClickDetector() override fun getIssues(): List = listOf(NoDeadClickDetector.ISSUE) @Test fun emptyButtonOnClickIsFlagged() { lint().files(UI_STUBS, COMPOSABLE_STUB, MODIFIER_STUB, FOUNDATION_STUB, kotlin( "src/main/java/com/vayunmathur/example/ui/Screen.kt", """ package com.vayunmathur.example.ui import androidx.compose.runtime.Composable import com.vayunmathur.library.ui.Button import com.vayunmathur.library.ui.Text @Composable fun Screen() { Button(onClick = {}) { Text("Save") } } """.trimIndent(), )).testModes(TestMode.DEFAULT).run().expectErrorCount(1) } @Test fun commentOnlyOnClickIsFlagged() { lint().files(UI_STUBS, COMPOSABLE_STUB, MODIFIER_STUB, FOUNDATION_STUB, kotlin( "src/main/java/com/vayunmathur/example/ui/Screen.kt", """ package com.vayunmathur.example.ui import androidx.compose.runtime.Composable import com.vayunmathur.library.ui.IconButton @Composable fun Screen() { IconButton(onClick = { // TODO: wire overflow menu }) { } } """.trimIndent(), )).testModes(TestMode.DEFAULT).run().expectErrorCount(1) } @Test fun wiredButtonIsClean() { lint().files(UI_STUBS, COMPOSABLE_STUB, MODIFIER_STUB, FOUNDATION_STUB, kotlin( "src/main/java/com/vayunmathur/example/ui/Screen.kt", """ package com.vayunmathur.example.ui import androidx.compose.runtime.Composable import com.vayunmathur.library.ui.Button import com.vayunmathur.library.ui.Text @Composable fun Screen(save: () -> Unit) { Button(onClick = save) { Text("Save") } } """.trimIndent(), )).testModes(TestMode.DEFAULT).run().expectClean() } @Test fun disabledButtonWithEmptyOnClickIsClean() { lint().files(UI_STUBS, COMPOSABLE_STUB, MODIFIER_STUB, FOUNDATION_STUB, kotlin( "src/main/java/com/vayunmathur/example/ui/Screen.kt", """ package com.vayunmathur.example.ui import androidx.compose.runtime.Composable import com.vayunmathur.library.ui.Button import com.vayunmathur.library.ui.Text @Composable fun Screen() { Button(onClick = {}, enabled = false) { Text("Working…") } } """.trimIndent(), )).testModes(TestMode.DEFAULT).run().expectClean() } @Test fun labelChipIsCleanByWidgetType() { lint().files(UI_STUBS, COMPOSABLE_STUB, MODIFIER_STUB, FOUNDATION_STUB, kotlin( "src/main/java/com/vayunmathur/example/ui/Screen.kt", """ package com.vayunmathur.example.ui import androidx.compose.runtime.Composable import com.vayunmathur.library.ui.AssistChip import com.vayunmathur.library.ui.Text @Composable fun Screen() { AssistChip(onClick = {}, label = { Text("Due today") }) } """.trimIndent(), )).testModes(TestMode.DEFAULT).run().expectClean() } @Test fun menuItemWithEmptyOnClickIsFlagged() { lint().files(UI_STUBS, COMPOSABLE_STUB, MODIFIER_STUB, FOUNDATION_STUB, kotlin( "src/main/java/com/vayunmathur/example/ui/Screen.kt", """ package com.vayunmathur.example.ui import androidx.compose.runtime.Composable import com.vayunmathur.library.ui.DropdownMenuItem import com.vayunmathur.library.ui.Text @Composable fun Screen() { DropdownMenuItem(text = { Text("Info") }, onClick = {}) } """.trimIndent(), )).testModes(TestMode.DEFAULT).run().expectErrorCount(1) } @Test fun disabledMenuItemIsClean() { lint().files(UI_STUBS, COMPOSABLE_STUB, MODIFIER_STUB, FOUNDATION_STUB, kotlin( "src/main/java/com/vayunmathur/example/ui/Screen.kt", """ package com.vayunmathur.example.ui import androidx.compose.runtime.Composable import com.vayunmathur.library.ui.DropdownMenuItem import com.vayunmathur.library.ui.Text @Composable fun Screen() { DropdownMenuItem(text = { Text("Synced") }, onClick = {}, enabled = false) } """.trimIndent(), )).testModes(TestMode.DEFAULT).run().expectClean() } @Test fun emptyClickableModifierIsFlagged() { lint().files(UI_STUBS, COMPOSABLE_STUB, MODIFIER_STUB, FOUNDATION_STUB, kotlin( "src/main/java/com/vayunmathur/example/ui/Screen.kt", """ package com.vayunmathur.example.ui import androidx.compose.foundation.clickable import androidx.compose.runtime.Composable import com.vayunmathur.library.ui.Box @Composable fun Screen() { Box(modifier = androidx.compose.ui.Modifier.clickable {}) } """.trimIndent(), )).testModes(TestMode.DEFAULT).run().expectErrorCount(1) } @Test fun longPressOnlyRowIsClean() { lint().files(UI_STUBS, COMPOSABLE_STUB, MODIFIER_STUB, FOUNDATION_STUB, kotlin( "src/main/java/com/vayunmathur/example/ui/Screen.kt", """ package com.vayunmathur.example.ui import androidx.compose.foundation.combinedClickable import androidx.compose.runtime.Composable import com.vayunmathur.library.ui.ListItem import com.vayunmathur.library.ui.Text @Composable fun Screen(copy: () -> Unit) { ListItem( headlineContent = { Text("Row") }, modifier = androidx.compose.ui.Modifier.combinedClickable(onClick = {}, onLongClick = copy), ) } """.trimIndent(), )).testModes(TestMode.DEFAULT).run().expectClean() } @Test fun listItemDeadClickableIsFlagged() { lint().files(UI_STUBS, COMPOSABLE_STUB, MODIFIER_STUB, FOUNDATION_STUB, kotlin( "src/main/java/com/vayunmathur/example/ui/Screen.kt", """ package com.vayunmathur.example.ui import androidx.compose.foundation.clickable import androidx.compose.runtime.Composable import com.vayunmathur.library.ui.ListItem import com.vayunmathur.library.ui.Text @Composable fun Screen() { ListItem( headlineContent = { Text("Row") }, modifier = androidx.compose.ui.Modifier.clickable {}, ) } """.trimIndent(), )).testModes(TestMode.DEFAULT).run().expectErrorCount(1) } @Test fun exceptionMarkerOptsOut() { lint().files(UI_STUBS, COMPOSABLE_STUB, MODIFIER_STUB, FOUNDATION_STUB, kotlin( "src/main/java/com/vayunmathur/example/ui/Screen.kt", """ package com.vayunmathur.example.ui // DEAD CLICK EXCEPTION: drag-handle grip; the reorder modifier owns the gesture import androidx.compose.runtime.Composable import com.vayunmathur.library.ui.IconButton @Composable fun Screen() { IconButton(onClick = {}) { } } """.trimIndent(), )).testModes(TestMode.DEFAULT).run().expectClean() } @Test fun testSourceIsOutOfScope() { lint().files(UI_STUBS, COMPOSABLE_STUB, MODIFIER_STUB, FOUNDATION_STUB, kotlin( "src/test/java/com/vayunmathur/example/ui/ScreenTest.kt", """ package com.vayunmathur.example.ui import androidx.compose.runtime.Composable import com.vayunmathur.library.ui.Button import com.vayunmathur.library.ui.Text @Composable fun Screen() { Button(onClick = {}) { Text("Save") } } """.trimIndent(), )).testModes(TestMode.DEFAULT).run().expectClean() } companion object { private val MODIFIER_STUB: TestFile = kotlin( "src/androidx/compose/ui/Modifier.kt", """ package androidx.compose.ui open class Modifier { companion object : Modifier() fun then(other: Modifier): Modifier = other } """.trimIndent(), ) private val FOUNDATION_STUB: TestFile = kotlin( "src/androidx/compose/foundation/Clickable.kt", """ package androidx.compose.foundation import androidx.compose.ui.Modifier fun Modifier.clickable(enabled: Boolean = true, onClick: () -> Unit): Modifier = this fun Modifier.combinedClickable(enabled: Boolean = true, onClick: () -> Unit, onLongClick: (() -> Unit)? = null): Modifier = this """.trimIndent(), ) private val COMPOSABLE_STUB: TestFile = kotlin( "src/androidx/compose/runtime/Composable.kt", """ package androidx.compose.runtime annotation class Composable """.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 Button(onClick: () -> Unit, enabled: Boolean = true, content: @Composable () -> Unit) {} @Composable fun IconButton(onClick: () -> Unit, enabled: Boolean = true, content: @Composable () -> Unit) {} @Composable fun DropdownMenuItem(text: @Composable () -> Unit, onClick: () -> Unit, enabled: Boolean = true) {} @Composable fun AssistChip(onClick: () -> Unit, label: @Composable () -> Unit, enabled: Boolean = true) {} @Composable fun ListItem(headlineContent: @Composable () -> Unit, modifier: androidx.compose.ui.Modifier = androidx.compose.ui.Modifier) {} @Composable fun Box(modifier: androidx.compose.ui.Modifier = androidx.compose.ui.Modifier) {} @Composable fun Text(text: String) {} """.trimIndent(), ) } } @RunWith(JUnit4::class) class DeadClickMarkerFormatTest : LintDetectorTest() { override fun getDetector(): Detector = ExceptionMarkerFormatDetector() override fun getIssues(): List = listOf(ExceptionMarkerFormatDetector.ISSUE) @Test fun wellFormedDeadClickMarkerIsClean() { lint().files(kotlin( """ package com.vayunmathur.example // DEAD CLICK EXCEPTION: drag handle; reorder modifier owns the gesture class Foo """.trimIndent(), )).testModes(TestMode.DEFAULT).run().expectClean() } @Test fun deadClickMarkerWithoutReasonIsFlagged() { lint().files(kotlin( """ package com.vayunmathur.example // DEAD CLICK EXCEPTION: class Foo """.trimIndent(), )).testModes(TestMode.DEFAULT).run().expectErrorCount(1) } }