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 17 package com.android.compatibility.common.tradefed.result; 18 19 import com.android.compatibility.common.tradefed.build.CompatibilityBuildHelper; 20 import com.android.compatibility.common.util.AbiUtils; 21 import com.android.tradefed.build.BuildInfo; 22 import com.android.tradefed.build.IBuildInfo; 23 import com.android.tradefed.config.OptionSetter; 24 import com.android.tradefed.invoker.IInvocationContext; 25 import com.android.tradefed.invoker.InvocationContext; 26 import com.android.tradefed.metrics.proto.MetricMeasurement.Metric; 27 import com.android.tradefed.result.TestDescription; 28 import com.android.tradefed.util.FileUtil; 29 import com.android.tradefed.util.RunUtil; 30 31 import junit.framework.TestCase; 32 33 import java.io.File; 34 import java.util.Collection; 35 import java.util.HashMap; 36 37 /** 38 * Unit Tests for {@link MetadataReporter} 39 */ 40 public class MetadataReporterTest extends TestCase { 41 42 private static final String MIN_TEST_DURATION = "10"; 43 private static final String BUILD_NUMBER = "2"; 44 private static final String ROOT_DIR_NAME = "root"; 45 private static final String BASE_DIR_NAME = "android-tests"; 46 private static final String TESTCASES = "testcases"; 47 private static final String NAME = "ModuleName"; 48 private static final String ABI = "mips64"; 49 private static final String ID = AbiUtils.createId(ABI, NAME); 50 private static final String CLASS = "android.test.FoorBar"; 51 private static final String METHOD_1 = "testBlah1"; 52 private static final String METHOD_2 = "testBlah2"; 53 private static final String METHOD_3 = "testBlah3"; 54 private static final String STACK_TRACE = "Something small is not alright\n " + 55 "at four.big.insects.Marley.sing(Marley.java:10)"; 56 57 private MetadataReporter mReporter; 58 private IBuildInfo mBuildInfo; 59 private IInvocationContext mContext; 60 61 private File mRoot = null; 62 private File mBase = null; 63 private File mTests = null; 64 65 @Override setUp()66 public void setUp() throws Exception { 67 mReporter = new MetadataReporter(); 68 OptionSetter setter = new OptionSetter(mReporter); 69 setter.setOptionValue("min-test-duration", MIN_TEST_DURATION); 70 mRoot = FileUtil.createTempDir(ROOT_DIR_NAME); 71 mBase = new File(mRoot, BASE_DIR_NAME); 72 mBase.mkdirs(); 73 mTests = new File(mBase, TESTCASES); 74 mTests.mkdirs(); 75 mBuildInfo = new BuildInfo(BUILD_NUMBER, ""); 76 mBuildInfo.addBuildAttribute(CompatibilityBuildHelper.ROOT_DIR, mRoot.getAbsolutePath()); 77 mBuildInfo.addBuildAttribute(CompatibilityBuildHelper.SUITE_NAME, "tests"); 78 mBuildInfo.addBuildAttribute(CompatibilityBuildHelper.START_TIME_MS, "0"); 79 mContext = new InvocationContext(); 80 mContext.addDeviceBuildInfo("fakeDevice", mBuildInfo); 81 } 82 83 @Override tearDown()84 public void tearDown() throws Exception { 85 mReporter = null; 86 FileUtil.recursiveDelete(mRoot); 87 } 88 89 /** 90 * Test that when tests execute faster than the threshold we do not report then. 91 */ testResultReportingFastTests()92 public void testResultReportingFastTests() throws Exception { 93 mReporter.invocationStarted(mContext); 94 mReporter.testRunStarted(ID, 3); 95 runTests(0l); 96 Collection<MetadataReporter.TestMetadata> metadata = mReporter.getTestMetadata(); 97 assertTrue(metadata.isEmpty()); 98 mReporter.testRunEnded(10, new HashMap<String, String>()); 99 mReporter.invocationEnded(10); 100 } 101 102 /** 103 * Test that when tests execute slower than the limit we report them if they passed. 104 */ testResultReportingSlowTests()105 public void testResultReportingSlowTests() throws Exception { 106 mReporter.invocationStarted(mContext); 107 mReporter.testRunStarted(ID, 3); 108 runTests(50l); 109 110 Collection<MetadataReporter.TestMetadata> metadata = mReporter.getTestMetadata(); 111 assertEquals(metadata.size(), 2); // Two passing slow tests. 112 113 mReporter.testRunEnded(10, new HashMap<String, String>()); 114 mReporter.invocationEnded(10); 115 } 116 117 /** Run 4 test. */ runTests(long waitTime)118 private void runTests(long waitTime) { 119 TestDescription test1 = new TestDescription(CLASS, METHOD_1); 120 mReporter.testStarted(test1); 121 RunUtil.getDefault().sleep(waitTime); 122 mReporter.testEnded(test1, new HashMap<String, Metric>()); 123 124 TestDescription test2 = new TestDescription(CLASS, METHOD_2); 125 mReporter.testStarted(test2); 126 RunUtil.getDefault().sleep(waitTime); 127 mReporter.testEnded(test1, new HashMap<String, Metric>()); 128 129 TestDescription test3 = new TestDescription(CLASS, METHOD_3); 130 mReporter.testStarted(test3); 131 RunUtil.getDefault().sleep(waitTime); 132 mReporter.testFailed(test3, STACK_TRACE); 133 mReporter.testEnded(test3, new HashMap<String, Metric>()); 134 135 TestDescription test4 = new TestDescription(CLASS, METHOD_3); 136 mReporter.testStarted(test4); 137 RunUtil.getDefault().sleep(waitTime); 138 mReporter.testIgnored(test4); 139 mReporter.testEnded(test4, new HashMap<String, Metric>()); 140 } 141 } 142