| 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 import glob | |
| 7 import logging | |
| 8 import os | |
| 9 | |
| 10 import pyauto_functional # Must be imported before pyauto | |
| 11 import pyauto | |
| 12 | |
| 13 | |
| 14 class ThemesTest(pyauto.PyUITest): | |
| 15 """TestCase for Themes.""" | |
| 16 | |
| 17 def Debug(self): | |
| 18 """Test method for experimentation. | |
| 19 | |
| 20 This method will not run automatically. | |
| 21 """ | |
| 22 while True: | |
| 23 raw_input('Hit <enter> to dump info.. ') | |
| 24 self.pprint(self.GetThemeInfo()) | |
| 25 | |
| 26 def _SetThemeAndVerify(self, crx_file, theme_name): | |
| 27 """Set theme and verify infobar appears and the theme name is correct. | |
| 28 | |
| 29 Args: | |
| 30 crx_file: Path to .crx file to be set as theme. | |
| 31 theme_name: String to be compared to GetThemeInfo()['name']. | |
| 32 """ | |
| 33 # Starting infobar count is the number of non-themes infobars. | |
| 34 infobars = self.GetBrowserInfo()['windows'][0]['tabs'][0]['infobars'] | |
| 35 infobar_count = 0 | |
| 36 for infobar in infobars: | |
| 37 if not (('text' in infobar) and | |
| 38 infobar['text'].startswith('Installed theme')): | |
| 39 infobar_count += 1 | |
| 40 self.SetTheme(crx_file) | |
| 41 # Verify infobar shows up. | |
| 42 self.assertTrue(self.WaitForInfobarCount(infobar_count + 1)) | |
| 43 self.assertTrue(self.GetBrowserInfo()['windows'][0]['tabs'][0]['infobars']) | |
| 44 # Verify theme name is correct. | |
| 45 self.assertEqual(theme_name, self.GetThemeInfo()['name']) | |
| 46 | |
| 47 def testSetTheme(self): | |
| 48 """Verify theme install.""" | |
| 49 self.assertFalse(self.GetThemeInfo()) # Verify there's no theme at startup | |
| 50 crx_file = os.path.abspath( | |
| 51 os.path.join(self.DataDir(), 'extensions', 'theme.crx')) | |
| 52 self._SetThemeAndVerify(crx_file, 'camo theme') | |
| 53 | |
| 54 def testThemeInFullScreen(self): | |
| 55 """Verify theme can be installed in FullScreen mode.""" | |
| 56 self.ApplyAccelerator(pyauto.IDC_FULLSCREEN) | |
| 57 self.assertFalse(self.GetThemeInfo()) # Verify there's no theme at startup | |
| 58 crx_file = os.path.abspath( | |
| 59 os.path.join(self.DataDir(), 'extensions', 'theme.crx')) | |
| 60 self._SetThemeAndVerify(crx_file, 'camo theme') | |
| 61 | |
| 62 def testThemeReset(self): | |
| 63 """Verify theme reset.""" | |
| 64 crx_file = os.path.abspath( | |
| 65 os.path.join(self.DataDir(), 'extensions', 'theme.crx')) | |
| 66 self.SetTheme(crx_file) | |
| 67 self.ResetToDefaultTheme() | |
| 68 self.assertFalse(self.GetThemeInfo()) | |
| 69 | |
| 70 def testThemeUndo(self): | |
| 71 """Verify theme undo.""" | |
| 72 crx_file = os.path.abspath( | |
| 73 os.path.join(self.DataDir(), 'extensions', 'theme.crx')) | |
| 74 self._SetThemeAndVerify(crx_file, 'camo theme') | |
| 75 # Undo theme install. | |
| 76 infobars = self.GetBrowserInfo()['windows'][0]['tabs'][0]['infobars'] | |
| 77 for index, infobar in enumerate(infobars): | |
| 78 if (('text' in infobar) and | |
| 79 infobar['text'].startswith('Installed theme')): | |
| 80 theme_index = index | |
| 81 break | |
| 82 self.PerformActionOnInfobar('cancel', infobar_index=theme_index) | |
| 83 self.assertFalse(self.GetThemeInfo()) | |
| 84 | |
| 85 def testThemeOverInstall(self): | |
| 86 """Verify that can install a theme over an existing theme.""" | |
| 87 crx_file = os.path.abspath( | |
| 88 os.path.join(self.DataDir(), 'extensions', 'theme.crx')) | |
| 89 self._SetThemeAndVerify(crx_file, 'camo theme') | |
| 90 # Install a different theme. | |
| 91 crx_file = os.path.abspath( | |
| 92 os.path.join(self.DataDir(), 'extensions', 'theme2.crx')) | |
| 93 self._SetThemeAndVerify(crx_file, 'snowflake theme') | |
| 94 | |
| 95 def _ReturnCrashingThemes(self, themes, group_size, urls): | |
| 96 """Install the given themes in groups of group_size and return the | |
| 97 group of themes that crashes (if any). | |
| 98 | |
| 99 Note: restarts the browser at the beginning of the function. | |
| 100 | |
| 101 Args: | |
| 102 themes: A list of themes to install. | |
| 103 group_size: The number of themes to install at one time. | |
| 104 urls: The list of urls to visit. | |
| 105 | |
| 106 Returns: | |
| 107 Group of themes that crashed (if any). | |
| 108 """ | |
| 109 self.RestartBrowser() | |
| 110 curr_theme = 0 | |
| 111 num_themes = len(themes) | |
| 112 | |
| 113 while curr_theme < num_themes: | |
| 114 logging.debug('New group of %d themes.' % group_size) | |
| 115 group_end = curr_theme + group_size | |
| 116 this_group = themes[curr_theme:group_end] | |
| 117 | |
| 118 # Apply each theme in this group. | |
| 119 for theme in this_group: | |
| 120 logging.debug('Applying theme: %s' % theme) | |
| 121 self.SetTheme(theme) | |
| 122 | |
| 123 for url in urls: | |
| 124 self.NavigateToURL(url) | |
| 125 | |
| 126 def _LogAndReturnCrashing(): | |
| 127 logging.debug('Crashing themes: %s' % this_group) | |
| 128 return this_group | |
| 129 | |
| 130 # Assert that there is at least 1 browser window. | |
| 131 try: | |
| 132 num_browser_windows = self.GetBrowserWindowCount() | |
| 133 except: | |
| 134 return _LogAndReturnCrashing() | |
| 135 else: | |
| 136 if not num_browser_windows: | |
| 137 return _LogAndReturnCrashing() | |
| 138 | |
| 139 curr_theme = group_end | |
| 140 | |
| 141 # None of the themes crashed. | |
| 142 return None | |
| 143 | |
| 144 def Runner(self): | |
| 145 """Apply themes; verify that theme has been applied and browser doesn't | |
| 146 crash. | |
| 147 | |
| 148 This does not get run automatically. To run: | |
| 149 python themes.py themes.ThemesTest.Runner | |
| 150 | |
| 151 Note: this test requires that a directory of crx files called 'themes' | |
| 152 exists in the data directory. | |
| 153 """ | |
| 154 themes_dir = os.path.join(self.DataDir(), 'themes') | |
| 155 urls_file = os.path.join(self.DataDir(), 'urls.txt') | |
| 156 | |
| 157 assert os.path.exists(themes_dir), \ | |
| 158 'The dir "%s" must exist' % os.path.abspath(themes_dir) | |
| 159 | |
| 160 group_size = 20 | |
| 161 num_urls_to_visit = 100 | |
| 162 | |
| 163 urls = [l.rstrip() for l in | |
| 164 open(urls_file).readlines()[:num_urls_to_visit]] | |
| 165 failed_themes = glob.glob(os.path.join(themes_dir, '*.crx')) | |
| 166 | |
| 167 while failed_themes and group_size: | |
| 168 failed_themes = self._ReturnCrashingThemes(failed_themes, group_size, | |
| 169 urls) | |
| 170 group_size = group_size // 2 | |
| 171 | |
| 172 self.assertFalse(failed_themes, | |
| 173 'Theme(s) in failing group: %s' % failed_themes) | |
| 174 | |
| 175 | |
| 176 if __name__ == '__main__': | |
| 177 pyauto_functional.Main() | |
| OLD | NEW |