1 /* 2 * Copyright (C) 2017 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.testtype.suite; 17 18 import static org.junit.Assert.assertEquals; 19 20 import com.android.ddmlib.testrunner.TestResult.TestStatus; 21 import com.android.tradefed.metrics.proto.MetricMeasurement.Metric; 22 import com.android.tradefed.result.ITestInvocationListener; 23 import com.android.tradefed.result.TestDescription; 24 25 import org.junit.Before; 26 import org.junit.Test; 27 import org.junit.runner.RunWith; 28 import org.junit.runners.JUnit4; 29 30 import java.util.HashMap; 31 32 /** Unit tests for {@link ModuleListener} * */ 33 @RunWith(JUnit4.class) 34 public class ModuleListenerTest { 35 36 private ModuleListener mListener; 37 private ITestInvocationListener mStubListener; 38 39 @Before 40 public void setUp() { 41 mStubListener = new ITestInvocationListener() {}; 42 mListener = new ModuleListener(mStubListener); 43 } 44 45 /** Test that a regular execution yield the proper number of tests. */ 46 @Test 47 public void testRegularExecution() { 48 final int numTests = 5; 49 mListener.testRunStarted("run1", numTests); 50 for (int i = 0; i < numTests; i++) { 51 TestDescription tid = new TestDescription("class", "test" + i); 52 mListener.testStarted(tid); 53 mListener.testEnded(tid, new HashMap<String, Metric>()); 54 } 55 mListener.testRunEnded(0, new HashMap<String, Metric>()); 56 assertEquals(numTests, mListener.getNumTotalTests()); 57 assertEquals(numTests, mListener.getNumTestsInState(TestStatus.PASSED)); 58 } 59 60 /** All the tests did not execute, so the amount of TestDescription seen is lower. */ 61 @Test 62 public void testRun_missingTests() { 63 final int numTests = 5; 64 mListener.testRunStarted("run1", numTests); 65 TestDescription tid = new TestDescription("class", "test" + numTests); 66 // Only one test execute 67 mListener.testStarted(tid); 68 mListener.testEnded(tid, new HashMap<String, Metric>()); 69 mListener.testRunEnded(0, new HashMap<String, Metric>()); 70 71 assertEquals(numTests, mListener.getNumTotalTests()); 72 assertEquals(1, mListener.getNumTestsInState(TestStatus.PASSED)); 73 } 74 75 /** 76 * Some tests internally restart testRunStart to retry not_executed tests. We should not 77 * aggregate the number of expected tests. 78 */ 79 @Test 80 public void testInternalRerun() { 81 final int numTests = 5; 82 mListener.testRunStarted("run1", numTests); 83 TestDescription tid = new TestDescription("class", "test" + numTests); 84 // Only one test execute the first time 85 mListener.testStarted(tid); 86 mListener.testEnded(tid, new HashMap<String, Metric>()); 87 mListener.testRunEnded(0, new HashMap<String, Metric>()); 88 89 // Runner restart to execute all the remaining 90 mListener.testRunStarted("run1", numTests - 1); 91 for (int i = 0; i < numTests - 1; i++) { 92 TestDescription tid2 = new TestDescription("class", "test" + i); 93 mListener.testStarted(tid2); 94 mListener.testEnded(tid2, new HashMap<String, Metric>()); 95 } 96 mListener.testRunEnded(0, new HashMap<String, Metric>()); 97 98 assertEquals(numTests, mListener.getNumTotalTests()); 99 assertEquals(numTests, mListener.getNumTestsInState(TestStatus.PASSED)); 100 } 101 102 /** Some test runner calls testRunStart several times. We need to count all their tests. */ 103 @Test 104 public void testMultiTestRun() { 105 final int numTests = 5; 106 mListener.testRunStarted("run1", numTests); 107 for (int i = 0; i < numTests; i++) { 108 TestDescription tid = new TestDescription("class", "test" + i); 109 mListener.testStarted(tid); 110 mListener.testEnded(tid, new HashMap<String, Metric>()); 111 } 112 mListener.testRunEnded(0, new HashMap<String, Metric>()); 113 114 mListener.testRunStarted("run2", numTests); 115 for (int i = 0; i < numTests; i++) { 116 TestDescription tid = new TestDescription("class2", "test" + i); 117 mListener.testStarted(tid); 118 mListener.testEnded(tid, new HashMap<String, Metric>()); 119 } 120 mListener.testRunEnded(0, new HashMap<String, Metric>()); 121 assertEquals(numTests * 2, mListener.getNumTotalTests()); 122 assertEquals(numTests * 2, mListener.getNumTestsInState(TestStatus.PASSED)); 123 } 124 } 125