Skip to content

Commit 741e31d

Browse files
DeviceInfracopybara-github
DeviceInfra
authored andcommitted
Internal change
PiperOrigin-RevId: 745626737
1 parent e8e6a18 commit 741e31d

File tree

10 files changed

+403
-3
lines changed

10 files changed

+403
-3
lines changed

src/java/com/google/devtools/mobileharness/api/model/error/AndroidErrorId.java

+1
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,7 @@ public enum AndroidErrorId implements ErrorId {
459459
ANDROID_ROBO_TEST_UTP_TEST_SUITE_RESULT_PROTO_FILE_ABSENT(115_536, ErrorType.INFRA_ISSUE),
460460
ANDROID_ROBO_TEST_UTP_TEST_SUITE_RESULT_READ_ERROR(115_537, ErrorType.INFRA_ISSUE),
461461
ANDROID_ROBO_TEST_CONFIG_READ_ERROR(115_538, ErrorType.DEPENDENCY_ISSUE),
462+
ANDROID_ROBO_TEST_FREE_PORT_UNAVAILABLE(115_539, ErrorType.INFRA_ISSUE),
462463

463464
// AndroidNativeBin driver: 115_551 ~ 115_600
464465
ANDROID_NATIVE_BIN_EXIT_CODE_ERROR(115_551, ErrorType.CUSTOMER_ISSUE),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Copyright 2022 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
16+
load("@rules_java//java:defs.bzl", "java_library")
17+
18+
package(
19+
default_applicable_licenses = ["//:license"],
20+
default_visibility = [
21+
"//:deviceinfra_all_pkg",
22+
],
23+
)
24+
25+
java_library(
26+
name = "appcrawler",
27+
srcs = [
28+
"PostProcessor.java",
29+
"PreProcessor.java",
30+
],
31+
deps = [
32+
"//src/java/com/google/devtools/mobileharness/api/model/error",
33+
"//src/java/com/google/devtools/mobileharness/platform/android/lightning/apkinstaller",
34+
"//src/java/com/google/wireless/qa/mobileharness/shared/api/device",
35+
"//src/java/com/google/wireless/qa/mobileharness/shared/model/job",
36+
"//src/java/com/google/wireless/qa/mobileharness/shared/proto/spec:android_robo_test_spec_java_proto",
37+
"@maven//:javax_inject_jsr330_api",
38+
],
39+
)
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.platform.android.appcrawler;
18+
19+
import com.google.devtools.mobileharness.platform.android.lightning.apkinstaller.ApkInstaller;
20+
import com.google.wireless.qa.mobileharness.shared.api.device.Device;
21+
import com.google.wireless.qa.mobileharness.shared.model.job.TestInfo;
22+
import com.google.wireless.qa.mobileharness.shared.proto.spec.driver.AndroidRoboTestSpec;
23+
import javax.inject.Inject;
24+
25+
/** Post processing operations for AndroidRoboTest. */
26+
public class PostProcessor {
27+
private static final String CRAWLER_PKG = "androidx.test.tools.crawler";
28+
private static final String STUB_CRAWLER_PKG = "androidx.test.tools.crawler.stubapp";
29+
30+
private final ApkInstaller apkInstaller;
31+
32+
@Inject
33+
PostProcessor(ApkInstaller apkInstaller) {
34+
this.apkInstaller = apkInstaller;
35+
}
36+
37+
/** Uninstall app packages installed as part of AndroidRoboTest. */
38+
public void uninstallApks(TestInfo testInfo, Device device, AndroidRoboTestSpec spec)
39+
throws InterruptedException {
40+
var crawlerPackageId = spec.hasCrawlerPackageId() ? spec.getCrawlerPackageId() : CRAWLER_PKG;
41+
var crawlerStubPackageId =
42+
spec.hasStubAppPackageId() ? spec.getStubAppPackageId() : STUB_CRAWLER_PKG;
43+
apkInstaller.uninstallApk(device, crawlerPackageId, true, testInfo.log());
44+
apkInstaller.uninstallApk(device, crawlerStubPackageId, true, testInfo.log());
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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.platform.android.appcrawler;
18+
19+
import com.google.devtools.mobileharness.api.model.error.MobileHarnessException;
20+
import com.google.devtools.mobileharness.platform.android.lightning.apkinstaller.ApkInstallArgs;
21+
import com.google.devtools.mobileharness.platform.android.lightning.apkinstaller.ApkInstaller;
22+
import com.google.wireless.qa.mobileharness.shared.api.device.Device;
23+
import com.google.wireless.qa.mobileharness.shared.model.job.TestInfo;
24+
import com.google.wireless.qa.mobileharness.shared.proto.spec.driver.AndroidRoboTestSpec;
25+
import javax.inject.Inject;
26+
27+
/** PreProcessor class to handle operations before the crawl. */
28+
public class PreProcessor {
29+
private final ApkInstaller apkInstaller;
30+
31+
@Inject
32+
PreProcessor(ApkInstaller apkInstaller) {
33+
this.apkInstaller = apkInstaller;
34+
}
35+
36+
/** Install apks needed for the test. */
37+
public void installApks(TestInfo testInfo, Device device, AndroidRoboTestSpec spec)
38+
throws MobileHarnessException, InterruptedException {
39+
apkInstaller.installApk(device, setupInstallable(spec.getCrawlerApk(), true), testInfo.log());
40+
apkInstaller.installApk(
41+
device, setupInstallable(spec.getCrawlerStubApk(), true), testInfo.log());
42+
}
43+
44+
private ApkInstallArgs setupInstallable(String path, boolean grantRuntimePermissions) {
45+
return ApkInstallArgs.builder()
46+
.setApkPath(path)
47+
.setSkipIfCached(true)
48+
.setGrantPermissions(grantRuntimePermissions)
49+
.build();
50+
}
51+
}

src/java/com/google/wireless/qa/mobileharness/shared/api/driver/AndroidRoboTest.java

+35-1
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,20 @@
1919

2020
import com.google.common.flogger.FluentLogger;
2121
import com.google.devtools.deviceinfra.platform.android.lightning.internal.sdk.adb.Adb;
22+
import com.google.devtools.mobileharness.api.model.error.AndroidErrorId;
2223
import com.google.devtools.mobileharness.api.model.error.MobileHarnessException;
24+
import com.google.devtools.mobileharness.platform.android.appcrawler.PostProcessor;
25+
import com.google.devtools.mobileharness.platform.android.appcrawler.PreProcessor;
26+
import com.google.devtools.mobileharness.shared.util.port.PortProber;
2327
import com.google.wireless.qa.mobileharness.shared.android.Aapt;
2428
import com.google.wireless.qa.mobileharness.shared.api.annotation.DriverAnnotation;
2529
import com.google.wireless.qa.mobileharness.shared.api.annotation.TestAnnotation;
2630
import com.google.wireless.qa.mobileharness.shared.api.device.Device;
2731
import com.google.wireless.qa.mobileharness.shared.model.job.TestInfo;
2832
import com.google.wireless.qa.mobileharness.shared.model.job.in.spec.SpecConfigable;
2933
import com.google.wireless.qa.mobileharness.shared.proto.spec.driver.AndroidRoboTestSpec;
34+
import java.io.IOException;
35+
import java.time.Clock;
3036
import javax.inject.Inject;
3137

3238
/** Driver for running Android Robo Tests using the UTP Android Robo Driver. */
@@ -39,12 +45,25 @@ public class AndroidRoboTest extends BaseDriver implements SpecConfigable<Androi
3945

4046
private final Aapt aapt;
4147
private final Adb adb;
48+
private final PreProcessor preProcessor;
49+
private final PostProcessor postProcessor;
50+
private final Clock clock;
4251

4352
@Inject
44-
AndroidRoboTest(Device device, TestInfo testInfo, Adb adb, Aapt aapt) {
53+
AndroidRoboTest(
54+
Device device,
55+
TestInfo testInfo,
56+
Adb adb,
57+
Aapt aapt,
58+
Clock clock,
59+
PreProcessor preProcessor,
60+
PostProcessor postProcessor) {
4561
super(device, testInfo);
4662
this.aapt = aapt;
4763
this.adb = adb;
64+
this.clock = clock;
65+
this.preProcessor = preProcessor;
66+
this.postProcessor = postProcessor;
4867
}
4968

5069
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
@@ -57,5 +76,20 @@ public void run(TestInfo testInfo) throws MobileHarnessException, InterruptedExc
5776
.alsoTo(logger)
5877
.log("Running Android Robo Driver on %s.", this.getDevice().getDeviceId());
5978
testInfo.log().atInfo().alsoTo(logger).log("Job Info: %s", this.getTest().jobInfo());
79+
80+
AndroidRoboTestSpec spec = testInfo.jobInfo().combinedSpec(this);
81+
testInfo.log().atInfo().alsoTo(logger).log("\n\nAndroid Robo Test Spec: \n\n%s", spec);
82+
preProcessor.installApks(testInfo, getDevice(), spec);
83+
84+
postProcessor.uninstallApks(testInfo, getDevice(), spec);
85+
}
86+
87+
private static int pickUnusedPort() throws InterruptedException, MobileHarnessException {
88+
try {
89+
return PortProber.pickUnusedPort();
90+
} catch (IOException e) {
91+
throw new MobileHarnessException(
92+
AndroidErrorId.ANDROID_ROBO_TEST_FREE_PORT_UNAVAILABLE, "Unable to find unused port", e);
93+
}
6094
}
6195
}

src/java/com/google/wireless/qa/mobileharness/shared/api/driver/BUILD

+2-1
Original file line numberDiff line numberDiff line change
@@ -308,15 +308,16 @@ java_library(
308308
":base_driver",
309309
"//src/java/com/google/devtools/deviceinfra/platform/android/lightning/internal/sdk/adb",
310310
"//src/java/com/google/devtools/mobileharness/api/model/error",
311+
"//src/java/com/google/devtools/mobileharness/platform/android/appcrawler",
311312
"//src/java/com/google/devtools/mobileharness/shared/util/logging:google_logger",
313+
"//src/java/com/google/devtools/mobileharness/shared/util/port:portprober",
312314
"//src/java/com/google/wireless/qa/mobileharness/shared/android:aapt",
313315
"//src/java/com/google/wireless/qa/mobileharness/shared/api/annotation",
314316
"//src/java/com/google/wireless/qa/mobileharness/shared/api/device",
315317
"//src/java/com/google/wireless/qa/mobileharness/shared/constant:property",
316318
"//src/java/com/google/wireless/qa/mobileharness/shared/model/job",
317319
"//src/java/com/google/wireless/qa/mobileharness/shared/model/job/in/spec",
318320
"//src/java/com/google/wireless/qa/mobileharness/shared/proto/spec:android_robo_test_spec_java_proto",
319-
"//src/java/com/google/wireless/qa/mobileharness/shared/util:base",
320321
"@maven//:com_google_inject_guice",
321322
"@maven//:javax_inject_jsr330_api",
322323
],
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Copyright 2022 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
16+
load("@rules_java//java:defs.bzl", "java_library")
17+
load("//src/javatests/com/google/devtools/mobileharness/builddefs:junit_test_suites.bzl", "junit_test_suites")
18+
19+
package(
20+
default_applicable_licenses = ["//:license"],
21+
default_testonly = 1,
22+
)
23+
24+
java_library(
25+
name = "appcrawler",
26+
srcs = [
27+
"PostProcessorTest.java",
28+
"PreProcessorTest.java",
29+
],
30+
deps = [
31+
"//src/java/com/google/devtools/mobileharness/platform/android/appcrawler",
32+
"//src/java/com/google/devtools/mobileharness/platform/android/lightning/apkinstaller",
33+
"//src/java/com/google/wireless/qa/mobileharness/shared/api/device",
34+
"//src/java/com/google/wireless/qa/mobileharness/shared/api/device:no_op_device",
35+
"//src/java/com/google/wireless/qa/mobileharness/shared/model/job",
36+
"//src/java/com/google/wireless/qa/mobileharness/shared/proto:job_java_proto",
37+
"//src/java/com/google/wireless/qa/mobileharness/shared/proto/spec:android_robo_test_spec_java_proto",
38+
"@maven//:junit_junit",
39+
"@maven//:org_mockito_mockito_core",
40+
],
41+
)
42+
43+
junit_test_suites(
44+
name = "gen_tests",
45+
sizes = [
46+
"small",
47+
],
48+
deps = [
49+
":appcrawler",
50+
],
51+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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.platform.android.appcrawler;
18+
19+
import static org.mockito.Mockito.verify;
20+
21+
import com.google.devtools.mobileharness.platform.android.lightning.apkinstaller.ApkInstaller;
22+
import com.google.wireless.qa.mobileharness.shared.api.device.Device;
23+
import com.google.wireless.qa.mobileharness.shared.api.device.NoOpDevice;
24+
import com.google.wireless.qa.mobileharness.shared.model.job.JobInfo;
25+
import com.google.wireless.qa.mobileharness.shared.model.job.JobLocator;
26+
import com.google.wireless.qa.mobileharness.shared.model.job.TestInfo;
27+
import com.google.wireless.qa.mobileharness.shared.proto.Job.JobType;
28+
import com.google.wireless.qa.mobileharness.shared.proto.spec.driver.AndroidRoboTestSpec;
29+
import org.junit.Before;
30+
import org.junit.Rule;
31+
import org.junit.Test;
32+
import org.junit.runner.RunWith;
33+
import org.junit.runners.JUnit4;
34+
import org.mockito.Mock;
35+
import org.mockito.junit.MockitoJUnit;
36+
import org.mockito.junit.MockitoRule;
37+
38+
@RunWith(JUnit4.class)
39+
public class PostProcessorTest {
40+
@Rule public final MockitoRule rule = MockitoJUnit.rule();
41+
42+
@Mock private ApkInstaller apkInstaller;
43+
44+
private PostProcessor postProcessor;
45+
46+
@Before
47+
public void setUp() throws Exception {
48+
postProcessor = new PostProcessor(apkInstaller);
49+
}
50+
51+
@Test
52+
public void uninstallApks_completes() throws Exception {
53+
TestInfo testInfo = setUpJobInfo().tests().add("some test");
54+
Device device = new NoOpDevice("device_name");
55+
56+
AndroidRoboTestSpec spec =
57+
AndroidRoboTestSpec.newBuilder()
58+
.setCrawlerPackageId("androidx.test.tools.crawler")
59+
.setStubAppPackageId("androidx.test.tools.crawler.stubapp")
60+
.build();
61+
postProcessor.uninstallApks(testInfo, device, spec);
62+
63+
verify(apkInstaller).uninstallApk(device, "androidx.test.tools.crawler", true, testInfo.log());
64+
verify(apkInstaller)
65+
.uninstallApk(device, "androidx.test.tools.crawler.stubapp", true, testInfo.log());
66+
}
67+
68+
private JobInfo setUpJobInfo() {
69+
return JobInfo.newBuilder()
70+
.setLocator(new JobLocator("job_id", "job_name"))
71+
.setType(JobType.newBuilder().setDevice("device_type").setDriver("AndroidRoboTest").build())
72+
.build();
73+
}
74+
}

0 commit comments

Comments
 (0)