Home | History | Annotate | Download | only in companion
      1 /*
      2  * Copyright (C) 2014 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 
     17 package com.android.tradefed.targetprep.companion;
     18 
     19 import com.android.tradefed.build.IBuildInfo;
     20 import com.android.tradefed.device.DeviceNotAvailableException;
     21 import com.android.tradefed.device.DeviceSelectionOptions;
     22 import com.android.tradefed.device.ITestDevice;
     23 import com.android.tradefed.targetprep.BaseTargetPreparer;
     24 import com.android.tradefed.targetprep.BuildError;
     25 import com.android.tradefed.targetprep.ITargetCleaner;
     26 import com.android.tradefed.targetprep.TargetSetupError;
     27 
     28 /**
     29  * Base class that takes care of allocating and freeing companion device
     30  *
     31  * <p>{@link #getCompanionDeviceSelectionOptions()} should be implemented to describe the criteria
     32  * needed to allocate the companion device
     33  */
     34 public abstract class CompanionAllocator extends BaseTargetPreparer implements ITargetCleaner {
     35 
     36     /**
     37      * Sets up the device.
     38      * <p>
     39      * Internal implementation of this method will request a companion device, and allocate it.
     40      */
     41     @Override
     42     public void setUp(ITestDevice device, IBuildInfo buildInfo) throws TargetSetupError,
     43             BuildError, DeviceNotAvailableException {
     44         ITestDevice companionDevice = getCompanionDeviceTracker().allocateCompanionDevice(
     45                 device, getCompanionDeviceSelectionOptions());
     46         if (companionDevice == null) {
     47             throw new TargetSetupError(String.format("failed to allocate companion device for %s",
     48                     device.getSerialNumber()), device.getDeviceDescriptor());
     49         }
     50     }
     51 
     52     /**
     53      * Describe the {@link DeviceSelectionOptions} for the companion device
     54      */
     55     protected abstract DeviceSelectionOptions getCompanionDeviceSelectionOptions();
     56 
     57     @Override
     58     public void tearDown(ITestDevice device, IBuildInfo buildInfo, Throwable e)
     59             throws DeviceNotAvailableException {
     60         getCompanionDeviceTracker().freeCompanionDevice(device);
     61     }
     62 
     63     private CompanionDeviceTracker getCompanionDeviceTracker() {
     64         return CompanionDeviceTracker.getInstance();
     65     }
     66 }
     67