Home | History | Annotate | Download | only in bluetooth_TurnOnOffUI
      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 
      5 import logging
      6 import os
      7 import time
      8 
      9 from autotest_lib.client.common_lib import error
     10 from autotest_lib.client.common_lib.cros import chromedriver
     11 from autotest_lib.client.cros.graphics import graphics_utils
     12 from autotest_lib.client.cros.bluetooth import bluetooth_device_xmlrpc_server
     13 
     14 
     15 class bluetooth_TurnOnOffUI(graphics_utils.GraphicsTest):
     16 
     17     """Go to settings and turn on BT On/Off"""
     18     version = 1
     19     SETTINGS_UI_TAG = "settings-ui"
     20     SHADOW_ROOT_JS = 'return arguments[0].shadowRoot'
     21     SETTINGS_MAIN_CSS = 'settings-main#main'
     22     SETTINGS_BASE_PAGE_CSS = 'settings-basic-page'
     23     SETTINGS_BT_PAGE = "settings-bluetooth-page"
     24     SETTINGS_URL = "chrome://settings"
     25     ENABLE_BT_CSS = 'cr-toggle#enableBluetooth'
     26     DELAY_BW_TOGGLE_ON_OFF = 5
     27 
     28     def initialize(self):
     29         """Autotest initialize function"""
     30         self.xmlrpc_delegate = bluetooth_device_xmlrpc_server.BluetoothDeviceXmlRpcDelegate()
     31         super(bluetooth_TurnOnOffUI, self).initialize(raise_error_on_hang=True)
     32 
     33     def cleanup(self):
     34         """Autotest cleanup function"""
     35         if self._GSC:
     36             keyvals = self._GSC.get_memory_difference_keyvals()
     37             for key, val in keyvals.iteritems():
     38                 self.output_perf_value(
     39                     description=key,
     40                     value=val,
     41                     units='bytes',
     42                     higher_is_better=False)
     43             self.write_perf_keyval(keyvals)
     44         super(bluetooth_TurnOnOffUI, self).cleanup()
     45         # If test fails then script will collect the screen shot to know at
     46         # which instance failure occurred.
     47         if not self.success:
     48             graphics_utils.take_screenshot(os.path.join(self.debugdir),
     49                                            "chrome")
     50 
     51     def open_settings(self, driver):
     52         """Open Settings
     53 
     54         @param driver: chrome driver object
     55         @return: Returns settings page web element
     56         """
     57         logging.info("Opening settings.")
     58         driver.get(self.SETTINGS_URL)
     59         logging.debug("Current tab is %s", driver.title)
     60         return self.settings_frame(driver)
     61 
     62     def settings_frame(self, driver):
     63         """Finds basic settings page web element
     64 
     65         @param driver: chrome driver object
     66         @return: Returns settings page web element
     67         """
     68         settings_ui = driver.find_element_by_tag_name(self.SETTINGS_UI_TAG)
     69         settings_ui_shroot = driver.execute_script(self.SHADOW_ROOT_JS,
     70                                                    settings_ui)
     71         settings_main = settings_ui_shroot.find_element_by_css_selector(
     72             self.SETTINGS_MAIN_CSS)
     73         settings_basic_page = driver.execute_script(self.SHADOW_ROOT_JS,
     74                                                     settings_main). \
     75             find_element_by_css_selector(self.SETTINGS_BASE_PAGE_CSS)
     76         return driver.execute_script(self.SHADOW_ROOT_JS, settings_basic_page)
     77 
     78     def bluetooth_page(self, driver):
     79         """BT settings page
     80 
     81         @param driver: chrome driver object
     82         @return: Bluetooth page web element
     83         """
     84         settings = self.open_settings(driver)
     85         basic_page = settings.find_element_by_id('basicPage')
     86         bt_device_page = basic_page.find_element_by_tag_name(
     87             self.SETTINGS_BT_PAGE)
     88         driver.execute_script('return arguments[0].scrollIntoView()',
     89                               bt_device_page)
     90         return driver.execute_script(self.SHADOW_ROOT_JS, bt_device_page)
     91 
     92     def is_bluetooth_enabled(self):
     93         """Returns True if bluetoothd is powered on, otherwise False"""
     94 
     95         return self.xmlrpc_delegate._is_powered_on()
     96 
     97     def click_bluetooth_button(self, driver, bt_page):
     98         """Click on the bluetooth on/off button using javascript
     99 
    100         @param: driver:Chromedriver object
    101         @param: bt_page:Bluetooth page web element
    102         """
    103         button = bt_page.find_element_by_css_selector(self.ENABLE_BT_CSS)
    104         driver.execute_script("arguments[0].click();", button)
    105 
    106     def turn_on_bluetooth(self, bt_page, driver):
    107         """Turn on BT through UI
    108 
    109         @param: bt_page:Bluetooth page web element
    110         """
    111         if self.is_bluetooth_enabled():
    112             logging.info('Bluetooth is turned on already..')
    113         else:
    114             self.click_bluetooth_button(driver, bt_page)
    115             time.sleep(self.DELAY_BW_TOGGLE_ON_OFF)
    116             if self.is_bluetooth_enabled():
    117                 logging.info('Turned on BT successfully..')
    118             else:
    119                 raise error.TestFail('BT is not turned on..')
    120 
    121     def turn_off_bluetooth(self, bt_page, driver):
    122         """Turn off BT through UI
    123 
    124         @param: bt_page:Bluetooth page web element
    125         """
    126         if not self.is_bluetooth_enabled():
    127             logging.info('Bluetooth is turned off already within time.')
    128         else:
    129             self.click_bluetooth_button(driver, bt_page)
    130             time.sleep(self.DELAY_BW_TOGGLE_ON_OFF)
    131             if not self.is_bluetooth_enabled():
    132                 logging.info('Turned off BT successfully..')
    133             else:
    134                 raise error.TestFail('Bluetooth is not turned off within time')
    135 
    136     def run_once(self, iteration_count=3):
    137         """Turn on/off bluetooth through UI
    138 
    139         @param iteration_count: Number of iterations to toggle on/off
    140 
    141         """
    142         self.success = False
    143         with chromedriver.chromedriver() as chromedriver_instance:
    144             driver = chromedriver_instance.driver
    145             bt_page = self.bluetooth_page(driver)
    146             self.turn_off_bluetooth(bt_page, driver)
    147             for iteration in xrange(1, iteration_count + 1):
    148                 logging.info("**** Turn on/off BT iteration: %d ****",
    149                              iteration)
    150                 self.turn_on_bluetooth(bt_page, driver)
    151                 self.turn_off_bluetooth(bt_page, driver)
    152         self.success = True
    153