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 import com.android.cts.apicoverage.TestSuiteProto.*; 20 import com.android.cts.apicommon.ApiCoverage; 21 22 import org.xml.sax.Attributes; 23 import org.xml.sax.SAXException; 24 import org.xml.sax.helpers.DefaultHandler; 25 26 /** 27 * {@link DefaultHandler} that builds an empty {@link ApiCoverage} object from scanning 28 * TestModule.xml. 29 */ 30 class TestModuleConfigHandler extends DefaultHandler { 31 private static final String CONFIGURATION_TAG = "configuration"; 32 private static final String DESCRIPTION_TAG = "description"; 33 private static final String OPTION_TAG = "option"; 34 private static final String TARGET_PREPARER_TAG = "target_preparer"; 35 private static final String TEST_TAG = "test"; 36 private static final String CLASS_TAG = "class"; 37 private static final String NAME_TAG = "name"; 38 private static final String KEY_TAG = "key"; 39 private static final String VALUE_TAG = "value"; 40 private static final String MODULE_NAME_TAG = "module-name"; 41 private static final String GTEST_CLASS_TAG = "com.android.tradefed.testtype.GTest"; 42 43 private FileMetadata.Builder mFileMetadata; 44 private ConfigMetadata.Builder mConfigMetadata; 45 private ConfigMetadata.TestClass.Builder mTestCase; 46 private ConfigMetadata.TargetPreparer.Builder mTargetPreparer; 47 private String mModuleName = null; 48 TestModuleConfigHandler(String configFileName)49 TestModuleConfigHandler(String configFileName) { 50 mFileMetadata = FileMetadata.newBuilder(); 51 mConfigMetadata = ConfigMetadata.newBuilder(); 52 mTestCase = null; 53 mTargetPreparer = null; 54 // Default Module Name is the Config File Name 55 mModuleName = configFileName.replaceAll(".config$", ""); 56 } 57 58 @Override startElement(String uri, String localName, String name, Attributes attributes)59 public void startElement(String uri, String localName, String name, Attributes attributes) 60 throws SAXException { 61 super.startElement(uri, localName, name, attributes); 62 63 if (CONFIGURATION_TAG.equalsIgnoreCase(localName)) { 64 if (null != attributes.getValue(DESCRIPTION_TAG)) { 65 mFileMetadata.setDescription(attributes.getValue(DESCRIPTION_TAG)); 66 } else { 67 mFileMetadata.setDescription("WARNING: no description."); 68 } 69 } else if (TEST_TAG.equalsIgnoreCase(localName)) { 70 mTestCase = ConfigMetadata.TestClass.newBuilder(); 71 mTestCase.setTestClass(attributes.getValue(CLASS_TAG)); 72 } else if (TARGET_PREPARER_TAG.equalsIgnoreCase(localName)) { 73 mTargetPreparer = ConfigMetadata.TargetPreparer.newBuilder(); 74 mTargetPreparer.setTestClass(attributes.getValue(CLASS_TAG)); 75 } else if (OPTION_TAG.equalsIgnoreCase(localName)) { 76 Option.Builder option = Option.newBuilder(); 77 option.setName(attributes.getValue(NAME_TAG)); 78 option.setValue(attributes.getValue(VALUE_TAG)); 79 String keyStr = attributes.getValue(KEY_TAG); 80 if (null != keyStr) { 81 option.setKey(keyStr); 82 } 83 if (null != mTestCase) { 84 mTestCase.addOptions(option); 85 if (MODULE_NAME_TAG.equalsIgnoreCase(option.getName())) { 86 mModuleName = option.getValue(); 87 } 88 } else if (null != mTargetPreparer) { 89 mTargetPreparer.addOptions(option); 90 } 91 } 92 } 93 94 @Override endElement(String uri, String localName, String name)95 public void endElement(String uri, String localName, String name) throws SAXException { 96 super.endElement(uri, localName, name); 97 if (TEST_TAG.equalsIgnoreCase(localName)) { 98 mConfigMetadata.addTestClasses(mTestCase); 99 mTestCase = null; 100 } else if (TARGET_PREPARER_TAG.equalsIgnoreCase(localName)) { 101 mConfigMetadata.addTargetPreparers(mTargetPreparer); 102 mTargetPreparer = null; 103 } else if (CONFIGURATION_TAG.equalsIgnoreCase(localName)) { 104 mFileMetadata.setConfigMetadata(mConfigMetadata); 105 } 106 } 107 getModuleName()108 public String getModuleName() { 109 return mModuleName; 110 } 111 getTestClassName()112 public String getTestClassName() { 113 //return the 1st Test Class 114 return mFileMetadata.getConfigMetadata().getTestClassesList().get(0).getTestClass(); 115 } 116 getFileMetadata()117 public FileMetadata getFileMetadata() { 118 return mFileMetadata.build(); 119 } 120 } 121