1 /*
2  * Copyright (C) 2011 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.targetprep;
18 
19 import com.android.tradefed.config.Option;
20 import com.android.tradefed.config.Option.Importance;
21 import com.android.tradefed.config.OptionClass;
22 import com.android.tradefed.device.DeviceNotAvailableException;
23 import com.android.tradefed.device.ITestDevice;
24 import com.android.tradefed.invoker.TestInformation;
25 import com.android.tradefed.log.LogUtil.CLog;
26 import com.android.tradefed.util.FileUtil;
27 
28 import java.io.File;
29 import java.util.ArrayList;
30 import java.util.Collection;
31 
32 /**
33  * A {@link ITargetPreparer} that installs one or more test apks from an Android platform build env.
34  *
35  * <p>Uses the <code>ANDROID_PRODUCT_OUT</code> env variable to determine location for build apks.
36  */
37 @OptionClass(alias = "install-apk-from-env")
38 public class InstallBuildEnvApkSetup extends BaseTargetPreparer {
39 
40     private static final String APK_SUFFIX = ".apk";
41 
42     @Option(name = "apk-name", description =
43         "the file name of the apk to install. Can be repeated.",
44         importance = Importance.IF_UNSET)
45     private Collection<String> mApkNames = new ArrayList<String>();
46 
47     /** {@inheritDoc} */
48     @Override
setUp(TestInformation testInfo)49     public void setUp(TestInformation testInfo)
50             throws TargetSetupError, BuildError, DeviceNotAvailableException {
51         ITestDevice device = testInfo.getDevice();
52         File testAppDir = getDataAppDirFromBuildEnv(device);
53         for (String apkName : mApkNames) {
54             String apkPrimaryFileName = apkName;
55             if (apkName.endsWith(APK_SUFFIX)) {
56                 apkPrimaryFileName = apkName.substring(0, apkName.length() - APK_SUFFIX.length());
57             }
58             File apk = FileUtil.getFileForPath(testAppDir, apkPrimaryFileName, apkName);
59             CLog.i("Installing %s on %s", apk.getName(), device.getSerialNumber());
60             String result = device.installPackage(apk, true);
61             if (result != null) {
62                 throw new TargetSetupError(String.format(
63                         "Failed to install %s on device %s. Reason: %s", apk.getAbsolutePath(),
64                         device.getSerialNumber(), result), device.getDeviceDescriptor());
65             }
66         }
67     }
68 
getDataAppDirFromBuildEnv(ITestDevice device)69     private File getDataAppDirFromBuildEnv(ITestDevice device) throws TargetSetupError {
70         String buildRoot = System.getenv("ANDROID_PRODUCT_OUT");
71         if (buildRoot == null) {
72             throw new TargetSetupError("ANDROID_PRODUCT_OUT is not set. Are you in Android "
73                     + "build env?", device.getDeviceDescriptor());
74         }
75         File testAppDir = new File(FileUtil.getPath(buildRoot, "data", "app"));
76         if (!testAppDir.exists()) {
77             throw new TargetSetupError(String.format("Could not find test app dir %s",
78                     testAppDir.getAbsolutePath()),device.getDeviceDescriptor());
79         }
80         return testAppDir;
81     }
82 }
83