• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
6
7from autotest_lib.client.common_lib import error
8from autotest_lib.client.cros.enterprise import enterprise_policy_base
9
10
11class policy_ManagedBookmarks(enterprise_policy_base.EnterprisePolicyTest):
12    """
13    Test effect of ManagedBookmarks policy on Chrome OS behavior.
14
15    This test verifies the behavior of Chrome OS for a range of valid values
16    of the ManagedBookmarks user policy, as defined by three test cases:
17    NotSet_NotShown, SingleBookmark_Shown, and MultiBookmarks_Shown.
18
19    When not set, the policy value is undefined. This induces the default
20    behavior of not showing the managed bookmarks folder, which is equivalent
21    to what is seen by an un-managed user.
22
23    When one or more bookmarks are specified by the policy, then the Managed
24    Bookmarks folder is shown, and the specified bookmarks within it.
25
26    """
27    version = 1
28
29    POLICY_NAME = 'ManagedBookmarks'
30    BOOKMARKS = [{'name': 'Google',
31                   'url': 'https://google.com/'},
32                 {'name': 'YouTube',
33                   'url': 'https://youtube.com/'},
34                 {'name': 'Chromium',
35                   'url': 'https://chromium.org/'}]
36
37    # Dictionary of test case names and policy values.
38    TEST_CASES = {
39        'NotSet_NotShown': None,
40        'SingleBookmark_Shown': BOOKMARKS[:1],
41        'MultipleBookmarks_Shown': BOOKMARKS
42    }
43
44
45    def _get_managed_bookmarks(self):
46        """
47        Return a list of the managed bookmarks.
48
49        @returns displayed_bookmarks: a list containing dictionaries of the
50            managed bookmarks.
51        """
52        # Open Bookmark Manager.
53        tab = self.navigate_to_url('chrome://bookmarks')
54
55        # Nodes are all bookmarks and directories.
56        nodes = tab.EvaluateJavaScript(
57                    'bookmarks.StoreClient[1].getState().nodes')
58
59        displayed_bookmarks = []
60        for node in nodes.values():
61            # The node with parentId 0 is the managed bookmarks directory.
62            if (node.get('unmodifiable') == 'managed' and
63                    node['parentId'] != '0'):
64                bookmark = {'name': node['title'], 'url': node['url']}
65                displayed_bookmarks.append(bookmark)
66
67        return displayed_bookmarks
68
69
70    def _test_managed_bookmarks(self, policy_value):
71        """
72        Verify CrOS enforces ManagedBookmarks policy.
73
74        When ManagedBookmarks is not set, the UI shall not show the managed
75        bookmarks folder nor its contents. When set to one or more bookmarks
76        the UI shows the folder and its contents.
77
78        @param policy_value: policy value for this case.
79
80        @raises error.TestFail: If displayed managed bookmarks does not match
81            the policy value.
82
83        """
84        managed = self._get_managed_bookmarks()
85
86        if policy_value is None:
87            if managed:
88                raise error.TestFail('Managed bookmarks should not be set.')
89        else:
90            if sorted(managed) != sorted(policy_value):
91                raise error.TestFail('Managed bookmarks (%s) '
92                                     'do not match policy value (%s).'
93                                     % (sorted(managed), sorted(policy_value)))
94
95
96    def run_once(self, case):
97        """
98        Setup and run the test configured for the specified test case.
99
100        @param case: Name of the test case to run.
101
102        """
103        case_value = self.TEST_CASES[case]
104        self.setup_case(user_policies={self.POLICY_NAME: case_value})
105        self._test_managed_bookmarks(case_value)
106