blob: 168a130911008dae8a68ba83a41e58ebd8b727b2 [file] [log] [blame]
Kuan-Tung Panfbd59a02017-08-02 19:50:04 +00001/*
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 */
16package com.android.media.tests;
17
Kuan-Tung Panfbd59a02017-08-02 19:50:04 +000018import com.android.tradefed.log.LogUtil.CLog;
19import com.android.tradefed.result.ITestInvocationListener;
jdesprez607ef1a2018-02-07 16:34:53 -080020import com.android.tradefed.result.TestDescription;
Kuan-Tung Panfbd59a02017-08-02 19:50:04 +000021
22import java.util.HashMap;
23import java.util.Map;
24
25/** Generic helper class for tests */
26public class TestRunHelper {
27
28 private long mTestStartTime = -1;
29 private long mTestStopTime = -1;
30 private ITestInvocationListener mListener;
jdesprez607ef1a2018-02-07 16:34:53 -080031 private TestDescription mTestId;
Kuan-Tung Panfbd59a02017-08-02 19:50:04 +000032
jdesprez607ef1a2018-02-07 16:34:53 -080033 public TestRunHelper(ITestInvocationListener listener, TestDescription testId) {
Kuan-Tung Panfbd59a02017-08-02 19:50:04 +000034 mListener = listener;
35 mTestId = testId;
36 }
37
38 public long getTotalTestTime() {
39 return mTestStopTime - mTestStartTime;
40 }
41
Hakan Lindh3e045412017-08-15 16:20:18 -070042 public void reportFailure(String errMsg) throws TestFailureException {
Kuan-Tung Panfbd59a02017-08-02 19:50:04 +000043 CLog.e(errMsg);
Hakan Lindh3e045412017-08-15 16:20:18 -070044 mListener.testRunFailed(errMsg);
Kuan-Tung Panfbd59a02017-08-02 19:50:04 +000045 mListener.testFailed(mTestId, errMsg);
46 mListener.testEnded(mTestId, new HashMap<String, String>());
Hakan Lindh3e045412017-08-15 16:20:18 -070047 throw new TestFailureException();
Kuan-Tung Panfbd59a02017-08-02 19:50:04 +000048 }
49
50 /** @param resultDictionary */
51 public void endTest(Map<String, String> resultDictionary) {
52 mTestStopTime = System.currentTimeMillis();
Kuan-Tung Panfbd59a02017-08-02 19:50:04 +000053 mListener.testRunEnded(getTotalTestTime(), resultDictionary);
Hakan Lindh3e045412017-08-15 16:20:18 -070054 mListener.testEnded(mTestId, resultDictionary);
Kuan-Tung Panfbd59a02017-08-02 19:50:04 +000055 }
56
57 public void startTest(int numberOfTests) {
58 mListener.testRunStarted(mTestId.getTestName(), numberOfTests);
59 mListener.testStarted(mTestId);
60 mTestStartTime = System.currentTimeMillis();
61 }
62}