1 /*
2  * Copyright (C) 2017 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 
17 package com.android.tradefed.util.clockwork;
18 
19 import com.android.tradefed.build.IBuildInfo;
20 import com.android.tradefed.device.DeviceNotAvailableException;
21 import com.android.tradefed.device.ITestDevice;
22 import java.util.List;
23 import java.util.Map;
24 
25 /** A clockwork utility for sharing multi-device logic */
26 public class ClockworkUtils {
27     /**
28      * Helper method to share multi-device setup that returns an ITestDevice for the companion and
29      * fills the deviceInfos list with watches
30      *
31      * @param deviceInfos device infos provided
32      * @param deviceList device list to fill
33      * @return {@link ITestDevice} companion device
34      */
setUpMultiDevice( Map<ITestDevice, IBuildInfo> deviceInfos, List<ITestDevice> deviceList)35     public ITestDevice setUpMultiDevice(
36             Map<ITestDevice, IBuildInfo> deviceInfos, List<ITestDevice> deviceList) {
37         ITestDevice companion = null;
38         if (deviceInfos.size() < 2) {
39             throw new RuntimeException(
40                     "there should be at least two devices for clockwork multiple device test");
41         }
42         for (Map.Entry<ITestDevice, IBuildInfo> entry : deviceInfos.entrySet()) {
43             try {
44                 String deviceBuildPropertyString =
45                         entry.getKey().getProperty("ro.build.characteristics");
46                 if (deviceBuildPropertyString.contains("watch")) {
47                     deviceList.add(entry.getKey());
48                 } else {
49                     if (companion != null) {
50                         throw new RuntimeException(
51                                 "there should be only one companion in the test");
52                     }
53                     companion = entry.getKey();
54                 }
55             } catch (DeviceNotAvailableException e) {
56                 throw new RuntimeException(
57                         "device not available, cannot get device build property to determine "
58                                 + "companion/watch device");
59             }
60         }
61         if (companion == null) {
62             throw new RuntimeException("no companion device found in the test");
63         }
64         return companion;
65     }
66 }
67