| 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/command_line.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/prefs/pref_change_registrar.h" | 11 #include "base/prefs/pref_change_registrar.h" |
| 12 #include "base/prefs/pref_service.h" | 12 #include "base/prefs/pref_service.h" |
| 13 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
| 14 #include "chrome/app/chrome_command_ids.h" | 14 #include "chrome/app/chrome_command_ids.h" |
| 15 #include "chrome/browser/chrome_notification_types.h" | 15 #include "chrome/browser/chrome_notification_types.h" |
| 16 #include "chrome/browser/extensions/test_extension_system.h" | 16 #include "chrome/browser/extensions/test_extension_system.h" |
| 17 #include "chrome/browser/infobars/infobar.h" | 17 #include "chrome/browser/infobars/infobar.h" |
| 18 #include "chrome/browser/infobars/infobar_service.h" | 18 #include "chrome/browser/infobars/infobar_service.h" |
| 19 #include "chrome/browser/prefs/session_startup_pref.h" | 19 #include "chrome/browser/prefs/session_startup_pref.h" |
| 20 #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_infobar_delegate.h" | 21 #include "chrome/browser/translate/translate_infobar_delegate.h" |
| 22 #include "chrome/browser/translate/translate_language_list.h" | 22 #include "chrome/browser/translate/translate_language_list.h" |
| 23 #include "chrome/browser/translate/translate_manager.h" | 23 #include "chrome/browser/translate/translate_manager.h" |
| 24 #include "chrome/browser/translate/translate_prefs.h" | 24 #include "chrome/browser/translate/translate_prefs.h" |
| 25 #include "chrome/browser/translate/translate_script.h" | 25 #include "chrome/browser/translate/translate_script.h" |
| 26 #include "chrome/browser/translate/translate_service.h" |
| 26 #include "chrome/browser/translate/translate_tab_helper.h" | 27 #include "chrome/browser/translate/translate_tab_helper.h" |
| 27 #include "chrome/browser/ui/browser.h" | 28 #include "chrome/browser/ui/browser.h" |
| 28 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 29 #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_factory.h" |
| 30 #include "chrome/browser/ui/translate/translate_bubble_model.h" | 31 #include "chrome/browser/ui/translate/translate_bubble_model.h" |
| 31 #include "chrome/browser/ui/translate/translate_bubble_model_impl.h" | 32 #include "chrome/browser/ui/translate/translate_bubble_model_impl.h" |
| 32 #include "chrome/common/chrome_switches.h" | 33 #include "chrome/common/chrome_switches.h" |
| 33 #include "chrome/common/pref_names.h" | 34 #include "chrome/common/pref_names.h" |
| 34 #include "chrome/common/render_messages.h" | 35 #include "chrome/common/render_messages.h" |
| 35 #include "chrome/common/url_constants.h" | 36 #include "chrome/common/url_constants.h" |
| 36 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 37 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 37 #include "chrome/test/base/in_process_browser_test.h" | 38 #include "chrome/test/base/in_process_browser_test.h" |
| 38 #include "chrome/test/base/testing_browser_process.h" | 39 #include "chrome/test/base/testing_browser_process.h" |
| 39 #include "chrome/test/base/testing_profile.h" | 40 #include "chrome/test/base/testing_profile.h" |
| 40 #include "chrome/test/base/ui_test_utils.h" | 41 #include "chrome/test/base/ui_test_utils.h" |
| 42 #include "components/translate/core/browser/translate_download_manager.h" |
| 41 #include "components/translate/core/common/language_detection_details.h" | 43 #include "components/translate/core/common/language_detection_details.h" |
| 42 #include "content/public/browser/navigation_details.h" | 44 #include "content/public/browser/navigation_details.h" |
| 43 #include "content/public/browser/navigation_entry.h" | 45 #include "content/public/browser/navigation_entry.h" |
| 44 #include "content/public/browser/notification_details.h" | 46 #include "content/public/browser/notification_details.h" |
| 45 #include "content/public/browser/notification_registrar.h" | 47 #include "content/public/browser/notification_registrar.h" |
| 46 #include "content/public/browser/web_contents.h" | 48 #include "content/public/browser/web_contents.h" |
| 47 #include "content/public/test/mock_render_process_host.h" | 49 #include "content/public/test/mock_render_process_host.h" |
| 48 #include "content/public/test/test_renderer_host.h" | 50 #include "content/public/test/test_renderer_host.h" |
| 49 #include "net/url_request/test_url_fetcher_factory.h" | 51 #include "net/url_request/test_url_fetcher_factory.h" |
| 50 #include "net/url_request/url_fetcher_delegate.h" | 52 #include "net/url_request/url_fetcher_delegate.h" |
| (...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 226 // Right now it fails to get the translate infobar if you run it there. | 228 // Right now it fails to get the translate infobar if you run it there. |
| 227 TestingBrowserProcess::CreateInstance(); | 229 TestingBrowserProcess::CreateInstance(); |
| 228 // Access the TranslateManager singleton so it is created before we call | 230 // Access the TranslateManager singleton so it is created before we call |
| 229 // ChromeRenderViewHostTestHarness::SetUp() to match what's done in Chrome, | 231 // ChromeRenderViewHostTestHarness::SetUp() to match what's done in Chrome, |
| 230 // where the TranslateManager is created before the WebContents. This | 232 // where the TranslateManager is created before the WebContents. This |
| 231 // matters as they both register for similar events and we want the | 233 // matters as they both register for similar events and we want the |
| 232 // notifications to happen in the same sequence (TranslateManager first, | 234 // notifications to happen in the same sequence (TranslateManager first, |
| 233 // WebContents second). Also clears the translate script so it is fetched | 235 // WebContents second). Also clears the translate script so it is fetched |
| 234 // everytime and sets the expiration delay to a large value by default (in | 236 // everytime and sets the expiration delay to a large value by default (in |
| 235 // case it was zeroed in a previous test). | 237 // case it was zeroed in a previous test). |
| 238 TranslateService::Initialize(); |
| 236 TranslateManager::GetInstance()->ClearTranslateScript(); | 239 TranslateManager::GetInstance()->ClearTranslateScript(); |
| 237 TranslateManager::GetInstance()-> | 240 TranslateManager::GetInstance()-> |
| 238 SetTranslateScriptExpirationDelay(60 * 60 * 1000); | 241 SetTranslateScriptExpirationDelay(60 * 60 * 1000); |
| 239 TranslateManager::GetInstance()->set_translate_max_reload_attemps(0); | 242 TranslateManager::GetInstance()->set_translate_max_reload_attemps(0); |
| 240 | 243 |
| 241 ChromeRenderViewHostTestHarness::SetUp(); | 244 ChromeRenderViewHostTestHarness::SetUp(); |
| 242 InfoBarService::CreateForWebContents(web_contents()); | 245 InfoBarService::CreateForWebContents(web_contents()); |
| 243 TranslateTabHelper::CreateForWebContents(web_contents()); | 246 TranslateTabHelper::CreateForWebContents(web_contents()); |
| 244 | 247 |
| 245 notification_registrar_.Add(this, | 248 notification_registrar_.Add(this, |
| 246 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, | 249 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, |
| 247 content::Source<InfoBarService>(infobar_service())); | 250 content::Source<InfoBarService>(infobar_service())); |
| 248 } | 251 } |
| 249 | 252 |
| 250 virtual void TearDown() { | 253 virtual void TearDown() { |
| 251 process()->sink().ClearMessages(); | 254 process()->sink().ClearMessages(); |
| 252 | 255 |
| 253 notification_registrar_.Remove(this, | 256 notification_registrar_.Remove(this, |
| 254 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, | 257 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, |
| 255 content::Source<InfoBarService>(infobar_service())); | 258 content::Source<InfoBarService>(infobar_service())); |
| 256 | 259 |
| 257 ChromeRenderViewHostTestHarness::TearDown(); | 260 ChromeRenderViewHostTestHarness::TearDown(); |
| 258 TestingBrowserProcess::DeleteInstance(); | 261 TestingBrowserProcess::DeleteInstance(); |
| 259 } | 262 } |
| 260 | 263 |
| 264 void SetApplicationLocale(const std::string& locale) { |
| 265 g_browser_process->SetApplicationLocale(locale); |
| 266 TranslateDownloadManager::GetInstance()->set_application_locale( |
| 267 g_browser_process->GetApplicationLocale()); |
| 268 } |
| 269 |
| 261 void SimulateTranslateScriptURLFetch(bool success) { | 270 void SimulateTranslateScriptURLFetch(bool success) { |
| 262 net::TestURLFetcher* fetcher = | 271 net::TestURLFetcher* fetcher = |
| 263 url_fetcher_factory_.GetFetcherByID(TranslateScript::kFetcherId); | 272 url_fetcher_factory_.GetFetcherByID(TranslateScript::kFetcherId); |
| 264 ASSERT_TRUE(fetcher); | 273 ASSERT_TRUE(fetcher); |
| 265 net::URLRequestStatus status; | 274 net::URLRequestStatus status; |
| 266 status.set_status(success ? net::URLRequestStatus::SUCCESS : | 275 status.set_status(success ? net::URLRequestStatus::SUCCESS : |
| 267 net::URLRequestStatus::FAILED); | 276 net::URLRequestStatus::FAILED); |
| 268 fetcher->set_url(fetcher->GetOriginalURL()); | 277 fetcher->set_url(fetcher->GetOriginalURL()); |
| 269 fetcher->set_status(status); | 278 fetcher->set_status(status); |
| 270 fetcher->set_response_code(success ? 200 : 500); | 279 fetcher->set_response_code(success ? 200 : 500); |
| (...skipping 704 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 975 ChromeViewMsg_RevertTranslation::ID); | 984 ChromeViewMsg_RevertTranslation::ID); |
| 976 EXPECT_TRUE(message != NULL); | 985 EXPECT_TRUE(message != NULL); |
| 977 // And it should have removed the infobar. | 986 // And it should have removed the infobar. |
| 978 EXPECT_TRUE(GetTranslateInfoBar() == NULL); | 987 EXPECT_TRUE(GetTranslateInfoBar() == NULL); |
| 979 } | 988 } |
| 980 | 989 |
| 981 // Tests that no translate infobar is shown and context menu is disabled, when | 990 // Tests that no translate infobar is shown and context menu is disabled, when |
| 982 // Chrome is in a language that the translate server does not support. | 991 // Chrome is in a language that the translate server does not support. |
| 983 TEST_F(TranslateManagerBrowserTest, UnsupportedUILanguage) { | 992 TEST_F(TranslateManagerBrowserTest, UnsupportedUILanguage) { |
| 984 std::string original_lang = g_browser_process->GetApplicationLocale(); | 993 std::string original_lang = g_browser_process->GetApplicationLocale(); |
| 985 g_browser_process->SetApplicationLocale("qbz"); | 994 SetApplicationLocale("qbz"); |
| 986 | 995 |
| 987 // Make sure that the accept language list only contains unsupported languages | 996 // Make sure that the accept language list only contains unsupported languages |
| 988 Profile* profile = | 997 Profile* profile = |
| 989 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); | 998 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); |
| 990 PrefService* prefs = profile->GetPrefs(); | 999 PrefService* prefs = profile->GetPrefs(); |
| 991 prefs->SetString(prefs::kAcceptLanguages, "qbz"); | 1000 prefs->SetString(prefs::kAcceptLanguages, "qbz"); |
| 992 | 1001 |
| 993 // Simulate navigating to a page in a language supported by the translate | 1002 // Simulate navigating to a page in a language supported by the translate |
| 994 // server. | 1003 // server. |
| 995 SimulateNavigation(GURL("http://www.google.com"), "en", true); | 1004 SimulateNavigation(GURL("http://www.google.com"), "en", true); |
| 996 | 1005 |
| 997 // No info-bar should be shown. | 1006 // No info-bar should be shown. |
| 998 EXPECT_TRUE(GetTranslateInfoBar() == NULL); | 1007 EXPECT_TRUE(GetTranslateInfoBar() == NULL); |
| 999 | 1008 |
| 1000 // And the context menu option should be disabled too. | 1009 // And the context menu option should be disabled too. |
| 1001 scoped_ptr<TestRenderViewContextMenu> menu( | 1010 scoped_ptr<TestRenderViewContextMenu> menu( |
| 1002 TestRenderViewContextMenu::CreateContextMenu(web_contents())); | 1011 TestRenderViewContextMenu::CreateContextMenu(web_contents())); |
| 1003 menu->Init(); | 1012 menu->Init(); |
| 1004 EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE)); | 1013 EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE)); |
| 1005 EXPECT_FALSE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE)); | 1014 EXPECT_FALSE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE)); |
| 1006 | 1015 |
| 1007 g_browser_process->SetApplicationLocale(original_lang); | 1016 SetApplicationLocale(original_lang); |
| 1008 } | 1017 } |
| 1009 | 1018 |
| 1010 // Tests that the first supported accept language is selected | 1019 // Tests that the first supported accept language is selected |
| 1011 TEST_F(TranslateManagerBrowserTest, TranslateAcceptLanguage) { | 1020 TEST_F(TranslateManagerBrowserTest, TranslateAcceptLanguage) { |
| 1012 // Set locate to non-existant language | 1021 // Set locate to non-existant language |
| 1013 std::string original_lang = g_browser_process->GetApplicationLocale(); | 1022 std::string original_lang = g_browser_process->GetApplicationLocale(); |
| 1014 g_browser_process->SetApplicationLocale("qbz"); | 1023 SetApplicationLocale("qbz"); |
| 1015 | 1024 |
| 1016 // Set Qbz and French as the only accepted languages | 1025 // Set Qbz and French as the only accepted languages |
| 1017 Profile* profile = | 1026 Profile* profile = |
| 1018 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); | 1027 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); |
| 1019 PrefService* prefs = profile->GetPrefs(); | 1028 PrefService* prefs = profile->GetPrefs(); |
| 1020 prefs->SetString(prefs::kAcceptLanguages, "qbz,fr"); | 1029 prefs->SetString(prefs::kAcceptLanguages, "qbz,fr"); |
| 1021 | 1030 |
| 1022 // Go to a German page | 1031 // Go to a German page |
| 1023 SimulateNavigation(GURL("http://google.de"), "de", true); | 1032 SimulateNavigation(GURL("http://google.de"), "de", true); |
| 1024 | 1033 |
| (...skipping 591 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1616 content::WebContents* current_web_contents = | 1625 content::WebContents* current_web_contents = |
| 1617 browser()->tab_strip_model()->GetActiveWebContents(); | 1626 browser()->tab_strip_model()->GetActiveWebContents(); |
| 1618 content::Source<content::WebContents> source(current_web_contents); | 1627 content::Source<content::WebContents> source(current_web_contents); |
| 1619 | 1628 |
| 1620 ui_test_utils::WindowedNotificationObserverWithDetails< | 1629 ui_test_utils::WindowedNotificationObserverWithDetails< |
| 1621 LanguageDetectionDetails> | 1630 LanguageDetectionDetails> |
| 1622 fr_language_detected_signal(chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED, | 1631 fr_language_detected_signal(chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED, |
| 1623 source); | 1632 source); |
| 1624 fr_language_detected_signal.Wait(); | 1633 fr_language_detected_signal.Wait(); |
| 1625 } | 1634 } |
| OLD | NEW |