1# Copyright 2016 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
6
7from autotest_lib.client.common_lib import error
8from autotest_lib.client.cros.enterprise import enterprise_policy_base
9
10
11class policy_SpellCheckServiceEnabled(
12        enterprise_policy_base.EnterprisePolicyTest):
13    """
14    Test effect of SpellCheckServiceEnabled policy on Chrome OS behavior.
15
16    This test verifies the behavior of Chrome OS for all valid values of the
17    SpellCheckServiceEnabled user policy: True, False, and Not set. 'Not set'
18    indicates no value, and will induce the default behavior that is seen by
19    an unmanaged user: unchecked and user-editable.
20
21    When True or Not set, spelling suggestiongs are given. When False, spelling
22    suggestions are not given. When set either True or False, the setting is
23    disabled, so users cannot change or override the setting. When not set
24    users can change the setting.
25
26    """
27    version = 1
28
29    POLICY_NAME = 'SpellCheckServiceEnabled'
30    STARTUP_URLS = ['chrome://policy']
31    SUPPORTING_POLICIES = {
32        'BookmarkBarEnabled': True,
33        'RestoreOnStartupURLs': STARTUP_URLS,
34        'RestoreOnStartup': 4
35    }
36    TEST_CASES = {
37        'True_Enable': True,
38        'False_Disable': False,
39        'NotSet_Editable': None
40    }
41
42
43    def _test_spelling_suggestions_enabled(self, policy_value):
44        """
45        Verify CrOS enforces the SpellCheckServiceEnabled policy.
46
47        @param policy_value: policy value expected.
48
49        """
50        setting_pref = 'spellcheck.use_spelling_service'
51        properties = self._get_settings_checkbox_properties(setting_pref)
52        setting_label = properties[self.SETTING_LABEL]
53        setting_is_checked = properties[self.SETTING_CHECKED]
54        setting_is_disabled = properties[self.SETTING_DISABLED]
55        logging.info('Check box "%s" status: checked=%s, disabled=%s',
56                     setting_label, setting_is_checked, setting_is_disabled)
57
58        # Setting checked if policy is True, unchecked if False.
59        if policy_value == True and not setting_is_checked:
60            raise error.TestFail('Spelling Suggestions should be checked.')
61        if policy_value == False and setting_is_checked:
62            raise error.TestFail('Spelling Suggestions should be unchecked.')
63
64        # Setting is enabled if policy is Not set, disabled if True or False.
65        if policy_value == None:
66            if setting_is_disabled:
67                raise error.TestFail('Spelling Suggestions should '
68                                     'be editable.')
69        else:
70            if not setting_is_disabled:
71                raise error.TestFail('Spelling Suggestions should not '
72                                     'be editable.')
73
74
75    def run_test_case(self, case):
76        """
77        Setup and run the test configured for the specified test case.
78
79        @param case: Name of the test case to run.
80
81        """
82        case_value = self.TEST_CASES[case]
83        self.setup_case(self.POLICY_NAME, case_value, self.SUPPORTING_POLICIES)
84        self._test_spelling_suggestions_enabled(case_value)
85