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 import os |
8 | 8 |
9 import pyauto_functional # must come before pyauto. | 9 import pyauto_functional # must come before pyauto. |
10 import policy_base | 10 import policy_base |
(...skipping 10 matching lines...) Expand all Loading... |
21 Args: | 21 Args: |
22 key: The preference key that you want to modify | 22 key: The preference key that you want to modify |
23 defaultval: Default value of the preference that we are trying to modify | 23 defaultval: Default value of the preference that we are trying to modify |
24 newval: New value that we are trying to set | 24 newval: New value that we are trying to set |
25 """ | 25 """ |
26 # Check if the default value of the preference is set as expected. | 26 # Check if the default value of the preference is set as expected. |
27 self.assertEqual( | 27 self.assertEqual( |
28 self.GetPrefsInfo().Prefs(key), defaultval, | 28 self.GetPrefsInfo().Prefs(key), defaultval, |
29 msg='Expected preference value "%s" does not match actual value "%s".' % | 29 msg='Expected preference value "%s" does not match actual value "%s".' % |
30 (defaultval, self.GetPrefsInfo().Prefs(key))) | 30 (defaultval, self.GetPrefsInfo().Prefs(key))) |
31 self.assertRaises(pyauto.JSONInterfaceError, | 31 self.assertRaises(pyauto_errors.JSONInterfaceError, |
32 lambda: self.SetPrefs(key, newval)) | 32 lambda: self.SetPrefs(key, newval)) |
33 | 33 |
34 # TODO(frankf): Move tests dependending on this to plugins.py. | 34 # TODO(frankf): Move tests dependending on this to plugins.py. |
35 def _GetPluginPID(self, plugin_name): | 35 def _GetPluginPID(self, plugin_name): |
36 """Fetch the pid of the plugin process with name |plugin_name|.""" | 36 """Fetch the pid of the plugin process with name |plugin_name|.""" |
37 child_processes = self.GetBrowserInfo()['child_processes'] | 37 child_processes = self.GetBrowserInfo()['child_processes'] |
38 plugin_type = 'Plug-in' | 38 plugin_type = 'Plug-in' |
39 for x in child_processes: | 39 for x in child_processes: |
40 if x['type'] == plugin_type and plugin_name in x['name']: | 40 if x['type'] == plugin_type and plugin_name in x['name']: |
41 return x['pid'] | 41 return x['pid'] |
(...skipping 13 matching lines...) Expand all Loading... |
55 """) | 55 """) |
56 ret = ret == 'true' | 56 ret = ret == 'true' |
57 self.assertEqual(blocked, ret) | 57 self.assertEqual(blocked, ret) |
58 return blocked | 58 return blocked |
59 | 59 |
60 def _IsJavascriptEnabled(self): | 60 def _IsJavascriptEnabled(self): |
61 """Returns true if Javascript is enabled, false otherwise.""" | 61 """Returns true if Javascript is enabled, false otherwise.""" |
62 try: | 62 try: |
63 ret = self.ExecuteJavascript('domAutomationController.send("done");') | 63 ret = self.ExecuteJavascript('domAutomationController.send("done");') |
64 return ret == 'done' | 64 return ret == 'done' |
65 except pyauto.JSONInterfaceError as e: | 65 except pyauto_errors.JSONInterfaceError as e: |
66 if 'Javascript execution was blocked' == str(e): | 66 if 'Javascript execution was blocked' == str(e): |
67 logging.debug('The previous failure was expected') | 67 logging.debug('The previous failure was expected') |
68 return False | 68 return False |
69 else: | 69 else: |
70 raise e | 70 raise e |
71 | 71 |
72 def _IsWebGLEnabled(self): | 72 def _IsWebGLEnabled(self): |
73 """Returns true if WebGL is enabled, false otherwise.""" | 73 """Returns true if WebGL is enabled, false otherwise.""" |
74 ret = self.GetDOMValue(""" | 74 ret = self.GetDOMValue(""" |
75 document.createElement('canvas'). | 75 document.createElement('canvas'). |
(...skipping 478 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
554 'DefaultSearchProviderEnabled': False, | 554 'DefaultSearchProviderEnabled': False, |
555 } | 555 } |
556 self.SetPolicies(policy) | 556 self.SetPolicies(policy) |
557 self._VerifyPrefIsLocked(pyauto.kDefaultSearchProviderEnabled, False, | 557 self._VerifyPrefIsLocked(pyauto.kDefaultSearchProviderEnabled, False, |
558 True) | 558 True) |
559 self.SetOmniboxText('deli') | 559 self.SetOmniboxText('deli') |
560 self.WaitUntilOmniboxQueryDone() | 560 self.WaitUntilOmniboxQueryDone() |
561 self.assertRaises(pyauto.JSONInterfaceError, | 561 self.assertRaises(pyauto.JSONInterfaceError, |
562 lambda: self.OmniboxAcceptInput()) | 562 lambda: self.OmniboxAcceptInput()) |
563 | 563 |
| 564 # Needed for extension tests |
| 565 _GOOD_CRX_ID = 'ldnnhddmnhbkjipkidpdiheffobcpfmf' |
| 566 _ADBLOCK_CRX_ID = 'dojnnbeimaimaojcialkkgajdnefpgcn' |
| 567 _SCREEN_CAPTURE_CRX_ID = 'cpngackimfmofbokmjmljamhdncknpmg' |
| 568 |
| 569 def _BuildCRXPath(self, crx_file_name): |
| 570 """Returns the complete path to a crx_file in the data directory. |
| 571 |
| 572 Args: |
| 573 crx_file_name: The file name of the extension |
| 574 |
| 575 Returns: |
| 576 The full path to the crx in the data directory |
| 577 """ |
| 578 return os.path.abspath(os.path.join(self.DataDir(), 'extensions', |
| 579 crx_file_name)) |
| 580 |
| 581 def _AttemptExtensionInstallThatShouldFail(self, crx_file_name): |
| 582 """Attempts to install an extension, raises an exception if installed. |
| 583 |
| 584 Args: |
| 585 crx_file_name: The file name of the extension |
| 586 """ |
| 587 install_failed = True |
| 588 try: |
| 589 self.InstallExtension(self._BuildCRXPath(crx_file_name)) |
| 590 install_failed = False |
| 591 except pyauto_errors.JSONInterfaceError as e: |
| 592 self.assertEqual(e[0], 'Extension could not be installed', |
| 593 msg='The extension failed to install which is expected. ' |
| 594 ' However it failed due to an unexpected reason: %s' |
| 595 % e[0]) |
| 596 self.assertTrue(install_failed, msg='The extension %s did not throw an ' |
| 597 'exception when installation was attempted. This most ' |
| 598 'likely means it succeeded, which it should not.') |
| 599 |
| 600 def _AttemptExtensionInstallThatShouldPass(self, crx_file_name): |
| 601 """Attempts to install an extension, raises an exception if not installed. |
| 602 |
| 603 Args: |
| 604 crx_file_name: The file name of the extension |
| 605 """ |
| 606 ext_id = self.InstallExtension(self._BuildCRXPath(crx_file_name)) |
| 607 self.assertTrue(self._CheckForExtensionByID(ext_id), |
| 608 msg='The %s extension was not install even though ' |
| 609 'it should have been.' % crx_file_name) |
| 610 |
| 611 def _CheckForExtensionByID(self, extension_id): |
| 612 """Returns if an extension is installed. |
| 613 |
| 614 Args: |
| 615 extension_id: The id of the extension |
| 616 |
| 617 Returns: |
| 618 True if the extension is installed; False otherwise |
| 619 """ |
| 620 all_extensions = [extension['id'] for extension in self.GetExtensionsInfo()] |
| 621 return extension_id in all_extensions |
| 622 |
| 623 def _RemoveTestingExtensions(self): |
| 624 """Temporary method that cleans state for extension test. |
| 625 |
| 626 Currently the tear down method for policy_base does not clear the user |
| 627 state. See crosbug.com/27227. |
| 628 """ |
| 629 if self._CheckForExtensionByID(self._SCREEN_CAPTURE_CRX_ID): |
| 630 self.UninstallExtensionById(self._SCREEN_CAPTURE_CRX_ID) |
| 631 if self._CheckForExtensionByID(self._GOOD_CRX_ID): |
| 632 self.UninstallExtensionById(self._GOOD_CRX_ID) |
| 633 self.NavigateToURL('chrome:extensions') |
| 634 if self._CheckForExtensionByID(self._ADBLOCK_CRX_ID): |
| 635 self.UninstallExtensionById(self._ADBLOCK_CRX_ID) |
| 636 # There is an issue where if you uninstall and reinstall and extension |
| 637 # quickly with self.InstallExtension, the reinstall fails. This is a hack |
| 638 # to fix it. Bug coming soon. |
| 639 self.NavigateToURL('chrome:extensions') |
| 640 |
| 641 def testExtensionInstallPopulatedBlacklist(self): |
| 642 """Verify blacklisted extensions cannot be installed.""" |
| 643 # TODO(krisr): Remove this when crosbug.com/27227 is fixed. |
| 644 self._RemoveTestingExtensions() |
| 645 # Blacklist good.crx |
| 646 self.SetPolicies({ |
| 647 'ExtensionInstallBlacklist': [self._GOOD_CRX_ID] |
| 648 }) |
| 649 self._AttemptExtensionInstallThatShouldFail('good.crx') |
| 650 # Check adblock is installed. |
| 651 self._AttemptExtensionInstallThatShouldPass('adblock.crx') |
| 652 |
| 653 def testExtensionInstallFailWithGlobalBlacklist(self): |
| 654 """Verify no extensions can be installed when all are blacklisted.""" |
| 655 # TODO(krisr): Remove this when crosbug.com/27227 is fixed. |
| 656 self._RemoveTestingExtensions() |
| 657 # Block installs of all extensions |
| 658 self.SetPolicies({ |
| 659 'ExtensionInstallBlacklist': ['*'] |
| 660 }) |
| 661 self._AttemptExtensionInstallThatShouldFail('good.crx') |
| 662 self._AttemptExtensionInstallThatShouldFail('adblock.crx') |
| 663 |
| 664 def testExtensionInstallWithGlobalBlacklistAndWhitelistedExtension(self): |
| 665 """Verify whitelisted extension is installed when all are blacklisted.""" |
| 666 # TODO(krisr): Remove this when crosbug.com/27227 is fixed. |
| 667 self._RemoveTestingExtensions() |
| 668 # Block installs of all extensions, but whitelist adblock.crx |
| 669 self.SetPolicies({ |
| 670 'ExtensionInstallBlacklist': ['*'], |
| 671 'ExtensionInstallWhitelist': [self._ADBLOCK_CRX_ID] |
| 672 }) |
| 673 self._AttemptExtensionInstallThatShouldFail('good.crx') |
| 674 self._AttemptExtensionInstallThatShouldPass('adblock.crx') |
| 675 |
| 676 # TODO(krisr): Enable this test once we figure out why it isn't downloading |
| 677 # the extension, crbug.com/118123. |
| 678 def testExtensionInstallFromForceList(self): |
| 679 """Verify force install extensions are installed.""" |
| 680 # TODO(krisr): Remove this when crosbug.com/27227 is fixed. |
| 681 self._RemoveTestingExtensions() |
| 682 # Force an extension download from the webstore. |
| 683 self.SetPolicies({ |
| 684 'ExtensionInstallForcelist': [self._SCREEN_CAPTURE_CRX_ID], |
| 685 }) |
| 686 # Give the system 30 seconds to go get this extension. We are not sure how |
| 687 # long it will take the policy to take affect and download the extension. |
| 688 self.assertTrue(self.WaitUntil(lambda: |
| 689 self._CheckForExtensionByID(self._SCREEN_CAPTURE_CRX_ID), |
| 690 expect_retval=True), |
| 691 msg='The force install extension was never installed.') |
| 692 |
564 | 693 |
565 if __name__ == '__main__': | 694 if __name__ == '__main__': |
566 pyauto_functional.Main() | 695 pyauto_functional.Main() |
OLD | NEW |