Home | History | Annotate | Download | only in tests
      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 package com.android.media.tests;
     17 
     18 import com.android.tradefed.log.LogUtil.CLog;
     19 import com.android.tradefed.metrics.proto.MetricMeasurement.Metric;
     20 import com.android.tradefed.result.ITestInvocationListener;
     21 import com.android.tradefed.result.TestDescription;
     22 import com.android.tradefed.util.proto.TfMetricProtoUtil;
     23 
     24 import java.util.HashMap;
     25 import java.util.Map;
     26 
     27 /** Generic helper class for tests */
     28 public class TestRunHelper {
     29 
     30     private long mTestStartTime = -1;
     31     private long mTestStopTime = -1;
     32     private ITestInvocationListener mListener;
     33     private TestDescription mTestId;
     34 
     35     public TestRunHelper(ITestInvocationListener listener, TestDescription testId) {
     36         mListener = listener;
     37         mTestId = testId;
     38     }
     39 
     40     public long getTotalTestTime() {
     41         return mTestStopTime - mTestStartTime;
     42     }
     43 
     44     public void reportFailure(String errMsg) throws TestFailureException {
     45         CLog.e(errMsg);
     46         mListener.testRunFailed(errMsg);
     47         mListener.testFailed(mTestId, errMsg);
     48         mListener.testEnded(mTestId, new HashMap<String, Metric>());
     49         throw new TestFailureException();
     50     }
     51 
     52     /** @param resultDictionary */
     53     public void endTest(Map<String, String> resultDictionary) {
     54         mTestStopTime = System.currentTimeMillis();
     55         mListener.testRunEnded(getTotalTestTime(), TfMetricProtoUtil.upgradeConvert(resultDictionary));
     56         mListener.testEnded(mTestId, TfMetricProtoUtil.upgradeConvert(resultDictionary));
     57     }
     58 
     59     public void startTest(int numberOfTests) {
     60         mListener.testRunStarted(mTestId.getTestName(), numberOfTests);
     61         mListener.testStarted(mTestId);
     62         mTestStartTime = System.currentTimeMillis();
     63     }
     64 }
     65