xref: /aosp_15_r20/cts/tools/lint/checks/src/main/java/com/android/cts/lint/PackageNameDetector.kt (revision b7c941bb3fa97aba169d73cee0bed2de8ac964bf)
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 /** Lint Detector that package names for CTS test apps. */
18 package com.android.cts.lint
19 
20 import com.android.tools.lint.detector.api.Category
21 import com.android.tools.lint.detector.api.Detector
22 import com.android.tools.lint.detector.api.Implementation
23 import com.android.tools.lint.detector.api.Incident
24 import com.android.tools.lint.detector.api.Issue
25 import com.android.tools.lint.detector.api.Scope
26 import com.android.tools.lint.detector.api.Severity
27 import com.android.tools.lint.detector.api.XmlContext
28 import com.android.tools.lint.detector.api.XmlScanner
29 import org.w3c.dom.Element
30 
31 class PackageNameDetector : Detector(), XmlScanner {
32 
getApplicableElementsnull33     override fun getApplicableElements() = listOf("manifest")
34 
35     override fun visitElement(context: XmlContext, element: Element) {
36         val packageNode = element.getAttributeNode("package")
37         val packageName = packageNode.value
38         if (!PACKAGE_NAME_REGEX.matches(packageName)) {
39             val location = context.getValueLocation(packageNode)
40             val incident =
41                 Incident(context, ISSUE)
42                     .location(location)
43                     .message(
44                         "$packageName does not follow the recommendation for package names " +
45                         "in CTS. It should match ${PACKAGE_NAME_REGEX.pattern}."
46                     )
47             context.report(incident)
48         }
49     }
50 
51     companion object {
52         val PACKAGE_NAME_REGEX = Regex("(com\\.)?android\\..*\\.cts")
53         @JvmField
54         val ISSUE: Issue =
55             Issue.create(
56                 id = "InvalidPackageName",
57                 briefDescription =
58                     "The package name for a CTS app should match ${PACKAGE_NAME_REGEX.pattern}",
59                 explanation =
60                     """
61          All new apps in CTS should have a package name that matches ${PACKAGE_NAME_REGEX.pattern}. For instance:
62              android.my_feature.cts
63              android.my_feature.other_package.cts
64              com.android.my_feature.another_subpackage.cts
65                     """
66                         .trimIndent(),
67                 category = Category.CORRECTNESS,
68                 priority = 6,
69                 severity = Severity.WARNING,
70                 implementation =
71                     Implementation(
72                         PackageNameDetector::class.java,
73                         Scope.MANIFEST_SCOPE
74                     ),
75             )
76     }
77 }
78