1 /*
2  * Copyright (C) 2016 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;
17 
18 import com.android.tradefed.device.DeviceNotAvailableException;
19 import com.android.tradefed.device.ITestDevice;
20 import com.android.tradefed.invoker.IInvocationContext;
21 import com.android.tradefed.invoker.TestInformation;
22 import com.android.tradefed.log.LogUtil.CLog;
23 import com.android.tradefed.result.ITestInvocationListener;
24 import com.android.tradefed.testtype.IInvocationContextReceiver;
25 import com.android.tradefed.testtype.IRemoteTest;
26 
27 import org.junit.Assert;
28 
29 /**
30  * Hello world example of Multiple Devices support in Trade Federation. We implements the existing
31  * {@link IRemoteTest} interface to be a TradeFed Tests, and you can implement {@link
32  * IInvocationContextReceiver} to get the full invocation metadata. In this example we implement
33  * both but you should only implement one or the other.
34  */
35 public class HelloWorldMultiDevices implements IRemoteTest {
36 
37     @Override
run(TestInformation testInfo, ITestInvocationListener listener)38     public void run(TestInformation testInfo, ITestInvocationListener listener)
39             throws DeviceNotAvailableException {
40         IInvocationContext context = testInfo.getContext();
41         // We can also use the IInvocationContext information, which have various functions to
42         // access the ITestDevice or IBuildInfo.
43         for (ITestDevice device : context.getDevices()) {
44             CLog.i(
45                     "Hello World!  device '%s' from context with build '%s'",
46                     device.getSerialNumber(), context.getBuildInfo(device));
47         }
48 
49         // We can do a look up by the device name in the configuration using the IInvocationContext
50         for (String deviceName : context.getDeviceConfigNames()) {
51             CLog.i(
52                     "device '%s' has the name '%s' in the config.",
53                     context.getDevice(deviceName).getSerialNumber(), deviceName);
54         }
55 
56         // if the device name is known, doing a direct look up is possible.
57         Assert.assertNotNull(context.getDevice("device1"));
58         CLog.i(
59                 "device named device1 direct look up is '%s'",
60                 context.getDevice("device1").getSerialNumber());
61     }
62 }
63