Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2017 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.app.ProcessStateEnum;
     20 import android.content.IntentProto;
     21 import android.os.BatteryPluggedStateEnum;
     22 import android.os.LooperProto;
     23 import android.os.PowerManagerInternalProto;
     24 import android.os.PowerManagerProto;
     25 import com.android.server.power.PowerManagerServiceDumpProto;
     26 import com.android.server.power.PowerServiceSettingsAndConfigurationDumpProto;
     27 import com.android.server.power.WakeLockProto;
     28 
     29 /** Test to check that the power manager properly outputs its dump state. */
     30 public class PowerIncidentTest extends ProtoDumpTestCase {
     31     private static final int SYSTEM_UID = 1000;
     32 
     33     public void testPowerServiceDump() throws Exception {
     34         final PowerManagerServiceDumpProto dump =
     35                 getDump(PowerManagerServiceDumpProto.parser(), "dumpsys power --proto");
     36 
     37         verifyPowerManagerServiceDumpProto(dump, PRIVACY_NONE);
     38     }
     39 
     40     static void verifyPowerManagerServiceDumpProto(PowerManagerServiceDumpProto dump, int filterLevel) {
     41         assertTrue(dump.getBatteryLevel() >= 0);
     42         assertTrue(dump.getBatteryLevel() <= 100);
     43 
     44         assertTrue(
     45                 PowerManagerInternalProto.Wakefulness.getDescriptor()
     46                         .getValues()
     47                         .contains(dump.getWakefulness().getValueDescriptor()));
     48         assertTrue(
     49                 BatteryPluggedStateEnum.getDescriptor()
     50                         .getValues()
     51                         .contains(dump.getPlugType().getValueDescriptor()));
     52         assertTrue(
     53                 IntentProto.DockState.getDescriptor()
     54                         .getValues()
     55                         .contains(dump.getDockState().getValueDescriptor()));
     56 
     57         final PowerServiceSettingsAndConfigurationDumpProto settingsAndConfiguration =
     58                 dump.getSettingsAndConfiguration();
     59         assertTrue(settingsAndConfiguration.getMinimumScreenOffTimeoutConfigMs() > 0);
     60         assertTrue(settingsAndConfiguration.getMaximumScreenDimDurationConfigMs() >= 0);
     61         assertTrue(settingsAndConfiguration.getMaximumScreenDimRatioConfig() > 0);
     62         assertTrue(settingsAndConfiguration.getScreenOffTimeoutSettingMs() > 0);
     63         // Default value is -1.
     64         assertTrue(settingsAndConfiguration.getSleepTimeoutSettingMs() >= -1);
     65         assertTrue(settingsAndConfiguration.getMaximumScreenOffTimeoutFromDeviceAdminMs() > 0);
     66         // -1 is used to disable, so is valid.
     67         assertTrue(settingsAndConfiguration.getUserActivityTimeoutOverrideFromWindowManagerMs() >= -1);
     68         final PowerServiceSettingsAndConfigurationDumpProto.ScreenBrightnessSettingLimitsProto
     69                 brightnessLimits = settingsAndConfiguration.getScreenBrightnessSettingLimits();
     70         int settingMax = brightnessLimits.getSettingMaximum();
     71         int settingMin = brightnessLimits.getSettingMinimum();
     72         assertTrue(settingMin >= 0);
     73         assertTrue(settingMax > 0);
     74         assertTrue("Brightness limit max setting (" + settingMax + ") is less than min setting (" + settingMin + ")",
     75                 settingMax >= settingMin);
     76         assertTrue(brightnessLimits.getSettingDefault() > 0);
     77 
     78         final PowerManagerServiceDumpProto.UidStateProto uid = dump.getUidStates(0);
     79         assertEquals(uid.getUid(), SYSTEM_UID);
     80         assertEquals(uid.getUidString(), Integer.toString(SYSTEM_UID));
     81         assertTrue(uid.getIsActive());
     82 
     83         for (PowerManagerServiceDumpProto.UidStateProto us : dump.getUidStatesList()) {
     84             assertTrue(0 <= us.getUid());
     85             assertTrue(0 <= us.getNumWakeLocks());
     86             assertTrue(ProcessStateEnum.getDescriptor()
     87                     .getValues()
     88                     .contains(us.getProcessState().getValueDescriptor()));
     89         }
     90 
     91         final LooperProto looper = dump.getLooper();
     92         assertNotNull(looper.getThreadName());
     93         assertTrue(looper.getThreadId() > 0);
     94 
     95         assertTrue(dump.getSuspendBlockersCount() > 0);
     96 
     97         // Check that times/durations are not incorrectly negative.
     98         assertTrue(dump.getNotifyLongScheduledMs() >= 0);
     99         assertTrue(dump.getNotifyLongDispatchedMs() >= 0);
    100         assertTrue(dump.getNotifyLongNextCheckMs() >= 0);
    101         assertTrue(dump.getLastWakeTimeMs() >= 0);
    102         assertTrue(dump.getLastSleepTimeMs() >= 0);
    103         assertTrue(dump.getLastUserActivityTimeMs() >= 0);
    104         assertTrue(dump.getLastUserActivityTimeNoChangeLightsMs() >= 0);
    105         assertTrue(dump.getLastInteractivePowerHintTimeMs() >= 0);
    106         assertTrue(dump.getLastScreenBrightnessBoostTimeMs() >= 0);
    107         // -1 is a valid value.
    108         assertTrue(dump.getSleepTimeoutMs() >= -1);
    109         assertTrue(dump.getScreenOffTimeoutMs() >= 0);
    110         assertTrue(dump.getScreenDimDurationMs() >= 0);
    111 
    112         for (WakeLockProto wl : dump.getWakeLocksList()) {
    113             assertTrue(0 <= wl.getAcqMs());
    114             assertTrue(0 <= wl.getUid());
    115             assertTrue(0 <= wl.getPid());
    116         }
    117     }
    118 }
    119