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 os 18import time 19import acts.test_utils.bt.bt_power_test_utils as btputils 20import acts.test_utils.bt.bt_test_utils as btutils 21import acts.test_utils.power.PowerBaseTest as PBT 22from acts.test_utils.abstract_devices.bluetooth_handsfree_abstract_device import BluetoothHandsfreeAbstractDeviceFactory as bt_factory 23from math import copysign 24 25BLE_LOCATION_SCAN_DISABLE = 'settings put secure location_mode 0' 26PHONE_MUSIC_FILE_DIRECTORY = '/sdcard/Music' 27INIT_ATTEN = [0] 28 29 30def ramp_attenuation(obj_atten, attenuation_target): 31 """Ramp the attenuation up or down for BT tests. 32 33 Ramp the attenuation slowly so it won't have dramatic signal drop to affect 34 Link. 35 36 Args: 37 obj_atten: attenuator object, a single port attenuator 38 attenuation_target: target attenuation level to reach to. 39 """ 40 attenuation_step_max = 20 41 sign = lambda x: copysign(1, x) 42 attenuation_delta = obj_atten.get_atten() - attenuation_target 43 while abs(attenuation_delta) > attenuation_step_max: 44 attenuation_intermediate = obj_atten.get_atten( 45 ) - sign(attenuation_delta) * attenuation_step_max 46 obj_atten.set_atten(attenuation_intermediate) 47 time.sleep(5) 48 attenuation_delta = obj_atten.get_atten() - attenuation_target 49 obj_atten.set_atten(attenuation_target) 50 51 52class PowerBTBaseTest(PBT.PowerBaseTest): 53 """Base class for BT power related tests. 54 55 Inherited from the PowerBaseTest class 56 """ 57 def setup_class(self): 58 59 super().setup_class() 60 # Get music file and push it to the phone 61 music_files = self.user_params.get('music_files', []) 62 if music_files: 63 music_src = music_files[0] 64 music_dest = PHONE_MUSIC_FILE_DIRECTORY 65 success = self.dut.push_system_file(music_src, music_dest) 66 if success: 67 self.music_file = os.path.join(PHONE_MUSIC_FILE_DIRECTORY, 68 os.path.basename(music_src)) 69 # Initialize media_control class 70 self.media = btputils.MediaControl(self.dut, self.music_file) 71 # Set Attenuator to the initial attenuation 72 if hasattr(self, 'attenuators'): 73 self.set_attenuation(INIT_ATTEN) 74 self.attenuator = self.attenuators[0] 75 # Create the BTOE(Bluetooth-Other-End) device object 76 bt_devices = self.user_params.get('bt_devices', []) 77 if bt_devices: 78 attr, idx = bt_devices.split(':') 79 self.bt_device_controller = getattr(self, attr)[int(idx)] 80 self.bt_device = bt_factory().generate(self.bt_device_controller) 81 else: 82 self.log.error('No BT devices config is provided!') 83 # Turn off screen as all tests will be screen off 84 self.dut.droid.goToSleepNow() 85 86 def setup_test(self): 87 88 super().setup_test() 89 self.unpack_userparams(volume=0.9) 90 # Reset BT to factory defaults 91 self.dut.droid.bluetoothFactoryReset() 92 self.bt_device.reset() 93 self.bt_device.power_on() 94 btutils.enable_bluetooth(self.dut.droid, self.dut.ed) 95 96 def teardown_test(self): 97 """Tear down necessary objects after test case is finished. 98 99 Bring down the AP interface, delete the bridge interface, stop the 100 packet sender, and reset the ethernet interface for the packet sender 101 """ 102 super().teardown_test() 103 self.dut.droid.bluetoothFactoryReset() 104 self.dut.adb.shell(BLE_LOCATION_SCAN_DISABLE) 105 if hasattr(self, 'media'): 106 self.media.stop() 107 # Set Attenuator to the initial attenuation 108 if hasattr(self, 'attenuators'): 109 self.set_attenuation(INIT_ATTEN) 110 self.bt_device.reset() 111 self.bt_device.power_off() 112 btutils.disable_bluetooth(self.dut.droid) 113 114 def teardown_class(self): 115 """Clean up the test class after tests finish running 116 117 """ 118 super().teardown_class() 119 self.dut.droid.bluetoothFactoryReset() 120 self.dut.adb.shell(BLE_LOCATION_SCAN_DISABLE) 121 self.bt_device.reset() 122 self.bt_device.power_off() 123 btutils.disable_bluetooth(self.dut.droid) 124