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 */ 16package com.android.cts.tradefed.testtype; 17 18import com.android.cts.tradefed.build.CtsBuildHelper; 19import com.android.ddmlib.Log; 20import com.android.tradefed.build.IBuildInfo; 21import com.android.tradefed.device.DeviceNotAvailableException; 22import com.android.tradefed.result.ITestInvocationListener; 23import com.android.tradefed.testtype.IBuildReceiver; 24import com.android.tradefed.testtype.InstrumentationTest; 25 26import java.io.FileNotFoundException; 27import java.util.ArrayList; 28import java.util.Collection; 29 30import junit.framework.Assert; 31 32/** 33 * A {@link InstrumentationTest] that will install CTS apks before test execution, 34 * and uninstall on execution completion. 35 */ 36public class InstrumentationApkTest extends InstrumentationTest implements IBuildReceiver { 37 38 private static final String LOG_TAG = "InstrumentationApkTest"; 39 40 /** the file names of the CTS apks to install */ 41 private Collection<String> mInstallFileNames = new ArrayList<String>(); 42 private Collection<String> mUninstallPackages = new ArrayList<String>(); 43 44 private CtsBuildHelper mCtsBuild = null; 45 46 /** 47 * {@inheritDoc} 48 */ 49 @Override 50 public void setBuild(IBuildInfo build) { 51 mCtsBuild = CtsBuildHelper.createBuildHelper(build); 52 } 53 54 /** 55 * Add an apk to install. 56 * 57 * @param apkFileName the apk file name 58 * @param packageName the apk's Android package name 59 */ 60 public void addInstallApk(String apkFileName, String packageName) { 61 mInstallFileNames.add(apkFileName); 62 mUninstallPackages.add(packageName); 63 } 64 65 /** 66 * {@inheritDoc} 67 */ 68 @Override 69 public void run(final ITestInvocationListener listener) 70 throws DeviceNotAvailableException { 71 Assert.assertNotNull("missing device", getDevice()); 72 Assert.assertNotNull("missing build", mCtsBuild); 73 74 for (String apkFileName : mInstallFileNames) { 75 Log.d(LOG_TAG, String.format("Installing %s on %s", apkFileName, 76 getDevice().getSerialNumber())); 77 try { 78 String installCode = getDevice().installPackage(mCtsBuild.getTestApp(apkFileName), 79 true); 80 Assert.assertNull(String.format("Failed to install %s on %s. Reason: %s", 81 apkFileName, getDevice().getSerialNumber(), installCode), installCode); 82 83 } catch (FileNotFoundException e) { 84 Assert.fail(String.format("Could not find file %s", apkFileName)); 85 } 86 } 87 super.run(listener); 88 for (String packageName : mUninstallPackages) { 89 Log.d(LOG_TAG, String.format("Uninstalling %s on %s", packageName, 90 getDevice().getSerialNumber())); 91 getDevice().uninstallPackage(packageName); 92 } 93 } 94} 95