• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.cts.apicoverage;
18 
19 
20 import org.xml.sax.InputSource;
21 import org.xml.sax.SAXException;
22 import org.xml.sax.XMLReader;
23 import org.xml.sax.helpers.XMLReaderFactory;
24 
25 import java.io.File;
26 import java.io.FileReader;
27 import java.io.FilenameFilter;
28 import java.io.IOException;
29 import java.util.ArrayList;
30 import java.util.List;
31 /**
32  * Class that outputs an XML report of the {@link ApiCoverage} collected. It can be viewed in a
33  * browser when used with the api-coverage.css and api-coverage.xsl files.
34  */
35 class GTestApiReport {
36     public static final String CONFIG_EXT = ".config";
37     public static final String DEFAULT_OUTPUT_FILE_NAME = "./gtest-coverage.txt";
38     public static final String TEST_TYPE = "com.android.tradefed.testtype.GTest";
39 
40     private static final FilenameFilter SUPPORTED_FILE_NAME_FILTER =
41             new FilenameFilter() {
42                 public boolean accept(File dir, String name) {
43                     String fileName = name.toLowerCase();
44                     return fileName.endsWith(CONFIG_EXT);
45                 }
46             };
47 
printUsage()48     private static void printUsage() {
49         System.out.println("Usage: GTestApiXmlReport [OPTION]... [APK]...");
50         System.out.println();
51         System.out.println("Generates a report about what Android NDK methods.");
52         System.out.println();
53         System.out.println("Options:");
54         System.out.println("  -o FILE                output file or standard out if not given");
55         System.out.println("  -t PATH                path to the CTS testcases Folder");
56         System.out.println("  -b BITS                64 or 32");
57         System.out.println();
58         System.exit(1);
59     }
60 
61     /** Get the argument or print out the usage and exit. */
getExpectedArg(String[] args, int index)62     private static String getExpectedArg(String[] args, int index) {
63         if (index < args.length) {
64             return args[index];
65         } else {
66             printUsage();
67             return null; // Never will happen because printUsage will call exit(1)
68         }
69     }
70 
main(String[] args)71     public static void main(String[] args) throws IOException, SAXException {
72         GTestApiReport apiReport;
73         String testCasePath = "";
74         String bits = "64";
75         String outputFileName = DEFAULT_OUTPUT_FILE_NAME;
76         int numTestModule = 0;
77 
78         for (int i = 0; i < args.length; i++) {
79             if (args[i].startsWith("-")) {
80                 if ("-o".equals(args[i])) {
81                     outputFileName = getExpectedArg(args, ++i);
82                 } else if ("-t".equals(args[i])) {
83                     testCasePath = getExpectedArg(args, ++i);
84                 } else if ("-b".equals(args[i])) {
85                     bits = getExpectedArg(args, ++i);
86                 } else {
87                     printUsage();
88                 }
89             } else {
90                 printUsage();
91             }
92         }
93 
94         apiReport = parseTestcasesFolder(testCasePath, bits);
95         if (apiReport != null) {
96             for (TestModule tm : apiReport.mTestModules) {
97                 tm.getDynSymArr();
98             }
99         } else {
100             printUsage();
101         }
102     }
103 
104     private List<TestModule> mTestModules;
105     private String mBits;
106 
GTestApiReport(List<TestModule> testModules, String bits)107     GTestApiReport(List<TestModule> testModules, String bits) {
108         mTestModules = testModules;
109         mBits = bits;
110     }
111 
getTestModules()112     public List<TestModule> getTestModules() {
113         return mTestModules;
114     }
115 
getBits()116     public String getBits() {
117         return mBits;
118     }
119 
parseTestcasesFolder(String testCasePath, String bits)120     public static GTestApiReport parseTestcasesFolder(String testCasePath, String bits)
121             throws IOException, SAXException {
122         File[] testConfigFiles;
123         List<TestModule> testModules = new ArrayList<TestModule>();
124 
125         File file = new File(testCasePath);
126         if (file.isDirectory()) {
127             testConfigFiles = file.listFiles(SUPPORTED_FILE_NAME_FILTER);
128 
129             for (File testConfigFile : testConfigFiles) {
130                 XMLReader xmlReader = XMLReaderFactory.createXMLReader();
131                 TestModuleConfigHandler testModuleXmlHandler = new TestModuleConfigHandler(file.getName());
132                 xmlReader.setContentHandler(testModuleXmlHandler);
133                 FileReader fileReader = null;
134 
135                 try {
136                     fileReader = new FileReader(testConfigFile);
137                     xmlReader.parse(new InputSource(fileReader));
138                     if (TEST_TYPE.equalsIgnoreCase(testModuleXmlHandler.getTestClassName())) {
139                         File gTestExe =
140                                 new File(
141                                         testCasePath
142                                                 + "/"
143                                                 + testModuleXmlHandler.getModuleName()
144                                                 + bits);
145 
146                         System.out.println(gTestExe.getName());
147                         System.out.println(
148                                 String.format(
149                                         "%s: %s, %s",
150                                         testConfigFile.getName(),
151                                         testModuleXmlHandler.getModuleName(),
152                                         testModuleXmlHandler.getTestClassName()));
153 
154                         testModules.add(
155                                 new TestModule(
156                                         gTestExe,
157                                         testModuleXmlHandler.getModuleName(),
158                                         testModuleXmlHandler.getTestClassName()));
159                     }
160 
161                 } finally {
162                     if (fileReader != null) {
163                         fileReader.close();
164                     }
165                 }
166             }
167         } else {
168             return null;
169         }
170         return new GTestApiReport(testModules, bits);
171     }
172 }
173