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

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

ScaffoldSearchDetectorTest.kt265 lines · 10.2 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.detector.api.Detector
7import com.android.tools.lint.detector.api.Issue
8import org.junit.Test
9import org.junit.runner.RunWith
10import org.junit.runners.JUnit4
11
12@RunWith(JUnit4::class)
13class ScaffoldSearchDetectorTest : LintDetectorTest() {
14
15 override fun getDetector(): Detector = ScaffoldSearchDetector()
16
17 override fun getIssues(): List<Issue> = listOf(ScaffoldSearchDetector.ISSUE)
18
19 @Test
20 fun commonSearchBarInContentIsFlagged() {
21 lint().files(UI_STUBS, COMPOSABLE_STUB, LAYOUT_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.CommonSearchBar
27 @Composable fun Screen(q: String) {
28 AppScaffold(title = "T") {
29 CommonSearchBar(value = q, onValueChange = {})
30 }
31 }
32 """.trimIndent(),
33 )).run().expectErrorCount(1)
34 }
35
36 @Test
37 fun commonSearchBarInCustomTopBarTitleIsFlagged() {
38 lint().files(UI_STUBS, COMPOSABLE_STUB, LAYOUT_STUB, kotlin(
39 """
40 package com.vayunmathur.example.ui
41 import androidx.compose.runtime.Composable
42 import com.vayunmathur.library.ui.CommonSearchBar
43 import com.vayunmathur.library.ui.LazyListScaffold
44 import com.vayunmathur.library.ui.TopAppBar
45 @Composable fun Screen(q: String) {
46 LazyListScaffold(topBar = {
47 TopAppBar(title = { CommonSearchBar(value = q, onValueChange = {}) })
48 }) { }
49 }
50 """.trimIndent(),
51 )).run().expectErrorCount(1)
52 }
53
54 @Test
55 fun scaffoldSearchParameterIsClean() {
56 lint().files(UI_STUBS, COMPOSABLE_STUB, LAYOUT_STUB, kotlin(
57 """
58 package com.vayunmathur.example.ui
59 import androidx.compose.runtime.Composable
60 import com.vayunmathur.library.ui.AppScaffold
61 import com.vayunmathur.library.ui.ScaffoldSearch
62 @Composable fun Screen(q: String) {
63 AppScaffold(
64 title = "T",
65 search = ScaffoldSearch(query = q, onQueryChange = {}),
66 ) { }
67 }
68 """.trimIndent(),
69 )).run().expectClean()
70 }
71
72 @Test
73 fun rawSearchBarAndInputFieldAreFlagged() {
74 lint().files(UI_STUBS, COMPOSABLE_STUB, LAYOUT_STUB, kotlin(
75 """
76 package com.vayunmathur.example.ui
77 import androidx.compose.runtime.Composable
78 import com.vayunmathur.library.ui.SearchBar
79 import com.vayunmathur.library.ui.SearchBarInputField
80 @Composable fun Screen(q: String) {
81 SearchBar(inputField = {}, expanded = false, onExpandedChange = {}) { }
82 SearchBarInputField(query = q, onQueryChange = {}, onSearch = {}, expanded = false, onExpandedChange = {})
83 }
84 """.trimIndent(),
85 )).run().expectErrorCount(2)
86 }
87
88 @Test
89 fun searchTextFieldByLiteralIsFlagged() {
90 lint().files(UI_STUBS, COMPOSABLE_STUB, LAYOUT_STUB, kotlin(
91 """
92 package com.vayunmathur.example.ui
93 import androidx.compose.runtime.Composable
94 import com.vayunmathur.library.ui.OutlinedTextField
95 import com.vayunmathur.library.ui.Text
96 @Composable fun Screen(q: String) {
97 OutlinedTextField(value = q, onValueChange = {}, label = { Text("Search stars") })
98 }
99 """.trimIndent(),
100 )).run().expectErrorCount(1)
101 }
102
103 @Test
104 fun searchingTextFieldByLiteralIsFlagged() {
105 lint().files(UI_STUBS, COMPOSABLE_STUB, LAYOUT_STUB, kotlin(
106 """
107 package com.vayunmathur.example.ui
108 import androidx.compose.runtime.Composable
109 import com.vayunmathur.library.ui.OutlinedTextField
110 import com.vayunmathur.library.ui.Text
111 @Composable fun Screen(q: String) {
112 OutlinedTextField(value = q, onValueChange = {}, label = { Text("Searching stars") })
113 }
114 """.trimIndent(),
115 )).run().expectErrorCount(1)
116 }
117
118 @Test
119 fun searchTextFieldByResourceIsFlagged() {
120 lint().files(UI_STUBS, COMPOSABLE_STUB, LAYOUT_STUB, kotlin(
121 """
122 package com.vayunmathur.example.ui
123 import androidx.compose.runtime.Composable
124 import com.vayunmathur.library.ui.OutlinedTextField
125 import com.vayunmathur.library.ui.Text
126 @Composable fun Screen(q: String) {
127 OutlinedTextField(
128 value = q,
129 onValueChange = {},
130 label = { Text(com.vayunmathur.example.R.string.search_stars) },
131 )
132 }
133 """.trimIndent(),
134 )).run().expectErrorCount(1)
135 }
136
137 @Test
138 fun ordinaryFormFieldIsClean() {
139 lint().files(UI_STUBS, COMPOSABLE_STUB, LAYOUT_STUB, kotlin(
140 """
141 package com.vayunmathur.example.ui
142 import androidx.compose.runtime.Composable
143 import com.vayunmathur.library.ui.OutlinedTextField
144 import com.vayunmathur.library.ui.Text
145 @Composable fun Screen(q: String) {
146 OutlinedTextField(value = q, onValueChange = {}, label = { Text("Email address") })
147 }
148 """.trimIndent(),
149 )).run().expectClean()
150 }
151
152 @Test
153 fun researchFormFieldIsClean() {
154 lint().files(UI_STUBS, COMPOSABLE_STUB, LAYOUT_STUB, kotlin(
155 """
156 package com.vayunmathur.example.ui
157 import androidx.compose.runtime.Composable
158 import com.vayunmathur.library.ui.OutlinedTextField
159 import com.vayunmathur.library.ui.Text
160 @Composable fun Screen(q: String) {
161 OutlinedTextField(value = q, onValueChange = {}, label = { Text("Research topic") })
162 }
163 """.trimIndent(),
164 )).run().expectClean()
165 }
166
167 @Test
168 fun researchResourceFormFieldIsClean() {
169 lint().files(UI_STUBS, COMPOSABLE_STUB, LAYOUT_STUB, kotlin(
170 """
171 package com.vayunmathur.example.ui
172 import androidx.compose.runtime.Composable
173 import com.vayunmathur.library.ui.OutlinedTextField
174 import com.vayunmathur.library.ui.Text
175 @Composable fun Screen(q: String) {
176 OutlinedTextField(
177 value = q,
178 onValueChange = {},
179 label = { Text(com.vayunmathur.example.R.string.research_topic) },
180 )
181 }
182 """.trimIndent(),
183 )).run().expectClean()
184 }
185
186 @Test
187 fun exceptionMarkerOptsOut() {
188 lint().files(UI_STUBS, COMPOSABLE_STUB, LAYOUT_STUB, kotlin(
189 """
190 package com.vayunmathur.example.ui
191 // SCAFFOLD SEARCH EXCEPTION: dialog-local filter, no scaffold host
192 import androidx.compose.runtime.Composable
193 import com.vayunmathur.library.ui.CommonSearchBar
194 @Composable fun Picker(q: String) {
195 CommonSearchBar(value = q, onValueChange = {})
196 }
197 """.trimIndent(),
198 )).run().expectClean()
199 }
200
201 @Test
202 fun libraryPackageIsExempt() {
203 lint().files(UI_STUBS, COMPOSABLE_STUB, LAYOUT_STUB, kotlin(
204 """
205 package com.vayunmathur.library.ui
206 import androidx.compose.runtime.Composable
207 @Composable fun Internal(q: String) {
208 CommonSearchBar(value = q, onValueChange = {})
209 }
210 """.trimIndent(),
211 )).run().expectClean()
212 }
213
214 companion object {
215 private val COMPOSABLE_STUB: TestFile = kotlin(
216 "src/androidx/compose/runtime/Composable.kt",
217 """
218 package androidx.compose.runtime
219 annotation class Composable
220 """.trimIndent(),
221 )
222
223 private val LAYOUT_STUB: TestFile = kotlin(
224 "src/androidx/compose/foundation/layout/Layout.kt",
225 """
226 package androidx.compose.foundation.layout
227 class PaddingValues
228 """.trimIndent(),
229 )
230
231 private val R_STUB: TestFile = kotlin(
232 "src/com/vayunmathur/example/R.kt",
233 """
234 package com.vayunmathur.example
235 object R { object string { const val search_stars: Int = 1 } }
236 """.trimIndent(),
237 )
238
239 private val UI_STUBS: TestFile = kotlin(
240 "src/com/vayunmathur/library/ui/UiStubs.kt",
241 """
242 package com.vayunmathur.library.ui
243 import androidx.compose.foundation.layout.PaddingValues
244 import androidx.compose.runtime.Composable
245 class ScaffoldSearch(val query: String, val onQueryChange: (String) -> Unit, val placeholder: String = "Search")
246 @Composable fun AppScaffold(
247 title: String,
248 search: ScaffoldSearch? = null,
249 content: @Composable (PaddingValues) -> Unit,
250 ) {}
251 @Composable fun LazyListScaffold(
252 topBar: @Composable () -> Unit = {},
253 content: () -> Unit,
254 ) {}
255 @Composable fun TopAppBar(title: @Composable () -> Unit) {}
256 @Composable fun CommonSearchBar(value: String, onValueChange: (String) -> Unit) {}
257 @Composable fun SearchBar(inputField: @Composable () -> Unit, expanded: Boolean, onExpandedChange: (Boolean) -> Unit, content: @Composable () -> Unit) {}
258 @Composable fun SearchBarInputField(query: String, onQueryChange: (String) -> Unit, onSearch: (String) -> Unit, expanded: Boolean, onExpandedChange: (Boolean) -> Unit) {}
259 @Composable fun OutlinedTextField(value: String, onValueChange: (String) -> Unit, label: @Composable () -> Unit) {}
260 @Composable fun Text(text: String) {}
261 @Composable fun Text(resId: Int) {}
262 """.trimIndent(),
263 )
264 }
265}