Home | History | Annotate | Download | only in device
      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 package com.android.tradefed.device;
     17 
     18 import com.android.ddmlib.IDevice;
     19 import com.android.tradefed.device.IManagedTestDevice.DeviceEventResponse;
     20 
     21 import junit.framework.TestCase;
     22 
     23 import org.easymock.EasyMock;
     24 
     25 import java.util.HashSet;
     26 import java.util.Set;
     27 
     28 /**
     29  * Unit tests for {@link ManagedDeviceList}.
     30  */
     31 public class ManagedDeviceListTest extends TestCase {
     32 
     33     private ManagedDeviceList mManagedDeviceList;
     34 
     35     @Override
     36     public void setUp() {
     37         mManagedDeviceList = new ManagedDeviceList(new IManagedTestDeviceFactory() {
     38 
     39             @Override
     40             public IManagedTestDevice createDevice(IDevice stubDevice) {
     41                 // use real TestDevice to get allocation state machine
     42                 return new TestDevice(stubDevice, EasyMock.createNiceMock(
     43                         IDeviceStateMonitor.class), null);
     44             }
     45 
     46             @Override
     47             public void setFastbootEnabled(boolean enable) {
     48                 // ignore
     49             }
     50         });
     51     }
     52 
     53     /**
     54      * Basic test for {@link ManagedDeviceList#find(String)} and
     55      * {@link ManagedDeviceList#findOrCreate(IDevice)}
     56      */
     57     public void testFindOrCreate() {
     58         // verify find returns null when list is empty
     59         assertNull(mManagedDeviceList.find("foo"));
     60         // verify device is created
     61         ITestDevice d = mManagedDeviceList.findOrCreate(new StubDevice("foo"));
     62         assertNotNull(d);
     63         // verify device can be found
     64         assertEquals(d, mManagedDeviceList.find("foo"));
     65         // verify same device is found, and new one is not created
     66         assertEquals(d, mManagedDeviceList.findOrCreate(new StubDevice("foo")));
     67         assertEquals(1, mManagedDeviceList.size());
     68     }
     69 
     70     /**
     71      * Test that {@link ManagedDeviceList#findOrCreate(IDevice)} ignores devices with invalid
     72      * serials
     73      */
     74     public void testFindOrCreate_invalidSerial() {
     75         assertNull(mManagedDeviceList.findOrCreate(new StubDevice("????")));
     76     }
     77 
     78     /**
     79      * Basic test for {@link ManagedDeviceList#allocate(IDeviceSelection)}
     80      */
     81     public void testAllocate() {
     82         // verify allocate fails when no devices are in list
     83         assertNull(mManagedDeviceList.allocate(DeviceManager.ANY_DEVICE_OPTIONS));
     84         IManagedTestDevice d = mManagedDeviceList.findOrCreate(new StubDevice("foo"));
     85         assertNotNull(d);
     86         // verify allocate fails because device is not available
     87         assertNull(mManagedDeviceList.allocate(DeviceManager.ANY_DEVICE_OPTIONS));
     88         d.handleAllocationEvent(DeviceEvent.FORCE_AVAILABLE);
     89         // verify allocate succeeds because device is available
     90         assertNotNull(mManagedDeviceList.allocate(DeviceManager.ANY_DEVICE_OPTIONS));
     91         // verify allocate fails because only device is already allocated
     92         assertNull(mManagedDeviceList.allocate(DeviceManager.ANY_DEVICE_OPTIONS));
     93     }
     94 
     95     /**
     96      * Basic test for {@link ManagedDeviceList#handleDeviceEvent(IManagedTestDevice, DeviceEvent)}
     97      */
     98     public void testHandleDeviceEvent() {
     99         // verify new device can be created
    100         IManagedTestDevice d = mManagedDeviceList.findOrCreate(new StubDevice("foo"));
    101         assertNotNull(d);
    102         d.handleAllocationEvent(DeviceEvent.FORCE_ALLOCATE_REQUEST);
    103         // verify allocated device remains in list on disconnect
    104         mManagedDeviceList.handleDeviceEvent(d, DeviceEvent.DISCONNECTED);
    105         assertEquals(1, mManagedDeviceList.size());
    106         d.handleAllocationEvent(DeviceEvent.FREE_AVAILABLE);
    107         // verify available device is removed from list on disconnect
    108         mManagedDeviceList.handleDeviceEvent(d, DeviceEvent.DISCONNECTED);
    109         assertEquals(0, mManagedDeviceList.size());
    110     }
    111 
    112     /**
    113      * Test for {@link ManagedDeviceList#updateFastbootStates(Set)} when device switch to fastboot
    114      * state.
    115      */
    116     public void testUpdateFastbootState() {
    117         IManagedTestDevice mockDevice = EasyMock.createMock(IManagedTestDevice.class);
    118         EasyMock.expect(mockDevice.getSerialNumber()).andReturn("serial1");
    119         mockDevice.setDeviceState(TestDeviceState.FASTBOOT);
    120         EasyMock.expectLastCall();
    121         mManagedDeviceList.add(mockDevice);
    122         assertEquals(1, mManagedDeviceList.size());
    123         EasyMock.replay(mockDevice);
    124         Set<String> serialFastbootSet = new HashSet<>();
    125         serialFastbootSet.add("serial1");
    126         mManagedDeviceList.updateFastbootStates(serialFastbootSet);
    127         EasyMock.verify(mockDevice);
    128         // Device is still showing in the list of device
    129         assertEquals(1, mManagedDeviceList.size());
    130     }
    131 
    132     /**
    133      * Test for {@link ManagedDeviceList#updateFastbootStates(Set)} when device was in fastboot and
    134      * appears to be gone now.
    135      */
    136     public void testUpdateFastbootState_gone() {
    137         IManagedTestDevice mockDevice = EasyMock.createMock(IManagedTestDevice.class);
    138         EasyMock.expect(mockDevice.getSerialNumber()).andStubReturn("serial1");
    139         EasyMock.expect(mockDevice.getDeviceState()).andReturn(TestDeviceState.FASTBOOT);
    140         mockDevice.setDeviceState(TestDeviceState.NOT_AVAILABLE);
    141         EasyMock.expectLastCall();
    142         DeviceEventResponse der = new DeviceEventResponse(DeviceAllocationState.Unknown, true);
    143         EasyMock.expect(mockDevice.handleAllocationEvent(DeviceEvent.DISCONNECTED)).andReturn(der);
    144         mManagedDeviceList.add(mockDevice);
    145         assertEquals(1, mManagedDeviceList.size());
    146         EasyMock.replay(mockDevice);
    147         Set<String> serialFastbootSet = new HashSet<>();
    148         mManagedDeviceList.updateFastbootStates(serialFastbootSet);
    149         EasyMock.verify(mockDevice);
    150         // Device has been removed from list
    151         assertEquals(0, mManagedDeviceList.size());
    152     }
    153 }
    154