1# Copyright 2015 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.cros import enterprise_policy_base
10
11
12class policy_EditBookmarksEnabled(enterprise_policy_base.EnterprisePolicyTest):
13    """Test effect of EditBookmarksEnabled policy on Chrome OS behavior.
14
15    This test verifies the behavior of Chrome OS for all valid values of the
16    EditBookmarksEnabled user policy: True, False, and not set. 'Not set'
17    means that the policy value is undefined. This should induce the default
18    behavior, equivalent to what is seen by an un-managed user.
19
20    When set True or not set, bookmarks can be added, removed, or modified.
21    When set False, bookmarks cannot be added, removed, or modified, though
22    existing bookmarks (if any) are still available.
23
24    """
25    version = 1
26
27    POLICY_NAME = 'EditBookmarksEnabled'
28    BOOKMARKS = '''
29    [
30        {
31          "name": "Google",
32          "url": "https://www.google.com/"
33        },
34        {
35          "name": "CNN",
36          "url": "http://www.cnn.com/"
37        },
38        {
39          "name": "IRS",
40          "url": "http://www.irs.gov/"
41        }
42    ]
43    '''
44    SUPPORTING_POLICIES = {
45        'BookmarkBarEnabled': True,
46        'ManagedBookmarks': BOOKMARKS
47    }
48
49    # Dictionary of named test cases and policy data.
50    TEST_CASES = {
51        'True_Enabled': True,
52        'False_Disabled': False,
53        'NotSet_Enabled': None
54    }
55
56    def _test_edit_bookmarks_enabled(self, policy_value, policies_json):
57        """Verify CrOS enforces EditBookmarksEnabled policy.
58
59        When EditBookmarksEnabled is true or not set, the UI allows the user
60        to add bookmarks. When false, the UI does not allow the user to add
61        bookmarks.
62
63        Warning: When the 'Bookmark Editing' setting on the CPanel User
64        Settings page is set to 'Enable bookmark editing', then the
65        EditBookmarksEnabled policy on the client will be not set. Thus, to
66        verify the 'Enable bookmark editing' choice from a production or
67        staging DMS, use case=NotSet_Enabled.
68
69        @param policy_value: policy value expected on chrome://policy page.
70        @param policies_json: policy JSON data to send to the fake DM server.
71
72        """
73        logging.info('Running _test_edit_bookmarks_enabled(%s, %s)',
74                     policy_value, policies_json)
75        self.setup_case(self.POLICY_NAME, policy_value, policies_json)
76        add_bookmark_is_disabled = self._is_add_bookmark_disabled()
77        if policy_value == 'true' or policy_value == 'null':
78            if add_bookmark_is_disabled:
79                raise error.TestFail('Add Bookmark should be enabled.')
80        else:
81            if not add_bookmark_is_disabled:
82                raise error.TestFail('Add Bookmark should be disabled.')
83
84    def _is_add_bookmark_disabled(self):
85        """Check whether add-new-bookmark-command menu item is disabled.
86
87        @returns: True if add-new-bookmarks-command is disabled.
88
89        """
90        tab = self.cr.browser.tabs.New()
91        tab.Navigate('chrome://bookmarks/#1')
92        tab.WaitForDocumentReadyStateToBeComplete()
93
94        # Wait until list.reload() is defined on bmm page.
95        tab.WaitForJavaScriptExpression(
96            "typeof bmm.list.reload == 'function'", 60)
97        time.sleep(1)  # Allow JS to run after function is defined.
98
99        # Check if add-new-bookmark menu command has disabled property.
100        is_disabled = tab.EvaluateJavaScript(
101            '$("add-new-bookmark-command").disabled;')
102        logging.info('add-new-bookmark-command is disabled: %s', is_disabled)
103        tab.Close()
104        return is_disabled
105
106    def run_test_case(self, case):
107        """Setup and run the test configured for the specified test case.
108
109        Set the expected |policy_value| and |policies_json| data based on the
110        test |case|. If the user specified an expected |value|, then use it to
111        set the |policy_value| and blank out |policies_json|.
112
113        @param case: Name of the test case to run.
114
115        """
116        if self.is_value_given:
117            # If |value| was given by user, then set expected |policy_value|
118            # to the given value, and setup |policies_json| to None.
119            policy_value = self.value
120            policies_json = None
121        else:
122            # Otherwise, set expected |policy_value| and setup |policies_json|
123            # data to the defaults required by the test |case|.
124            policy_value = self.json_string(self.TEST_CASES[case])
125            policy_json = {self.POLICY_NAME: self.TEST_CASES[case]}
126            policies_json = self.SUPPORTING_POLICIES.copy()
127            policies_json.update(policy_json)
128
129        # Run test using values configured for the test case.
130        self._test_edit_bookmarks_enabled(policy_value, policies_json)
131