Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 #!/usr/bin/python | 1 #!/usr/bin/python |
| 2 # Copyright (c) 2010 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2010 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 glob | 6 import glob |
| 7 import logging | 7 import logging |
| 8 import os | 8 import os |
| 9 import shutil | 9 import shutil |
| 10 import tempfile | 10 import tempfile |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 34 """Return the file URL for the given file in the data directory.""" | 34 """Return the file URL for the given file in the data directory.""" |
| 35 return self.GetFileURLForPath(os.path.join(self.DataDir(), filename)) | 35 return self.GetFileURLForPath(os.path.join(self.DataDir(), filename)) |
| 36 | 36 |
| 37 def _GetDefaultSpanishURL(self): | 37 def _GetDefaultSpanishURL(self): |
| 38 return self._GetURLForDataDirFile( | 38 return self._GetURLForDataDirFile( |
| 39 os.path.join('translate', self.spanish, 'google.html')) | 39 os.path.join('translate', self.spanish, 'google.html')) |
| 40 | 40 |
| 41 def _GetDefaultEnglishURL(self): | 41 def _GetDefaultEnglishURL(self): |
| 42 return self._GetURLForDataDirFile('title1.html') | 42 return self._GetURLForDataDirFile('title1.html') |
| 43 | 43 |
| 44 def _NavigateAndWaitForBar(self, url): | 44 def _NavigateAndWaitForBar(self, url, window_index=0, tab_index=0): |
| 45 self.NavigateToURL(url) | 45 self.NavigateToURL(url, window_index, tab_index) |
| 46 self.WaitForInfobarCount(1) | 46 self.WaitForInfobarCount(1, windex=window_index, tab_index=tab_index) |
| 47 | 47 |
| 48 def _ClickTranslateUntilSuccess(self, window_index=0, tab_index=0): | 48 def _ClickTranslateUntilSuccess(self, window_index=0, tab_index=0): |
| 49 """Since the translate can fail due to server error, continue trying until | 49 """Since the translate can fail due to server error, continue trying until |
| 50 it is successful or until it has tried too many times.""" | 50 it is successful or until it has tried too many times.""" |
| 51 max_tries = 10 | 51 max_tries = 10 |
| 52 curr_try = 0 | 52 curr_try = 0 |
| 53 while (curr_try < max_tries and | 53 while (curr_try < max_tries and |
| 54 not self.ClickTranslateBarTranslate(window_index=window_index, | 54 not self.ClickTranslateBarTranslate(window_index=window_index, |
| 55 tab_index=tab_index)): | 55 tab_index=tab_index)): |
| 56 curr_try = curr_try + 1 | 56 curr_try = curr_try + 1 |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 189 self.assertFalse('translate_bar' in translate_info) | 189 self.assertFalse('translate_bar' in translate_info) |
| 190 | 190 |
| 191 def testTranslateDiffURLs(self): | 191 def testTranslateDiffURLs(self): |
| 192 """Test that HTTP, HTTPS, and file urls all get translated.""" | 192 """Test that HTTP, HTTPS, and file urls all get translated.""" |
| 193 http_url = 'http://www.google.com/webhp?hl=es' | 193 http_url = 'http://www.google.com/webhp?hl=es' |
| 194 https_url = 'https://www.google.com/webhp?hl=es' | 194 https_url = 'https://www.google.com/webhp?hl=es' |
| 195 file_url = self._GetDefaultSpanishURL() | 195 file_url = self._GetDefaultSpanishURL() |
| 196 for url in (http_url, https_url, file_url): | 196 for url in (http_url, https_url, file_url): |
| 197 self._AssertTranslateWorks(url, self.spanish) | 197 self._AssertTranslateWorks(url, self.spanish) |
| 198 | 198 |
| 199 def testNeverTranslateLanguage(self): | |
|
Alyssa
2010/08/23 22:42:54
A test with the same name and almost duplicate cod
| |
| 200 """Verify no translate bar for blacklisted language.""" | |
| 201 self._NavigateAndWaitForBar(self._GetDefaultSpanishURL()) | |
| 202 self.SelectTranslateOption('never_translate_language') | |
| 203 self.assertFalse(self.GetBrowserInfo()['windows'][0]['tabs'][0]['infobars']) | |
| 204 # Navigate to a page that will show the translate bar, then navigate away. | |
| 205 self._NavigateAndWaitForBar( | |
| 206 self._GetURLForDataDirFile('french_page.html')) | |
| 207 self.NavigateToURL('http://es.wikipedia.org/wiki/Wikipedia:Portada') | |
| 208 self.WaitForInfobarCount(0) | |
| 209 translate_info = self.GetTranslateInfo() | |
| 210 self.assertFalse('translate_bar' in translate_info) | |
| 211 self.assertFalse(translate_info['page_translated']) | |
| 212 self.assertFalse(translate_info['can_translate_page']) | |
| 213 | |
| 214 def testNotranslateMetaTag(self): | 199 def testNotranslateMetaTag(self): |
| 215 """Test "notranslate" meta tag.""" | 200 """Test "notranslate" meta tag.""" |
| 216 self._NavigateAndWaitForBar(self._GetDefaultSpanishURL()) | 201 self._NavigateAndWaitForBar(self._GetDefaultSpanishURL()) |
| 217 self.NavigateToURL(self._GetURLForDataDirFile( | 202 self.NavigateToURL(self._GetURLForDataDirFile( |
| 218 os.path.join('translate', 'notranslate_meta_tag.html'))) | 203 os.path.join('translate', 'notranslate_meta_tag.html'))) |
| 219 self.WaitForInfobarCount(0) | 204 self.WaitForInfobarCount(0) |
| 220 translate_info = self.GetTranslateInfo() | 205 translate_info = self.GetTranslateInfo() |
| 221 self.assertFalse('translate_bar' in translate_info) | 206 self.assertFalse('translate_bar' in translate_info) |
| 222 | 207 |
| 223 def testToggleTranslateOption(self): | 208 def testToggleTranslateOption(self): |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 330 self.assertFalse(self.GetPrefsInfo().Prefs(pyauto.kEnableTranslate)) | 315 self.assertFalse(self.GetPrefsInfo().Prefs(pyauto.kEnableTranslate)) |
| 331 self.NavigateToURL(self._GetDefaultSpanishURL()) | 316 self.NavigateToURL(self._GetDefaultSpanishURL()) |
| 332 self.assertFalse('translate_bar' in self.GetTranslateInfo()) | 317 self.assertFalse('translate_bar' in self.GetTranslateInfo()) |
| 333 | 318 |
| 334 def testAlwaysTranslateLanguageButton(self): | 319 def testAlwaysTranslateLanguageButton(self): |
| 335 """Test the always translate language button.""" | 320 """Test the always translate language button.""" |
| 336 spanish_url = self._GetDefaultSpanishURL() | 321 spanish_url = self._GetDefaultSpanishURL() |
| 337 self._NavigateAndWaitForBar(spanish_url) | 322 self._NavigateAndWaitForBar(spanish_url) |
| 338 | 323 |
| 339 # The 'Always Translate' button doesn't show up until the user has clicked | 324 # The 'Always Translate' button doesn't show up until the user has clicked |
| 340 # 'Translate' for a language several times. | 325 # 'Translate' for a language 3 times. |
| 341 max_tries = 10 | 326 for i in range(3): |
|
Nirnimesh
2010/08/24 01:23:19
replace i with '_' or 'unused' to avoid pychecker
| |
| 342 curr_try = 0 | |
| 343 while (curr_try < max_tries and | |
| 344 not self.GetTranslateInfo()['translate_bar']\ | |
| 345 ['always_translate_lang_button_showing']): | |
| 346 self._ClickTranslateUntilSuccess() | 327 self._ClickTranslateUntilSuccess() |
| 347 self._NavigateAndWaitForBar(spanish_url) | 328 self.GetBrowserWindow(0).GetTab(0).Reload() |
| 348 curr_try = curr_try + 1 | |
| 349 if curr_try == max_tries: | |
| 350 self.fail('Clicked translate %d times and always translate button never '\ | |
| 351 'showed up.' % max_tries) | |
| 352 | 329 |
|
Nirnimesh
2010/08/24 01:23:19
Shouldn't you check that the button shows up?
| |
| 353 # Click the 'Always Translate' button. | 330 # Click the 'Always Translate' button. |
| 354 self.SelectTranslateOption('click_always_translate_lang_button') | 331 self.SelectTranslateOption('click_always_translate_lang_button') |
| 355 # Navigate to another Spanish page and verify it was translated. | 332 # Navigate to another Spanish page and verify it was translated. |
| 356 self._NavigateAndWaitForBar('http://www.google.com/webhp?hl=es') | 333 self._NavigateAndWaitForBar('http://www.google.com/webhp?hl=es') |
| 357 self.WaitUntilTranslateComplete() | 334 self.WaitUntilTranslateComplete() |
| 358 # Assert that a translation was attempted. We don't care if it was error | 335 # Assert that a translation was attempted. We don't care if it was error |
| 359 # or success. | 336 # or success. |
| 360 self.assertNotEqual(self.before_translate, | 337 self.assertNotEqual(self.before_translate, |
| 361 self.GetTranslateInfo()['translate_bar']['bar_state']) | 338 self.GetTranslateInfo()['translate_bar']['bar_state']) |
| 362 | 339 |
| 340 def testNeverTranslateLanguageButton(self): | |
| 341 """Test the never translate language button.""" | |
| 342 spanish_url = self._GetDefaultSpanishURL() | |
| 343 self._NavigateAndWaitForBar(spanish_url) | |
| 344 | |
| 345 # The 'Never Translate' button doesn't show up until the user has clicked | |
| 346 # 'Nope' for a language 3 times. | |
| 347 for i in range(3): | |
|
Nirnimesh
2010/08/24 01:23:19
same comments as above
| |
| 348 self.SelectTranslateOption('decline_translation') | |
| 349 self.GetBrowserWindow(0).GetTab(0).Reload() | |
| 350 | |
| 351 # Click the 'Never Translate' button. | |
| 352 self.SelectTranslateOption('click_never_translate_lang_button') | |
| 353 # Navigate to another Spanish page and verify the page can't be translated. | |
| 354 # First navigate to a French page, wait for bar, navigate to Spanish page | |
| 355 # and wait for bar to go away. | |
| 356 self._NavigateAndWaitForBar('http://www.google.com/webhp?hl=fr') | |
| 357 self.NavigateToURL('http://www.google.com/webhp?hl=es') | |
| 358 self.WaitForInfobarCount(0) | |
| 359 self.assertFalse(self.GetTranslateInfo()['can_translate_page']) | |
| 360 | |
| 361 def testChangeTargetLanguageAlwaysTranslate(self): | |
| 362 """Tests that the change target language option works with always translate | |
| 363 on reload. | |
| 364 | |
| 365 This test is motivated by crbug.com/37313. | |
| 366 """ | |
| 367 self._NavigateAndWaitForBar(self._GetDefaultSpanishURL()) | |
| 368 self._ClickTranslateUntilSuccess() | |
| 369 # Select a different target language on translate info bar (French). | |
| 370 self.ChangeTranslateToLanguage('French') | |
| 371 translate_info = self.GetTranslateInfo() | |
| 372 self.assertTrue('translate_bar' in translate_info) | |
| 373 self.assertEqual('fr', translate_info['translate_bar']['target_lang_code']) | |
| 374 # Select always translate Spanish to French. | |
| 375 self.SelectTranslateOption('toggle_always_translate') | |
| 376 # Reload the page and assert that the page has been translated to French. | |
| 377 self.GetBrowserWindow(0).GetTab(0).Reload() | |
| 378 self.WaitUntilTranslateComplete() | |
| 379 translate_info = self.GetTranslateInfo() | |
| 380 self.assertTrue(translate_info['page_translated']) | |
| 381 self.assertTrue(translate_info['can_translate_page']) | |
| 382 self.assertTrue('translate_bar' in translate_info) | |
| 383 self.assertEqual('fr', translate_info['translate_bar']['target_lang_code']) | |
| 384 | |
| 363 def testSeveralLanguages(self): | 385 def testSeveralLanguages(self): |
| 364 """Verify translation for several languages. | 386 """Verify translation for several languages. |
| 365 | 387 |
| 366 This assumes that there is a directory of directories in the data dir. | 388 This assumes that there is a directory of directories in the data dir. |
| 367 The directory is called 'translate', and within that directory there are | 389 The directory is called 'translate', and within that directory there are |
| 368 subdirectories for each language code. Ex. a subdirectory 'es' with Spanish | 390 subdirectories for each language code. Ex. a subdirectory 'es' with Spanish |
| 369 html files. | 391 html files. |
| 370 """ | 392 """ |
| 371 corpora_path = os.path.join(self.DataDir(), 'translate') | 393 corpora_path = os.path.join(self.DataDir(), 'translate') |
| 372 corp_dir = glob.glob(os.path.join(corpora_path, '??')) + \ | 394 corp_dir = glob.glob(os.path.join(corpora_path, '??')) + \ |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 404 # Ref: http://boodebr.org/main/python/all-about-python-and-unicode | 426 # Ref: http://boodebr.org/main/python/all-about-python-and-unicode |
| 405 try: | 427 try: |
| 406 for filename in crazy_filenames: # filename is unicode. | 428 for filename in crazy_filenames: # filename is unicode. |
| 407 utf8_filename = filename.encode('utf-8') | 429 utf8_filename = filename.encode('utf-8') |
| 408 file_path = os.path.join(temp_dir, utf8_filename) | 430 file_path = os.path.join(temp_dir, utf8_filename) |
| 409 _CreateFile(os.path.join(temp_dir, filename)) # unicode file. | 431 _CreateFile(os.path.join(temp_dir, filename)) # unicode file. |
| 410 file_url = self.GetFileURLForPath(file_path) | 432 file_url = self.GetFileURLForPath(file_path) |
| 411 downloaded_file = os.path.join(download_dir, filename) | 433 downloaded_file = os.path.join(download_dir, filename) |
| 412 os.path.exists(downloaded_file) and os.remove(downloaded_file) | 434 os.path.exists(downloaded_file) and os.remove(downloaded_file) |
| 413 self.DownloadAndWaitForStart(file_url) | 435 self.DownloadAndWaitForStart(file_url) |
| 414 self.WaitForAllDownloadsToComplete() | 436 # Wait for files and remove them as we go. |
| 415 | 437 self.WaitForAllDownloadsToComplete() |
| 438 os.path.exists(downloaded_file) and os.remove(downloaded_file) | |
| 416 finally: | 439 finally: |
| 417 shutil.rmtree(unicode(temp_dir)) # unicode so that win treats nicely. | 440 shutil.rmtree(unicode(temp_dir)) # unicode so that win treats nicely. |
| 418 | 441 |
| 419 def testHistoryNotTranslated(self): | 442 def testHistoryNotTranslated(self): |
| 420 """Tests navigating to History page with other languages.""" | 443 """Tests navigating to History page with other languages.""" |
| 421 # Build the history with non-English content. | 444 # Build the history with non-English content. |
| 422 history_file = os.path.join( | 445 history_file = os.path.join( |
| 423 self.DataDir(), 'translate', 'crazy_history.txt') | 446 self.DataDir(), 'translate', 'crazy_history.txt') |
| 424 history_items = self.EvalDataFrom(history_file) | 447 history_items = self.EvalDataFrom(history_file) |
| 425 for history_item in history_items: | 448 for history_item in history_items: |
| 426 self.AddHistoryItem(history_item) | 449 self.AddHistoryItem(history_item) |
| 427 # Navigate to a page that triggers the translate bar so we can verify that | 450 # Navigate to a page that triggers the translate bar so we can verify that |
| 428 # it disappears on the history page. | 451 # it disappears on the history page. |
| 429 self._NavigateAndWaitForBar(self._GetDefaultSpanishURL()) | 452 self._NavigateAndWaitForBar(self._GetDefaultSpanishURL()) |
| 430 self.NavigateToURL('chrome://history/') | 453 self.NavigateToURL('chrome://history/') |
| 431 self.WaitForInfobarCount(0) | 454 self.WaitForInfobarCount(0) |
| 432 self.assertFalse('translate_bar' in self.GetTranslateInfo()) | 455 self.assertFalse('translate_bar' in self.GetTranslateInfo()) |
| 433 | 456 |
| 434 def testDownloadsNotTranslated(self): | 457 def testDownloadsNotTranslated(self): |
| 435 """Tests navigating to the Downloads page with other languages.""" | 458 """Tests navigating to the Downloads page with other languages.""" |
| 436 # Build the download history with non-English content. | 459 # Build the download history with non-English content. |
| 437 self._CreateCrazyDownloads() | 460 self._CreateCrazyDownloads() |
| 438 # Navigate to a page that triggers the translate bar so we can verify that | 461 # Navigate to a page that triggers the translate bar so we can verify that |
| 439 # it disappears on the downloads page. | 462 # it disappears on the downloads page. |
| 440 self._NavigateAndWaitForBar(self._GetDefaultSpanishURL()) | 463 self._NavigateAndWaitForBar(self._GetDefaultSpanishURL()) |
| 441 self.NavigateToURL('chrome://downloads/') | 464 self.NavigateToURL('chrome://downloads/') |
| 442 self.WaitForInfobarCount(0) | 465 self.WaitForInfobarCount(0) |
| 443 self.assertFalse('translate_bar' in self.GetTranslateInfo()) | 466 self.assertFalse('translate_bar' in self.GetTranslateInfo()) |
| 444 | 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.NavigateToURL(url, window_index, tab_index) | |
| 494 self.WaitForInfobarCount(1, windex=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 | |
| 445 | 511 |
| 446 if __name__ == '__main__': | 512 if __name__ == '__main__': |
| 447 pyauto_functional.Main() | 513 pyauto_functional.Main() |
| OLD | NEW |