1"""A module to trigger device into doze mode.""" 2import enum 3from retry import retry 4 5 6_UNPLUG_POWER = "dumpsys battery unplug" 7_RESET_POWER = "dumpsys battery reset" 8_GET_DOZE_STATUS = "dumpsys deviceidle get {status}" 9_FORCE_IDLE = "dumpsys deviceidle force-idle {status}" 10_LEAVE_IDLE = "dumpsys deviceidle disable" 11 12 13class DozeState(enum.Enum): 14 """Doze state.""" 15 INACTIVE = "INACTIVE" 16 ACTIVE = "ACTIVE" 17 IDLE = "IDLE" 18 19 20class DozeType(enum.Enum): 21 DEEP = "deep" 22 LIGHT = "light" 23 24 25def _check_doze_status( 26 dut, 27 doze_type: DozeType, 28 doze_state: DozeState): 29 command = _GET_DOZE_STATUS.format(status=doze_type.value) 30 doze_status = dut.adb.shell(command).strip() 31 dut.log.info("%s doze status is %s" % (doze_type.value, doze_status)) 32 if doze_status != doze_state.value: 33 raise ValueError("Unexpected doze status.") 34 35 36@retry(exceptions=ValueError, tries=3, delay=1) 37def enter_doze_mode( 38 dut, 39 doze_type: DozeType): 40 """Sets device into doze mode according to given doze type. 41 Args: 42 dut: The device under test. 43 doze_type: The desired doze type. 44 Raises: 45 ValueError: When fail to sets device into doze mode. 46 """ 47 dut.adb.shell(_UNPLUG_POWER) 48 dut.adb.shell( 49 _FORCE_IDLE.format(status=doze_type.value), 50 ) 51 _check_doze_status(dut, doze_type, DozeState.IDLE) 52 53 54@retry(exceptions=ValueError, tries=3, delay=1) 55def leave_doze_mode( 56 dut, 57 doze_type: DozeType): 58 """Sets device out of doze mode. 59 Args: 60 dut: The device under test. 61 doze_type: The desired doze type. 62 Raises: 63 ValueError: When fail to sets device out of doze mode. 64 """ 65 dut.adb.shell(_RESET_POWER) 66 dut.adb.shell(_LEAVE_IDLE) 67 _check_doze_status(dut, doze_type, DozeState.ACTIVE) 68