| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <algorithm> | 5 #include <algorithm> |
| 6 #include <set> | 6 #include <set> |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/command_line.h" |
| 9 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/prefs/pref_change_registrar.h" | 11 #include "base/prefs/pref_change_registrar.h" |
| 11 #include "base/prefs/pref_service.h" | 12 #include "base/prefs/pref_service.h" |
| 12 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
| 13 #include "chrome/app/chrome_command_ids.h" | 14 #include "chrome/app/chrome_command_ids.h" |
| 14 #include "chrome/browser/chrome_notification_types.h" | 15 #include "chrome/browser/chrome_notification_types.h" |
| 15 #include "chrome/browser/extensions/test_extension_system.h" | 16 #include "chrome/browser/extensions/test_extension_system.h" |
| 16 #include "chrome/browser/infobars/infobar.h" | 17 #include "chrome/browser/infobars/infobar.h" |
| 17 #include "chrome/browser/infobars/infobar_service.h" | 18 #include "chrome/browser/infobars/infobar_service.h" |
| 18 #include "chrome/browser/prefs/session_startup_pref.h" | 19 #include "chrome/browser/prefs/session_startup_pref.h" |
| 19 #include "chrome/browser/tab_contents/render_view_context_menu.h" | 20 #include "chrome/browser/tab_contents/render_view_context_menu.h" |
| 21 #include "chrome/browser/translate/translate_bubble_showable.h" |
| 20 #include "chrome/browser/translate/translate_infobar_delegate.h" | 22 #include "chrome/browser/translate/translate_infobar_delegate.h" |
| 21 #include "chrome/browser/translate/translate_language_list.h" | 23 #include "chrome/browser/translate/translate_language_list.h" |
| 22 #include "chrome/browser/translate/translate_manager.h" | 24 #include "chrome/browser/translate/translate_manager.h" |
| 23 #include "chrome/browser/translate/translate_prefs.h" | 25 #include "chrome/browser/translate/translate_prefs.h" |
| 24 #include "chrome/browser/translate/translate_script.h" | 26 #include "chrome/browser/translate/translate_script.h" |
| 25 #include "chrome/browser/translate/translate_tab_helper.h" | 27 #include "chrome/browser/translate/translate_tab_helper.h" |
| 26 #include "chrome/browser/ui/browser.h" | 28 #include "chrome/browser/ui/browser.h" |
| 27 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 29 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 30 #include "chrome/browser/ui/translate/translate_bubble_model.h" |
| 31 #include "chrome/browser/ui/translate/translate_bubble_model_impl.h" |
| 32 #include "chrome/common/chrome_switches.h" |
| 28 #include "chrome/common/pref_names.h" | 33 #include "chrome/common/pref_names.h" |
| 29 #include "chrome/common/render_messages.h" | 34 #include "chrome/common/render_messages.h" |
| 30 #include "chrome/common/translate/language_detection_details.h" | 35 #include "chrome/common/translate/language_detection_details.h" |
| 31 #include "chrome/common/url_constants.h" | 36 #include "chrome/common/url_constants.h" |
| 32 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 37 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 33 #include "chrome/test/base/in_process_browser_test.h" | 38 #include "chrome/test/base/in_process_browser_test.h" |
| 34 #include "chrome/test/base/testing_browser_process.h" | 39 #include "chrome/test/base/testing_browser_process.h" |
| 35 #include "chrome/test/base/testing_profile.h" | 40 #include "chrome/test/base/testing_profile.h" |
| 36 #include "chrome/test/base/ui_test_utils.h" | 41 #include "chrome/test/base/ui_test_utils.h" |
| 37 #include "content/public/browser/navigation_details.h" | 42 #include "content/public/browser/navigation_details.h" |
| (...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 317 content::NotificationRegistrar notification_registrar_; | 322 content::NotificationRegistrar notification_registrar_; |
| 318 net::TestURLFetcherFactory url_fetcher_factory_; | 323 net::TestURLFetcherFactory url_fetcher_factory_; |
| 319 | 324 |
| 320 // The infobars that have been removed. | 325 // The infobars that have been removed. |
| 321 // WARNING: the pointers point to deleted objects, use only for comparison. | 326 // WARNING: the pointers point to deleted objects, use only for comparison. |
| 322 std::set<InfoBarDelegate*> removed_infobars_; | 327 std::set<InfoBarDelegate*> removed_infobars_; |
| 323 | 328 |
| 324 DISALLOW_COPY_AND_ASSIGN(TranslateManagerBrowserTest); | 329 DISALLOW_COPY_AND_ASSIGN(TranslateManagerBrowserTest); |
| 325 }; | 330 }; |
| 326 | 331 |
| 332 class MockTranslateBubbleShowable : public TranslateBubbleShowable { |
| 333 public: |
| 334 MockTranslateBubbleShowable() { |
| 335 } |
| 336 |
| 337 virtual void Show(content::WebContents* web_contents, |
| 338 TranslateBubbleModel::ViewState view_state) OVERRIDE { |
| 339 if (model_) { |
| 340 model_->SetViewState(view_state); |
| 341 return; |
| 342 } |
| 343 |
| 344 TranslateTabHelper* translate_tab_helper = |
| 345 TranslateTabHelper::FromWebContents(web_contents); |
| 346 std::string source_language = |
| 347 translate_tab_helper->language_state().original_language(); |
| 348 std::string target_language = TranslateManager::GetLanguageCode( |
| 349 g_browser_process->GetApplicationLocale()); |
| 350 scoped_ptr<TranslateUIDelegate> ui_delegate( |
| 351 new TranslateUIDelegate(web_contents, |
| 352 source_language, |
| 353 target_language)); |
| 354 model_.reset( |
| 355 new TranslateBubbleModelImpl(view_state, ui_delegate.Pass())); |
| 356 } |
| 357 |
| 358 TranslateBubbleModel* model() { return model_.get(); } |
| 359 |
| 360 private: |
| 361 scoped_ptr<TranslateBubbleModel> model_; |
| 362 |
| 363 DISALLOW_COPY_AND_ASSIGN(MockTranslateBubbleShowable); |
| 364 }; |
| 365 |
| 327 namespace { | 366 namespace { |
| 328 | 367 |
| 329 class TestRenderViewContextMenu : public RenderViewContextMenu { | 368 class TestRenderViewContextMenu : public RenderViewContextMenu { |
| 330 public: | 369 public: |
| 331 static TestRenderViewContextMenu* CreateContextMenu( | 370 static TestRenderViewContextMenu* CreateContextMenu( |
| 332 content::WebContents* web_contents) { | 371 content::WebContents* web_contents) { |
| 333 content::ContextMenuParams params; | 372 content::ContextMenuParams params; |
| 334 params.media_type = WebKit::WebContextMenuData::MediaTypeNone; | 373 params.media_type = WebKit::WebContextMenuData::MediaTypeNone; |
| 335 params.x = 0; | 374 params.x = 0; |
| 336 params.y = 0; | 375 params.y = 0; |
| (...skipping 1066 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1403 EXPECT_EQ("en", target_lang); | 1442 EXPECT_EQ("en", target_lang); |
| 1404 } | 1443 } |
| 1405 | 1444 |
| 1406 TEST_F(TranslateManagerBrowserTest, DownloadsAndHistoryNotTranslated) { | 1445 TEST_F(TranslateManagerBrowserTest, DownloadsAndHistoryNotTranslated) { |
| 1407 ASSERT_FALSE(TranslateManager::IsTranslatableURL( | 1446 ASSERT_FALSE(TranslateManager::IsTranslatableURL( |
| 1408 GURL(chrome::kChromeUIDownloadsURL))); | 1447 GURL(chrome::kChromeUIDownloadsURL))); |
| 1409 ASSERT_FALSE(TranslateManager::IsTranslatableURL( | 1448 ASSERT_FALSE(TranslateManager::IsTranslatableURL( |
| 1410 GURL(chrome::kChromeUIHistoryURL))); | 1449 GURL(chrome::kChromeUIHistoryURL))); |
| 1411 } | 1450 } |
| 1412 | 1451 |
| 1452 TEST_F(TranslateManagerBrowserTest, BubbleNormalTranslate) { |
| 1453 // Prepare for the bubble |
| 1454 CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 1455 command_line->AppendSwitch(switches::kEnableTranslateNewUX); |
| 1456 MockTranslateBubbleShowable* showable = new MockTranslateBubbleShowable; |
| 1457 scoped_ptr<TranslateBubbleShowable> showable_ptr(showable); |
| 1458 TranslateManager::GetInstance()->SetTranslateBubbleShowable( |
| 1459 showable_ptr.Pass()); |
| 1460 |
| 1461 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); |
| 1462 |
| 1463 // Check the bubble exists instead of the infobar. |
| 1464 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); |
| 1465 ASSERT_TRUE(infobar == NULL); |
| 1466 TranslateBubbleModel* bubble = showable->model(); |
| 1467 ASSERT_TRUE(bubble != NULL); |
| 1468 EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_BEFORE_TRANSLATE, |
| 1469 bubble->GetViewState()); |
| 1470 |
| 1471 // Simulate clicking translate. |
| 1472 process()->sink().ClearMessages(); |
| 1473 bubble->Translate(); |
| 1474 |
| 1475 // Check the bubble shows "Translating...". |
| 1476 bubble = showable->model(); |
| 1477 ASSERT_TRUE(bubble != NULL); |
| 1478 EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_TRANSLATING, |
| 1479 bubble->GetViewState()); |
| 1480 |
| 1481 // Simulate the translate script being retrieved (it only needs to be done |
| 1482 // once in the test as it is cached). |
| 1483 SimulateTranslateScriptURLFetch(true); |
| 1484 |
| 1485 // Simulate the render notifying the translation has been done. |
| 1486 SimulateOnPageTranslated("fr", "en"); |
| 1487 |
| 1488 // Check the bubble shows "Translated." |
| 1489 bubble = showable->model(); |
| 1490 ASSERT_TRUE(bubble != NULL); |
| 1491 EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_AFTER_TRANSLATE, |
| 1492 bubble->GetViewState()); |
| 1493 } |
| 1494 |
| 1495 TEST_F(TranslateManagerBrowserTest, BubbleTranslateScriptNotAvailable) { |
| 1496 // Prepare for the bubble |
| 1497 CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 1498 command_line->AppendSwitch(switches::kEnableTranslateNewUX); |
| 1499 MockTranslateBubbleShowable* showable = new MockTranslateBubbleShowable; |
| 1500 scoped_ptr<TranslateBubbleShowable> showable_ptr(showable); |
| 1501 TranslateManager::GetInstance()->SetTranslateBubbleShowable( |
| 1502 showable_ptr.Pass()); |
| 1503 |
| 1504 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); |
| 1505 |
| 1506 // Check the bubble exists instead of the infobar. |
| 1507 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); |
| 1508 ASSERT_TRUE(infobar == NULL); |
| 1509 TranslateBubbleModel* bubble = showable->model(); |
| 1510 ASSERT_TRUE(bubble != NULL); |
| 1511 EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_BEFORE_TRANSLATE, |
| 1512 bubble->GetViewState()); |
| 1513 |
| 1514 // Simulate clicking translate. |
| 1515 process()->sink().ClearMessages(); |
| 1516 bubble->Translate(); |
| 1517 SimulateTranslateScriptURLFetch(false); |
| 1518 |
| 1519 // We should not have sent any message to translate to the renderer. |
| 1520 EXPECT_FALSE(GetTranslateMessage(NULL, NULL, NULL)); |
| 1521 |
| 1522 // And we should have an error infobar showing. |
| 1523 bubble = showable->model(); |
| 1524 ASSERT_TRUE(bubble != NULL); |
| 1525 EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_ERROR, |
| 1526 bubble->GetViewState()); |
| 1527 } |
| 1528 |
| 1529 TEST_F(TranslateManagerBrowserTest, BubbleUnknownLanguage) { |
| 1530 // Prepare for the bubble |
| 1531 CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 1532 command_line->AppendSwitch(switches::kEnableTranslateNewUX); |
| 1533 MockTranslateBubbleShowable* showable = new MockTranslateBubbleShowable; |
| 1534 scoped_ptr<TranslateBubbleShowable> showable_ptr(showable); |
| 1535 TranslateManager::GetInstance()->SetTranslateBubbleShowable( |
| 1536 showable_ptr.Pass()); |
| 1537 |
| 1538 // Simulate navigating to a page ("und" is the string returned by the CLD for |
| 1539 // languages it does not recognize). |
| 1540 SimulateNavigation(GURL("http://www.google.mys"), "und", true); |
| 1541 |
| 1542 // We should not have a bubble as we don't know the language. |
| 1543 ASSERT_TRUE(showable->model() == NULL); |
| 1544 |
| 1545 // Translate the page anyway throught the context menu. |
| 1546 scoped_ptr<TestRenderViewContextMenu> menu( |
| 1547 TestRenderViewContextMenu::CreateContextMenu(web_contents())); |
| 1548 menu->Init(); |
| 1549 menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE, 0); |
| 1550 |
| 1551 // Check the bubble exists instead of the infobar. |
| 1552 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); |
| 1553 ASSERT_TRUE(infobar == NULL); |
| 1554 TranslateBubbleModel* bubble = showable->model(); |
| 1555 ASSERT_TRUE(bubble != NULL); |
| 1556 EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_TRANSLATING, |
| 1557 bubble->GetViewState()); |
| 1558 } |
| 1559 |
| 1413 // Test is flaky on Win http://crbug.com/166334 | 1560 // Test is flaky on Win http://crbug.com/166334 |
| 1414 #if defined(OS_WIN) | 1561 #if defined(OS_WIN) |
| 1415 #define MAYBE_PRE_TranslateSessionRestore DISABLED_PRE_TranslateSessionRestore | 1562 #define MAYBE_PRE_TranslateSessionRestore DISABLED_PRE_TranslateSessionRestore |
| 1416 #else | 1563 #else |
| 1417 #define MAYBE_PRE_TranslateSessionRestore PRE_TranslateSessionRestore | 1564 #define MAYBE_PRE_TranslateSessionRestore PRE_TranslateSessionRestore |
| 1418 #endif | 1565 #endif |
| 1419 // Test that session restore restores the translate infobar and other translate | 1566 // Test that session restore restores the translate infobar and other translate |
| 1420 // settings. | 1567 // settings. |
| 1421 IN_PROC_BROWSER_TEST_F(InProcessBrowserTest, | 1568 IN_PROC_BROWSER_TEST_F(InProcessBrowserTest, |
| 1422 MAYBE_PRE_TranslateSessionRestore) { | 1569 MAYBE_PRE_TranslateSessionRestore) { |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1454 content::WebContents* current_web_contents = | 1601 content::WebContents* current_web_contents = |
| 1455 browser()->tab_strip_model()->GetActiveWebContents(); | 1602 browser()->tab_strip_model()->GetActiveWebContents(); |
| 1456 content::Source<content::WebContents> source(current_web_contents); | 1603 content::Source<content::WebContents> source(current_web_contents); |
| 1457 | 1604 |
| 1458 ui_test_utils::WindowedNotificationObserverWithDetails< | 1605 ui_test_utils::WindowedNotificationObserverWithDetails< |
| 1459 LanguageDetectionDetails> | 1606 LanguageDetectionDetails> |
| 1460 fr_language_detected_signal(chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED, | 1607 fr_language_detected_signal(chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED, |
| 1461 source); | 1608 source); |
| 1462 fr_language_detected_signal.Wait(); | 1609 fr_language_detected_signal.Wait(); |
| 1463 } | 1610 } |
| OLD | NEW |