1# Copyright 2018 The Chromium OS Authors. All rights reserved. 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4 5import logging 6import time 7 8from autotest_lib.client.common_lib import error 9from autotest_lib.client.common_lib import ui_utils 10from autotest_lib.client.common_lib.cros import chrome 11from autotest_lib.client.cros.graphics import graphics_utils 12from autotest_lib.client.cros.bluetooth import bluetooth_device_xmlrpc_server 13 14 15class bluetooth_TurnOnOffUI(graphics_utils.GraphicsTest): 16 17 """Go to Status Tray and turn BT On/Off""" 18 version = 1 19 20 # Node roles 21 BUTTON_ROLE = "button" 22 SWITCH_ROLE = "switch" 23 24 # Node names 25 STATUS_TRAY_REGEXP = "/Status tray, /i" 26 SHOW_BLUETOOTH_SETTINGS = "/Show Bluetooth settings./i" 27 BLUETOOTH = "Bluetooth" 28 DELAY_BW_TOGGLE_ON_OFF = 5 29 30 def initialize(self): 31 """Autotest initialize function""" 32 self.xmlrpc_delegate = \ 33 bluetooth_device_xmlrpc_server.BluetoothDeviceXmlRpcDelegate() 34 super(bluetooth_TurnOnOffUI, self).initialize(raise_error_on_hang=True) 35 36 def cleanup(self): 37 """Autotest cleanup function""" 38 if self._GSC: 39 keyvals = self._GSC.get_memory_difference_keyvals() 40 for key, val in keyvals.iteritems(): 41 self.output_perf_value( 42 description=key, 43 value=val, 44 units='bytes', 45 higher_is_better=False) 46 self.write_perf_keyval(keyvals) 47 super(bluetooth_TurnOnOffUI, self).cleanup() 48 49 def open_status_tray(self, ui): 50 """Open status tray 51 52 @param ui: ui object 53 """ 54 logging.info("Opening status tray") 55 ui.doDefault_on_obj(self.STATUS_TRAY_REGEXP, True, self.BUTTON_ROLE) 56 ui.wait_for_ui_obj(self.SHOW_BLUETOOTH_SETTINGS, True, 57 role=self.BUTTON_ROLE) 58 59 def open_bluetooth_page(self, ui): 60 """Opens bluetooth settings in tray 61 62 @param ui: ui object 63 """ 64 logging.info("Opening bluetooth settings in tray") 65 ui.doDefault_on_obj(self.SHOW_BLUETOOTH_SETTINGS, True, 66 self.BUTTON_ROLE) 67 ui.wait_for_ui_obj(self.BLUETOOTH, False, role=self.SWITCH_ROLE) 68 69 def is_bluetooth_enabled(self): 70 """Returns True if bluetoothd is powered on, otherwise False""" 71 72 return self.xmlrpc_delegate._is_powered_on() 73 74 def turn_on_bluetooth(self, ui): 75 """Turn on BT in status tray 76 77 @param ui: ui object 78 """ 79 if self.is_bluetooth_enabled(): 80 logging.info('Bluetooth is turned on already..') 81 else: 82 logging.info("Turning on bluetooth") 83 ui.doDefault_on_obj(self.BLUETOOTH, False, self.SWITCH_ROLE) 84 time.sleep(self.DELAY_BW_TOGGLE_ON_OFF) 85 if self.is_bluetooth_enabled(): 86 logging.info('Turned on BT successfully..') 87 else: 88 raise error.TestFail('BT is not turned on..') 89 90 def turn_off_bluetooth(self, ui): 91 """Turn off BT in status tray 92 93 @param ui: ui object 94 """ 95 if not self.is_bluetooth_enabled(): 96 logging.info('Bluetooth is turned off already') 97 else: 98 logging.info("Turning off bluetooth") 99 ui.doDefault_on_obj(self.BLUETOOTH, False, self.SWITCH_ROLE) 100 time.sleep(self.DELAY_BW_TOGGLE_ON_OFF) 101 if not self.is_bluetooth_enabled(): 102 logging.info('Turned off BT successfully..') 103 else: 104 raise error.TestFail('Bluetooth is not turned off within time') 105 106 def run_once(self, iteration_count=3): 107 """Turn on/off bluetooth in status tray 108 109 @param iteration_count: Number of iterations to toggle on/off 110 111 """ 112 try: 113 with chrome.Chrome(autotest_ext=True) as cr: 114 ui = ui_utils.UI_Handler() 115 ui.start_ui_root(cr) 116 self.open_status_tray(ui) 117 self.open_bluetooth_page(ui) 118 logging.info("Turning off bluetooth before start test") 119 self.turn_off_bluetooth(ui) 120 for iteration in range(1, iteration_count + 1): 121 logging.info("Iteration: %d", iteration) 122 self.turn_on_bluetooth(ui) 123 self.turn_off_bluetooth(ui) 124 except error.TestFail: 125 raise 126 except Exception as e: 127 logging.error('Exception "%s" seen during test', e) 128 raise error.TestFail('Exception "%s" seen during test' % e) 129 finally: 130 self.xmlrpc_delegate.reset_on() 131