1#!/usr/bin/env python3.4 2# 3# Copyright 2016 - The Android Open Source Project 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16 17import os 18 19import acts.base_test 20from acts import asserts 21from acts import utils 22from acts.controllers import monsoon 23from acts.test_utils.wifi import wifi_test_utils as wutils 24from acts.test_utils.tel import tel_test_utils as tel_utils 25 26pmc_base_cmd = ("am broadcast -a com.android.pmc.action.AUTOPOWER --es" 27 " PowerAction ") 28start_pmc_cmd = ("am start -n com.android.pmc/com.android.pmc." 29 "PMCMainActivity") 30pmc_interval_cmd = ("am broadcast -a com.android.pmc.action.SETINTERVAL --es " 31 "Value %s ") 32pmc_start_connect_scan_cmd = "%sStartConnectivityScan" % pmc_base_cmd 33pmc_stop_connect_scan_cmd = "%sStopConnectivityScan" % pmc_base_cmd 34pmc_start_gscan_no_dfs_cmd = "%sStartGScanBand" % pmc_base_cmd 35pmc_start_gscan_specific_channels_cmd = "%sStartGScanChannel" % pmc_base_cmd 36pmc_stop_gscan_cmd = "%sStopGScan" % pmc_base_cmd 37pmc_start_1MB_download_cmd = "%sDownload1MB" % pmc_base_cmd 38pmc_stop_1MB_download_cmd = "%sStopDownload" % pmc_base_cmd 39 40class WifiPowerTest(acts.base_test.BaseTestClass): 41 42 def __init__(self, controllers): 43 super(WifiPowerTest, self).__init__(controllers) 44 self.tests = ( 45 "test_power_wifi_off", 46 "test_power_wifi_on_idle", 47 "test_power_disconnected_connectivity_scan", 48 "test_power_connected_2g_continuous_download", 49 "test_power_connected_2g_idle", 50 "test_power_connected_5g_continuous_download", 51 "test_power_connected_5g_idle", 52 "test_power_gscan_three_2g_channels", 53 "test_power_gscan_all_channels_no_dfs", 54 "test_power_connected_2g_gscan_all_channels_no_dfs", 55 "test_power_connected_5g_gscan_all_channels_no_dfs" 56 ) 57 58 def setup_class(self): 59 self.hz = 10 60 self.offset = 5 * 60 61 self.duration = 30 * 60 + self.offset 62 self.scan_interval = 15 63 # Continuosly download 64 self.download_interval = 0 65 self.mon_data_path = os.path.join(self.log_path, "Monsoon") 66 67 self.mon = self.monsoons[0] 68 self.mon.set_voltage(4.2) 69 self.mon.set_max_current(7.8) 70 self.dut = self.android_devices[0] 71 self.mon.attach_device(self.dut) 72 asserts.assert_true(self.mon.usb("auto"), 73 "Failed to turn USB mode to auto on monsoon.") 74 required_userparam_names = ( 75 # These two params should follow the format of 76 # {"SSID": <SSID>, "password": <Password>} 77 "network_2g", 78 "network_5g" 79 ) 80 self.unpack_userparams(required_userparam_names, threshold=None) 81 wutils.wifi_test_device_init(self.dut) 82 # Start pmc app. 83 self.dut.adb.shell(start_pmc_cmd) 84 self.dut.adb.shell("setprop log.tag.PMC VERBOSE") 85 86 def teardown_class(self): 87 self.mon.usb("on") 88 89 def setup_test(self): 90 wutils.reset_wifi(self.dut) 91 self.dut.ed.clear_all_events() 92 93 def measure_and_process_result(self): 94 """Measure the current drawn by the device for the period of 95 self.duration, at the frequency of self.hz. 96 97 If self.threshold exists, also verify that the average current of the 98 measurement is below the acceptable threshold. 99 """ 100 tag = self.current_test_name 101 result = self.mon.measure_power(self.hz, 102 self.duration, 103 tag=tag, 104 offset=self.offset) 105 asserts.assert_true(result, "Got empty measurement data set in %s." % tag) 106 self.log.info(repr(result)) 107 data_path = os.path.join(self.mon_data_path, "%s.txt" % tag) 108 monsoon.MonsoonData.save_to_text_file([result], data_path) 109 actual_current = result.average_current 110 actual_current_str = "%.2fmA" % actual_current 111 result_extra = {"Average Current": actual_current_str} 112 if self.threshold: 113 model = utils.trim_model_name(self.dut.model) 114 asserts.assert_true(tag in self.threshold[model], 115 "Acceptance threshold for %s is missing" % tag, 116 extras=result_extra) 117 acceptable_threshold = self.threshold[model][tag] 118 asserts.assert_true(actual_current < acceptable_threshold, 119 ("Measured average current for %s - %s - is " 120 "higher than acceptable threshold %.2f.") % ( 121 tag, actual_current_str, acceptable_threshold), 122 extras=result_extra) 123 asserts.explicit_pass("Measurement finished for %s." % tag, 124 extras=result_extra) 125 126 def test_power_wifi_off(self): 127 asserts.assert_true(wutils.wifi_toggle_state(self.dut, False), 128 "Failed to toggle wifi off.") 129 self.measure_and_process_result() 130 131 def test_power_wifi_on_idle(self): 132 asserts.assert_true(wutils.wifi_toggle_state(self.dut, True), 133 "Failed to toggle wifi on.") 134 self.measure_and_process_result() 135 136 def test_power_disconnected_connectivity_scan(self): 137 try: 138 self.dut.adb.shell(pmc_interval_cmd % self.scan_interval) 139 self.dut.adb.shell(pmc_start_connect_scan_cmd) 140 self.log.info("Started connectivity scan.") 141 self.measure_and_process_result() 142 finally: 143 self.dut.adb.shell(pmc_stop_connect_scan_cmd) 144 self.log.info("Stoped connectivity scan.") 145 146 def test_power_connected_2g_idle(self): 147 wutils.wifi_connect(self.dut, self.network_2g) 148 self.measure_and_process_result() 149 150 def test_power_connected_2g_continuous_download(self): 151 try: 152 self.dut.adb.shell(pmc_interval_cmd % self.download_interval) 153 self.dut.adb.shell(pmc_start_1MB_download_cmd) 154 self.log.info("Start downloading 1MB file continuously.") 155 self.measure_and_process_result() 156 finally: 157 self.dut.adb.shell(pmc_stop_1MB_download_cmd) 158 self.log.info("Stopped downloading 1MB file.") 159 160 def test_power_connected_5g_idle(self): 161 wutils.reset_wifi(self.dut) 162 self.dut.ed.clear_all_events() 163 wutils.wifi_connect(self.dut, self.network_5g) 164 self.measure_and_process_result() 165 166 def test_power_connected_5g_continuous_download(self): 167 try: 168 self.dut.adb.shell(pmc_interval_cmd % self.download_interval) 169 self.dut.adb.shell(pmc_start_1MB_download_cmd) 170 self.log.info("Started downloading 1MB file continuously.") 171 self.measure_and_process_result() 172 finally: 173 self.dut.adb.shell(pmc_stop_1MB_download_cmd) 174 self.log.info("Stopped downloading 1MB file.") 175 176 def test_power_gscan_three_2g_channels(self): 177 try: 178 self.dut.adb.shell(pmc_interval_cmd % self.scan_interval) 179 self.dut.adb.shell(pmc_start_gscan_specific_channels_cmd) 180 self.log.info("Started gscan for 2G channels 1, 6, and 11.") 181 self.measure_and_process_result() 182 finally: 183 self.dut.adb.shell(pmc_stop_gscan_cmd) 184 self.log.info("Stopped gscan.") 185 186 def test_power_gscan_all_channels_no_dfs(self): 187 try: 188 self.dut.adb.shell(pmc_interval_cmd % self.scan_interval) 189 self.dut.adb.shell(pmc_start_gscan_no_dfs_cmd) 190 self.log.info("Started gscan for all non-DFS channels.") 191 self.measure_and_process_result() 192 finally: 193 self.dut.adb.shell(pmc_stop_gscan_cmd) 194 self.log.info("Stopped gscan.") 195 196 def test_power_connected_2g_gscan_all_channels_no_dfs(self): 197 try: 198 wutils.wifi_connect(self.dut, self.network_2g) 199 self.dut.adb.shell(pmc_interval_cmd % self.scan_interval) 200 self.dut.adb.shell(pmc_start_gscan_no_dfs_cmd) 201 self.log.info("Started gscan for all non-DFS channels.") 202 self.measure_and_process_result() 203 finally: 204 self.dut.adb.shell(pmc_stop_gscan_cmd) 205 self.log.info("Stopped gscan.") 206 207 def test_power_connected_5g_gscan_all_channels_no_dfs(self): 208 try: 209 wutils.wifi_connect(self.dut, self.network_5g) 210 self.dut.adb.shell(pmc_interval_cmd % self.scan_interval) 211 self.dut.adb.shell(pmc_start_gscan_no_dfs_cmd) 212 self.log.info("Started gscan for all non-DFS channels.") 213 self.measure_and_process_result() 214 finally: 215 self.dut.adb.shell(pmc_stop_gscan_cmd) 216 self.log.info("Stopped gscan.") 217