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

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

Issue 25373009: Translate: New Bubble UX (for the view toolkit) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Sky's review (3) Created 7 years, 2 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
OLDNEW
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
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
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 TEST_F(TranslateManagerBrowserTest, BubbleNormalTranslate) {
1455 // Prepare for the bubble
1456 CommandLine* command_line = CommandLine::ForCurrentProcess();
1457 command_line->AppendSwitch(switches::kEnableTranslateNewUX);
1458 MockTranslateBubbleFactory* factory = new MockTranslateBubbleFactory;
1459 scoped_ptr<TranslateBubbleFactory> factory_ptr(factory);
1460 TranslateBubbleFactory::SetFactory(factory);
1461
1462 SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
1463
1464 // Check the bubble exists instead of the infobar.
1465 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
1466 ASSERT_TRUE(infobar == NULL);
1467 TranslateBubbleModel* bubble = factory->model();
1468 ASSERT_TRUE(bubble != NULL);
1469 EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_BEFORE_TRANSLATE,
1470 bubble->GetViewState());
1471
1472 // Simulate clicking translate.
1473 process()->sink().ClearMessages();
1474 bubble->Translate();
1475
1476 // Check the bubble shows "Translating...".
1477 bubble = factory->model();
1478 ASSERT_TRUE(bubble != NULL);
1479 EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_TRANSLATING,
1480 bubble->GetViewState());
1481
1482 // Simulate the translate script being retrieved (it only needs to be done
1483 // once in the test as it is cached).
1484 SimulateTranslateScriptURLFetch(true);
1485
1486 // Simulate the render notifying the translation has been done.
1487 SimulateOnPageTranslated("fr", "en");
1488
1489 // Check the bubble shows "Translated."
1490 bubble = factory->model();
1491 ASSERT_TRUE(bubble != NULL);
1492 EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_AFTER_TRANSLATE,
1493 bubble->GetViewState());
1494 }
1495
1496 TEST_F(TranslateManagerBrowserTest, BubbleTranslateScriptNotAvailable) {
1497 // Prepare for the bubble
1498 CommandLine* command_line = CommandLine::ForCurrentProcess();
1499 command_line->AppendSwitch(switches::kEnableTranslateNewUX);
1500 MockTranslateBubbleFactory* factory = new MockTranslateBubbleFactory;
1501 scoped_ptr<TranslateBubbleFactory> factory_ptr(factory);
1502 TranslateBubbleFactory::SetFactory(factory);
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 = factory->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 = factory->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 MockTranslateBubbleFactory* factory = new MockTranslateBubbleFactory;
1534 scoped_ptr<TranslateBubbleFactory> factory_ptr(factory);
1535 TranslateBubbleFactory::SetFactory(factory);
1536
1537 // Simulate navigating to a page ("und" is the string returned by the CLD for
1538 // languages it does not recognize).
1539 SimulateNavigation(GURL("http://www.google.mys"), "und", true);
1540
1541 // We should not have a bubble as we don't know the language.
1542 ASSERT_TRUE(factory->model() == NULL);
1543
1544 // Translate the page anyway throught the context menu.
1545 scoped_ptr<TestRenderViewContextMenu> menu(
1546 TestRenderViewContextMenu::CreateContextMenu(web_contents()));
1547 menu->Init();
1548 menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE, 0);
1549
1550 // Check the bubble exists instead of the infobar.
1551 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
1552 ASSERT_TRUE(infobar == NULL);
1553 TranslateBubbleModel* bubble = factory->model();
1554 ASSERT_TRUE(bubble != NULL);
1555 EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_TRANSLATING,
1556 bubble->GetViewState());
1557 }
1558
1413 // Test is flaky on Win http://crbug.com/166334 1559 // Test is flaky on Win http://crbug.com/166334
1414 #if defined(OS_WIN) 1560 #if defined(OS_WIN)
1415 #define MAYBE_PRE_TranslateSessionRestore DISABLED_PRE_TranslateSessionRestore 1561 #define MAYBE_PRE_TranslateSessionRestore DISABLED_PRE_TranslateSessionRestore
1416 #else 1562 #else
1417 #define MAYBE_PRE_TranslateSessionRestore PRE_TranslateSessionRestore 1563 #define MAYBE_PRE_TranslateSessionRestore PRE_TranslateSessionRestore
1418 #endif 1564 #endif
1419 // Test that session restore restores the translate infobar and other translate 1565 // Test that session restore restores the translate infobar and other translate
1420 // settings. 1566 // settings.
1421 IN_PROC_BROWSER_TEST_F(InProcessBrowserTest, 1567 IN_PROC_BROWSER_TEST_F(InProcessBrowserTest,
1422 MAYBE_PRE_TranslateSessionRestore) { 1568 MAYBE_PRE_TranslateSessionRestore) {
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
1454 content::WebContents* current_web_contents = 1600 content::WebContents* current_web_contents =
1455 browser()->tab_strip_model()->GetActiveWebContents(); 1601 browser()->tab_strip_model()->GetActiveWebContents();
1456 content::Source<content::WebContents> source(current_web_contents); 1602 content::Source<content::WebContents> source(current_web_contents);
1457 1603
1458 ui_test_utils::WindowedNotificationObserverWithDetails< 1604 ui_test_utils::WindowedNotificationObserverWithDetails<
1459 LanguageDetectionDetails> 1605 LanguageDetectionDetails>
1460 fr_language_detected_signal(chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED, 1606 fr_language_detected_signal(chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED,
1461 source); 1607 source);
1462 fr_language_detected_signal.Wait(); 1608 fr_language_detected_signal.Wait();
1463 } 1609 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698