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