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.targetprep.multi;
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.TestInformation;
22 import com.android.tradefed.log.LogUtil.CLog;
23 import com.android.tradefed.targetprep.TargetSetupError;
24 
25 import java.util.Map;
26 import java.util.Map.Entry;
27 
28 /** An example implementation of a {@link IMultiTargetPreparer}. */
29 public class HelloWorldMultiTargetPreparer extends BaseMultiTargetPreparer {
30 
31     /** {@inheritDoc} */
32     @Override
setUp(TestInformation testInfo)33     public void setUp(TestInformation testInfo) throws TargetSetupError {
34         Map<ITestDevice, IBuildInfo> deviceBuildInfo = testInfo.getContext().getDeviceBuildMap();
35         if (deviceBuildInfo.entrySet().size() != 2) {
36             ITestDevice device = testInfo.getContext().getDevices().get(0);
37             throw new TargetSetupError("The HelloWorldMultiTargetPreparer assumes 2 devices only.",
38                     device.getDeviceDescriptor());
39         }
40         // This would be the perfect place to do setup that requires multiple devices like
41         // syncing two devices, etc.
42         for (Entry<ITestDevice, IBuildInfo> entry : deviceBuildInfo.entrySet()) {
43             CLog.i("Hello World! multi preparer '%s' with build id '%s'",
44                     entry.getKey().getSerialNumber(), entry.getValue().getBuildId());
45         }
46         // Possible look up using the context instead: Getting all the device names configured in
47         // the xml.
48         CLog.i("The device names configured are: %s", testInfo.getContext().getDeviceConfigNames());
49     }
50 
51     @Override
tearDown(TestInformation testInfo, Throwable e)52     public void tearDown(TestInformation testInfo, Throwable e) throws DeviceNotAvailableException {
53         Map<ITestDevice, IBuildInfo> deviceBuildInfo = testInfo.getContext().getDeviceBuildMap();
54         for (Entry<ITestDevice, IBuildInfo> entry : deviceBuildInfo.entrySet()) {
55             CLog.i("Hello World! multi tear down '%s' with build id '%s'",
56                     entry.getKey().getSerialNumber(), entry.getValue().getBuildId());
57         }
58     }
59 }
60