1 /*
2  * Copyright (C) 2018 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.apilevels
18 
19 import com.android.tools.metalava.DriverTest
20 import com.android.utils.XmlUtils
21 import com.google.common.truth.Truth
22 import org.junit.Assert.assertNotNull
23 import org.junit.Assert.assertTrue
24 import org.junit.Test
25 import java.io.File
26 import kotlin.text.Charsets.UTF_8
27 
28 class ApiGeneratorTest : DriverTest() {
29     @Test
Extract API levelsnull30     fun `Extract API levels`() {
31         val oldSdkJars = File("prebuilts/tools/common/api-versions")
32         if (!oldSdkJars.isDirectory) {
33             println("Ignoring ${ApiGeneratorTest::class.java}: prebuilts not found - is \$PWD set to an Android source tree?")
34             return
35         }
36 
37         val platformJars = File("prebuilts/sdk")
38         if (!platformJars.isDirectory) {
39             println("Ignoring ${ApiGeneratorTest::class.java}: prebuilts not found: $platformJars")
40             return
41         }
42 
43         val output = File.createTempFile("api-info", "xml")
44         output.deleteOnExit()
45         val outputPath = output.path
46 
47         check(
48             extraArguments = arrayOf(
49                 "--generate-api-levels",
50                 outputPath,
51                 "--android-jar-pattern",
52                 "${oldSdkJars.path}/android-%/android.jar",
53                 "--android-jar-pattern",
54                 "${platformJars.path}/%/public/android.jar"
55             ),
56             checkDoclava1 = false,
57             signatureSource = """
58                 package test.pkg {
59                   public class MyTest {
60                     ctor public MyTest();
61                     method public int clamp(int);
62                     method public java.lang.Double convert(java.lang.Float);
63                     field public java.lang.Number myNumber;
64                   }
65                 }
66                 """
67         )
68 
69         assertTrue(output.isFile)
70 
71         val xml = output.readText(UTF_8)
72         Truth.assertThat(xml).contains("<class name=\"android/Manifest\$permission\" since=\"1\">")
73         Truth.assertThat(xml)
74             .contains("<field name=\"BIND_CARRIER_MESSAGING_SERVICE\" since=\"22\" deprecated=\"23\"/>")
75 
76         val document = XmlUtils.parseDocumentSilently(xml, false)
77         assertNotNull(document)
78     }
79 }
80