Home | History | Annotate | Download | only in targetprep
      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 package com.android.tradefed.targetprep;
     17 
     18 import java.io.File;
     19 import java.util.ArrayList;
     20 import java.util.Collection;
     21 
     22 import com.android.tradefed.build.IBuildInfo;
     23 import com.android.tradefed.build.IDeviceBuildInfo;
     24 import com.android.tradefed.config.Option;
     25 import com.android.tradefed.config.Option.Importance;
     26 import com.android.tradefed.config.OptionClass;
     27 import com.android.tradefed.device.DeviceNotAvailableException;
     28 import com.android.tradefed.device.ITestDevice;
     29 import com.android.tradefed.device.ITestDevice.RecoveryMode;
     30 import com.android.tradefed.log.LogUtil.CLog;
     31 import com.android.tradefed.util.FileUtil;
     32 
     33 /**
     34  * A {@link ITargetPreparer} that installs one or more apps from a {@link
     35  * IDeviceBuildInfo#getTestsDir()} folder onto the /system partition on device.
     36  *
     37  * <p>Requires adb root
     38  */
     39 @OptionClass(alias = "tests-system-app")
     40 public class TestSystemAppInstallSetup extends BaseTargetPreparer {
     41 
     42     @Option(name = "system-file-name", description =
     43         "the name of a test zip file to install on device system partition. Can be repeated.",
     44         importance = Importance.IF_UNSET)
     45     private Collection<String> mTestFileNames = new ArrayList<String>();
     46 
     47     /**
     48      * Adds a file to the list of apks to install
     49      *
     50      * @param fileName
     51      */
     52     public void addTestFileName(String fileName) {
     53         mTestFileNames.add(fileName);
     54     }
     55 
     56     /**
     57      * {@inheritDoc}
     58      */
     59     @Override
     60     public void setUp(ITestDevice device, IBuildInfo buildInfo) throws TargetSetupError,
     61             DeviceNotAvailableException {
     62         if (!(buildInfo instanceof IDeviceBuildInfo)) {
     63             throw new IllegalArgumentException(String.format("Provided buildInfo is not a %s",
     64                     IDeviceBuildInfo.class.getCanonicalName()));
     65         }
     66         if (mTestFileNames.size() == 0) {
     67             CLog.i("No test apps to install, skipping");
     68             return;
     69         }
     70         File testsDir = ((IDeviceBuildInfo)buildInfo).getTestsDir();
     71         if (testsDir == null || !testsDir.exists()) {
     72             throw new TargetSetupError("Provided buildInfo does not contain a valid tests "
     73                     + "directory", device.getDeviceDescriptor());
     74         }
     75         device.remountSystemWritable();
     76         device.setRecoveryMode(RecoveryMode.ONLINE);
     77         device.executeShellCommand("stop");
     78 
     79         for (String testAppName : mTestFileNames) {
     80             File testAppFile = FileUtil.getFileForPath(testsDir, "DATA", "app", testAppName);
     81             if (!testAppFile.exists()) {
     82                 throw new TargetSetupError(String.format("Could not find test app %s directory in "
     83                         + "extracted tests.zip", testAppFile), device.getDeviceDescriptor());
     84             }
     85             device.pushFile(testAppFile,  String.format("/system/app/%s", testAppName));
     86         }
     87         device.setRecoveryMode(RecoveryMode.AVAILABLE);
     88         device.executeShellCommand("start");
     89         device.waitForDeviceAvailable();
     90     }
     91 }
     92