| OLD | NEW |
| (Empty) |
| 1 #!/usr/bin/env python | |
| 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 | |
| 4 # found in the LICENSE file. | |
| 5 | |
| 6 import glob | |
| 7 import logging | |
| 8 import os | |
| 9 import shutil | |
| 10 import tempfile | |
| 11 | |
| 12 import pyauto_functional # Must be imported before pyauto | |
| 13 import pyauto | |
| 14 | |
| 15 | |
| 16 class TranslateTest(pyauto.PyUITest): | |
| 17 """Tests that translate works correctly""" | |
| 18 | |
| 19 spanish = 'es' | |
| 20 after_translate = 'AFTER_TRANSLATE' | |
| 21 before_translate = 'BEFORE_TRANSLATE' | |
| 22 translating = 'TRANSLATING' | |
| 23 translation_error = 'TRANSLATION_ERROR' | |
| 24 | |
| 25 def Debug(self): | |
| 26 """ Test method for experimentation. """ | |
| 27 while True: | |
| 28 raw_input('Hit <enter> to dump translate info.. ') | |
| 29 self.pprint(self.GetTranslateInfo()) | |
| 30 | |
| 31 def _GetDefaultSpanishURL(self): | |
| 32 return self.GetHttpURLForDataPath('translate', self.spanish, 'google.html') | |
| 33 | |
| 34 def _GetDefaultEnglishURL(self): | |
| 35 return self.GetHttpURLForDataPath('title1.html') | |
| 36 | |
| 37 def _NavigateAndWaitForBar(self, url, window_index=0, tab_index=0): | |
| 38 self.NavigateToURL(url, window_index, tab_index) | |
| 39 self.assertTrue(self.WaitForInfobarCount( | |
| 40 1, windex=window_index, tab_index=tab_index)) | |
| 41 | |
| 42 def _ClickTranslateUntilSuccess(self, window_index=0, tab_index=0): | |
| 43 """Since the translate can fail due to server error, continue trying until | |
| 44 it is successful or until it has tried too many times.""" | |
| 45 max_tries = 10 | |
| 46 curr_try = 0 | |
| 47 while (curr_try < max_tries and | |
| 48 not self.ClickTranslateBarTranslate(window_index=window_index, | |
| 49 tab_index=tab_index)): | |
| 50 curr_try = curr_try + 1 | |
| 51 if curr_try == max_tries: | |
| 52 self.fail('Translation failed more than %d times.' % max_tries) | |
| 53 | |
| 54 def _AssertTranslateWorks(self, url, original_language): | |
| 55 """Translate the page at the given URL and assert that the page has been | |
| 56 translated. | |
| 57 """ | |
| 58 self._NavigateAndWaitForBar(url) | |
| 59 translate_info = self.GetTranslateInfo() | |
| 60 self.assertEqual(original_language, translate_info['original_language']) | |
| 61 self.assertFalse(translate_info['page_translated']) | |
| 62 self.assertTrue(translate_info['can_translate_page']) | |
| 63 self.assertTrue('translate_bar' in translate_info) | |
| 64 self._ClickTranslateUntilSuccess() | |
| 65 translate_info = self.GetTranslateInfo() | |
| 66 self.assertTrue(translate_info['can_translate_page']) | |
| 67 self.assertTrue('translate_bar' in translate_info) | |
| 68 translate_bar = translate_info['translate_bar'] | |
| 69 self.assertEquals(self.after_translate, | |
| 70 translate_info['translate_bar']['bar_state']) | |
| 71 self.assertEquals(original_language, | |
| 72 translate_bar['original_lang_code']) | |
| 73 | |
| 74 def testTranslate(self): | |
| 75 """Tests that a page was translated if the user chooses to translate.""" | |
| 76 self._AssertTranslateWorks(self._GetDefaultSpanishURL(), self.spanish) | |
| 77 | |
| 78 def testNoTranslate(self): | |
| 79 """Tests that a page isn't translated if the user declines translate.""" | |
| 80 self._NavigateAndWaitForBar(self._GetDefaultSpanishURL()) | |
| 81 self.SelectTranslateOption('decline_translation') | |
| 82 translate_info = self.GetTranslateInfo() | |
| 83 self.assertEqual(self.spanish, translate_info['original_language']) | |
| 84 self.assertFalse(translate_info['page_translated']) | |
| 85 self.assertTrue(translate_info['can_translate_page']) | |
| 86 # If the user goes to the site again, the infobar should drop down but | |
| 87 # the page should not be automatically translated. | |
| 88 self._NavigateAndWaitForBar(self._GetDefaultSpanishURL()) | |
| 89 translate_info = self.GetTranslateInfo() | |
| 90 self.assertFalse(translate_info['page_translated']) | |
| 91 self.assertTrue(translate_info['can_translate_page']) | |
| 92 self.assertTrue('translate_bar' in translate_info) | |
| 93 self.assertEquals(self.before_translate, | |
| 94 translate_info['translate_bar']['bar_state']) | |
| 95 | |
| 96 def testNeverTranslateLanguage(self): | |
| 97 """Tests that blacklisting a language for translate works.""" | |
| 98 self._NavigateAndWaitForBar(self._GetDefaultSpanishURL()) | |
| 99 self.SelectTranslateOption('never_translate_language') | |
| 100 translate_info = self.GetTranslateInfo() | |
| 101 self.assertEqual(self.spanish, translate_info['original_language']) | |
| 102 self.assertFalse(translate_info['page_translated']) | |
| 103 self.assertFalse(translate_info['can_translate_page']) | |
| 104 spanish_wikipedia = 'http://es.wikipedia.org/wiki/Wikipedia:Portada' | |
| 105 self.NavigateToURL(spanish_wikipedia) | |
| 106 translate_info = self.GetTranslateInfo() | |
| 107 self.assertEqual(self.spanish, translate_info['original_language']) | |
| 108 self.assertFalse(translate_info['page_translated']) | |
| 109 self.assertFalse(translate_info['can_translate_page']) | |
| 110 | |
| 111 def testAlwaysTranslateLanguage(self): | |
| 112 """Tests that the always translate a language option works.""" | |
| 113 self._NavigateAndWaitForBar(self._GetDefaultSpanishURL()) | |
| 114 self.SelectTranslateOption('toggle_always_translate') | |
| 115 self._ClickTranslateUntilSuccess() | |
| 116 translate_info = self.GetTranslateInfo() | |
| 117 self.assertEquals(self.spanish, translate_info['original_language']) | |
| 118 self.assertTrue(translate_info['page_translated']) | |
| 119 self.assertTrue(translate_info['can_translate_page']) | |
| 120 self.assertTrue('translate_bar' in translate_info) | |
| 121 self.assertEquals(self.after_translate, | |
| 122 translate_info['translate_bar']['bar_state']) | |
| 123 # Go to another spanish site and verify that it is translated. | |
| 124 # Note that the 'This page has been translated..." bar will show up. | |
| 125 self._NavigateAndWaitForBar( | |
| 126 'http://es.wikipedia.org/wiki/Wikipedia:Portada') | |
| 127 translate_info = self.GetTranslateInfo() | |
| 128 self.assertTrue('translate_bar' in translate_info) | |
| 129 curr_bar_state = translate_info['translate_bar']['bar_state'] | |
| 130 # We don't care whether the translation has finished, just that it is | |
| 131 # trying to translate. | |
| 132 self.assertTrue(curr_bar_state is self.after_translate or | |
| 133 self.translating or self.translation_error, | |
| 134 'Bar state was %s.' % curr_bar_state) | |
| 135 | |
| 136 def testNeverTranslateSite(self): | |
| 137 """Tests that blacklisting a site for translate works.""" | |
| 138 spanish_google = 'http://www.google.com/webhp?hl=es' | |
| 139 self._NavigateAndWaitForBar(spanish_google) | |
| 140 self.SelectTranslateOption('never_translate_site') | |
| 141 translate_info = self.GetTranslateInfo() | |
| 142 self.assertFalse(translate_info['page_translated']) | |
| 143 self.assertFalse(translate_info['can_translate_page']) | |
| 144 french_google = 'http://www.google.com/webhp?hl=fr' | |
| 145 # Go to another page in the same site and verify that the page can't be | |
| 146 # translated even though it's in a different language. | |
| 147 self.NavigateToURL(french_google) | |
| 148 translate_info = self.GetTranslateInfo() | |
| 149 self.assertFalse(translate_info['page_translated']) | |
| 150 self.assertFalse(translate_info['can_translate_page']) | |
| 151 | |
| 152 def testRevert(self): | |
| 153 """Tests that reverting a site to its original language works.""" | |
| 154 self._NavigateAndWaitForBar(self._GetDefaultSpanishURL()) | |
| 155 self._ClickTranslateUntilSuccess() | |
| 156 self.RevertPageTranslation() | |
| 157 translate_info = self.GetTranslateInfo() | |
| 158 self.assertFalse(translate_info['page_translated']) | |
| 159 self.assertTrue(translate_info['can_translate_page']) | |
| 160 | |
| 161 def testBarNotVisibleOnSSLErrorPage(self): | |
| 162 """Test Translate bar is not visible on SSL error pages.""" | |
| 163 self._NavigateAndWaitForBar(self._GetDefaultSpanishURL()) | |
| 164 translate_info = self.GetTranslateInfo() | |
| 165 self.assertTrue('translate_bar' in translate_info) | |
| 166 self.assertTrue(translate_info['can_translate_page']) | |
| 167 # This page is an ssl error page. | |
| 168 self.NavigateToURL('https://www.sourceforge.net') | |
| 169 self.assertTrue(self.WaitForInfobarCount(0)) | |
| 170 translate_info = self.GetTranslateInfo() | |
| 171 self.assertFalse('translate_bar' in translate_info) | |
| 172 | |
| 173 def testBarNotVisibleOnEnglishPage(self): | |
| 174 """Test Translate bar is not visible on English pages.""" | |
| 175 self._NavigateAndWaitForBar(self._GetDefaultSpanishURL()) | |
| 176 translate_info = self.GetTranslateInfo() | |
| 177 self.assertTrue('translate_bar' in translate_info) | |
| 178 self.assertTrue(translate_info['can_translate_page']) | |
| 179 # With the translate bar visible in same tab open an English page. | |
| 180 self.NavigateToURL(self._GetDefaultEnglishURL()) | |
| 181 self.assertTrue(self.WaitForInfobarCount(0)) | |
| 182 translate_info = self.GetTranslateInfo() | |
| 183 self.assertFalse('translate_bar' in translate_info) | |
| 184 | |
| 185 def testTranslateDiffURLs(self): | |
| 186 """Test that HTTP, HTTPS, and file urls all get translated.""" | |
| 187 http_url = 'http://www.google.com/webhp?hl=es' | |
| 188 https_url = 'https://www.google.com/webhp?hl=es' | |
| 189 file_url = self._GetDefaultSpanishURL() | |
| 190 for url in (http_url, https_url, file_url): | |
| 191 self._AssertTranslateWorks(url, self.spanish) | |
| 192 | |
| 193 def testNotranslateMetaTag(self): | |
| 194 """Test "notranslate" meta tag.""" | |
| 195 self._NavigateAndWaitForBar(self._GetDefaultSpanishURL()) | |
| 196 self.NavigateToURL(self.GetFileURLForDataPath( | |
| 197 'translate', 'notranslate_meta_tag.html')) | |
| 198 self.assertTrue(self.WaitForInfobarCount(0)) | |
| 199 translate_info = self.GetTranslateInfo() | |
| 200 self.assertFalse('translate_bar' in translate_info) | |
| 201 | |
| 202 def testToggleTranslateOption(self): | |
| 203 """Test to toggle the 'Always Translate' option.""" | |
| 204 self._NavigateAndWaitForBar(self._GetDefaultSpanishURL()) | |
| 205 # Assert the default. | |
| 206 translate_info = self.GetTranslateInfo() | |
| 207 self.assertFalse(translate_info['page_translated']) | |
| 208 self.assertTrue('translate_bar' in translate_info) | |
| 209 # Select 'Always Translate'. | |
| 210 self.SelectTranslateOption('toggle_always_translate') | |
| 211 self._ClickTranslateUntilSuccess() | |
| 212 translate_info = self.GetTranslateInfo() | |
| 213 self.assertTrue(translate_info['page_translated']) | |
| 214 # Reload the tab and confirm the page was translated. | |
| 215 self.ReloadTab() | |
| 216 self.assertTrue(self.WaitForInfobarCount(1)) | |
| 217 success = self.WaitUntilTranslateComplete() | |
| 218 # Sometimes the translation fails. Continue clicking until it succeeds. | |
| 219 if not success: | |
| 220 self._ClickTranslateUntilSuccess() | |
| 221 # Uncheck 'Always Translate' | |
| 222 self.SelectTranslateOption('toggle_always_translate') | |
| 223 self.PerformActionOnInfobar('dismiss', 0) | |
| 224 translate_info = self.GetTranslateInfo() | |
| 225 self.assertTrue(translate_info['page_translated']) | |
| 226 self.assertFalse('translate_bar' in translate_info) | |
| 227 # Reload the tab and confirm that the page has not been translated. | |
| 228 self.ReloadTab() | |
| 229 translate_info = self.GetTranslateInfo() | |
| 230 self.assertFalse(translate_info['page_translated']) | |
| 231 self.assertTrue('translate_bar' in translate_info) | |
| 232 | |
| 233 def testSessionRestore(self): | |
| 234 """Test that session restore restores the translate infobar and other | |
| 235 translate settings. | |
| 236 """ | |
| 237 # Due to crbug.com/51439, we must open two tabs here. | |
| 238 self.NavigateToURL("http://www.news.google.com") | |
| 239 self.AppendTab(pyauto.GURL("http://www.google.com/webhp?hl=es")) | |
| 240 self.assertTrue(self.WaitForInfobarCount(1, tab_index=1)) | |
| 241 translate_info = self.GetTranslateInfo(tab_index=1) | |
| 242 self.assertTrue('translate_bar' in translate_info) | |
| 243 self.SelectTranslateOption('toggle_always_translate', tab_index=1) | |
| 244 self._ClickTranslateUntilSuccess(tab_index=1) | |
| 245 self.SetPrefs(pyauto.kRestoreOnStartup, 1) | |
| 246 self.RestartBrowser(clear_profile=False) | |
| 247 self.assertTrue(self.WaitForInfobarCount(1, tab_index=1)) | |
| 248 self.WaitUntilTranslateComplete(tab_index=1) | |
| 249 translate_info = self.GetTranslateInfo(tab_index=1) | |
| 250 self.assertTrue('translate_bar' in translate_info) | |
| 251 # Sometimes translation fails. We don't really care whether it succeededs, | |
| 252 # just that a translation was attempted. | |
| 253 self.assertNotEqual(self.before_translate, | |
| 254 translate_info['translate_bar']['bar_state']) | |
| 255 | |
| 256 def testGoBackAndForwardToTranslatePage(self): | |
| 257 """Tests that translate bar re-appears after backward and forward | |
| 258 navigation to a page that can be translated. | |
| 259 """ | |
| 260 no_trans_url = self._GetDefaultEnglishURL() | |
| 261 trans_url = self._GetDefaultSpanishURL() | |
| 262 self._NavigateAndWaitForBar(trans_url) | |
| 263 translate_info = self.GetTranslateInfo() | |
| 264 self.assertTrue('translate_bar' in translate_info) | |
| 265 self.NavigateToURL(no_trans_url) | |
| 266 self.assertTrue(self.WaitForInfobarCount(0)) | |
| 267 self.assertFalse('translate_bar' in self.GetTranslateInfo()) | |
| 268 # Go back to the page that should be translated and assert that the | |
| 269 # translate bar re-appears. | |
| 270 self.TabGoBack() | |
| 271 self.assertTrue(self.WaitForInfobarCount(1)) | |
| 272 self.assertTrue('translate_bar' in self.GetTranslateInfo()) | |
| 273 | |
| 274 # Now test going forward. | |
| 275 self.NavigateToURL(no_trans_url) | |
| 276 translate_info = self.GetTranslateInfo() | |
| 277 self.assertFalse('translate_bar' in translate_info) | |
| 278 self._AssertTranslateWorks(trans_url, self.spanish) | |
| 279 self.TabGoBack() | |
| 280 self.assertTrue(self.WaitForInfobarCount(0)) | |
| 281 translate_info = self.GetTranslateInfo() | |
| 282 self.assertFalse('translate_bar' in translate_info) | |
| 283 self.TabGoForward() | |
| 284 self.assertTrue(self.WaitForInfobarCount(1)) | |
| 285 translate_info = self.GetTranslateInfo() | |
| 286 self.assertTrue(translate_info['can_translate_page']) | |
| 287 self.assertTrue('translate_bar' in translate_info) | |
| 288 | |
| 289 def testForCrashedTab(self): | |
| 290 """Tests that translate bar is dimissed if the renderer crashes.""" | |
| 291 crash_url = 'about:crash' | |
| 292 self._NavigateAndWaitForBar(self._GetDefaultSpanishURL()) | |
| 293 translate_info = self.GetTranslateInfo() | |
| 294 self.assertTrue('translate_bar' in translate_info) | |
| 295 self.NavigateToURL(crash_url) | |
| 296 self.assertTrue(self.WaitForInfobarCount(0)) | |
| 297 self.assertFalse('translate_bar' in self.GetTranslateInfo()) | |
| 298 | |
| 299 def testTranslatePrefs(self): | |
| 300 """Test the Prefs:Translate option.""" | |
| 301 # Assert defaults first. | |
| 302 self.assertTrue(self.GetPrefsInfo().Prefs(pyauto.kEnableTranslate)) | |
| 303 # Uncheck. | |
| 304 self.SetPrefs(pyauto.kEnableTranslate, False) | |
| 305 self.NavigateToURL(self._GetDefaultSpanishURL()) | |
| 306 self.assertFalse('translate_bar' in self.GetTranslateInfo()) | |
| 307 # Restart the browser and assert the translate preference stays. | |
| 308 self.RestartBrowser(clear_profile=False) | |
| 309 self.assertFalse(self.GetPrefsInfo().Prefs(pyauto.kEnableTranslate)) | |
| 310 self.NavigateToURL(self._GetDefaultSpanishURL()) | |
| 311 self.assertFalse('translate_bar' in self.GetTranslateInfo()) | |
| 312 | |
| 313 def testAlwaysTranslateLanguageButton(self): | |
| 314 """Test the always translate language button.""" | |
| 315 spanish_url = self._GetDefaultSpanishURL() | |
| 316 self._NavigateAndWaitForBar(spanish_url) | |
| 317 | |
| 318 # The 'Always Translate' button doesn't show up until the user has clicked | |
| 319 # 'Translate' for a language 3 times. | |
| 320 for unused in range(3): | |
| 321 self._ClickTranslateUntilSuccess() | |
| 322 self.ReloadTab() | |
| 323 | |
| 324 # Click the 'Always Translate' button. | |
| 325 self.assertTrue(self.GetTranslateInfo()\ | |
| 326 ['translate_bar']['always_translate_lang_button_showing']) | |
| 327 self.SelectTranslateOption('click_always_translate_lang_button') | |
| 328 # Navigate to another Spanish page and verify it was translated. | |
| 329 self._NavigateAndWaitForBar('http://www.google.com/webhp?hl=es') | |
| 330 self.WaitUntilTranslateComplete() | |
| 331 # Assert that a translation was attempted. We don't care if it was error | |
| 332 # or success. | |
| 333 self.assertNotEqual(self.before_translate, | |
| 334 self.GetTranslateInfo()['translate_bar']['bar_state']) | |
| 335 | |
| 336 def testNeverTranslateLanguageButton(self): | |
| 337 """Test the never translate language button.""" | |
| 338 spanish_url = self._GetDefaultSpanishURL() | |
| 339 self._NavigateAndWaitForBar(spanish_url) | |
| 340 | |
| 341 # The 'Never Translate' button doesn't show up until the user has clicked | |
| 342 # 'Nope' for a language 3 times. | |
| 343 for unused in range(3): | |
| 344 self.SelectTranslateOption('decline_translation') | |
| 345 self.ReloadTab() | |
| 346 | |
| 347 # Click the 'Never Translate' button. | |
| 348 self.assertTrue(self.GetTranslateInfo()\ | |
| 349 ['translate_bar']['never_translate_lang_button_showing']) | |
| 350 self.SelectTranslateOption('click_never_translate_lang_button') | |
| 351 # Navigate to another Spanish page and verify the page can't be translated. | |
| 352 # First navigate to a French page, wait for bar, navigate to Spanish page | |
| 353 # and wait for bar to go away. | |
| 354 self._NavigateAndWaitForBar('http://www.google.com/webhp?hl=fr') | |
| 355 self.NavigateToURL('http://www.google.com/webhp?hl=es') | |
| 356 self.assertTrue(self.WaitForInfobarCount(0)) | |
| 357 self.assertFalse(self.GetTranslateInfo()['can_translate_page']) | |
| 358 | |
| 359 def testChangeTargetLanguageAlwaysTranslate(self): | |
| 360 """Tests that the change target language option works with always translate | |
| 361 on reload. | |
| 362 | |
| 363 This test is motivated by crbug.com/37313. | |
| 364 """ | |
| 365 self._NavigateAndWaitForBar(self._GetDefaultSpanishURL()) | |
| 366 self._ClickTranslateUntilSuccess() | |
| 367 # Select a different target language on translate info bar (French). | |
| 368 self.ChangeTranslateToLanguage('French') | |
| 369 translate_info = self.GetTranslateInfo() | |
| 370 self.assertTrue('translate_bar' in translate_info) | |
| 371 self.assertEqual('fr', translate_info['translate_bar']['target_lang_code']) | |
| 372 # Select always translate Spanish to French. | |
| 373 self.SelectTranslateOption('toggle_always_translate') | |
| 374 # Reload the page and assert that the page has been translated to French. | |
| 375 self.ReloadTab() | |
| 376 self.WaitUntilTranslateComplete() | |
| 377 translate_info = self.GetTranslateInfo() | |
| 378 self.assertTrue(translate_info['page_translated']) | |
| 379 self.assertTrue(translate_info['can_translate_page']) | |
| 380 self.assertTrue('translate_bar' in translate_info) | |
| 381 self.assertEqual('fr', translate_info['translate_bar']['target_lang_code']) | |
| 382 | |
| 383 def testSeveralLanguages(self): | |
| 384 """Verify translation for several languages. | |
| 385 | |
| 386 This assumes that there is a directory of directories in the data dir. | |
| 387 The directory is called 'translate', and within that directory there are | |
| 388 subdirectories for each language code. Ex. a subdirectory 'es' with Spanish | |
| 389 html files. | |
| 390 """ | |
| 391 corpora_path = os.path.join(self.DataDir(), 'translate') | |
| 392 corp_dir = glob.glob(os.path.join(corpora_path, '??')) + \ | |
| 393 glob.glob(os.path.join(corpora_path, '??-??')) | |
| 394 | |
| 395 for language in corp_dir: | |
| 396 files = glob.glob(os.path.join(language, '*.html*')) | |
| 397 lang_code = os.path.basename(language) | |
| 398 logging.debug('Starting language %s' % lang_code) | |
| 399 # Translate each html file in the language directory. | |
| 400 for lang_file in files: | |
| 401 logging.debug('Starting file %s' % lang_file) | |
| 402 lang_file = self.GetFileURLForPath(lang_file) | |
| 403 self._AssertTranslateWorks(lang_file, lang_code) | |
| 404 | |
| 405 def _CreateCrazyDownloads(self): | |
| 406 """Doownload files with filenames containing special chars. | |
| 407 The files are created on the fly and cleaned after use. | |
| 408 """ | |
| 409 download_dir = self.GetDownloadDirectory().value() | |
| 410 filename = os.path.join(self.DataDir(), 'downloads', 'crazy_filenames.txt') | |
| 411 crazy_filenames = self.EvalDataFrom(filename) | |
| 412 logging.info('Testing with %d crazy filenames' % len(crazy_filenames)) | |
| 413 | |
| 414 def _CreateFile(name): | |
| 415 """Create and fill the given file with some junk.""" | |
| 416 fp = open(name, 'w') # name could be unicode | |
| 417 print >>fp, 'This is a junk file named %s. ' % repr(name) * 100 | |
| 418 fp.close() | |
| 419 | |
| 420 # Temp dir for hosting crazy filenames. | |
| 421 temp_dir = tempfile.mkdtemp(prefix='download') | |
| 422 # Filesystem-interfacing functions like os.listdir() need to | |
| 423 # be given unicode strings to "do the right thing" on win. | |
| 424 # Ref: http://boodebr.org/main/python/all-about-python-and-unicode | |
| 425 try: | |
| 426 for filename in crazy_filenames: # filename is unicode. | |
| 427 utf8_filename = filename.encode('utf-8') | |
| 428 file_path = os.path.join(temp_dir, utf8_filename) | |
| 429 _CreateFile(os.path.join(temp_dir, filename)) # unicode file. | |
| 430 file_url = self.GetFileURLForPath(file_path) | |
| 431 downloaded_file = os.path.join(download_dir, filename) | |
| 432 os.path.exists(downloaded_file) and os.remove(downloaded_file) | |
| 433 pre_download_ids = [x['id'] | |
| 434 for x in self.GetDownloadsInfo().Downloads()] | |
| 435 self.DownloadAndWaitForStart(file_url) | |
| 436 # Wait for files and remove them as we go. | |
| 437 self.WaitForAllDownloadsToComplete(pre_download_ids) | |
| 438 os.path.exists(downloaded_file) and os.remove(downloaded_file) | |
| 439 finally: | |
| 440 shutil.rmtree(unicode(temp_dir)) # unicode so that win treats nicely. | |
| 441 | |
| 442 def testHistoryNotTranslated(self): | |
| 443 """Tests navigating to History page with other languages.""" | |
| 444 # Build the history with non-English content. | |
| 445 history_file = os.path.join( | |
| 446 self.DataDir(), 'translate', 'crazy_history.txt') | |
| 447 history_items = self.EvalDataFrom(history_file) | |
| 448 for history_item in history_items: | |
| 449 self.AddHistoryItem(history_item) | |
| 450 # Navigate to a page that triggers the translate bar so we can verify that | |
| 451 # it disappears on the history page. | |
| 452 self._NavigateAndWaitForBar(self._GetDefaultSpanishURL()) | |
| 453 self.NavigateToURL('chrome://history/') | |
| 454 self.assertTrue(self.WaitForInfobarCount(0)) | |
| 455 self.assertFalse('translate_bar' in self.GetTranslateInfo()) | |
| 456 | |
| 457 def testDownloadsNotTranslated(self): | |
| 458 """Tests navigating to the Downloads page with other languages.""" | |
| 459 # Build the download history with non-English content. | |
| 460 self._CreateCrazyDownloads() | |
| 461 # Navigate to a page that triggers the translate bar so we can verify that | |
| 462 # it disappears on the downloads page. | |
| 463 self._NavigateAndWaitForBar(self._GetDefaultSpanishURL()) | |
| 464 self.NavigateToURL('chrome://downloads/') | |
| 465 self.assertTrue(self.WaitForInfobarCount(0)) | |
| 466 self.assertFalse('translate_bar' in self.GetTranslateInfo()) | |
| 467 | |
| 468 def testAlwaysTranslateInIncognito(self): | |
| 469 """Verify that pages aren't auto-translated in incognito windows.""" | |
| 470 url = self._GetDefaultSpanishURL() | |
| 471 self._NavigateAndWaitForBar(url) | |
| 472 info_before_translate = self.GetTranslateInfo() | |
| 473 self.assertTrue('translate_bar' in info_before_translate) | |
| 474 self.SelectTranslateOption('toggle_always_translate') | |
| 475 | |
| 476 # Navigate to a page in incognito and verify that it is not auto-translated. | |
| 477 self.RunCommand(pyauto.IDC_NEW_INCOGNITO_WINDOW) | |
| 478 self._NavigateAndWaitForBar(url, window_index=1) | |
| 479 info_before_translate = self.GetTranslateInfo() | |
| 480 self.assertTrue('translate_bar' in info_before_translate) | |
| 481 self.assertFalse(info_before_translate['page_translated']) | |
| 482 self.assertTrue(info_before_translate['can_translate_page']) | |
| 483 self.assertEqual(self.before_translate, | |
| 484 info_before_translate['translate_bar']['bar_state']) | |
| 485 | |
| 486 def testMultipleTabsAndWindows(self): | |
| 487 """Verify that translate infobar shows up in multiple tabs and windows.""" | |
| 488 url = self._GetDefaultSpanishURL() | |
| 489 def _AssertTranslateInfobarShowing(window_index=0, tab_index=0): | |
| 490 """Navigate to a Spanish page in the given window/tab and verify that the | |
| 491 translate bar shows up. | |
| 492 """ | |
| 493 self._NavigateAndWaitForBar( | |
| 494 url, window_index=window_index, tab_index=tab_index) | |
| 495 info_before_translate = self.GetTranslateInfo(window_index=window_index, | |
| 496 tab_index=tab_index) | |
| 497 self.assertTrue('translate_bar' in info_before_translate) | |
| 498 | |
| 499 _AssertTranslateInfobarShowing() | |
| 500 self.AppendTab(pyauto.GURL('about:blank')) | |
| 501 _AssertTranslateInfobarShowing(tab_index=1) | |
| 502 self.OpenNewBrowserWindow(True) | |
| 503 _AssertTranslateInfobarShowing(window_index=1) | |
| 504 self.AppendTab(pyauto.GURL('about:blank'), 1) | |
| 505 _AssertTranslateInfobarShowing(window_index=1, tab_index=1) | |
| 506 self.RunCommand(pyauto.IDC_NEW_INCOGNITO_WINDOW) | |
| 507 _AssertTranslateInfobarShowing(window_index=2) | |
| 508 self.AppendTab(pyauto.GURL('about:blank'), 2) | |
| 509 _AssertTranslateInfobarShowing(window_index=2, tab_index=1) | |
| 510 | |
| 511 def testNoTranslateInfobarAfterNeverTranslate(self): | |
| 512 """Verify Translate Info bar should not stay on the page after opting | |
| 513 Never translate the page""" | |
| 514 url = self._GetDefaultSpanishURL() | |
| 515 self._NavigateAndWaitForBar(url) | |
| 516 self.SelectTranslateOption('never_translate_language') | |
| 517 self.assertFalse(self.GetBrowserInfo()['windows'][0]['tabs'][0]['infobars']) | |
| 518 | |
| 519 if __name__ == '__main__': | |
| 520 pyauto_functional.Main() | |
| OLD | NEW |