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