| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/json/json_writer.h" | 10 #include "base/json/json_writer.h" |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 #include "chrome/test/base/testing_profile.h" | 30 #include "chrome/test/base/testing_profile.h" |
| 31 #include "content/browser/renderer_host/mock_render_process_host.h" | 31 #include "content/browser/renderer_host/mock_render_process_host.h" |
| 32 #include "content/browser/tab_contents/test_tab_contents.h" | 32 #include "content/browser/tab_contents/test_tab_contents.h" |
| 33 #include "content/public/browser/navigation_details.h" | 33 #include "content/public/browser/navigation_details.h" |
| 34 #include "content/public/browser/navigation_entry.h" | 34 #include "content/public/browser/navigation_entry.h" |
| 35 #include "content/public/browser/notification_details.h" | 35 #include "content/public/browser/notification_details.h" |
| 36 #include "content/public/browser/notification_registrar.h" | 36 #include "content/public/browser/notification_registrar.h" |
| 37 #include "content/test/notification_observer_mock.h" | 37 #include "content/test/notification_observer_mock.h" |
| 38 #include "content/test/render_view_test.h" | 38 #include "content/test/render_view_test.h" |
| 39 #include "content/test/test_browser_thread.h" | 39 #include "content/test/test_browser_thread.h" |
| 40 #include "content/test/test_renderer_host.h" |
| 40 #include "content/test/test_url_fetcher_factory.h" | 41 #include "content/test/test_url_fetcher_factory.h" |
| 42 #include "content/test/test_renderer_host.h" |
| 41 #include "grit/generated_resources.h" | 43 #include "grit/generated_resources.h" |
| 42 #include "ipc/ipc_test_sink.h" | 44 #include "ipc/ipc_test_sink.h" |
| 43 #include "testing/gmock/include/gmock/gmock.h" | 45 #include "testing/gmock/include/gmock/gmock.h" |
| 44 #include "third_party/WebKit/Source/WebKit/chromium/public/WebContextMenuData.h" | 46 #include "third_party/WebKit/Source/WebKit/chromium/public/WebContextMenuData.h" |
| 45 #include "third_party/WebKit/Source/WebKit/chromium/public/WebKit.h" | 47 #include "third_party/WebKit/Source/WebKit/chromium/public/WebKit.h" |
| 46 #include "third_party/cld/languages/public/languages.h" | 48 #include "third_party/cld/languages/public/languages.h" |
| 47 | 49 |
| 48 using content::BrowserThread; | 50 using content::BrowserThread; |
| 49 using content::NavigationController; | 51 using content::NavigationController; |
| 52 using content::RenderViewHostTester; |
| 50 using content::WebContents; | 53 using content::WebContents; |
| 51 using testing::_; | 54 using testing::_; |
| 52 using testing::Pointee; | 55 using testing::Pointee; |
| 53 using testing::Property; | 56 using testing::Property; |
| 54 using WebKit::WebContextMenuData; | 57 using WebKit::WebContextMenuData; |
| 55 | 58 |
| 56 class TranslateManagerTest : public TabContentsWrapperTestHarness, | 59 class TranslateManagerTest : public TabContentsWrapperTestHarness, |
| 57 public content::NotificationObserver { | 60 public content::NotificationObserver { |
| 58 public: | 61 public: |
| 59 TranslateManagerTest() | 62 TranslateManagerTest() |
| 60 : ui_thread_(BrowserThread::UI, &message_loop_) { | 63 : ui_thread_(BrowserThread::UI, &message_loop_) { |
| 61 } | 64 } |
| 62 | 65 |
| 63 // Simulates navigating to a page and getting the page contents and language | 66 // Simulates navigating to a page and getting the page contents and language |
| 64 // for that navigation. | 67 // for that navigation. |
| 65 void SimulateNavigation(const GURL& url, | 68 void SimulateNavigation(const GURL& url, |
| 66 const std::string& lang, | 69 const std::string& lang, |
| 67 bool page_translatable) { | 70 bool page_translatable) { |
| 68 NavigateAndCommit(url); | 71 NavigateAndCommit(url); |
| 69 SimulateOnTranslateLanguageDetermined(lang, page_translatable); | 72 SimulateOnTranslateLanguageDetermined(lang, page_translatable); |
| 70 } | 73 } |
| 71 | 74 |
| 72 void SimulateOnTranslateLanguageDetermined(const std::string& lang, | 75 void SimulateOnTranslateLanguageDetermined(const std::string& lang, |
| 73 bool page_translatable) { | 76 bool page_translatable) { |
| 74 rvh()->TestOnMessageReceived(ChromeViewHostMsg_TranslateLanguageDetermined( | 77 rvh_tester()->TestOnMessageReceived( |
| 75 0, lang, page_translatable)); | 78 ChromeViewHostMsg_TranslateLanguageDetermined( |
| 79 0, lang, page_translatable)); |
| 76 } | 80 } |
| 77 | 81 |
| 78 bool GetTranslateMessage(int* page_id, | 82 bool GetTranslateMessage(int* page_id, |
| 79 std::string* original_lang, | 83 std::string* original_lang, |
| 80 std::string* target_lang) { | 84 std::string* target_lang) { |
| 81 const IPC::Message* message = | 85 const IPC::Message* message = |
| 82 process()->sink().GetFirstMessageMatching( | 86 process()->sink().GetFirstMessageMatching( |
| 83 ChromeViewMsg_TranslatePage::ID); | 87 ChromeViewMsg_TranslatePage::ID); |
| 84 if (!message) | 88 if (!message) |
| 85 return false; | 89 return false; |
| (...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 355 SimulateTranslateScriptURLFetch(true); | 359 SimulateTranslateScriptURLFetch(true); |
| 356 | 360 |
| 357 // Test that we sent the right message to the renderer. | 361 // Test that we sent the right message to the renderer. |
| 358 int page_id = 0; | 362 int page_id = 0; |
| 359 std::string original_lang, target_lang; | 363 std::string original_lang, target_lang; |
| 360 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); | 364 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); |
| 361 EXPECT_EQ("fr", original_lang); | 365 EXPECT_EQ("fr", original_lang); |
| 362 EXPECT_EQ("en", target_lang); | 366 EXPECT_EQ("en", target_lang); |
| 363 | 367 |
| 364 // Simulate the render notifying the translation has been done. | 368 // Simulate the render notifying the translation has been done. |
| 365 rvh()->TestOnMessageReceived(ChromeViewHostMsg_PageTranslated( | 369 rvh_tester()->TestOnMessageReceived(ChromeViewHostMsg_PageTranslated( |
| 366 0, 0, "fr", "en", TranslateErrors::NONE)); | 370 0, 0, "fr", "en", TranslateErrors::NONE)); |
| 367 | 371 |
| 368 // The after translate infobar should be showing. | 372 // The after translate infobar should be showing. |
| 369 infobar = GetTranslateInfoBar(); | 373 infobar = GetTranslateInfoBar(); |
| 370 ASSERT_TRUE(infobar != NULL); | 374 ASSERT_TRUE(infobar != NULL); |
| 371 EXPECT_EQ(TranslateInfoBarDelegate::AFTER_TRANSLATE, infobar->type()); | 375 EXPECT_EQ(TranslateInfoBarDelegate::AFTER_TRANSLATE, infobar->type()); |
| 372 | 376 |
| 373 // Simulate changing the original language, this should trigger a translation. | 377 // Simulate changing the original language, this should trigger a translation. |
| 374 process()->sink().ClearMessages(); | 378 process()->sink().ClearMessages(); |
| 375 std::string new_original_lang = infobar->GetLanguageCodeAt(0); | 379 std::string new_original_lang = infobar->GetLanguageCodeAt(0); |
| 376 infobar->SetOriginalLanguage(0); | 380 infobar->SetOriginalLanguage(0); |
| 377 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); | 381 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); |
| 378 EXPECT_EQ(new_original_lang, original_lang); | 382 EXPECT_EQ(new_original_lang, original_lang); |
| 379 EXPECT_EQ("en", target_lang); | 383 EXPECT_EQ("en", target_lang); |
| 380 // Simulate the render notifying the translation has been done. | 384 // Simulate the render notifying the translation has been done. |
| 381 rvh()->TestOnMessageReceived(ChromeViewHostMsg_PageTranslated( | 385 rvh_tester()->TestOnMessageReceived(ChromeViewHostMsg_PageTranslated( |
| 382 0, 0, new_original_lang, "en", TranslateErrors::NONE)); | 386 0, 0, new_original_lang, "en", TranslateErrors::NONE)); |
| 383 // infobar is now invalid. | 387 // infobar is now invalid. |
| 384 TranslateInfoBarDelegate* new_infobar = GetTranslateInfoBar(); | 388 TranslateInfoBarDelegate* new_infobar = GetTranslateInfoBar(); |
| 385 ASSERT_TRUE(new_infobar != NULL); | 389 ASSERT_TRUE(new_infobar != NULL); |
| 386 infobar = new_infobar; | 390 infobar = new_infobar; |
| 387 | 391 |
| 388 // Simulate changing the target language, this should trigger a translation. | 392 // Simulate changing the target language, this should trigger a translation. |
| 389 process()->sink().ClearMessages(); | 393 process()->sink().ClearMessages(); |
| 390 std::string new_target_lang = infobar->GetLanguageCodeAt(1); | 394 std::string new_target_lang = infobar->GetLanguageCodeAt(1); |
| 391 infobar->SetTargetLanguage(1); | 395 infobar->SetTargetLanguage(1); |
| 392 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); | 396 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); |
| 393 EXPECT_EQ(new_original_lang, original_lang); | 397 EXPECT_EQ(new_original_lang, original_lang); |
| 394 EXPECT_EQ(new_target_lang, target_lang); | 398 EXPECT_EQ(new_target_lang, target_lang); |
| 395 // Simulate the render notifying the translation has been done. | 399 // Simulate the render notifying the translation has been done. |
| 396 rvh()->TestOnMessageReceived(ChromeViewHostMsg_PageTranslated(0, 0, | 400 rvh_tester()->TestOnMessageReceived( |
| 397 new_original_lang, new_target_lang, TranslateErrors::NONE)); | 401 ChromeViewHostMsg_PageTranslated( |
| 402 0, 0, new_original_lang, new_target_lang, TranslateErrors::NONE)); |
| 398 // infobar is now invalid. | 403 // infobar is now invalid. |
| 399 new_infobar = GetTranslateInfoBar(); | 404 new_infobar = GetTranslateInfoBar(); |
| 400 ASSERT_TRUE(new_infobar != NULL); | 405 ASSERT_TRUE(new_infobar != NULL); |
| 401 } | 406 } |
| 402 | 407 |
| 403 TEST_F(TranslateManagerTest, TranslateScriptNotAvailable) { | 408 TEST_F(TranslateManagerTest, TranslateScriptNotAvailable) { |
| 404 // Simulate navigating to a page. | 409 // Simulate navigating to a page. |
| 405 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); | 410 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); |
| 406 | 411 |
| 407 // We should have an infobar. | 412 // We should have an infobar. |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 445 SimulateTranslateScriptURLFetch(false); | 450 SimulateTranslateScriptURLFetch(false); |
| 446 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); | 451 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); |
| 447 ASSERT_TRUE(infobar != NULL); | 452 ASSERT_TRUE(infobar != NULL); |
| 448 EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATION_ERROR, infobar->type()); | 453 EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATION_ERROR, infobar->type()); |
| 449 EXPECT_TRUE(infobar->IsError()); | 454 EXPECT_TRUE(infobar->IsError()); |
| 450 infobar->MessageInfoBarButtonPressed(); | 455 infobar->MessageInfoBarButtonPressed(); |
| 451 SimulateTranslateScriptURLFetch(true); // This time succeed. | 456 SimulateTranslateScriptURLFetch(true); // This time succeed. |
| 452 | 457 |
| 453 // Simulate the render notifying the translation has been done, the server | 458 // Simulate the render notifying the translation has been done, the server |
| 454 // having detected the page was in a known and supported language. | 459 // having detected the page was in a known and supported language. |
| 455 rvh()->TestOnMessageReceived(ChromeViewHostMsg_PageTranslated( | 460 rvh_tester()->TestOnMessageReceived(ChromeViewHostMsg_PageTranslated( |
| 456 0, 0, "fr", "en", TranslateErrors::NONE)); | 461 0, 0, "fr", "en", TranslateErrors::NONE)); |
| 457 | 462 |
| 458 // The after translate infobar should be showing. | 463 // The after translate infobar should be showing. |
| 459 infobar = GetTranslateInfoBar(); | 464 infobar = GetTranslateInfoBar(); |
| 460 ASSERT_TRUE(infobar != NULL); | 465 ASSERT_TRUE(infobar != NULL); |
| 461 EXPECT_EQ(TranslateInfoBarDelegate::AFTER_TRANSLATE, infobar->type()); | 466 EXPECT_EQ(TranslateInfoBarDelegate::AFTER_TRANSLATE, infobar->type()); |
| 462 EXPECT_EQ("fr", infobar->GetOriginalLanguageCode()); | 467 EXPECT_EQ("fr", infobar->GetOriginalLanguageCode()); |
| 463 EXPECT_EQ("en", infobar->GetTargetLanguageCode()); | 468 EXPECT_EQ("en", infobar->GetTargetLanguageCode()); |
| 464 | 469 |
| 465 // Let's run the same steps but this time the server detects the page is | 470 // Let's run the same steps but this time the server detects the page is |
| 466 // already in English. | 471 // already in English. |
| 467 SimulateNavigation(GURL("http://www.google.com"), "und", true); | 472 SimulateNavigation(GURL("http://www.google.com"), "und", true); |
| 468 menu.reset(TestRenderViewContextMenu::CreateContextMenu(contents())); | 473 menu.reset(TestRenderViewContextMenu::CreateContextMenu(contents())); |
| 469 menu->Init(); | 474 menu->Init(); |
| 470 menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE); | 475 menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE); |
| 471 rvh()->TestOnMessageReceived(ChromeViewHostMsg_PageTranslated( | 476 rvh_tester()->TestOnMessageReceived(ChromeViewHostMsg_PageTranslated( |
| 472 1, 0, "en", "en", TranslateErrors::IDENTICAL_LANGUAGES)); | 477 1, 0, "en", "en", TranslateErrors::IDENTICAL_LANGUAGES)); |
| 473 infobar = GetTranslateInfoBar(); | 478 infobar = GetTranslateInfoBar(); |
| 474 ASSERT_TRUE(infobar != NULL); | 479 ASSERT_TRUE(infobar != NULL); |
| 475 EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATION_ERROR, infobar->type()); | 480 EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATION_ERROR, infobar->type()); |
| 476 EXPECT_EQ(TranslateErrors::IDENTICAL_LANGUAGES, infobar->error()); | 481 EXPECT_EQ(TranslateErrors::IDENTICAL_LANGUAGES, infobar->error()); |
| 477 | 482 |
| 478 // Let's run the same steps again but this time the server fails to detect the | 483 // Let's run the same steps again but this time the server fails to detect the |
| 479 // page's language (it returns an empty string). | 484 // page's language (it returns an empty string). |
| 480 SimulateNavigation(GURL("http://www.google.com"), "und", true); | 485 SimulateNavigation(GURL("http://www.google.com"), "und", true); |
| 481 menu.reset(TestRenderViewContextMenu::CreateContextMenu(contents())); | 486 menu.reset(TestRenderViewContextMenu::CreateContextMenu(contents())); |
| 482 menu->Init(); | 487 menu->Init(); |
| 483 menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE); | 488 menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE); |
| 484 rvh()->TestOnMessageReceived(ChromeViewHostMsg_PageTranslated( | 489 rvh_tester()->TestOnMessageReceived(ChromeViewHostMsg_PageTranslated( |
| 485 2, 0, "", "en", TranslateErrors::UNKNOWN_LANGUAGE)); | 490 2, 0, "", "en", TranslateErrors::UNKNOWN_LANGUAGE)); |
| 486 infobar = GetTranslateInfoBar(); | 491 infobar = GetTranslateInfoBar(); |
| 487 ASSERT_TRUE(infobar != NULL); | 492 ASSERT_TRUE(infobar != NULL); |
| 488 EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATION_ERROR, infobar->type()); | 493 EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATION_ERROR, infobar->type()); |
| 489 EXPECT_EQ(TranslateErrors::UNKNOWN_LANGUAGE, infobar->error()); | 494 EXPECT_EQ(TranslateErrors::UNKNOWN_LANGUAGE, infobar->error()); |
| 490 } | 495 } |
| 491 | 496 |
| 492 // Tests that we show/don't show an info-bar for all languages the CLD can | 497 // Tests that we show/don't show an info-bar for all languages the CLD can |
| 493 // report. | 498 // report. |
| 494 TEST_F(TranslateManagerTest, TestAllLanguages) { | 499 TEST_F(TranslateManagerTest, TestAllLanguages) { |
| (...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 674 // Simulate navigating to a page and getting its language. | 679 // Simulate navigating to a page and getting its language. |
| 675 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); | 680 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); |
| 676 | 681 |
| 677 // Simulate the user translating. | 682 // Simulate the user translating. |
| 678 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); | 683 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); |
| 679 ASSERT_TRUE(infobar != NULL); | 684 ASSERT_TRUE(infobar != NULL); |
| 680 infobar->Translate(); | 685 infobar->Translate(); |
| 681 // Simulate the translate script being retrieved. | 686 // Simulate the translate script being retrieved. |
| 682 SimulateTranslateScriptURLFetch(true); | 687 SimulateTranslateScriptURLFetch(true); |
| 683 | 688 |
| 684 rvh()->TestOnMessageReceived(ChromeViewHostMsg_PageTranslated( | 689 rvh_tester()->TestOnMessageReceived(ChromeViewHostMsg_PageTranslated( |
| 685 0, 0, "fr", "en", TranslateErrors::NONE)); | 690 0, 0, "fr", "en", TranslateErrors::NONE)); |
| 686 | 691 |
| 687 // Now navigate to a new page in the same language. | 692 // Now navigate to a new page in the same language. |
| 688 process()->sink().ClearMessages(); | 693 process()->sink().ClearMessages(); |
| 689 SimulateNavigation(GURL("http://news.google.fr"), "fr", true); | 694 SimulateNavigation(GURL("http://news.google.fr"), "fr", true); |
| 690 | 695 |
| 691 // This should have automatically triggered a translation. | 696 // This should have automatically triggered a translation. |
| 692 int page_id = 0; | 697 int page_id = 0; |
| 693 std::string original_lang, target_lang; | 698 std::string original_lang, target_lang; |
| 694 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); | 699 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 759 | 764 |
| 760 // Close the infobar. | 765 // Close the infobar. |
| 761 EXPECT_TRUE(CloseTranslateInfoBar()); | 766 EXPECT_TRUE(CloseTranslateInfoBar()); |
| 762 | 767 |
| 763 // Create a pending navigation and simulate a page load. That should be the | 768 // Create a pending navigation and simulate a page load. That should be the |
| 764 // equivalent of typing the URL again in the location bar. | 769 // equivalent of typing the URL again in the location bar. |
| 765 NavEntryCommittedObserver nav_observer(contents()); | 770 NavEntryCommittedObserver nav_observer(contents()); |
| 766 contents()->GetController().LoadURL(url, content::Referrer(), | 771 contents()->GetController().LoadURL(url, content::Referrer(), |
| 767 content::PAGE_TRANSITION_TYPED, | 772 content::PAGE_TRANSITION_TYPED, |
| 768 std::string()); | 773 std::string()); |
| 769 rvh()->SendNavigate(0, url); | 774 rvh_tester()->SendNavigate(0, url); |
| 770 | 775 |
| 771 // Test that we are really getting a same page navigation, the test would be | 776 // Test that we are really getting a same page navigation, the test would be |
| 772 // useless if it was not the case. | 777 // useless if it was not the case. |
| 773 const content::LoadCommittedDetails& nav_details = | 778 const content::LoadCommittedDetails& nav_details = |
| 774 nav_observer.get_load_commited_details(); | 779 nav_observer.get_load_commited_details(); |
| 775 EXPECT_TRUE(nav_details.entry != NULL); // There was a navigation. | 780 EXPECT_TRUE(nav_details.entry != NULL); // There was a navigation. |
| 776 EXPECT_EQ(content::NAVIGATION_TYPE_SAME_PAGE, nav_details.type); | 781 EXPECT_EQ(content::NAVIGATION_TYPE_SAME_PAGE, nav_details.type); |
| 777 | 782 |
| 778 // The TranslateManager class processes the navigation entry committed | 783 // The TranslateManager class processes the navigation entry committed |
| 779 // notification in a posted task; process that task. | 784 // notification in a posted task; process that task. |
| (...skipping 23 matching lines...) Expand all Loading... |
| 803 // Tests that a closed translate infobar does not reappear when navigating | 808 // Tests that a closed translate infobar does not reappear when navigating |
| 804 // in a subframe. (http://crbug.com/48215) | 809 // in a subframe. (http://crbug.com/48215) |
| 805 TEST_F(TranslateManagerTest, CloseInfoBarInSubframeNavigation) { | 810 TEST_F(TranslateManagerTest, CloseInfoBarInSubframeNavigation) { |
| 806 // Simulate navigating to a page and getting its language. | 811 // Simulate navigating to a page and getting its language. |
| 807 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); | 812 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); |
| 808 | 813 |
| 809 // Close the infobar. | 814 // Close the infobar. |
| 810 EXPECT_TRUE(CloseTranslateInfoBar()); | 815 EXPECT_TRUE(CloseTranslateInfoBar()); |
| 811 | 816 |
| 812 // Simulate a sub-frame auto-navigating. | 817 // Simulate a sub-frame auto-navigating. |
| 813 rvh()->SendNavigateWithTransition(1, GURL("http://pub.com"), | 818 rvh_tester()->SendNavigateWithTransition( |
| 814 content::PAGE_TRANSITION_AUTO_SUBFRAME); | 819 1, GURL("http://pub.com"), content::PAGE_TRANSITION_AUTO_SUBFRAME); |
| 815 EXPECT_TRUE(GetTranslateInfoBar() == NULL); | 820 EXPECT_TRUE(GetTranslateInfoBar() == NULL); |
| 816 | 821 |
| 817 // Simulate the user navigating in a sub-frame. | 822 // Simulate the user navigating in a sub-frame. |
| 818 rvh()->SendNavigateWithTransition(2, GURL("http://pub.com"), | 823 rvh_tester()->SendNavigateWithTransition( |
| 819 content::PAGE_TRANSITION_MANUAL_SUBFRAME); | 824 2, GURL("http://pub.com"), content::PAGE_TRANSITION_MANUAL_SUBFRAME); |
| 820 EXPECT_TRUE(GetTranslateInfoBar() == NULL); | 825 EXPECT_TRUE(GetTranslateInfoBar() == NULL); |
| 821 | 826 |
| 822 // Navigate out of page, a new infobar should show. | 827 // Navigate out of page, a new infobar should show. |
| 823 SimulateNavigation(GURL("http://www.google.fr/foot"), "fr", true); | 828 SimulateNavigation(GURL("http://www.google.fr/foot"), "fr", true); |
| 824 EXPECT_TRUE(GetTranslateInfoBar() != NULL); | 829 EXPECT_TRUE(GetTranslateInfoBar() != NULL); |
| 825 } | 830 } |
| 826 | 831 |
| 827 | 832 |
| 828 | 833 |
| 829 // Tests that denying translation is sticky when navigating in page. | 834 // Tests that denying translation is sticky when navigating in page. |
| (...skipping 18 matching lines...) Expand all Loading... |
| 848 TEST_F(TranslateManagerTest, TranslateCloseInfoBarInPageNavigation) { | 853 TEST_F(TranslateManagerTest, TranslateCloseInfoBarInPageNavigation) { |
| 849 // Simulate navigating to a page and getting its language. | 854 // Simulate navigating to a page and getting its language. |
| 850 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); | 855 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); |
| 851 | 856 |
| 852 // Simulate the user translating. | 857 // Simulate the user translating. |
| 853 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); | 858 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); |
| 854 ASSERT_TRUE(infobar != NULL); | 859 ASSERT_TRUE(infobar != NULL); |
| 855 infobar->Translate(); | 860 infobar->Translate(); |
| 856 // Simulate the translate script being retrieved. | 861 // Simulate the translate script being retrieved. |
| 857 SimulateTranslateScriptURLFetch(true); | 862 SimulateTranslateScriptURLFetch(true); |
| 858 rvh()->TestOnMessageReceived(ChromeViewHostMsg_PageTranslated( | 863 rvh_tester()->TestOnMessageReceived(ChromeViewHostMsg_PageTranslated( |
| 859 0, 0, "fr", "en", TranslateErrors::NONE)); | 864 0, 0, "fr", "en", TranslateErrors::NONE)); |
| 860 | 865 |
| 861 // Close the infobar. | 866 // Close the infobar. |
| 862 EXPECT_TRUE(CloseTranslateInfoBar()); | 867 EXPECT_TRUE(CloseTranslateInfoBar()); |
| 863 | 868 |
| 864 // Navigate in page, no infobar should be shown. | 869 // Navigate in page, no infobar should be shown. |
| 865 SimulateNavigation(GURL("http://www.google.fr/#ref1"), "fr", true); | 870 SimulateNavigation(GURL("http://www.google.fr/#ref1"), "fr", true); |
| 866 EXPECT_TRUE(GetTranslateInfoBar() == NULL); | 871 EXPECT_TRUE(GetTranslateInfoBar() == NULL); |
| 867 | 872 |
| 868 // Navigate out of page, a new infobar should show. | 873 // Navigate out of page, a new infobar should show. |
| 869 // Note that we navigate to a page in a different language so we don't trigger | 874 // Note that we navigate to a page in a different language so we don't trigger |
| 870 // the auto-translate feature (it would translate the page automatically and | 875 // the auto-translate feature (it would translate the page automatically and |
| 871 // the before translate inforbar would not be shown). | 876 // the before translate inforbar would not be shown). |
| 872 SimulateNavigation(GURL("http://www.google.de"), "de", true); | 877 SimulateNavigation(GURL("http://www.google.de"), "de", true); |
| 873 EXPECT_TRUE(GetTranslateInfoBar() != NULL); | 878 EXPECT_TRUE(GetTranslateInfoBar() != NULL); |
| 874 } | 879 } |
| 875 | 880 |
| 876 // Tests that the after translate the infobar still shows when navigating | 881 // Tests that the after translate the infobar still shows when navigating |
| 877 // in-page. | 882 // in-page. |
| 878 TEST_F(TranslateManagerTest, TranslateInPageNavigation) { | 883 TEST_F(TranslateManagerTest, TranslateInPageNavigation) { |
| 879 // Simulate navigating to a page and getting its language. | 884 // Simulate navigating to a page and getting its language. |
| 880 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); | 885 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); |
| 881 | 886 |
| 882 // Simulate the user translating. | 887 // Simulate the user translating. |
| 883 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); | 888 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); |
| 884 ASSERT_TRUE(infobar != NULL); | 889 ASSERT_TRUE(infobar != NULL); |
| 885 infobar->Translate(); | 890 infobar->Translate(); |
| 886 // Simulate the translate script being retrieved. | 891 // Simulate the translate script being retrieved. |
| 887 SimulateTranslateScriptURLFetch(true); | 892 SimulateTranslateScriptURLFetch(true); |
| 888 rvh()->TestOnMessageReceived(ChromeViewHostMsg_PageTranslated( | 893 rvh_tester()->TestOnMessageReceived(ChromeViewHostMsg_PageTranslated( |
| 889 0, 0, "fr", "en", TranslateErrors::NONE)); | 894 0, 0, "fr", "en", TranslateErrors::NONE)); |
| 890 // The after translate infobar is showing. | 895 // The after translate infobar is showing. |
| 891 infobar = GetTranslateInfoBar(); | 896 infobar = GetTranslateInfoBar(); |
| 892 ASSERT_TRUE(infobar != NULL); | 897 ASSERT_TRUE(infobar != NULL); |
| 893 | 898 |
| 894 // Navigate out of page, a new infobar should show. | 899 // Navigate out of page, a new infobar should show. |
| 895 // See note in TranslateCloseInfoBarInPageNavigation test on why it is | 900 // See note in TranslateCloseInfoBarInPageNavigation test on why it is |
| 896 // important to navigate to a page in a different language for this test. | 901 // important to navigate to a page in a different language for this test. |
| 897 SimulateNavigation(GURL("http://www.google.de"), "de", true); | 902 SimulateNavigation(GURL("http://www.google.de"), "de", true); |
| 898 // The old infobar is gone. | 903 // The old infobar is gone. |
| (...skipping 18 matching lines...) Expand all Loading... |
| 917 TEST_F(TranslateManagerTest, ServerReportsUnsupportedLanguage) { | 922 TEST_F(TranslateManagerTest, ServerReportsUnsupportedLanguage) { |
| 918 // Simulate navigating to a page and translating it. | 923 // Simulate navigating to a page and translating it. |
| 919 SimulateNavigation(GURL("http://mail.google.fr"), "fr", true); | 924 SimulateNavigation(GURL("http://mail.google.fr"), "fr", true); |
| 920 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); | 925 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); |
| 921 ASSERT_TRUE(infobar != NULL); | 926 ASSERT_TRUE(infobar != NULL); |
| 922 process()->sink().ClearMessages(); | 927 process()->sink().ClearMessages(); |
| 923 infobar->Translate(); | 928 infobar->Translate(); |
| 924 SimulateTranslateScriptURLFetch(true); | 929 SimulateTranslateScriptURLFetch(true); |
| 925 // Simulate the render notifying the translation has been done, but it | 930 // Simulate the render notifying the translation has been done, but it |
| 926 // reports a language we don't support. | 931 // reports a language we don't support. |
| 927 rvh()->TestOnMessageReceived(ChromeViewHostMsg_PageTranslated( | 932 rvh_tester()->TestOnMessageReceived(ChromeViewHostMsg_PageTranslated( |
| 928 0, 0, "qbz", "en", TranslateErrors::NONE)); | 933 0, 0, "qbz", "en", TranslateErrors::NONE)); |
| 929 | 934 |
| 930 // An error infobar should be showing to report that we don't support this | 935 // An error infobar should be showing to report that we don't support this |
| 931 // language. | 936 // language. |
| 932 infobar = GetTranslateInfoBar(); | 937 infobar = GetTranslateInfoBar(); |
| 933 ASSERT_TRUE(infobar != NULL); | 938 ASSERT_TRUE(infobar != NULL); |
| 934 EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATION_ERROR, infobar->type()); | 939 EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATION_ERROR, infobar->type()); |
| 935 | 940 |
| 936 // This infobar should have a button (so the string should not be empty). | 941 // This infobar should have a button (so the string should not be empty). |
| 937 ASSERT_FALSE(infobar->GetMessageInfoBarButtonText().empty()); | 942 ASSERT_FALSE(infobar->GetMessageInfoBarButtonText().empty()); |
| (...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1235 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); | 1240 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); |
| 1236 EXPECT_EQ("fr", original_lang); | 1241 EXPECT_EQ("fr", original_lang); |
| 1237 EXPECT_EQ("en", target_lang); | 1242 EXPECT_EQ("en", target_lang); |
| 1238 process()->sink().ClearMessages(); | 1243 process()->sink().ClearMessages(); |
| 1239 | 1244 |
| 1240 // This should also have reverted the blacklisting of this site and language. | 1245 // This should also have reverted the blacklisting of this site and language. |
| 1241 EXPECT_FALSE(translate_prefs.IsLanguageBlacklisted("fr")); | 1246 EXPECT_FALSE(translate_prefs.IsLanguageBlacklisted("fr")); |
| 1242 EXPECT_FALSE(translate_prefs.IsSiteBlacklisted(url.host())); | 1247 EXPECT_FALSE(translate_prefs.IsSiteBlacklisted(url.host())); |
| 1243 | 1248 |
| 1244 // Let's simulate the page being translated. | 1249 // Let's simulate the page being translated. |
| 1245 rvh()->TestOnMessageReceived(ChromeViewHostMsg_PageTranslated( | 1250 rvh_tester()->TestOnMessageReceived(ChromeViewHostMsg_PageTranslated( |
| 1246 0, 0, "fr", "en", TranslateErrors::NONE)); | 1251 0, 0, "fr", "en", TranslateErrors::NONE)); |
| 1247 | 1252 |
| 1248 // The translate menu should now be disabled. | 1253 // The translate menu should now be disabled. |
| 1249 menu.reset(TestRenderViewContextMenu::CreateContextMenu(contents())); | 1254 menu.reset(TestRenderViewContextMenu::CreateContextMenu(contents())); |
| 1250 menu->Init(); | 1255 menu->Init(); |
| 1251 EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE)); | 1256 EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE)); |
| 1252 EXPECT_FALSE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE)); | 1257 EXPECT_FALSE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE)); |
| 1253 | 1258 |
| 1254 // Test that selecting translate in the context menu WHILE the page is being | 1259 // Test that selecting translate in the context menu WHILE the page is being |
| 1255 // translated does nothing (this could happen if autotranslate kicks-in and | 1260 // translated does nothing (this could happen if autotranslate kicks-in and |
| (...skipping 15 matching lines...) Expand all Loading... |
| 1271 // been translated does nothing. | 1276 // been translated does nothing. |
| 1272 SimulateNavigation(GURL("http://www.google.de"), "de", true); | 1277 SimulateNavigation(GURL("http://www.google.de"), "de", true); |
| 1273 infobar = GetTranslateInfoBar(); | 1278 infobar = GetTranslateInfoBar(); |
| 1274 ASSERT_TRUE(infobar != NULL); | 1279 ASSERT_TRUE(infobar != NULL); |
| 1275 infobar->Translate(); | 1280 infobar->Translate(); |
| 1276 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); | 1281 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); |
| 1277 process()->sink().ClearMessages(); | 1282 process()->sink().ClearMessages(); |
| 1278 menu.reset(TestRenderViewContextMenu::CreateContextMenu(contents())); | 1283 menu.reset(TestRenderViewContextMenu::CreateContextMenu(contents())); |
| 1279 menu->Init(); | 1284 menu->Init(); |
| 1280 EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE)); | 1285 EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE)); |
| 1281 rvh()->TestOnMessageReceived(ChromeViewHostMsg_PageTranslated( | 1286 rvh_tester()->TestOnMessageReceived(ChromeViewHostMsg_PageTranslated( |
| 1282 0, 0, "de", "en", TranslateErrors::NONE)); | 1287 0, 0, "de", "en", TranslateErrors::NONE)); |
| 1283 menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE); | 1288 menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE); |
| 1284 // No message expected since the translation should have been ignored. | 1289 // No message expected since the translation should have been ignored. |
| 1285 EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); | 1290 EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); |
| 1286 | 1291 |
| 1287 // Test that the translate context menu is enabled when the page is in an | 1292 // Test that the translate context menu is enabled when the page is in an |
| 1288 // unknown language. | 1293 // unknown language. |
| 1289 SimulateNavigation(url, "und", true); | 1294 SimulateNavigation(url, "und", true); |
| 1290 menu.reset(TestRenderViewContextMenu::CreateContextMenu(contents())); | 1295 menu.reset(TestRenderViewContextMenu::CreateContextMenu(contents())); |
| 1291 menu->Init(); | 1296 menu->Init(); |
| 1292 EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE)); | 1297 EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE)); |
| 1293 EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE)); | 1298 EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE)); |
| 1294 | 1299 |
| 1295 // Test that the translate context menu is disabled when the page is in an | 1300 // Test that the translate context menu is disabled when the page is in an |
| 1296 // unsupported language. | 1301 // unsupported language. |
| 1297 SimulateNavigation(url, "qbz", true); | 1302 SimulateNavigation(url, "qbz", true); |
| 1298 menu.reset(TestRenderViewContextMenu::CreateContextMenu(contents())); | 1303 menu.reset(TestRenderViewContextMenu::CreateContextMenu(contents())); |
| 1299 menu->Init(); | 1304 menu->Init(); |
| 1300 EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE)); | 1305 EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE)); |
| 1301 EXPECT_FALSE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE)); | 1306 EXPECT_FALSE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE)); |
| 1302 } | 1307 } |
| 1303 | 1308 |
| 1304 // Tests that an extra always/never translate button is shown on the "before | 1309 // Tests that an extra always/never translate button is shown on the "before |
| 1305 // translate" infobar when the translation is accepted/declined 3 times, | 1310 // translate" infobar when the translation is accepted/declined 3 times, |
| 1306 // only when not in incognito mode. | 1311 // only when not in incognito mode. |
| 1307 TEST_F(TranslateManagerTest, BeforeTranslateExtraButtons) { | 1312 TEST_F(TranslateManagerTest, BeforeTranslateExtraButtons) { |
| 1308 Profile* profile = | 1313 Profile* profile = |
| 1309 Profile::FromBrowserContext(contents()->GetBrowserContext()); | 1314 Profile::FromBrowserContext(contents()->GetBrowserContext()); |
| 1310 TranslatePrefs translate_prefs(profile->GetPrefs()); | 1315 TranslatePrefs translate_prefs(profile->GetPrefs()); |
| 1311 translate_prefs.ResetTranslationAcceptedCount("fr"); | 1316 translate_prefs.ResetTranslationAcceptedCount("fr"); |
| 1312 translate_prefs.ResetTranslationDeniedCount("fr"); | 1317 translate_prefs.ResetTranslationDeniedCount("fr"); |
| 1313 translate_prefs.ResetTranslationAcceptedCount("de"); | 1318 translate_prefs.ResetTranslationAcceptedCount("de"); |
| 1314 translate_prefs.ResetTranslationDeniedCount("de"); | 1319 translate_prefs.ResetTranslationDeniedCount("de"); |
| 1315 | 1320 |
| 1316 // We'll do 4 times in incognito mode first to make sure the button is not | 1321 // We'll do 4 times in incognito mode first to make sure the button is not |
| 1317 // shown in that case, then 4 times in normal mode. | 1322 // shown in that case, then 4 times in normal mode. |
| 1318 TranslateInfoBarDelegate* infobar; | 1323 TranslateInfoBarDelegate* infobar; |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1398 TEST_F(TranslateManagerTest, ScriptExpires) { | 1403 TEST_F(TranslateManagerTest, ScriptExpires) { |
| 1399 ExpireTranslateScriptImmediately(); | 1404 ExpireTranslateScriptImmediately(); |
| 1400 | 1405 |
| 1401 // Simulate navigating to a page and translating it. | 1406 // Simulate navigating to a page and translating it. |
| 1402 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); | 1407 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); |
| 1403 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); | 1408 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); |
| 1404 ASSERT_TRUE(infobar != NULL); | 1409 ASSERT_TRUE(infobar != NULL); |
| 1405 process()->sink().ClearMessages(); | 1410 process()->sink().ClearMessages(); |
| 1406 infobar->Translate(); | 1411 infobar->Translate(); |
| 1407 SimulateTranslateScriptURLFetch(true); | 1412 SimulateTranslateScriptURLFetch(true); |
| 1408 rvh()->TestOnMessageReceived(ChromeViewHostMsg_PageTranslated( | 1413 rvh_tester()->TestOnMessageReceived(ChromeViewHostMsg_PageTranslated( |
| 1409 0, 0, "fr", "en", TranslateErrors::NONE)); | 1414 0, 0, "fr", "en", TranslateErrors::NONE)); |
| 1410 | 1415 |
| 1411 // A task should have been posted to clear the script, run it. | 1416 // A task should have been posted to clear the script, run it. |
| 1412 MessageLoop::current()->RunAllPending(); | 1417 MessageLoop::current()->RunAllPending(); |
| 1413 | 1418 |
| 1414 // Do another navigation and translation. | 1419 // Do another navigation and translation. |
| 1415 SimulateNavigation(GURL("http://www.google.es"), "es", true); | 1420 SimulateNavigation(GURL("http://www.google.es"), "es", true); |
| 1416 infobar = GetTranslateInfoBar(); | 1421 infobar = GetTranslateInfoBar(); |
| 1417 ASSERT_TRUE(infobar != NULL); | 1422 ASSERT_TRUE(infobar != NULL); |
| 1418 process()->sink().ClearMessages(); | 1423 process()->sink().ClearMessages(); |
| 1419 infobar->Translate(); | 1424 infobar->Translate(); |
| 1420 // If we don't simulate the URL fetch, the TranslateManager should be waiting | 1425 // If we don't simulate the URL fetch, the TranslateManager should be waiting |
| 1421 // for the script and no message should have been sent to the renderer. | 1426 // for the script and no message should have been sent to the renderer. |
| 1422 EXPECT_TRUE( | 1427 EXPECT_TRUE( |
| 1423 process()->sink().GetFirstMessageMatching( | 1428 process()->sink().GetFirstMessageMatching( |
| 1424 ChromeViewMsg_TranslatePage::ID) == | 1429 ChromeViewMsg_TranslatePage::ID) == |
| 1425 NULL); | 1430 NULL); |
| 1426 // Now simulate the URL fetch. | 1431 // Now simulate the URL fetch. |
| 1427 SimulateTranslateScriptURLFetch(true); | 1432 SimulateTranslateScriptURLFetch(true); |
| 1428 // Now the message should have been sent. | 1433 // Now the message should have been sent. |
| 1429 int page_id = 0; | 1434 int page_id = 0; |
| 1430 std::string original_lang, target_lang; | 1435 std::string original_lang, target_lang; |
| 1431 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); | 1436 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); |
| 1432 EXPECT_EQ("es", original_lang); | 1437 EXPECT_EQ("es", original_lang); |
| 1433 EXPECT_EQ("en", target_lang); | 1438 EXPECT_EQ("en", target_lang); |
| 1434 } | 1439 } |
| OLD | NEW |