Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright (C) 2015 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.compatibility.common.util;
     17 
     18 import java.util.ArrayList;
     19 import java.util.Collections;
     20 import java.util.HashMap;
     21 import java.util.List;
     22 import java.util.Map;
     23 
     24 /**
     25  * Data structure for a Compatibility test module result.
     26  */
     27 public class ModuleResult implements IModuleResult {
     28 
     29     private String mId;
     30     private long mRuntime = 0;
     31 
     32     /* Variables related to completion of the module */
     33     private boolean mDone = false;
     34     private boolean mHaveSetDone = false;
     35     private boolean mInProgress = false;
     36     private int mExpectedTestRuns = 0;
     37     private int mActualTestRuns = 0;
     38     private int mNotExecuted = 0;
     39 
     40     private Map<String, ICaseResult> mResults = new HashMap<>();
     41 
     42     /**
     43      * Creates a {@link ModuleResult} for the given id, created with
     44      * {@link AbiUtils#createId(String, String)}
     45      */
     46     public ModuleResult(String id) {
     47         mId = id;
     48     }
     49 
     50     /**
     51      * {@inheritDoc}
     52      */
     53     @Override
     54     public boolean isDone() {
     55         return mDone && !mInProgress && (mActualTestRuns >= mExpectedTestRuns);
     56     }
     57 
     58     /**
     59      * {@inheritDoc}
     60      */
     61     @Override
     62     public boolean isDoneSoFar() {
     63         return mDone && !mInProgress;
     64     }
     65 
     66     /**
     67      * {@inheritDoc}
     68      */
     69     @Override
     70     public void initializeDone(boolean done) {
     71         mDone = done;
     72         mHaveSetDone = false;
     73         if (mDone) {
     74             mNotExecuted = 0;
     75         }
     76     }
     77 
     78     /**
     79      * {@inheritDoc}
     80      */
     81     @Override
     82     public void setDone(boolean done) {
     83         if (mHaveSetDone) {
     84             mDone &= done; // If we've already set done for this instance, AND the received value
     85         } else {
     86             mDone = done; // If done has only been initialized, overwrite the existing value
     87         }
     88         mHaveSetDone = true;
     89         if (mDone) {
     90             mNotExecuted = 0;
     91         }
     92     }
     93 
     94     /**
     95      * {@inheritDoc}
     96      */
     97     @Override
     98     public void inProgress(boolean inProgress) {
     99         mInProgress = inProgress;
    100     }
    101 
    102     /**
    103      * {@inheritDoc}
    104      */
    105     @Override
    106     public int getExpectedTestRuns() {
    107         return mExpectedTestRuns;
    108     }
    109 
    110     /**
    111      * {@inheritDoc}
    112      */
    113     @Override
    114     public void setExpectedTestRuns(int numRuns) {
    115         mExpectedTestRuns = numRuns;
    116     }
    117 
    118     /**
    119      * {@inheritDoc}
    120      */
    121     @Override
    122     public int getTestRuns() {
    123         return mActualTestRuns;
    124     }
    125 
    126     /**
    127      * {@inheritDoc}
    128      */
    129     @Override
    130     public void addTestRun() {
    131         mActualTestRuns++;
    132     }
    133 
    134     /**
    135      * {@inheritDoc}
    136      */
    137     @Override
    138     public void resetTestRuns() {
    139         mActualTestRuns = 0;
    140     }
    141 
    142     /**
    143      * {@inheritDoc}
    144      */
    145     @Override
    146     public int getNotExecuted() {
    147         return mNotExecuted;
    148     }
    149 
    150     /**
    151      * {@inheritDoc}
    152      */
    153     @Override
    154     public void setNotExecuted(int numTests) {
    155         mNotExecuted = numTests;
    156     }
    157 
    158     /**
    159      * {@inheritDoc}
    160      */
    161     @Override
    162     public String getId() {
    163         return mId;
    164     }
    165 
    166     /**
    167      * {@inheritDoc}
    168      */
    169     @Override
    170     public String getName() {
    171         return AbiUtils.parseTestName(mId);
    172     }
    173 
    174     /**
    175      * {@inheritDoc}
    176      */
    177     @Override
    178     public String getAbi() {
    179         return AbiUtils.parseAbi(mId);
    180     }
    181 
    182     /**
    183      * {@inheritDoc}
    184      */
    185     @Override
    186     public void addRuntime(long elapsedTime) {
    187         mRuntime += elapsedTime;
    188     }
    189 
    190     /**
    191      * {@inheritDoc}
    192      */
    193     @Override
    194     public void resetRuntime() {
    195         mRuntime = 0;
    196     }
    197 
    198     /**
    199      * {@inheritDoc}
    200      */
    201     @Override
    202     public long getRuntime() {
    203         return mRuntime;
    204     }
    205 
    206     /**
    207      * {@inheritDoc}
    208      */
    209     @Override
    210     public ICaseResult getOrCreateResult(String caseName) {
    211         ICaseResult result = mResults.get(caseName);
    212         if (result == null) {
    213             result = new CaseResult(caseName);
    214             mResults.put(caseName, result);
    215         }
    216         return result;
    217     }
    218 
    219     /**
    220      * {@inheritDoc}
    221      */
    222     @Override
    223     public ICaseResult getResult(String caseName) {
    224         return mResults.get(caseName);
    225     }
    226 
    227     /**
    228      * {@inheritDoc}
    229      */
    230     @Override
    231     public List<ICaseResult> getResults() {
    232         ArrayList<ICaseResult> results = new ArrayList<>(mResults.values());
    233         Collections.sort(results);
    234         return results;
    235     }
    236 
    237     /**
    238      * {@inheritDoc}
    239      */
    240     @Override
    241     public int countResults(TestStatus status) {
    242         int total = 0;
    243         for (ICaseResult result : mResults.values()) {
    244             total += result.countResults(status);
    245         }
    246         return total;
    247     }
    248 
    249     /**
    250      * {@inheritDoc}
    251      */
    252     @Override
    253     public int compareTo(IModuleResult another) {
    254         return getId().compareTo(another.getId());
    255     }
    256 
    257     /**
    258      * {@inheritDoc}
    259      */
    260     @Override
    261     public void mergeFrom(IModuleResult otherModuleResult) {
    262         if (!otherModuleResult.getId().equals(getId())) {
    263             throw new IllegalArgumentException(String.format(
    264                 "Cannot merge module result with mismatched id. Expected %s, Found %s",
    265                         otherModuleResult.getId(), getId()));
    266         }
    267 
    268         this.mRuntime += otherModuleResult.getRuntime();
    269         this.mNotExecuted += otherModuleResult.getNotExecuted();
    270         // only touch variables related to 'done' status if this module is not already done
    271         if (!isDone()) {
    272             this.setDone(otherModuleResult.isDoneSoFar());
    273             this.mActualTestRuns += otherModuleResult.getTestRuns();
    274             // expected test runs are the same across shards, except for shards that do not run
    275             // this module at least once (for which the value is not yet set).
    276             this.mExpectedTestRuns = otherModuleResult.getExpectedTestRuns();
    277         }
    278         for (ICaseResult otherCaseResult : otherModuleResult.getResults()) {
    279             ICaseResult caseResult = getOrCreateResult(otherCaseResult.getName());
    280             caseResult.mergeFrom(otherCaseResult);
    281         }
    282     }
    283 }
    284