Brett Chabot | be0c0cd | 2010-06-30 18:34:59 -0700 | [diff] [blame] | 1 | /* |
| 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 Chabot | beaebb0 | 2011-02-07 16:16:06 -0800 | [diff] [blame] | 16 | package com.android.tradefed.targetprep; |
Brett Chabot | be0c0cd | 2010-06-30 18:34:59 -0700 | [diff] [blame] | 17 | |
Brett Chabot | beaebb0 | 2011-02-07 16:16:06 -0800 | [diff] [blame] | 18 | import com.android.tradefed.build.IAppBuildInfo; |
| 19 | import com.android.tradefed.build.IBuildInfo; |
Brett Chabot | bdf1dc5 | 2012-04-30 15:56:53 -0700 | [diff] [blame] | 20 | import com.android.tradefed.build.VersionedFile; |
Brett Chabot | be0c0cd | 2010-06-30 18:34:59 -0700 | [diff] [blame] | 21 | import com.android.tradefed.config.Option; |
Brett Chabot | 4ce3fb8 | 2012-12-18 15:29:44 -0800 | [diff] [blame^] | 22 | import com.android.tradefed.config.OptionClass; |
Brett Chabot | be0c0cd | 2010-06-30 18:34:59 -0700 | [diff] [blame] | 23 | import com.android.tradefed.device.DeviceNotAvailableException; |
| 24 | import com.android.tradefed.device.ITestDevice; |
Brett Chabot | f2589c8 | 2012-11-13 09:44:19 -0800 | [diff] [blame] | 25 | import com.android.tradefed.log.LogUtil.CLog; |
Brett Chabot | be0c0cd | 2010-06-30 18:34:59 -0700 | [diff] [blame] | 26 | |
Brett Chabot | 4ce3fb8 | 2012-12-18 15:29:44 -0800 | [diff] [blame^] | 27 | import java.util.HashSet; |
Brett Chabot | f2589c8 | 2012-11-13 09:44:19 -0800 | [diff] [blame] | 28 | import java.util.Set; |
Brett Chabot | 4b0a271 | 2010-08-12 18:51:53 -0700 | [diff] [blame] | 29 | |
Brett Chabot | be0c0cd | 2010-06-30 18:34:59 -0700 | [diff] [blame] | 30 | /** |
| 31 | * A {@link ITargetPreparer} that installs an apk and its tests. |
| 32 | */ |
Brett Chabot | 4ce3fb8 | 2012-12-18 15:29:44 -0800 | [diff] [blame^] | 33 | @OptionClass(alias="app-setup") |
Brett Chabot | 6374a56 | 2012-11-28 15:35:49 -0800 | [diff] [blame] | 34 | public class AppSetup implements ITargetPreparer, ITargetCleaner { |
Brett Chabot | be0c0cd | 2010-06-30 18:34:59 -0700 | [diff] [blame] | 35 | |
Brett Chabot | 4ce3fb8 | 2012-12-18 15:29:44 -0800 | [diff] [blame^] | 36 | @Option(name = "reboot", description = "reboot device during setup.") |
Brett Chabot | be0c0cd | 2010-06-30 18:34:59 -0700 | [diff] [blame] | 37 | private boolean mReboot = true; |
| 38 | |
Brett Chabot | 4ce3fb8 | 2012-12-18 15:29:44 -0800 | [diff] [blame^] | 39 | @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 Chabot | f2589c8 | 2012-11-13 09:44:19 -0800 | [diff] [blame] | 43 | private boolean mUninstall = true; |
| 44 | |
Brett Chabot | 4ce3fb8 | 2012-12-18 15:29:44 -0800 | [diff] [blame^] | 45 | @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 Chabot | be0c0cd | 2010-06-30 18:34:59 -0700 | [diff] [blame] | 49 | /** |
| 50 | * {@inheritDoc} |
| 51 | */ |
Brett Chabot | 683ee43 | 2011-12-08 11:43:50 -0800 | [diff] [blame] | 52 | @Override |
Brett Chabot | be0c0cd | 2010-06-30 18:34:59 -0700 | [diff] [blame] | 53 | public void setUp(ITestDevice device, IBuildInfo buildInfo) throws TargetSetupError, |
| 54 | DeviceNotAvailableException { |
Brett Chabot | babb943 | 2011-01-20 19:29:07 -0800 | [diff] [blame] | 55 | if (!(buildInfo instanceof IAppBuildInfo)) { |
Brett Chabot | be0c0cd | 2010-06-30 18:34:59 -0700 | [diff] [blame] | 56 | throw new IllegalArgumentException("Provided buildInfo is not a AppBuildInfo"); |
| 57 | } |
Brett Chabot | babb943 | 2011-01-20 19:29:07 -0800 | [diff] [blame] | 58 | IAppBuildInfo appBuild = (IAppBuildInfo)buildInfo; |
Brett Chabot | 4ce3fb8 | 2012-12-18 15:29:44 -0800 | [diff] [blame^] | 59 | CLog.i("Performing setup on %s", device.getSerialNumber()); |
Brett Chabot | be0c0cd | 2010-06-30 18:34:59 -0700 | [diff] [blame] | 60 | |
Brett Chabot | babb943 | 2011-01-20 19:29:07 -0800 | [diff] [blame] | 61 | if (mReboot) { |
| 62 | // reboot device to get a clean state |
| 63 | device.reboot(); |
| 64 | } |
Brett Chabot | a17af5d | 2010-08-23 13:51:17 -0700 | [diff] [blame] | 65 | |
Brett Chabot | 4ce3fb8 | 2012-12-18 15:29:44 -0800 | [diff] [blame^] | 66 | 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 Chabot | 71d6649 | 2012-10-26 11:27:00 -0700 | [diff] [blame] | 74 | } |
Brett Chabot | a17af5d | 2010-08-23 13:51:17 -0700 | [diff] [blame] | 75 | } |
Brett Chabot | be0c0cd | 2010-06-30 18:34:59 -0700 | [diff] [blame] | 76 | } |
Brett Chabot | 4b0a271 | 2010-08-12 18:51:53 -0700 | [diff] [blame] | 77 | |
Brett Chabot | f2589c8 | 2012-11-13 09:44:19 -0800 | [diff] [blame] | 78 | /** |
| 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 Chabot | 4ce3fb8 | 2012-12-18 15:29:44 -0800 | [diff] [blame^] | 87 | 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 Chabot | f2589c8 | 2012-11-13 09:44:19 -0800 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | } |
| 96 | } |
Brett Chabot | be0c0cd | 2010-06-30 18:34:59 -0700 | [diff] [blame] | 97 | } |