1# Copyright 2019 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.
4import logging
5
6from autotest_lib.client.common_lib import error
7from autotest_lib.client.cros.a11y import a11y_test_base
8from autotest_lib.client.cros.enterprise import enterprise_policy_base
9
10
11class policy_AccessibilityTest(
12        enterprise_policy_base.EnterprisePolicyTest,
13        a11y_test_base.a11y_test_base):
14    """
15    Test effect of the following accessibility policies on Chrome OS:
16    HighContrastEnabled, LargeCursorEnabled, VirtualKeyboardEnabled, and
17    ScreenMagnifierType.
18
19    This test will set the policy and value, then call the Accessibility API
20    to see if the feature is enabled or not.
21
22    """
23    version = 1
24
25    _LOOKUP = {'HighContrastEnabled': 'highContrast',
26               'LargeCursorEnabled': 'largeCursor',
27               'VirtualKeyboardEnabled': 'virtualKeyboard',
28               'ScreenMagnifierType': 'screenMagnifier'}
29
30    def _check_settings(self, policy, case):
31        """Call the accessibility API extension and check the policy was set
32        correctly.
33
34        @param policy: Name of the policy set.
35        @param case: Value of the set policy.
36
37        """
38        value_str = 'true' if case else 'false'
39        feature = self._LOOKUP[policy]
40
41        cmd = ('window.__result = null;\n'
42               'chrome.accessibilityFeatures.%s.get({}, function(d) {'
43               'window.__result = d[\'value\']; });' % (feature))
44        self._extension.ExecuteJavaScript(cmd)
45        poll_cmd = 'window.__result == %s;' % value_str
46        pol_status = self._extension.EvaluateJavaScript(poll_cmd)
47
48        if not pol_status:
49            raise error.TestError('{} setting incorrect'.format(policy))
50
51    def run_once(self, policy, case):
52        """
53        Setup and run the test configured for the specified test case.
54
55        @param policy: Name of the policy to set.
56        @param case: Value of the policy to set.
57
58        """
59
60        # Get the accessibility API extension path from the ally_test_base
61        extension_path = self._get_extension_path()
62
63        self.setup_case(user_policies={policy: case},
64                        extension_paths=[extension_path])
65
66        self._extension = self.cr.get_extension(extension_path)
67        self._check_settings(policy, case)
68