blob: 75c32fb22b2dbae035770ade378b118b60293c9c [file] [log] [blame]
Brett Chabotbe0c0cd2010-06-30 18:34:59 -07001/*
2 * Copyright (C) 2010 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 */
Brett Chabotbeaebb02011-02-07 16:16:06 -080016package com.android.tradefed.targetprep;
Brett Chabotbe0c0cd2010-06-30 18:34:59 -070017
Brett Chabotbeaebb02011-02-07 16:16:06 -080018import com.android.tradefed.build.IAppBuildInfo;
19import com.android.tradefed.build.IBuildInfo;
Brett Chabotbdf1dc52012-04-30 15:56:53 -070020import com.android.tradefed.build.VersionedFile;
Brett Chabotbe0c0cd2010-06-30 18:34:59 -070021import com.android.tradefed.config.Option;
Brett Chabot4ce3fb82012-12-18 15:29:44 -080022import com.android.tradefed.config.OptionClass;
Brett Chabotbe0c0cd2010-06-30 18:34:59 -070023import com.android.tradefed.device.DeviceNotAvailableException;
24import com.android.tradefed.device.ITestDevice;
Brett Chabotf2589c82012-11-13 09:44:19 -080025import com.android.tradefed.log.LogUtil.CLog;
Brett Chabotbe0c0cd2010-06-30 18:34:59 -070026
Brett Chabot4ce3fb82012-12-18 15:29:44 -080027import java.util.HashSet;
Brett Chabotf2589c82012-11-13 09:44:19 -080028import java.util.Set;
Brett Chabot4b0a2712010-08-12 18:51:53 -070029
Brett Chabotbe0c0cd2010-06-30 18:34:59 -070030/**
31 * A {@link ITargetPreparer} that installs an apk and its tests.
32 */
Brett Chabot4ce3fb82012-12-18 15:29:44 -080033@OptionClass(alias="app-setup")
Brett Chabot6374a562012-11-28 15:35:49 -080034public class AppSetup implements ITargetPreparer, ITargetCleaner {
Brett Chabotbe0c0cd2010-06-30 18:34:59 -070035
Brett Chabot4ce3fb82012-12-18 15:29:44 -080036 @Option(name = "reboot", description = "reboot device during setup.")
Brett Chabotbe0c0cd2010-06-30 18:34:59 -070037 private boolean mReboot = true;
38
Brett Chabot4ce3fb82012-12-18 15:29:44 -080039 @Option(name = "install", description = "install all apks in build.")
40 private boolean mInstall = true;
41
42 @Option(name = "uninstall", description = "uninstall all apks after test completes.")
Brett Chabotf2589c82012-11-13 09:44:19 -080043 private boolean mUninstall = true;
44
Brett Chabot4ce3fb82012-12-18 15:29:44 -080045 @Option(name = "skip-uninstall-pkg", description =
46 "force retention of this package when --uninstall is set.")
47 private Set<String> mSkipUninstallPkgs = new HashSet<String>();
48
Brett Chabotbe0c0cd2010-06-30 18:34:59 -070049 /**
50 * {@inheritDoc}
51 */
Brett Chabot683ee432011-12-08 11:43:50 -080052 @Override
Brett Chabotbe0c0cd2010-06-30 18:34:59 -070053 public void setUp(ITestDevice device, IBuildInfo buildInfo) throws TargetSetupError,
54 DeviceNotAvailableException {
Brett Chabotbabb9432011-01-20 19:29:07 -080055 if (!(buildInfo instanceof IAppBuildInfo)) {
Brett Chabotbe0c0cd2010-06-30 18:34:59 -070056 throw new IllegalArgumentException("Provided buildInfo is not a AppBuildInfo");
57 }
Brett Chabotbabb9432011-01-20 19:29:07 -080058 IAppBuildInfo appBuild = (IAppBuildInfo)buildInfo;
Brett Chabot4ce3fb82012-12-18 15:29:44 -080059 CLog.i("Performing setup on %s", device.getSerialNumber());
Brett Chabotbe0c0cd2010-06-30 18:34:59 -070060
Brett Chabotbabb9432011-01-20 19:29:07 -080061 if (mReboot) {
62 // reboot device to get a clean state
63 device.reboot();
64 }
Brett Chabota17af5d2010-08-23 13:51:17 -070065
Brett Chabot4ce3fb82012-12-18 15:29:44 -080066 if (mInstall) {
67 for (VersionedFile apkFile : appBuild.getAppPackageFiles()) {
68 String result = device.installPackage(apkFile.getFile(), true);
69 if (result != null) {
70 throw new TargetSetupError(String.format(
71 "Failed to install %s on %s. Reason: %s",
72 apkFile.getFile().getName(), device.getSerialNumber(), result));
73 }
Brett Chabot71d66492012-10-26 11:27:00 -070074 }
Brett Chabota17af5d2010-08-23 13:51:17 -070075 }
Brett Chabotbe0c0cd2010-06-30 18:34:59 -070076 }
Brett Chabot4b0a2712010-08-12 18:51:53 -070077
Brett Chabotf2589c82012-11-13 09:44:19 -080078 /**
79 * {@inheritDoc}
80 */
81 @Override
82 public void tearDown(ITestDevice device, IBuildInfo buildInfo, Throwable e)
83 throws DeviceNotAvailableException {
84 if (mUninstall) {
85 Set<String> pkgs = device.getInstalledNonSystemPackageNames();
86 for (String pkg : pkgs) {
Brett Chabot4ce3fb82012-12-18 15:29:44 -080087 if (!mSkipUninstallPkgs.contains(pkg)) {
88 String result = device.uninstallPackage(pkg);
89 if (result != null) {
90 CLog.w("Uninstall of %s on %s failed: %s", pkg, device.getSerialNumber(),
91 result);
92 }
Brett Chabotf2589c82012-11-13 09:44:19 -080093 }
94 }
95 }
96 }
Brett Chabotbe0c0cd2010-06-30 18:34:59 -070097}