Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / lint-rules / src / test / java / com / vayunmathur / lint / RouteScreenshotCoverageDetectorTest.kt
RouteScreenshotCoverageDetectorTest.kt238 lines · 8.4 KiBRaw
| 1 | package com.vayunmathur.lint |
| 2 | |
| 3 | import com.android.tools.lint.checks.infrastructure.LintDetectorTest |
| 4 | import com.android.tools.lint.checks.infrastructure.TestFile |
| 5 | import com.android.tools.lint.checks.infrastructure.TestMode |
| 6 | import com.android.tools.lint.checks.infrastructure.TestFiles.kotlin |
| 7 | import com.android.tools.lint.detector.api.Detector |
| 8 | import com.android.tools.lint.detector.api.Issue |
| 9 | import org.junit.Test |
| 10 | import org.junit.runner.RunWith |
| 11 | import org.junit.runners.JUnit4 |
| 12 | |
| 13 | @RunWith(JUnit4::class) |
| 14 | class RouteScreenshotCoverageDetectorTest : LintDetectorTest() { |
| 15 | |
| 16 | override fun getDetector(): Detector = RouteScreenshotCoverageDetector() |
| 17 | |
| 18 | override fun getIssues(): List<Issue> = listOf(RouteScreenshotCoverageDetector.ISSUE) |
| 19 | |
| 20 | @Test |
| 21 | fun allRoutesCoveredIsClean() { |
| 22 | lint().files(NAVKEY_STUB, PREVIEW_STUBS, COMPOSABLE_STUB, PREVIEWTEST_STUB, routeFile(), previews("1-feed", "2-story")).testModes(TestMode.DEFAULT).run().expectClean() |
| 23 | } |
| 24 | |
| 25 | @Test |
| 26 | fun unmappedRouteIsFlagged() { |
| 27 | lint().files(NAVKEY_STUB, PREVIEW_STUBS, COMPOSABLE_STUB, PREVIEWTEST_STUB, routeFile(), previews("1-feed")).testModes(TestMode.DEFAULT).run().expectErrorCount(1) |
| 28 | } |
| 29 | |
| 30 | @Test |
| 31 | fun variantSuffixStillCovers() { |
| 32 | lint().files(NAVKEY_STUB, PREVIEW_STUBS, COMPOSABLE_STUB, PREVIEWTEST_STUB, routeFile(), previews("1-feed", "2-story-expanded")).testModes(TestMode.DEFAULT).run().expectClean() |
| 33 | } |
| 34 | |
| 35 | @Test |
| 36 | fun longestRouteKeyWins() { |
| 37 | // `1-call-logs` must cover CallLogs, not Call: Call stays unmapped. |
| 38 | lint().files( |
| 39 | NAVKEY_STUB, PREVIEW_STUBS, COMPOSABLE_STUB, PREVIEWTEST_STUB, |
| 40 | kotlin( |
| 41 | "src/main/java/com/vayunmathur/example/Route.kt", |
| 42 | """ |
| 43 | package com.vayunmathur.example |
| 44 | import com.vayunmathur.library.util.NavKey |
| 45 | sealed interface Route : NavKey { |
| 46 | data object Call : Route |
| 47 | data object CallLogs : Route |
| 48 | } |
| 49 | """.trimIndent(), |
| 50 | ), |
| 51 | previews("1-call-logs"), |
| 52 | ).testModes(TestMode.DEFAULT).run().expectErrorCount(1) |
| 53 | } |
| 54 | |
| 55 | @Test |
| 56 | fun nestedRouteLeafNeedsItsOwnCapture() { |
| 57 | lint().files( |
| 58 | NAVKEY_STUB, PREVIEW_STUBS, COMPOSABLE_STUB, PREVIEWTEST_STUB, |
| 59 | kotlin( |
| 60 | "src/main/java/com/vayunmathur/example/Route.kt", |
| 61 | """ |
| 62 | package com.vayunmathur.example |
| 63 | import com.vayunmathur.library.util.NavKey |
| 64 | sealed interface Route : NavKey { |
| 65 | data object Feed : Route |
| 66 | sealed interface Registration : Route { |
| 67 | data object Number : Registration |
| 68 | data class Code(val cc: String) : Registration |
| 69 | } |
| 70 | } |
| 71 | """.trimIndent(), |
| 72 | ), |
| 73 | previews("1-feed", "2-registration-number"), |
| 74 | ).testModes(TestMode.DEFAULT).run().expectErrorCount(1) |
| 75 | } |
| 76 | |
| 77 | @Test |
| 78 | fun exceptionMarkerOptsRouteOut() { |
| 79 | lint().files( |
| 80 | NAVKEY_STUB, PREVIEW_STUBS, COMPOSABLE_STUB, PREVIEWTEST_STUB, |
| 81 | kotlin( |
| 82 | "src/main/java/com/vayunmathur/example/Route.kt", |
| 83 | """ |
| 84 | package com.vayunmathur.example |
| 85 | import com.vayunmathur.library.util.NavKey |
| 86 | // SCREENSHOT COVERAGE EXCEPTION: Story: live call surface, cannot render in Layoutlib |
| 87 | sealed interface Route : NavKey { |
| 88 | data object Feed : Route |
| 89 | data object Story : Route |
| 90 | } |
| 91 | """.trimIndent(), |
| 92 | ), |
| 93 | previews("1-feed"), |
| 94 | ).testModes(TestMode.DEFAULT).run().expectClean() |
| 95 | } |
| 96 | |
| 97 | @Test |
| 98 | fun exemptionNamingNoRouteIsFlagged() { |
| 99 | lint().files( |
| 100 | NAVKEY_STUB, PREVIEW_STUBS, COMPOSABLE_STUB, PREVIEWTEST_STUB, |
| 101 | kotlin( |
| 102 | "src/main/java/com/vayunmathur/example/Route.kt", |
| 103 | """ |
| 104 | package com.vayunmathur.example |
| 105 | import com.vayunmathur.library.util.NavKey |
| 106 | // SCREENSHOT COVERAGE EXCEPTION: Nope: not a route |
| 107 | sealed interface Route : NavKey { |
| 108 | data object Feed : Route |
| 109 | } |
| 110 | """.trimIndent(), |
| 111 | ), |
| 112 | previews("1-feed"), |
| 113 | ).testModes(TestMode.DEFAULT).run().expectErrorCount(1) |
| 114 | } |
| 115 | |
| 116 | @Test |
| 117 | fun noRouteIsClean() { |
| 118 | lint().files( |
| 119 | NAVKEY_STUB, PREVIEW_STUBS, COMPOSABLE_STUB, PREVIEWTEST_STUB, |
| 120 | kotlin( |
| 121 | "src/main/java/com/vayunmathur/example/Screen.kt", |
| 122 | """ |
| 123 | package com.vayunmathur.example |
| 124 | class Screen |
| 125 | """.trimIndent(), |
| 126 | ), |
| 127 | ).testModes(TestMode.DEFAULT).run().expectClean() |
| 128 | } |
| 129 | |
| 130 | private fun routeFile(): TestFile = kotlin( |
| 131 | "src/main/java/com/vayunmathur/example/Route.kt", |
| 132 | """ |
| 133 | package com.vayunmathur.example |
| 134 | import com.vayunmathur.library.util.NavKey |
| 135 | sealed interface Route : NavKey { |
| 136 | data object Feed : Route |
| 137 | data object Story : Route |
| 138 | } |
| 139 | """.trimIndent(), |
| 140 | ) |
| 141 | |
| 142 | private fun previews(vararg names: String): TestFile { |
| 143 | val body = names.mapIndexed { index, name -> |
| 144 | """ |
| 145 | @PreviewTest |
| 146 | @Preview(name = "$name", group = "phone") |
| 147 | @Composable fun Preview${index + 1}Shot$index() {} |
| 148 | """.trimIndent() |
| 149 | }.joinToString("\n") |
| 150 | return kotlin( |
| 151 | "src/screenshotTest/kotlin/com/vayunmathur/example/ui/MetadataPreviews.kt", |
| 152 | """ |
| 153 | package com.vayunmathur.example.ui |
| 154 | import androidx.compose.runtime.Composable |
| 155 | import androidx.compose.ui.tooling.preview.Preview |
| 156 | import com.android.tools.screenshot.PreviewTest |
| 157 | class MetadataPreviews { |
| 158 | $body |
| 159 | } |
| 160 | """.trimIndent(), |
| 161 | ) |
| 162 | } |
| 163 | |
| 164 | companion object { |
| 165 | private val NAVKEY_STUB: TestFile = kotlin( |
| 166 | "src/main/java/com/vayunmathur/library/util/NavKey.kt", |
| 167 | """ |
| 168 | package com.vayunmathur.library.util |
| 169 | interface NavKey |
| 170 | """.trimIndent(), |
| 171 | ) |
| 172 | |
| 173 | private val COMPOSABLE_STUB: TestFile = kotlin( |
| 174 | "src/androidx/compose/runtime/Composable.kt", |
| 175 | """ |
| 176 | package androidx.compose.runtime |
| 177 | annotation class Composable |
| 178 | """.trimIndent(), |
| 179 | ) |
| 180 | |
| 181 | private val PREVIEWTEST_STUB: TestFile = kotlin( |
| 182 | "src/com/android/tools/screenshot/PreviewTest.kt", |
| 183 | """ |
| 184 | package com.android.tools.screenshot |
| 185 | annotation class PreviewTest |
| 186 | """.trimIndent(), |
| 187 | ) |
| 188 | |
| 189 | private val PREVIEW_STUBS: TestFile = kotlin( |
| 190 | "src/androidx/compose/ui/tooling/preview/Preview.kt", |
| 191 | """ |
| 192 | package androidx.compose.ui.tooling.preview |
| 193 | annotation class Preview(val name: String = "", val group: String = "") |
| 194 | """.trimIndent(), |
| 195 | ) |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | @RunWith(JUnit4::class) |
| 200 | class ScreenshotCoverageMarkerFormatTest : LintDetectorTest() { |
| 201 | |
| 202 | override fun getDetector(): Detector = ExceptionMarkerFormatDetector() |
| 203 | |
| 204 | override fun getIssues(): List<Issue> = listOf(ExceptionMarkerFormatDetector.ISSUE) |
| 205 | |
| 206 | @Test |
| 207 | fun wellFormedScreenshotMarkerIsClean() { |
| 208 | lint().files(kotlin( |
| 209 | """ |
| 210 | package com.vayunmathur.example |
| 211 | // SCREENSHOT COVERAGE EXCEPTION: Story: live call surface, cannot render in Layoutlib |
| 212 | sealed interface Route |
| 213 | """.trimIndent(), |
| 214 | )).testModes(TestMode.DEFAULT).run().expectClean() |
| 215 | } |
| 216 | |
| 217 | @Test |
| 218 | fun screenshotMarkerWithoutRouteAndReasonIsFlagged() { |
| 219 | lint().files(kotlin( |
| 220 | """ |
| 221 | package com.vayunmathur.example |
| 222 | // SCREENSHOT COVERAGE EXCEPTION: |
| 223 | sealed interface Route |
| 224 | """.trimIndent(), |
| 225 | )).testModes(TestMode.DEFAULT).run().expectErrorCount(1) |
| 226 | } |
| 227 | |
| 228 | @Test |
| 229 | fun screenshotMarkerWithoutColonIsFlagged() { |
| 230 | lint().files(kotlin( |
| 231 | """ |
| 232 | package com.vayunmathur.example |
| 233 | // SCREENSHOT COVERAGE EXCEPTION Story: no colon after the marker |
| 234 | sealed interface Route |
| 235 | """.trimIndent(), |
| 236 | )).testModes(TestMode.DEFAULT).run().expectErrorCount(1) |
| 237 | } |
| 238 | } |