Home | History | Annotate | Download | only in targetprep
      1 /*
      2  * Copyright (C) 2015 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.compatibility.common.tradefed.targetprep;
     17 
     18 import com.android.compatibility.common.tradefed.build.CompatibilityBuildHelper;
     19 import com.android.tradefed.build.IBuildInfo;
     20 import com.android.tradefed.config.OptionClass;
     21 import com.android.tradefed.device.ITestDevice;
     22 import com.android.tradefed.targetprep.TargetSetupError;
     23 import com.android.tradefed.targetprep.TestAppInstallSetup;
     24 
     25 import java.io.File;
     26 import java.io.FileNotFoundException;
     27 
     28 /**
     29  * Installs specified APKs from Compatibility repository.
     30  */
     31 @OptionClass(alias="apk-installer")
     32 public class ApkInstaller extends TestAppInstallSetup {
     33 
     34     private CompatibilityBuildHelper mBuildHelper = null;
     35 
     36     private void setBuildHelper(IBuildInfo buildInfo) {
     37         if (mBuildHelper == null) {
     38             mBuildHelper = new CompatibilityBuildHelper(buildInfo);
     39         }
     40     }
     41 
     42     protected File getTestsDir(IBuildInfo buildInfo) throws FileNotFoundException {
     43         setBuildHelper(buildInfo);
     44         return mBuildHelper.getTestsDir();
     45     }
     46 
     47     protected File getTestFile(IBuildInfo buildInfo, String filename) throws FileNotFoundException {
     48         setBuildHelper(buildInfo);
     49         return mBuildHelper.getTestFile(filename);
     50     }
     51 
     52     /**
     53      * {@inheritDoc}
     54      */
     55     @Override
     56     protected File getLocalPathForFilename(IBuildInfo buildInfo, String apkFileName,
     57             ITestDevice device) throws TargetSetupError {
     58         File apkFile = null;
     59         try {
     60             apkFile = getTestFile(buildInfo, apkFileName);
     61             if (!apkFile.isFile()) {
     62                 throw new FileNotFoundException();
     63             }
     64         } catch (FileNotFoundException e) {
     65             throw new TargetSetupError(String.format("%s not found", apkFileName), e,
     66                     device.getDeviceDescriptor());
     67         }
     68         return apkFile;
     69     }
     70 }
     71