1 /* 2 * Copyright (C) 2010 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.device.ITestDevice; 20 import com.android.tradefed.result.ITestInvocationListener; 21 22 import junit.framework.Test; 23 import junit.framework.TestResult; 24 import junit.framework.TestSuite; 25 26 /** 27 * Helper JUnit test suite that provides the {@link IRemoteTest} and {@link IDeviceTest} services. 28 */ 29 public class DeviceTestSuite extends TestSuite implements IDeviceTest, IRemoteTest { 30 31 private ITestDevice mDevice = null; 32 DeviceTestSuite(Class<?> testClass)33 public DeviceTestSuite(Class<?> testClass) { 34 super(testClass); 35 } 36 DeviceTestSuite()37 public DeviceTestSuite() { 38 super(); 39 } 40 41 /** 42 * {@inheritDoc} 43 */ 44 @Override getDevice()45 public ITestDevice getDevice() { 46 return mDevice; 47 } 48 49 /** 50 * {@inheritDoc} 51 */ 52 @Override setDevice(ITestDevice device)53 public void setDevice(ITestDevice device) { 54 mDevice = device; 55 } 56 57 /** 58 * {@inheritDoc} 59 */ 60 @Override run(ITestInvocationListener listener)61 public void run(ITestInvocationListener listener) throws DeviceNotAvailableException { 62 JUnitRunUtil.runTest(listener, this); 63 } 64 65 /** 66 * Adds the tests from the given class to the suite 67 */ 68 @SuppressWarnings("rawtypes") 69 @Override addTestSuite(Class testClass)70 public void addTestSuite(Class testClass) { 71 addTest(new DeviceTestSuite(testClass)); 72 } 73 74 /** 75 * Overrides parent method to pass in device to included test 76 */ 77 @Override runTest(Test test, TestResult result)78 public void runTest(Test test, TestResult result) { 79 if (test instanceof IDeviceTest) { 80 IDeviceTest deviceTest = (IDeviceTest)test; 81 deviceTest.setDevice(mDevice); 82 } 83 test.run(result); 84 } 85 } 86