| OLD | NEW |
| (Empty) |
| 1 #!/usr/bin/env python | |
| 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 3 # Use of this source code is governed by a BSD-style license that can be | |
| 4 # found in the LICENSE file. | |
| 5 | |
| 6 # If this test is failing, please check these steps. | |
| 7 # | |
| 8 # - You introduced a new policy: | |
| 9 # Cool! Edit |policies| in policy_test_cases.py and add an entry for it. | |
| 10 # See the comment above it for the format. | |
| 11 # | |
| 12 # - This test fails after your changes: | |
| 13 # Did you change the chrome://settings WebUI and edited "pref" attributes? | |
| 14 # Make sure any preferences that can be managed by policy have a "pref" | |
| 15 # attribute. Ping joaodasilva@chromium.org or bauerb@chromium.org. | |
| 16 # | |
| 17 # - This test fails for other reasons, or is flaky: | |
| 18 # Ping joaodasilva@chromium.org, pastarmovj@chromium.org or | |
| 19 # mnissler@chromium.org. | |
| 20 | |
| 21 import logging | |
| 22 | |
| 23 import pyauto_functional # must come before pyauto. | |
| 24 import policy_base | |
| 25 import pyauto | |
| 26 | |
| 27 from policy_test_cases import PolicyPrefsTestCases | |
| 28 | |
| 29 | |
| 30 class PolicyPrefsUITest(policy_base.PolicyTestBase): | |
| 31 """Tests user policies and their impact on the prefs UI.""" | |
| 32 | |
| 33 settings_pages = [ | |
| 34 'chrome://settings-frame', | |
| 35 'chrome://settings-frame/searchEngines', | |
| 36 'chrome://settings-frame/passwords', | |
| 37 'chrome://settings-frame/autofill', | |
| 38 'chrome://settings-frame/content', | |
| 39 'chrome://settings-frame/homePageOverlay', | |
| 40 'chrome://settings-frame/languages', | |
| 41 ] | |
| 42 if pyauto.PyUITest.IsChromeOS(): | |
| 43 settings_pages += [ | |
| 44 'chrome://settings-frame/accounts', | |
| 45 ] | |
| 46 | |
| 47 def setUp(self): | |
| 48 policy_base.PolicyTestBase.setUp(self) | |
| 49 if self.IsChromeOS(): | |
| 50 self.LoginWithTestAccount() | |
| 51 | |
| 52 def IsAnyBannerVisible(self): | |
| 53 """Returns true if any banner (e.g. for managed prefs) is visible.""" | |
| 54 ret = self.ExecuteJavascript(""" | |
| 55 var visible = false; | |
| 56 var banners = document.querySelectorAll('.page-banner'); | |
| 57 for (var i=0; i<banners.length; i++) { | |
| 58 if (banners[i].parentElement.id == 'templates') | |
| 59 continue; | |
| 60 | |
| 61 if (window.getComputedStyle(banners[i]).display != 'none') | |
| 62 visible = true; | |
| 63 } | |
| 64 domAutomationController.send(visible.toString()); | |
| 65 """) | |
| 66 return ret == 'true' | |
| 67 | |
| 68 def testNoUserPoliciesNoBanner(self): | |
| 69 """Verifies the banner isn't present when no user policies are in place.""" | |
| 70 | |
| 71 self.SetUserPolicy({}) | |
| 72 for page in PolicyPrefsUITest.settings_pages: | |
| 73 self.NavigateToURL(page) | |
| 74 self.assertFalse(self.IsAnyBannerVisible(), msg= | |
| 75 'Unexpected banner in %s.\n' | |
| 76 'Please check that chrome/test/functional/policy_prefs_ui.py has an ' | |
| 77 'entry for any new policies introduced.' % page) | |
| 78 | |
| 79 def RunUserPoliciesShowBanner(self, include_expected, include_unexpected): | |
| 80 """Tests all the user policies on each settings page. | |
| 81 | |
| 82 If |include_expected|, pages where the banner is expected will be verified. | |
| 83 If |include_unexpected|, pages where the banner should not appear will also | |
| 84 be verified. This can take some time. | |
| 85 """ | |
| 86 os = self.GetPlatform() | |
| 87 all_policies = self.GetPolicyDefinitionList() | |
| 88 | |
| 89 for policy, policy_test in PolicyPrefsTestCases.policies.iteritems(): | |
| 90 # Skip device policies | |
| 91 if policy in all_policies and all_policies[policy][1]: | |
| 92 continue | |
| 93 if os not in policy_test[PolicyPrefsTestCases.INDEX_OS]: | |
| 94 continue | |
| 95 expected_pages = [PolicyPrefsUITest.settings_pages[n] | |
| 96 for n in policy_test[PolicyPrefsTestCases.INDEX_PAGES]] | |
| 97 did_test = False | |
| 98 for page in PolicyPrefsUITest.settings_pages: | |
| 99 expected = page in expected_pages | |
| 100 if expected and not include_expected: | |
| 101 continue | |
| 102 if not expected and not include_unexpected: | |
| 103 continue | |
| 104 if not did_test: | |
| 105 did_test = True | |
| 106 policy_dict = { | |
| 107 policy: policy_test[PolicyPrefsTestCases.INDEX_VALUE] | |
| 108 } | |
| 109 self.SetUserPolicy(policy_dict) | |
| 110 self.NavigateToURL(page) | |
| 111 self.assertEqual(expected, self.IsAnyBannerVisible(), msg= | |
| 112 'Banner was%sexpected in %s, but it was%svisible.\n' | |
| 113 'The policy tested was "%s".\n' | |
| 114 'Please check that chrome/test/functional/policy_prefs_ui.py has ' | |
| 115 'an entry for any new policies introduced.' % | |
| 116 (expected and ' ' or ' NOT ', page, expected and ' NOT ' or ' ', | |
| 117 policy)) | |
| 118 if did_test: | |
| 119 logging.debug('Policy passed: %s' % policy) | |
| 120 | |
| 121 def testUserPoliciesShowBanner(self): | |
| 122 """Verifies the banner is shown when a user pref is managed by policy.""" | |
| 123 self.RunUserPoliciesShowBanner(True, False) | |
| 124 | |
| 125 # This test is disabled by default because it takes a very long time, | |
| 126 # for little benefit. | |
| 127 def UserPoliciesDontShowBanner(self): | |
| 128 """Verifies that the banner is NOT shown on unrelated pages.""" | |
| 129 self.RunUserPoliciesShowBanner(False, True) | |
| 130 | |
| 131 def testFailOnUserPoliciesNotTested(self): | |
| 132 """Verifies that all existing user policies are covered. | |
| 133 | |
| 134 Fails for all user policies listed in GetPolicyDefinitionList() that aren't | |
| 135 listed in |PolicyPrefsUITest.policies|, and thus are not tested by | |
| 136 |testUserPoliciesShowBanner|. | |
| 137 """ | |
| 138 | |
| 139 all_policies = self.GetPolicyDefinitionList() | |
| 140 for policy in all_policies: | |
| 141 # Skip device policies | |
| 142 if all_policies[policy][1]: | |
| 143 continue | |
| 144 self.assertTrue(policy in PolicyPrefsTestCases.policies, msg= | |
| 145 'Policy "%s" does not have a test in ' | |
| 146 'chrome/test/functional/policy_prefs_ui.py.\n' | |
| 147 'Please edit the file and add an entry for this policy.' % policy) | |
| 148 test_type = type(PolicyPrefsTestCases.policies[policy] | |
| 149 [PolicyPrefsTestCases.INDEX_VALUE]).__name__ | |
| 150 expected_type = all_policies[policy][0] | |
| 151 self.assertEqual(expected_type, test_type, msg= | |
| 152 'Policy "%s" has type "%s" but the test value has type "%s".' % | |
| 153 (policy, expected_type, test_type)) | |
| 154 | |
| 155 def testToggleUserPolicyTogglesBanner(self): | |
| 156 """Verifies that toggling a user policy toggles the banner's visibility.""" | |
| 157 # |policy| just has to be any user policy that has at least a settings page | |
| 158 # that displays the banner when the policy is set. | |
| 159 policy = 'ShowHomeButton' | |
| 160 | |
| 161 policy_test = PolicyPrefsTestCases.policies[policy] | |
| 162 page = PolicyPrefsUITest.settings_pages[ | |
| 163 policy_test[PolicyPrefsTestCases.INDEX_PAGES][0]] | |
| 164 policy_dict = { | |
| 165 policy: policy_test[PolicyPrefsTestCases.INDEX_VALUE] | |
| 166 } | |
| 167 | |
| 168 self.SetUserPolicy({}) | |
| 169 self.NavigateToURL(page) | |
| 170 self.assertFalse(self.IsAnyBannerVisible()) | |
| 171 | |
| 172 self.SetUserPolicy(policy_dict) | |
| 173 self.assertTrue(self.IsAnyBannerVisible()) | |
| 174 | |
| 175 self.SetUserPolicy({}) | |
| 176 self.assertFalse(self.IsAnyBannerVisible()) | |
| 177 | |
| 178 | |
| 179 if __name__ == '__main__': | |
| 180 pyauto_functional.Main() | |
| OLD | NEW |