OLD | NEW |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env 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 | 6 import logging |
7 import os | |
7 | 8 |
8 import pyauto_functional # must come before pyauto. | 9 import pyauto_functional # must come before pyauto. |
9 import policy_base | 10 import policy_base |
10 import pyauto | 11 import pyauto |
11 from pyauto_errors import JSONInterfaceError | 12 from pyauto_errors import JSONInterfaceError |
12 | 13 |
13 | 14 |
14 class PolicyTest(policy_base.PolicyTestBase): | 15 class PolicyTest(policy_base.PolicyTestBase): |
15 """Tests that the effects of policies are being enforced as expected.""" | 16 """Tests that the effects of policies are being enforced as expected.""" |
16 | 17 |
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
185 self.assertTrue(self.IsWebGLEnabled()) | 186 self.assertTrue(self.IsWebGLEnabled()) |
186 | 187 |
187 self.SetPolicies({ | 188 self.SetPolicies({ |
188 'Disable3DAPIs': True | 189 'Disable3DAPIs': True |
189 }) | 190 }) |
190 self.assertTrue(self.GetPrefsInfo().Prefs(pyauto.kDisable3DAPIs)) | 191 self.assertTrue(self.GetPrefsInfo().Prefs(pyauto.kDisable3DAPIs)) |
191 # The Disable3DAPIs policy only applies updated values to new renderers. | 192 # The Disable3DAPIs policy only applies updated values to new renderers. |
192 self.RestartRenderer() | 193 self.RestartRenderer() |
193 self.assertFalse(self.IsWebGLEnabled()) | 194 self.assertFalse(self.IsWebGLEnabled()) |
194 | 195 |
196 # Needed for extension tests | |
197 GOOD_CRX_ID = 'ldnnhddmnhbkjipkidpdiheffobcpfmf' | |
198 ADBLOCK_CRX_ID = 'dojnnbeimaimaojcialkkgajdnefpgcn' | |
199 SCREEN_CAPTURE_CRX_ID = 'cpngackimfmofbokmjmljamhdncknpmg' | |
200 | |
201 def _BuildCRXPath(self, crx_file_name): | |
202 """Returns the complete path to a crx_file in the data directory | |
frankf
2012/03/14 00:25:25
period.
| |
203 | |
204 Args: | |
205 crx_file_name: The file name of the extension | |
frankf
2012/03/14 00:25:25
period everywhere!
| |
206 | |
207 Returns: | |
208 The full path to the crx in the data directory | |
209 """ | |
210 return os.path.abspath(os.path.join(self.DataDir(), 'extensions', | |
211 crx_file_name)) | |
212 | |
213 def _AttemptExtensionInstallThatShouldFail(self, crx_file_name): | |
214 """ Attempts to install an extension, throws an exception if it is installed | |
215 | |
216 Args: | |
217 crx_file_name: The file name of the extension | |
frankf
2012/03/14 00:25:25
Raises section
| |
218 """ | |
219 install_failed = True | |
220 try: | |
221 ext_id = self.InstallExtension(self._BuildCRXPath(crx_file_name)) | |
222 install_failed = False | |
223 except JSONInterfaceError, e: | |
frankf
2012/03/14 00:25:25
Please rebase your code. You have to do pyauto_err
| |
224 self.assertEqual(e[0], 'Extension could not be installed', | |
225 msg='The extension failed to install which is expected. ' | |
226 ' However it failed due to an unexpected reason: %s' | |
227 % e[0]) | |
228 self.assertTrue(install_failed, msg='The extension %s did not throw an ' | |
229 'exception when installation was attempted. This most ' | |
230 'likely means it succeeded, which it should not.') | |
231 | |
232 def _CheckForExtensionByID(self, extension_id): | |
233 """ Returns if an extension is installed | |
frankf
2012/03/14 00:25:25
No space before Returns. Also in other places.
| |
234 | |
235 Args: | |
236 extension_id: The id of the extension | |
237 | |
238 Returns: | |
239 True if the extension is installed; False otherwise | |
240 """ | |
241 all_extensions = [extension['id'] for extension in self.GetExtensionsInfo()] | |
242 return extension_id in all_extensions | |
243 | |
244 def _RemoveTestingExtensions(self): | |
245 """ Temporary method that cleans state for extension test. | |
246 | |
247 Currently the tear down method for policy_base does not clear the user | |
248 state. See crosbug.com/27227. | |
249 """ | |
250 if self._CheckForExtensionByID(PolicyTest.SCREEN_CAPTURE_CRX_ID): | |
frankf
2012/03/14 00:25:25
don't need the PolicyTest. prefix
| |
251 self.UninstallExtensionById(PolicyTest.SCREEN_CAPTURE_CRX_ID) | |
252 if self._CheckForExtensionByID(PolicyTest.GOOD_CRX_ID): | |
253 self.UninstallExtensionById(PolicyTest.GOOD_CRX_ID) | |
254 self.NavigateToURL('chrome:extensions') | |
255 if self._CheckForExtensionByID(PolicyTest.ADBLOCK_CRX_ID): | |
256 self.UninstallExtensionById(PolicyTest.ADBLOCK_CRX_ID) | |
257 # There is an issue where if you uninstall and reinstall and extension | |
258 # quickly with self.InstallExtension, the reinstall fails. This is a hack | |
259 # to fix it. Bug coming soon. | |
260 self.NavigateToURL('chrome:extensions') | |
261 | |
262 def testExtensionInstallPopulatedBlacklist(self): | |
263 """Verify blacklisted extensions cannot be installed.""" | |
264 # TODO: Remove this when crosbug.com/27227 is fixed. | |
265 self._RemoveTestingExtensions() | |
266 # Blacklist good.crx | |
267 self.SetPolicies({ | |
268 'ExtensionInstallBlacklist': [PolicyTest.GOOD_CRX_ID] | |
269 }) | |
270 self._AttemptExtensionInstallThatShouldFail('good.crx') | |
271 ext_id = self.InstallExtension(self._BuildCRXPath('adblock.crx')) | |
frankf
2012/03/14 00:25:25
Please factor this to AttemptExtensionInstallThatS
| |
272 # Check adblock is installed. | |
273 self.assertTrue(self._CheckForExtensionByID(ext_id), | |
274 msg='The adblock.crx extension was not install even though ' | |
275 'it is not on the Blacklist.') | |
276 | |
277 def testExtensionInstallFailWithGlobalBlacklist(self): | |
278 """Verify no extensions can be installed when all are blacklisted.""" | |
279 # TODO: Remove this when crosbug.com/27227 is fixed. | |
280 self._RemoveTestingExtensions() | |
281 # Block installs of all extensions | |
282 self.SetPolicies({ | |
283 'ExtensionInstallBlacklist': ['*'] | |
284 }) | |
285 self._AttemptExtensionInstallThatShouldFail('good.crx') | |
286 self._AttemptExtensionInstallThatShouldFail('adblock.crx') | |
287 | |
288 def testExtensionInstallWithGlobalBlacklistAndWhitelistedExtension(self): | |
289 """Verify whitelisted extension is installed when all are blacklisted.""" | |
290 # TODO: Remove this when crosbug.com/27227 is fixed. | |
291 self._RemoveTestingExtensions() | |
292 # Block installs of all extensions, but whitelist adblock.crx | |
293 self.SetPolicies({ | |
294 'ExtensionInstallBlacklist': ['*'], | |
295 'ExtensionInstallWhitelist': [PolicyTest.ADBLOCK_CRX_ID] | |
296 }) | |
297 self._AttemptExtensionInstallThatShouldFail('good.crx') | |
298 ext_id = self.InstallExtension(self._BuildCRXPath('adblock.crx')) | |
299 # Check good.crx is not installed | |
300 self.assertFalse(self._CheckForExtensionByID(PolicyTest.GOOD_CRX_ID), | |
301 msg='The good.crx extension was installed even though no ' | |
302 'extension installs are permitted except whitelisted ones,' | |
303 ' which this is not.') | |
304 # Check adblock is installed | |
305 self.assertTrue(self._CheckForExtensionByID(ext_id), | |
306 msg='The adblock.crx extension was not install even though' | |
307 ' it is on the whitelist.') | |
308 | |
309 # TODO: Enable this test once we figure out why it isn't downloading the | |
310 # extension | |
311 def testExtensionInstallFromForceList(self): | |
312 """Verify force install extensions are installed.""" | |
313 # TODO: Remove this when crosbug.com/27227 is fixed. | |
314 self._RemoveTestingExtensions() | |
315 # Force an extension download from the webstore. | |
316 self.SetPolicies({ | |
317 'ExtensionInstallForcelist': [PolicyTest.SCREEN_CAPTURE_CRX_ID], | |
318 }) | |
319 # Give the system 30 seconds to go get this extension. | |
frankf
2012/03/14 00:25:25
can you explain the logic for waiting here.
| |
320 self.assertTrue(self.WaitUntil(lambda: | |
321 self._CheckForExtensionByID(PolicyTest.SCREEN_CAPTURE_CRX_ID), | |
322 expect_retval=True), | |
323 msg='The force install extension was never installed.') | |
324 | |
195 | 325 |
196 if __name__ == '__main__': | 326 if __name__ == '__main__': |
197 pyauto_functional.Main() | 327 pyauto_functional.Main() |
OLD | NEW |