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.TestMode import com.android.tools.lint.checks.infrastructure.TestFiles.kotlin 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 RouteScreenshotCoverageDetectorTest : LintDetectorTest() { override fun getDetector(): Detector = RouteScreenshotCoverageDetector() override fun getIssues(): List = listOf(RouteScreenshotCoverageDetector.ISSUE) @Test fun allRoutesCoveredIsClean() { lint().files(NAVKEY_STUB, PREVIEW_STUBS, COMPOSABLE_STUB, PREVIEWTEST_STUB, routeFile(), previews("1-feed", "2-story")).testModes(TestMode.DEFAULT).run().expectClean() } @Test fun unmappedRouteIsFlagged() { lint().files(NAVKEY_STUB, PREVIEW_STUBS, COMPOSABLE_STUB, PREVIEWTEST_STUB, routeFile(), previews("1-feed")).testModes(TestMode.DEFAULT).run().expectErrorCount(1) } @Test fun variantSuffixStillCovers() { lint().files(NAVKEY_STUB, PREVIEW_STUBS, COMPOSABLE_STUB, PREVIEWTEST_STUB, routeFile(), previews("1-feed", "2-story-expanded")).testModes(TestMode.DEFAULT).run().expectClean() } @Test fun longestRouteKeyWins() { // `1-call-logs` must cover CallLogs, not Call: Call stays unmapped. lint().files( NAVKEY_STUB, PREVIEW_STUBS, COMPOSABLE_STUB, PREVIEWTEST_STUB, kotlin( "src/main/java/com/vayunmathur/example/Route.kt", """ package com.vayunmathur.example import com.vayunmathur.library.util.NavKey sealed interface Route : NavKey { data object Call : Route data object CallLogs : Route } """.trimIndent(), ), previews("1-call-logs"), ).testModes(TestMode.DEFAULT).run().expectErrorCount(1) } @Test fun nestedRouteLeafNeedsItsOwnCapture() { lint().files( NAVKEY_STUB, PREVIEW_STUBS, COMPOSABLE_STUB, PREVIEWTEST_STUB, kotlin( "src/main/java/com/vayunmathur/example/Route.kt", """ package com.vayunmathur.example import com.vayunmathur.library.util.NavKey sealed interface Route : NavKey { data object Feed : Route sealed interface Registration : Route { data object Number : Registration data class Code(val cc: String) : Registration } } """.trimIndent(), ), previews("1-feed", "2-registration-number"), ).testModes(TestMode.DEFAULT).run().expectErrorCount(1) } @Test fun exceptionMarkerOptsRouteOut() { lint().files( NAVKEY_STUB, PREVIEW_STUBS, COMPOSABLE_STUB, PREVIEWTEST_STUB, kotlin( "src/main/java/com/vayunmathur/example/Route.kt", """ package com.vayunmathur.example import com.vayunmathur.library.util.NavKey // SCREENSHOT COVERAGE EXCEPTION: Story: live call surface, cannot render in Layoutlib sealed interface Route : NavKey { data object Feed : Route data object Story : Route } """.trimIndent(), ), previews("1-feed"), ).testModes(TestMode.DEFAULT).run().expectClean() } @Test fun exemptionNamingNoRouteIsFlagged() { lint().files( NAVKEY_STUB, PREVIEW_STUBS, COMPOSABLE_STUB, PREVIEWTEST_STUB, kotlin( "src/main/java/com/vayunmathur/example/Route.kt", """ package com.vayunmathur.example import com.vayunmathur.library.util.NavKey // SCREENSHOT COVERAGE EXCEPTION: Nope: not a route sealed interface Route : NavKey { data object Feed : Route } """.trimIndent(), ), previews("1-feed"), ).testModes(TestMode.DEFAULT).run().expectErrorCount(1) } @Test fun noRouteIsClean() { lint().files( NAVKEY_STUB, PREVIEW_STUBS, COMPOSABLE_STUB, PREVIEWTEST_STUB, kotlin( "src/main/java/com/vayunmathur/example/Screen.kt", """ package com.vayunmathur.example class Screen """.trimIndent(), ), ).testModes(TestMode.DEFAULT).run().expectClean() } private fun routeFile(): TestFile = kotlin( "src/main/java/com/vayunmathur/example/Route.kt", """ package com.vayunmathur.example import com.vayunmathur.library.util.NavKey sealed interface Route : NavKey { data object Feed : Route data object Story : Route } """.trimIndent(), ) private fun previews(vararg names: String): TestFile { val body = names.mapIndexed { index, name -> """ @PreviewTest @Preview(name = "$name", group = "phone") @Composable fun Preview${index + 1}Shot$index() {} """.trimIndent() }.joinToString("\n") return kotlin( "src/screenshotTest/kotlin/com/vayunmathur/example/ui/MetadataPreviews.kt", """ package com.vayunmathur.example.ui import androidx.compose.runtime.Composable import androidx.compose.ui.tooling.preview.Preview import com.android.tools.screenshot.PreviewTest class MetadataPreviews { $body } """.trimIndent(), ) } companion object { private val NAVKEY_STUB: TestFile = kotlin( "src/main/java/com/vayunmathur/library/util/NavKey.kt", """ package com.vayunmathur.library.util interface NavKey """.trimIndent(), ) private val COMPOSABLE_STUB: TestFile = kotlin( "src/androidx/compose/runtime/Composable.kt", """ package androidx.compose.runtime annotation class Composable """.trimIndent(), ) private val PREVIEWTEST_STUB: TestFile = kotlin( "src/com/android/tools/screenshot/PreviewTest.kt", """ package com.android.tools.screenshot annotation class PreviewTest """.trimIndent(), ) private val PREVIEW_STUBS: TestFile = kotlin( "src/androidx/compose/ui/tooling/preview/Preview.kt", """ package androidx.compose.ui.tooling.preview annotation class Preview(val name: String = "", val group: String = "") """.trimIndent(), ) } } @RunWith(JUnit4::class) class ScreenshotCoverageMarkerFormatTest : LintDetectorTest() { override fun getDetector(): Detector = ExceptionMarkerFormatDetector() override fun getIssues(): List = listOf(ExceptionMarkerFormatDetector.ISSUE) @Test fun wellFormedScreenshotMarkerIsClean() { lint().files(kotlin( """ package com.vayunmathur.example // SCREENSHOT COVERAGE EXCEPTION: Story: live call surface, cannot render in Layoutlib sealed interface Route """.trimIndent(), )).testModes(TestMode.DEFAULT).run().expectClean() } @Test fun screenshotMarkerWithoutRouteAndReasonIsFlagged() { lint().files(kotlin( """ package com.vayunmathur.example // SCREENSHOT COVERAGE EXCEPTION: sealed interface Route """.trimIndent(), )).testModes(TestMode.DEFAULT).run().expectErrorCount(1) } @Test fun screenshotMarkerWithoutColonIsFlagged() { lint().files(kotlin( """ package com.vayunmathur.example // SCREENSHOT COVERAGE EXCEPTION Story: no colon after the marker sealed interface Route """.trimIndent(), )).testModes(TestMode.DEFAULT).run().expectErrorCount(1) } }