| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 | 5 |
| 6 #include <algorithm> | 6 #include <algorithm> |
| 7 #include <set> | 7 #include <set> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 56 // for that navigation. | 56 // for that navigation. |
| 57 void SimulateNavigation(const GURL& url, | 57 void SimulateNavigation(const GURL& url, |
| 58 const std::string& lang, | 58 const std::string& lang, |
| 59 bool page_translatable) { | 59 bool page_translatable) { |
| 60 NavigateAndCommit(url); | 60 NavigateAndCommit(url); |
| 61 SimulateOnTranslateLanguageDetermined(lang, page_translatable); | 61 SimulateOnTranslateLanguageDetermined(lang, page_translatable); |
| 62 } | 62 } |
| 63 | 63 |
| 64 void SimulateOnTranslateLanguageDetermined(const std::string& lang, | 64 void SimulateOnTranslateLanguageDetermined(const std::string& lang, |
| 65 bool page_translatable) { | 65 bool page_translatable) { |
| 66 rvh()->TestOnMessageReceived(ViewHostMsg_TranslateLanguageDetermined( | 66 rvh()->TestOnMessageReceived(ChromeViewHostMsg_TranslateLanguageDetermined( |
| 67 0, lang, page_translatable)); | 67 0, lang, page_translatable)); |
| 68 } | 68 } |
| 69 | 69 |
| 70 bool GetTranslateMessage(int* page_id, | 70 bool GetTranslateMessage(int* page_id, |
| 71 std::string* original_lang, | 71 std::string* original_lang, |
| 72 std::string* target_lang) { | 72 std::string* target_lang) { |
| 73 const IPC::Message* message = | 73 const IPC::Message* message = |
| 74 process()->sink().GetFirstMessageMatching(ViewMsg_TranslatePage::ID); | 74 process()->sink().GetFirstMessageMatching( |
| 75 ChromeViewMsg_TranslatePage::ID); |
| 75 if (!message) | 76 if (!message) |
| 76 return false; | 77 return false; |
| 77 Tuple4<int, std::string, std::string, std::string> translate_param; | 78 Tuple4<int, std::string, std::string, std::string> translate_param; |
| 78 ViewMsg_TranslatePage::Read(message, &translate_param); | 79 ChromeViewMsg_TranslatePage::Read(message, &translate_param); |
| 79 if (page_id) | 80 if (page_id) |
| 80 *page_id = translate_param.a; | 81 *page_id = translate_param.a; |
| 81 // Ignore translate_param.b which is the script injected in the page. | 82 // Ignore translate_param.b which is the script injected in the page. |
| 82 if (original_lang) | 83 if (original_lang) |
| 83 *original_lang = translate_param.c; | 84 *original_lang = translate_param.c; |
| 84 if (target_lang) | 85 if (target_lang) |
| 85 *target_lang = translate_param.d; | 86 *target_lang = translate_param.d; |
| 86 return true; | 87 return true; |
| 87 } | 88 } |
| 88 | 89 |
| (...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 333 SimulateTranslateScriptURLFetch(true); | 334 SimulateTranslateScriptURLFetch(true); |
| 334 | 335 |
| 335 // Test that we sent the right message to the renderer. | 336 // Test that we sent the right message to the renderer. |
| 336 int page_id = 0; | 337 int page_id = 0; |
| 337 std::string original_lang, target_lang; | 338 std::string original_lang, target_lang; |
| 338 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); | 339 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); |
| 339 EXPECT_EQ("fr", original_lang); | 340 EXPECT_EQ("fr", original_lang); |
| 340 EXPECT_EQ("en", target_lang); | 341 EXPECT_EQ("en", target_lang); |
| 341 | 342 |
| 342 // Simulate the render notifying the translation has been done. | 343 // Simulate the render notifying the translation has been done. |
| 343 rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(0, 0, "fr", "en", | 344 rvh()->TestOnMessageReceived(ChromeViewHostMsg_PageTranslated( |
| 344 TranslateErrors::NONE)); | 345 0, 0, "fr", "en", TranslateErrors::NONE)); |
| 345 | 346 |
| 346 // The after translate infobar should be showing. | 347 // The after translate infobar should be showing. |
| 347 infobar = GetTranslateInfoBar(); | 348 infobar = GetTranslateInfoBar(); |
| 348 ASSERT_TRUE(infobar != NULL); | 349 ASSERT_TRUE(infobar != NULL); |
| 349 EXPECT_EQ(TranslateInfoBarDelegate::AFTER_TRANSLATE, infobar->type()); | 350 EXPECT_EQ(TranslateInfoBarDelegate::AFTER_TRANSLATE, infobar->type()); |
| 350 | 351 |
| 351 // Simulate changing the original language, this should trigger a translation. | 352 // Simulate changing the original language, this should trigger a translation. |
| 352 process()->sink().ClearMessages(); | 353 process()->sink().ClearMessages(); |
| 353 std::string new_original_lang = infobar->GetLanguageCodeAt(0); | 354 std::string new_original_lang = infobar->GetLanguageCodeAt(0); |
| 354 infobar->SetOriginalLanguage(0); | 355 infobar->SetOriginalLanguage(0); |
| 355 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); | 356 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); |
| 356 EXPECT_EQ(new_original_lang, original_lang); | 357 EXPECT_EQ(new_original_lang, original_lang); |
| 357 EXPECT_EQ("en", target_lang); | 358 EXPECT_EQ("en", target_lang); |
| 358 // Simulate the render notifying the translation has been done. | 359 // Simulate the render notifying the translation has been done. |
| 359 rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(0, 0, | 360 rvh()->TestOnMessageReceived(ChromeViewHostMsg_PageTranslated( |
| 360 new_original_lang, "en", TranslateErrors::NONE)); | 361 0, 0, new_original_lang, "en", TranslateErrors::NONE)); |
| 361 // infobar is now invalid. | 362 // infobar is now invalid. |
| 362 TranslateInfoBarDelegate* new_infobar = GetTranslateInfoBar(); | 363 TranslateInfoBarDelegate* new_infobar = GetTranslateInfoBar(); |
| 363 ASSERT_TRUE(new_infobar != NULL); | 364 ASSERT_TRUE(new_infobar != NULL); |
| 364 infobar = new_infobar; | 365 infobar = new_infobar; |
| 365 | 366 |
| 366 // Simulate changing the target language, this should trigger a translation. | 367 // Simulate changing the target language, this should trigger a translation. |
| 367 process()->sink().ClearMessages(); | 368 process()->sink().ClearMessages(); |
| 368 std::string new_target_lang = infobar->GetLanguageCodeAt(1); | 369 std::string new_target_lang = infobar->GetLanguageCodeAt(1); |
| 369 infobar->SetTargetLanguage(1); | 370 infobar->SetTargetLanguage(1); |
| 370 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); | 371 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); |
| 371 EXPECT_EQ(new_original_lang, original_lang); | 372 EXPECT_EQ(new_original_lang, original_lang); |
| 372 EXPECT_EQ(new_target_lang, target_lang); | 373 EXPECT_EQ(new_target_lang, target_lang); |
| 373 // Simulate the render notifying the translation has been done. | 374 // Simulate the render notifying the translation has been done. |
| 374 rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(0, 0, | 375 rvh()->TestOnMessageReceived(ChromeViewHostMsg_PageTranslated(0, 0, |
| 375 new_original_lang, new_target_lang, TranslateErrors::NONE)); | 376 new_original_lang, new_target_lang, TranslateErrors::NONE)); |
| 376 // infobar is now invalid. | 377 // infobar is now invalid. |
| 377 new_infobar = GetTranslateInfoBar(); | 378 new_infobar = GetTranslateInfoBar(); |
| 378 ASSERT_TRUE(new_infobar != NULL); | 379 ASSERT_TRUE(new_infobar != NULL); |
| 379 } | 380 } |
| 380 | 381 |
| 381 TEST_F(TranslateManagerTest, TranslateScriptNotAvailable) { | 382 TEST_F(TranslateManagerTest, TranslateScriptNotAvailable) { |
| 382 // Simulate navigating to a page. | 383 // Simulate navigating to a page. |
| 383 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); | 384 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); |
| 384 | 385 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 423 SimulateTranslateScriptURLFetch(false); | 424 SimulateTranslateScriptURLFetch(false); |
| 424 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); | 425 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); |
| 425 ASSERT_TRUE(infobar != NULL); | 426 ASSERT_TRUE(infobar != NULL); |
| 426 EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATION_ERROR, infobar->type()); | 427 EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATION_ERROR, infobar->type()); |
| 427 EXPECT_TRUE(infobar->IsError()); | 428 EXPECT_TRUE(infobar->IsError()); |
| 428 infobar->MessageInfoBarButtonPressed(); | 429 infobar->MessageInfoBarButtonPressed(); |
| 429 SimulateTranslateScriptURLFetch(true); // This time succeed. | 430 SimulateTranslateScriptURLFetch(true); // This time succeed. |
| 430 | 431 |
| 431 // Simulate the render notifying the translation has been done, the server | 432 // Simulate the render notifying the translation has been done, the server |
| 432 // having detected the page was in a known and supported language. | 433 // having detected the page was in a known and supported language. |
| 433 rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(0, 0, "fr", "en", | 434 rvh()->TestOnMessageReceived(ChromeViewHostMsg_PageTranslated( |
| 434 TranslateErrors::NONE)); | 435 0, 0, "fr", "en", TranslateErrors::NONE)); |
| 435 | 436 |
| 436 // The after translate infobar should be showing. | 437 // The after translate infobar should be showing. |
| 437 infobar = GetTranslateInfoBar(); | 438 infobar = GetTranslateInfoBar(); |
| 438 ASSERT_TRUE(infobar != NULL); | 439 ASSERT_TRUE(infobar != NULL); |
| 439 EXPECT_EQ(TranslateInfoBarDelegate::AFTER_TRANSLATE, infobar->type()); | 440 EXPECT_EQ(TranslateInfoBarDelegate::AFTER_TRANSLATE, infobar->type()); |
| 440 EXPECT_EQ("fr", infobar->GetOriginalLanguageCode()); | 441 EXPECT_EQ("fr", infobar->GetOriginalLanguageCode()); |
| 441 EXPECT_EQ("en", infobar->GetTargetLanguageCode()); | 442 EXPECT_EQ("en", infobar->GetTargetLanguageCode()); |
| 442 | 443 |
| 443 // Let's run the same steps but this time the server detects the page is | 444 // Let's run the same steps but this time the server detects the page is |
| 444 // already in English. | 445 // already in English. |
| 445 SimulateNavigation(GURL("http://www.google.com"), "und", true); | 446 SimulateNavigation(GURL("http://www.google.com"), "und", true); |
| 446 menu.reset(TestRenderViewContextMenu::CreateContextMenu(contents())); | 447 menu.reset(TestRenderViewContextMenu::CreateContextMenu(contents())); |
| 447 menu->Init(); | 448 menu->Init(); |
| 448 menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE); | 449 menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE); |
| 449 rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(1, 0, "en", "en", | 450 rvh()->TestOnMessageReceived(ChromeViewHostMsg_PageTranslated( |
| 450 TranslateErrors::IDENTICAL_LANGUAGES)); | 451 1, 0, "en", "en", TranslateErrors::IDENTICAL_LANGUAGES)); |
| 451 infobar = GetTranslateInfoBar(); | 452 infobar = GetTranslateInfoBar(); |
| 452 ASSERT_TRUE(infobar != NULL); | 453 ASSERT_TRUE(infobar != NULL); |
| 453 EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATION_ERROR, infobar->type()); | 454 EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATION_ERROR, infobar->type()); |
| 454 EXPECT_EQ(TranslateErrors::IDENTICAL_LANGUAGES, infobar->error()); | 455 EXPECT_EQ(TranslateErrors::IDENTICAL_LANGUAGES, infobar->error()); |
| 455 | 456 |
| 456 // Let's run the same steps again but this time the server fails to detect the | 457 // Let's run the same steps again but this time the server fails to detect the |
| 457 // page's language (it returns an empty string). | 458 // page's language (it returns an empty string). |
| 458 SimulateNavigation(GURL("http://www.google.com"), "und", true); | 459 SimulateNavigation(GURL("http://www.google.com"), "und", true); |
| 459 menu.reset(TestRenderViewContextMenu::CreateContextMenu(contents())); | 460 menu.reset(TestRenderViewContextMenu::CreateContextMenu(contents())); |
| 460 menu->Init(); | 461 menu->Init(); |
| 461 menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE); | 462 menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE); |
| 462 rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(2, 0, "", "en", | 463 rvh()->TestOnMessageReceived(ChromeViewHostMsg_PageTranslated( |
| 463 TranslateErrors::UNKNOWN_LANGUAGE)); | 464 2, 0, "", "en", TranslateErrors::UNKNOWN_LANGUAGE)); |
| 464 infobar = GetTranslateInfoBar(); | 465 infobar = GetTranslateInfoBar(); |
| 465 ASSERT_TRUE(infobar != NULL); | 466 ASSERT_TRUE(infobar != NULL); |
| 466 EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATION_ERROR, infobar->type()); | 467 EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATION_ERROR, infobar->type()); |
| 467 EXPECT_EQ(TranslateErrors::UNKNOWN_LANGUAGE, infobar->error()); | 468 EXPECT_EQ(TranslateErrors::UNKNOWN_LANGUAGE, infobar->error()); |
| 468 } | 469 } |
| 469 | 470 |
| 470 // Tests that we show/don't show an info-bar for all languages the CLD can | 471 // Tests that we show/don't show an info-bar for all languages the CLD can |
| 471 // report. | 472 // report. |
| 472 TEST_F(TranslateManagerTest, TestAllLanguages) { | 473 TEST_F(TranslateManagerTest, TestAllLanguages) { |
| 473 // The index in kExpectation are the Language enum (see languages.pb.h). | 474 // The index in kExpectation are the Language enum (see languages.pb.h). |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 594 // Simulate navigating to a page and getting its language. | 595 // Simulate navigating to a page and getting its language. |
| 595 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); | 596 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); |
| 596 | 597 |
| 597 // Simulate the user translating. | 598 // Simulate the user translating. |
| 598 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); | 599 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); |
| 599 ASSERT_TRUE(infobar != NULL); | 600 ASSERT_TRUE(infobar != NULL); |
| 600 infobar->Translate(); | 601 infobar->Translate(); |
| 601 // Simulate the translate script being retrieved. | 602 // Simulate the translate script being retrieved. |
| 602 SimulateTranslateScriptURLFetch(true); | 603 SimulateTranslateScriptURLFetch(true); |
| 603 | 604 |
| 604 rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(0, 0, "fr", "en", | 605 rvh()->TestOnMessageReceived(ChromeViewHostMsg_PageTranslated( |
| 605 TranslateErrors::NONE)); | 606 0, 0, "fr", "en", TranslateErrors::NONE)); |
| 606 | 607 |
| 607 // Now navigate to a new page in the same language. | 608 // Now navigate to a new page in the same language. |
| 608 process()->sink().ClearMessages(); | 609 process()->sink().ClearMessages(); |
| 609 SimulateNavigation(GURL("http://news.google.fr"), "fr", true); | 610 SimulateNavigation(GURL("http://news.google.fr"), "fr", true); |
| 610 | 611 |
| 611 // This should have automatically triggered a translation. | 612 // This should have automatically triggered a translation. |
| 612 int page_id = 0; | 613 int page_id = 0; |
| 613 std::string original_lang, target_lang; | 614 std::string original_lang, target_lang; |
| 614 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); | 615 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); |
| 615 EXPECT_EQ(1, page_id); | 616 EXPECT_EQ(1, page_id); |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 766 TEST_F(TranslateManagerTest, TranslateCloseInfoBarInPageNavigation) { | 767 TEST_F(TranslateManagerTest, TranslateCloseInfoBarInPageNavigation) { |
| 767 // Simulate navigating to a page and getting its language. | 768 // Simulate navigating to a page and getting its language. |
| 768 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); | 769 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); |
| 769 | 770 |
| 770 // Simulate the user translating. | 771 // Simulate the user translating. |
| 771 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); | 772 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); |
| 772 ASSERT_TRUE(infobar != NULL); | 773 ASSERT_TRUE(infobar != NULL); |
| 773 infobar->Translate(); | 774 infobar->Translate(); |
| 774 // Simulate the translate script being retrieved. | 775 // Simulate the translate script being retrieved. |
| 775 SimulateTranslateScriptURLFetch(true); | 776 SimulateTranslateScriptURLFetch(true); |
| 776 rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(0, 0, "fr", "en", | 777 rvh()->TestOnMessageReceived(ChromeViewHostMsg_PageTranslated( |
| 777 TranslateErrors::NONE)); | 778 0, 0, "fr", "en", TranslateErrors::NONE)); |
| 778 | 779 |
| 779 // Close the infobar. | 780 // Close the infobar. |
| 780 EXPECT_TRUE(CloseTranslateInfoBar()); | 781 EXPECT_TRUE(CloseTranslateInfoBar()); |
| 781 | 782 |
| 782 // Navigate in page, no infobar should be shown. | 783 // Navigate in page, no infobar should be shown. |
| 783 SimulateNavigation(GURL("http://www.google.fr/#ref1"), "fr", true); | 784 SimulateNavigation(GURL("http://www.google.fr/#ref1"), "fr", true); |
| 784 EXPECT_TRUE(GetTranslateInfoBar() == NULL); | 785 EXPECT_TRUE(GetTranslateInfoBar() == NULL); |
| 785 | 786 |
| 786 // Navigate out of page, a new infobar should show. | 787 // Navigate out of page, a new infobar should show. |
| 787 // Note that we navigate to a page in a different language so we don't trigger | 788 // Note that we navigate to a page in a different language so we don't trigger |
| 788 // the auto-translate feature (it would translate the page automatically and | 789 // the auto-translate feature (it would translate the page automatically and |
| 789 // the before translate inforbar would not be shown). | 790 // the before translate inforbar would not be shown). |
| 790 SimulateNavigation(GURL("http://www.google.de"), "de", true); | 791 SimulateNavigation(GURL("http://www.google.de"), "de", true); |
| 791 EXPECT_TRUE(GetTranslateInfoBar() != NULL); | 792 EXPECT_TRUE(GetTranslateInfoBar() != NULL); |
| 792 } | 793 } |
| 793 | 794 |
| 794 // Tests that the after translate the infobar still shows when navigating | 795 // Tests that the after translate the infobar still shows when navigating |
| 795 // in-page. | 796 // in-page. |
| 796 TEST_F(TranslateManagerTest, TranslateInPageNavigation) { | 797 TEST_F(TranslateManagerTest, TranslateInPageNavigation) { |
| 797 // Simulate navigating to a page and getting its language. | 798 // Simulate navigating to a page and getting its language. |
| 798 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); | 799 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); |
| 799 | 800 |
| 800 // Simulate the user translating. | 801 // Simulate the user translating. |
| 801 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); | 802 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); |
| 802 ASSERT_TRUE(infobar != NULL); | 803 ASSERT_TRUE(infobar != NULL); |
| 803 infobar->Translate(); | 804 infobar->Translate(); |
| 804 // Simulate the translate script being retrieved. | 805 // Simulate the translate script being retrieved. |
| 805 SimulateTranslateScriptURLFetch(true); | 806 SimulateTranslateScriptURLFetch(true); |
| 806 rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(0, 0, "fr", "en", | 807 rvh()->TestOnMessageReceived(ChromeViewHostMsg_PageTranslated( |
| 807 TranslateErrors::NONE)); | 808 0, 0, "fr", "en", TranslateErrors::NONE)); |
| 808 // The after translate infobar is showing. | 809 // The after translate infobar is showing. |
| 809 infobar = GetTranslateInfoBar(); | 810 infobar = GetTranslateInfoBar(); |
| 810 ASSERT_TRUE(infobar != NULL); | 811 ASSERT_TRUE(infobar != NULL); |
| 811 | 812 |
| 812 // Navigate out of page, a new infobar should show. | 813 // Navigate out of page, a new infobar should show. |
| 813 // See note in TranslateCloseInfoBarInPageNavigation test on why it is | 814 // See note in TranslateCloseInfoBarInPageNavigation test on why it is |
| 814 // important to navigate to a page in a different language for this test. | 815 // important to navigate to a page in a different language for this test. |
| 815 SimulateNavigation(GURL("http://www.google.de"), "de", true); | 816 SimulateNavigation(GURL("http://www.google.de"), "de", true); |
| 816 // The old infobar is gone. | 817 // The old infobar is gone. |
| 817 EXPECT_TRUE(CheckInfoBarRemovedAndReset(infobar)); | 818 EXPECT_TRUE(CheckInfoBarRemovedAndReset(infobar)); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 835 TEST_F(TranslateManagerTest, ServerReportsUnsupportedLanguage) { | 836 TEST_F(TranslateManagerTest, ServerReportsUnsupportedLanguage) { |
| 836 // Simulate navigating to a page and translating it. | 837 // Simulate navigating to a page and translating it. |
| 837 SimulateNavigation(GURL("http://mail.google.fr"), "fr", true); | 838 SimulateNavigation(GURL("http://mail.google.fr"), "fr", true); |
| 838 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); | 839 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); |
| 839 ASSERT_TRUE(infobar != NULL); | 840 ASSERT_TRUE(infobar != NULL); |
| 840 process()->sink().ClearMessages(); | 841 process()->sink().ClearMessages(); |
| 841 infobar->Translate(); | 842 infobar->Translate(); |
| 842 SimulateTranslateScriptURLFetch(true); | 843 SimulateTranslateScriptURLFetch(true); |
| 843 // Simulate the render notifying the translation has been done, but it | 844 // Simulate the render notifying the translation has been done, but it |
| 844 // reports a language we don't support. | 845 // reports a language we don't support. |
| 845 rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(0, 0, "qbz", "en", | 846 rvh()->TestOnMessageReceived(ChromeViewHostMsg_PageTranslated( |
| 846 TranslateErrors::NONE)); | 847 0, 0, "qbz", "en", TranslateErrors::NONE)); |
| 847 | 848 |
| 848 // An error infobar should be showing to report that we don't support this | 849 // An error infobar should be showing to report that we don't support this |
| 849 // language. | 850 // language. |
| 850 infobar = GetTranslateInfoBar(); | 851 infobar = GetTranslateInfoBar(); |
| 851 ASSERT_TRUE(infobar != NULL); | 852 ASSERT_TRUE(infobar != NULL); |
| 852 EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATION_ERROR, infobar->type()); | 853 EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATION_ERROR, infobar->type()); |
| 853 | 854 |
| 854 // This infobar should have a button (so the string should not be empty). | 855 // This infobar should have a button (so the string should not be empty). |
| 855 ASSERT_FALSE(infobar->GetMessageInfoBarButtonText().empty()); | 856 ASSERT_FALSE(infobar->GetMessageInfoBarButtonText().empty()); |
| 856 | 857 |
| 857 // Pressing the button on that infobar should revert to the original language. | 858 // Pressing the button on that infobar should revert to the original language. |
| 858 process()->sink().ClearMessages(); | 859 process()->sink().ClearMessages(); |
| 859 infobar->MessageInfoBarButtonPressed(); | 860 infobar->MessageInfoBarButtonPressed(); |
| 860 const IPC::Message* message = | 861 const IPC::Message* message = |
| 861 process()->sink().GetFirstMessageMatching(ViewMsg_RevertTranslation::ID); | 862 process()->sink().GetFirstMessageMatching( |
| 863 ChromeViewMsg_RevertTranslation::ID); |
| 862 EXPECT_TRUE(message != NULL); | 864 EXPECT_TRUE(message != NULL); |
| 863 // And it should have removed the infobar. | 865 // And it should have removed the infobar. |
| 864 EXPECT_TRUE(GetTranslateInfoBar() == NULL); | 866 EXPECT_TRUE(GetTranslateInfoBar() == NULL); |
| 865 } | 867 } |
| 866 | 868 |
| 867 // Tests that no translate infobar is shown when Chrome is in a language that | 869 // Tests that no translate infobar is shown when Chrome is in a language that |
| 868 // the translate server does not support. | 870 // the translate server does not support. |
| 869 TEST_F(TranslateManagerTest, UnsupportedUILanguage) { | 871 TEST_F(TranslateManagerTest, UnsupportedUILanguage) { |
| 870 TestingBrowserProcess* browser_process = testing_browser_process_.get(); | 872 TestingBrowserProcess* browser_process = testing_browser_process_.get(); |
| 871 std::string original_lang = browser_process->GetApplicationLocale(); | 873 std::string original_lang = browser_process->GetApplicationLocale(); |
| (...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1140 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); | 1142 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); |
| 1141 EXPECT_EQ("fr", original_lang); | 1143 EXPECT_EQ("fr", original_lang); |
| 1142 EXPECT_EQ("en", target_lang); | 1144 EXPECT_EQ("en", target_lang); |
| 1143 process()->sink().ClearMessages(); | 1145 process()->sink().ClearMessages(); |
| 1144 | 1146 |
| 1145 // This should also have reverted the blacklisting of this site and language. | 1147 // This should also have reverted the blacklisting of this site and language. |
| 1146 EXPECT_FALSE(translate_prefs.IsLanguageBlacklisted("fr")); | 1148 EXPECT_FALSE(translate_prefs.IsLanguageBlacklisted("fr")); |
| 1147 EXPECT_FALSE(translate_prefs.IsSiteBlacklisted(url.host())); | 1149 EXPECT_FALSE(translate_prefs.IsSiteBlacklisted(url.host())); |
| 1148 | 1150 |
| 1149 // Let's simulate the page being translated. | 1151 // Let's simulate the page being translated. |
| 1150 rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(0, 0, "fr", "en", | 1152 rvh()->TestOnMessageReceived(ChromeViewHostMsg_PageTranslated( |
| 1151 TranslateErrors::NONE)); | 1153 0, 0, "fr", "en", TranslateErrors::NONE)); |
| 1152 | 1154 |
| 1153 // The translate menu should now be disabled. | 1155 // The translate menu should now be disabled. |
| 1154 menu.reset(TestRenderViewContextMenu::CreateContextMenu(contents())); | 1156 menu.reset(TestRenderViewContextMenu::CreateContextMenu(contents())); |
| 1155 menu->Init(); | 1157 menu->Init(); |
| 1156 EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE)); | 1158 EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE)); |
| 1157 EXPECT_FALSE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE)); | 1159 EXPECT_FALSE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE)); |
| 1158 | 1160 |
| 1159 // Test that selecting translate in the context menu WHILE the page is being | 1161 // Test that selecting translate in the context menu WHILE the page is being |
| 1160 // translated does nothing (this could happen if autotranslate kicks-in and | 1162 // translated does nothing (this could happen if autotranslate kicks-in and |
| 1161 // the user selects the menu while the translation is being performed). | 1163 // the user selects the menu while the translation is being performed). |
| (...skipping 14 matching lines...) Expand all Loading... |
| 1176 // been translated does nothing. | 1178 // been translated does nothing. |
| 1177 SimulateNavigation(GURL("http://www.google.de"), "de", true); | 1179 SimulateNavigation(GURL("http://www.google.de"), "de", true); |
| 1178 infobar = GetTranslateInfoBar(); | 1180 infobar = GetTranslateInfoBar(); |
| 1179 ASSERT_TRUE(infobar != NULL); | 1181 ASSERT_TRUE(infobar != NULL); |
| 1180 infobar->Translate(); | 1182 infobar->Translate(); |
| 1181 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); | 1183 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); |
| 1182 process()->sink().ClearMessages(); | 1184 process()->sink().ClearMessages(); |
| 1183 menu.reset(TestRenderViewContextMenu::CreateContextMenu(contents())); | 1185 menu.reset(TestRenderViewContextMenu::CreateContextMenu(contents())); |
| 1184 menu->Init(); | 1186 menu->Init(); |
| 1185 EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE)); | 1187 EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE)); |
| 1186 rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(0, 0, "de", "en", | 1188 rvh()->TestOnMessageReceived(ChromeViewHostMsg_PageTranslated( |
| 1187 TranslateErrors::NONE)); | 1189 0, 0, "de", "en", TranslateErrors::NONE)); |
| 1188 menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE); | 1190 menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE); |
| 1189 // No message expected since the translation should have been ignored. | 1191 // No message expected since the translation should have been ignored. |
| 1190 EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); | 1192 EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); |
| 1191 | 1193 |
| 1192 // Test that the translate context menu is enabled when the page is in an | 1194 // Test that the translate context menu is enabled when the page is in an |
| 1193 // unknown language. | 1195 // unknown language. |
| 1194 SimulateNavigation(url, "und", true); | 1196 SimulateNavigation(url, "und", true); |
| 1195 menu.reset(TestRenderViewContextMenu::CreateContextMenu(contents())); | 1197 menu.reset(TestRenderViewContextMenu::CreateContextMenu(contents())); |
| 1196 menu->Init(); | 1198 menu->Init(); |
| 1197 EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE)); | 1199 EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE)); |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1301 TEST_F(TranslateManagerTest, ScriptExpires) { | 1303 TEST_F(TranslateManagerTest, ScriptExpires) { |
| 1302 ExpireTranslateScriptImmediately(); | 1304 ExpireTranslateScriptImmediately(); |
| 1303 | 1305 |
| 1304 // Simulate navigating to a page and translating it. | 1306 // Simulate navigating to a page and translating it. |
| 1305 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); | 1307 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); |
| 1306 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); | 1308 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); |
| 1307 ASSERT_TRUE(infobar != NULL); | 1309 ASSERT_TRUE(infobar != NULL); |
| 1308 process()->sink().ClearMessages(); | 1310 process()->sink().ClearMessages(); |
| 1309 infobar->Translate(); | 1311 infobar->Translate(); |
| 1310 SimulateTranslateScriptURLFetch(true); | 1312 SimulateTranslateScriptURLFetch(true); |
| 1311 rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(0, 0, "fr", "en", | 1313 rvh()->TestOnMessageReceived(ChromeViewHostMsg_PageTranslated( |
| 1312 TranslateErrors::NONE)); | 1314 0, 0, "fr", "en", TranslateErrors::NONE)); |
| 1313 | 1315 |
| 1314 // A task should have been posted to clear the script, run it. | 1316 // A task should have been posted to clear the script, run it. |
| 1315 MessageLoop::current()->RunAllPending(); | 1317 MessageLoop::current()->RunAllPending(); |
| 1316 | 1318 |
| 1317 // Do another navigation and translation. | 1319 // Do another navigation and translation. |
| 1318 SimulateNavigation(GURL("http://www.google.es"), "es", true); | 1320 SimulateNavigation(GURL("http://www.google.es"), "es", true); |
| 1319 infobar = GetTranslateInfoBar(); | 1321 infobar = GetTranslateInfoBar(); |
| 1320 ASSERT_TRUE(infobar != NULL); | 1322 ASSERT_TRUE(infobar != NULL); |
| 1321 process()->sink().ClearMessages(); | 1323 process()->sink().ClearMessages(); |
| 1322 infobar->Translate(); | 1324 infobar->Translate(); |
| 1323 // If we don't simulate the URL fetch, the TranslateManager should be waiting | 1325 // If we don't simulate the URL fetch, the TranslateManager should be waiting |
| 1324 // for the script and no message should have been sent to the renderer. | 1326 // for the script and no message should have been sent to the renderer. |
| 1325 EXPECT_TRUE( | 1327 EXPECT_TRUE( |
| 1326 process()->sink().GetFirstMessageMatching(ViewMsg_TranslatePage::ID) == | 1328 process()->sink().GetFirstMessageMatching( |
| 1327 NULL); | 1329 ChromeViewMsg_TranslatePage::ID) == |
| 1330 NULL); |
| 1328 // Now simulate the URL fetch. | 1331 // Now simulate the URL fetch. |
| 1329 SimulateTranslateScriptURLFetch(true); | 1332 SimulateTranslateScriptURLFetch(true); |
| 1330 // Now the message should have been sent. | 1333 // Now the message should have been sent. |
| 1331 int page_id = 0; | 1334 int page_id = 0; |
| 1332 std::string original_lang, target_lang; | 1335 std::string original_lang, target_lang; |
| 1333 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); | 1336 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); |
| 1334 EXPECT_EQ("es", original_lang); | 1337 EXPECT_EQ("es", original_lang); |
| 1335 EXPECT_EQ("en", target_lang); | 1338 EXPECT_EQ("en", target_lang); |
| 1336 } | 1339 } |
| OLD | NEW |