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.build.IBuildInfo; 19 import com.android.tradefed.device.DeviceNotAvailableException; 20 import com.android.tradefed.device.ITestDevice; 21 import com.android.tradefed.invoker.IInvocationContext; 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.IMultiDeviceTest; 26 import com.android.tradefed.testtype.IRemoteTest; 27 28 import org.junit.Assert; 29 30 import java.util.Map; 31 import java.util.Map.Entry; 32 33 /** 34 * Hello world example of Multiple Devices support in Trade Federation. 35 * </p> 36 * We implements the existing {@link IRemoteTest} interface to be a TradeFed Tests, and we can now 37 * also implement the new {@link IMultiDeviceTest} to get all the device informations for our test. 38 * OR you can implement {@link IInvocationContextReceiver} to get the full invocation metadata. 39 * </p> 40 * In this example we implement both but you should only implement one or the other. 41 */ 42 public class HelloWorldMultiDevices implements IRemoteTest, IMultiDeviceTest, 43 IInvocationContextReceiver { 44 45 private Map<ITestDevice, IBuildInfo> mDevicesInfos; 46 private IInvocationContext mContext; 47 48 @Override 49 public void setDeviceInfos(Map<ITestDevice, IBuildInfo> deviceInfos) { 50 mDevicesInfos = deviceInfos; 51 } 52 53 @Override 54 public void setInvocationContext(IInvocationContext invocationContext) { 55 mContext = invocationContext; 56 } 57 58 @Override 59 public void run(ITestInvocationListener listener) throws DeviceNotAvailableException { 60 // This is going to iterate over all the devices in the configuration using the 61 // {@link IMultiDeviceTest} interface 62 for (Entry<ITestDevice, IBuildInfo> entry : mDevicesInfos.entrySet()) { 63 CLog.i("Hello World! device '%s' with build id '%s'", entry.getKey().getSerialNumber(), 64 entry.getValue().getBuildId()); 65 } 66 67 // We can also use the IInvocationContext information, which have various functions to 68 // access the ITestDevice or IBuildInfo. 69 for (ITestDevice device : mContext.getDevices()) { 70 CLog.i("Hello World! device '%s' from context with build '%s'", 71 device.getSerialNumber(), mContext.getBuildInfo(device)); 72 } 73 74 // We can do a look up by the device name in the configuration using the IInvocationContext 75 for (String deviceName : mContext.getDeviceConfigNames()) { 76 CLog.i( 77 "device '%s' has the name '%s' in the config.", 78 mContext.getDevice(deviceName).getSerialNumber(), deviceName); 79 } 80 81 // if the device name is known, doing a direct look up is possible. 82 Assert.assertNotNull(mContext.getDevice("device1")); 83 CLog.i( 84 "device named device1 direct look up is '%s'", 85 mContext.getDevice("device1").getSerialNumber()); 86 } 87 } 88