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