Home | History | Annotate | Download | only in accessibility_Sanity
      1 # Copyright (c) 2014 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 os
      6 import logging
      7 import time
      8 
      9 from autotest_lib.client.bin import test
     10 from autotest_lib.client.bin import utils
     11 from autotest_lib.client.common_lib import error
     12 from autotest_lib.client.common_lib.cros import chrome
     13 
     14 
     15 class accessibility_Sanity(test.test):
     16     """Enables then disables all a11y features via accessibilityFeatures API."""
     17     version = 1
     18 
     19     # Features that do not have their own separate tests
     20     _FEATURE_LIST = [
     21         'largeCursor',
     22         'stickyKeys',
     23         'highContrast',
     24         'screenMagnifier',
     25         'autoclick',
     26         'virtualKeyboard'
     27     ]
     28 
     29     # ChromeVox extension id
     30     _CHROMEVOX_ID = 'mndnfokpggljbaajbnioimlmbfngpief'
     31 
     32     def _set_feature(self, feature, value):
     33         """Set given feature to given value using a11y API call.
     34 
     35             @param feature: string of accessibility feature to change
     36             @param value: boolean of expected value
     37         """
     38         value_str = 'true' if value else 'false'
     39         cmd = '''
     40             window.__result = null;
     41             chrome.accessibilityFeatures.%s.set({value: %s});
     42             chrome.accessibilityFeatures.%s.get({}, function(d) {
     43                 window.__result = d[\'value\'];
     44             });
     45         ''' % (feature, value_str, feature)
     46         self._extension.ExecuteJavaScript(cmd)
     47 
     48         poll_cmd = 'window.__result == %s;' % value_str
     49         utils.poll_for_condition(
     50                 lambda: self._extension.EvaluateJavaScript(poll_cmd),
     51                 exception = error.TestError(
     52                         'Timeout while trying to set %s to %s' %
     53                         (feature, value_str)))
     54 
     55     def _confirm_chromevox_indicator(self, value):
     56         """Fail test unless indicator presence is given value on self._tab."""
     57         poll_cmd = '''
     58             document.getElementsByClassName("cvox_indicator_container").length;
     59         '''
     60         def _poll_function():
     61             if value:
     62                 return self._tab.EvaluateJavaScript(poll_cmd) > 0
     63             else:
     64                 return self._tab.EvaluateJavaScript(poll_cmd) == 0
     65 
     66         utils.poll_for_condition(
     67                 _poll_function,
     68                 exception=error.TestError('ChromeVox: "Indicator present" '
     69                                           'was not %s.' % value))
     70 
     71     def _confirm_chromevox_enabled(self, value):
     72         """Fail test unless management.get.enabled is given value."""
     73         cmd = '''
     74             window.__enabled = false;
     75             chrome.management.get(
     76                     '%s', function(r) {window.__enabled = r[\'enabled\']});
     77         ''' % self._CHROMEVOX_ID
     78         self._extension.ExecuteJavaScript(cmd)
     79 
     80         poll_cmd = 'window.__enabled;'
     81         utils.poll_for_condition(
     82                 lambda: self._extension.EvaluateJavaScript(poll_cmd) == value,
     83                 exception=error.TestError(
     84                         'ChromeVox: management.get.enabled not %s.' % value))
     85 
     86     def _check_chromevox(self):
     87         """Run ChromeVox specific checks.
     88 
     89             Check result of management.get.enabled before/after enable and
     90             for presence of indicator before/after disable.
     91         """
     92         # Check for ChromeVox running in the background.
     93         self._confirm_chromevox_enabled(False)
     94         self._set_feature('spokenFeedback', True)
     95         time.sleep(1)
     96         self._confirm_chromevox_enabled(True)
     97 
     98         # Check for presence of ChromeVox indicators.
     99         self._confirm_chromevox_indicator(True)
    100         self._set_feature('spokenFeedback', False)
    101         self._tab.Navigate(self._url) # reload page to remove old indicators
    102         self._confirm_chromevox_indicator(False)
    103 
    104     def run_once(self):
    105         """Entry point of this test."""
    106         extension_path = os.path.join(os.path.dirname(__file__), 'a11y_ext')
    107 
    108         with chrome.Chrome(extension_paths=[extension_path],
    109                            is_component=False) as cr:
    110             self._extension = cr.get_extension(extension_path)
    111 
    112             # Open test page.
    113             self._tab = cr.browser.tabs[0]
    114             cr.browser.platform.SetHTTPServerDirectories(
    115                     os.path.join(os.path.dirname(__file__)))
    116             page_path = os.path.join(self.bindir, 'page.html')
    117             self._url = cr.browser.platform.http_server.UrlOf(page_path)
    118             self._tab.Navigate(self._url)
    119 
    120             # Check specific features.
    121             self._check_chromevox()
    122 
    123             # Enable then disable all other accessibility features.
    124             for value in [True, False]:
    125                 for feature in self._FEATURE_LIST:
    126                     logging.info('Setting %s to %s.', feature, value)
    127                     self._set_feature(feature, value)
    128                     time.sleep(1)
    129