• 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         apiCoverage.removeEmptyAbstractClasses();
113         for (File testApk : testApks) {
114             addApiCoverage(apiCoverage, testApk, dexDeps);
115         }
116         outputCoverageReport(apiCoverage, testApks, outputFile, format);
117     }
118 
119     /** Get the argument or print out the usage and exit. */
getExpectedArg(String[] args, int index)120     private static String getExpectedArg(String[] args, int index) {
121         if (index < args.length) {
122             return args[index];
123         } else {
124             printUsage();
125             return null;    // Never will happen because printUsage will call exit(1)
126         }
127     }
128 
129     /**
130      * Creates an object representing the API that will be used later to collect coverage
131      * statistics as we iterate over the test APKs.
132      *
133      * @param apiXmlPath to the API XML file
134      * @return an {@link ApiCoverage} object representing the API in current.xml without any
135      *     coverage statistics yet
136      */
getEmptyApiCoverage(String apiXmlPath)137     private static ApiCoverage getEmptyApiCoverage(String apiXmlPath)
138             throws SAXException, IOException {
139         XMLReader xmlReader = XMLReaderFactory.createXMLReader();
140         CurrentXmlHandler currentXmlHandler = new CurrentXmlHandler();
141         xmlReader.setContentHandler(currentXmlHandler);
142 
143         File currentXml = new File(apiXmlPath);
144         FileReader fileReader = null;
145         try {
146             fileReader = new FileReader(currentXml);
147             xmlReader.parse(new InputSource(fileReader));
148         } finally {
149             if (fileReader != null) {
150                 fileReader.close();
151             }
152         }
153 
154         return currentXmlHandler.getApi();
155     }
156 
157     /**
158      * Adds coverage information gleamed from running dexdeps on the APK to the
159      * {@link ApiCoverage} object.
160      *
161      * @param apiCoverage object to which the coverage statistics will be added to
162      * @param testApk containing the tests that will be scanned by dexdeps
163      */
addApiCoverage(ApiCoverage apiCoverage, File testApk, String dexdeps)164     private static void addApiCoverage(ApiCoverage apiCoverage, File testApk, String dexdeps)
165             throws SAXException, IOException {
166         XMLReader xmlReader = XMLReaderFactory.createXMLReader();
167         DexDepsXmlHandler dexDepsXmlHandler = new DexDepsXmlHandler(apiCoverage);
168         xmlReader.setContentHandler(dexDepsXmlHandler);
169 
170         Process process = new ProcessBuilder(dexdeps, "--format=xml", testApk.getPath()).start();
171         xmlReader.parse(new InputSource(process.getInputStream()));
172     }
173 
outputCoverageReport(ApiCoverage apiCoverage, List<File> testApks, File outputFile, int format)174     private static void outputCoverageReport(ApiCoverage apiCoverage, List<File> testApks,
175             File outputFile, int format) throws IOException, TransformerException,
176                     InterruptedException {
177 
178         OutputStream out = outputFile != null
179                 ? new FileOutputStream(outputFile)
180                 : System.out;
181 
182         try {
183             switch (format) {
184                 case FORMAT_TXT:
185                     TextReport.printTextReport(apiCoverage, out);
186                     break;
187 
188                 case FORMAT_XML:
189                     XmlReport.printXmlReport(testApks, apiCoverage, out);
190                     break;
191 
192                 case FORMAT_HTML:
193                     HtmlReport.printHtmlReport(testApks, apiCoverage, out);
194                     break;
195             }
196         } finally {
197             out.close();
198         }
199     }
200 }
201