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 | 9 |
10 import pyauto_functional # Must be imported before pyauto | 10 import pyauto_functional # Must be imported before pyauto |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 self.assertEquals(original_language, | 75 self.assertEquals(original_language, |
76 translate_bar['original_lang_code']) | 76 translate_bar['original_lang_code']) |
77 | 77 |
78 def testTranslate(self): | 78 def testTranslate(self): |
79 """Tests that a page was translated if the user chooses to translate.""" | 79 """Tests that a page was translated if the user chooses to translate.""" |
80 self._AssertTranslateWorks(self._GetDefaultSpanishURL(), self.spanish) | 80 self._AssertTranslateWorks(self._GetDefaultSpanishURL(), self.spanish) |
81 | 81 |
82 def testNoTranslate(self): | 82 def testNoTranslate(self): |
83 """Tests that a page isn't translated if the user declines translate.""" | 83 """Tests that a page isn't translated if the user declines translate.""" |
84 self._NavigateAndWaitForBar(self._GetDefaultSpanishURL()) | 84 self._NavigateAndWaitForBar(self._GetDefaultSpanishURL()) |
85 self.PerformActionOnInfobar('dismiss', 0) | 85 self.SelectTranslateOption('decline_translation') |
86 translate_info = self.GetTranslateInfo() | 86 translate_info = self.GetTranslateInfo() |
87 self.assertEqual(self.spanish, translate_info['original_language']) | 87 self.assertEqual(self.spanish, translate_info['original_language']) |
88 self.assertFalse(translate_info['page_translated']) | 88 self.assertFalse(translate_info['page_translated']) |
89 self.assertTrue(translate_info['can_translate_page']) | 89 self.assertTrue(translate_info['can_translate_page']) |
90 # If the user goes to the site again, the infobar should drop down but | 90 # If the user goes to the site again, the infobar should drop down but |
91 # the page should not be automatically translated. | 91 # the page should not be automatically translated. |
92 self._NavigateAndWaitForBar(self._GetDefaultSpanishURL()) | 92 self._NavigateAndWaitForBar(self._GetDefaultSpanishURL()) |
93 translate_info = self.GetTranslateInfo() | 93 translate_info = self.GetTranslateInfo() |
94 self.assertFalse(translate_info['page_translated']) | 94 self.assertFalse(translate_info['page_translated']) |
95 self.assertTrue(translate_info['can_translate_page']) | 95 self.assertTrue(translate_info['can_translate_page']) |
(...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
322 # Uncheck. | 322 # Uncheck. |
323 self.SetPrefs(pyauto.kEnableTranslate, False) | 323 self.SetPrefs(pyauto.kEnableTranslate, False) |
324 self.NavigateToURL(self._GetDefaultSpanishURL()) | 324 self.NavigateToURL(self._GetDefaultSpanishURL()) |
325 self.assertFalse('translate_bar' in self.GetTranslateInfo()) | 325 self.assertFalse('translate_bar' in self.GetTranslateInfo()) |
326 # Restart the browser and assert the translate preference stays. | 326 # Restart the browser and assert the translate preference stays. |
327 self.RestartBrowser(clear_profile=False) | 327 self.RestartBrowser(clear_profile=False) |
328 self.assertFalse(self.GetPrefsInfo().Prefs(pyauto.kEnableTranslate)) | 328 self.assertFalse(self.GetPrefsInfo().Prefs(pyauto.kEnableTranslate)) |
329 self.NavigateToURL(self._GetDefaultSpanishURL()) | 329 self.NavigateToURL(self._GetDefaultSpanishURL()) |
330 self.assertFalse('translate_bar' in self.GetTranslateInfo()) | 330 self.assertFalse('translate_bar' in self.GetTranslateInfo()) |
331 | 331 |
| 332 def testAlwaysTranslateLanguageButton(self): |
| 333 """Test the always translate language button.""" |
| 334 spanish_url = self._GetDefaultSpanishURL() |
| 335 self._NavigateAndWaitForBar(spanish_url) |
| 336 |
| 337 # The 'Always Translate' button doesn't show up until the user has clicked |
| 338 # 'Translate' for a language several times. |
| 339 max_tries = 10 |
| 340 curr_try = 0 |
| 341 while (curr_try < max_tries and |
| 342 not self.GetTranslateInfo()['translate_bar']\ |
| 343 ['always_translate_lang_button_showing']): |
| 344 self._ClickTranslateUntilSuccess() |
| 345 self._NavigateAndWaitForBar(spanish_url) |
| 346 curr_try = curr_try + 1 |
| 347 if curr_try == max_tries: |
| 348 self.fail('Clicked translate %d times and always translate button never '\ |
| 349 'showed up.' % max_tries) |
| 350 |
| 351 # Click the 'Always Translate' button. |
| 352 self.SelectTranslateOption('click_always_translate_lang_button') |
| 353 # Navigate to another Spanish page and verify it was translated. |
| 354 self._NavigateAndWaitForBar('http://www.google.com/webhp?hl=es') |
| 355 self.WaitUntilTranslateComplete() |
| 356 # Assert that a translation was attempted. We don't care if it was error |
| 357 # or success. |
| 358 self.assertNotEqual(self.before_translate, |
| 359 self.GetTranslateInfo()['translate_bar']['bar_state']) |
| 360 |
332 def testSeveralLanguages(self): | 361 def testSeveralLanguages(self): |
333 """Verify translation for several languages. | 362 """Verify translation for several languages. |
334 | 363 |
335 This assumes that there is a directory of directories in the data dir. | 364 This assumes that there is a directory of directories in the data dir. |
336 The directory is called 'translate', and within that directory there are | 365 The directory is called 'translate', and within that directory there are |
337 subdirectories for each language code. Ex. a subdirectory 'es' with Spanish | 366 subdirectories for each language code. Ex. a subdirectory 'es' with Spanish |
338 html files. | 367 html files. |
339 """ | 368 """ |
340 corpora_path = os.path.join(self.DataDir(), 'translate') | 369 corpora_path = os.path.join(self.DataDir(), 'translate') |
341 corp_dir = glob.glob(os.path.join(corpora_path, '??')) + \ | 370 corp_dir = glob.glob(os.path.join(corpora_path, '??')) + \ |
342 glob.glob(os.path.join(corpora_path, '??-??')) | 371 glob.glob(os.path.join(corpora_path, '??-??')) |
343 | 372 |
344 for language in corp_dir: | 373 for language in corp_dir: |
345 files = glob.glob(os.path.join(language, '*.html*')) | 374 files = glob.glob(os.path.join(language, '*.html*')) |
346 lang_code = os.path.basename(language) | 375 lang_code = os.path.basename(language) |
347 logging.debug('Starting language %s' % lang_code) | 376 logging.debug('Starting language %s' % lang_code) |
348 # Translate each html file in the language directory. | 377 # Translate each html file in the language directory. |
349 for lang_file in files: | 378 for lang_file in files: |
350 logging.debug('Starting file %s' % lang_file) | 379 logging.debug('Starting file %s' % lang_file) |
351 lang_file = self.GetFileURLForPath(lang_file) | 380 lang_file = self.GetFileURLForPath(lang_file) |
352 self._AssertTranslateWorks(lang_file, lang_code) | 381 self._AssertTranslateWorks(lang_file, lang_code) |
353 | 382 |
354 | 383 |
355 if __name__ == '__main__': | 384 if __name__ == '__main__': |
356 pyauto_functional.Main() | 385 pyauto_functional.Main() |
OLD | NEW |