Chromium Code Reviews| 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(); | |
| 239 TranslateDownloadManager::GetInstance()->set_application_locale( | |
|
blundell
2014/01/27 16:02:33
Should this not be part of TranslateService::Initi
droger
2014/01/28 14:16:48
Good suggestion.
Done.
| |
| 240 g_browser_process->GetApplicationLocale()); | |
| 236 TranslateManager::GetInstance()->ClearTranslateScript(); | 241 TranslateManager::GetInstance()->ClearTranslateScript(); |
| 237 TranslateManager::GetInstance()-> | 242 TranslateManager::GetInstance()-> |
| 238 SetTranslateScriptExpirationDelay(60 * 60 * 1000); | 243 SetTranslateScriptExpirationDelay(60 * 60 * 1000); |
| 239 TranslateManager::GetInstance()->set_translate_max_reload_attemps(0); | 244 TranslateManager::GetInstance()->set_translate_max_reload_attemps(0); |
| 240 | 245 |
| 241 ChromeRenderViewHostTestHarness::SetUp(); | 246 ChromeRenderViewHostTestHarness::SetUp(); |
| 242 InfoBarService::CreateForWebContents(web_contents()); | 247 InfoBarService::CreateForWebContents(web_contents()); |
| 243 TranslateTabHelper::CreateForWebContents(web_contents()); | 248 TranslateTabHelper::CreateForWebContents(web_contents()); |
| 244 | 249 |
| 245 notification_registrar_.Add(this, | 250 notification_registrar_.Add(this, |
| 246 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, | 251 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, |
| 247 content::Source<InfoBarService>(infobar_service())); | 252 content::Source<InfoBarService>(infobar_service())); |
| 248 } | 253 } |
| 249 | 254 |
| 250 virtual void TearDown() { | 255 virtual void TearDown() { |
| 251 process()->sink().ClearMessages(); | 256 process()->sink().ClearMessages(); |
| 252 | 257 |
| 253 notification_registrar_.Remove(this, | 258 notification_registrar_.Remove(this, |
| 254 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, | 259 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, |
| 255 content::Source<InfoBarService>(infobar_service())); | 260 content::Source<InfoBarService>(infobar_service())); |
| 256 | 261 |
| 257 ChromeRenderViewHostTestHarness::TearDown(); | 262 ChromeRenderViewHostTestHarness::TearDown(); |
| 258 TestingBrowserProcess::DeleteInstance(); | 263 TestingBrowserProcess::DeleteInstance(); |
| 259 } | 264 } |
| 260 | 265 |
| 266 void SetApplicationLocale(const std::string& locale) { | |
| 267 g_browser_process->SetApplicationLocale(locale); | |
| 268 TranslateDownloadManager::GetInstance()->set_application_locale( | |
| 269 g_browser_process->GetApplicationLocale()); | |
| 270 } | |
| 271 | |
| 261 void SimulateTranslateScriptURLFetch(bool success) { | 272 void SimulateTranslateScriptURLFetch(bool success) { |
| 262 net::TestURLFetcher* fetcher = | 273 net::TestURLFetcher* fetcher = |
| 263 url_fetcher_factory_.GetFetcherByID(TranslateScript::kFetcherId); | 274 url_fetcher_factory_.GetFetcherByID(TranslateScript::kFetcherId); |
| 264 ASSERT_TRUE(fetcher); | 275 ASSERT_TRUE(fetcher); |
| 265 net::URLRequestStatus status; | 276 net::URLRequestStatus status; |
| 266 status.set_status(success ? net::URLRequestStatus::SUCCESS : | 277 status.set_status(success ? net::URLRequestStatus::SUCCESS : |
| 267 net::URLRequestStatus::FAILED); | 278 net::URLRequestStatus::FAILED); |
| 268 fetcher->set_url(fetcher->GetOriginalURL()); | 279 fetcher->set_url(fetcher->GetOriginalURL()); |
| 269 fetcher->set_status(status); | 280 fetcher->set_status(status); |
| 270 fetcher->set_response_code(success ? 200 : 500); | 281 fetcher->set_response_code(success ? 200 : 500); |
| (...skipping 704 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 975 ChromeViewMsg_RevertTranslation::ID); | 986 ChromeViewMsg_RevertTranslation::ID); |
| 976 EXPECT_TRUE(message != NULL); | 987 EXPECT_TRUE(message != NULL); |
| 977 // And it should have removed the infobar. | 988 // And it should have removed the infobar. |
| 978 EXPECT_TRUE(GetTranslateInfoBar() == NULL); | 989 EXPECT_TRUE(GetTranslateInfoBar() == NULL); |
| 979 } | 990 } |
| 980 | 991 |
| 981 // Tests that no translate infobar is shown and context menu is disabled, when | 992 // 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. | 993 // Chrome is in a language that the translate server does not support. |
| 983 TEST_F(TranslateManagerBrowserTest, UnsupportedUILanguage) { | 994 TEST_F(TranslateManagerBrowserTest, UnsupportedUILanguage) { |
| 984 std::string original_lang = g_browser_process->GetApplicationLocale(); | 995 std::string original_lang = g_browser_process->GetApplicationLocale(); |
| 985 g_browser_process->SetApplicationLocale("qbz"); | 996 SetApplicationLocale("qbz"); |
| 986 | 997 |
| 987 // Make sure that the accept language list only contains unsupported languages | 998 // Make sure that the accept language list only contains unsupported languages |
| 988 Profile* profile = | 999 Profile* profile = |
| 989 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); | 1000 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); |
| 990 PrefService* prefs = profile->GetPrefs(); | 1001 PrefService* prefs = profile->GetPrefs(); |
| 991 prefs->SetString(prefs::kAcceptLanguages, "qbz"); | 1002 prefs->SetString(prefs::kAcceptLanguages, "qbz"); |
| 992 | 1003 |
| 993 // Simulate navigating to a page in a language supported by the translate | 1004 // Simulate navigating to a page in a language supported by the translate |
| 994 // server. | 1005 // server. |
| 995 SimulateNavigation(GURL("http://www.google.com"), "en", true); | 1006 SimulateNavigation(GURL("http://www.google.com"), "en", true); |
| 996 | 1007 |
| 997 // No info-bar should be shown. | 1008 // No info-bar should be shown. |
| 998 EXPECT_TRUE(GetTranslateInfoBar() == NULL); | 1009 EXPECT_TRUE(GetTranslateInfoBar() == NULL); |
| 999 | 1010 |
| 1000 // And the context menu option should be disabled too. | 1011 // And the context menu option should be disabled too. |
| 1001 scoped_ptr<TestRenderViewContextMenu> menu( | 1012 scoped_ptr<TestRenderViewContextMenu> menu( |
| 1002 TestRenderViewContextMenu::CreateContextMenu(web_contents())); | 1013 TestRenderViewContextMenu::CreateContextMenu(web_contents())); |
| 1003 menu->Init(); | 1014 menu->Init(); |
| 1004 EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE)); | 1015 EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE)); |
| 1005 EXPECT_FALSE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE)); | 1016 EXPECT_FALSE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE)); |
| 1006 | 1017 |
| 1007 g_browser_process->SetApplicationLocale(original_lang); | 1018 SetApplicationLocale(original_lang); |
| 1008 } | 1019 } |
| 1009 | 1020 |
| 1010 // Tests that the first supported accept language is selected | 1021 // Tests that the first supported accept language is selected |
| 1011 TEST_F(TranslateManagerBrowserTest, TranslateAcceptLanguage) { | 1022 TEST_F(TranslateManagerBrowserTest, TranslateAcceptLanguage) { |
| 1012 // Set locate to non-existant language | 1023 // Set locate to non-existant language |
| 1013 std::string original_lang = g_browser_process->GetApplicationLocale(); | 1024 std::string original_lang = g_browser_process->GetApplicationLocale(); |
| 1014 g_browser_process->SetApplicationLocale("qbz"); | 1025 SetApplicationLocale("qbz"); |
| 1015 | 1026 |
| 1016 // Set Qbz and French as the only accepted languages | 1027 // Set Qbz and French as the only accepted languages |
| 1017 Profile* profile = | 1028 Profile* profile = |
| 1018 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); | 1029 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); |
| 1019 PrefService* prefs = profile->GetPrefs(); | 1030 PrefService* prefs = profile->GetPrefs(); |
| 1020 prefs->SetString(prefs::kAcceptLanguages, "qbz,fr"); | 1031 prefs->SetString(prefs::kAcceptLanguages, "qbz,fr"); |
| 1021 | 1032 |
| 1022 // Go to a German page | 1033 // Go to a German page |
| 1023 SimulateNavigation(GURL("http://google.de"), "de", true); | 1034 SimulateNavigation(GURL("http://google.de"), "de", true); |
| 1024 | 1035 |
| (...skipping 591 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1616 content::WebContents* current_web_contents = | 1627 content::WebContents* current_web_contents = |
| 1617 browser()->tab_strip_model()->GetActiveWebContents(); | 1628 browser()->tab_strip_model()->GetActiveWebContents(); |
| 1618 content::Source<content::WebContents> source(current_web_contents); | 1629 content::Source<content::WebContents> source(current_web_contents); |
| 1619 | 1630 |
| 1620 ui_test_utils::WindowedNotificationObserverWithDetails< | 1631 ui_test_utils::WindowedNotificationObserverWithDetails< |
| 1621 LanguageDetectionDetails> | 1632 LanguageDetectionDetails> |
| 1622 fr_language_detected_signal(chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED, | 1633 fr_language_detected_signal(chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED, |
| 1623 source); | 1634 source); |
| 1624 fr_language_detected_signal.Wait(); | 1635 fr_language_detected_signal.Wait(); |
| 1625 } | 1636 } |
| OLD | NEW |