1#!/usr/bin/env python3.4 2# 3# Copyright 2016 - Google 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""" 17 Base Class for Defining Common Bluetooth Test Functionality 18""" 19 20import os 21import time 22from acts import utils 23from acts.base_test import BaseTestClass 24from acts.controllers import android_device 25from acts.test_utils.bt.bt_test_utils import ( 26 log_energy_info, reset_bluetooth, setup_multiple_devices_for_bt_test, 27 take_btsnoop_logs) 28 29 30class BluetoothBaseTest(BaseTestClass): 31 def __init__(self, controllers): 32 BaseTestClass.__init__(self, controllers) 33 34 # Use for logging in the test cases to facilitate 35 # faster log lookup and reduce ambiguity in logging. 36 def bt_test_wrap(fn): 37 def _safe_wrap_test_case(self, *args, **kwargs): 38 test_id = "{}:{}:{}".format(self.__class__.__name__, fn.__name__, 39 time.time()) 40 log_string = "[Test ID] {}".format(test_id) 41 self.log.info(log_string) 42 return fn(self, *args, **kwargs) 43 44 return _safe_wrap_test_case 45 46 def setup_class(self): 47 return setup_multiple_devices_for_bt_test(self.android_devices) 48 49 def setup_test(self): 50 self.log.debug(log_energy_info(self.android_devices, "Start")) 51 for a in self.android_devices: 52 a.ed.clear_all_events() 53 return True 54 55 def teardown_test(self): 56 self.log.debug(log_energy_info(self.android_devices, "End")) 57 return True 58 59 def on_fail(self, test_name, begin_time): 60 self.log.debug( 61 "Test {} failed. Gathering bugreport and btsnoop logs".format( 62 test_name)) 63 take_btsnoop_logs(self.android_devices, self, test_name) 64 self._take_bug_report(test_name, begin_time) 65 for _ in range(5): 66 if reset_bluetooth(self.android_devices): 67 break 68 else: 69 self.log.error("Failed to reset Bluetooth... retrying.") 70 return 71 72 def _take_bug_report(self, test_name, begin_time): 73 if "no_bug_report_on_fail" in self.user_params: 74 return 75 76 # magical sleep to ensure the runtime restart or reboot begins 77 time.sleep(1) 78 for ad in self.android_devices: 79 try: 80 ad.adb.wait_for_device() 81 ad.take_bug_report(test_name, begin_time) 82 tombstone_path = os.path.join(ad.log_path, "BugReports", 83 "{},{}".format(begin_time, ad.serial).replace(' ','_')) 84 utils.create_dir(tombstone_path) 85 ad.adb.pull('/data/tombstones/', tombstone_path) 86 except: 87 ad.log.error("Failed to take a bug report for {}, {}" 88 .format(ad.serial, test_name)) 89