Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
translate ▾

Modern-Apps.git / lint-rules / src / test / java / com / vayunmathur / lint / DialogAsRouteDetectorTest.kt

DialogAsRouteDetectorTest.kt307 lines · 12.6 KiBRaw
1package com.vayunmathur.lint
2
3import com.android.tools.lint.checks.infrastructure.LintDetectorTest
4import com.android.tools.lint.checks.infrastructure.TestFile
5import com.android.tools.lint.checks.infrastructure.TestFiles.kotlin
6import com.android.tools.lint.checks.infrastructure.TestMode
7import com.android.tools.lint.detector.api.Detector
8import com.android.tools.lint.detector.api.Issue
9import org.junit.Test
10import org.junit.runner.RunWith
11import org.junit.runners.JUnit4
12
13@RunWith(JUnit4::class)
14class DialogAsRouteDetectorTest : LintDetectorTest() {
15
16 override fun getDetector(): Detector = DialogAsRouteDetector()
17
18 override fun getIssues(): List<Issue> = listOf(DialogAsRouteDetector.ISSUE)
19
20 @Test
21 fun gatedAlertDialogInScreenIsFlagged() {
22 lint().files(UI_STUBS, STATE_STUBS, kotlin(
23 "src/main/java/com/vayunmathur/example/ui/ListScreen.kt",
24 """
25 package com.vayunmathur.example.ui
26 import androidx.compose.runtime.Composable
27 import androidx.compose.runtime.mutableStateOf
28 import androidx.compose.runtime.remember
29 import com.vayunmathur.library.ui.AlertDialog
30 @Composable fun ListScreen() {
31 var showDeleteDialog by remember { mutableStateOf(false) }
32 if (showDeleteDialog) {
33 AlertDialog(onDismissRequest = { showDeleteDialog = false })
34 }
35 }
36 """.trimIndent(),
37 )).testModes(TestMode.DEFAULT).run().expectErrorCount(1)
38 }
39
40 @Test
41 fun nullablePendingStateLetDialogIsFlagged() {
42 lint().files(UI_STUBS, STATE_STUBS, kotlin(
43 "src/main/java/com/vayunmathur/example/ui/ListScreen.kt",
44 """
45 package com.vayunmathur.example.ui
46 import androidx.compose.runtime.Composable
47 import androidx.compose.runtime.mutableStateOf
48 import androidx.compose.runtime.remember
49 import com.vayunmathur.library.ui.ConfirmDialog
50 @Composable fun ListScreen() {
51 var pendingName by remember { mutableStateOf<String?>(null) }
52 pendingName?.let { name ->
53 ConfirmDialog(title = name, confirmLabel = "Delete", onConfirm = {}, onDismiss = {})
54 }
55 }
56 """.trimIndent(),
57 )).testModes(TestMode.DEFAULT).run().expectErrorCount(1)
58 }
59
60 @Test
61 fun customDialogComposableGatedByStateIsFlagged() {
62 lint().files(UI_STUBS, STATE_STUBS, kotlin(
63 "src/main/java/com/vayunmathur/example/ui/ListScreen.kt",
64 """
65 package com.vayunmathur.example.ui
66 import androidx.compose.runtime.Composable
67 import androidx.compose.runtime.mutableStateOf
68 import androidx.compose.runtime.rememberSaveable
69 @Composable fun ListScreen() {
70 var renaming by rememberSaveable { mutableStateOf<String?>(null) }
71 renaming?.let { tag ->
72 RenameTagDialog(tag = tag, onDismiss = { renaming = null })
73 }
74 }
75 @Composable fun RenameTagDialog(tag: String, onDismiss: () -> Unit) {}
76 """.trimIndent(),
77 )).testModes(TestMode.DEFAULT).run().expectErrorCount(1)
78 }
79
80 @Test
81 fun gatedModalBottomSheetIsFlagged() {
82 lint().files(UI_STUBS, STATE_STUBS, kotlin(
83 "src/main/java/com/vayunmathur/example/ui/ListScreen.kt",
84 """
85 package com.vayunmathur.example.ui
86 import androidx.compose.runtime.Composable
87 import androidx.compose.runtime.mutableStateOf
88 import androidx.compose.runtime.remember
89 import com.vayunmathur.library.ui.ModalBottomSheet
90 @Composable fun ListScreen() {
91 var showSheet by remember { mutableStateOf(false) }
92 if (showSheet) {
93 ModalBottomSheet(onDismissRequest = { showSheet = false }) {}
94 }
95 }
96 """.trimIndent(),
97 )).testModes(TestMode.DEFAULT).run().expectErrorCount(1)
98 }
99
100 @Test
101 fun dialogInsideSameFileHelperInvokedUnderGateIsFlagged() {
102 // The helper's AlertDialog and its gated call site are both reported:
103 // the definition does not become legal by moving one brace away.
104 lint().files(UI_STUBS, STATE_STUBS, kotlin(
105 "src/main/java/com/vayunmathur/example/ui/EditScreen.kt",
106 """
107 package com.vayunmathur.example.ui
108 import androidx.compose.runtime.Composable
109 import androidx.compose.runtime.mutableStateOf
110 import androidx.compose.runtime.remember
111 import com.vayunmathur.library.ui.AlertDialog
112 @Composable fun EditScreen() {
113 var showCustomDialog by remember { mutableStateOf(false) }
114 if (showCustomDialog) {
115 CustomReminderDialog(onDismiss = { showCustomDialog = false })
116 }
117 }
118 @Composable private fun CustomReminderDialog(onDismiss: () -> Unit) {
119 AlertDialog(onDismissRequest = onDismiss)
120 }
121 """.trimIndent(),
122 )).testModes(TestMode.DEFAULT).run().expectErrorCount(2)
123 }
124
125 @Test
126 fun unconditionalDialogInRouteEntryIsClean() {
127 lint().files(UI_STUBS, STATE_STUBS, kotlin(
128 "src/main/java/com/vayunmathur/example/ui/DialogEntries.kt",
129 """
130 package com.vayunmathur.example.ui
131 import androidx.compose.runtime.Composable
132 import com.vayunmathur.library.ui.ConfirmDialog
133 @Composable fun DeleteDialogHost(onConfirm: () -> Unit, onDismiss: () -> Unit) {
134 ConfirmDialog(title = "Delete?", confirmLabel = "Delete", onConfirm = onConfirm, onDismiss = onDismiss)
135 }
136 """.trimIndent(),
137 )).testModes(TestMode.DEFAULT).run().expectClean()
138 }
139
140 @Test
141 fun dialogDefinitionUnderDialogsPackageIsClean() {
142 lint().files(UI_STUBS, STATE_STUBS, kotlin(
143 "src/main/java/com/vayunmathur/example/ui/dialogs/RenameDialog.kt",
144 """
145 package com.vayunmathur.example.ui.dialogs
146 import androidx.compose.runtime.Composable
147 import androidx.compose.runtime.mutableStateOf
148 import androidx.compose.runtime.remember
149 import com.vayunmathur.library.ui.AlertDialog
150 @Composable fun RenameDialog(onDismiss: () -> Unit) {
151 var showHelp by remember { mutableStateOf(false) }
152 if (showHelp) {
153 AlertDialog(onDismissRequest = { showHelp = false })
154 }
155 AlertDialog(onDismissRequest = onDismiss)
156 }
157 """.trimIndent(),
158 )).testModes(TestMode.DEFAULT).run().expectClean()
159 }
160
161 @Test
162 fun routeConstructionAndUnconditionalContentCallAreClean() {
163 lint().files(UI_STUBS, STATE_STUBS, kotlin(
164 "src/main/java/com/vayunmathur/example/ui/ListScreen.kt",
165 """
166 package com.vayunmathur.example.ui
167 import androidx.compose.runtime.Composable
168 import androidx.compose.runtime.mutableStateOf
169 import androidx.compose.runtime.remember
170 @Composable fun ListScreen(openDialog: () -> Unit) {
171 var query by remember { mutableStateOf("") }
172 if (query.isNotEmpty()) {
173 HelperText(query)
174 }
175 }
176 @Composable fun HelperText(text: String) {}
177 """.trimIndent(),
178 )).testModes(TestMode.DEFAULT).run().expectClean()
179 }
180
181 @Test
182 fun exceptionMarkerOptsOut() {
183 lint().files(UI_STUBS, STATE_STUBS, kotlin(
184 "src/main/java/com/vayunmathur/example/ui/ListScreen.kt",
185 """
186 package com.vayunmathur.example.ui
187 // DIALOG LOCAL EXCEPTION: multi-step transfer flow; route conversion is a separate change
188 import androidx.compose.runtime.Composable
189 import androidx.compose.runtime.mutableStateOf
190 import androidx.compose.runtime.remember
191 import com.vayunmathur.library.ui.AlertDialog
192 @Composable fun ListScreen() {
193 var showDialog by remember { mutableStateOf(false) }
194 if (showDialog) {
195 AlertDialog(onDismissRequest = { showDialog = false })
196 }
197 }
198 """.trimIndent(),
199 )).testModes(TestMode.DEFAULT).run().expectClean()
200 }
201
202 @Test
203 fun libraryPackageIsExempt() {
204 lint().files(UI_STUBS, STATE_STUBS, kotlin(
205 "src/main/java/com/vayunmathur/library/ui/Internal.kt",
206 """
207 package com.vayunmathur.library.ui
208 import androidx.compose.runtime.Composable
209 import androidx.compose.runtime.mutableStateOf
210 import androidx.compose.runtime.remember
211 @Composable fun Internal() {
212 var showDialog by remember { mutableStateOf(false) }
213 if (showDialog) {
214 AlertDialog(onDismissRequest = { showDialog = false })
215 }
216 }
217 """.trimIndent(),
218 )).testModes(TestMode.DEFAULT).run().expectClean()
219 }
220
221 @Test
222 fun screenshotTestSourceIsSkipped() {
223 lint().files(UI_STUBS, STATE_STUBS, kotlin(
224 "src/screenshotTest/kotlin/com/vayunmathur/example/ui/Previews.kt",
225 """
226 package com.vayunmathur.example.ui
227 import androidx.compose.runtime.Composable
228 import androidx.compose.runtime.mutableStateOf
229 import androidx.compose.runtime.remember
230 import com.vayunmathur.library.ui.AlertDialog
231 @Composable fun PreviewDialog() {
232 var showDialog by remember { mutableStateOf(true) }
233 if (showDialog) {
234 AlertDialog(onDismissRequest = { showDialog = false })
235 }
236 }
237 """.trimIndent(),
238 )).testModes(TestMode.DEFAULT).run().expectClean()
239 }
240
241 companion object {
242 private val STATE_STUBS: TestFile = kotlin(
243 "src/androidx/compose/runtime/StateStubs.kt",
244 """
245 package androidx.compose.runtime
246 annotation class Composable
247 interface State<T> { val value: T }
248 class MutableState<T>(override var value: T) : State<T>
249 fun <T> mutableStateOf(value: T): MutableState<T> = MutableState(value)
250 fun <T> remember(calculation: () -> T): T = calculation()
251 fun <T> rememberSaveable(calculation: () -> T): T = calculation()
252 """.trimIndent(),
253 )
254
255 private val UI_STUBS: TestFile = kotlin(
256 "src/com/vayunmathur/library/ui/UiStubs.kt",
257 """
258 package com.vayunmathur.library.ui
259 import androidx.compose.runtime.Composable
260 @Composable fun AlertDialog(onDismissRequest: () -> Unit) {}
261 @Composable fun ConfirmDialog(title: String, confirmLabel: String, onConfirm: () -> Unit, onDismiss: () -> Unit) {}
262 @Composable fun ModalBottomSheet(onDismissRequest: () -> Unit, content: @Composable () -> Unit) {}
263 """.trimIndent(),
264 )
265 }
266}
267
268@RunWith(JUnit4::class)
269class DialogLocalMarkerFormatTest : LintDetectorTest() {
270
271 override fun getDetector(): Detector = ExceptionMarkerFormatDetector()
272
273 override fun getIssues(): List<Issue> = listOf(ExceptionMarkerFormatDetector.ISSUE)
274
275 @Test
276 fun wellFormedDialogMarkerIsClean() {
277 lint().files(kotlin(
278 """
279 package com.vayunmathur.example
280 // DIALOG LOCAL EXCEPTION: multi-step flow, no route shape yet
281 class Screen
282 """.trimIndent(),
283 )).testModes(TestMode.DEFAULT).run().expectClean()
284 }
285
286 @Test
287 fun dialogMarkerWithoutReasonIsFlagged() {
288 lint().files(kotlin(
289 """
290 package com.vayunmathur.example
291 // DIALOG LOCAL EXCEPTION:
292 class Screen
293 """.trimIndent(),
294 )).testModes(TestMode.DEFAULT).run().expectErrorCount(1)
295 }
296
297 @Test
298 fun dialogMarkerWithoutColonIsFlagged() {
299 lint().files(kotlin(
300 """
301 package com.vayunmathur.example
302 // DIALOG LOCAL EXCEPTION multi-step flow
303 class Screen
304 """.trimIndent(),
305 )).testModes(TestMode.DEFAULT).run().expectErrorCount(1)
306 }
307}