aboutsummaryrefslogtreecommitdiff
path: root/src/com/android/tradefed/targetprep
diff options
context:
space:
mode:
authorjdesprez <jdesprez@google.com>2017-04-11 11:37:08 -0700
committerJulien Desprez <jdesprez@google.com>2017-04-15 00:54:00 +0000
commit7592be0ca6fd53c8790aa519d0d0ad87bfac4684 (patch)
tree459db5b087f7314481736acaa2bed68bf21e99cb /src/com/android/tradefed/targetprep
parentbb02e1ecae15c1a71854f35bea25534f23abc2a2 (diff)
downloadtradefederation-7592be0ca6fd53c8790aa519d0d0ad87bfac4684.tar.gz
Adapt ApkInstaller for Tradefed as a replacement
Allow the preparer to look for env variable or root dir in order to work inside TF and CTS. Test: unit tests, tests ./cts-tradefed run cts-dev -m CtsGestureTestCases using new preparer Bug: 37213493 Change-Id: Ic5b3f830a2e4e63f272f508a321a00cf8fcb596d (cherry picked from commit 0c3f6d82e390f1b01ba32ba314598692ce6b5c74)
Diffstat (limited to 'src/com/android/tradefed/targetprep')
-rw-r--r--src/com/android/tradefed/targetprep/suite/SuiteApkInstaller.java84
1 files changed, 84 insertions, 0 deletions
diff --git a/src/com/android/tradefed/targetprep/suite/SuiteApkInstaller.java b/src/com/android/tradefed/targetprep/suite/SuiteApkInstaller.java
new file mode 100644
index 000000000..758453398
--- /dev/null
+++ b/src/com/android/tradefed/targetprep/suite/SuiteApkInstaller.java
@@ -0,0 +1,84 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.tradefed.targetprep.suite;
+
+import com.android.tradefed.build.IBuildInfo;
+import com.android.tradefed.config.OptionClass;
+import com.android.tradefed.device.ITestDevice;
+import com.android.tradefed.targetprep.TargetSetupError;
+import com.android.tradefed.targetprep.TestAppInstallSetup;
+import com.android.tradefed.util.FileUtil;
+
+import com.google.common.annotations.VisibleForTesting;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+
+/**
+ * Installs specified APKs for Suite configuration: either from $ANDROID_TARGET_OUT_TESTCASES
+ * variable or the ROOT_DIR in build info.
+ */
+@OptionClass(alias = "apk-installer")
+public class SuiteApkInstaller extends TestAppInstallSetup {
+
+ private static final String ANDROID_TARGET_TESTCASES = "ANDROID_TARGET_OUT_TESTCASES";
+ private static final String ROOT_DIR = "ROOT_DIR";
+
+ @VisibleForTesting
+ String getEnvVariable() {
+ return System.getenv(ANDROID_TARGET_TESTCASES);
+ }
+
+ /**
+ * Try to find a path for the base root tests directory.
+ *
+ * @param buildInfo the {@link IBuildInfo} describing the build.
+ * @return a {@link File} pointing to the directory of the root tests dir.
+ * @throws FileNotFoundException if no root dir is defined.
+ */
+ @VisibleForTesting
+ protected File getTestsDir(IBuildInfo buildInfo) throws FileNotFoundException {
+ String testcasesPath = getEnvVariable();
+ if (testcasesPath != null) {
+ return new File(testcasesPath);
+ }
+ if (buildInfo.getBuildAttributes().get(ROOT_DIR) != null) {
+ return new File(buildInfo.getBuildAttributes().get(ROOT_DIR));
+ }
+ throw new FileNotFoundException(
+ String.format(
+ "$%s is undefined, and no %s was found.",
+ ANDROID_TARGET_TESTCASES, ROOT_DIR));
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ protected File getLocalPathForFilename(
+ IBuildInfo buildInfo, String apkFileName, ITestDevice device) throws TargetSetupError {
+ File apkFile = null;
+ try {
+ // use findFile because there may be a subdirectory
+ apkFile = FileUtil.findFile(getTestsDir(buildInfo), apkFileName);
+ if (apkFile == null || !apkFile.isFile()) {
+ throw new FileNotFoundException();
+ }
+ } catch (FileNotFoundException e) {
+ throw new TargetSetupError(
+ String.format("%s not found", apkFileName), e, device.getDeviceDescriptor());
+ }
+ return apkFile;
+ }
+}