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.bt_test_utils as btutils
19import acts.test_utils.power.PowerBTBaseTest as PBtBT
20from acts import asserts
21from acts.test_utils.bt import BtEnum
22
23EXTRA_PLAY_TIME = 10
24
25
26class PowerBTa2dpTest(PBtBT.PowerBTBaseTest):
27    def __init__(self, configs):
28        super().__init__(configs)
29        req_params = ['codecs', 'tx_power_levels', 'atten_pl_settings']
30        self.unpack_userparams(req_params)
31        # Loop all codecs and tx power levels
32        for codec_config in self.codecs:
33            for tpl in self.tx_power_levels:
34                self.generate_test_case(codec_config, tpl)
35
36    def setup_test(self):
37        super().setup_test()
38        btutils.connect_phone_to_headset(self.dut, self.bt_device, 60)
39        vol = self.dut.droid.getMaxMediaVolume() * self.volume
40        self.dut.droid.setMediaVolume(0)
41        time.sleep(1)
42        self.dut.droid.setMediaVolume(int(vol))
43
44    def generate_test_case(self, codec_config, tpl):
45        def test_case_fn():
46            self.measure_a2dp_power(codec_config, tpl)
47
48        test_case_name = ('test_BTa2dp_{}_codec_at_PL{}'.format(
49            codec_config['codec_type'], tpl))
50        setattr(self, test_case_name, test_case_fn)
51
52    def measure_a2dp_power(self, codec_config, tpl):
53
54        current_codec = self.dut.droid.bluetoothA2dpGetCurrentCodecConfig()
55        current_codec_type = BtEnum.BluetoothA2dpCodecType(
56            current_codec['codecType']).name
57        if current_codec_type != codec_config['codec_type']:
58            codec_set = btutils.set_bluetooth_codec(self.dut, **codec_config)
59            asserts.assert_true(codec_set, 'Codec configuration failed.')
60        else:
61            self.log.info('Current Codec is {}, no need to change'.format(
62                current_codec_type))
63        # Start music playing
64        self.media.play()
65        time.sleep(EXTRA_PLAY_TIME)
66
67        # Set attenuation so BT tx at desired power level
68        self.log.info('Current Attenuation {} dB'.format(
69            self.attenuator.get_atten()))
70        tpl = 'PL' + str(tpl)
71        PBtBT.ramp_attenuation(self.attenuator, self.atten_pl_settings[tpl][0],
72                               attenuation_step_max=1, time_wait_in_between=1)
73        self.log.info('Setting Attenuator to {} dB'.format(
74            self.atten_pl_settings[tpl][0]))
75
76        self.log.info('Running A2DP with codec {} at {}'.format(
77            codec_config['codec_type'], tpl))
78        self.dut.droid.goToSleepNow()
79        self.measure_power_and_validate()
80