Home | History | Annotate | Download | only in testtype
      1 /*
      2  * Copyright (C) 2011 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.ddmlib.IDevice;
     20 import com.android.tradefed.device.ITestDevice;
     21 import com.android.tradefed.device.StubDevice;
     22 import com.android.tradefed.log.LogUtil.CLog;
     23 import com.android.tradefed.util.IRunUtil;
     24 
     25 import com.google.common.util.concurrent.SettableFuture;
     26 
     27 import org.easymock.EasyMock;
     28 import org.junit.Before;
     29 import org.junit.Test;
     30 import org.junit.runner.RunWith;
     31 import org.junit.runners.JUnit4;
     32 
     33 import java.util.concurrent.Future;
     34 
     35 /** Unit tests for {@link DeviceBatteryLevelChecker}. */
     36 @RunWith(JUnit4.class)
     37 public class DeviceBatteryLevelCheckerTest {
     38     private DeviceBatteryLevelChecker mChecker = null;
     39     ITestDevice mFakeTestDevice = null;
     40     IDevice mFakeDevice = null;
     41     public Integer mBatteryLevel = 10;
     42 
     43     @Before
     44     public void setUp() throws Exception {
     45         mChecker = new DeviceBatteryLevelChecker() {
     46             @Override
     47             IRunUtil getRunUtil() {
     48                 return EasyMock.createNiceMock(IRunUtil.class);
     49             }
     50         };
     51         mFakeTestDevice = EasyMock.createStrictMock(ITestDevice.class);
     52         mFakeDevice = new StubDevice("serial") {
     53             @Override
     54             public Future<Integer> getBattery() {
     55                 SettableFuture<Integer> f = SettableFuture.create();
     56                 f.set(mBatteryLevel);
     57                 return f;
     58             }
     59         };
     60 
     61         mChecker.setDevice(mFakeTestDevice);
     62 
     63         EasyMock.expect(mFakeTestDevice.getSerialNumber()).andStubReturn("SERIAL");
     64         EasyMock.expect(mFakeTestDevice.getIDevice()).andStubReturn(mFakeDevice);
     65     }
     66 
     67     @Test
     68     public void testNull() throws Exception {
     69         expectBattLevel(null);
     70         replayDevices();
     71 
     72         mChecker.run(null);
     73         // expect this to return immediately without throwing an exception.  Should log a warning.
     74         verifyDevices();
     75     }
     76 
     77     @Test
     78     public void testNormal() throws Exception {
     79         expectBattLevel(45);
     80         replayDevices();
     81 
     82         mChecker.run(null);
     83         verifyDevices();
     84     }
     85 
     86     /** Low battery with a resume level very low to check a resume if some level are reached. */
     87     @Test
     88     public void testLow() throws Exception {
     89         mFakeTestDevice.stopLogcat();
     90         EasyMock.expectLastCall();
     91         expectBattLevel(5);
     92         EasyMock.expect(mFakeTestDevice.executeShellCommand("svc power stayon false"))
     93                 .andStubReturn("");
     94         EasyMock.expect(mFakeTestDevice.executeShellCommand(
     95                 "settings put system screen_off_timeout 1000")).andStubReturn("");
     96         replayDevices();
     97         mChecker.setResumeLevel(5);
     98         mChecker.run(null);
     99         verifyDevices();
    100     }
    101 
    102     /** Battery is low, device idles and battery gets high again. */
    103     @Test
    104     public void testLow_becomeHigh() throws Exception {
    105         mFakeTestDevice.stopLogcat();
    106         EasyMock.expectLastCall();
    107         expectBattLevel(5);
    108         EasyMock.expect(mFakeTestDevice.executeShellCommand("svc power stayon false"))
    109                 .andStubReturn("");
    110         EasyMock.expect(mFakeTestDevice.executeShellCommand(
    111                 "settings put system screen_off_timeout 1000")).andStubReturn("");
    112         replayDevices();
    113         Thread raise = new Thread(new Runnable() {
    114             @Override
    115             public void run() {
    116                 try {
    117                     Thread.sleep(100);
    118                     expectBattLevel(85);
    119                 } catch (Exception e) {
    120                     CLog.e(e);
    121                 }
    122             }
    123         });
    124         raise.start();
    125         mChecker.run(null);
    126         verifyDevices();
    127     }
    128 
    129     /** Battery is low, device idles and battery gets null, break the loop. */
    130     @Test
    131     public void testLow_becomeNull() throws Exception {
    132         mFakeTestDevice.stopLogcat();
    133         EasyMock.expectLastCall();
    134         expectBattLevel(5);
    135         EasyMock.expect(mFakeTestDevice.executeShellCommand("svc power stayon false"))
    136                 .andStubReturn("");
    137         EasyMock.expect(mFakeTestDevice.executeShellCommand(
    138                 "settings put system screen_off_timeout 1000")).andStubReturn("");
    139         replayDevices();
    140         Thread raise = new Thread(new Runnable() {
    141             @Override
    142             public void run() {
    143                 try {
    144                     Thread.sleep(10);
    145                     expectBattLevel(null);
    146                 } catch (Exception e) {
    147                     CLog.e(e);
    148                 }
    149             }
    150         });
    151         raise.start();
    152         mChecker.run(null);
    153         verifyDevices();
    154     }
    155 
    156     private void expectBattLevel(Integer level) throws Exception {
    157         mBatteryLevel = level;
    158         EasyMock.expect(mFakeDevice.getBattery());
    159     }
    160 
    161     private void replayDevices() {
    162         EasyMock.replay(mFakeTestDevice);
    163     }
    164 
    165     private void verifyDevices() {
    166         EasyMock.verify(mFakeTestDevice);
    167     }
    168 }
    169 
    170