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.
32  */
33 public class JarHostTest extends HostTest {
34 
35     /** {@inheritDoc} */
36     @Override
run(TestInformation testInfo, ITestInvocationListener listener)37     public void run(TestInformation testInfo, ITestInvocationListener listener)
38             throws DeviceNotAvailableException {
39         // Set test information otherwise it might fail to countTestCases.
40         setTestInformation(testInfo);
41         int numTests = 0;
42         RuntimeException bufferedException = null;
43         try {
44             numTests = countTestCases();
45         } catch (RuntimeException e) {
46             bufferedException = e;
47         }
48         long startTime = System.currentTimeMillis();
49         listener.testRunStarted(getClass().getName(), numTests);
50         HostTestListener hostListener = new HostTestListener(listener);
51         try {
52             if (bufferedException != null) {
53                 throw bufferedException;
54             }
55             super.run(testInfo, hostListener);
56         } finally {
57             HashMap<String, Metric> metrics = hostListener.getNewMetrics();
58             metrics.putAll(TfMetricProtoUtil.upgradeConvert(hostListener.getMetrics()));
59             listener.testRunEnded(System.currentTimeMillis() - startTime, metrics);
60         }
61     }
62 
63     /**
64      * Wrapper listener that forwards all events except testRunStarted() and testRunEnded() to
65      * the embedded listener. Each test class in the jar will invoke these events, which
66      * HostTestListener withholds from listeners for console logging and result reporting.
67      */
68     public class HostTestListener extends ResultForwarder {
69 
70         private Map<String, String> mCollectedMetrics = new HashMap<>();
71         private HashMap<String, Metric> mCollectedNewMetrics = new HashMap<>();
72 
HostTestListener(ITestInvocationListener listener)73         public HostTestListener(ITestInvocationListener listener) {
74             super(listener);
75         }
76 
77         /**
78          * {@inheritDoc}
79          */
80         @Override
testRunStarted(String name, int numTests)81         public void testRunStarted(String name, int numTests) {
82             CLog.d("HostTestListener.testRunStarted(%s, %d)", name, numTests);
83         }
84 
85         /**
86          * {@inheritDoc}
87          */
88         @Override
testRunEnded(long elapsedTime, Map<String, String> metrics)89         public void testRunEnded(long elapsedTime, Map<String, String> metrics) {
90             CLog.d("HostTestListener.testRunEnded(%d, %s)", elapsedTime, metrics.toString());
91             mCollectedMetrics.putAll(metrics);
92         }
93 
94         /**
95          * {@inheritDoc}
96          */
97         @Override
testRunEnded(long elapsedTime, HashMap<String, Metric> metrics)98         public void testRunEnded(long elapsedTime, HashMap<String, Metric> metrics) {
99             CLog.d("HostTestListener.testRunEnded(%d, %s)", elapsedTime, metrics.toString());
100             mCollectedNewMetrics.putAll(metrics);
101         }
102 
103         /**
104          * Returns all the metrics reported by the tests
105          */
getMetrics()106         Map<String, String> getMetrics() {
107             return mCollectedMetrics;
108         }
109 
110         /**
111          * Returns all the proto metrics reported by the tests
112          */
getNewMetrics()113         HashMap<String, Metric> getNewMetrics() {
114             return mCollectedNewMetrics;
115         }
116     }
117 }
118