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 package com.android.tradefed.targetprep.suite; 17 18 import com.android.tradefed.build.IBuildInfo; 19 import com.android.tradefed.build.IDeviceBuildInfo; 20 import com.android.tradefed.config.OptionClass; 21 import com.android.tradefed.device.ITestDevice; 22 import com.android.tradefed.targetprep.TargetSetupError; 23 import com.android.tradefed.targetprep.TestAppInstallSetup; 24 import com.android.tradefed.util.FileUtil; 25 26 import com.google.common.annotations.VisibleForTesting; 27 28 import java.io.File; 29 import java.io.FileNotFoundException; 30 31 /** 32 * Installs specified APKs for Suite configuration: either from $ANDROID_TARGET_OUT_TESTCASES 33 * variable or the ROOT_DIR in build info. 34 */ 35 @OptionClass(alias = "apk-installer") 36 public class SuiteApkInstaller extends TestAppInstallSetup { 37 38 private static final String ANDROID_TARGET_TESTCASES = "ANDROID_TARGET_OUT_TESTCASES"; 39 private static final String ROOT_DIR = "ROOT_DIR"; 40 41 @VisibleForTesting getEnvVariable()42 String getEnvVariable() { 43 return System.getenv(ANDROID_TARGET_TESTCASES); 44 } 45 46 /** 47 * Try to find a path for the base root tests directory. 48 * 49 * @param buildInfo the {@link IBuildInfo} describing the build. 50 * @return a {@link File} pointing to the directory of the root tests dir. 51 * @throws FileNotFoundException if no root dir is defined. 52 */ 53 @VisibleForTesting getTestsDir(IBuildInfo buildInfo)54 protected File getTestsDir(IBuildInfo buildInfo) throws FileNotFoundException { 55 if (buildInfo.getBuildAttributes().get(ROOT_DIR) != null) { 56 return new File(buildInfo.getBuildAttributes().get(ROOT_DIR)); 57 } 58 throw new FileNotFoundException(String.format("%s was found.", ROOT_DIR)); 59 } 60 61 /** Check within $ANDROID_TARGET_OUT_TESTCASES if the apk exists. */ getApkFromVariableTestsDir(String apkFileName)62 private File getApkFromVariableTestsDir(String apkFileName) { 63 String testcasesPath = getEnvVariable(); 64 if (testcasesPath != null) { 65 File testCasesFile = new File(testcasesPath); 66 // Only return the variable directory if it exists 67 if (testCasesFile.isDirectory()) { 68 return FileUtil.findFile(testCasesFile, apkFileName); 69 } 70 } 71 return null; 72 } 73 74 /** Check within {@link IDeviceBuildInfo#getTestsDir()} if the apk exists. */ getApkFromBuildTestsDir(IBuildInfo buildInfo, String apkFileName)75 private File getApkFromBuildTestsDir(IBuildInfo buildInfo, String apkFileName) { 76 if (buildInfo instanceof IDeviceBuildInfo) { 77 IDeviceBuildInfo deviceBuildInfo = (IDeviceBuildInfo) buildInfo; 78 File testDir = deviceBuildInfo.getTestsDir(); 79 if (testDir != null && testDir.isDirectory()) { 80 return FileUtil.findFile(testDir, apkFileName); 81 } 82 } 83 return null; 84 } 85 86 /** {@inheritDoc} */ 87 @Override getLocalPathForFilename( IBuildInfo buildInfo, String apkFileName, ITestDevice device)88 protected File getLocalPathForFilename( 89 IBuildInfo buildInfo, String apkFileName, ITestDevice device) throws TargetSetupError { 90 File apkFile = null; 91 try { 92 // check in ANDROID_TARGET_OUT_TESTCASES first. 93 apkFile = getApkFromVariableTestsDir(apkFileName); 94 if (apkFile != null && apkFile.isFile()) { 95 return apkFile; 96 } 97 98 // check from IDeviceBuildInfo. 99 apkFile = getApkFromBuildTestsDir(buildInfo, apkFileName); 100 if (apkFile != null && apkFile.isFile()) { 101 return apkFile; 102 } 103 104 // check ROOT_DIR 105 apkFile = FileUtil.findFile(getTestsDir(buildInfo), apkFileName); 106 if (apkFile == null || !apkFile.isFile()) { 107 throw new FileNotFoundException(); 108 } 109 } catch (FileNotFoundException e) { 110 throw new TargetSetupError( 111 String.format("%s not found", apkFileName), e, device.getDeviceDescriptor()); 112 } 113 return apkFile; 114 } 115 } 116