Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(858)

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

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

Powered by Google App Engine
This is Rietveld 408576698