Skip to content

Commit 60e7913

Browse files
DeviceInfracopybara-github
authored andcommitted
Internal change
PiperOrigin-RevId: 871470966
1 parent 7c58f0e commit 60e7913

File tree

10 files changed

+275
-0
lines changed

10 files changed

+275
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2022 Google LLC
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+
* https://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.google.devtools.mobileharness.api.devicemanager.detector;
18+
19+
import static com.google.common.collect.ImmutableList.toImmutableList;
20+
21+
import com.google.devtools.mobileharness.api.devicemanager.detector.model.DetectionResult;
22+
import com.google.devtools.mobileharness.api.devicemanager.detector.model.DetectionResult.DetectionType;
23+
import com.google.devtools.mobileharness.shared.util.flags.Flags;
24+
import java.util.List;
25+
import java.util.stream.IntStream;
26+
27+
/**
28+
* Detector to detect AndroidDesktopExecutorDevices. The number of devices detected is based on the
29+
* value of the flag AndroidDesktopExecutorDevicesNum. The device IDs of each device is
30+
* "al_device_$i" where i is 1 to AndroidDesktopExecutorDevicesNum.
31+
*/
32+
public class AndroidDesktopExecutorDetector implements Detector {
33+
34+
@Override
35+
public boolean precondition() {
36+
return Flags.instance().androidDesktopExecutorDevicesNum.getNonNull() > 0;
37+
}
38+
39+
@Override
40+
public List<DetectionResult> detectDevices() {
41+
return IntStream.range(1, Flags.instance().androidDesktopExecutorDevicesNum.getNonNull() + 1)
42+
.mapToObj(i -> "al_device_" + i)
43+
.map(deviceId -> DetectionResult.of(deviceId, DetectionType.ANDROID_DESKTOP_EXECUTOR))
44+
.collect(toImmutableList());
45+
}
46+
}

src/java/com/google/devtools/mobileharness/api/devicemanager/detector/BUILD

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,17 @@ java_library(
4040
],
4141
)
4242

43+
java_library(
44+
name = "android_desktop_executor_detector",
45+
srcs = ["AndroidDesktopExecutorDetector.java"],
46+
deps = [
47+
":base",
48+
"//src/java/com/google/devtools/mobileharness/api/devicemanager/detector/model",
49+
"//src/java/com/google/devtools/mobileharness/shared/util/flags",
50+
"@maven//:com_google_guava_guava",
51+
],
52+
)
53+
4354
java_library(
4455
name = "base",
4556
srcs = ["Detector.java"],
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright 2022 Google LLC
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+
* https://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.google.devtools.mobileharness.api.devicemanager.dispatcher;
18+
19+
import com.google.devtools.mobileharness.api.devicemanager.detector.model.DetectionResult;
20+
import com.google.devtools.mobileharness.api.devicemanager.detector.model.DetectionResult.DetectionType;
21+
import com.google.devtools.mobileharness.api.devicemanager.detector.model.DetectionResults;
22+
import com.google.devtools.mobileharness.api.devicemanager.dispatcher.model.DispatchResult.DispatchType;
23+
import com.google.devtools.mobileharness.api.devicemanager.dispatcher.model.DispatchResults;
24+
import com.google.devtools.mobileharness.api.devicemanager.dispatcher.util.DeviceIdGenerator;
25+
import com.google.devtools.mobileharness.api.model.lab.DeviceId;
26+
import java.util.HashMap;
27+
import java.util.Map;
28+
import java.util.stream.Collectors;
29+
30+
/** Dispatcher for AndroidDesktopExecutor device. */
31+
public class AndroidDesktopExecutorDeviceDispatcher implements Dispatcher {
32+
33+
private final DeviceIdGenerator deviceIdGenerator;
34+
35+
public AndroidDesktopExecutorDeviceDispatcher() {
36+
this(new DeviceIdGenerator());
37+
}
38+
39+
public AndroidDesktopExecutorDeviceDispatcher(DeviceIdGenerator deviceIdGenerator) {
40+
this.deviceIdGenerator = deviceIdGenerator;
41+
}
42+
43+
@Override
44+
public Map<String, DispatchType> dispatchDevices(
45+
DetectionResults detectionResults, DispatchResults dispatchResults) {
46+
return detectionResults.getByType(DetectionType.ANDROID_DESKTOP_EXECUTOR).stream()
47+
.collect(
48+
Collectors.toMap(
49+
DetectionResult::deviceControlId,
50+
unused -> DispatchType.LIVE,
51+
(a, b) -> a,
52+
HashMap::new));
53+
}
54+
55+
@Override
56+
public DeviceId generateDeviceId(String deviceControlId) {
57+
return deviceIdGenerator.getNoOpDeviceId(deviceControlId);
58+
}
59+
}

src/java/com/google/devtools/mobileharness/api/devicemanager/dispatcher/BUILD

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,21 @@ java_library(
108108
],
109109
)
110110

