Home | History | Annotate | Download | only in suite
      1 /*
      2  * Copyright (C) 2018 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.tradefed.result.suite;
     17 
     18 import com.android.tradefed.build.BuildInfo;
     19 import com.android.tradefed.build.IBuildInfo;
     20 import com.android.tradefed.invoker.IInvocationContext;
     21 import com.android.tradefed.invoker.InvocationContext;
     22 import com.android.tradefed.metrics.proto.MetricMeasurement.Metric;
     23 import com.android.tradefed.result.TestDescription;
     24 import com.android.tradefed.testtype.Abi;
     25 import com.android.tradefed.testtype.IAbi;
     26 import com.android.tradefed.testtype.suite.ModuleDefinition;
     27 
     28 import org.junit.Assert;
     29 import org.junit.Before;
     30 import org.junit.Test;
     31 import org.junit.runner.RunWith;
     32 import org.junit.runners.JUnit4;
     33 
     34 import java.util.HashMap;
     35 import java.util.LinkedHashMap;
     36 import java.util.Map;
     37 
     38 /** Unit tests for {@link FormattedGeneratorReporter}. */
     39 @RunWith(JUnit4.class)
     40 public class FormattedGeneratorReporterTest {
     41 
     42     private FormattedGeneratorReporter mReporter;
     43     private IInvocationContext mContext;
     44     private IBuildInfo mBuildInfo;
     45 
     46     @Before
     47     public void setUp() {
     48         mContext = new InvocationContext();
     49         mBuildInfo = new BuildInfo();
     50         mContext.addDeviceBuildInfo("default", mBuildInfo);
     51     }
     52 
     53     /** Test the value in the result holder when no module or tests actually ran. */
     54     @Test
     55     public void testFinalizedResults_nothingRan() {
     56         mReporter =
     57                 new FormattedGeneratorReporter() {
     58 
     59                     @Override
     60                     public void finalizeResults(
     61                             IFormatterGenerator generator, SuiteResultHolder resultHolder) {
     62                         validateSuiteHolder(
     63                                 resultHolder, 0, 0, 0, 0, 500L, mContext, new HashMap<>());
     64                     }
     65 
     66                     @Override
     67                     public IFormatterGenerator createFormatter() {
     68                         return null;
     69                     }
     70                 };
     71         mReporter.invocationStarted(mContext);
     72         mReporter.invocationEnded(500L);
     73     }
     74 
     75     /**
     76      * Test the values in the result holder when no module abi was reported, but we got some tests
     77      * results.
     78      */
     79     @Test
     80     public void testFinalizeResults_noAbi() {
     81         mReporter =
     82                 new FormattedGeneratorReporter() {
     83 
     84                     @Override
     85                     public void finalizeResults(
     86                             IFormatterGenerator generator, SuiteResultHolder resultHolder) {
     87                         validateSuiteHolder(
     88                                 resultHolder, 1, 1, 1, 0, 500L, mContext, new HashMap<>());
     89                     }
     90 
     91                     @Override
     92                     public IFormatterGenerator createFormatter() {
     93                         return null;
     94                     }
     95                 };
     96         mReporter.invocationStarted(mContext);
     97         mReporter.testRunStarted("run1", 1);
     98         mReporter.testStarted(new TestDescription("class", "method"));
     99         mReporter.testEnded(new TestDescription("class", "method"), new HashMap<String, Metric>());
    100         mReporter.testRunEnded(450L, new HashMap<String, Metric>());
    101         mReporter.invocationEnded(500L);
    102     }
    103 
    104     /** Test the values inside the suite holder when some tests and module were reported. */
    105     @Test
    106     public void testFinalizeResults() {
    107         mReporter =
    108                 new FormattedGeneratorReporter() {
    109 
    110                     @Override
    111                     public void finalizeResults(
    112                             IFormatterGenerator generator, SuiteResultHolder resultHolder) {
    113                         Map<String, IAbi> expectedModuleAbi = new LinkedHashMap<>();
    114                         expectedModuleAbi.put("module1", new Abi("abi1", "64"));
    115                         validateSuiteHolder(
    116                                 resultHolder, 1, 1, 1, 0, 500L, mContext, expectedModuleAbi);
    117                     }
    118 
    119                     @Override
    120                     public IFormatterGenerator createFormatter() {
    121                         return null;
    122                     }
    123                 };
    124         mReporter.invocationStarted(mContext);
    125         IInvocationContext moduleContext = new InvocationContext();
    126         moduleContext.addInvocationAttribute(ModuleDefinition.MODULE_ID, "module1");
    127         moduleContext.addInvocationAttribute(ModuleDefinition.MODULE_ABI, "abi1");
    128         mReporter.testModuleStarted(moduleContext);
    129         mReporter.testRunStarted("run1", 1);
    130         mReporter.testStarted(new TestDescription("class", "method"));
    131         mReporter.testEnded(new TestDescription("class", "method"), new HashMap<String, Metric>());
    132         mReporter.testRunEnded(450L, new HashMap<String, Metric>());
    133         mReporter.testModuleEnded();
    134         mReporter.invocationEnded(500L);
    135     }
    136 
    137     /** Validate the information inside the suite holder. */
    138     private void validateSuiteHolder(
    139             SuiteResultHolder holder,
    140             int completeModules,
    141             int totalModules,
    142             long passedTests,
    143             long failedTests,
    144             long elapsedTime,
    145             IInvocationContext context,
    146             Map<String, IAbi> moduleAbis) {
    147         Assert.assertEquals(completeModules, holder.completeModules);
    148         Assert.assertEquals(totalModules, holder.totalModules);
    149         Assert.assertEquals(passedTests, holder.passedTests);
    150         Assert.assertEquals(failedTests, holder.failedTests);
    151         Assert.assertEquals(elapsedTime, holder.endTime - holder.startTime);
    152         Assert.assertEquals(context, holder.context);
    153         for (String keyModule : moduleAbis.keySet()) {
    154             Assert.assertEquals(moduleAbis.get(keyModule), holder.modulesAbi.get(keyModule));
    155         }
    156     }
    157 }
    158