Home | History | Annotate | Download | only in device
      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  */
     16 package com.android.tradefed.device;
     17 
     18 import com.android.ddmlib.Log;
     19 import com.android.tradefed.command.remote.DeviceDescriptor;
     20 import com.android.tradefed.config.GlobalConfiguration;
     21 import com.android.tradefed.util.RunUtil;
     22 
     23 import junit.framework.TestCase;
     24 
     25 import java.util.ArrayList;
     26 import java.util.Collection;
     27 
     28 /**
     29  * A test that diagnoses the devices available to run tests.
     30  * <p/>
     31  * Intended to be used when setting up a machine to run TradeFederation, to verify that all
     32  * device's are operating correctly.
     33  */
     34 public class DeviceDiagTest extends TestCase {
     35 
     36     private static final String LOG_TAG = "DeviceDiagTest";
     37 
     38     /**
     39      * Queries the {@link DeviceManager} to verify all visible devices are available for testing.
     40      */
     41     public void testAllDevicesAvailable() {
     42 
     43         Collection<String> unavailDevices = getUnavailableDevices();
     44         for (int i=0; i < 5 && unavailDevices.size() > 0; i++) {
     45             Log.i(LOG_TAG, "Unavailable devices detected, sleeping and polling");
     46             RunUtil.getDefault().sleep(1*1000);
     47             unavailDevices = getUnavailableDevices();
     48         }
     49         for (String device : unavailDevices) {
     50             System.out.println(String.format(
     51                     "Device %s is not available for testing. Check that package manager is up " +
     52                     "and sdcard is mounted.", device));
     53             // TODO: add more specific hints
     54         }
     55         assertEquals("Not all devices are available", 0, unavailDevices.size());
     56     }
     57 
     58     private IDeviceManager getDeviceManager() {
     59         return GlobalConfiguration.getDeviceManagerInstance();
     60     }
     61 
     62     private Collection<String> getUnavailableDevices() {
     63         Collection<String> unavailDevices = new ArrayList<String>();
     64         for (DeviceDescriptor deviceDesc : getDeviceManager().listAllDevices()) {
     65             if (deviceDesc.getState() == DeviceAllocationState.Unavailable) {
     66                 unavailDevices.add(deviceDesc.getSerial());
     67             }
     68         }
     69         return unavailDevices;
     70     }
     71 }
     72