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 5 import time 6 7 from autotest_lib.client.common_lib import error 8 from autotest_lib.client.cros.enterprise import enterprise_policy_base 9 10 11 class policy_ManagedBookmarks(enterprise_policy_base.EnterprisePolicyTest): 12 """Test effect of ManagedBookmarks policy on Chrome OS behavior. 13 14 This test verifies the behavior of Chrome OS for a range of valid values 15 of the ManagedBookmarks user policy, as defined by three test cases: 16 NotSet_NotShown, SingleBookmark_Shown, and MultiBookmarks_Shown. 17 18 When not set, the policy value is undefined. This induces the default 19 behavior of not showing the managed bookmarks folder, which is equivalent 20 to what is seen by an un-managed user. 21 22 When one or more bookmarks are specified by the policy, then the Managed 23 Bookmarks folder is shown, and the specified bookmarks within it. 24 25 """ 26 version = 1 27 28 POLICY_NAME = 'ManagedBookmarks' 29 SINGLE_BOOKMARK = [{'name': 'Google', 30 'url': 'https://www.google.com/'}] 31 32 MULTI_BOOKMARK = [{'name': 'Google', 33 'url': 'https://www.google.com/'}, 34 {'name': 'CNN', 35 'url': 'http://www.cnn.com/'}, 36 {'name': 'IRS', 37 'url': 'http://www.irs.gov/'}] 38 39 SUPPORTING_POLICIES = { 40 'BookmarkBarEnabled': True 41 } 42 43 # Dictionary of test case names and policy values. 44 TEST_CASES = { 45 'NotSet_NotShown': None, 46 'SingleBookmark_Shown': SINGLE_BOOKMARK, 47 'MultipleBookmarks_Shown': MULTI_BOOKMARK 48 } 49 50 def _test_managed_bookmarks(self, policy_value): 51 """Verify CrOS enforces ManagedBookmarks policy. 52 53 When ManagedBookmarks is not set, the UI shall not show the managed 54 bookmarks folder nor its contents. When set to one or more bookmarks 55 the UI shows the folder and its contents. 56 57 @param policy_value: policy value for this case. 58 59 """ 60 managed_bookmarks_are_shown = self._are_bookmarks_shown(policy_value) 61 if policy_value is None: 62 if managed_bookmarks_are_shown: 63 raise error.TestFail('Managed Bookmarks should be hidden.') 64 else: 65 if not managed_bookmarks_are_shown: 66 raise error.TestFail('Managed Bookmarks should be shown.') 67 68 def _are_bookmarks_shown(self, policy_bookmarks): 69 """Check whether managed bookmarks are shown in the UI. 70 71 @param policy_bookmarks: bookmarks expected. 72 @returns: True if the managed bookmarks are shown. 73 74 """ 75 # Extract dictionary of folders shown in bookmark tree. 76 tab = self._open_boomark_manager_to_folder(0) 77 cmd = 'document.getElementsByClassName("tree-item");' 78 tree_items = self.get_elements_from_page(tab, cmd) 79 80 # Scan bookmark tree for a folder with the domain-name in title. 81 domain_name = self.username.split('@')[1] 82 folder_title = domain_name + ' bookmarks' 83 for bookmark_element in tree_items.itervalues(): 84 bookmark_node = bookmark_element['bookmarkNode'] 85 bookmark_title = bookmark_node['title'] 86 if bookmark_title == folder_title: 87 folder_id = bookmark_node['id'].encode('ascii', 'ignore') 88 break 89 else: 90 tab.Close() 91 return False 92 tab.Close() 93 94 # Extract list of bookmarks shown in bookmark list-pane. 95 tab = self._open_boomark_manager_to_folder(folder_id) 96 cmd = ''' 97 var bookmarks = []; 98 var listPane = document.getElementById("list-pane"); 99 var labels = listPane.getElementsByClassName("label"); 100 for (var i = 0; i < labels.length; i++) { 101 bookmarks.push(labels[i].textContent); 102 } 103 bookmarks; 104 ''' 105 bookmark_items = self.get_elements_from_page(tab, cmd) 106 tab.Close() 107 108 # Get list of expected bookmarks as set by policy. 109 bookmarks_expected = None 110 if policy_bookmarks: 111 bookmarks_expected = [bmk['name'] for bmk in policy_bookmarks] 112 113 # Compare bookmarks shown vs expected. 114 if bookmark_items != bookmarks_expected: 115 raise error.TestFail('Bookmarks shown are not correct: %s ' 116 '(expected: %s)' % 117 (bookmark_items, bookmarks_expected)) 118 return True 119 120 def _open_boomark_manager_to_folder(self, folder_number): 121 """Open bookmark manager page and select specified folder. 122 123 @param folder_number: folder to select when opening page. 124 @returns: tab loaded with bookmark manager page. 125 126 """ 127 # Open Bookmark Manager with specified folder selected. 128 bmp_url = ('chrome://bookmarks/#%s' % folder_number) 129 tab = self.navigate_to_url(bmp_url) 130 131 # Wait until list.reload() is defined on page. 132 tab.WaitForJavaScriptCondition( 133 "typeof bmm.list.reload == 'function'", timeout=60) 134 time.sleep(1) # Allow JS to run after function is defined. 135 return tab 136 137 def run_test_case(self, case): 138 """Setup and run the test configured for the specified test case. 139 140 @param case: Name of the test case to run. 141 142 """ 143 case_value = self.TEST_CASES[case] 144 self.setup_case(self.POLICY_NAME, case_value, self.SUPPORTING_POLICIES) 145 self._test_managed_bookmarks(case_value) 146