Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / lint-rules / src / test / java / com / vayunmathur / lint / ScaffoldLoadingDetectorTest.kt
ScaffoldLoadingDetectorTest.kt240 lines · 9.6 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.TestFiles.kotlin |
| 6 | import com.android.tools.lint.detector.api.Detector |
| 7 | import com.android.tools.lint.detector.api.Issue |
| 8 | import org.junit.Test |
| 9 | import org.junit.runner.RunWith |
| 10 | import org.junit.runners.JUnit4 |
| 11 | |
| 12 | @RunWith(JUnit4::class) |
| 13 | class ScaffoldLoadingDetectorTest : LintDetectorTest() { |
| 14 | |
| 15 | override fun getDetector(): Detector = ScaffoldLoadingDetector() |
| 16 | |
| 17 | override fun getIssues(): List<Issue> = listOf(ScaffoldLoadingDetector.ISSUE) |
| 18 | |
| 19 | @Test |
| 20 | fun loadingStateInsideAppScaffoldIsFlagged() { |
| 21 | lint().files(UI_STUBS, MODIFIER_STUB, LAYOUT_STUBS, COMPOSABLE_STUB, kotlin( |
| 22 | """ |
| 23 | package com.vayunmathur.example.ui |
| 24 | import androidx.compose.runtime.Composable |
| 25 | import com.vayunmathur.library.ui.AppScaffold |
| 26 | import com.vayunmathur.library.ui.LoadingState |
| 27 | @Composable fun Screen(loading: Boolean) { |
| 28 | AppScaffold(title = "T") { |
| 29 | if (loading) LoadingState() |
| 30 | } |
| 31 | } |
| 32 | """.trimIndent(), |
| 33 | )).run().expectErrorCount(1) |
| 34 | } |
| 35 | |
| 36 | @Test |
| 37 | fun fullSizeIndicatorInsideAppScaffoldIsFlagged() { |
| 38 | lint().files(UI_STUBS, MODIFIER_STUB, LAYOUT_STUBS, COMPOSABLE_STUB, kotlin( |
| 39 | """ |
| 40 | package com.vayunmathur.example.ui |
| 41 | import androidx.compose.foundation.layout.Box |
| 42 | import androidx.compose.runtime.Composable |
| 43 | import androidx.compose.ui.Modifier |
| 44 | import com.vayunmathur.library.ui.AppScaffold |
| 45 | import com.vayunmathur.library.ui.LoadingIndicator |
| 46 | @Composable fun Screen(loading: Boolean) { |
| 47 | AppScaffold(title = "T") { |
| 48 | if (loading) Box(Modifier.fillMaxSize()) { LoadingIndicator() } |
| 49 | } |
| 50 | } |
| 51 | """.trimIndent(), |
| 52 | )).run().expectErrorCount(1) |
| 53 | } |
| 54 | |
| 55 | @Test |
| 56 | fun scaffoldRefreshParameterIsClean() { |
| 57 | lint().files(UI_STUBS, MODIFIER_STUB, LAYOUT_STUBS, COMPOSABLE_STUB, kotlin( |
| 58 | """ |
| 59 | package com.vayunmathur.example.ui |
| 60 | import androidx.compose.foundation.layout.Box |
| 61 | import androidx.compose.runtime.Composable |
| 62 | import androidx.compose.ui.Modifier |
| 63 | import com.vayunmathur.library.ui.AppScaffold |
| 64 | @Composable fun Screen(loading: Boolean) { |
| 65 | AppScaffold(title = "T", isRefreshing = loading, onRefresh = {}) { |
| 66 | Box(Modifier.fillMaxSize()) { } |
| 67 | } |
| 68 | } |
| 69 | """.trimIndent(), |
| 70 | )).run().expectClean() |
| 71 | } |
| 72 | |
| 73 | @Test |
| 74 | fun sectionLocalIndicatorIsFlagged() { |
| 75 | lint().files(UI_STUBS, MODIFIER_STUB, LAYOUT_STUBS, COMPOSABLE_STUB, kotlin( |
| 76 | """ |
| 77 | package com.vayunmathur.example.ui |
| 78 | import androidx.compose.foundation.layout.Box |
| 79 | import androidx.compose.runtime.Composable |
| 80 | import androidx.compose.ui.Modifier |
| 81 | import com.vayunmathur.library.ui.AppScaffold |
| 82 | import com.vayunmathur.library.ui.LoadingIndicator |
| 83 | @Composable fun Screen(loading: Boolean) { |
| 84 | AppScaffold(title = "T") { |
| 85 | if (loading) Box(Modifier.fillMaxWidth()) { LoadingIndicator() } |
| 86 | } |
| 87 | } |
| 88 | """.trimIndent(), |
| 89 | )).run().expectErrorCount(1) |
| 90 | } |
| 91 | |
| 92 | @Test |
| 93 | fun sizedDeterminateAndButtonIndicatorsAreFlagged() { |
| 94 | lint().files(UI_STUBS, MODIFIER_STUB, LAYOUT_STUBS, COMPOSABLE_STUB, kotlin( |
| 95 | """ |
| 96 | package com.vayunmathur.example.ui |
| 97 | import androidx.compose.runtime.Composable |
| 98 | import androidx.compose.ui.Modifier |
| 99 | import com.vayunmathur.library.ui.AppScaffold |
| 100 | import com.vayunmathur.library.ui.Button |
| 101 | import com.vayunmathur.library.ui.CircularProgressIndicator |
| 102 | import com.vayunmathur.library.ui.LoadingIndicator |
| 103 | @Composable fun Screen(saving: Boolean) { |
| 104 | AppScaffold(title = "T") { |
| 105 | if (saving) CircularProgressIndicator(Modifier.size(16)) |
| 106 | LoadingIndicator(progress = { 0.5f }) |
| 107 | Button(onClick = {}) { CircularProgressIndicator() } |
| 108 | } |
| 109 | } |
| 110 | """.trimIndent(), |
| 111 | )).run().expectErrorCount(3) |
| 112 | } |
| 113 | |
| 114 | @Test |
| 115 | fun standaloneIndicatorBeforeScaffoldIsFlagged() { |
| 116 | lint().files(UI_STUBS, MODIFIER_STUB, LAYOUT_STUBS, COMPOSABLE_STUB, kotlin( |
| 117 | """ |
| 118 | package com.vayunmathur.example.ui |
| 119 | import androidx.compose.foundation.layout.Box |
| 120 | import androidx.compose.runtime.Composable |
| 121 | import androidx.compose.ui.Modifier |
| 122 | import com.vayunmathur.library.ui.AppScaffold |
| 123 | import com.vayunmathur.library.ui.LoadingIndicator |
| 124 | @Composable fun Screen(photo: String?) { |
| 125 | if (photo == null) { |
| 126 | Box(Modifier.fillMaxSize()) { LoadingIndicator() } |
| 127 | return |
| 128 | } |
| 129 | AppScaffold(title = "T") { Box(Modifier.fillMaxSize()) { } } |
| 130 | } |
| 131 | """.trimIndent(), |
| 132 | )).run().expectErrorCount(1) |
| 133 | } |
| 134 | |
| 135 | @Test |
| 136 | fun handRolledPullToRefreshIsFlagged() { |
| 137 | lint().files(UI_STUBS, MODIFIER_STUB, LAYOUT_STUBS, COMPOSABLE_STUB, kotlin( |
| 138 | """ |
| 139 | package com.vayunmathur.example.ui |
| 140 | import androidx.compose.runtime.Composable |
| 141 | import com.vayunmathur.library.ui.AppPullToRefreshBox |
| 142 | @Composable fun Screen(loading: Boolean) { |
| 143 | AppPullToRefreshBox(isRefreshing = loading, onRefresh = {}) { } |
| 144 | } |
| 145 | """.trimIndent(), |
| 146 | )).run().expectErrorCount(1) |
| 147 | } |
| 148 | |
| 149 | @Test |
| 150 | fun determinateBarIsClean() { |
| 151 | lint().files(UI_STUBS, MODIFIER_STUB, LAYOUT_STUBS, COMPOSABLE_STUB, kotlin( |
| 152 | """ |
| 153 | package com.vayunmathur.example.ui |
| 154 | import androidx.compose.runtime.Composable |
| 155 | import com.vayunmathur.library.ui.LinearProgressIndicator |
| 156 | @Composable fun Screen() { |
| 157 | LinearProgressIndicator(progress = { 0.5f }) |
| 158 | } |
| 159 | """.trimIndent(), |
| 160 | )).run().expectClean() |
| 161 | } |
| 162 | |
| 163 | @Test |
| 164 | fun exceptionMarkerOptsOut() { |
| 165 | lint().files(UI_STUBS, MODIFIER_STUB, LAYOUT_STUBS, COMPOSABLE_STUB, kotlin( |
| 166 | """ |
| 167 | package com.vayunmathur.example.ui |
| 168 | // SCAFFOLD LOADING EXCEPTION: pager pages own their loading chrome |
| 169 | import androidx.compose.runtime.Composable |
| 170 | import com.vayunmathur.library.ui.AppScaffold |
| 171 | import com.vayunmathur.library.ui.LoadingState |
| 172 | @Composable fun Screen(loading: Boolean) { |
| 173 | AppScaffold(title = "T") { |
| 174 | if (loading) LoadingState() |
| 175 | } |
| 176 | } |
| 177 | """.trimIndent(), |
| 178 | )).run().expectClean() |
| 179 | } |
| 180 | |
| 181 | companion object { |
| 182 | private val COMPOSABLE_STUB: TestFile = kotlin( |
| 183 | "src/androidx/compose/runtime/Composable.kt", |
| 184 | """ |
| 185 | package androidx.compose.runtime |
| 186 | annotation class Composable |
| 187 | """.trimIndent(), |
| 188 | ) |
| 189 | |
| 190 | private val MODIFIER_STUB: TestFile = kotlin( |
| 191 | "src/androidx/compose/ui/Modifier.kt", |
| 192 | """ |
| 193 | package androidx.compose.ui |
| 194 | open class Modifier { |
| 195 | fun fillMaxSize(): Modifier = this |
| 196 | fun fillMaxWidth(): Modifier = this |
| 197 | fun size(dp: Int): Modifier = this |
| 198 | fun padding(values: Any): Modifier = this |
| 199 | companion object : Modifier() |
| 200 | } |
| 201 | """.trimIndent(), |
| 202 | ) |
| 203 | |
| 204 | private val LAYOUT_STUBS: TestFile = kotlin( |
| 205 | "src/androidx/compose/foundation/layout/Layout.kt", |
| 206 | """ |
| 207 | package androidx.compose.foundation.layout |
| 208 | import androidx.compose.runtime.Composable |
| 209 | import androidx.compose.ui.Modifier |
| 210 | class PaddingValues |
| 211 | @Composable fun Box(modifier: Modifier = Modifier, content: @Composable () -> Unit) {} |
| 212 | """.trimIndent(), |
| 213 | ) |
| 214 | |
| 215 | private val UI_STUBS: TestFile = kotlin( |
| 216 | "src/com/vayunmathur/library/ui/UiStubs.kt", |
| 217 | """ |
| 218 | package com.vayunmathur.library.ui |
| 219 | import androidx.compose.foundation.layout.PaddingValues |
| 220 | import androidx.compose.runtime.Composable |
| 221 | import androidx.compose.ui.Modifier |
| 222 | @Composable fun AppScaffold( |
| 223 | title: String, |
| 224 | isRefreshing: Boolean = false, |
| 225 | onRefresh: (() -> Unit)? = null, |
| 226 | refreshEnabled: Boolean = true, |
| 227 | content: @Composable (PaddingValues) -> Unit, |
| 228 | ) {} |
| 229 | @Composable fun AppPullToRefreshBox(isRefreshing: Boolean, onRefresh: () -> Unit, content: @Composable () -> Unit) {} |
| 230 | @Composable fun LinearProgressIndicator(progress: () -> Float) {} |
| 231 | @Composable fun LoadingState(modifier: Modifier = Modifier, message: String? = null) {} |
| 232 | @Composable fun LoadingIndicator(modifier: Modifier = Modifier) {} |
| 233 | @Composable fun LoadingIndicator(progress: () -> Float, modifier: Modifier = Modifier) {} |
| 234 | @Composable fun CircularProgressIndicator(modifier: Modifier = Modifier) {} |
| 235 | @Composable fun CircularProgressIndicator(progress: () -> Float, modifier: Modifier = Modifier) {} |
| 236 | @Composable fun Button(onClick: () -> Unit, content: @Composable () -> Unit) {} |
| 237 | """.trimIndent(), |
| 238 | ) |
| 239 | } |
| 240 | } |