| 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" |
| 20 #include "chrome/browser/translate/translate_infobar_delegate.h" | 21 #include "chrome/browser/translate/translate_infobar_delegate.h" |
| 21 #include "chrome/browser/translate/translate_language_list.h" | 22 #include "chrome/browser/translate/translate_language_list.h" |
| 22 #include "chrome/browser/translate/translate_manager.h" | 23 #include "chrome/browser/translate/translate_manager.h" |
| 23 #include "chrome/browser/translate/translate_prefs.h" | 24 #include "chrome/browser/translate/translate_prefs.h" |
| 24 #include "chrome/browser/translate/translate_script.h" | 25 #include "chrome/browser/translate/translate_script.h" |
| 25 #include "chrome/browser/translate/translate_tab_helper.h" | 26 #include "chrome/browser/translate/translate_tab_helper.h" |
| 26 #include "chrome/browser/ui/browser.h" | 27 #include "chrome/browser/ui/browser.h" |
| 27 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 28 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 29 #include "chrome/browser/ui/translate/translate_bubble_factory.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 MockTranslateBubbleFactory : public TranslateBubbleFactory { |
| 333 public: |
| 334 MockTranslateBubbleFactory() { |
| 335 } |
| 336 |
| 337 virtual void ShowImplementation( |
| 338 BrowserWindow* window, |
| 339 content::WebContents* web_contents, |
| 340 TranslateBubbleModel::ViewState view_state) OVERRIDE { |
| 341 if (model_) { |
| 342 model_->SetViewState(view_state); |
| 343 return; |
| 344 } |
| 345 |
| 346 TranslateTabHelper* translate_tab_helper = |
| 347 TranslateTabHelper::FromWebContents(web_contents); |
| 348 std::string source_language = |
| 349 translate_tab_helper->language_state().original_language(); |
| 350 std::string target_language = TranslateManager::GetLanguageCode( |
| 351 g_browser_process->GetApplicationLocale()); |
| 352 scoped_ptr<TranslateUIDelegate> ui_delegate( |
| 353 new TranslateUIDelegate(web_contents, |
| 354 source_language, |
| 355 target_language)); |
| 356 model_.reset( |
| 357 new TranslateBubbleModelImpl(view_state, ui_delegate.Pass())); |
| 358 } |
| 359 |
| 360 TranslateBubbleModel* model() { return model_.get(); } |
| 361 |
| 362 private: |
| 363 scoped_ptr<TranslateBubbleModel> model_; |
| 364 |
| 365 DISALLOW_COPY_AND_ASSIGN(MockTranslateBubbleFactory); |
| 366 }; |
| 367 |
| 327 namespace { | 368 namespace { |
| 328 | 369 |
| 329 class TestRenderViewContextMenu : public RenderViewContextMenu { | 370 class TestRenderViewContextMenu : public RenderViewContextMenu { |
| 330 public: | 371 public: |
| 331 static TestRenderViewContextMenu* CreateContextMenu( | 372 static TestRenderViewContextMenu* CreateContextMenu( |
| 332 content::WebContents* web_contents) { | 373 content::WebContents* web_contents) { |
| 333 content::ContextMenuParams params; | 374 content::ContextMenuParams params; |
| 334 params.media_type = WebKit::WebContextMenuData::MediaTypeNone; | 375 params.media_type = WebKit::WebContextMenuData::MediaTypeNone; |
| 335 params.x = 0; | 376 params.x = 0; |
| 336 params.y = 0; | 377 params.y = 0; |
| (...skipping 1066 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1403 EXPECT_EQ("en", target_lang); | 1444 EXPECT_EQ("en", target_lang); |
| 1404 } | 1445 } |
| 1405 | 1446 |
| 1406 TEST_F(TranslateManagerBrowserTest, DownloadsAndHistoryNotTranslated) { | 1447 TEST_F(TranslateManagerBrowserTest, DownloadsAndHistoryNotTranslated) { |
| 1407 ASSERT_FALSE(TranslateManager::IsTranslatableURL( | 1448 ASSERT_FALSE(TranslateManager::IsTranslatableURL( |
| 1408 GURL(chrome::kChromeUIDownloadsURL))); | 1449 GURL(chrome::kChromeUIDownloadsURL))); |
| 1409 ASSERT_FALSE(TranslateManager::IsTranslatableURL( | 1450 ASSERT_FALSE(TranslateManager::IsTranslatableURL( |
| 1410 GURL(chrome::kChromeUIHistoryURL))); | 1451 GURL(chrome::kChromeUIHistoryURL))); |
| 1411 } | 1452 } |
| 1412 | 1453 |
| 1454 #if !defined(OS_ANDROID) && !defined(OS_IOS) |
| 1455 |
| 1456 TEST_F(TranslateManagerBrowserTest, BubbleNormalTranslate) { |
| 1457 // Prepare for the bubble |
| 1458 CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 1459 command_line->AppendSwitch(switches::kEnableTranslateNewUX); |
| 1460 MockTranslateBubbleFactory* factory = new MockTranslateBubbleFactory; |
| 1461 scoped_ptr<TranslateBubbleFactory> factory_ptr(factory); |
| 1462 TranslateBubbleFactory::SetFactory(factory); |
| 1463 |
| 1464 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); |
| 1465 |
| 1466 // Check the bubble exists instead of the infobar. |
| 1467 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); |
| 1468 ASSERT_TRUE(infobar == NULL); |
| 1469 TranslateBubbleModel* bubble = factory->model(); |
| 1470 ASSERT_TRUE(bubble != NULL); |
| 1471 EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_BEFORE_TRANSLATE, |
| 1472 bubble->GetViewState()); |
| 1473 |
| 1474 // Simulate clicking translate. |
| 1475 process()->sink().ClearMessages(); |
| 1476 bubble->Translate(); |
| 1477 |
| 1478 // Check the bubble shows "Translating...". |
| 1479 bubble = factory->model(); |
| 1480 ASSERT_TRUE(bubble != NULL); |
| 1481 EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_TRANSLATING, |
| 1482 bubble->GetViewState()); |
| 1483 |
| 1484 // Simulate the translate script being retrieved (it only needs to be done |
| 1485 // once in the test as it is cached). |
| 1486 SimulateTranslateScriptURLFetch(true); |
| 1487 |
| 1488 // Simulate the render notifying the translation has been done. |
| 1489 SimulateOnPageTranslated("fr", "en"); |
| 1490 |
| 1491 // Check the bubble shows "Translated." |
| 1492 bubble = factory->model(); |
| 1493 ASSERT_TRUE(bubble != NULL); |
| 1494 EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_AFTER_TRANSLATE, |
| 1495 bubble->GetViewState()); |
| 1496 } |
| 1497 |
| 1498 TEST_F(TranslateManagerBrowserTest, BubbleTranslateScriptNotAvailable) { |
| 1499 // Prepare for the bubble |
| 1500 CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 1501 command_line->AppendSwitch(switches::kEnableTranslateNewUX); |
| 1502 MockTranslateBubbleFactory* factory = new MockTranslateBubbleFactory; |
| 1503 scoped_ptr<TranslateBubbleFactory> factory_ptr(factory); |
| 1504 TranslateBubbleFactory::SetFactory(factory); |
| 1505 |
| 1506 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); |
| 1507 |
| 1508 // Check the bubble exists instead of the infobar. |
| 1509 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); |
| 1510 ASSERT_TRUE(infobar == NULL); |
| 1511 TranslateBubbleModel* bubble = factory->model(); |
| 1512 ASSERT_TRUE(bubble != NULL); |
| 1513 EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_BEFORE_TRANSLATE, |
| 1514 bubble->GetViewState()); |
| 1515 |
| 1516 // Simulate clicking translate. |
| 1517 process()->sink().ClearMessages(); |
| 1518 bubble->Translate(); |
| 1519 SimulateTranslateScriptURLFetch(false); |
| 1520 |
| 1521 // We should not have sent any message to translate to the renderer. |
| 1522 EXPECT_FALSE(GetTranslateMessage(NULL, NULL, NULL)); |
| 1523 |
| 1524 // And we should have an error infobar showing. |
| 1525 bubble = factory->model(); |
| 1526 ASSERT_TRUE(bubble != NULL); |
| 1527 EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_ERROR, |
| 1528 bubble->GetViewState()); |
| 1529 } |
| 1530 |
| 1531 TEST_F(TranslateManagerBrowserTest, BubbleUnknownLanguage) { |
| 1532 // Prepare for the bubble |
| 1533 CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 1534 command_line->AppendSwitch(switches::kEnableTranslateNewUX); |
| 1535 MockTranslateBubbleFactory* factory = new MockTranslateBubbleFactory; |
| 1536 scoped_ptr<TranslateBubbleFactory> factory_ptr(factory); |
| 1537 TranslateBubbleFactory::SetFactory(factory); |
| 1538 |
| 1539 // Simulate navigating to a page ("und" is the string returned by the CLD for |
| 1540 // languages it does not recognize). |
| 1541 SimulateNavigation(GURL("http://www.google.mys"), "und", true); |
| 1542 |
| 1543 // We should not have a bubble as we don't know the language. |
| 1544 ASSERT_TRUE(factory->model() == NULL); |
| 1545 |
| 1546 // Translate the page anyway throught the context menu. |
| 1547 scoped_ptr<TestRenderViewContextMenu> menu( |
| 1548 TestRenderViewContextMenu::CreateContextMenu(web_contents())); |
| 1549 menu->Init(); |
| 1550 menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE, 0); |
| 1551 |
| 1552 // Check the bubble exists instead of the infobar. |
| 1553 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); |
| 1554 ASSERT_TRUE(infobar == NULL); |
| 1555 TranslateBubbleModel* bubble = factory->model(); |
| 1556 ASSERT_TRUE(bubble != NULL); |
| 1557 EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_TRANSLATING, |
| 1558 bubble->GetViewState()); |
| 1559 } |
| 1560 |
| 1561 #endif // !defined(OS_ANDROID) && !defined(OS_IOS) |
| 1562 |
| 1413 // Test is flaky on Win http://crbug.com/166334 | 1563 // Test is flaky on Win http://crbug.com/166334 |
| 1414 #if defined(OS_WIN) | 1564 #if defined(OS_WIN) |
| 1415 #define MAYBE_PRE_TranslateSessionRestore DISABLED_PRE_TranslateSessionRestore | 1565 #define MAYBE_PRE_TranslateSessionRestore DISABLED_PRE_TranslateSessionRestore |
| 1416 #else | 1566 #else |
| 1417 #define MAYBE_PRE_TranslateSessionRestore PRE_TranslateSessionRestore | 1567 #define MAYBE_PRE_TranslateSessionRestore PRE_TranslateSessionRestore |
| 1418 #endif | 1568 #endif |
| 1419 // Test that session restore restores the translate infobar and other translate | 1569 // Test that session restore restores the translate infobar and other translate |
| 1420 // settings. | 1570 // settings. |
| 1421 IN_PROC_BROWSER_TEST_F(InProcessBrowserTest, | 1571 IN_PROC_BROWSER_TEST_F(InProcessBrowserTest, |
| 1422 MAYBE_PRE_TranslateSessionRestore) { | 1572 MAYBE_PRE_TranslateSessionRestore) { |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1454 content::WebContents* current_web_contents = | 1604 content::WebContents* current_web_contents = |
| 1455 browser()->tab_strip_model()->GetActiveWebContents(); | 1605 browser()->tab_strip_model()->GetActiveWebContents(); |
| 1456 content::Source<content::WebContents> source(current_web_contents); | 1606 content::Source<content::WebContents> source(current_web_contents); |
| 1457 | 1607 |
| 1458 ui_test_utils::WindowedNotificationObserverWithDetails< | 1608 ui_test_utils::WindowedNotificationObserverWithDetails< |
| 1459 LanguageDetectionDetails> | 1609 LanguageDetectionDetails> |
| 1460 fr_language_detected_signal(chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED, | 1610 fr_language_detected_signal(chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED, |
| 1461 source); | 1611 source); |
| 1462 fr_language_detected_signal.Wait(); | 1612 fr_language_detected_signal.Wait(); |
| 1463 } | 1613 } |
| OLD | NEW |