111+
java_library(
112+
name = "android_desktop_executor_device",
113+
srcs = ["AndroidDesktopExecutorDeviceDispatcher.java"],
114+
runtime_deps = [
115+
"//src/java/com/google/wireless/qa/mobileharness/shared/api/device:android_desktop_executor_device",
116+
],
117+
deps = [
118+
":base",
119+
"//src/java/com/google/devtools/mobileharness/api/devicemanager/detector/model",
120+
"//src/java/com/google/devtools/mobileharness/api/devicemanager/dispatcher/model",
121+
"//src/java/com/google/devtools/mobileharness/api/devicemanager/dispatcher/util:device_id_descriptor_generator",
122+
"//src/java/com/google/devtools/mobileharness/api/model/lab:device_id_descriptor",
123+
],
124+
)
125+
111126
java_library(
112127
name = "failed_device",
113128
srcs = ["FailedDeviceDispatcher.java"],

src/java/com/google/devtools/mobileharness/infra/controller/device/bootstrap/AllDetectorsAndDispatchers.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ public static ImmutableList<Detector> detectorCandidatesForLabServerOss() {
6262
detectorCandidatesForLocalModeInternalOssAndLabServerOss() {
6363
ImmutableList.Builder<Detector> detectorCandidates = ImmutableList.builder();
6464

65+
// Android Desktop Executor detector.
66+
if (Flags.instance().androidDesktopExecutorDevicesNum.getNonNull() > 0) {
67+
detectorCandidates.add(createDetector("AndroidDesktopExecutorDetector"));
68+
return detectorCandidates.build();
69+
}
70+
6571
// ADB detector.
6672
if (Flags.instance().detectAdbDevice.getNonNull()) {
6773
detectorCandidates.add(new AdbDetector());
@@ -78,6 +84,7 @@ public static ImmutableList<Detector> detectorCandidatesForLabServerOss() {
7884
if (Flags.instance().noOpDeviceNum.getNonNull() > 0) {
7985
detectorCandidates.add(new NoOpDeviceDetector());
8086
}
87+
8188
return detectorCandidates.build();
8289
}
8390

@@ -112,6 +119,11 @@ private static void addDispatchersForAll(DispatcherManager dispatcherManager) {
112119
dispatcherManager.add(loadDispatcherClass("AndroidJitEmulatorDispatcher"));
113120
}
114121
}
122+
123+
// Android Desktop Executor dispatcher.
124+
if (Flags.instance().androidDesktopExecutorDevicesNum.getNonNull() > 0) {
125+
dispatcherManager.add(loadDispatcherClass("AndroidDesktopExecutorDeviceDispatcher"));
126+
}
115127
}
116128

117129
private static void addDispatchersForLocalModeLabServerOss(DispatcherManager dispatcherManager) {

src/java/com/google/devtools/mobileharness/infra/controller/device/bootstrap/BUILD

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ java_library(
2525
visibility = [
2626
"//src/javatests/com/google/devtools/mobileharness/infra/controller/device/bootstrap:__subpackages__",
2727
],
28+
runtime_deps = [
29+
"//src/java/com/google/devtools/mobileharness/api/devicemanager/detector:android_desktop_executor_detector",
30+
"//src/java/com/google/devtools/mobileharness/api/devicemanager/dispatcher:android_desktop_executor_device",
31+
],
2832
deps = [
2933
"//src/java/com/google/devtools/mobileharness/api/devicemanager/detector:adb",
3034
"//src/java/com/google/devtools/mobileharness/api/devicemanager/detector:base",

src/java/com/google/devtools/mobileharness/shared/util/flags/Flags.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,16 @@ public class Flags {
243243
converter = Flag.StringConverter.class)
244244
public Flag<String> androidDesktopExecutorGroup = androidDesktopExecutorGroupDefault;
245245

246+
private static final Flag<Integer> androidDesktopExecutorDevicesNumDefault = Flag.value(0);
247+
248+
@com.beust.jcommander.Parameter(
249+
names = "--android_desktop_executor_devices_num",
250+
description =
251+
"If the number is greater than 0, the labserver will create that many number of"
252+
+ " AndroidDesktopExecutorDevices.",
253+
converter = Flag.IntegerConverter.class)
254+
public Flag<Integer> androidDesktopExecutorDevicesNum = androidDesktopExecutorDevicesNumDefault;
255+
246256
private static final Flag<Boolean> enableDaemonDefault = Flag.value(true);
247257

248258
@com.beust.jcommander.Parameter(
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2022 Google LLC
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+
* https://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.google.devtools.mobileharness.api.devicemanager.detector;
18+
19+
import static com.google.common.flags.Flags.disableStateCheckingForTest;
20+
import static com.google.common.truth.Truth.assertThat;
21+
22+
import com.google.devtools.mobileharness.api.devicemanager.detector.model.DetectionResult;
23+
import com.google.devtools.mobileharness.shared.util.flags.Flags;
24+
import java.util.List;
25+
import org.junit.Test;
26+
import org.junit.runner.RunWith;
27+
import org.junit.runners.JUnit4;
28+
29+
@RunWith(JUnit4.class)
30+
public final class AndroidDesktopExecutorDetectorTest {
31+
@Test
32+
public void detectDevices_returnsExpectedDevices() {
33+
disableStateCheckingForTest();
34+
Flags.instance().androidDesktopExecutorDevicesNum.setForTest(3);
35+
AndroidDesktopExecutorDetector detector = new AndroidDesktopExecutorDetector();
36+
37+
List<DetectionResult> detectionResults = detector.detectDevices();
38+
39+
assertThat(detectionResults).hasSize(3);
40+
assertThat(detectionResults.get(0).deviceControlId()).isEqualTo("al_device_1");
41+
assertThat(detectionResults.get(1).deviceControlId()).isEqualTo("al_device_2");
42+
assertThat(detectionResults.get(2).deviceControlId()).isEqualTo("al_device_3");
43+
}
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright 2022 Google LLC
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+
* https://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.google.devtools.mobileharness.api.devicemanager.dispatcher;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
import static org.mockito.Mockito.when;
21+
22+
import com.google.common.collect.ImmutableList;
23+
import com.google.devtools.mobileharness.api.devicemanager.detector.model.DetectionResult;
24+
import com.google.devtools.mobileharness.api.devicemanager.detector.model.DetectionResult.DetectionType;
25+
import com.google.devtools.mobileharness.api.devicemanager.detector.model.DetectionResults;
26+
import com.google.devtools.mobileharness.api.devicemanager.dispatcher.model.DispatchResult.DispatchType;
27+
import com.google.devtools.mobileharness.api.devicemanager.dispatcher.model.DispatchResults;
28+
import com.google.devtools.mobileharness.api.devicemanager.dispatcher.util.DeviceIdGenerator;
29+
import com.google.devtools.mobileharness.api.model.lab.DeviceId;
30+
import org.junit.Before;
31+
import org.junit.Rule;
32+
import org.junit.Test;
33+
import org.junit.runner.RunWith;
34+
import org.junit.runners.JUnit4;
35+
import org.mockito.Mock;
36+
import org.mockito.junit.MockitoJUnit;
37+
import org.mockito.junit.MockitoRule;
38+
39+
@RunWith(JUnit4.class)
40+
public class AndroidDesktopExecutorDeviceDispatcherTest {
41+
42+
@Rule public final MockitoRule mocks = MockitoJUnit.rule();
43+
@Mock private DeviceIdGenerator mockDeviceIdGenerator;
44+
45+
private static final String DEVICE_CONTROL_ID = "35352252-3d64-49a3-b1a3-4a5a92a52d73";
46+
private static final DetectionResult DETECTION_RESULT =
47+
DetectionResult.of(DEVICE_CONTROL_ID, DetectionType.ANDROID_DESKTOP_EXECUTOR);
48+
private static final DetectionResults DETECTION_RESULTS =
49+
new DetectionResults(ImmutableList.of(DETECTION_RESULT));
50+
51+
private AndroidDesktopExecutorDeviceDispatcher dispatcher;
52+
53+
@Before
54+
public void setUp() {
55+
dispatcher = new AndroidDesktopExecutorDeviceDispatcher(mockDeviceIdGenerator);
56+
}
57+
58+
@Test
59+
public void dispatchDevices() {
60+
assertThat(dispatcher.dispatchDevices(DETECTION_RESULTS, new DispatchResults()))
61+
.containsExactly(DEVICE_CONTROL_ID, DispatchType.LIVE);
62+
}
63+
64+
@Test
65+
public void generateDeviceId() {
66+
DeviceId deviceId = DeviceId.of("fake_device_id", "fake_uuid", /* isUuidVolatile= */ true);
67+
when(mockDeviceIdGenerator.getNoOpDeviceId(DEVICE_CONTROL_ID)).thenReturn(deviceId);
68+
69+
assertThat(dispatcher.generateDeviceId(DEVICE_CONTROL_ID)).isEqualTo(deviceId);
70+
}
71+
}

src/javatests/com/google/devtools/mobileharness/api/devicemanager/dispatcher/BUILD

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,15 @@ java_library(
2626
srcs = glob(["*Test.java"]),
2727
deps = [
2828
"//src/java/com/google/devtools/mobileharness/api/devicemanager/detector/model",
29+
"//src/java/com/google/devtools/mobileharness/api/devicemanager/dispatcher:android_desktop_executor_device",
2930
"//src/java/com/google/devtools/mobileharness/api/devicemanager/dispatcher:failed_device",
3031
"//src/java/com/google/devtools/mobileharness/api/devicemanager/dispatcher/model",
32+
"//src/java/com/google/devtools/mobileharness/api/devicemanager/dispatcher/util:device_id_descriptor_generator",
3133
"//src/java/com/google/devtools/mobileharness/api/model/lab:device_id_descriptor",
3234
"//src/javatests/com/google/devtools/mobileharness/builddefs:truth",
3335
"@maven//:com_google_guava_guava",
3436
"@maven//:junit_junit",
37+
"@maven//:org_mockito_mockito_core",
3538
],
3639
)
3740

0 commit comments

Comments
 (0)