1 /*
2  * Copyright (C) 2017 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.tools.metalava.model
18 
19 import com.android.tools.metalava.model.text.TextBackedAnnotationItem
20 import org.junit.Assert.assertEquals
21 import org.junit.Assert.assertNotNull
22 import org.junit.Assert.assertTrue
23 import org.junit.Test
24 import java.io.File
25 
26 class TextBackedAnnotationItemTest {
27     // Placeholder for use in test where we don't need codebase functionality
28     private val placeholderCodebase = object : DefaultCodebase(File("").canonicalFile) {
supportsDocumentationnull29         override fun supportsDocumentation(): Boolean = false
30         override var description: String = ""
31         override fun getPackages(): PackageList = unsupported()
32         override fun size(): Int = unsupported()
33         override fun findClass(className: String): ClassItem? = unsupported()
34         override fun findPackage(pkgName: String): PackageItem? = unsupported()
35         override fun trustedApi(): Boolean = false
36     }
37 
38     @Test
39     fun testSimple() {
40         val annotation = TextBackedAnnotationItem(
41             placeholderCodebase,
42             "@androidx.annotation.Nullable"
43         )
44         assertEquals("@androidx.annotation.Nullable", annotation.toSource())
45         assertEquals("androidx.annotation.Nullable", annotation.qualifiedName())
46         assertTrue(annotation.attributes().isEmpty())
47     }
48 
49     @Test
testIntRangenull50     fun testIntRange() {
51         val annotation = TextBackedAnnotationItem(
52             placeholderCodebase,
53             "@androidx.annotation.IntRange(from = 20, to = 40)"
54         )
55         assertEquals("@androidx.annotation.IntRange(from = 20, to = 40)", annotation.toSource())
56         assertEquals("androidx.annotation.IntRange", annotation.qualifiedName())
57         assertEquals(2, annotation.attributes().size)
58         assertEquals("from", annotation.findAttribute("from")?.name)
59         assertEquals("20", annotation.findAttribute("from")?.value.toString())
60         assertEquals("to", annotation.findAttribute("to")?.name)
61         assertEquals("40", annotation.findAttribute("to")?.value.toString())
62     }
63 
64     @Test
testIntDefnull65     fun testIntDef() {
66         val annotation = TextBackedAnnotationItem(
67             placeholderCodebase,
68             "@androidx.annotation.IntDef({STYLE_NORMAL, STYLE_NO_TITLE, STYLE_NO_FRAME, STYLE_NO_INPUT})"
69         )
70         assertEquals(
71             "@androidx.annotation.IntDef({STYLE_NORMAL, STYLE_NO_TITLE, STYLE_NO_FRAME, STYLE_NO_INPUT})",
72             annotation.toSource()
73         )
74         assertEquals("androidx.annotation.IntDef", annotation.qualifiedName())
75         assertEquals(1, annotation.attributes().size)
76         val attribute = annotation.findAttribute("value")
77         assertNotNull(attribute)
78         assertEquals("value", attribute?.name)
79         assertEquals(
80             "{STYLE_NORMAL, STYLE_NO_TITLE, STYLE_NO_FRAME, STYLE_NO_INPUT}",
81             annotation.findAttribute("value")?.value.toString()
82         )
83 
84         assertTrue(attribute?.value is AnnotationArrayAttributeValue)
85         if (attribute is AnnotationArrayAttributeValue) {
86             val list = attribute.values
87             assertEquals(3, list.size)
88             assertEquals("STYLE_NO_TITLE", list[1].toSource())
89         }
90     }
91 }