Chromium Code Reviews

Side by Side Diff: chrome/browser/translate/translate_manager_unittest.cc

Issue 1184001: Don't autotranslate in incognito mode (so we don't send information... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 10 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | | Annotate | Revision Log
« no previous file with comments | « chrome/browser/translate/translate_manager.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 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 #include "chrome/browser/renderer_host/test/test_render_view_host.h" 5 #include "chrome/browser/renderer_host/test/test_render_view_host.h"
6 6
7 #include "chrome/browser/renderer_host/mock_render_process_host.h" 7 #include "chrome/browser/renderer_host/mock_render_process_host.h"
8 #include "chrome/browser/translate/translate_infobars_delegates.h" 8 #include "chrome/browser/translate/translate_infobars_delegates.h"
9 #include "chrome/browser/translate/translate_manager.h" 9 #include "chrome/browser/translate/translate_manager.h"
10 #include "chrome/common/ipc_test_sink.h" 10 #include "chrome/common/ipc_test_sink.h"
(...skipping 510 matching lines...)
521 // Simulate navigating to a page in a language supported by the translate 521 // Simulate navigating to a page in a language supported by the translate
522 // server. 522 // server.
523 SimulateNavigation(GURL("http://www.google.com"), 0, L"Google", "en"); 523 SimulateNavigation(GURL("http://www.google.com"), 0, L"Google", "en");
524 524
525 // No info-bar should be shown. 525 // No info-bar should be shown.
526 EXPECT_TRUE(GetTranslateInfoBar() == NULL); 526 EXPECT_TRUE(GetTranslateInfoBar() == NULL);
527 527
528 browser_process->SetApplicationLocale(original_lang); 528 browser_process->SetApplicationLocale(original_lang);
529 } 529 }
530 530
531 // Tests that the translate preference is honored. 531 // Tests that the translate enabled preference is honored.
532 TEST_F(TranslateManagerTest, TranslatePref) { 532 TEST_F(TranslateManagerTest, TranslateEnabledPref) {
533 // Make sure the pref allows translate. 533 // Make sure the pref allows translate.
534 PrefService* prefs = contents()->profile()->GetPrefs(); 534 PrefService* prefs = contents()->profile()->GetPrefs();
535 prefs->SetBoolean(prefs::kEnableTranslate, true); 535 prefs->SetBoolean(prefs::kEnableTranslate, true);
536 536
537 // Simulate navigating to a page and getting its language. 537 // Simulate navigating to a page and getting its language.
538 SimulateNavigation(GURL("http://www.google.fr"), 0, L"Le Google", "fr"); 538 SimulateNavigation(GURL("http://www.google.fr"), 0, L"Le Google", "fr");
539 539
540 // An infobar should be shown. 540 // An infobar should be shown.
541 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); 541 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
542 EXPECT_TRUE(infobar != NULL); 542 EXPECT_TRUE(infobar != NULL);
543 543
544 // Disable translate. 544 // Disable translate.
545 prefs->SetBoolean(prefs::kEnableTranslate, false); 545 prefs->SetBoolean(prefs::kEnableTranslate, false);
546 546
547 // Navigate to a new page, that should close the previous infobar. 547 // Navigate to a new page, that should close the previous infobar.
548 GURL url("http://www.youtube.fr"); 548 GURL url("http://www.youtube.fr");
549 NavigateAndCommit(url); 549 NavigateAndCommit(url);
550 infobar = GetTranslateInfoBar(); 550 infobar = GetTranslateInfoBar();
551 EXPECT_TRUE(infobar == NULL); 551 EXPECT_TRUE(infobar == NULL);
552 552
553 // Simulate getting the page contents and language, that should not trigger 553 // Simulate getting the page contents and language, that should not trigger
554 // a translate infobar. 554 // a translate infobar.
555 SimulateOnPageContents(url, 1, L"Le YouTube", "fr"); 555 SimulateOnPageContents(url, 1, L"Le YouTube", "fr");
556 infobar = GetTranslateInfoBar(); 556 infobar = GetTranslateInfoBar();
557 EXPECT_TRUE(infobar == NULL); 557 EXPECT_TRUE(infobar == NULL);
558 } 558 }
559
560 // Tests the "Never translate <language>" pref.
561 TEST_F(TranslateManagerTest, NeverTranslateLanguagePref) {
562 // Simulate navigating to a page and getting its language.
563 GURL url("http://www.google.fr");
564 SimulateNavigation(url, 0, L"Le Google", "fr");
565
566 // An infobar should be shown.
567 EXPECT_TRUE(GetTranslateInfoBar() != NULL);
568
569 // Select never translate this language.
570 PrefService* prefs = contents()->profile()->GetPrefs();
571 TranslatePrefs translate_prefs(contents()->profile()->GetPrefs());
572 EXPECT_FALSE(translate_prefs.IsLanguageBlacklisted("fr"));
573 EXPECT_TRUE(translate_prefs.CanTranslate(prefs, "fr", url));
574 translate_prefs.BlacklistLanguage("fr");
575 EXPECT_TRUE(translate_prefs.IsLanguageBlacklisted("fr"));
576 EXPECT_FALSE(translate_prefs.CanTranslate(prefs, "fr", url));
577
578 // Close the infobar.
579 EXPECT_TRUE(CloseTranslateInfoBar());
580
581 // Navigate to a new page also in French.
582 SimulateNavigation(GURL("http://wwww.youtube.fr"), 1, L"Le YouTube", "fr");
583
584 // There should not be a translate infobar.
585 EXPECT_TRUE(GetTranslateInfoBar() == NULL);
586
587 // Remove the language from the blacklist.
588 translate_prefs.RemoveLanguageFromBlacklist("fr");
589 EXPECT_FALSE(translate_prefs.IsLanguageBlacklisted("fr"));
590 EXPECT_TRUE(translate_prefs.CanTranslate(prefs, "fr", url));
591
592 // Navigate to a page in French.
593 SimulateNavigation(url, 2, L"Le Google", "fr");
594
595 // There should be a translate infobar.
596 EXPECT_TRUE(GetTranslateInfoBar() != NULL);
597 }
598
599 // Tests the "Never translate this site" pref.
600 TEST_F(TranslateManagerTest, NeverTranslateSitePref) {
601 // Simulate navigating to a page and getting its language.
602 GURL url("http://www.google.fr");
603 std::string host(url.host());
604 SimulateNavigation(url, 0, L"Le Google", "fr");
605
606 // An infobar should be shown.
607 EXPECT_TRUE(GetTranslateInfoBar() != NULL);
608
609 // Select never translate this site.
610 PrefService* prefs = contents()->profile()->GetPrefs();
611 TranslatePrefs translate_prefs(contents()->profile()->GetPrefs());
612 EXPECT_FALSE(translate_prefs.IsSiteBlacklisted(host));
613 EXPECT_TRUE(translate_prefs.CanTranslate(prefs, "fr", url));
614 translate_prefs.BlacklistSite(host);
615 EXPECT_TRUE(translate_prefs.IsSiteBlacklisted(host));
616 EXPECT_FALSE(translate_prefs.CanTranslate(prefs, "fr", url));
617
618 // Close the infobar.
619 EXPECT_TRUE(CloseTranslateInfoBar());
620
621 // Navigate to a new page also on the same site.
622 SimulateNavigation(GURL("http://www.google.fr/hello"), 1, L"Bonjour", "fr");
623
624 // There should not be a translate infobar.
625 EXPECT_TRUE(GetTranslateInfoBar() == NULL);
626
627 // Remove the site from the blacklist.
628 translate_prefs.RemoveSiteFromBlacklist(host);
629 EXPECT_FALSE(translate_prefs.IsSiteBlacklisted(host));
630 EXPECT_TRUE(translate_prefs.CanTranslate(prefs, "fr", url));
631
632 // Navigate to a page in French.
633 SimulateNavigation(url, 0, L"Le Google", "fr");
634
635 // There should be a translate infobar.
636 EXPECT_TRUE(GetTranslateInfoBar() != NULL);
637 }
638
639 // Tests the "Always translate this language" pref.
640 TEST_F(TranslateManagerTest, AlwaysTranslateLanguagePref) {
641 // Select always translate French to English.
642 TranslatePrefs translate_prefs(contents()->profile()->GetPrefs());
643 translate_prefs.WhitelistLanguagePair("fr", "en");
644
645 // Load a page in French.
646 SimulateNavigation(GURL("http://www.google.fr"), 0, L"Le Google", "fr");
647
648 // It should have triggered an automatic translation to English.
649 int page_id = 0;
650 std::string original_lang, target_lang;
651 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
652 EXPECT_EQ(0, page_id);
653 EXPECT_EQ("fr", original_lang);
654 EXPECT_EQ("en", target_lang);
655 process()->sink().ClearMessages();
656 // And we should have no infobar (since we don't send the page translated
657 // notification, the after translate infobar is not shown).
658 EXPECT_TRUE(GetTranslateInfoBar() == NULL);
659
660 // Try another language, it should not be autotranslated.
661 SimulateNavigation(GURL("http://www.google.es"), 1, L"El Google", "es");
662 EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
663 EXPECT_TRUE(GetTranslateInfoBar() != NULL);
664 EXPECT_TRUE(CloseTranslateInfoBar());
665
666 // Let's switch to incognito mode, it should not be autotranslated in that
667 // case either.
668 TestingProfile* test_profile =
669 static_cast<TestingProfile*>(contents()->profile());
670 test_profile->set_off_the_record(true);
671 SimulateNavigation(GURL("http://www.youtube.fr"), 2, L"Le YouTube", "fr");
672 EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
673 EXPECT_TRUE(GetTranslateInfoBar() != NULL);
674 EXPECT_TRUE(CloseTranslateInfoBar());
675 test_profile->set_off_the_record(false); // Get back to non incognito.
676
677 // Now revert the always translate pref and make sure we go back to expected
678 // behavior, which is show an infobar.
679 translate_prefs.RemoveLanguagePairFromWhitelist("fr", "en");
680 SimulateNavigation(GURL("http://www.google.fr"), 3, L"Le Google", "fr");
681 EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
682 EXPECT_TRUE(GetTranslateInfoBar() != NULL);
683 }
OLDNEW
« no previous file with comments | « chrome/browser/translate/translate_manager.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine