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. We implements the existing 35 * {@link IRemoteTest} interface to be a TradeFed Tests, and we can now also implement the new 36 * {@link IMultiDeviceTest} to get all the device informations for our test. OR you can implement 37 * {@link IInvocationContextReceiver} to get the full invocation metadata. In this example we 38 * implement both but you should only implement one or the other. 39 */ 40 public class HelloWorldMultiDevices 41 implements IRemoteTest, IMultiDeviceTest, IInvocationContextReceiver { 42 43 private Map<ITestDevice, IBuildInfo> mDevicesInfos; 44 private IInvocationContext mContext; 45 46 @Override setDeviceInfos(Map<ITestDevice, IBuildInfo> deviceInfos)47 public void setDeviceInfos(Map<ITestDevice, IBuildInfo> deviceInfos) { 48 mDevicesInfos = deviceInfos; 49 } 50 51 @Override setInvocationContext(IInvocationContext invocationContext)52 public void setInvocationContext(IInvocationContext invocationContext) { 53 mContext = invocationContext; 54 } 55 56 @Override run(ITestInvocationListener listener)57 public void run(ITestInvocationListener listener) throws DeviceNotAvailableException { 58 // This is going to iterate over all the devices in the configuration using the 59 // {@link IMultiDeviceTest} interface 60 for (Entry<ITestDevice, IBuildInfo> entry : mDevicesInfos.entrySet()) { 61 CLog.i( 62 "Hello World! device '%s' with build id '%s'", 63 entry.getKey().getSerialNumber(), entry.getValue().getBuildId()); 64 } 65 66 // We can also use the IInvocationContext information, which have various functions to 67 // access the ITestDevice or IBuildInfo. 68 for (ITestDevice device : mContext.getDevices()) { 69 CLog.i( 70 "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