1 /* 2 * Copyright (C) 2011 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.tradefed.testtype; 17 18 import com.android.tradefed.device.DeviceNotAvailableException; 19 import com.android.tradefed.invoker.TestInformation; 20 import com.android.tradefed.metrics.proto.MetricMeasurement.Metric; 21 import com.android.tradefed.result.ITestInvocationListener; 22 import com.android.tradefed.result.JUnitToInvocationResultForwarder; 23 import com.android.tradefed.testtype.DeviceTestResult.RuntimeDeviceNotAvailableException; 24 import com.android.tradefed.testtype.junit4.CarryInterruptedException; 25 26 import junit.framework.Test; 27 import junit.framework.TestResult; 28 29 import java.util.HashMap; 30 31 /** 32 * A helper class for directing a {@link IRemoteTest#run(TestInformation, ITestInvocationListener)} 33 * call to a {@link Test#run(TestResult)} call. 34 */ 35 public class JUnitRunUtil { 36 runTest(ITestInvocationListener listener, Test junitTest)37 public static boolean runTest(ITestInvocationListener listener, Test junitTest) 38 throws DeviceNotAvailableException { 39 return runTest(listener, junitTest, junitTest.getClass().getName()); 40 } 41 runTest(ITestInvocationListener listener, Test junitTest, String runName)42 public static boolean runTest(ITestInvocationListener listener, Test junitTest, String runName) 43 throws DeviceNotAvailableException { 44 return runTest(listener, junitTest, runName, null); 45 } 46 runTest( ITestInvocationListener listener, Test junitTest, String runName, TestInformation testInfo)47 public static boolean runTest( 48 ITestInvocationListener listener, 49 Test junitTest, 50 String runName, 51 TestInformation testInfo) 52 throws DeviceNotAvailableException { 53 if (junitTest.countTestCases() == 0) { 54 return false; 55 } 56 listener.testRunStarted(runName, junitTest.countTestCases()); 57 long startTime = System.currentTimeMillis(); 58 // forward the JUnit results to the invocation listener 59 JUnitToInvocationResultForwarder resultForwarder = 60 new JUnitToInvocationResultForwarder(listener); 61 DeviceTestResult result = new DeviceTestResult(); 62 result.setTestInfo(testInfo); 63 result.addListener(resultForwarder); 64 try { 65 junitTest.run(result); 66 } catch (RuntimeDeviceNotAvailableException e) { 67 listener.testRunFailed(e.getDeviceException().getMessage()); 68 throw e.getDeviceException(); 69 } catch (CarryInterruptedException e) { 70 listener.testRunFailed("Test Phase Timeout Reached."); 71 } finally { 72 listener.testRunEnded( 73 System.currentTimeMillis() - startTime, new HashMap<String, Metric>()); 74 } 75 return true; 76 } 77 } 78