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 17 package com.android.server.cts; 18 19 import android.os.BatteryHealthEnum; 20 import android.os.BatteryPluggedStateEnum; 21 import android.os.BatteryStatusEnum; 22 import android.service.battery.BatteryServiceDumpProto; 23 import com.android.tradefed.device.DeviceNotAvailableException; 24 import com.android.tradefed.device.ITestDevice; 25 26 /** Test to check that the battery manager properly outputs its dump state. */ 27 public class BatteryIncidentTest extends ProtoDumpTestCase { 28 private static final String LEANBACK_FEATURE = "android.software.leanback"; 29 30 public void testBatteryServiceDump() throws Exception { 31 if (!hasBattery(getDevice())) { 32 return; 33 } 34 35 final BatteryServiceDumpProto dump = 36 getDump(BatteryServiceDumpProto.parser(), "dumpsys battery --proto"); 37 38 verifyBatteryServiceDumpProto(dump, PRIVACY_NONE); 39 } 40 41 static void verifyBatteryServiceDumpProto(BatteryServiceDumpProto dump, final int filterLevel) { 42 if (!dump.getIsPresent()) { 43 /* If the battery isn't present, no need to run this test. */ 44 return; 45 } 46 47 assertTrue(dump.getPlugged() != BatteryPluggedStateEnum.BATTERY_PLUGGED_WIRELESS); 48 assertTrue(dump.getMaxChargingCurrent() >= 0); 49 assertTrue(dump.getMaxChargingVoltage() >= 0); 50 assertTrue(dump.getChargeCounter() >= 0); 51 assertTrue( 52 dump.getStatus() != BatteryStatusEnum.BATTERY_STATUS_INVALID); 53 assertTrue( 54 dump.getHealth() != BatteryHealthEnum.BATTERY_HEALTH_INVALID); 55 int scale = dump.getScale(); 56 assertTrue(scale > 0); 57 int level = dump.getLevel(); 58 assertTrue(level >= 0 && level <= scale); 59 assertTrue(dump.getVoltage() >= 0); 60 } 61 62 static boolean hasBattery(ITestDevice device) throws DeviceNotAvailableException { 63 final String batteryinfo = device.executeShellCommand("dumpsys battery"); 64 boolean hasBattery = batteryinfo.contains("present: true"); 65 return hasBattery; 66 } 67 68 } 69