Home | History | Annotate | Download | only in device
      1 /*
      2  * Copyright (C) 2016 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.util.CommandResult;
     20 import com.android.tradefed.util.CommandStatus;
     21 import com.android.tradefed.util.IRunUtil;
     22 
     23 import junit.framework.TestCase;
     24 
     25 import org.easymock.EasyMock;
     26 
     27 /**
     28  * Unit tests for {@link RemoteAndroidDevice}.
     29  */
     30 public class RemoteAndroidDeviceTest extends TestCase {
     31 
     32     private static final String MOCK_DEVICE_SERIAL = "localhost:1234";
     33     private IDevice mMockIDevice;
     34     private IDeviceStateMonitor mMockStateMonitor;
     35     private IRunUtil mMockRunUtil;
     36     private IDeviceMonitor mMockDvcMonitor;
     37     private IDeviceRecovery mMockRecovery;
     38     private RemoteAndroidDevice mTestDevice;
     39 
     40     /**
     41      * A {@link TestDevice} that is suitable for running tests against
     42      */
     43     private class TestableRemoteAndroidDevice extends RemoteAndroidDevice {
     44         public TestableRemoteAndroidDevice() {
     45             super(mMockIDevice, mMockStateMonitor, mMockDvcMonitor);
     46         }
     47 
     48         @Override
     49         protected IRunUtil getRunUtil() {
     50             return mMockRunUtil;
     51         }
     52     }
     53 
     54     /**
     55      * {@inheritDoc}
     56      */
     57     @Override
     58     protected void setUp() throws Exception {
     59         super.setUp();
     60         mMockIDevice = EasyMock.createMock(IDevice.class);
     61         EasyMock.expect(mMockIDevice.getSerialNumber()).andReturn(MOCK_DEVICE_SERIAL).anyTimes();
     62         mMockRecovery = EasyMock.createMock(IDeviceRecovery.class);
     63         mMockStateMonitor = EasyMock.createMock(IDeviceStateMonitor.class);
     64         mMockDvcMonitor = EasyMock.createMock(IDeviceMonitor.class);
     65         mMockRunUtil = EasyMock.createMock(IRunUtil.class);
     66 
     67         // A TestDevice with a no-op recoverDevice() implementation
     68         mTestDevice = new TestableRemoteAndroidDevice();
     69         mTestDevice.setRecovery(mMockRecovery);
     70     }
     71 
     72     /**
     73      * Test {@link RemoteAndroidDevice#adbTcpConnect(String, String)} in a success case.
     74      */
     75     public void testAdbConnect() {
     76         CommandResult adbResult = new CommandResult();
     77         adbResult.setStatus(CommandStatus.SUCCESS);
     78         adbResult.setStdout("connected to");
     79         CommandResult adbResultConfirmation = new CommandResult();
     80         adbResultConfirmation.setStatus(CommandStatus.SUCCESS);
     81         adbResultConfirmation.setStdout("already connected to localhost:1234");
     82         EasyMock.expect(mMockRunUtil.runTimedCmd(EasyMock.anyLong(),
     83                 EasyMock.eq("adb"), EasyMock.eq("connect"), EasyMock.eq(MOCK_DEVICE_SERIAL)))
     84                 .andReturn(adbResult);
     85         EasyMock.expect(mMockRunUtil.runTimedCmd(EasyMock.anyLong(),
     86                 EasyMock.eq("adb"), EasyMock.eq("connect"), EasyMock.eq(MOCK_DEVICE_SERIAL)))
     87                 .andReturn(adbResultConfirmation);
     88         EasyMock.replay(mMockRunUtil);
     89         assertTrue(mTestDevice.adbTcpConnect("localhost", "1234"));
     90     }
     91 
     92     /**
     93      * Test {@link RemoteAndroidDevice#adbTcpConnect(String, String)} in a failure case.
     94      */
     95     public void testAdbConnect_fails() {
     96         CommandResult adbResult = new CommandResult();
     97         adbResult.setStatus(CommandStatus.SUCCESS);
     98         adbResult.setStdout("cannot connect");
     99         EasyMock.expect(mMockRunUtil.runTimedCmd(EasyMock.anyLong(),
    100                 EasyMock.eq("adb"), EasyMock.eq("connect"), EasyMock.eq(MOCK_DEVICE_SERIAL)))
    101                 .andReturn(adbResult).times(RemoteAndroidDevice.MAX_RETRIES);
    102         mMockRunUtil.sleep(EasyMock.anyLong());
    103         EasyMock.expectLastCall().times(RemoteAndroidDevice.MAX_RETRIES);
    104         EasyMock.replay(mMockRunUtil);
    105         assertFalse(mTestDevice.adbTcpConnect("localhost", "1234"));
    106     }
    107 
    108     /**
    109      * Test {@link RemoteAndroidDevice#adbTcpConnect(String, String)} in a case where adb connect
    110      * always return connect success (never really connected so confirmation: "already connected"
    111      * fails.
    112      */
    113     public void testAdbConnect_fails_confirmation() {
    114         CommandResult adbResult = new CommandResult();
    115         adbResult.setStatus(CommandStatus.SUCCESS);
    116         adbResult.setStdout("connected to");
    117         EasyMock.expect(mMockRunUtil.runTimedCmd(EasyMock.anyLong(),
    118                 EasyMock.eq("adb"), EasyMock.eq("connect"), EasyMock.eq(MOCK_DEVICE_SERIAL)))
    119                 .andReturn(adbResult).times(RemoteAndroidDevice.MAX_RETRIES * 2);
    120         mMockRunUtil.sleep(EasyMock.anyLong());
    121         EasyMock.expectLastCall().times(RemoteAndroidDevice.MAX_RETRIES);
    122         EasyMock.replay(mMockRunUtil);
    123         assertFalse(mTestDevice.adbTcpConnect("localhost", "1234"));
    124     }
    125 
    126     /**
    127      * Test {@link RemoteAndroidDevice#adbTcpDisconnect(String, String)}.
    128      */
    129     public void testAdbDisconnect() {
    130         CommandResult adbResult = new CommandResult();
    131         adbResult.setStatus(CommandStatus.SUCCESS);
    132         EasyMock.expect(mMockRunUtil.runTimedCmd(EasyMock.anyLong(),
    133                 EasyMock.eq("adb"), EasyMock.eq("disconnect"), EasyMock.eq(MOCK_DEVICE_SERIAL)))
    134                 .andReturn(adbResult);
    135         EasyMock.replay(mMockRunUtil);
    136         assertTrue(mTestDevice.adbTcpDisconnect("localhost", "1234"));
    137     }
    138 
    139     /**
    140      * Test {@link RemoteAndroidDevice#adbTcpDisconnect(String, String)} in a failure case.
    141      */
    142     public void testAdbDisconnect_fails() {
    143         CommandResult adbResult = new CommandResult();
    144         adbResult.setStatus(CommandStatus.FAILED);
    145         EasyMock.expect(mMockRunUtil.runTimedCmd(EasyMock.anyLong(),
    146                 EasyMock.eq("adb"), EasyMock.eq("disconnect"), EasyMock.eq(MOCK_DEVICE_SERIAL)))
    147                 .andReturn(adbResult);
    148         EasyMock.replay(mMockRunUtil);
    149         assertFalse(mTestDevice.adbTcpDisconnect("localhost", "1234"));
    150     }
    151 
    152     public void testCheckSerial() {
    153         EasyMock.replay(mMockIDevice);
    154         assertEquals("localhost", mTestDevice.getHostName());
    155         assertEquals("1234", mTestDevice.getPortNum());
    156     }
    157 
    158     public void testCheckSerial_invalid() {
    159         mMockIDevice = EasyMock.createMock(IDevice.class);
    160         EasyMock.expect(mMockIDevice.getSerialNumber()).andReturn("wrongserial").anyTimes();
    161         try {
    162             mTestDevice.getHostName();
    163         } catch (RuntimeException e) {
    164             // expected
    165             return;
    166         }
    167         fail("Wrong Serial should throw a RuntimeException");
    168     }
    169 
    170     public void testGetMacAddress() {
    171         assertNull(mTestDevice.getMacAddress());
    172     }
    173 }
    174