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.tradefed.testtype; 17 18 import com.android.tradefed.device.DeviceNotAvailableException; 19 import com.android.tradefed.invoker.TestInformation; 20 import com.android.tradefed.log.LogUtil.CLog; 21 import com.android.tradefed.metrics.proto.MetricMeasurement.Metric; 22 import com.android.tradefed.result.ITestInvocationListener; 23 import com.android.tradefed.result.ResultForwarder; 24 import com.android.tradefed.testtype.HostTest; 25 import com.android.tradefed.util.proto.TfMetricProtoUtil; 26 27 import java.util.HashMap; 28 import java.util.Map; 29 30 /** 31 * Test runner for host-side JUnit tests. Package is mis-aligned for historic & backward 32 * compatibility reasons. 33 */ 34 public class JarHostTest extends HostTest { 35 36 /** {@inheritDoc} */ 37 @Override run(TestInformation testInfo, ITestInvocationListener listener)38 public void run(TestInformation testInfo, ITestInvocationListener listener) 39 throws DeviceNotAvailableException { 40 // Set test information otherwise it might fail to countTestCases. 41 setTestInformation(testInfo); 42 int numTests = 0; 43 RuntimeException bufferedException = null; 44 try { 45 numTests = countTestCases(); 46 } catch (RuntimeException e) { 47 bufferedException = e; 48 } 49 long startTime = System.currentTimeMillis(); 50 listener.testRunStarted(getClass().getName(), numTests); 51 HostTestListener hostListener = new HostTestListener(listener); 52 try { 53 if (bufferedException != null) { 54 throw bufferedException; 55 } 56 super.run(testInfo, hostListener); 57 } finally { 58 HashMap<String, Metric> metrics = hostListener.getNewMetrics(); 59 metrics.putAll(TfMetricProtoUtil.upgradeConvert(hostListener.getMetrics())); 60 listener.testRunEnded(System.currentTimeMillis() - startTime, metrics); 61 } 62 } 63 64 /** 65 * Wrapper listener that forwards all events except testRunStarted() and testRunEnded() to the 66 * embedded listener. Each test class in the jar will invoke these events, which 67 * HostTestListener withholds from listeners for console logging and result reporting. 68 */ 69 public class HostTestListener extends ResultForwarder { 70 71 private Map<String, String> mCollectedMetrics = new HashMap<>(); 72 private HashMap<String, Metric> mCollectedNewMetrics = new HashMap<>(); 73 HostTestListener(ITestInvocationListener listener)74 public HostTestListener(ITestInvocationListener listener) { 75 super(listener); 76 } 77 78 /** {@inheritDoc} */ 79 @Override testRunStarted(String name, int numTests)80 public void testRunStarted(String name, int numTests) { 81 CLog.d("HostTestListener.testRunStarted(%s, %d)", name, numTests); 82 } 83 84 /** {@inheritDoc} */ 85 @Override testRunEnded(long elapsedTime, Map<String, String> metrics)86 public void testRunEnded(long elapsedTime, Map<String, String> metrics) { 87 CLog.d("HostTestListener.testRunEnded(%d, %s)", elapsedTime, metrics.toString()); 88 mCollectedMetrics.putAll(metrics); 89 } 90 91 /** {@inheritDoc} */ 92 @Override testRunEnded(long elapsedTime, HashMap<String, Metric> metrics)93 public void testRunEnded(long elapsedTime, HashMap<String, Metric> metrics) { 94 CLog.d("HostTestListener.testRunEnded(%d, %s)", elapsedTime, metrics.toString()); 95 mCollectedNewMetrics.putAll(metrics); 96 } 97 98 /** Returns all the metrics reported by the tests */ getMetrics()99 Map<String, String> getMetrics() { 100 return mCollectedMetrics; 101 } 102 103 /** Returns all the proto metrics reported by the tests */ getNewMetrics()104 HashMap<String, Metric> getNewMetrics() { 105 return mCollectedNewMetrics; 106 } 107 } 108 } 109