1 /* 2 * Copyright (C) 2024 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.internal.systemui.lint 18 19 import com.android.tools.lint.checks.infrastructure.TestFiles 20 import com.android.tools.lint.detector.api.Detector 21 import com.android.tools.lint.detector.api.Issue 22 import org.junit.Test 23 24 class CollectAsStateDetectorTest : SystemUILintDetectorTest() { 25 getDetectornull26 override fun getDetector(): Detector { 27 return CollectAsStateDetector() 28 } 29 getIssuesnull30 override fun getIssues(): List<Issue> { 31 return listOf( 32 CollectAsStateDetector.ISSUE, 33 ) 34 } 35 36 @Test testViolationnull37 fun testViolation() { 38 lint() 39 .files(COLLECT_AS_STATE_STUB, COLLECT_WITH_LIFECYCLE_AS_STATE_STUB, GOOD_FILE, BAD_FILE) 40 .issues(CollectAsStateDetector.ISSUE) 41 .run() 42 .expect( 43 """ 44 src/com/android/internal/systemui/lint/Bad.kt:3: Error: collectAsState considered harmful [OverlyEagerCollectAsState] 45 import androidx.compose.runtime.collectAsState 46 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 47 1 errors, 0 warnings 48 """ 49 .trimIndent() 50 ) 51 } 52 53 @Test testNoViolationnull54 fun testNoViolation() { 55 lint() 56 .files(COLLECT_AS_STATE_STUB, COLLECT_WITH_LIFECYCLE_AS_STATE_STUB, GOOD_FILE) 57 .issues(CollectAsStateDetector.ISSUE) 58 .run() 59 .expectClean() 60 } 61 62 companion object { 63 private val COLLECT_AS_STATE_STUB = 64 TestFiles.kotlin( 65 """ 66 package androidx.compose.runtime 67 collectAsStatenull68 fun collectAsState() {} 69 """ 70 .trimIndent() 71 ) 72 private val COLLECT_WITH_LIFECYCLE_AS_STATE_STUB = 73 TestFiles.kotlin( 74 """ 75 package androidx.lifecycle.compose 76 77 fun collectAsStateWithLifecycle() {} 78 """ 79 .trimIndent() 80 ) 81 82 private val BAD_FILE = 83 TestFiles.kotlin( 84 """ 85 package com.android.internal.systemui.lint 86 87 import androidx.compose.runtime.collectAsState 88 89 class Bad 90 """ 91 .trimIndent() 92 ) 93 94 private val GOOD_FILE = 95 TestFiles.kotlin( 96 """ 97 package com.android.internal.systemui.lint 98 99 import androidx.lifecycle.compose.collectAsStateWithLifecycle 100 101 class Good 102 """ 103 .trimIndent() 104 ) 105 } 106 } 107