xref: /aosp_15_r20/cts/tools/lint/checks/src/test/java/com/android/cts/lint/PackageNameDetectorTest.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 package com.android.cts.lint
18 
19 import com.android.tools.lint.checks.infrastructure.LintDetectorTest
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 PackageNameDetectorTest : LintDetectorTest() {
getDetectornull25     override fun getDetector(): Detector = PackageNameDetector()
26 
27     override fun getIssues(): List<Issue> = listOf(PackageNameDetector.ISSUE)
28 
29     @Test
30     fun testPackageNameIsInvalid() {
31         lint()
32             .files(
33                 xml(
34                         "AndroidManifest.xml",
35                         """
36 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
37         package="random.android">
38 </manifest>
39 """
40                     )
41                     .indented()
42             )
43             .allowMissingSdk()
44             .run()
45             .expect(
46                 """
47 AndroidManifest.xml:2: Warning: random.android does not follow the recommendation for package names in CTS. It should match (com.)?android..*.cts. [InvalidPackageName]
48         package="random.android">
49                  ~~~~~~~~~~~~~~
50 0 errors, 1 warnings
51 """
52                     .trimIndent()
53             )
54     }
55 
56     @Test
testPackageNameIsValidnull57     fun testPackageNameIsValid() {
58         lint()
59             .files(
60                 xml(
61                         "AndroidManifest.xml",
62                         """
63 <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="android.my_feature.cts">
64 </manifest>
65 """
66                     )
67                     .indented()
68             )
69             .allowMissingSdk()
70             .run()
71             .expectClean()
72     }
73 
74     @Test
testPackageNameWithComIsValidnull75     fun testPackageNameWithComIsValid() {
76         lint()
77             .files(
78                 xml(
79                         "AndroidManifest.xml",
80                         """
81 <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.android.my_other_feature.cts">
82 </manifest>
83 """
84                     )
85                     .indented()
86             )
87             .allowMissingSdk()
88             .run()
89             .expectClean()
90     }
91 }
92