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