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(); 62 System.exit(1); 63 } 64 main(String[] args)65 public static void main(String[] args) throws Exception { 66 List<File> testApks = new ArrayList<File>(); 67 File outputFile = null; 68 int format = FORMAT_TXT; 69 String dexDeps = "dexDeps"; 70 71 for (int i = 0; i < args.length; i++) { 72 if (args[i].startsWith("-")) { 73 if ("-o".equals(args[i])) { 74 outputFile = new File(getExpectedArg(args, ++i)); 75 } else if ("-f".equals(args[i])) { 76 String formatSpec = getExpectedArg(args, ++i); 77 if ("xml".equalsIgnoreCase(formatSpec)) { 78 format = FORMAT_XML; 79 } else if ("txt".equalsIgnoreCase(formatSpec)) { 80 format = FORMAT_TXT; 81 } else if ("html".equalsIgnoreCase(formatSpec)) { 82 format = FORMAT_HTML; 83 } else { 84 printUsage(); 85 } 86 } else if ("-d".equals(args[i])) { 87 dexDeps = getExpectedArg(args, ++i); 88 } else { 89 printUsage(); 90 } 91 } else { 92 testApks.add(new File(args[i])); 93 } 94 } 95 96 /* 97 * 1. Create an ApiCoverage object that is a tree of Java objects representing the API 98 * in current.xml. The object will have no information about the coverage for each 99 * constructor or method yet. 100 * 101 * 2. For each provided APK, scan it using dexdeps, parse the output of dexdeps, and 102 * call methods on the ApiCoverage object to cumulatively add coverage stats. 103 * 104 * 3. Output a report based on the coverage stats in the ApiCoverage object. 105 */ 106 107 ApiCoverage apiCoverage = getEmptyApiCoverage(); 108 for (File testApk : testApks) { 109 addApiCoverage(apiCoverage, testApk, dexDeps); 110 } 111 outputCoverageReport(apiCoverage, testApks, outputFile, format); 112 } 113 114 /** Get the argument or print out the usage and exit. */ getExpectedArg(String[] args, int index)115 private static String getExpectedArg(String[] args, int index) { 116 if (index < args.length) { 117 return args[index]; 118 } else { 119 printUsage(); 120 return null; // Never will happen because printUsage will call exit(1) 121 } 122 } 123 124 /** 125 * Creates an object representing the API that will be used later to collect coverage 126 * statistics as we iterate over the test APKs. 127 * 128 * @return an {@link ApiCoverage} object representing the API in current.xml without any 129 * coverage statistics yet 130 */ getEmptyApiCoverage()131 private static ApiCoverage getEmptyApiCoverage() 132 throws SAXException, IOException { 133 XMLReader xmlReader = XMLReaderFactory.createXMLReader(); 134 CurrentXmlHandler currentXmlHandler = new CurrentXmlHandler(); 135 xmlReader.setContentHandler(currentXmlHandler); 136 137 File currentXml = new File("frameworks/base/api/current.xml"); 138 FileReader fileReader = null; 139 try { 140 fileReader = new FileReader(currentXml); 141 xmlReader.parse(new InputSource(fileReader)); 142 } finally { 143 if (fileReader != null) { 144 fileReader.close(); 145 } 146 } 147 148 return currentXmlHandler.getApi(); 149 } 150 151 /** 152 * Adds coverage information gleamed from running dexdeps on the APK to the 153 * {@link ApiCoverage} object. 154 * 155 * @param apiCoverage object to which the coverage statistics will be added to 156 * @param testApk containing the tests that will be scanned by dexdeps 157 */ addApiCoverage(ApiCoverage apiCoverage, File testApk, String dexdeps)158 private static void addApiCoverage(ApiCoverage apiCoverage, File testApk, String dexdeps) 159 throws SAXException, IOException { 160 XMLReader xmlReader = XMLReaderFactory.createXMLReader(); 161 DexDepsXmlHandler dexDepsXmlHandler = new DexDepsXmlHandler(apiCoverage); 162 xmlReader.setContentHandler(dexDepsXmlHandler); 163 164 Process process = new ProcessBuilder(dexdeps, "--format=xml", testApk.getPath()).start(); 165 xmlReader.parse(new InputSource(process.getInputStream())); 166 } 167 outputCoverageReport(ApiCoverage apiCoverage, List<File> testApks, File outputFile, int format)168 private static void outputCoverageReport(ApiCoverage apiCoverage, List<File> testApks, 169 File outputFile, int format) throws IOException, TransformerException, 170 InterruptedException { 171 172 OutputStream out = outputFile != null 173 ? new FileOutputStream(outputFile) 174 : System.out; 175 176 try { 177 switch (format) { 178 case FORMAT_TXT: 179 TextReport.printTextReport(apiCoverage, out); 180 break; 181 182 case FORMAT_XML: 183 XmlReport.printXmlReport(testApks, apiCoverage, out); 184 break; 185 186 case FORMAT_HTML: 187 HtmlReport.printHtmlReport(testApks, apiCoverage, out); 188 break; 189 } 190 } finally { 191 out.close(); 192 } 193 } 194 } 195