| OLD | NEW |
| 1 #!/usr/bin/python | 1 #!/usr/bin/python |
| 2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 import logging |
| 7 |
| 6 import pyauto_functional # must come before pyauto. | 8 import pyauto_functional # must come before pyauto. |
| 7 import pyauto | 9 import pyauto |
| 10 from pyauto_errors import JSONInterfaceError |
| 8 | 11 |
| 9 | 12 |
| 10 class PolicyTest(pyauto.PyUITest): | 13 class PolicyTest(pyauto.PyUITest): |
| 11 """Tests that the effects of policies are being enforced as expected.""" | 14 """Tests that the effects of policies are being enforced as expected.""" |
| 12 | 15 |
| 13 def IsBlocked(self, url): | 16 def IsBlocked(self, url): |
| 14 """Returns true if navigating to |url| is blocked.""" | 17 """Returns true if navigating to |url| is blocked.""" |
| 15 self.NavigateToURL(url) | 18 self.NavigateToURL(url) |
| 16 blocked = self.GetActiveTabTitle() == url + ' is not available' | 19 blocked = self.GetActiveTabTitle() == url + ' is not available' |
| 17 ret = self.ExecuteJavascript(""" | 20 ret = self.ExecuteJavascript(""" |
| 18 var hasError = false; | 21 var hasError = false; |
| 19 var error = document.getElementById('errorDetails'); | 22 var error = document.getElementById('errorDetails'); |
| 20 if (error) { | 23 if (error) { |
| 21 hasError = error.textContent.indexOf('Error 138') == 0; | 24 hasError = error.textContent.indexOf('Error 138') == 0; |
| 22 } | 25 } |
| 23 domAutomationController.send(hasError.toString()); | 26 domAutomationController.send(hasError.toString()); |
| 24 """) | 27 """) |
| 25 ret = ret == 'true' | 28 ret = ret == 'true' |
| 26 self.assertEqual(blocked, ret) | 29 self.assertEqual(blocked, ret) |
| 27 return blocked | 30 return blocked |
| 28 | 31 |
| 29 def GetInnerHeight(self): | 32 def IsJavascriptEnabled(self): |
| 30 """Returns the inner height of the content area.""" | 33 try: |
| 31 ret = self.ExecuteJavascript( | 34 ret = self.ExecuteJavascript('domAutomationController.send("done");') |
| 32 'domAutomationController.send(innerHeight.toString());'); | 35 return ret == 'done' |
| 33 return int(ret) | 36 except JSONInterfaceError as e: |
| 37 if 'Javascript execution was blocked' == str(e): |
| 38 logging.debug('The previous failure was expected') |
| 39 return False |
| 40 else: |
| 41 raise e |
| 34 | 42 |
| 35 def testBlacklistPolicy(self): | 43 def testBlacklistPolicy(self): |
| 36 """Tests the URLBlacklist and URLWhitelist policies.""" | 44 """Tests the URLBlacklist and URLWhitelist policies.""" |
| 37 # This is an end to end test and not an exaustive test of the filter format. | 45 # This is an end to end test and not an exaustive test of the filter format. |
| 38 policy = { | 46 policy = { |
| 39 'URLBlacklist': [ | 47 'URLBlacklist': [ |
| 40 'news.google.com', | 48 'news.google.com', |
| 41 'chromium.org', | 49 'chromium.org', |
| 42 ], | 50 ], |
| 43 'URLWhitelist': [ | 51 'URLWhitelist': [ |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 86 | 94 |
| 87 self.assertTrue(self.WaitForBookmarkBarVisibilityChange(False)) | 95 self.assertTrue(self.WaitForBookmarkBarVisibilityChange(False)) |
| 88 self.assertFalse(self.GetBookmarkBarVisibility()) | 96 self.assertFalse(self.GetBookmarkBarVisibility()) |
| 89 self.ApplyAccelerator(pyauto.IDC_SHOW_BOOKMARK_BAR) | 97 self.ApplyAccelerator(pyauto.IDC_SHOW_BOOKMARK_BAR) |
| 90 self.assertTrue(self.WaitForBookmarkBarVisibilityChange(False)) | 98 self.assertTrue(self.WaitForBookmarkBarVisibilityChange(False)) |
| 91 self.assertFalse(self.GetBookmarkBarVisibility()) | 99 self.assertFalse(self.GetBookmarkBarVisibility()) |
| 92 # When disabled by policy, it should never be displayed at all, | 100 # When disabled by policy, it should never be displayed at all, |
| 93 # not even on the NTP. | 101 # not even on the NTP. |
| 94 self.assertFalse(self.IsBookmarkBarDetached()) | 102 self.assertFalse(self.IsBookmarkBarDetached()) |
| 95 | 103 |
| 104 def testJavascriptPolicies(self): |
| 105 """Tests the Javascript policies.""" |
| 106 # The navigation to about:blank after each policy reset is to reset the |
| 107 # content settings state. |
| 108 policy = {} |
| 109 self.SetPolicies(policy) |
| 110 self.assertTrue(self.IsJavascriptEnabled()) |
| 111 self.assertTrue(self.IsMenuCommandEnabled(pyauto.IDC_DEV_TOOLS)) |
| 112 self.assertTrue(self.IsMenuCommandEnabled(pyauto.IDC_DEV_TOOLS_CONSOLE)) |
| 113 |
| 114 policy['DeveloperToolsDisabled'] = True |
| 115 self.SetPolicies(policy) |
| 116 self.assertTrue(self.IsJavascriptEnabled()) |
| 117 self.assertFalse(self.IsMenuCommandEnabled(pyauto.IDC_DEV_TOOLS)) |
| 118 self.assertFalse(self.IsMenuCommandEnabled(pyauto.IDC_DEV_TOOLS_CONSOLE)) |
| 119 |
| 120 policy['DeveloperToolsDisabled'] = False |
| 121 self.SetPolicies(policy) |
| 122 self.assertTrue(self.IsJavascriptEnabled()) |
| 123 self.assertTrue(self.IsMenuCommandEnabled(pyauto.IDC_DEV_TOOLS)) |
| 124 self.assertTrue(self.IsMenuCommandEnabled(pyauto.IDC_DEV_TOOLS_CONSOLE)) |
| 125 |
| 126 # The Developer Tools still work when javascript is disabled. |
| 127 policy['JavascriptEnabled'] = False |
| 128 self.SetPolicies(policy) |
| 129 self.assertFalse(self.IsJavascriptEnabled()) |
| 130 self.assertTrue(self.IsMenuCommandEnabled(pyauto.IDC_DEV_TOOLS)) |
| 131 self.assertTrue(self.IsMenuCommandEnabled(pyauto.IDC_DEV_TOOLS_CONSOLE)) |
| 132 # Javascript is always enabled for internal Chrome pages. |
| 133 self.NavigateToURL('chrome://settings') |
| 134 self.assertTrue(self.IsJavascriptEnabled()) |
| 135 |
| 136 # The Developer Tools can be explicitly disabled. |
| 137 policy['DeveloperToolsDisabled'] = True |
| 138 self.SetPolicies(policy) |
| 139 self.NavigateToURL('about:blank') |
| 140 self.assertFalse(self.IsJavascriptEnabled()) |
| 141 self.assertFalse(self.IsMenuCommandEnabled(pyauto.IDC_DEV_TOOLS)) |
| 142 self.assertFalse(self.IsMenuCommandEnabled(pyauto.IDC_DEV_TOOLS_CONSOLE)) |
| 143 |
| 144 # Javascript can also be disabled with content settings policies. |
| 145 policy = { |
| 146 'DefaultJavaScriptSetting': 2, |
| 147 } |
| 148 self.SetPolicies(policy) |
| 149 self.NavigateToURL('about:blank') |
| 150 self.assertFalse(self.IsJavascriptEnabled()) |
| 151 self.assertTrue(self.IsMenuCommandEnabled(pyauto.IDC_DEV_TOOLS)) |
| 152 self.assertTrue(self.IsMenuCommandEnabled(pyauto.IDC_DEV_TOOLS_CONSOLE)) |
| 153 |
| 154 # The content setting overrides JavascriptEnabled. |
| 155 policy = { |
| 156 'DefaultJavaScriptSetting': 1, |
| 157 'JavascriptEnabled': False, |
| 158 } |
| 159 self.SetPolicies(policy) |
| 160 self.NavigateToURL('about:blank') |
| 161 self.assertTrue(self.IsJavascriptEnabled()) |
| 162 |
| 96 | 163 |
| 97 if __name__ == '__main__': | 164 if __name__ == '__main__': |
| 98 pyauto_functional.Main() | 165 pyauto_functional.Main() |
| OLD | NEW |