Home | History | Annotate | Download | only in testtype
      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 package com.android.cts.tradefed.testtype;
     17 
     18 import com.android.cts.tradefed.testtype.CtsTest.TestPackage;
     19 import com.android.ddmlib.testrunner.TestIdentifier;
     20 import com.android.tradefed.log.LogUtil.CLog;
     21 import com.android.tradefed.result.ITestInvocationListener;
     22 import com.android.tradefed.result.ResultForwarder;
     23 
     24 import java.util.Collection;
     25 import java.util.HashMap;
     26 import java.util.HashSet;
     27 import java.util.LinkedHashMap;
     28 import java.util.LinkedHashSet;
     29 import java.util.List;
     30 import java.util.Map;
     31 
     32 /**
     33  * A {@link ITestInvocationListener} that filters test results based on the set of expected tests
     34  * in CTS test package xml files.
     35  * <p/>
     36  * It will only report test results for expected tests, and at end of invocation, will report the
     37  * set of expected tests that were not executed.
     38  */
     39 class ResultFilter extends ResultForwarder {
     40 
     41     private final Map<String, Collection<TestIdentifier>> mKnownTestsMap;
     42     private final Map<String, Collection<TestIdentifier>> mRemainingTestsMap;
     43     private String mCurrentTestRun = null;
     44 
     45     /**
     46      * Create a {@link ResultFilter}.
     47      *
     48      * @param listener the real {@link ITestInvocationListener} to forward results to
     49      */
     50     ResultFilter(ITestInvocationListener listener, List<TestPackage> testPackages) {
     51         super(listener);
     52 
     53         mKnownTestsMap = new HashMap<String, Collection<TestIdentifier>>();
     54         // use LinkedHashMap for predictable test order
     55         mRemainingTestsMap = new LinkedHashMap<String, Collection<TestIdentifier>>();
     56 
     57         for (TestPackage testPkg : testPackages) {
     58             mKnownTestsMap.put(testPkg.getTestRunName(), new HashSet<TestIdentifier>(
     59                     testPkg.getKnownTests()));
     60             mRemainingTestsMap.put(testPkg.getTestRunName(), new LinkedHashSet<TestIdentifier>(
     61                     testPkg.getKnownTests()));
     62         }
     63     }
     64 
     65     /**
     66      * {@inheritDoc}
     67      */
     68     @Override
     69     public void testRunStarted(String runName, int testCount) {
     70         super.testRunStarted(runName, testCount);
     71         mCurrentTestRun = runName;
     72     }
     73 
     74     /**
     75      * {@inheritDoc}
     76      */
     77     @Override
     78     public void testStarted(TestIdentifier test) {
     79         if (isKnownTest(test)) {
     80             super.testStarted(test);
     81         } else {
     82             CLog.d("Skipping reporting unknown test %s", test);
     83         }
     84     }
     85 
     86     /**
     87      * {@inheritDoc}
     88      */
     89     @Override
     90     public void testFailed(TestFailure status, TestIdentifier test, String trace) {
     91         if (isKnownTest(test)) {
     92             super.testFailed(status, test, trace);
     93         }
     94     }
     95 
     96     /**
     97      * {@inheritDoc}
     98      */
     99     @Override
    100     public void testEnded(TestIdentifier test, Map<String, String> testMetrics) {
    101         if (isKnownTest(test)) {
    102             super.testEnded(test, testMetrics);
    103             removeExecutedTest(test);
    104         }
    105     }
    106 
    107     /**
    108      * @param test
    109      * @return
    110      */
    111     private boolean isKnownTest(TestIdentifier test) {
    112         if (mCurrentTestRun != null && mKnownTestsMap.containsKey(mCurrentTestRun)) {
    113             return mKnownTestsMap.get(mCurrentTestRun).contains(test);
    114         }
    115         return false;
    116     }
    117 
    118     /**
    119      * Remove given test from the 'remaining tests' data structure.
    120      * @param test
    121      */
    122     private void removeExecutedTest(TestIdentifier test) {
    123         if (mCurrentTestRun != null && mRemainingTestsMap.containsKey(mCurrentTestRun)) {
    124              mRemainingTestsMap.get(mCurrentTestRun).remove(test);
    125         }
    126     }
    127 
    128     /**
    129      * Report the set of expected tests that were not executed
    130      */
    131     public void reportUnexecutedTests() {
    132         for (Map.Entry<String, Collection<TestIdentifier>> entry : mRemainingTestsMap.entrySet()) {
    133             if (!entry.getValue().isEmpty()) {
    134                 super.testRunStarted(entry.getKey(), entry.getValue().size());
    135                 for (TestIdentifier test : entry.getValue()) {
    136                     // an unexecuted test is currently reported as a 'testStarted' event without a
    137                     // 'testEnded'. TODO: consider adding an explict API for reporting an unexecuted
    138                     // test
    139                     super.testStarted(test);
    140                 }
    141                 super.testRunEnded(0, new HashMap<String,String>());
    142             }
    143         }
    144     }
    145 }
    146