1#!/usr/bin/env python3.4
2#
3#   Copyright 2018 - 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 time
18import acts.test_utils.bt.BleEnum as bleenum
19import acts.test_utils.bt.bt_power_test_utils as btputils
20import acts.test_utils.power.PowerBTBaseTest as PBtBT
21
22BLE_LOCATION_SCAN_ENABLE = 'settings put secure location_mode 3'
23EXTRA_SCAN_TIME = 10
24MONSOON_TAIL_CUT = 5
25
26
27class PowerBLEscanTest(PBtBT.PowerBTBaseTest):
28    def __init__(self, configs):
29        super().__init__(configs)
30        req_params = ['scan_modes', 'scan_duration']
31        self.unpack_userparams(req_params)
32
33        for scan_mode in self.scan_modes:
34            self.generate_test_case_no_devices_around(scan_mode,
35                                                      self.scan_duration)
36
37    def setup_class(self):
38
39        super().setup_class()
40        self.dut.adb.shell(BLE_LOCATION_SCAN_ENABLE)
41        # Make sure during power measurement, scan is always on
42        self.mon_info.duration = (self.scan_duration - self.mon_offset -
43                                  EXTRA_SCAN_TIME - MONSOON_TAIL_CUT)
44
45    def generate_test_case_no_devices_around(self, scan_mode, scan_duration):
46        def test_case_fn():
47
48            self.measure_ble_scan_power(scan_mode, scan_duration)
49
50        test_case_name = ('test_BLE_{}_no_advertisers'.format(
51            bleenum.ScanSettingsScanMode(scan_mode).name))
52        setattr(self, test_case_name, test_case_fn)
53
54    def measure_ble_scan_power(self, scan_mode, scan_duration):
55
56        btputils.start_apk_ble_scan(self.dut, scan_mode, scan_duration)
57        time.sleep(EXTRA_SCAN_TIME)
58        self.measure_power_and_validate()
59