Home | History | Annotate | Download | only in testtype
      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.testtype;
     18 
     19 import com.android.tradefed.device.ITestDevice;
     20 import com.android.tradefed.targetprep.companion.CompanionAllocator;
     21 import com.android.tradefed.targetprep.companion.CompanionDeviceTracker;
     22 
     23 /**
     24  * Base test class that encapsulates boilerpate of getting and checking companion device
     25  * <p>
     26  * Subclass may call {@link #getCompanion()} to retrieve the allocated companion. Must be used
     27  * in conjunction with an implementation of {@link CompanionAllocator}
     28  */
     29 public abstract class CompanionAwareTest implements IRemoteTest, IDeviceTest {
     30 
     31     private ITestDevice mCompanionDevice;
     32 
     33     /**
     34      * Fetches the companion device allocated for the primary device
     35      * @return the allocated companion device
     36      * @throws RuntimeException if no companion device has been allocated
     37      */
     38     protected ITestDevice getCompanion() {
     39         if (mCompanionDevice == null) {
     40             mCompanionDevice = CompanionDeviceTracker.getInstance().getCompanionDevice(getDevice());
     41             if (mCompanionDevice == null) {
     42                 throw new RuntimeException("no companion device allocated, "
     43                         + "use appropriate ITargetPreparer");
     44             }
     45         }
     46         return mCompanionDevice;
     47     }
     48 }
     49