• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 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.apicoverage;
18 
19 import org.xml.sax.InputSource;
20 import org.xml.sax.SAXException;
21 import org.xml.sax.XMLReader;
22 import org.xml.sax.helpers.XMLReaderFactory;
23 
24 import java.io.File;
25 import java.io.FileOutputStream;
26 import java.io.FileReader;
27 import java.io.IOException;
28 import java.io.OutputStream;
29 import java.util.ArrayList;
30 import java.util.List;
31 
32 import javax.xml.transform.TransformerException;
33 
34 /**
35  * Tool that generates a report of what Android framework methods are being called from a given
36  * set of APKS. See the {@link #printUsage()} method for more details.
37  */
38 public class CtsApiCoverage {
39 
40     private static final int FORMAT_TXT = 0;
41 
42     private static final int FORMAT_XML = 1;
43 
44     private static final int FORMAT_HTML = 2;
45 
printUsage()46     private static void printUsage() {
47         System.out.println("Usage: cts-api-coverage [OPTION]... [APK]...");
48         System.out.println();
49         System.out.println("Generates a report about what Android framework methods are called ");
50         System.out.println("from the given APKs.");
51         System.out.println();
52         System.out.println("Use the Makefiles rules in CtsTestCoverage.mk to generate the report ");
53         System.out.println("rather than executing this directly. If you still want to run this ");
54         System.out.println("directly, then this must be used from the $ANDROID_BUILD_TOP ");
55         System.out.println("directory and dexdeps must be built via \"make dexdeps\".");
56         System.out.println();
57         System.out.println("Options:");
58         System.out.println("  -o FILE              output file or standard out if not given");
59         System.out.println("  -f [txt|xml|html]    format of output");
60         System.out.println("  -d PATH              path to dexdeps or expected to be in $PATH");
61         System.out.println("  -a PATH              path to the API XML file");
62         System.out.println();
63         System.exit(1);
64     }
65 
main(String[] args)66     public static void main(String[] args) throws Exception {
67         List<File> testApks = new ArrayList<File>();
68         File outputFile = null;
69         int format = FORMAT_TXT;
70         String dexDeps = "dexDeps";
71         String apiXmlPath = "";
72 
73         for (int i = 0; i < args.length; i++) {
74             if (args[i].startsWith("-")) {
75                 if ("-o".equals(args[i])) {
76                     outputFile = new File(getExpectedArg(args, ++i));
77                 } else if ("-f".equals(args[i])) {
78                     String formatSpec = getExpectedArg(args, ++i);
79                     if ("xml".equalsIgnoreCase(formatSpec)) {
80                         format = FORMAT_XML;
81                     } else if ("txt".equalsIgnoreCase(formatSpec)) {
82                         format = FORMAT_TXT;
83                     } else if ("html".equalsIgnoreCase(formatSpec)) {
84                         format = FORMAT_HTML;
85                     } else {
86                         printUsage();
87                     }
88                 } else if ("-d".equals(args[i])) {
89                     dexDeps = getExpectedArg(args, ++i);
90                 } else if ("-a".equals(args[i])) {
91                     apiXmlPath = getExpectedArg(args, ++i);
92                 } else {
93                     printUsage();
94                 }
95             } else {
96                 testApks.add(new File(args[i]));
97             }
98         }
99 
100         /*
101          * 1. Create an ApiCoverage object that is a tree of Java objects representing the API
102          *    in current.xml. The object will have no information about the coverage for each
103          *    constructor or method yet.
104          *
105          * 2. For each provided APK, scan it using dexdeps, parse the output of dexdeps, and
106          *    call methods on the ApiCoverage object to cumulatively add coverage stats.
107          *
108          * 3. Output a report based on the coverage stats in the ApiCoverage object.
109          */
110 
111         ApiCoverage apiCoverage = getEmptyApiCoverage(apiXmlPath);
112         for (File testApk : testApks) {
113             addApiCoverage(apiCoverage, testApk, dexDeps);
114         }
115         outputCoverageReport(apiCoverage, testApks, outputFile, format);
116     }
117 
118     /** Get the argument or print out the usage and exit. */
getExpectedArg(String[] args, int index)119     private static String getExpectedArg(String[] args, int index) {
120         if (index < args.length) {
121             return args[index];
122         } else {
123             printUsage();
124             return null;    // Never will happen because printUsage will call exit(1)
125         }
126     }
127 
128     /**
129      * Creates an object representing the API that will be used later to collect coverage
130      * statistics as we iterate over the test APKs.
131      *
132      * @param apiXmlPath to the API XML file
133      * @return an {@link ApiCoverage} object representing the API in current.xml without any
134      *     coverage statistics yet
135      */
getEmptyApiCoverage(String apiXmlPath)136     private static ApiCoverage getEmptyApiCoverage(String apiXmlPath)
137             throws SAXException, IOException {
138         XMLReader xmlReader = XMLReaderFactory.createXMLReader();
139         CurrentXmlHandler currentXmlHandler = new CurrentXmlHandler();
140         xmlReader.setContentHandler(currentXmlHandler);
141 
142         File currentXml = new File(apiXmlPath);
143         FileReader fileReader = null;
144         try {
145             fileReader = new FileReader(currentXml);
146             xmlReader.parse(new InputSource(fileReader));
147         } finally {
148             if (fileReader != null) {
149                 fileReader.close();
150             }
151         }
152 
153         return currentXmlHandler.getApi();
154     }
155 
156     /**
157      * Adds coverage information gleamed from running dexdeps on the APK to the
158      * {@link ApiCoverage} object.
159      *
160      * @param apiCoverage object to which the coverage statistics will be added to
161      * @param testApk containing the tests that will be scanned by dexdeps
162      */
addApiCoverage(ApiCoverage apiCoverage, File testApk, String dexdeps)163     private static void addApiCoverage(ApiCoverage apiCoverage, File testApk, String dexdeps)
164             throws SAXException, IOException {
165         XMLReader xmlReader = XMLReaderFactory.createXMLReader();
166         DexDepsXmlHandler dexDepsXmlHandler = new DexDepsXmlHandler(apiCoverage);
167         xmlReader.setContentHandler(dexDepsXmlHandler);
168 
169         Process process = new ProcessBuilder(dexdeps, "--format=xml", testApk.getPath()).start();
170         xmlReader.parse(new InputSource(process.getInputStream()));
171     }
172 
outputCoverageReport(ApiCoverage apiCoverage, List<File> testApks, File outputFile, int format)173     private static void outputCoverageReport(ApiCoverage apiCoverage, List<File> testApks,
174             File outputFile, int format) throws IOException, TransformerException,
175                     InterruptedException {
176 
177         OutputStream out = outputFile != null
178                 ? new FileOutputStream(outputFile)
179                 : System.out;
180 
181         try {
182             switch (format) {
183                 case FORMAT_TXT:
184                     TextReport.printTextReport(apiCoverage, out);
185                     break;
186 
187                 case FORMAT_XML:
188                     XmlReport.printXmlReport(testApks, apiCoverage, out);
189                     break;
190 
191                 case FORMAT_HTML:
192                     HtmlReport.printHtmlReport(testApks, apiCoverage, out);
193                     break;
194             }
195         } finally {
196             out.close();
197         }
198     }
199 }
200