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.invoker.TestInformation;
21 import com.android.tradefed.result.ITestInvocationListener;
22 
23 import junit.framework.Test;
24 import junit.framework.TestResult;
25 import junit.framework.TestSuite;
26 
27 /**
28  *  Helper JUnit test suite that provides the {@link IRemoteTest} and {@link IDeviceTest} services.
29  */
30 public class DeviceTestSuite extends TestSuite implements IDeviceTest, IRemoteTest {
31 
32     private ITestDevice mDevice = null;
33 
DeviceTestSuite(Class<?> testClass)34     public DeviceTestSuite(Class<?> testClass) {
35         super(testClass);
36     }
37 
DeviceTestSuite()38     public DeviceTestSuite() {
39         super();
40     }
41 
42     /**
43      * {@inheritDoc}
44      */
45     @Override
getDevice()46     public ITestDevice getDevice() {
47         return mDevice;
48     }
49 
50     /**
51      * {@inheritDoc}
52      */
53     @Override
setDevice(ITestDevice device)54     public void setDevice(ITestDevice device) {
55         mDevice = device;
56     }
57 
58     /** {@inheritDoc} */
59     @Override
run(TestInformation testInfo, ITestInvocationListener listener)60     public void run(TestInformation testInfo, ITestInvocationListener listener)
61             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