1 /*
2  * Copyright (C) 2014 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.cts.tradefed.testtype;
17 
18 import com.android.compatibility.common.util.AbiUtils;
19 import com.android.cts.tradefed.build.CtsBuildHelper;
20 import com.android.ddmlib.Log;
21 import com.android.tradefed.build.IBuildInfo;
22 import com.android.tradefed.device.DeviceNotAvailableException;
23 import com.android.tradefed.device.ITestDevice;
24 import com.android.tradefed.result.ITestInvocationListener;
25 import com.android.tradefed.testtype.IAbi;
26 import com.android.tradefed.testtype.IBuildReceiver;
27 import com.android.tradefed.testtype.InstrumentationTest;
28 
29 import java.io.File;
30 import java.io.FileNotFoundException;
31 import java.util.ArrayList;
32 import java.util.Collection;
33 
34 /**
35  * An {@link InstrumentationTest} that will install CTS apks
36  * before test execution, and uninstall on execution completion.
37  */
38 public class CtsInstrumentationApkTest extends InstrumentationTest implements IBuildReceiver {
39 
40     private static final String LOG_TAG = "CtsInstrumentationApkTest";
41 
42     /** the file names of the CTS apks to install */
43     private Collection<String> mInstallFileNames = new ArrayList<String>();
44     private Collection<String> mUninstallPackages = new ArrayList<String>();
45     protected CtsBuildHelper mCtsBuild = null;
46     protected IAbi mAbi = null;
47 
48     /**
49      * @param abi the ABI to run the test on
50      */
setAbi(IAbi abi)51     public void setAbi(IAbi abi) {
52         mAbi = abi;
53     }
54 
55     /**
56      * {@inheritDoc}
57      */
58     @Override
setBuild(IBuildInfo build)59     public void setBuild(IBuildInfo build) {
60         mCtsBuild = CtsBuildHelper.createBuildHelper(build);
61     }
62 
63     /**
64      * Add an apk to install.
65      *
66      * @param apkFileName the apk file name
67      * @param packageName the apk's Android package name
68      */
addInstallApk(String apkFileName, String packageName)69     public void addInstallApk(String apkFileName, String packageName) {
70         mInstallFileNames.add(apkFileName);
71         mUninstallPackages.add(packageName);
72     }
73 
74     /**
75      * {@inheritDoc}
76      */
77     @Override
run(final ITestInvocationListener listener)78     public void run(final ITestInvocationListener listener)
79             throws DeviceNotAvailableException {
80         ITestDevice testDevice = getDevice();
81 
82         if (testDevice == null) {
83             Log.e(LOG_TAG, "Missing device.");
84             return;
85         }
86         if (mCtsBuild == null) {
87             Log.e(LOG_TAG, "Missing build");
88             return;
89         }
90         boolean success = true;
91         for (String apkFileName : mInstallFileNames) {
92             Log.d(LOG_TAG, String.format("Installing %s on %s", apkFileName,
93                     testDevice.getSerialNumber()));
94             try {
95                 File apkFile = mCtsBuild.getTestApp(apkFileName);
96                 String errorCode = null;
97                 String[] options = {AbiUtils.createAbiFlag(mAbi.getName())};
98                 errorCode = testDevice.installPackage(apkFile, true, options);
99                 if (errorCode != null) {
100                     Log.e(LOG_TAG, String.format("Failed to install %s on %s. Reason: %s",
101                           apkFileName, testDevice.getSerialNumber(), errorCode));
102                     success = false;
103                 }
104             } catch (FileNotFoundException e) {
105                 Log.e(LOG_TAG, String.format("Could not find file %s", apkFileName));
106                 success = false;
107             }
108         }
109         if (success) {
110             super.run(listener);
111         }
112         for (String packageName : mUninstallPackages) {
113             Log.d(LOG_TAG, String.format("Uninstalling %s on %s", packageName,
114                     testDevice.getSerialNumber()));
115             testDevice.uninstallPackage(packageName);
116         }
117     }
118 }
119