Home | History | Annotate | Download | only in result
      1 /*
      2  * Copyright (C) 2011 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 package com.android.cts.tradefed.result;
     17 
     18 import com.android.ddmlib.testrunner.TestIdentifier;
     19 import com.android.tradefed.util.ArrayUtil;
     20 
     21 import org.kxml2.io.KXmlSerializer;
     22 import org.xmlpull.v1.XmlPullParser;
     23 import org.xmlpull.v1.XmlPullParserException;
     24 
     25 import java.io.IOException;
     26 import java.util.Collection;
     27 import java.util.Deque;
     28 import java.util.LinkedHashMap;
     29 import java.util.Map;
     30 
     31 /**
     32  * Data structure that represents a "TestCase" XML element and its children.
     33  */
     34 class TestCase extends AbstractXmlPullParser {
     35 
     36     static final String TAG = "TestCase";
     37 
     38     private String mName;
     39 
     40     Map<String, Test> mChildTestMap = new LinkedHashMap<String, Test>();
     41 
     42     /**
     43      * Create a {@link TestCase}
     44      * @param testCaseName
     45      */
     46     public TestCase(String testCaseName) {
     47         setName(testCaseName);
     48     }
     49 
     50     public TestCase() {
     51     }
     52 
     53     public void setName(String name) {
     54         mName = name;
     55     }
     56 
     57     public String getName() {
     58         return mName;
     59     }
     60 
     61     /**
     62      * Gets the child tests
     63      */
     64     public Collection<Test> getTests() {
     65         return mChildTestMap.values();
     66     }
     67 
     68     /**
     69      * @param testName
     70      * @param insertIfMissing
     71      * @return
     72      */
     73     public Test findTest(String testName, boolean insertIfMissing) {
     74         Test t = mChildTestMap.get(testName);
     75         if (t == null && insertIfMissing) {
     76             t = new Test(testName);
     77             mChildTestMap.put(t.getName(), t);
     78         }
     79         return t;
     80     }
     81 
     82     /**
     83      * Serialize this object and all its contents to XML.
     84      *
     85      * @param serializer
     86      * @throws IOException
     87      */
     88     public void serialize(KXmlSerializer serializer) throws IOException {
     89         serializer.startTag(CtsXmlResultReporter.ns, TAG);
     90         serializer.attribute(CtsXmlResultReporter.ns, "name", getName());
     91         // unused
     92         serializer.attribute(CtsXmlResultReporter.ns, "priority", "");
     93         for (Test t : mChildTestMap.values()) {
     94             t.serialize(serializer);
     95         }
     96        serializer.endTag(CtsXmlResultReporter.ns, TAG);
     97     }
     98 
     99     /**
    100      * Populates this class with test case result data parsed from XML.
    101      *
    102      * @param parser the {@link XmlPullParser}. Expected to be pointing at start
    103      *            of a TestCase tag
    104      */
    105     @Override
    106     void parse(XmlPullParser parser) throws XmlPullParserException, IOException {
    107         if (!parser.getName().equals(TAG)) {
    108             throw new XmlPullParserException(String.format(
    109                     "invalid XML: Expected %s tag but received %s", TAG, parser.getName()));
    110         }
    111         setName(getAttribute(parser, "name"));
    112         int eventType = parser.next();
    113         while (eventType != XmlPullParser.END_DOCUMENT) {
    114             if (eventType == XmlPullParser.START_TAG && parser.getName().equals(Test.TAG)) {
    115                 Test test = new Test();
    116                 test.parse(parser);
    117                 mChildTestMap.put(test.getName(), test);
    118             } else if (eventType == XmlPullParser.END_TAG && parser.getName().equals(TAG)) {
    119                 return;
    120             }
    121             eventType = parser.next();
    122         }
    123     }
    124 
    125     /**
    126      * Adds tests contained in this result that have the given <var>resultFilter</var>.
    127      *
    128      * @param tests the {@link Collection} of {@link TestIdentifier}s to add to
    129      * @param parentSuiteNames a {@link Deque} of parent suite names. Used to
    130      *            construct the full class name of the test
    131      * @param resultFilter the {@link CtsTestStatus} to filter by
    132      */
    133     void addTestsWithStatus(Collection<TestIdentifier> tests, Deque<String> parentSuiteNames,
    134             CtsTestStatus resultFilter) {
    135         if (getName() != null) {
    136             parentSuiteNames.addLast(getName());
    137         }
    138         String fullClassName = ArrayUtil.join(".", parentSuiteNames);
    139         for (Test test : mChildTestMap.values()) {
    140             if (resultFilter.equals(test.getResult())) {
    141                 tests.add(new TestIdentifier(fullClassName, test.getName()));
    142             }
    143         }
    144         if (getName() != null) {
    145             parentSuiteNames.removeLast();
    146         }
    147     }
    148 
    149     /**
    150      * Count the number of tests in this {@link TestCase} with given status.
    151      *
    152      * @param status
    153      * @return the test count
    154      */
    155     public int countTests(CtsTestStatus status) {
    156         int total = 0;
    157         for (Test test : mChildTestMap.values()) {
    158             if (test.getResult().equals(status)) {
    159                 total++;
    160             }
    161         }
    162         return total;
    163     }
    164 }
    165