Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2008 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;
     18 
     19 import java.io.File;
     20 import java.io.IOException;
     21 import java.util.ArrayList;
     22 import java.util.Collection;
     23 
     24 import javax.xml.parsers.DocumentBuilder;
     25 import javax.xml.parsers.DocumentBuilderFactory;
     26 import javax.xml.parsers.ParserConfigurationException;
     27 
     28 import org.w3c.dom.Document;
     29 import org.w3c.dom.Node;
     30 import org.w3c.dom.NodeList;
     31 import org.xml.sax.SAXException;
     32 
     33 /**
     34  * Define TestPlan tags and attributes.
     35  */
     36 public class TestPlan extends XMLResourceHandler{
     37     public static final String EXCLUDE_SEPARATOR = ";";
     38 
     39     public interface Tag {
     40         public static final String TEST_SUITE = "TestSuite";
     41         public static final String ENTRY = "Entry";
     42         public static final String TEST_PLAN = "TestPlan";
     43         public static final String PLAN_SETTING = "PlanSettings";
     44         public static final String REQUIRED_DEVICE = "RequiredDevice";
     45         public static final String TEST_CASE = "TestCase";
     46     }
     47 
     48     public interface Attribute {
     49         public static final String NAME = "name";
     50         public static final String URI = "uri";
     51         public static final String EXCLUDE = "exclude";
     52         public static final String AMOUNT = "amount";
     53     }
     54 
     55     /**
     56      * Get test package names via test plan file path.
     57      *
     58      * @param planPath TestPlan configuration file path
     59      * @param removedPkgList The removed package list.
     60      * @return The package names.
     61      */
     62     public static Collection<String> getEntries(String planPath,
     63             ArrayList<String> removedPkgList)
     64             throws SAXException, IOException, ParserConfigurationException {
     65         ArrayList<String> entries = new ArrayList<String>();
     66 
     67         DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
     68         File planFile = new File(planPath);
     69         Document doc = builder.parse(planFile);
     70 
     71         NodeList pkgEntries = doc.getElementsByTagName(TestPlan.Tag.ENTRY);
     72         for (int i = 0; i < pkgEntries.getLength(); i++) {
     73             Node pkgEntry = pkgEntries.item(i);
     74             String uri = getStringAttributeValue(pkgEntry, TestPlan.Attribute.URI);
     75 
     76             String packageBinaryName = HostConfig.getInstance().getPackageBinaryName(uri);
     77             if (packageBinaryName != null) {
     78                 entries.add(getStringAttributeValue(pkgEntry, TestPlan.Attribute.URI));
     79             } else {
     80                 removedPkgList.add(uri);
     81             }
     82         }
     83 
     84         return entries;
     85     }
     86 
     87     /**
     88      * Check if the given package name is valid in the case repository.
     89      *
     90      * @param pkgName
     91      * @return if both the apk file and xml file exist, return true;
     92      *         else, return false.
     93      */
     94     public static boolean isValidPackageName(String pkgName) {
     95         String xmlPath = HostConfig.getInstance().getCaseRepository().getXmlPath(pkgName);
     96         String apkPath = HostConfig.getInstance().getCaseRepository().getApkPath(pkgName);
     97         File xmlFile = new File(xmlPath);
     98         File apkFile = new File(apkPath);
     99 
    100         if (xmlFile.exists() && apkFile.exists()) {
    101             return true;
    102         } else {
    103             return false;
    104         }
    105     }
    106 }
    107