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 17 package com.android.compatibility.common.tradefed.result; 18 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.assertFalse; 21 import static org.junit.Assert.assertTrue; 22 23 import com.android.tradefed.config.OptionSetter; 24 import com.android.tradefed.invoker.IInvocationContext; 25 import com.android.tradefed.metrics.proto.MetricMeasurement.Metric; 26 import com.android.tradefed.result.TestDescription; 27 import com.android.tradefed.util.AbiUtils; 28 29 import org.junit.After; 30 import org.junit.Before; 31 import org.junit.Test; 32 import org.junit.runner.RunWith; 33 import org.junit.runners.JUnit4; 34 35 import java.util.HashMap; 36 37 /** 38 * Unit tests for {@link ConsoleReporter}. 39 */ 40 @RunWith(JUnit4.class) 41 public class ConsoleReporterTest { 42 43 private static final String NAME = "ModuleName"; 44 private static final String NAME2 = "ModuleName2"; 45 private static final String ABI = "mips64"; 46 private static final String ID = AbiUtils.createId(ABI, NAME); 47 private static final String ID2 = AbiUtils.createId(ABI, NAME2); 48 private static final String CLASS = "android.test.FoorBar"; 49 private static final String METHOD_1 = "testBlah1"; 50 private static final String METHOD_2 = "testBlah2"; 51 private static final String METHOD_3 = "testBlah3"; 52 private static final String STACK_TRACE = "Something small is not alright\n " + 53 "at four.big.insects.Marley.sing(Marley.java:10)"; 54 55 private ConsoleReporter mReporter; 56 private IInvocationContext mContext; 57 58 @Before setUp()59 public void setUp() throws Exception { 60 mReporter = new ConsoleReporter(); 61 OptionSetter setter = new OptionSetter(mReporter); 62 setter.setOptionValue("quiet-output", "true"); 63 } 64 65 @After tearDown()66 public void tearDown() throws Exception { 67 mReporter = null; 68 } 69 70 @Test testResultReporting_singleModule()71 public void testResultReporting_singleModule() throws Exception { 72 mReporter.invocationStarted(mContext); 73 mReporter.testRunStarted(ID, 4); 74 runTests(); 75 76 mReporter.testRunEnded(10, new HashMap<String, String>()); 77 mReporter.invocationEnded(10); 78 79 assertEquals(ID, mReporter.getModuleId()); 80 assertEquals(2, mReporter.getFailedTests()); 81 assertEquals(1, mReporter.getPassedTests()); 82 assertEquals(4, mReporter.getCurrentTestNum()); 83 assertEquals(4, mReporter.getTotalTestsInModule()); 84 } 85 86 @Test testResultReporting_multipleModules()87 public void testResultReporting_multipleModules() throws Exception { 88 mReporter.invocationStarted(mContext); 89 mReporter.testRunStarted(ID, 4); 90 runTests(); 91 92 assertEquals(ID, mReporter.getModuleId()); 93 assertEquals(2, mReporter.getFailedTests()); 94 assertEquals(1, mReporter.getPassedTests()); 95 assertEquals(4, mReporter.getCurrentTestNum()); 96 assertEquals(4, mReporter.getTotalTestsInModule()); 97 98 // Should reset counters 99 mReporter.testRunStarted(ID2, 4); 100 assertEquals(ID2, mReporter.getModuleId()); 101 assertEquals(0, mReporter.getFailedTests()); 102 assertEquals(0, mReporter.getPassedTests()); 103 assertEquals(0, mReporter.getCurrentTestNum()); 104 assertEquals(4, mReporter.getTotalTestsInModule()); 105 } 106 107 /** Run 4 test, but one is ignored */ runTests()108 private void runTests() { 109 TestDescription test1 = new TestDescription(CLASS, METHOD_1); 110 mReporter.testStarted(test1); 111 mReporter.testEnded(test1, new HashMap<String, Metric>()); 112 assertFalse(mReporter.getTestFailed()); 113 114 TestDescription test2 = new TestDescription(CLASS, METHOD_2); 115 mReporter.testStarted(test2); 116 assertFalse(mReporter.getTestFailed()); 117 mReporter.testFailed(test2, STACK_TRACE); 118 assertTrue(mReporter.getTestFailed()); 119 120 TestDescription test3 = new TestDescription(CLASS, METHOD_3); 121 mReporter.testStarted(test3); 122 assertFalse(mReporter.getTestFailed()); 123 mReporter.testFailed(test3, STACK_TRACE); 124 assertTrue(mReporter.getTestFailed()); 125 126 TestDescription test4 = new TestDescription(CLASS, METHOD_3); 127 mReporter.testStarted(test4); 128 assertFalse(mReporter.getTestFailed()); 129 mReporter.testIgnored(test4); 130 assertFalse(mReporter.getTestFailed()); 131 } 132 } 133