| 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 <set> | 7 #include <set> |
| 8 #include <vector> |
| 7 | 9 |
| 8 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
| 11 #include "base/stringprintf.h" |
| 9 #include "chrome/app/chrome_command_ids.h" | 12 #include "chrome/app/chrome_command_ids.h" |
| 10 #include "chrome/browser/prefs/pref_change_registrar.h" | 13 #include "chrome/browser/prefs/pref_change_registrar.h" |
| 11 #include "chrome/browser/prefs/pref_service.h" | 14 #include "chrome/browser/prefs/pref_service.h" |
| 12 #include "chrome/browser/tab_contents/render_view_context_menu.h" | 15 #include "chrome/browser/tab_contents/render_view_context_menu.h" |
| 13 #include "chrome/browser/translate/translate_infobar_delegate.h" | 16 #include "chrome/browser/translate/translate_infobar_delegate.h" |
| 14 #include "chrome/browser/translate/translate_manager.h" | 17 #include "chrome/browser/translate/translate_manager.h" |
| 15 #include "chrome/browser/translate/translate_prefs.h" | 18 #include "chrome/browser/translate/translate_prefs.h" |
| 16 #include "chrome/browser/ui/tab_contents/test_tab_contents_wrapper.h" | 19 #include "chrome/browser/ui/tab_contents/test_tab_contents_wrapper.h" |
| 17 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 20 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
| 18 #include "chrome/common/pref_names.h" | 21 #include "chrome/common/pref_names.h" |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 170 | 173 |
| 171 notification_registrar_.Remove(this, | 174 notification_registrar_.Remove(this, |
| 172 NotificationType::TAB_CONTENTS_INFOBAR_REMOVED, | 175 NotificationType::TAB_CONTENTS_INFOBAR_REMOVED, |
| 173 Source<TabContentsWrapper>(contents_wrapper())); | 176 Source<TabContentsWrapper>(contents_wrapper())); |
| 174 | 177 |
| 175 TabContentsWrapperTestHarness::TearDown(); | 178 TabContentsWrapperTestHarness::TearDown(); |
| 176 | 179 |
| 177 URLFetcher::set_factory(NULL); | 180 URLFetcher::set_factory(NULL); |
| 178 } | 181 } |
| 179 | 182 |
| 180 void SimulateURLFetch(bool success) { | 183 void SimulateTranslateScriptURLFetch(bool success) { |
| 181 TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); | 184 TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); |
| 182 ASSERT_TRUE(fetcher); | 185 ASSERT_TRUE(fetcher); |
| 183 net::URLRequestStatus status; | 186 net::URLRequestStatus status; |
| 184 status.set_status(success ? net::URLRequestStatus::SUCCESS : | 187 status.set_status(success ? net::URLRequestStatus::SUCCESS : |
| 185 net::URLRequestStatus::FAILED); | 188 net::URLRequestStatus::FAILED); |
| 186 fetcher->delegate()->OnURLFetchComplete(fetcher, fetcher->original_url(), | 189 fetcher->delegate()->OnURLFetchComplete(fetcher, fetcher->original_url(), |
| 187 status, success ? 200 : 500, | 190 status, success ? 200 : 500, |
| 188 net::ResponseCookies(), | 191 net::ResponseCookies(), |
| 189 std::string()); | 192 std::string()); |
| 190 } | 193 } |
| 191 | 194 |
| 195 void SimulateSupportedLanguagesURLFetch( |
| 196 bool success, const std::vector<std::string>& languages) { |
| 197 TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(1); |
| 198 ASSERT_TRUE(fetcher); |
| 199 net::URLRequestStatus status; |
| 200 status.set_status(success ? net::URLRequestStatus::SUCCESS : |
| 201 net::URLRequestStatus::FAILED); |
| 202 |
| 203 std::string data; |
| 204 if (success) { |
| 205 data = base::StringPrintf("%s{'sl': {'bla': 'bla'}, '%s': {", |
| 206 TranslateManager::kLanguageListCallbackName, |
| 207 TranslateManager::kTargetLanguagesKey); |
| 208 const char* comma = ""; |
| 209 for (size_t i = 0; i < languages.size(); ++i) { |
| 210 data += base::StringPrintf( |
| 211 "%s'%s': 'UnusedFullName'", comma, languages[i].c_str()); |
| 212 if (i == 0) |
| 213 comma = ","; |
| 214 } |
| 215 data += "}})"; |
| 216 } |
| 217 fetcher->delegate()->OnURLFetchComplete(fetcher, fetcher->original_url(), |
| 218 status, success ? 200 : 500, |
| 219 net::ResponseCookies(), |
| 220 data); |
| 221 } |
| 222 |
| 192 void SetPrefObserverExpectation(const char* path) { | 223 void SetPrefObserverExpectation(const char* path) { |
| 193 EXPECT_CALL( | 224 EXPECT_CALL( |
| 194 pref_observer_, | 225 pref_observer_, |
| 195 Observe(NotificationType(NotificationType::PREF_CHANGED), | 226 Observe(NotificationType(NotificationType::PREF_CHANGED), |
| 196 _, | 227 _, |
| 197 Property(&Details<std::string>::ptr, Pointee(path)))); | 228 Property(&Details<std::string>::ptr, Pointee(path)))); |
| 198 } | 229 } |
| 199 | 230 |
| 200 NotificationObserverMock pref_observer_; | 231 NotificationObserverMock pref_observer_; |
| 201 ScopedTestingBrowserProcess testing_browser_process_; | 232 ScopedTestingBrowserProcess testing_browser_process_; |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 293 process()->sink().ClearMessages(); | 324 process()->sink().ClearMessages(); |
| 294 infobar->Translate(); | 325 infobar->Translate(); |
| 295 | 326 |
| 296 // The "Translating..." infobar should be showing. | 327 // The "Translating..." infobar should be showing. |
| 297 infobar = GetTranslateInfoBar(); | 328 infobar = GetTranslateInfoBar(); |
| 298 ASSERT_TRUE(infobar != NULL); | 329 ASSERT_TRUE(infobar != NULL); |
| 299 EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATING, infobar->type()); | 330 EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATING, infobar->type()); |
| 300 | 331 |
| 301 // Simulate the translate script being retrieved (it only needs to be done | 332 // Simulate the translate script being retrieved (it only needs to be done |
| 302 // once in the test as it is cached). | 333 // once in the test as it is cached). |
| 303 SimulateURLFetch(true); | 334 SimulateTranslateScriptURLFetch(true); |
| 304 | 335 |
| 305 // Test that we sent the right message to the renderer. | 336 // Test that we sent the right message to the renderer. |
| 306 int page_id = 0; | 337 int page_id = 0; |
| 307 std::string original_lang, target_lang; | 338 std::string original_lang, target_lang; |
| 308 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); | 339 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); |
| 309 EXPECT_EQ("fr", original_lang); | 340 EXPECT_EQ("fr", original_lang); |
| 310 EXPECT_EQ("en", target_lang); | 341 EXPECT_EQ("en", target_lang); |
| 311 | 342 |
| 312 // Simulate the render notifying the translation has been done. | 343 // Simulate the render notifying the translation has been done. |
| 313 rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(0, 0, "fr", "en", | 344 rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(0, 0, "fr", "en", |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 354 | 385 |
| 355 // We should have an infobar. | 386 // We should have an infobar. |
| 356 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); | 387 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); |
| 357 ASSERT_TRUE(infobar != NULL); | 388 ASSERT_TRUE(infobar != NULL); |
| 358 EXPECT_EQ(TranslateInfoBarDelegate::BEFORE_TRANSLATE, infobar->type()); | 389 EXPECT_EQ(TranslateInfoBarDelegate::BEFORE_TRANSLATE, infobar->type()); |
| 359 | 390 |
| 360 // Simulate clicking translate. | 391 // Simulate clicking translate. |
| 361 process()->sink().ClearMessages(); | 392 process()->sink().ClearMessages(); |
| 362 infobar->Translate(); | 393 infobar->Translate(); |
| 363 // Simulate a failure retrieving the translate script. | 394 // Simulate a failure retrieving the translate script. |
| 364 SimulateURLFetch(false); | 395 SimulateTranslateScriptURLFetch(false); |
| 365 | 396 |
| 366 // We should not have sent any message to translate to the renderer. | 397 // We should not have sent any message to translate to the renderer. |
| 367 EXPECT_FALSE(GetTranslateMessage(NULL, NULL, NULL)); | 398 EXPECT_FALSE(GetTranslateMessage(NULL, NULL, NULL)); |
| 368 | 399 |
| 369 // And we should have an error infobar showing. | 400 // And we should have an error infobar showing. |
| 370 infobar = GetTranslateInfoBar(); | 401 infobar = GetTranslateInfoBar(); |
| 371 ASSERT_TRUE(infobar != NULL); | 402 ASSERT_TRUE(infobar != NULL); |
| 372 EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATION_ERROR, infobar->type()); | 403 EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATION_ERROR, infobar->type()); |
| 373 } | 404 } |
| 374 | 405 |
| 375 // Ensures we deal correctly with pages for which the browser does not recognize | 406 // Ensures we deal correctly with pages for which the browser does not recognize |
| 376 // the language (the translate server may or not detect the language). | 407 // the language (the translate server may or not detect the language). |
| 377 TEST_F(TranslateManagerTest, TranslateUnknownLanguage) { | 408 TEST_F(TranslateManagerTest, TranslateUnknownLanguage) { |
| 378 // Simulate navigating to a page ("und" is the string returned by the CLD for | 409 // Simulate navigating to a page ("und" is the string returned by the CLD for |
| 379 // languages it does not recognize). | 410 // languages it does not recognize). |
| 380 SimulateNavigation(GURL("http://www.google.mys"), "und", true); | 411 SimulateNavigation(GURL("http://www.google.mys"), "und", true); |
| 381 | 412 |
| 382 // We should not have an infobar as we don't know the language. | 413 // We should not have an infobar as we don't know the language. |
| 383 ASSERT_TRUE(GetTranslateInfoBar() == NULL); | 414 ASSERT_TRUE(GetTranslateInfoBar() == NULL); |
| 384 | 415 |
| 385 // Translate the page anyway throught the context menu. | 416 // Translate the page anyway throught the context menu. |
| 386 scoped_ptr<TestRenderViewContextMenu> menu( | 417 scoped_ptr<TestRenderViewContextMenu> menu( |
| 387 TestRenderViewContextMenu::CreateContextMenu(contents())); | 418 TestRenderViewContextMenu::CreateContextMenu(contents())); |
| 388 menu->Init(); | 419 menu->Init(); |
| 389 menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE); | 420 menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE); |
| 390 | 421 |
| 391 // To test that bug #49018 if fixed, make sure we deal correctly with errors. | 422 // To test that bug #49018 if fixed, make sure we deal correctly with errors. |
| 392 SimulateURLFetch(false); // Simulate a failure to fetch the translate script. | 423 // Simulate a failure to fetch the translate script. |
| 424 SimulateTranslateScriptURLFetch(false); |
| 393 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); | 425 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); |
| 394 ASSERT_TRUE(infobar != NULL); | 426 ASSERT_TRUE(infobar != NULL); |
| 395 EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATION_ERROR, infobar->type()); | 427 EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATION_ERROR, infobar->type()); |
| 396 EXPECT_TRUE(infobar->IsError()); | 428 EXPECT_TRUE(infobar->IsError()); |
| 397 infobar->MessageInfoBarButtonPressed(); | 429 infobar->MessageInfoBarButtonPressed(); |
| 398 SimulateURLFetch(true); // This time succeed. | 430 SimulateTranslateScriptURLFetch(true); // This time succeed. |
| 399 | 431 |
| 400 // Simulate the render notifying the translation has been done, the server | 432 // Simulate the render notifying the translation has been done, the server |
| 401 // having detected the page was in a known and supported language. | 433 // having detected the page was in a known and supported language. |
| 402 rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(0, 0, "fr", "en", | 434 rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(0, 0, "fr", "en", |
| 403 TranslateErrors::NONE)); | 435 TranslateErrors::NONE)); |
| 404 | 436 |
| 405 // The after translate infobar should be showing. | 437 // The after translate infobar should be showing. |
| 406 infobar = GetTranslateInfoBar(); | 438 infobar = GetTranslateInfoBar(); |
| 407 ASSERT_TRUE(infobar != NULL); | 439 ASSERT_TRUE(infobar != NULL); |
| 408 EXPECT_EQ(TranslateInfoBarDelegate::AFTER_TRANSLATE, infobar->type()); | 440 EXPECT_EQ(TranslateInfoBarDelegate::AFTER_TRANSLATE, infobar->type()); |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 499 // Verify we have/don't have an info-bar as expected. | 531 // Verify we have/don't have an info-bar as expected. |
| 500 infobar = GetTranslateInfoBar(); | 532 infobar = GetTranslateInfoBar(); |
| 501 EXPECT_EQ(kExpectations[i], infobar != NULL); | 533 EXPECT_EQ(kExpectations[i], infobar != NULL); |
| 502 | 534 |
| 503 // Close the info-bar if applicable. | 535 // Close the info-bar if applicable. |
| 504 if (infobar != NULL) | 536 if (infobar != NULL) |
| 505 EXPECT_TRUE(CloseTranslateInfoBar()); | 537 EXPECT_TRUE(CloseTranslateInfoBar()); |
| 506 } | 538 } |
| 507 } | 539 } |
| 508 | 540 |
| 541 // Test the fetching of languages from the translate server |
| 542 TEST_F(TranslateManagerTest, FetchLanguagesFromTranslateServer) { |
| 543 std::vector<std::string> server_languages; |
| 544 // A list of languages to fake being returned by the translate server. |
| 545 server_languages.push_back("aa"); |
| 546 server_languages.push_back("bb"); |
| 547 server_languages.push_back("ab"); |
| 548 server_languages.push_back("en-CA"); |
| 549 server_languages.push_back("zz"); |
| 550 server_languages.push_back("yy"); |
| 551 server_languages.push_back("fr-FR"); |
| 552 |
| 553 // First, get the default languages list: |
| 554 std::vector<std::string> default_supported_languages; |
| 555 TranslateManager::GetSupportedLanguages(&default_supported_languages); |
| 556 // To make sure we got the defaults and don't confuse them with the mocks. |
| 557 ASSERT_NE(default_supported_languages.size(), server_languages.size()); |
| 558 |
| 559 TranslateManager::GetInstance()->FetchLanguageListFromTranslateServer( |
| 560 contents()->profile()->GetPrefs()); |
| 561 |
| 562 // Check that we still get the defaults until the URLFetch has completed. |
| 563 std::vector<std::string> current_supported_languages; |
| 564 TranslateManager::GetSupportedLanguages(¤t_supported_languages); |
| 565 EXPECT_EQ(default_supported_languages, current_supported_languages); |
| 566 |
| 567 // Also check that it didn't change if we failed the URL fetch. |
| 568 SimulateSupportedLanguagesURLFetch(false, std::vector<std::string>()); |
| 569 current_supported_languages.clear(); |
| 570 TranslateManager::GetSupportedLanguages(¤t_supported_languages); |
| 571 EXPECT_EQ(default_supported_languages, current_supported_languages); |
| 572 |
| 573 // Now check that we got the appropriate set of languages from the server. |
| 574 TranslateManager::GetInstance()->FetchLanguageListFromTranslateServer( |
| 575 contents()->profile()->GetPrefs()); |
| 576 SimulateSupportedLanguagesURLFetch(true, server_languages); |
| 577 current_supported_languages.clear(); |
| 578 TranslateManager::GetSupportedLanguages(¤t_supported_languages); |
| 579 // Not sure we need to guarantee the order of languages, so we find them. |
| 580 EXPECT_EQ(server_languages.size(), current_supported_languages.size()); |
| 581 for (size_t i = 0; i < server_languages.size(); ++i) { |
| 582 EXPECT_NE(current_supported_languages.end(), |
| 583 std::find(current_supported_languages.begin(), |
| 584 current_supported_languages.end(), |
| 585 server_languages[i])); |
| 586 } |
| 587 |
| 588 // Reset to original state. |
| 589 TranslateManager::GetInstance()->FetchLanguageListFromTranslateServer( |
| 590 contents()->profile()->GetPrefs()); |
| 591 SimulateSupportedLanguagesURLFetch(true, default_supported_languages); |
| 592 } |
| 593 |
| 509 // Tests auto-translate on page. | 594 // Tests auto-translate on page. |
| 510 TEST_F(TranslateManagerTest, AutoTranslateOnNavigate) { | 595 TEST_F(TranslateManagerTest, AutoTranslateOnNavigate) { |
| 511 // Simulate navigating to a page and getting its language. | 596 // Simulate navigating to a page and getting its language. |
| 512 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); | 597 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); |
| 513 | 598 |
| 514 // Simulate the user translating. | 599 // Simulate the user translating. |
| 515 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); | 600 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); |
| 516 ASSERT_TRUE(infobar != NULL); | 601 ASSERT_TRUE(infobar != NULL); |
| 517 infobar->Translate(); | 602 infobar->Translate(); |
| 518 SimulateURLFetch(true); // Simulate the translate script being retrieved. | 603 // Simulate the translate script being retrieved. |
| 604 SimulateTranslateScriptURLFetch(true); |
| 519 | 605 |
| 520 rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(0, 0, "fr", "en", | 606 rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(0, 0, "fr", "en", |
| 521 TranslateErrors::NONE)); | 607 TranslateErrors::NONE)); |
| 522 | 608 |
| 523 // Now navigate to a new page in the same language. | 609 // Now navigate to a new page in the same language. |
| 524 process()->sink().ClearMessages(); | 610 process()->sink().ClearMessages(); |
| 525 SimulateNavigation(GURL("http://news.google.fr"), "fr", true); | 611 SimulateNavigation(GURL("http://news.google.fr"), "fr", true); |
| 526 | 612 |
| 527 // This should have automatically triggered a translation. | 613 // This should have automatically triggered a translation. |
| 528 int page_id = 0; | 614 int page_id = 0; |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 680 // Tests that after translating and closing the infobar, the infobar does not | 766 // Tests that after translating and closing the infobar, the infobar does not |
| 681 // return when navigating in page. | 767 // return when navigating in page. |
| 682 TEST_F(TranslateManagerTest, TranslateCloseInfoBarInPageNavigation) { | 768 TEST_F(TranslateManagerTest, TranslateCloseInfoBarInPageNavigation) { |
| 683 // Simulate navigating to a page and getting its language. | 769 // Simulate navigating to a page and getting its language. |
| 684 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); | 770 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); |
| 685 | 771 |
| 686 // Simulate the user translating. | 772 // Simulate the user translating. |
| 687 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); | 773 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); |
| 688 ASSERT_TRUE(infobar != NULL); | 774 ASSERT_TRUE(infobar != NULL); |
| 689 infobar->Translate(); | 775 infobar->Translate(); |
| 690 SimulateURLFetch(true); // Simulate the translate script being retrieved. | 776 // Simulate the translate script being retrieved. |
| 777 SimulateTranslateScriptURLFetch(true); |
| 691 rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(0, 0, "fr", "en", | 778 rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(0, 0, "fr", "en", |
| 692 TranslateErrors::NONE)); | 779 TranslateErrors::NONE)); |
| 693 | 780 |
| 694 // Close the infobar. | 781 // Close the infobar. |
| 695 EXPECT_TRUE(CloseTranslateInfoBar()); | 782 EXPECT_TRUE(CloseTranslateInfoBar()); |
| 696 | 783 |
| 697 // Navigate in page, no infobar should be shown. | 784 // Navigate in page, no infobar should be shown. |
| 698 SimulateNavigation(GURL("http://www.google.fr/#ref1"), "fr", true); | 785 SimulateNavigation(GURL("http://www.google.fr/#ref1"), "fr", true); |
| 699 EXPECT_TRUE(GetTranslateInfoBar() == NULL); | 786 EXPECT_TRUE(GetTranslateInfoBar() == NULL); |
| 700 | 787 |
| 701 // Navigate out of page, a new infobar should show. | 788 // Navigate out of page, a new infobar should show. |
| 702 // Note that we navigate to a page in a different language so we don't trigger | 789 // Note that we navigate to a page in a different language so we don't trigger |
| 703 // the auto-translate feature (it would translate the page automatically and | 790 // the auto-translate feature (it would translate the page automatically and |
| 704 // the before translate inforbar would not be shown). | 791 // the before translate inforbar would not be shown). |
| 705 SimulateNavigation(GURL("http://www.google.de"), "de", true); | 792 SimulateNavigation(GURL("http://www.google.de"), "de", true); |
| 706 EXPECT_TRUE(GetTranslateInfoBar() != NULL); | 793 EXPECT_TRUE(GetTranslateInfoBar() != NULL); |
| 707 } | 794 } |
| 708 | 795 |
| 709 // Tests that the after translate the infobar still shows when navigating | 796 // Tests that the after translate the infobar still shows when navigating |
| 710 // in-page. | 797 // in-page. |
| 711 TEST_F(TranslateManagerTest, TranslateInPageNavigation) { | 798 TEST_F(TranslateManagerTest, TranslateInPageNavigation) { |
| 712 // Simulate navigating to a page and getting its language. | 799 // Simulate navigating to a page and getting its language. |
| 713 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); | 800 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); |
| 714 | 801 |
| 715 // Simulate the user translating. | 802 // Simulate the user translating. |
| 716 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); | 803 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); |
| 717 ASSERT_TRUE(infobar != NULL); | 804 ASSERT_TRUE(infobar != NULL); |
| 718 infobar->Translate(); | 805 infobar->Translate(); |
| 719 SimulateURLFetch(true); // Simulate the translate script being retrieved. | 806 // Simulate the translate script being retrieved. |
| 807 SimulateTranslateScriptURLFetch(true); |
| 720 rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(0, 0, "fr", "en", | 808 rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(0, 0, "fr", "en", |
| 721 TranslateErrors::NONE)); | 809 TranslateErrors::NONE)); |
| 722 // The after translate infobar is showing. | 810 // The after translate infobar is showing. |
| 723 infobar = GetTranslateInfoBar(); | 811 infobar = GetTranslateInfoBar(); |
| 724 ASSERT_TRUE(infobar != NULL); | 812 ASSERT_TRUE(infobar != NULL); |
| 725 | 813 |
| 726 // Navigate out of page, a new infobar should show. | 814 // Navigate out of page, a new infobar should show. |
| 727 // See note in TranslateCloseInfoBarInPageNavigation test on why it is | 815 // See note in TranslateCloseInfoBarInPageNavigation test on why it is |
| 728 // important to navigate to a page in a different language for this test. | 816 // important to navigate to a page in a different language for this test. |
| 729 SimulateNavigation(GURL("http://www.google.de"), "de", true); | 817 SimulateNavigation(GURL("http://www.google.de"), "de", true); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 746 // Tests that we deal correctly with unsupported languages returned by the | 834 // Tests that we deal correctly with unsupported languages returned by the |
| 747 // server. | 835 // server. |
| 748 // The translation server might return a language we don't support. | 836 // The translation server might return a language we don't support. |
| 749 TEST_F(TranslateManagerTest, ServerReportsUnsupportedLanguage) { | 837 TEST_F(TranslateManagerTest, ServerReportsUnsupportedLanguage) { |
| 750 // Simulate navigating to a page and translating it. | 838 // Simulate navigating to a page and translating it. |
| 751 SimulateNavigation(GURL("http://mail.google.fr"), "fr", true); | 839 SimulateNavigation(GURL("http://mail.google.fr"), "fr", true); |
| 752 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); | 840 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); |
| 753 ASSERT_TRUE(infobar != NULL); | 841 ASSERT_TRUE(infobar != NULL); |
| 754 process()->sink().ClearMessages(); | 842 process()->sink().ClearMessages(); |
| 755 infobar->Translate(); | 843 infobar->Translate(); |
| 756 SimulateURLFetch(true); | 844 SimulateTranslateScriptURLFetch(true); |
| 757 // Simulate the render notifying the translation has been done, but it | 845 // Simulate the render notifying the translation has been done, but it |
| 758 // reports a language we don't support. | 846 // reports a language we don't support. |
| 759 rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(0, 0, "qbz", "en", | 847 rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(0, 0, "qbz", "en", |
| 760 TranslateErrors::NONE)); | 848 TranslateErrors::NONE)); |
| 761 | 849 |
| 762 // An error infobar should be showing to report that we don't support this | 850 // An error infobar should be showing to report that we don't support this |
| 763 // language. | 851 // language. |
| 764 infobar = GetTranslateInfoBar(); | 852 infobar = GetTranslateInfoBar(); |
| 765 ASSERT_TRUE(infobar != NULL); | 853 ASSERT_TRUE(infobar != NULL); |
| 766 EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATION_ERROR, infobar->type()); | 854 EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATION_ERROR, infobar->type()); |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 929 | 1017 |
| 930 // Load a page in French. | 1018 // Load a page in French. |
| 931 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); | 1019 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); |
| 932 | 1020 |
| 933 // It should have triggered an automatic translation to English. | 1021 // It should have triggered an automatic translation to English. |
| 934 | 1022 |
| 935 // The translating infobar should be showing. | 1023 // The translating infobar should be showing. |
| 936 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); | 1024 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); |
| 937 ASSERT_TRUE(infobar != NULL); | 1025 ASSERT_TRUE(infobar != NULL); |
| 938 EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATING, infobar->type()); | 1026 EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATING, infobar->type()); |
| 939 | 1027 // Simulate the translate script being retrieved. |
| 940 SimulateURLFetch(true); // Simulate the translate script being retrieved. | 1028 SimulateTranslateScriptURLFetch(true); |
| 941 int page_id = 0; | 1029 int page_id = 0; |
| 942 std::string original_lang, target_lang; | 1030 std::string original_lang, target_lang; |
| 943 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); | 1031 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); |
| 944 EXPECT_EQ("fr", original_lang); | 1032 EXPECT_EQ("fr", original_lang); |
| 945 EXPECT_EQ("en", target_lang); | 1033 EXPECT_EQ("en", target_lang); |
| 946 process()->sink().ClearMessages(); | 1034 process()->sink().ClearMessages(); |
| 947 | 1035 |
| 948 // Try another language, it should not be autotranslated. | 1036 // Try another language, it should not be autotranslated. |
| 949 SimulateNavigation(GURL("http://www.google.es"), "es", true); | 1037 SimulateNavigation(GURL("http://www.google.es"), "es", true); |
| 950 EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); | 1038 EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1000 EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE)); | 1088 EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE)); |
| 1001 | 1089 |
| 1002 // Use the menu to translate the page. | 1090 // Use the menu to translate the page. |
| 1003 menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE); | 1091 menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE); |
| 1004 | 1092 |
| 1005 // That should have triggered a translation. | 1093 // That should have triggered a translation. |
| 1006 // The "translating..." infobar should be showing. | 1094 // The "translating..." infobar should be showing. |
| 1007 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); | 1095 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); |
| 1008 ASSERT_TRUE(infobar != NULL); | 1096 ASSERT_TRUE(infobar != NULL); |
| 1009 EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATING, infobar->type()); | 1097 EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATING, infobar->type()); |
| 1010 SimulateURLFetch(true); // Simulate the translate script being retrieved. | 1098 // Simulate the translate script being retrieved. |
| 1099 SimulateTranslateScriptURLFetch(true); |
| 1011 int page_id = 0; | 1100 int page_id = 0; |
| 1012 std::string original_lang, target_lang; | 1101 std::string original_lang, target_lang; |
| 1013 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); | 1102 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); |
| 1014 EXPECT_EQ("fr", original_lang); | 1103 EXPECT_EQ("fr", original_lang); |
| 1015 EXPECT_EQ("en", target_lang); | 1104 EXPECT_EQ("en", target_lang); |
| 1016 process()->sink().ClearMessages(); | 1105 process()->sink().ClearMessages(); |
| 1017 | 1106 |
| 1018 // This should also have reverted the blacklisting of this site and language. | 1107 // This should also have reverted the blacklisting of this site and language. |
| 1019 EXPECT_FALSE(translate_prefs.IsLanguageBlacklisted("fr")); | 1108 EXPECT_FALSE(translate_prefs.IsLanguageBlacklisted("fr")); |
| 1020 EXPECT_FALSE(translate_prefs.IsSiteBlacklisted(url.host())); | 1109 EXPECT_FALSE(translate_prefs.IsSiteBlacklisted(url.host())); |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1110 EXPECT_TRUE(infobar->ShouldShowAlwaysTranslateButton()); | 1199 EXPECT_TRUE(infobar->ShouldShowAlwaysTranslateButton()); |
| 1111 } | 1200 } |
| 1112 if (i == 3) | 1201 if (i == 3) |
| 1113 test_profile->set_incognito(false); | 1202 test_profile->set_incognito(false); |
| 1114 } | 1203 } |
| 1115 // Simulate the user pressing "Always translate French". | 1204 // Simulate the user pressing "Always translate French". |
| 1116 infobar->AlwaysTranslatePageLanguage(); | 1205 infobar->AlwaysTranslatePageLanguage(); |
| 1117 EXPECT_TRUE(translate_prefs.IsLanguagePairWhitelisted("fr", "en")); | 1206 EXPECT_TRUE(translate_prefs.IsLanguagePairWhitelisted("fr", "en")); |
| 1118 // Simulate the translate script being retrieved (it only needs to be done | 1207 // Simulate the translate script being retrieved (it only needs to be done |
| 1119 // once in the test as it is cached). | 1208 // once in the test as it is cached). |
| 1120 SimulateURLFetch(true); | 1209 SimulateTranslateScriptURLFetch(true); |
| 1121 // That should have triggered a page translate. | 1210 // That should have triggered a page translate. |
| 1122 int page_id = 0; | 1211 int page_id = 0; |
| 1123 std::string original_lang, target_lang; | 1212 std::string original_lang, target_lang; |
| 1124 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); | 1213 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); |
| 1125 process()->sink().ClearMessages(); | 1214 process()->sink().ClearMessages(); |
| 1126 | 1215 |
| 1127 // Now test that declining the translation causes a "never translate" button | 1216 // Now test that declining the translation causes a "never translate" button |
| 1128 // to be shown (in non incognito mode only). | 1217 // to be shown (in non incognito mode only). |
| 1129 test_profile->set_incognito(true); | 1218 test_profile->set_incognito(true); |
| 1130 for (int i = 0; i < 8; ++i) { | 1219 for (int i = 0; i < 8; ++i) { |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1172 // Tests that the script is expired and refetched as expected. | 1261 // Tests that the script is expired and refetched as expected. |
| 1173 TEST_F(TranslateManagerTest, ScriptExpires) { | 1262 TEST_F(TranslateManagerTest, ScriptExpires) { |
| 1174 ExpireTranslateScriptImmediately(); | 1263 ExpireTranslateScriptImmediately(); |
| 1175 | 1264 |
| 1176 // Simulate navigating to a page and translating it. | 1265 // Simulate navigating to a page and translating it. |
| 1177 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); | 1266 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); |
| 1178 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); | 1267 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); |
| 1179 ASSERT_TRUE(infobar != NULL); | 1268 ASSERT_TRUE(infobar != NULL); |
| 1180 process()->sink().ClearMessages(); | 1269 process()->sink().ClearMessages(); |
| 1181 infobar->Translate(); | 1270 infobar->Translate(); |
| 1182 SimulateURLFetch(true); | 1271 SimulateTranslateScriptURLFetch(true); |
| 1183 rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(0, 0, "fr", "en", | 1272 rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(0, 0, "fr", "en", |
| 1184 TranslateErrors::NONE)); | 1273 TranslateErrors::NONE)); |
| 1185 | 1274 |
| 1186 // A task should have been posted to clear the script, run it. | 1275 // A task should have been posted to clear the script, run it. |
| 1187 MessageLoop::current()->RunAllPending(); | 1276 MessageLoop::current()->RunAllPending(); |
| 1188 | 1277 |
| 1189 // Do another navigation and translation. | 1278 // Do another navigation and translation. |
| 1190 SimulateNavigation(GURL("http://www.google.es"), "es", true); | 1279 SimulateNavigation(GURL("http://www.google.es"), "es", true); |
| 1191 infobar = GetTranslateInfoBar(); | 1280 infobar = GetTranslateInfoBar(); |
| 1192 ASSERT_TRUE(infobar != NULL); | 1281 ASSERT_TRUE(infobar != NULL); |
| 1193 process()->sink().ClearMessages(); | 1282 process()->sink().ClearMessages(); |
| 1194 infobar->Translate(); | 1283 infobar->Translate(); |
| 1195 // If we don't simulate the URL fetch, the TranslateManager should be waiting | 1284 // If we don't simulate the URL fetch, the TranslateManager should be waiting |
| 1196 // for the script and no message should have been sent to the renderer. | 1285 // for the script and no message should have been sent to the renderer. |
| 1197 EXPECT_TRUE( | 1286 EXPECT_TRUE( |
| 1198 process()->sink().GetFirstMessageMatching(ViewMsg_TranslatePage::ID) == | 1287 process()->sink().GetFirstMessageMatching(ViewMsg_TranslatePage::ID) == |
| 1199 NULL); | 1288 NULL); |
| 1200 // Now simulate the URL fetch. | 1289 // Now simulate the URL fetch. |
| 1201 SimulateURLFetch(true); | 1290 SimulateTranslateScriptURLFetch(true); |
| 1202 // Now the message should have been sent. | 1291 // Now the message should have been sent. |
| 1203 int page_id = 0; | 1292 int page_id = 0; |
| 1204 std::string original_lang, target_lang; | 1293 std::string original_lang, target_lang; |
| 1205 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); | 1294 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); |
| 1206 EXPECT_EQ("es", original_lang); | 1295 EXPECT_EQ("es", original_lang); |
| 1207 EXPECT_EQ("en", target_lang); | 1296 EXPECT_EQ("en", target_lang); |
| 1208 } | 1297 } |
| OLD | NEW |