Kuan-Tung Pan | fbd59a0 | 2017-08-02 19:50:04 +0000 | [diff] [blame] | 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 | |
Kuan-Tung Pan | fbd59a0 | 2017-08-02 19:50:04 +0000 | [diff] [blame] | 18 | import com.android.tradefed.log.LogUtil.CLog; |
| 19 | import com.android.tradefed.result.ITestInvocationListener; |
jdesprez | 607ef1a | 2018-02-07 16:34:53 -0800 | [diff] [blame^] | 20 | import com.android.tradefed.result.TestDescription; |
Kuan-Tung Pan | fbd59a0 | 2017-08-02 19:50:04 +0000 | [diff] [blame] | 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; |
jdesprez | 607ef1a | 2018-02-07 16:34:53 -0800 | [diff] [blame^] | 31 | private TestDescription mTestId; |
Kuan-Tung Pan | fbd59a0 | 2017-08-02 19:50:04 +0000 | [diff] [blame] | 32 | |
jdesprez | 607ef1a | 2018-02-07 16:34:53 -0800 | [diff] [blame^] | 33 | public TestRunHelper(ITestInvocationListener listener, TestDescription testId) { |
Kuan-Tung Pan | fbd59a0 | 2017-08-02 19:50:04 +0000 | [diff] [blame] | 34 | mListener = listener; |
| 35 | mTestId = testId; |
| 36 | } |
| 37 | |
| 38 | public long getTotalTestTime() { |
| 39 | return mTestStopTime - mTestStartTime; |
| 40 | } |
| 41 | |
Hakan Lindh | 3e04541 | 2017-08-15 16:20:18 -0700 | [diff] [blame] | 42 | public void reportFailure(String errMsg) throws TestFailureException { |
Kuan-Tung Pan | fbd59a0 | 2017-08-02 19:50:04 +0000 | [diff] [blame] | 43 | CLog.e(errMsg); |
Hakan Lindh | 3e04541 | 2017-08-15 16:20:18 -0700 | [diff] [blame] | 44 | mListener.testRunFailed(errMsg); |
Kuan-Tung Pan | fbd59a0 | 2017-08-02 19:50:04 +0000 | [diff] [blame] | 45 | mListener.testFailed(mTestId, errMsg); |
| 46 | mListener.testEnded(mTestId, new HashMap<String, String>()); |
Hakan Lindh | 3e04541 | 2017-08-15 16:20:18 -0700 | [diff] [blame] | 47 | throw new TestFailureException(); |
Kuan-Tung Pan | fbd59a0 | 2017-08-02 19:50:04 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | /** @param resultDictionary */ |
| 51 | public void endTest(Map<String, String> resultDictionary) { |
| 52 | mTestStopTime = System.currentTimeMillis(); |
Kuan-Tung Pan | fbd59a0 | 2017-08-02 19:50:04 +0000 | [diff] [blame] | 53 | mListener.testRunEnded(getTotalTestTime(), resultDictionary); |
Hakan Lindh | 3e04541 | 2017-08-15 16:20:18 -0700 | [diff] [blame] | 54 | mListener.testEnded(mTestId, resultDictionary); |
Kuan-Tung Pan | fbd59a0 | 2017-08-02 19:50:04 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | public void startTest(int numberOfTests) { |
| 58 | mListener.testRunStarted(mTestId.getTestName(), numberOfTests); |
| 59 | mListener.testStarted(mTestId); |
| 60 | mTestStartTime = System.currentTimeMillis(); |
| 61 | } |
| 62 | } |