| OLD | NEW |
| 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 "base/utf_string_conversions.h" | |
| 8 #include "chrome/app/chrome_dll_resource.h" | 7 #include "chrome/app/chrome_dll_resource.h" |
| 9 #include "chrome/browser/renderer_host/mock_render_process_host.h" | 8 #include "chrome/browser/renderer_host/mock_render_process_host.h" |
| 10 #include "chrome/browser/tab_contents/render_view_context_menu.h" | 9 #include "chrome/browser/tab_contents/render_view_context_menu.h" |
| 11 #include "chrome/browser/translate/translate_infobars_delegates.h" | 10 #include "chrome/browser/translate/translate_infobars_delegates.h" |
| 12 #include "chrome/browser/translate/translate_manager.h" | 11 #include "chrome/browser/translate/translate_manager.h" |
| 13 #include "chrome/browser/translate/translate_prefs.h" | 12 #include "chrome/browser/translate/translate_prefs.h" |
| 14 #include "chrome/common/ipc_test_sink.h" | 13 #include "chrome/common/ipc_test_sink.h" |
| 15 #include "chrome/common/notification_details.h" | 14 #include "chrome/common/notification_details.h" |
| 16 #include "chrome/common/notification_observer_mock.h" | 15 #include "chrome/common/notification_observer_mock.h" |
| 17 #include "chrome/common/notification_registrar.h" | 16 #include "chrome/common/notification_registrar.h" |
| (...skipping 17 matching lines...) Expand all Loading... |
| 35 }; | 34 }; |
| 36 | 35 |
| 37 class TranslateManagerTest : public RenderViewHostTestHarness, | 36 class TranslateManagerTest : public RenderViewHostTestHarness, |
| 38 public NotificationObserver { | 37 public NotificationObserver { |
| 39 public: | 38 public: |
| 40 TranslateManagerTest() {} | 39 TranslateManagerTest() {} |
| 41 | 40 |
| 42 // Simluates navigating to a page and getting the page contents and language | 41 // Simluates navigating to a page and getting the page contents and language |
| 43 // for that navigation. | 42 // for that navigation. |
| 44 void SimulateNavigation(const GURL& url, int page_id, | 43 void SimulateNavigation(const GURL& url, int page_id, |
| 45 const std::string& contents, | 44 const std::wstring& contents, |
| 46 const std::string& lang) { | 45 const std::string& lang) { |
| 47 NavigateAndCommit(url); | 46 NavigateAndCommit(url); |
| 48 SimulateOnPageContents(url, page_id, contents, lang); | 47 SimulateOnPageContents(url, page_id, contents, lang); |
| 49 } | 48 } |
| 50 | 49 |
| 51 void SimulateOnPageContents(const GURL& url, int page_id, | 50 void SimulateOnPageContents(const GURL& url, int page_id, |
| 52 const std::string& contents, | 51 const std::wstring& contents, |
| 53 const std::string& lang) { | 52 const std::string& lang) { |
| 54 rvh()->TestOnMessageReceived(ViewHostMsg_PageContents(0, url, page_id, | 53 rvh()->TestOnMessageReceived(ViewHostMsg_PageContents(0, url, page_id, |
| 55 UTF8ToUTF16(contents), | 54 contents, lang)); |
| 56 lang)); | |
| 57 } | 55 } |
| 58 | 56 |
| 59 bool GetTranslateMessage(int* page_id, | 57 bool GetTranslateMessage(int* page_id, |
| 60 std::string* original_lang, | 58 std::string* original_lang, |
| 61 std::string* target_lang) { | 59 std::string* target_lang) { |
| 62 const IPC::Message* message = | 60 const IPC::Message* message = |
| 63 process()->sink().GetFirstMessageMatching(ViewMsg_TranslatePage::ID); | 61 process()->sink().GetFirstMessageMatching(ViewMsg_TranslatePage::ID); |
| 64 if (!message) | 62 if (!message) |
| 65 return false; | 63 return false; |
| 66 Tuple4<int, std::string, std::string, std::string> translate_param; | 64 Tuple4<int, std::string, std::string, std::string> translate_param; |
| (...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 254 TestRenderViewContextMenu(TabContents* tab_contents, | 252 TestRenderViewContextMenu(TabContents* tab_contents, |
| 255 const ContextMenuParams& params) | 253 const ContextMenuParams& params) |
| 256 : RenderViewContextMenu(tab_contents, params) { | 254 : RenderViewContextMenu(tab_contents, params) { |
| 257 } | 255 } |
| 258 | 256 |
| 259 DISALLOW_COPY_AND_ASSIGN(TestRenderViewContextMenu); | 257 DISALLOW_COPY_AND_ASSIGN(TestRenderViewContextMenu); |
| 260 }; | 258 }; |
| 261 | 259 |
| 262 TEST_F(TranslateManagerTest, NormalTranslate) { | 260 TEST_F(TranslateManagerTest, NormalTranslate) { |
| 263 // Simulate navigating to a page. | 261 // Simulate navigating to a page. |
| 264 SimulateNavigation(GURL("http://www.google.fr"), 0, "Le Google", "fr"); | 262 SimulateNavigation(GURL("http://www.google.fr"), 0, L"Le Google", "fr"); |
| 265 | 263 |
| 266 // We should have an info-bar. | 264 // We should have an info-bar. |
| 267 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); | 265 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); |
| 268 ASSERT_TRUE(infobar != NULL); | 266 ASSERT_TRUE(infobar != NULL); |
| 269 EXPECT_EQ(TranslateInfoBarDelegate::kBeforeTranslate, infobar->state()); | 267 EXPECT_EQ(TranslateInfoBarDelegate::kBeforeTranslate, infobar->state()); |
| 270 | 268 |
| 271 // Simulate clicking translate. | 269 // Simulate clicking translate. |
| 272 process()->sink().ClearMessages(); | 270 process()->sink().ClearMessages(); |
| 273 infobar->Translate(); | 271 infobar->Translate(); |
| 274 // Simulate the translate script being retrieved (it only needs to be done | 272 // Simulate the translate script being retrieved (it only needs to be done |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 315 | 313 |
| 316 // Test that we sent the right message to the renderer. | 314 // Test that we sent the right message to the renderer. |
| 317 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); | 315 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); |
| 318 EXPECT_EQ(0, page_id); | 316 EXPECT_EQ(0, page_id); |
| 319 EXPECT_EQ(new_original_lang, original_lang); | 317 EXPECT_EQ(new_original_lang, original_lang); |
| 320 EXPECT_EQ(new_target_lang, target_lang); | 318 EXPECT_EQ(new_target_lang, target_lang); |
| 321 } | 319 } |
| 322 | 320 |
| 323 TEST_F(TranslateManagerTest, TranslateScriptNotAvailable) { | 321 TEST_F(TranslateManagerTest, TranslateScriptNotAvailable) { |
| 324 // Simulate navigating to a page. | 322 // Simulate navigating to a page. |
| 325 SimulateNavigation(GURL("http://www.google.fr"), 0, "Le Google", "fr"); | 323 SimulateNavigation(GURL("http://www.google.fr"), 0, L"Le Google", "fr"); |
| 326 | 324 |
| 327 // We should have an info-bar. | 325 // We should have an info-bar. |
| 328 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); | 326 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); |
| 329 ASSERT_TRUE(infobar != NULL); | 327 ASSERT_TRUE(infobar != NULL); |
| 330 EXPECT_EQ(TranslateInfoBarDelegate::kBeforeTranslate, infobar->state()); | 328 EXPECT_EQ(TranslateInfoBarDelegate::kBeforeTranslate, infobar->state()); |
| 331 | 329 |
| 332 // Simulate clicking translate. | 330 // Simulate clicking translate. |
| 333 process()->sink().ClearMessages(); | 331 process()->sink().ClearMessages(); |
| 334 infobar->Translate(); | 332 infobar->Translate(); |
| 335 // Simulate a failure retrieving the translate script. | 333 // Simulate a failure retrieving the translate script. |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 396 std::string lang = LanguageCodeWithDialects(static_cast<Language>(i)); | 394 std::string lang = LanguageCodeWithDialects(static_cast<Language>(i)); |
| 397 SCOPED_TRACE(::testing::Message::Message() << "Iteration " << i << | 395 SCOPED_TRACE(::testing::Message::Message() << "Iteration " << i << |
| 398 " language=" << lang); | 396 " language=" << lang); |
| 399 | 397 |
| 400 // We should not have a translate infobar. | 398 // We should not have a translate infobar. |
| 401 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); | 399 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); |
| 402 ASSERT_TRUE(infobar == NULL); | 400 ASSERT_TRUE(infobar == NULL); |
| 403 | 401 |
| 404 // Simulate navigating to a page. | 402 // Simulate navigating to a page. |
| 405 NavigateAndCommit(url); | 403 NavigateAndCommit(url); |
| 406 SimulateOnPageContents(url, i, "", lang); | 404 SimulateOnPageContents(url, i, L"", lang); |
| 407 | 405 |
| 408 // Verify we have/don't have an info-bar as expected. | 406 // Verify we have/don't have an info-bar as expected. |
| 409 infobar = GetTranslateInfoBar(); | 407 infobar = GetTranslateInfoBar(); |
| 410 EXPECT_EQ(kExpectations[i], infobar != NULL); | 408 EXPECT_EQ(kExpectations[i], infobar != NULL); |
| 411 | 409 |
| 412 // Close the info-bar if applicable. | 410 // Close the info-bar if applicable. |
| 413 if (infobar != NULL) | 411 if (infobar != NULL) |
| 414 EXPECT_TRUE(CloseTranslateInfoBar()); | 412 EXPECT_TRUE(CloseTranslateInfoBar()); |
| 415 } | 413 } |
| 416 } | 414 } |
| 417 | 415 |
| 418 // Tests auto-translate on page. | 416 // Tests auto-translate on page. |
| 419 TEST_F(TranslateManagerTest, AutoTranslateOnNavigate) { | 417 TEST_F(TranslateManagerTest, AutoTranslateOnNavigate) { |
| 420 // Simulate navigating to a page and getting its language. | 418 // Simulate navigating to a page and getting its language. |
| 421 SimulateNavigation(GURL("http://www.google.fr"), 0, "Le Google", "fr"); | 419 SimulateNavigation(GURL("http://www.google.fr"), 0, L"Le Google", "fr"); |
| 422 | 420 |
| 423 // Simulate the user translating. | 421 // Simulate the user translating. |
| 424 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); | 422 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); |
| 425 ASSERT_TRUE(infobar != NULL); | 423 ASSERT_TRUE(infobar != NULL); |
| 426 infobar->Translate(); | 424 infobar->Translate(); |
| 427 SimulateURLFetch(true); // Simulate the translate script being retrieved. | 425 SimulateURLFetch(true); // Simulate the translate script being retrieved. |
| 428 | 426 |
| 429 rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(0, 0, "fr", "en", | 427 rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(0, 0, "fr", "en", |
| 430 TranslateErrors::NONE)); | 428 TranslateErrors::NONE)); |
| 431 | 429 |
| 432 // Now navigate to a new page in the same language. | 430 // Now navigate to a new page in the same language. |
| 433 process()->sink().ClearMessages(); | 431 process()->sink().ClearMessages(); |
| 434 SimulateNavigation(GURL("http://news.google.fr"), 1, "Les news", "fr"); | 432 SimulateNavigation(GURL("http://news.google.fr"), 1, L"Les news", "fr"); |
| 435 | 433 |
| 436 // This should have automatically triggered a translation. | 434 // This should have automatically triggered a translation. |
| 437 int page_id = 0; | 435 int page_id = 0; |
| 438 std::string original_lang, target_lang; | 436 std::string original_lang, target_lang; |
| 439 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); | 437 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); |
| 440 EXPECT_EQ(1, page_id); | 438 EXPECT_EQ(1, page_id); |
| 441 EXPECT_EQ("fr", original_lang); | 439 EXPECT_EQ("fr", original_lang); |
| 442 EXPECT_EQ("en", target_lang); | 440 EXPECT_EQ("en", target_lang); |
| 443 | 441 |
| 444 // Now navigate to a page in a different language. | 442 // Now navigate to a page in a different language. |
| 445 process()->sink().ClearMessages(); | 443 process()->sink().ClearMessages(); |
| 446 SimulateNavigation(GURL("http://news.google.es"), 1, "Las news", "es"); | 444 SimulateNavigation(GURL("http://news.google.es"), 1, L"Las news", "es"); |
| 447 | 445 |
| 448 // This should not have triggered a translate. | 446 // This should not have triggered a translate. |
| 449 EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); | 447 EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); |
| 450 } | 448 } |
| 451 | 449 |
| 452 // Tests that multiple OnPageContents do not cause multiple infobars. | 450 // Tests that multiple OnPageContents do not cause multiple infobars. |
| 453 TEST_F(TranslateManagerTest, MultipleOnPageContents) { | 451 TEST_F(TranslateManagerTest, MultipleOnPageContents) { |
| 454 // Simulate navigating to a page and getting its language. | 452 // Simulate navigating to a page and getting its language. |
| 455 SimulateNavigation(GURL("http://www.google.fr"), 0, "Le Google", "fr"); | 453 SimulateNavigation(GURL("http://www.google.fr"), 0, L"Le Google", "fr"); |
| 456 | 454 |
| 457 // Simulate clicking 'Nope' (don't translate). | 455 // Simulate clicking 'Nope' (don't translate). |
| 458 EXPECT_TRUE(DenyTranslation()); | 456 EXPECT_TRUE(DenyTranslation()); |
| 459 EXPECT_EQ(0, contents()->infobar_delegate_count()); | 457 EXPECT_EQ(0, contents()->infobar_delegate_count()); |
| 460 | 458 |
| 461 // Send a new PageContents, we should not show an infobar. | 459 // Send a new PageContents, we should not show an infobar. |
| 462 SimulateOnPageContents(GURL("http://www.google.fr"), 0, "Le Google", "fr"); | 460 SimulateOnPageContents(GURL("http://www.google.fr"), 0, L"Le Google", "fr"); |
| 463 EXPECT_EQ(0, contents()->infobar_delegate_count()); | 461 EXPECT_EQ(0, contents()->infobar_delegate_count()); |
| 464 | 462 |
| 465 // Do the same steps but simulate closing the infobar this time. | 463 // Do the same steps but simulate closing the infobar this time. |
| 466 SimulateNavigation(GURL("http://www.youtube.fr"), 1, "Le YouTube", "fr"); | 464 SimulateNavigation(GURL("http://www.youtube.fr"), 1, L"Le YouTube", "fr"); |
| 467 EXPECT_TRUE(CloseTranslateInfoBar()); | 465 EXPECT_TRUE(CloseTranslateInfoBar()); |
| 468 EXPECT_EQ(0, contents()->infobar_delegate_count()); | 466 EXPECT_EQ(0, contents()->infobar_delegate_count()); |
| 469 SimulateOnPageContents(GURL("http://www.youtube.fr"), 1, "Le YouTube", "fr"); | 467 SimulateOnPageContents(GURL("http://www.youtube.fr"), 1, L"Le YouTube", "fr"); |
| 470 EXPECT_EQ(0, contents()->infobar_delegate_count()); | 468 EXPECT_EQ(0, contents()->infobar_delegate_count()); |
| 471 } | 469 } |
| 472 | 470 |
| 473 // Test that reloading the page brings back the infobar. | 471 // Test that reloading the page brings back the infobar. |
| 474 TEST_F(TranslateManagerTest, Reload) { | 472 TEST_F(TranslateManagerTest, Reload) { |
| 475 // Simulate navigating to a page and getting its language. | 473 // Simulate navigating to a page and getting its language. |
| 476 SimulateNavigation(GURL("http://www.google.fr"), 0, "Le Google", "fr"); | 474 SimulateNavigation(GURL("http://www.google.fr"), 0, L"Le Google", "fr"); |
| 477 | 475 |
| 478 // Close the infobar. | 476 // Close the infobar. |
| 479 EXPECT_TRUE(CloseTranslateInfoBar()); | 477 EXPECT_TRUE(CloseTranslateInfoBar()); |
| 480 | 478 |
| 481 // Reload should bring back the infobar. | 479 // Reload should bring back the infobar. |
| 482 NavEntryCommittedObserver nav_observer(contents()); | 480 NavEntryCommittedObserver nav_observer(contents()); |
| 483 Reload(); | 481 Reload(); |
| 484 | 482 |
| 485 // Ensures it is really handled a reload. | 483 // Ensures it is really handled a reload. |
| 486 const NavigationController::LoadCommittedDetails& nav_details = | 484 const NavigationController::LoadCommittedDetails& nav_details = |
| 487 nav_observer.get_load_commited_details(); | 485 nav_observer.get_load_commited_details(); |
| 488 EXPECT_TRUE(nav_details.entry != NULL); // There was a navigation. | 486 EXPECT_TRUE(nav_details.entry != NULL); // There was a navigation. |
| 489 EXPECT_EQ(NavigationType::EXISTING_PAGE, nav_details.type); | 487 EXPECT_EQ(NavigationType::EXISTING_PAGE, nav_details.type); |
| 490 | 488 |
| 491 // The TranslateManager class processes the navigation entry committed | 489 // The TranslateManager class processes the navigation entry committed |
| 492 // notification in a posted task; process that task. | 490 // notification in a posted task; process that task. |
| 493 MessageLoop::current()->RunAllPending(); | 491 MessageLoop::current()->RunAllPending(); |
| 494 EXPECT_TRUE(GetTranslateInfoBar() != NULL); | 492 EXPECT_TRUE(GetTranslateInfoBar() != NULL); |
| 495 } | 493 } |
| 496 | 494 |
| 497 // Test that reloading the page by way of typing again the URL in the | 495 // Test that reloading the page by way of typing again the URL in the |
| 498 // location bar brings back the infobar. | 496 // location bar brings back the infobar. |
| 499 TEST_F(TranslateManagerTest, ReloadFromLocationBar) { | 497 TEST_F(TranslateManagerTest, ReloadFromLocationBar) { |
| 500 GURL url("http://www.google.fr"); | 498 GURL url("http://www.google.fr"); |
| 501 | 499 |
| 502 // Simulate navigating to a page and getting its language. | 500 // Simulate navigating to a page and getting its language. |
| 503 SimulateNavigation(url, 0, "Le Google", "fr"); | 501 SimulateNavigation(url, 0, L"Le Google", "fr"); |
| 504 | 502 |
| 505 // Close the infobar. | 503 // Close the infobar. |
| 506 EXPECT_TRUE(CloseTranslateInfoBar()); | 504 EXPECT_TRUE(CloseTranslateInfoBar()); |
| 507 | 505 |
| 508 // Create a pending navigation and simulate a page load. That should be the | 506 // Create a pending navigation and simulate a page load. That should be the |
| 509 // equivalent of typing the URL again in the location bar. | 507 // equivalent of typing the URL again in the location bar. |
| 510 NavEntryCommittedObserver nav_observer(contents()); | 508 NavEntryCommittedObserver nav_observer(contents()); |
| 511 contents()->controller().LoadURL(url, GURL(), PageTransition::TYPED); | 509 contents()->controller().LoadURL(url, GURL(), PageTransition::TYPED); |
| 512 rvh()->SendNavigate(0, url); | 510 rvh()->SendNavigate(0, url); |
| 513 | 511 |
| 514 // Test that we are really getting a same page navigation, the test would be | 512 // Test that we are really getting a same page navigation, the test would be |
| 515 // useless if it was not the case. | 513 // useless if it was not the case. |
| 516 const NavigationController::LoadCommittedDetails& nav_details = | 514 const NavigationController::LoadCommittedDetails& nav_details = |
| 517 nav_observer.get_load_commited_details(); | 515 nav_observer.get_load_commited_details(); |
| 518 EXPECT_TRUE(nav_details.entry != NULL); // There was a navigation. | 516 EXPECT_TRUE(nav_details.entry != NULL); // There was a navigation. |
| 519 EXPECT_EQ(NavigationType::SAME_PAGE, nav_details.type); | 517 EXPECT_EQ(NavigationType::SAME_PAGE, nav_details.type); |
| 520 | 518 |
| 521 // The TranslateManager class processes the navigation entry committed | 519 // The TranslateManager class processes the navigation entry committed |
| 522 // notification in a posted task; process that task. | 520 // notification in a posted task; process that task. |
| 523 MessageLoop::current()->RunAllPending(); | 521 MessageLoop::current()->RunAllPending(); |
| 524 EXPECT_TRUE(GetTranslateInfoBar() != NULL); | 522 EXPECT_TRUE(GetTranslateInfoBar() != NULL); |
| 525 } | 523 } |
| 526 | 524 |
| 527 // Tests that a close translate infobar does not reappear when navigating | 525 // Tests that a close translate infobar does not reappear when navigating |
| 528 // in-page. | 526 // in-page. |
| 529 TEST_F(TranslateManagerTest, CloseInfoBarInPageNavigation) { | 527 TEST_F(TranslateManagerTest, CloseInfoBarInPageNavigation) { |
| 530 // Simulate navigating to a page and getting its language. | 528 // Simulate navigating to a page and getting its language. |
| 531 SimulateNavigation(GURL("http://www.google.fr"), 0, "Le Google", "fr"); | 529 SimulateNavigation(GURL("http://www.google.fr"), 0, L"Le Google", "fr"); |
| 532 | 530 |
| 533 // Close the infobar. | 531 // Close the infobar. |
| 534 EXPECT_TRUE(CloseTranslateInfoBar()); | 532 EXPECT_TRUE(CloseTranslateInfoBar()); |
| 535 | 533 |
| 536 // Navigate in page, no infobar should be shown. | 534 // Navigate in page, no infobar should be shown. |
| 537 SimulateNavigation(GURL("http://www.google.fr/#ref1"), 0, "Le Google", "fr"); | 535 SimulateNavigation(GURL("http://www.google.fr/#ref1"), 0, L"Le Google", "fr"); |
| 538 EXPECT_TRUE(GetTranslateInfoBar() == NULL); | 536 EXPECT_TRUE(GetTranslateInfoBar() == NULL); |
| 539 | 537 |
| 540 // Navigate out of page, a new infobar should show. | 538 // Navigate out of page, a new infobar should show. |
| 541 SimulateNavigation(GURL("http://www.google.fr/foot"), 0, "Le Google", "fr"); | 539 SimulateNavigation(GURL("http://www.google.fr/foot"), 0, L"Le Google", "fr"); |
| 542 EXPECT_TRUE(GetTranslateInfoBar() != NULL); | 540 EXPECT_TRUE(GetTranslateInfoBar() != NULL); |
| 543 } | 541 } |
| 544 | 542 |
| 545 // Tests that denying translation is sticky when navigating in page. | 543 // Tests that denying translation is sticky when navigating in page. |
| 546 TEST_F(TranslateManagerTest, DenyTranslateInPageNavigation) { | 544 TEST_F(TranslateManagerTest, DenyTranslateInPageNavigation) { |
| 547 // Simulate navigating to a page and getting its language. | 545 // Simulate navigating to a page and getting its language. |
| 548 SimulateNavigation(GURL("http://www.google.fr"), 0, "Le Google", "fr"); | 546 SimulateNavigation(GURL("http://www.google.fr"), 0, L"Le Google", "fr"); |
| 549 | 547 |
| 550 // Simulate clicking 'Nope' (don't translate). | 548 // Simulate clicking 'Nope' (don't translate). |
| 551 EXPECT_TRUE(DenyTranslation()); | 549 EXPECT_TRUE(DenyTranslation()); |
| 552 | 550 |
| 553 // Navigate in page, no infobar should be shown. | 551 // Navigate in page, no infobar should be shown. |
| 554 SimulateNavigation(GURL("http://www.google.fr/#ref1"), 0, "Le Google", "fr"); | 552 SimulateNavigation(GURL("http://www.google.fr/#ref1"), 0, L"Le Google", "fr"); |
| 555 EXPECT_TRUE(GetTranslateInfoBar() == NULL); | 553 EXPECT_TRUE(GetTranslateInfoBar() == NULL); |
| 556 | 554 |
| 557 // Navigate out of page, a new infobar should show. | 555 // Navigate out of page, a new infobar should show. |
| 558 SimulateNavigation(GURL("http://www.google.fr/foot"), 0, "Le Google", "fr"); | 556 SimulateNavigation(GURL("http://www.google.fr/foot"), 0, L"Le Google", "fr"); |
| 559 EXPECT_TRUE(GetTranslateInfoBar() != NULL); | 557 EXPECT_TRUE(GetTranslateInfoBar() != NULL); |
| 560 } | 558 } |
| 561 | 559 |
| 562 // Tests that after translating and closing the infobar, the infobar does not | 560 // Tests that after translating and closing the infobar, the infobar does not |
| 563 // return when navigating in page. | 561 // return when navigating in page. |
| 564 TEST_F(TranslateManagerTest, TranslateCloseInfoBarInPageNavigation) { | 562 TEST_F(TranslateManagerTest, TranslateCloseInfoBarInPageNavigation) { |
| 565 // Simulate navigating to a page and getting its language. | 563 // Simulate navigating to a page and getting its language. |
| 566 SimulateNavigation(GURL("http://www.google.fr"), 0, "Le Google", "fr"); | 564 SimulateNavigation(GURL("http://www.google.fr"), 0, L"Le Google", "fr"); |
| 567 | 565 |
| 568 // Simulate the user translating. | 566 // Simulate the user translating. |
| 569 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); | 567 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); |
| 570 ASSERT_TRUE(infobar != NULL); | 568 ASSERT_TRUE(infobar != NULL); |
| 571 infobar->Translate(); | 569 infobar->Translate(); |
| 572 SimulateURLFetch(true); // Simulate the translate script being retrieved. | 570 SimulateURLFetch(true); // Simulate the translate script being retrieved. |
| 573 rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(0, 0, "fr", "en", | 571 rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(0, 0, "fr", "en", |
| 574 TranslateErrors::NONE)); | 572 TranslateErrors::NONE)); |
| 575 | 573 |
| 576 // Close the infobar. | 574 // Close the infobar. |
| 577 EXPECT_TRUE(CloseTranslateInfoBar()); | 575 EXPECT_TRUE(CloseTranslateInfoBar()); |
| 578 | 576 |
| 579 // Navigate in page, no infobar should be shown. | 577 // Navigate in page, no infobar should be shown. |
| 580 SimulateNavigation(GURL("http://www.google.fr/#ref1"), 0, "Le Google", "fr"); | 578 SimulateNavigation(GURL("http://www.google.fr/#ref1"), 0, L"Le Google", "fr"); |
| 581 EXPECT_TRUE(GetTranslateInfoBar() == NULL); | 579 EXPECT_TRUE(GetTranslateInfoBar() == NULL); |
| 582 | 580 |
| 583 // Navigate out of page, a new infobar should show. | 581 // Navigate out of page, a new infobar should show. |
| 584 // Note that we navigate to a page in a different language so we don't trigger | 582 // Note that we navigate to a page in a different language so we don't trigger |
| 585 // the auto-translate feature (it would translate the page automatically and | 583 // the auto-translate feature (it would translate the page automatically and |
| 586 // the before translate inforbar would not be shown). | 584 // the before translate inforbar would not be shown). |
| 587 SimulateNavigation(GURL("http://www.google.de"), 0, "Das Google", "de"); | 585 SimulateNavigation(GURL("http://www.google.de"), 0, L"Das Google", "de"); |
| 588 EXPECT_TRUE(GetTranslateInfoBar() != NULL); | 586 EXPECT_TRUE(GetTranslateInfoBar() != NULL); |
| 589 } | 587 } |
| 590 | 588 |
| 591 // Tests that the after translate the infobar still shows when navigating | 589 // Tests that the after translate the infobar still shows when navigating |
| 592 // in-page. | 590 // in-page. |
| 593 TEST_F(TranslateManagerTest, TranslateInPageNavigation) { | 591 TEST_F(TranslateManagerTest, TranslateInPageNavigation) { |
| 594 // Simulate navigating to a page and getting its language. | 592 // Simulate navigating to a page and getting its language. |
| 595 SimulateNavigation(GURL("http://www.google.fr"), 0, "Le Google", "fr"); | 593 SimulateNavigation(GURL("http://www.google.fr"), 0, L"Le Google", "fr"); |
| 596 | 594 |
| 597 // Simulate the user translating. | 595 // Simulate the user translating. |
| 598 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); | 596 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); |
| 599 ASSERT_TRUE(infobar != NULL); | 597 ASSERT_TRUE(infobar != NULL); |
| 600 infobar->Translate(); | 598 infobar->Translate(); |
| 601 SimulateURLFetch(true); // Simulate the translate script being retrieved. | 599 SimulateURLFetch(true); // Simulate the translate script being retrieved. |
| 602 rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(0, 0, "fr", "en", | 600 rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(0, 0, "fr", "en", |
| 603 TranslateErrors::NONE)); | 601 TranslateErrors::NONE)); |
| 604 | 602 |
| 605 // Navigate in page, the same infobar should still be shown. | 603 // Navigate in page, the same infobar should still be shown. |
| 606 SimulateNavigation(GURL("http://www.google.fr/#ref1"), 0, "Le Google", "fr"); | 604 SimulateNavigation(GURL("http://www.google.fr/#ref1"), 0, L"Le Google", "fr"); |
| 607 EXPECT_FALSE(InfoBarRemoved()); | 605 EXPECT_FALSE(InfoBarRemoved()); |
| 608 EXPECT_EQ(infobar, GetTranslateInfoBar()); | 606 EXPECT_EQ(infobar, GetTranslateInfoBar()); |
| 609 | 607 |
| 610 // Navigate out of page, a new infobar should show. | 608 // Navigate out of page, a new infobar should show. |
| 611 // See note in TranslateCloseInfoBarInPageNavigation test on why it is | 609 // See note in TranslateCloseInfoBarInPageNavigation test on why it is |
| 612 // important to navigate to a page in a different language for this test. | 610 // important to navigate to a page in a different language for this test. |
| 613 SimulateNavigation(GURL("http://www.google.de"), 0, "Das Google", "de"); | 611 SimulateNavigation(GURL("http://www.google.de"), 0, L"Das Google", "de"); |
| 614 // The old infobar is gone. | 612 // The old infobar is gone. |
| 615 EXPECT_TRUE(CheckInfoBarRemovedAndReset(infobar)); | 613 EXPECT_TRUE(CheckInfoBarRemovedAndReset(infobar)); |
| 616 // And there is a new one. | 614 // And there is a new one. |
| 617 EXPECT_TRUE(GetTranslateInfoBar() != NULL); | 615 EXPECT_TRUE(GetTranslateInfoBar() != NULL); |
| 618 } | 616 } |
| 619 | 617 |
| 620 // Tests that no translate infobar is shown when navigating to a page in an | 618 // Tests that no translate infobar is shown when navigating to a page in an |
| 621 // unsupported language. | 619 // unsupported language. |
| 622 TEST_F(TranslateManagerTest, UnsupportedPageLanguage) { | 620 TEST_F(TranslateManagerTest, UnsupportedPageLanguage) { |
| 623 // Simulate navigating to a page and getting an unsupported language. | 621 // Simulate navigating to a page and getting an unsupported language. |
| 624 SimulateNavigation(GURL("http://www.google.com"), 0, "Google", "qbz"); | 622 SimulateNavigation(GURL("http://www.google.com"), 0, L"Google", "qbz"); |
| 625 | 623 |
| 626 // No info-bar should be shown. | 624 // No info-bar should be shown. |
| 627 EXPECT_TRUE(GetTranslateInfoBar() == NULL); | 625 EXPECT_TRUE(GetTranslateInfoBar() == NULL); |
| 628 } | 626 } |
| 629 | 627 |
| 630 // Tests that no translate infobar is shown when Chrome is in a language that | 628 // Tests that no translate infobar is shown when Chrome is in a language that |
| 631 // the translate server does not support. | 629 // the translate server does not support. |
| 632 TEST_F(TranslateManagerTest, UnsupportedUILanguage) { | 630 TEST_F(TranslateManagerTest, UnsupportedUILanguage) { |
| 633 TestingBrowserProcess* browser_process = | 631 TestingBrowserProcess* browser_process = |
| 634 static_cast<TestingBrowserProcess*>(g_browser_process); | 632 static_cast<TestingBrowserProcess*>(g_browser_process); |
| 635 std::string original_lang = browser_process->GetApplicationLocale(); | 633 std::string original_lang = browser_process->GetApplicationLocale(); |
| 636 browser_process->SetApplicationLocale("qbz"); | 634 browser_process->SetApplicationLocale("qbz"); |
| 637 | 635 |
| 638 // Simulate navigating to a page in a language supported by the translate | 636 // Simulate navigating to a page in a language supported by the translate |
| 639 // server. | 637 // server. |
| 640 SimulateNavigation(GURL("http://www.google.com"), 0, "Google", "en"); | 638 SimulateNavigation(GURL("http://www.google.com"), 0, L"Google", "en"); |
| 641 | 639 |
| 642 // No info-bar should be shown. | 640 // No info-bar should be shown. |
| 643 EXPECT_TRUE(GetTranslateInfoBar() == NULL); | 641 EXPECT_TRUE(GetTranslateInfoBar() == NULL); |
| 644 | 642 |
| 645 browser_process->SetApplicationLocale(original_lang); | 643 browser_process->SetApplicationLocale(original_lang); |
| 646 } | 644 } |
| 647 | 645 |
| 648 // Tests that the translate enabled preference is honored. | 646 // Tests that the translate enabled preference is honored. |
| 649 TEST_F(TranslateManagerTest, TranslateEnabledPref) { | 647 TEST_F(TranslateManagerTest, TranslateEnabledPref) { |
| 650 // Make sure the pref allows translate. | 648 // Make sure the pref allows translate. |
| 651 PrefService* prefs = contents()->profile()->GetPrefs(); | 649 PrefService* prefs = contents()->profile()->GetPrefs(); |
| 652 prefs->SetBoolean(prefs::kEnableTranslate, true); | 650 prefs->SetBoolean(prefs::kEnableTranslate, true); |
| 653 | 651 |
| 654 // Simulate navigating to a page and getting its language. | 652 // Simulate navigating to a page and getting its language. |
| 655 SimulateNavigation(GURL("http://www.google.fr"), 0, "Le Google", "fr"); | 653 SimulateNavigation(GURL("http://www.google.fr"), 0, L"Le Google", "fr"); |
| 656 | 654 |
| 657 // An infobar should be shown. | 655 // An infobar should be shown. |
| 658 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); | 656 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); |
| 659 EXPECT_TRUE(infobar != NULL); | 657 EXPECT_TRUE(infobar != NULL); |
| 660 | 658 |
| 661 // Disable translate. | 659 // Disable translate. |
| 662 prefs->SetBoolean(prefs::kEnableTranslate, false); | 660 prefs->SetBoolean(prefs::kEnableTranslate, false); |
| 663 | 661 |
| 664 // Navigate to a new page, that should close the previous infobar. | 662 // Navigate to a new page, that should close the previous infobar. |
| 665 GURL url("http://www.youtube.fr"); | 663 GURL url("http://www.youtube.fr"); |
| 666 NavigateAndCommit(url); | 664 NavigateAndCommit(url); |
| 667 infobar = GetTranslateInfoBar(); | 665 infobar = GetTranslateInfoBar(); |
| 668 EXPECT_TRUE(infobar == NULL); | 666 EXPECT_TRUE(infobar == NULL); |
| 669 | 667 |
| 670 // Simulate getting the page contents and language, that should not trigger | 668 // Simulate getting the page contents and language, that should not trigger |
| 671 // a translate infobar. | 669 // a translate infobar. |
| 672 SimulateOnPageContents(url, 1, "Le YouTube", "fr"); | 670 SimulateOnPageContents(url, 1, L"Le YouTube", "fr"); |
| 673 infobar = GetTranslateInfoBar(); | 671 infobar = GetTranslateInfoBar(); |
| 674 EXPECT_TRUE(infobar == NULL); | 672 EXPECT_TRUE(infobar == NULL); |
| 675 } | 673 } |
| 676 | 674 |
| 677 // Tests the "Never translate <language>" pref. | 675 // Tests the "Never translate <language>" pref. |
| 678 TEST_F(TranslateManagerTest, NeverTranslateLanguagePref) { | 676 TEST_F(TranslateManagerTest, NeverTranslateLanguagePref) { |
| 679 // Simulate navigating to a page and getting its language. | 677 // Simulate navigating to a page and getting its language. |
| 680 GURL url("http://www.google.fr"); | 678 GURL url("http://www.google.fr"); |
| 681 SimulateNavigation(url, 0, "Le Google", "fr"); | 679 SimulateNavigation(url, 0, L"Le Google", "fr"); |
| 682 | 680 |
| 683 // An infobar should be shown. | 681 // An infobar should be shown. |
| 684 EXPECT_TRUE(GetTranslateInfoBar() != NULL); | 682 EXPECT_TRUE(GetTranslateInfoBar() != NULL); |
| 685 | 683 |
| 686 // Select never translate this language. | 684 // Select never translate this language. |
| 687 PrefService* prefs = contents()->profile()->GetPrefs(); | 685 PrefService* prefs = contents()->profile()->GetPrefs(); |
| 688 prefs->AddPrefObserver(TranslatePrefs::kPrefTranslateLanguageBlacklist, | 686 prefs->AddPrefObserver(TranslatePrefs::kPrefTranslateLanguageBlacklist, |
| 689 &pref_observer_); | 687 &pref_observer_); |
| 690 TranslatePrefs translate_prefs(prefs); | 688 TranslatePrefs translate_prefs(prefs); |
| 691 EXPECT_FALSE(translate_prefs.IsLanguageBlacklisted("fr")); | 689 EXPECT_FALSE(translate_prefs.IsLanguageBlacklisted("fr")); |
| 692 EXPECT_TRUE(translate_prefs.CanTranslate(prefs, "fr", url)); | 690 EXPECT_TRUE(translate_prefs.CanTranslate(prefs, "fr", url)); |
| 693 SetPrefObserverExpectation(TranslatePrefs::kPrefTranslateLanguageBlacklist); | 691 SetPrefObserverExpectation(TranslatePrefs::kPrefTranslateLanguageBlacklist); |
| 694 translate_prefs.BlacklistLanguage("fr"); | 692 translate_prefs.BlacklistLanguage("fr"); |
| 695 EXPECT_TRUE(translate_prefs.IsLanguageBlacklisted("fr")); | 693 EXPECT_TRUE(translate_prefs.IsLanguageBlacklisted("fr")); |
| 696 EXPECT_FALSE(translate_prefs.CanTranslate(prefs, "fr", url)); | 694 EXPECT_FALSE(translate_prefs.CanTranslate(prefs, "fr", url)); |
| 697 | 695 |
| 698 // Close the infobar. | 696 // Close the infobar. |
| 699 EXPECT_TRUE(CloseTranslateInfoBar()); | 697 EXPECT_TRUE(CloseTranslateInfoBar()); |
| 700 | 698 |
| 701 // Navigate to a new page also in French. | 699 // Navigate to a new page also in French. |
| 702 SimulateNavigation(GURL("http://wwww.youtube.fr"), 1, "Le YouTube", "fr"); | 700 SimulateNavigation(GURL("http://wwww.youtube.fr"), 1, L"Le YouTube", "fr"); |
| 703 | 701 |
| 704 // There should not be a translate infobar. | 702 // There should not be a translate infobar. |
| 705 EXPECT_TRUE(GetTranslateInfoBar() == NULL); | 703 EXPECT_TRUE(GetTranslateInfoBar() == NULL); |
| 706 | 704 |
| 707 // Remove the language from the blacklist. | 705 // Remove the language from the blacklist. |
| 708 SetPrefObserverExpectation(TranslatePrefs::kPrefTranslateLanguageBlacklist); | 706 SetPrefObserverExpectation(TranslatePrefs::kPrefTranslateLanguageBlacklist); |
| 709 translate_prefs.RemoveLanguageFromBlacklist("fr"); | 707 translate_prefs.RemoveLanguageFromBlacklist("fr"); |
| 710 EXPECT_FALSE(translate_prefs.IsLanguageBlacklisted("fr")); | 708 EXPECT_FALSE(translate_prefs.IsLanguageBlacklisted("fr")); |
| 711 EXPECT_TRUE(translate_prefs.CanTranslate(prefs, "fr", url)); | 709 EXPECT_TRUE(translate_prefs.CanTranslate(prefs, "fr", url)); |
| 712 | 710 |
| 713 // Navigate to a page in French. | 711 // Navigate to a page in French. |
| 714 SimulateNavigation(url, 2, "Le Google", "fr"); | 712 SimulateNavigation(url, 2, L"Le Google", "fr"); |
| 715 | 713 |
| 716 // There should be a translate infobar. | 714 // There should be a translate infobar. |
| 717 EXPECT_TRUE(GetTranslateInfoBar() != NULL); | 715 EXPECT_TRUE(GetTranslateInfoBar() != NULL); |
| 718 prefs->RemovePrefObserver(TranslatePrefs::kPrefTranslateLanguageBlacklist, | 716 prefs->RemovePrefObserver(TranslatePrefs::kPrefTranslateLanguageBlacklist, |
| 719 &pref_observer_); | 717 &pref_observer_); |
| 720 } | 718 } |
| 721 | 719 |
| 722 // Tests the "Never translate this site" pref. | 720 // Tests the "Never translate this site" pref. |
| 723 TEST_F(TranslateManagerTest, NeverTranslateSitePref) { | 721 TEST_F(TranslateManagerTest, NeverTranslateSitePref) { |
| 724 // Simulate navigating to a page and getting its language. | 722 // Simulate navigating to a page and getting its language. |
| 725 GURL url("http://www.google.fr"); | 723 GURL url("http://www.google.fr"); |
| 726 std::string host(url.host()); | 724 std::string host(url.host()); |
| 727 SimulateNavigation(url, 0, "Le Google", "fr"); | 725 SimulateNavigation(url, 0, L"Le Google", "fr"); |
| 728 | 726 |
| 729 // An infobar should be shown. | 727 // An infobar should be shown. |
| 730 EXPECT_TRUE(GetTranslateInfoBar() != NULL); | 728 EXPECT_TRUE(GetTranslateInfoBar() != NULL); |
| 731 | 729 |
| 732 // Select never translate this site. | 730 // Select never translate this site. |
| 733 PrefService* prefs = contents()->profile()->GetPrefs(); | 731 PrefService* prefs = contents()->profile()->GetPrefs(); |
| 734 prefs->AddPrefObserver(TranslatePrefs::kPrefTranslateSiteBlacklist, | 732 prefs->AddPrefObserver(TranslatePrefs::kPrefTranslateSiteBlacklist, |
| 735 &pref_observer_); | 733 &pref_observer_); |
| 736 TranslatePrefs translate_prefs(prefs); | 734 TranslatePrefs translate_prefs(prefs); |
| 737 EXPECT_FALSE(translate_prefs.IsSiteBlacklisted(host)); | 735 EXPECT_FALSE(translate_prefs.IsSiteBlacklisted(host)); |
| 738 EXPECT_TRUE(translate_prefs.CanTranslate(prefs, "fr", url)); | 736 EXPECT_TRUE(translate_prefs.CanTranslate(prefs, "fr", url)); |
| 739 SetPrefObserverExpectation(TranslatePrefs::kPrefTranslateSiteBlacklist); | 737 SetPrefObserverExpectation(TranslatePrefs::kPrefTranslateSiteBlacklist); |
| 740 translate_prefs.BlacklistSite(host); | 738 translate_prefs.BlacklistSite(host); |
| 741 EXPECT_TRUE(translate_prefs.IsSiteBlacklisted(host)); | 739 EXPECT_TRUE(translate_prefs.IsSiteBlacklisted(host)); |
| 742 EXPECT_FALSE(translate_prefs.CanTranslate(prefs, "fr", url)); | 740 EXPECT_FALSE(translate_prefs.CanTranslate(prefs, "fr", url)); |
| 743 | 741 |
| 744 // Close the infobar. | 742 // Close the infobar. |
| 745 EXPECT_TRUE(CloseTranslateInfoBar()); | 743 EXPECT_TRUE(CloseTranslateInfoBar()); |
| 746 | 744 |
| 747 // Navigate to a new page also on the same site. | 745 // Navigate to a new page also on the same site. |
| 748 SimulateNavigation(GURL("http://www.google.fr/hello"), 1, "Bonjour", "fr"); | 746 SimulateNavigation(GURL("http://www.google.fr/hello"), 1, L"Bonjour", "fr"); |
| 749 | 747 |
| 750 // There should not be a translate infobar. | 748 // There should not be a translate infobar. |
| 751 EXPECT_TRUE(GetTranslateInfoBar() == NULL); | 749 EXPECT_TRUE(GetTranslateInfoBar() == NULL); |
| 752 | 750 |
| 753 // Remove the site from the blacklist. | 751 // Remove the site from the blacklist. |
| 754 SetPrefObserverExpectation(TranslatePrefs::kPrefTranslateSiteBlacklist); | 752 SetPrefObserverExpectation(TranslatePrefs::kPrefTranslateSiteBlacklist); |
| 755 translate_prefs.RemoveSiteFromBlacklist(host); | 753 translate_prefs.RemoveSiteFromBlacklist(host); |
| 756 EXPECT_FALSE(translate_prefs.IsSiteBlacklisted(host)); | 754 EXPECT_FALSE(translate_prefs.IsSiteBlacklisted(host)); |
| 757 EXPECT_TRUE(translate_prefs.CanTranslate(prefs, "fr", url)); | 755 EXPECT_TRUE(translate_prefs.CanTranslate(prefs, "fr", url)); |
| 758 | 756 |
| 759 // Navigate to a page in French. | 757 // Navigate to a page in French. |
| 760 SimulateNavigation(url, 0, "Le Google", "fr"); | 758 SimulateNavigation(url, 0, L"Le Google", "fr"); |
| 761 | 759 |
| 762 // There should be a translate infobar. | 760 // There should be a translate infobar. |
| 763 EXPECT_TRUE(GetTranslateInfoBar() != NULL); | 761 EXPECT_TRUE(GetTranslateInfoBar() != NULL); |
| 764 prefs->RemovePrefObserver(TranslatePrefs::kPrefTranslateSiteBlacklist, | 762 prefs->RemovePrefObserver(TranslatePrefs::kPrefTranslateSiteBlacklist, |
| 765 &pref_observer_); | 763 &pref_observer_); |
| 766 } | 764 } |
| 767 | 765 |
| 768 // Tests the "Always translate this language" pref. | 766 // Tests the "Always translate this language" pref. |
| 769 TEST_F(TranslateManagerTest, AlwaysTranslateLanguagePref) { | 767 TEST_F(TranslateManagerTest, AlwaysTranslateLanguagePref) { |
| 770 // Select always translate French to English. | 768 // Select always translate French to English. |
| 771 PrefService* prefs = contents()->profile()->GetPrefs(); | 769 PrefService* prefs = contents()->profile()->GetPrefs(); |
| 772 prefs->AddPrefObserver(TranslatePrefs::kPrefTranslateWhitelists, | 770 prefs->AddPrefObserver(TranslatePrefs::kPrefTranslateWhitelists, |
| 773 &pref_observer_); | 771 &pref_observer_); |
| 774 TranslatePrefs translate_prefs(prefs); | 772 TranslatePrefs translate_prefs(prefs); |
| 775 SetPrefObserverExpectation(TranslatePrefs::kPrefTranslateWhitelists); | 773 SetPrefObserverExpectation(TranslatePrefs::kPrefTranslateWhitelists); |
| 776 translate_prefs.WhitelistLanguagePair("fr", "en"); | 774 translate_prefs.WhitelistLanguagePair("fr", "en"); |
| 777 | 775 |
| 778 // Load a page in French. | 776 // Load a page in French. |
| 779 SimulateNavigation(GURL("http://www.google.fr"), 0, "Le Google", "fr"); | 777 SimulateNavigation(GURL("http://www.google.fr"), 0, L"Le Google", "fr"); |
| 780 | 778 |
| 781 // It should have triggered an automatic translation to English. | 779 // It should have triggered an automatic translation to English. |
| 782 SimulateURLFetch(true); // Simulate the translate script being retrieved. | 780 SimulateURLFetch(true); // Simulate the translate script being retrieved. |
| 783 int page_id = 0; | 781 int page_id = 0; |
| 784 std::string original_lang, target_lang; | 782 std::string original_lang, target_lang; |
| 785 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); | 783 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); |
| 786 EXPECT_EQ(0, page_id); | 784 EXPECT_EQ(0, page_id); |
| 787 EXPECT_EQ("fr", original_lang); | 785 EXPECT_EQ("fr", original_lang); |
| 788 EXPECT_EQ("en", target_lang); | 786 EXPECT_EQ("en", target_lang); |
| 789 process()->sink().ClearMessages(); | 787 process()->sink().ClearMessages(); |
| 790 // And we should have no infobar (since we don't send the page translated | 788 // And we should have no infobar (since we don't send the page translated |
| 791 // notification, the after translate infobar is not shown). | 789 // notification, the after translate infobar is not shown). |
| 792 EXPECT_TRUE(GetTranslateInfoBar() == NULL); | 790 EXPECT_TRUE(GetTranslateInfoBar() == NULL); |
| 793 | 791 |
| 794 // Try another language, it should not be autotranslated. | 792 // Try another language, it should not be autotranslated. |
| 795 SimulateNavigation(GURL("http://www.google.es"), 1, "El Google", "es"); | 793 SimulateNavigation(GURL("http://www.google.es"), 1, L"El Google", "es"); |
| 796 EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); | 794 EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); |
| 797 EXPECT_TRUE(GetTranslateInfoBar() != NULL); | 795 EXPECT_TRUE(GetTranslateInfoBar() != NULL); |
| 798 EXPECT_TRUE(CloseTranslateInfoBar()); | 796 EXPECT_TRUE(CloseTranslateInfoBar()); |
| 799 | 797 |
| 800 // Let's switch to incognito mode, it should not be autotranslated in that | 798 // Let's switch to incognito mode, it should not be autotranslated in that |
| 801 // case either. | 799 // case either. |
| 802 TestingProfile* test_profile = | 800 TestingProfile* test_profile = |
| 803 static_cast<TestingProfile*>(contents()->profile()); | 801 static_cast<TestingProfile*>(contents()->profile()); |
| 804 test_profile->set_off_the_record(true); | 802 test_profile->set_off_the_record(true); |
| 805 SimulateNavigation(GURL("http://www.youtube.fr"), 2, "Le YouTube", "fr"); | 803 SimulateNavigation(GURL("http://www.youtube.fr"), 2, L"Le YouTube", "fr"); |
| 806 EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); | 804 EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); |
| 807 EXPECT_TRUE(GetTranslateInfoBar() != NULL); | 805 EXPECT_TRUE(GetTranslateInfoBar() != NULL); |
| 808 EXPECT_TRUE(CloseTranslateInfoBar()); | 806 EXPECT_TRUE(CloseTranslateInfoBar()); |
| 809 test_profile->set_off_the_record(false); // Get back to non incognito. | 807 test_profile->set_off_the_record(false); // Get back to non incognito. |
| 810 | 808 |
| 811 // Now revert the always translate pref and make sure we go back to expected | 809 // Now revert the always translate pref and make sure we go back to expected |
| 812 // behavior, which is show an infobar. | 810 // behavior, which is show an infobar. |
| 813 SetPrefObserverExpectation(TranslatePrefs::kPrefTranslateWhitelists); | 811 SetPrefObserverExpectation(TranslatePrefs::kPrefTranslateWhitelists); |
| 814 translate_prefs.RemoveLanguagePairFromWhitelist("fr", "en"); | 812 translate_prefs.RemoveLanguagePairFromWhitelist("fr", "en"); |
| 815 SimulateNavigation(GURL("http://www.google.fr"), 3, "Le Google", "fr"); | 813 SimulateNavigation(GURL("http://www.google.fr"), 3, L"Le Google", "fr"); |
| 816 EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); | 814 EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); |
| 817 EXPECT_TRUE(GetTranslateInfoBar() != NULL); | 815 EXPECT_TRUE(GetTranslateInfoBar() != NULL); |
| 818 prefs->RemovePrefObserver(TranslatePrefs::kPrefTranslateWhitelists, | 816 prefs->RemovePrefObserver(TranslatePrefs::kPrefTranslateWhitelists, |
| 819 &pref_observer_); | 817 &pref_observer_); |
| 820 } | 818 } |
| 821 | 819 |
| 822 // Context menu. | 820 // Context menu. |
| 823 TEST_F(TranslateManagerTest, ContextMenu) { | 821 TEST_F(TranslateManagerTest, ContextMenu) { |
| 824 // Blacklist www.google.fr and French for translation. | 822 // Blacklist www.google.fr and French for translation. |
| 825 GURL url("http://www.google.fr"); | 823 GURL url("http://www.google.fr"); |
| 826 TranslatePrefs translate_prefs(contents()->profile()->GetPrefs()); | 824 TranslatePrefs translate_prefs(contents()->profile()->GetPrefs()); |
| 827 translate_prefs.BlacklistLanguage("fr"); | 825 translate_prefs.BlacklistLanguage("fr"); |
| 828 translate_prefs.BlacklistSite(url.host()); | 826 translate_prefs.BlacklistSite(url.host()); |
| 829 EXPECT_TRUE(translate_prefs.IsLanguageBlacklisted("fr")); | 827 EXPECT_TRUE(translate_prefs.IsLanguageBlacklisted("fr")); |
| 830 EXPECT_TRUE(translate_prefs.IsSiteBlacklisted(url.host())); | 828 EXPECT_TRUE(translate_prefs.IsSiteBlacklisted(url.host())); |
| 831 | 829 |
| 832 // Simulate navigating to a page in French. The translate menu should show. | 830 // Simulate navigating to a page in French. The translate menu should show. |
| 833 SimulateNavigation(url, 0, "Le Google", "fr"); | 831 SimulateNavigation(url, 0, L"Le Google", "fr"); |
| 834 scoped_ptr<TestRenderViewContextMenu> menu( | 832 scoped_ptr<TestRenderViewContextMenu> menu( |
| 835 TestRenderViewContextMenu::CreateContextMenu(contents())); | 833 TestRenderViewContextMenu::CreateContextMenu(contents())); |
| 836 menu->Init(); | 834 menu->Init(); |
| 837 EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE)); | 835 EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE)); |
| 838 EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE)); | 836 EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE)); |
| 839 | 837 |
| 840 // Use the menu to translate the page. | 838 // Use the menu to translate the page. |
| 841 menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE); | 839 menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE); |
| 842 | 840 |
| 843 // That should have triggered a translation. | 841 // That should have triggered a translation. |
| (...skipping 16 matching lines...) Expand all Loading... |
| 860 | 858 |
| 861 // The translate menu should now be disabled. | 859 // The translate menu should now be disabled. |
| 862 menu.reset(TestRenderViewContextMenu::CreateContextMenu(contents())); | 860 menu.reset(TestRenderViewContextMenu::CreateContextMenu(contents())); |
| 863 menu->Init(); | 861 menu->Init(); |
| 864 EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE)); | 862 EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE)); |
| 865 EXPECT_FALSE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE)); | 863 EXPECT_FALSE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE)); |
| 866 | 864 |
| 867 // Test that selecting translate in the context menu WHILE the page is being | 865 // Test that selecting translate in the context menu WHILE the page is being |
| 868 // translated does nothing (this could happen if autotranslate kicks-in and | 866 // translated does nothing (this could happen if autotranslate kicks-in and |
| 869 // the user selects the menu while the translation is being performed). | 867 // the user selects the menu while the translation is being performed). |
| 870 SimulateNavigation(GURL("http://www.google.es"), 1, "El Google", "es"); | 868 SimulateNavigation(GURL("http://www.google.es"), 1, L"El Google", "es"); |
| 871 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); | 869 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); |
| 872 ASSERT_TRUE(infobar != NULL); | 870 ASSERT_TRUE(infobar != NULL); |
| 873 infobar->Translate(); | 871 infobar->Translate(); |
| 874 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); | 872 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); |
| 875 EXPECT_EQ(1, page_id); | 873 EXPECT_EQ(1, page_id); |
| 876 process()->sink().ClearMessages(); | 874 process()->sink().ClearMessages(); |
| 877 menu.reset(TestRenderViewContextMenu::CreateContextMenu(contents())); | 875 menu.reset(TestRenderViewContextMenu::CreateContextMenu(contents())); |
| 878 menu->Init(); | 876 menu->Init(); |
| 879 EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE)); | 877 EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE)); |
| 880 menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE); | 878 menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE); |
| 881 // No message expected since the translation should have been ignored. | 879 // No message expected since the translation should have been ignored. |
| 882 EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); | 880 EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); |
| 883 | 881 |
| 884 // Now test that selecting translate in the context menu AFTER the page has | 882 // Now test that selecting translate in the context menu AFTER the page has |
| 885 // been translated does nothing. | 883 // been translated does nothing. |
| 886 SimulateNavigation(GURL("http://www.google.de"), 2, "Das Google", "de"); | 884 SimulateNavigation(GURL("http://www.google.de"), 2, L"Das Google", "de"); |
| 887 infobar = GetTranslateInfoBar(); | 885 infobar = GetTranslateInfoBar(); |
| 888 ASSERT_TRUE(infobar != NULL); | 886 ASSERT_TRUE(infobar != NULL); |
| 889 infobar->Translate(); | 887 infobar->Translate(); |
| 890 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); | 888 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); |
| 891 EXPECT_EQ(2, page_id); | 889 EXPECT_EQ(2, page_id); |
| 892 process()->sink().ClearMessages(); | 890 process()->sink().ClearMessages(); |
| 893 menu.reset(TestRenderViewContextMenu::CreateContextMenu(contents())); | 891 menu.reset(TestRenderViewContextMenu::CreateContextMenu(contents())); |
| 894 menu->Init(); | 892 menu->Init(); |
| 895 EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE)); | 893 EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE)); |
| 896 rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(0, 0, "de", "en", | 894 rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(0, 0, "de", "en", |
| 897 TranslateErrors::NONE)); | 895 TranslateErrors::NONE)); |
| 898 menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE); | 896 menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE); |
| 899 // No message expected since the translation should have been ignored. | 897 // No message expected since the translation should have been ignored. |
| 900 EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); | 898 EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); |
| 901 | 899 |
| 902 // Test that the translate context menu is disabled when the page is in the | 900 // Test that the translate context menu is disabled when the page is in the |
| 903 // same language as the UI. | 901 // same language as the UI. |
| 904 SimulateNavigation(url, 0, "Google", "en"); | 902 SimulateNavigation(url, 0, L"Google", "en"); |
| 905 menu.reset(TestRenderViewContextMenu::CreateContextMenu(contents())); | 903 menu.reset(TestRenderViewContextMenu::CreateContextMenu(contents())); |
| 906 menu->Init(); | 904 menu->Init(); |
| 907 EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE)); | 905 EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE)); |
| 908 EXPECT_FALSE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE)); | 906 EXPECT_FALSE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE)); |
| 909 } | 907 } |
| OLD | NEW |