| 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 "chrome/browser/translate/translate_manager.h" |
| 6 #include <set> | |
| 7 #include <vector> | |
| 8 | 6 |
| 9 #include "base/command_line.h" | |
| 10 #include "base/memory/scoped_ptr.h" | |
| 11 #include "base/prefs/pref_change_registrar.h" | |
| 12 #include "base/prefs/pref_service.h" | |
| 13 #include "base/strings/stringprintf.h" | |
| 14 #include "chrome/app/chrome_command_ids.h" | |
| 15 #include "chrome/browser/chrome_notification_types.h" | 7 #include "chrome/browser/chrome_notification_types.h" |
| 16 #include "chrome/browser/extensions/test_extension_system.h" | |
| 17 #include "chrome/browser/infobars/infobar.h" | |
| 18 #include "chrome/browser/infobars/infobar_service.h" | |
| 19 #include "chrome/browser/prefs/session_startup_pref.h" | 8 #include "chrome/browser/prefs/session_startup_pref.h" |
| 20 #include "chrome/browser/renderer_context_menu/render_view_context_menu.h" | |
| 21 #include "chrome/browser/translate/translate_infobar_delegate.h" | |
| 22 #include "chrome/browser/translate/translate_manager.h" | |
| 23 #include "chrome/browser/translate/translate_service.h" | |
| 24 #include "chrome/browser/translate/translate_tab_helper.h" | 9 #include "chrome/browser/translate/translate_tab_helper.h" |
| 25 #include "chrome/browser/ui/browser.h" | 10 #include "chrome/browser/ui/browser.h" |
| 26 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 11 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 27 #include "chrome/browser/ui/translate/translate_bubble_factory.h" | |
| 28 #include "chrome/browser/ui/translate/translate_bubble_model.h" | |
| 29 #include "chrome/browser/ui/translate/translate_bubble_model_impl.h" | |
| 30 #include "chrome/common/chrome_switches.h" | |
| 31 #include "chrome/common/pref_names.h" | |
| 32 #include "chrome/common/render_messages.h" | |
| 33 #include "chrome/common/url_constants.h" | |
| 34 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | |
| 35 #include "chrome/test/base/in_process_browser_test.h" | 12 #include "chrome/test/base/in_process_browser_test.h" |
| 36 #include "chrome/test/base/testing_browser_process.h" | |
| 37 #include "chrome/test/base/testing_profile.h" | |
| 38 #include "chrome/test/base/ui_test_utils.h" | 13 #include "chrome/test/base/ui_test_utils.h" |
| 39 #include "components/translate/core/browser/translate_accept_languages.h" | |
| 40 #include "components/translate/core/browser/translate_download_manager.h" | |
| 41 #include "components/translate/core/browser/translate_language_list.h" | |
| 42 #include "components/translate/core/browser/translate_prefs.h" | |
| 43 #include "components/translate/core/browser/translate_script.h" | |
| 44 #include "components/translate/core/common/language_detection_details.h" | 14 #include "components/translate/core/common/language_detection_details.h" |
| 45 #include "components/translate/core/common/translate_pref_names.h" | 15 #include "url/gurl.h" |
| 46 #include "content/public/browser/navigation_details.h" | |
| 47 #include "content/public/browser/navigation_entry.h" | |
| 48 #include "content/public/browser/notification_details.h" | |
| 49 #include "content/public/browser/notification_registrar.h" | |
| 50 #include "content/public/browser/web_contents.h" | |
| 51 #include "content/public/test/mock_render_process_host.h" | |
| 52 #include "content/public/test/test_renderer_host.h" | |
| 53 #include "net/url_request/test_url_fetcher_factory.h" | |
| 54 #include "net/url_request/url_fetcher_delegate.h" | |
| 55 #include "testing/gmock/include/gmock/gmock.h" | |
| 56 #include "third_party/WebKit/public/web/WebContextMenuData.h" | |
| 57 | 16 |
| 58 | 17 class TranslateManagerBrowserTest : public InProcessBrowserTest {}; |
| 59 // An observer that keeps track of whether a navigation entry was committed. | |
| 60 class NavEntryCommittedObserver : public content::NotificationObserver { | |
| 61 public: | |
| 62 explicit NavEntryCommittedObserver(content::WebContents* web_contents) { | |
| 63 registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, | |
| 64 content::Source<content::NavigationController>( | |
| 65 &web_contents->GetController())); | |
| 66 } | |
| 67 | |
| 68 virtual void Observe(int type, | |
| 69 const content::NotificationSource& source, | |
| 70 const content::NotificationDetails& details) OVERRIDE { | |
| 71 DCHECK(type == content::NOTIFICATION_NAV_ENTRY_COMMITTED); | |
| 72 details_ = | |
| 73 *(content::Details<content::LoadCommittedDetails>(details).ptr()); | |
| 74 } | |
| 75 | |
| 76 const content::LoadCommittedDetails& load_committed_details() const { | |
| 77 return details_; | |
| 78 } | |
| 79 | |
| 80 private: | |
| 81 content::LoadCommittedDetails details_; | |
| 82 content::NotificationRegistrar registrar_; | |
| 83 | |
| 84 DISALLOW_COPY_AND_ASSIGN(NavEntryCommittedObserver); | |
| 85 }; | |
| 86 | |
| 87 class TranslateManagerBrowserTest : public ChromeRenderViewHostTestHarness, | |
| 88 public content::NotificationObserver { | |
| 89 public: | |
| 90 TranslateManagerBrowserTest() | |
| 91 : pref_callback_( | |
| 92 base::Bind(&TranslateManagerBrowserTest::OnPreferenceChanged, | |
| 93 base::Unretained(this))) { | |
| 94 } | |
| 95 | |
| 96 // Simulates navigating to a page and getting the page contents and language | |
| 97 // for that navigation. | |
| 98 void SimulateNavigation(const GURL& url, | |
| 99 const std::string& lang, | |
| 100 bool page_translatable) { | |
| 101 NavigateAndCommit(url); | |
| 102 SimulateOnTranslateLanguageDetermined(lang, page_translatable); | |
| 103 } | |
| 104 | |
| 105 void SimulateOnTranslateLanguageDetermined(const std::string& lang, | |
| 106 bool page_translatable) { | |
| 107 LanguageDetectionDetails details; | |
| 108 details.adopted_language = lang; | |
| 109 content::RenderViewHostTester::TestOnMessageReceived( | |
| 110 rvh(), | |
| 111 ChromeViewHostMsg_TranslateLanguageDetermined( | |
| 112 0, details, page_translatable)); | |
| 113 } | |
| 114 | |
| 115 void SimulateOnPageTranslated(int routing_id, | |
| 116 const std::string& source_lang, | |
| 117 const std::string& target_lang, | |
| 118 TranslateErrors::Type error) { | |
| 119 content::RenderViewHostTester::TestOnMessageReceived( | |
| 120 rvh(), | |
| 121 ChromeViewHostMsg_PageTranslated( | |
| 122 routing_id, 0, source_lang, target_lang, error)); | |
| 123 } | |
| 124 | |
| 125 void SimulateOnPageTranslated(const std::string& source_lang, | |
| 126 const std::string& target_lang) { | |
| 127 SimulateOnPageTranslated(0, source_lang, target_lang, | |
| 128 TranslateErrors::NONE); | |
| 129 } | |
| 130 | |
| 131 bool GetTranslateMessage(int* page_id, | |
| 132 std::string* original_lang, | |
| 133 std::string* target_lang) { | |
| 134 const IPC::Message* message = | |
| 135 process()->sink().GetFirstMessageMatching( | |
| 136 ChromeViewMsg_TranslatePage::ID); | |
| 137 if (!message) | |
| 138 return false; | |
| 139 Tuple4<int, std::string, std::string, std::string> translate_param; | |
| 140 ChromeViewMsg_TranslatePage::Read(message, &translate_param); | |
| 141 if (page_id) | |
| 142 *page_id = translate_param.a; | |
| 143 // Ignore translate_param.b which is the script injected in the page. | |
| 144 if (original_lang) | |
| 145 *original_lang = translate_param.c; | |
| 146 if (target_lang) | |
| 147 *target_lang = translate_param.d; | |
| 148 return true; | |
| 149 } | |
| 150 | |
| 151 InfoBarService* infobar_service() { | |
| 152 return InfoBarService::FromWebContents(web_contents()); | |
| 153 } | |
| 154 | |
| 155 // Returns the translate infobar if there is 1 infobar and it is a translate | |
| 156 // infobar. | |
| 157 TranslateInfoBarDelegate* GetTranslateInfoBar() { | |
| 158 return (infobar_service()->infobar_count() == 1) ? | |
| 159 infobar_service()->infobar_at(0)->delegate()-> | |
| 160 AsTranslateInfoBarDelegate() : NULL; | |
| 161 } | |
| 162 | |
| 163 // If there is 1 infobar and it is a translate infobar, closes it and returns | |
| 164 // true. Returns false otherwise. | |
| 165 bool CloseTranslateInfoBar() { | |
| 166 InfoBarDelegate* infobar = GetTranslateInfoBar(); | |
| 167 if (!infobar) | |
| 168 return false; | |
| 169 infobar->InfoBarDismissed(); // Simulates closing the infobar. | |
| 170 infobar_service()->RemoveInfoBar(infobar_service()->infobar_at(0)); | |
| 171 return true; | |
| 172 } | |
| 173 | |
| 174 // Checks whether |infobar| has been removed and clears the removed infobar | |
| 175 // list. | |
| 176 bool CheckInfoBarRemovedAndReset(InfoBarDelegate* delegate) { | |
| 177 bool found = removed_infobars_.count(delegate) != 0; | |
| 178 removed_infobars_.clear(); | |
| 179 return found; | |
| 180 } | |
| 181 | |
| 182 void ExpireTranslateScriptImmediately() { | |
| 183 TranslateDownloadManager::GetInstance()->SetTranslateScriptExpirationDelay( | |
| 184 0); | |
| 185 } | |
| 186 | |
| 187 // If there is 1 infobar and it is a translate infobar, deny translation and | |
| 188 // returns true. Returns false otherwise. | |
| 189 bool DenyTranslation() { | |
| 190 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); | |
| 191 if (!infobar) | |
| 192 return false; | |
| 193 infobar->TranslationDeclined(); | |
| 194 infobar_service()->RemoveInfoBar(infobar_service()->infobar_at(0)); | |
| 195 return true; | |
| 196 } | |
| 197 | |
| 198 void ReloadAndWait(bool successful_reload) { | |
| 199 NavEntryCommittedObserver nav_observer(web_contents()); | |
| 200 if (successful_reload) | |
| 201 Reload(); | |
| 202 else | |
| 203 FailedReload(); | |
| 204 | |
| 205 // Ensures it is really handled a reload. | |
| 206 const content::LoadCommittedDetails& nav_details = | |
| 207 nav_observer.load_committed_details(); | |
| 208 EXPECT_TRUE(nav_details.entry != NULL); // There was a navigation. | |
| 209 EXPECT_EQ(content::NAVIGATION_TYPE_EXISTING_PAGE, nav_details.type); | |
| 210 | |
| 211 // The TranslateManager class processes the navigation entry committed | |
| 212 // notification in a posted task; process that task. | |
| 213 base::MessageLoop::current()->RunUntilIdle(); | |
| 214 } | |
| 215 | |
| 216 virtual void Observe(int type, | |
| 217 const content::NotificationSource& source, | |
| 218 const content::NotificationDetails& details) { | |
| 219 DCHECK_EQ(chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, type); | |
| 220 removed_infobars_.insert( | |
| 221 content::Details<InfoBar::RemovedDetails>(details)->first->delegate()); | |
| 222 } | |
| 223 | |
| 224 MOCK_METHOD1(OnPreferenceChanged, void(const std::string&)); | |
| 225 | |
| 226 protected: | |
| 227 virtual void SetUp() { | |
| 228 // This test is a unit test but runs in the browser_tests suite. Therefore | |
| 229 // it needs to manage its own TestingBrowserProcess. | |
| 230 // TODO(jamescook): Figure out how to move this suite back to unit_tests. | |
| 231 // Right now it fails to get the translate infobar if you run it there. | |
| 232 TestingBrowserProcess::CreateInstance(); | |
| 233 | |
| 234 TranslateService::Initialize(); | |
| 235 TranslateService::SetUseInfobar(true); | |
| 236 | |
| 237 // Clears the translate script so it is fetched everytime and sets the | |
| 238 // expiration delay to a large value by default (in case it was zeroed in a | |
| 239 // previous test). | |
| 240 TranslateDownloadManager* download_manager = | |
| 241 TranslateDownloadManager::GetInstance(); | |
| 242 download_manager->ClearTranslateScriptForTesting(); | |
| 243 download_manager->SetTranslateScriptExpirationDelay(60 * 60 * 1000); | |
| 244 | |
| 245 ChromeRenderViewHostTestHarness::SetUp(); | |
| 246 InfoBarService::CreateForWebContents(web_contents()); | |
| 247 TranslateTabHelper::CreateForWebContents(web_contents()); | |
| 248 TranslateManager* manager = | |
| 249 TranslateTabHelper::GetManagerFromWebContents(web_contents()); | |
| 250 manager->set_translate_max_reload_attemps(0); | |
| 251 | |
| 252 notification_registrar_.Add(this, | |
| 253 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, | |
| 254 content::Source<InfoBarService>(infobar_service())); | |
| 255 } | |
| 256 | |
| 257 virtual void TearDown() { | |
| 258 process()->sink().ClearMessages(); | |
| 259 | |
| 260 notification_registrar_.Remove(this, | |
| 261 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, | |
| 262 content::Source<InfoBarService>(infobar_service())); | |
| 263 | |
| 264 ChromeRenderViewHostTestHarness::TearDown(); | |
| 265 TestingBrowserProcess::DeleteInstance(); | |
| 266 } | |
| 267 | |
| 268 void SetApplicationLocale(const std::string& locale) { | |
| 269 g_browser_process->SetApplicationLocale(locale); | |
| 270 TranslateDownloadManager::GetInstance()->set_application_locale( | |
| 271 g_browser_process->GetApplicationLocale()); | |
| 272 } | |
| 273 | |
| 274 void SimulateTranslateScriptURLFetch(bool success) { | |
| 275 net::TestURLFetcher* fetcher = | |
| 276 url_fetcher_factory_.GetFetcherByID(TranslateScript::kFetcherId); | |
| 277 ASSERT_TRUE(fetcher); | |
| 278 net::URLRequestStatus status; | |
| 279 status.set_status(success ? net::URLRequestStatus::SUCCESS : | |
| 280 net::URLRequestStatus::FAILED); | |
| 281 fetcher->set_url(fetcher->GetOriginalURL()); | |
| 282 fetcher->set_status(status); | |
| 283 fetcher->set_response_code(success ? 200 : 500); | |
| 284 fetcher->delegate()->OnURLFetchComplete(fetcher); | |
| 285 } | |
| 286 | |
| 287 void SimulateSupportedLanguagesURLFetch( | |
| 288 bool success, | |
| 289 const std::vector<std::string>& languages, | |
| 290 bool use_alpha_languages, | |
| 291 const std::vector<std::string>& alpha_languages) { | |
| 292 net::URLRequestStatus status; | |
| 293 status.set_status(success ? net::URLRequestStatus::SUCCESS : | |
| 294 net::URLRequestStatus::FAILED); | |
| 295 | |
| 296 std::string data; | |
| 297 if (success) { | |
| 298 data = base::StringPrintf( | |
| 299 "%s{\"sl\": {\"bla\": \"bla\"}, \"%s\": {", | |
| 300 TranslateLanguageList::kLanguageListCallbackName, | |
| 301 TranslateLanguageList::kTargetLanguagesKey); | |
| 302 const char* comma = ""; | |
| 303 for (size_t i = 0; i < languages.size(); ++i) { | |
| 304 data += base::StringPrintf( | |
| 305 "%s\"%s\": \"UnusedFullName\"", comma, languages[i].c_str()); | |
| 306 if (i == 0) | |
| 307 comma = ","; | |
| 308 } | |
| 309 | |
| 310 if (use_alpha_languages) { | |
| 311 data += base::StringPrintf("},\"%s\": {", | |
| 312 TranslateLanguageList::kAlphaLanguagesKey); | |
| 313 comma = ""; | |
| 314 for (size_t i = 0; i < alpha_languages.size(); ++i) { | |
| 315 data += base::StringPrintf("%s\"%s\": 1", comma, | |
| 316 alpha_languages[i].c_str()); | |
| 317 if (i == 0) | |
| 318 comma = ","; | |
| 319 } | |
| 320 } | |
| 321 | |
| 322 data += "}})"; | |
| 323 } | |
| 324 net::TestURLFetcher* fetcher = | |
| 325 url_fetcher_factory_.GetFetcherByID(TranslateLanguageList::kFetcherId); | |
| 326 ASSERT_TRUE(fetcher != NULL); | |
| 327 fetcher->set_url(fetcher->GetOriginalURL()); | |
| 328 fetcher->set_status(status); | |
| 329 fetcher->set_response_code(success ? 200 : 500); | |
| 330 fetcher->SetResponseString(data); | |
| 331 fetcher->delegate()->OnURLFetchComplete(fetcher); | |
| 332 } | |
| 333 | |
| 334 void SetPrefObserverExpectation(const char* path) { | |
| 335 EXPECT_CALL(*this, OnPreferenceChanged(std::string(path))); | |
| 336 } | |
| 337 | |
| 338 PrefChangeRegistrar::NamedChangeCallback pref_callback_; | |
| 339 | |
| 340 private: | |
| 341 content::NotificationRegistrar notification_registrar_; | |
| 342 net::TestURLFetcherFactory url_fetcher_factory_; | |
| 343 | |
| 344 // The infobars that have been removed. | |
| 345 // WARNING: the pointers point to deleted objects, use only for comparison. | |
| 346 std::set<InfoBarDelegate*> removed_infobars_; | |
| 347 | |
| 348 DISALLOW_COPY_AND_ASSIGN(TranslateManagerBrowserTest); | |
| 349 }; | |
| 350 | |
| 351 class MockTranslateBubbleFactory : public TranslateBubbleFactory { | |
| 352 public: | |
| 353 MockTranslateBubbleFactory() { | |
| 354 } | |
| 355 | |
| 356 virtual void ShowImplementation(BrowserWindow* window, | |
| 357 content::WebContents* web_contents, | |
| 358 TranslateTabHelper::TranslateStep step, | |
| 359 TranslateErrors::Type error_type) OVERRIDE { | |
| 360 if (model_) { | |
| 361 model_->SetViewState( | |
| 362 TranslateBubbleModelImpl::TranslateStepToViewState(step)); | |
| 363 return; | |
| 364 } | |
| 365 | |
| 366 TranslateTabHelper* translate_tab_helper = | |
| 367 TranslateTabHelper::FromWebContents(web_contents); | |
| 368 std::string source_language = | |
| 369 translate_tab_helper->GetLanguageState().original_language(); | |
| 370 std::string target_language = TranslateDownloadManager::GetLanguageCode( | |
| 371 g_browser_process->GetApplicationLocale()); | |
| 372 scoped_ptr<TranslateUIDelegate> ui_delegate( | |
| 373 new TranslateUIDelegate(web_contents, | |
| 374 source_language, | |
| 375 target_language)); | |
| 376 model_.reset(new TranslateBubbleModelImpl(step, ui_delegate.Pass())); | |
| 377 } | |
| 378 | |
| 379 TranslateBubbleModel* model() { return model_.get(); } | |
| 380 | |
| 381 private: | |
| 382 scoped_ptr<TranslateBubbleModel> model_; | |
| 383 | |
| 384 DISALLOW_COPY_AND_ASSIGN(MockTranslateBubbleFactory); | |
| 385 }; | |
| 386 | |
| 387 namespace { | |
| 388 | |
| 389 class TestRenderViewContextMenu : public RenderViewContextMenu { | |
| 390 public: | |
| 391 static TestRenderViewContextMenu* CreateContextMenu( | |
| 392 content::WebContents* web_contents) { | |
| 393 content::ContextMenuParams params; | |
| 394 params.media_type = blink::WebContextMenuData::MediaTypeNone; | |
| 395 params.x = 0; | |
| 396 params.y = 0; | |
| 397 params.has_image_contents = true; | |
| 398 params.media_flags = 0; | |
| 399 params.spellcheck_enabled = false; | |
| 400 params.is_editable = false; | |
| 401 params.page_url = web_contents->GetController().GetActiveEntry()->GetURL(); | |
| 402 #if defined(OS_MACOSX) | |
| 403 params.writing_direction_default = 0; | |
| 404 params.writing_direction_left_to_right = 0; | |
| 405 params.writing_direction_right_to_left = 0; | |
| 406 #endif // OS_MACOSX | |
| 407 params.edit_flags = blink::WebContextMenuData::CanTranslate; | |
| 408 return new TestRenderViewContextMenu(web_contents->GetMainFrame(), params); | |
| 409 } | |
| 410 | |
| 411 bool IsItemPresent(int id) { | |
| 412 return menu_model_.GetIndexOfCommandId(id) != -1; | |
| 413 } | |
| 414 | |
| 415 virtual void PlatformInit() OVERRIDE { } | |
| 416 virtual void PlatformCancel() OVERRIDE { } | |
| 417 virtual bool GetAcceleratorForCommandId( | |
| 418 int command_id, | |
| 419 ui::Accelerator* accelerator) OVERRIDE { return false; } | |
| 420 | |
| 421 private: | |
| 422 TestRenderViewContextMenu(content::RenderFrameHost* render_frame_host, | |
| 423 const content::ContextMenuParams& params) | |
| 424 : RenderViewContextMenu(render_frame_host, params) { | |
| 425 } | |
| 426 | |
| 427 DISALLOW_COPY_AND_ASSIGN(TestRenderViewContextMenu); | |
| 428 }; | |
| 429 | |
| 430 } // namespace | |
| 431 | |
| 432 TEST_F(TranslateManagerBrowserTest, NormalTranslate) { | |
| 433 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); | |
| 434 | |
| 435 // We should have an infobar. | |
| 436 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); | |
| 437 ASSERT_TRUE(infobar != NULL); | |
| 438 EXPECT_EQ(TranslateTabHelper::BEFORE_TRANSLATE, infobar->translate_step()); | |
| 439 | |
| 440 // Simulate clicking translate. | |
| 441 process()->sink().ClearMessages(); | |
| 442 infobar->Translate(); | |
| 443 | |
| 444 // The "Translating..." infobar should be showing. | |
| 445 infobar = GetTranslateInfoBar(); | |
| 446 ASSERT_TRUE(infobar != NULL); | |
| 447 EXPECT_EQ(TranslateTabHelper::TRANSLATING, infobar->translate_step()); | |
| 448 | |
| 449 // Simulate the translate script being retrieved (it only needs to be done | |
| 450 // once in the test as it is cached). | |
| 451 SimulateTranslateScriptURLFetch(true); | |
| 452 | |
| 453 // Test that we sent the right message to the renderer. | |
| 454 int page_id = 0; | |
| 455 std::string original_lang, target_lang; | |
| 456 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); | |
| 457 EXPECT_EQ("fr", original_lang); | |
| 458 EXPECT_EQ("en", target_lang); | |
| 459 | |
| 460 // Simulate the render notifying the translation has been done. | |
| 461 SimulateOnPageTranslated("fr", "en"); | |
| 462 | |
| 463 // The after translate infobar should be showing. | |
| 464 infobar = GetTranslateInfoBar(); | |
| 465 ASSERT_TRUE(infobar != NULL); | |
| 466 EXPECT_EQ(TranslateTabHelper::AFTER_TRANSLATE, infobar->translate_step()); | |
| 467 | |
| 468 // Simulate changing the original language and translating. | |
| 469 process()->sink().ClearMessages(); | |
| 470 std::string new_original_lang = infobar->language_code_at(0); | |
| 471 infobar->UpdateOriginalLanguageIndex(0); | |
| 472 infobar->Translate(); | |
| 473 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); | |
| 474 EXPECT_EQ(new_original_lang, original_lang); | |
| 475 EXPECT_EQ("en", target_lang); | |
| 476 // Simulate the render notifying the translation has been done. | |
| 477 SimulateOnPageTranslated(new_original_lang, "en"); | |
| 478 infobar = GetTranslateInfoBar(); | |
| 479 ASSERT_TRUE(infobar != NULL); | |
| 480 | |
| 481 // Simulate changing the target language and translating. | |
| 482 process()->sink().ClearMessages(); | |
| 483 std::string new_target_lang = infobar->language_code_at(1); | |
| 484 infobar->UpdateTargetLanguageIndex(1); | |
| 485 infobar->Translate(); | |
| 486 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); | |
| 487 EXPECT_EQ(new_original_lang, original_lang); | |
| 488 EXPECT_EQ(new_target_lang, target_lang); | |
| 489 // Simulate the render notifying the translation has been done. | |
| 490 SimulateOnPageTranslated(new_original_lang, new_target_lang); | |
| 491 infobar = GetTranslateInfoBar(); | |
| 492 ASSERT_TRUE(infobar != NULL); | |
| 493 EXPECT_EQ(new_target_lang, infobar->target_language_code()); | |
| 494 | |
| 495 // Reloading should trigger translation iff Always Translate is on. | |
| 496 ReloadAndWait(true); | |
| 497 infobar = GetTranslateInfoBar(); | |
| 498 ASSERT_TRUE(infobar != NULL); | |
| 499 EXPECT_EQ(TranslateTabHelper::BEFORE_TRANSLATE, infobar->translate_step()); | |
| 500 infobar->UpdateTargetLanguageIndex(1); | |
| 501 infobar->ToggleAlwaysTranslate(); | |
| 502 ReloadAndWait(true); | |
| 503 infobar = GetTranslateInfoBar(); | |
| 504 ASSERT_TRUE(infobar != NULL); | |
| 505 EXPECT_EQ(TranslateTabHelper::TRANSLATING, infobar->translate_step()); | |
| 506 EXPECT_EQ(new_target_lang, infobar->target_language_code()); | |
| 507 } | |
| 508 | |
| 509 TEST_F(TranslateManagerBrowserTest, TranslateScriptNotAvailable) { | |
| 510 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); | |
| 511 | |
| 512 // We should have an infobar. | |
| 513 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); | |
| 514 ASSERT_TRUE(infobar != NULL); | |
| 515 EXPECT_EQ(TranslateTabHelper::BEFORE_TRANSLATE, infobar->translate_step()); | |
| 516 | |
| 517 // Simulate clicking translate. | |
| 518 process()->sink().ClearMessages(); | |
| 519 infobar->Translate(); | |
| 520 SimulateTranslateScriptURLFetch(false); | |
| 521 | |
| 522 // We should not have sent any message to translate to the renderer. | |
| 523 EXPECT_FALSE(GetTranslateMessage(NULL, NULL, NULL)); | |
| 524 | |
| 525 // And we should have an error infobar showing. | |
| 526 infobar = GetTranslateInfoBar(); | |
| 527 ASSERT_TRUE(infobar != NULL); | |
| 528 EXPECT_EQ(TranslateTabHelper::TRANSLATE_ERROR, infobar->translate_step()); | |
| 529 } | |
| 530 | |
| 531 // Ensures we deal correctly with pages for which the browser does not recognize | |
| 532 // the language (the translate server may or not detect the language). | |
| 533 TEST_F(TranslateManagerBrowserTest, TranslateUnknownLanguage) { | |
| 534 // Simulate navigating to a page ("und" is the string returned by the CLD for | |
| 535 // languages it does not recognize). | |
| 536 SimulateNavigation(GURL("http://www.google.mys"), "und", true); | |
| 537 | |
| 538 // We should not have an infobar as we don't know the language. | |
| 539 ASSERT_TRUE(GetTranslateInfoBar() == NULL); | |
| 540 | |
| 541 // Translate the page anyway throught the context menu. | |
| 542 scoped_ptr<TestRenderViewContextMenu> menu( | |
| 543 TestRenderViewContextMenu::CreateContextMenu(web_contents())); | |
| 544 menu->Init(); | |
| 545 menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE, 0); | |
| 546 | |
| 547 // To test that bug #49018 if fixed, make sure we deal correctly with errors. | |
| 548 // Simulate a failure to fetch the translate script. | |
| 549 SimulateTranslateScriptURLFetch(false); | |
| 550 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); | |
| 551 ASSERT_TRUE(infobar != NULL); | |
| 552 EXPECT_EQ(TranslateTabHelper::TRANSLATE_ERROR, infobar->translate_step()); | |
| 553 EXPECT_TRUE(infobar->is_error()); | |
| 554 infobar->MessageInfoBarButtonPressed(); | |
| 555 SimulateTranslateScriptURLFetch(true); // This time succeed. | |
| 556 | |
| 557 // Simulate the render notifying the translation has been done, the server | |
| 558 // having detected the page was in a known and supported language. | |
| 559 SimulateOnPageTranslated("fr", "en"); | |
| 560 | |
| 561 // The after translate infobar should be showing. | |
| 562 infobar = GetTranslateInfoBar(); | |
| 563 ASSERT_TRUE(infobar != NULL); | |
| 564 EXPECT_EQ(TranslateTabHelper::AFTER_TRANSLATE, infobar->translate_step()); | |
| 565 EXPECT_EQ("fr", infobar->original_language_code()); | |
| 566 EXPECT_EQ("en", infobar->target_language_code()); | |
| 567 | |
| 568 // Let's run the same steps but this time the server detects the page is | |
| 569 // already in English. | |
| 570 SimulateNavigation(GURL("http://www.google.com"), "und", true); | |
| 571 menu.reset(TestRenderViewContextMenu::CreateContextMenu(web_contents())); | |
| 572 menu->Init(); | |
| 573 menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE, 0); | |
| 574 SimulateOnPageTranslated(1, "en", "en", TranslateErrors::IDENTICAL_LANGUAGES); | |
| 575 infobar = GetTranslateInfoBar(); | |
| 576 ASSERT_TRUE(infobar != NULL); | |
| 577 EXPECT_EQ(TranslateTabHelper::TRANSLATE_ERROR, infobar->translate_step()); | |
| 578 EXPECT_EQ(TranslateErrors::IDENTICAL_LANGUAGES, infobar->error_type()); | |
| 579 | |
| 580 // Let's run the same steps again but this time the server fails to detect the | |
| 581 // page's language (it returns an empty string). | |
| 582 SimulateNavigation(GURL("http://www.google.com"), "und", true); | |
| 583 menu.reset(TestRenderViewContextMenu::CreateContextMenu(web_contents())); | |
| 584 menu->Init(); | |
| 585 menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE, 0); | |
| 586 SimulateOnPageTranslated(2, std::string(), "en", | |
| 587 TranslateErrors::UNKNOWN_LANGUAGE); | |
| 588 infobar = GetTranslateInfoBar(); | |
| 589 ASSERT_TRUE(infobar != NULL); | |
| 590 EXPECT_EQ(TranslateTabHelper::TRANSLATE_ERROR, infobar->translate_step()); | |
| 591 EXPECT_EQ(TranslateErrors::UNKNOWN_LANGUAGE, infobar->error_type()); | |
| 592 } | |
| 593 | |
| 594 // Tests that we show/don't show an info-bar for the languages. | |
| 595 TEST_F(TranslateManagerBrowserTest, TestLanguages) { | |
| 596 std::vector<std::string> languages; | |
| 597 languages.push_back("en"); | |
| 598 languages.push_back("ja"); | |
| 599 languages.push_back("fr"); | |
| 600 languages.push_back("ht"); | |
| 601 languages.push_back("xx"); | |
| 602 languages.push_back("zh"); | |
| 603 languages.push_back("zh-CN"); | |
| 604 languages.push_back("und"); | |
| 605 | |
| 606 GURL url("http://www.google.com"); | |
| 607 for (size_t i = 0; i < languages.size(); ++i) { | |
| 608 std::string lang = languages[i]; | |
| 609 SCOPED_TRACE(::testing::Message() << "Iteration " << i << | |
| 610 " language=" << lang); | |
| 611 | |
| 612 // We should not have a translate infobar. | |
| 613 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); | |
| 614 ASSERT_TRUE(infobar == NULL); | |
| 615 | |
| 616 SimulateNavigation(url, lang, true); | |
| 617 | |
| 618 // Verify we have/don't have an info-bar as expected. | |
| 619 infobar = GetTranslateInfoBar(); | |
| 620 bool expected = | |
| 621 TranslateDownloadManager::IsSupportedLanguage(lang) && lang != "en"; | |
| 622 EXPECT_EQ(expected, infobar != NULL); | |
| 623 | |
| 624 if (infobar != NULL) | |
| 625 EXPECT_TRUE(CloseTranslateInfoBar()); | |
| 626 } | |
| 627 } | |
| 628 | |
| 629 // Test the fetching of languages from the translate server | |
| 630 TEST_F(TranslateManagerBrowserTest, FetchLanguagesFromTranslateServer) { | |
| 631 std::vector<std::string> server_languages; | |
| 632 // A list of languages to fake being returned by the translate server. | |
| 633 server_languages.push_back("aa"); | |
| 634 server_languages.push_back("ak"); | |
| 635 server_languages.push_back("ab"); | |
| 636 server_languages.push_back("en-CA"); | |
| 637 server_languages.push_back("zh"); | |
| 638 server_languages.push_back("yi"); | |
| 639 server_languages.push_back("fr-FR"); | |
| 640 server_languages.push_back("xx"); | |
| 641 | |
| 642 std::vector<std::string> alpha_languages; | |
| 643 alpha_languages.push_back("aa"); | |
| 644 alpha_languages.push_back("yi"); | |
| 645 | |
| 646 // First, get the default languages list. Note that calling | |
| 647 // GetSupportedLanguages() invokes RequestLanguageList() internally. | |
| 648 std::vector<std::string> default_supported_languages; | |
| 649 TranslateDownloadManager::GetSupportedLanguages(&default_supported_languages); | |
| 650 // To make sure we got the defaults and don't confuse them with the mocks. | |
| 651 ASSERT_NE(default_supported_languages.size(), server_languages.size()); | |
| 652 | |
| 653 // Check that we still get the defaults until the URLFetch has completed. | |
| 654 std::vector<std::string> current_supported_languages; | |
| 655 TranslateDownloadManager::GetSupportedLanguages(¤t_supported_languages); | |
| 656 EXPECT_EQ(default_supported_languages, current_supported_languages); | |
| 657 | |
| 658 // Also check that it didn't change if we failed the URL fetch. | |
| 659 SimulateSupportedLanguagesURLFetch(false, std::vector<std::string>(), | |
| 660 true, std::vector<std::string>()); | |
| 661 current_supported_languages.clear(); | |
| 662 TranslateDownloadManager::GetSupportedLanguages(¤t_supported_languages); | |
| 663 EXPECT_EQ(default_supported_languages, current_supported_languages); | |
| 664 | |
| 665 // Now check that we got the appropriate set of languages from the server. | |
| 666 SimulateSupportedLanguagesURLFetch(true, server_languages, | |
| 667 true, alpha_languages); | |
| 668 current_supported_languages.clear(); | |
| 669 TranslateDownloadManager::GetSupportedLanguages(¤t_supported_languages); | |
| 670 // "xx" can't be displayed in the Translate inforbar, so this is eliminated. | |
| 671 EXPECT_EQ(server_languages.size() - 1, current_supported_languages.size()); | |
| 672 // Not sure we need to guarantee the order of languages, so we find them. | |
| 673 for (size_t i = 0; i < server_languages.size(); ++i) { | |
| 674 const std::string& lang = server_languages[i]; | |
| 675 if (lang == "xx") | |
| 676 continue; | |
| 677 EXPECT_NE(current_supported_languages.end(), | |
| 678 std::find(current_supported_languages.begin(), | |
| 679 current_supported_languages.end(), | |
| 680 lang)); | |
| 681 bool is_alpha = std::find(alpha_languages.begin(), | |
| 682 alpha_languages.end(), | |
| 683 lang) != alpha_languages.end(); | |
| 684 EXPECT_EQ(TranslateDownloadManager::IsAlphaLanguage(lang), is_alpha); | |
| 685 } | |
| 686 } | |
| 687 | |
| 688 // Test the fetching of languages from the translate server without 'al' | |
| 689 // parameter. | |
| 690 TEST_F(TranslateManagerBrowserTest, | |
| 691 FetchLanguagesFromTranslateServerWithoutAlpha) { | |
| 692 std::vector<std::string> server_languages; | |
| 693 server_languages.push_back("aa"); | |
| 694 server_languages.push_back("ak"); | |
| 695 server_languages.push_back("ab"); | |
| 696 server_languages.push_back("en-CA"); | |
| 697 server_languages.push_back("zh"); | |
| 698 server_languages.push_back("yi"); | |
| 699 server_languages.push_back("fr-FR"); | |
| 700 server_languages.push_back("xx"); | |
| 701 | |
| 702 std::vector<std::string> alpha_languages; | |
| 703 alpha_languages.push_back("aa"); | |
| 704 alpha_languages.push_back("yi"); | |
| 705 | |
| 706 // call GetSupportedLanguages to call RequestLanguageList internally. | |
| 707 std::vector<std::string> default_supported_languages; | |
| 708 TranslateDownloadManager::GetSupportedLanguages(&default_supported_languages); | |
| 709 | |
| 710 SimulateSupportedLanguagesURLFetch(true, server_languages, | |
| 711 false, alpha_languages); | |
| 712 | |
| 713 std::vector<std::string> current_supported_languages; | |
| 714 TranslateDownloadManager::GetSupportedLanguages(¤t_supported_languages); | |
| 715 | |
| 716 // "xx" can't be displayed in the Translate inforbar, so this is eliminated. | |
| 717 EXPECT_EQ(server_languages.size() - 1, current_supported_languages.size()); | |
| 718 | |
| 719 for (size_t i = 0; i < server_languages.size(); ++i) { | |
| 720 const std::string& lang = server_languages[i]; | |
| 721 if (lang == "xx") | |
| 722 continue; | |
| 723 EXPECT_NE(current_supported_languages.end(), | |
| 724 std::find(current_supported_languages.begin(), | |
| 725 current_supported_languages.end(), | |
| 726 lang)); | |
| 727 EXPECT_FALSE(TranslateDownloadManager::IsAlphaLanguage(lang)); | |
| 728 } | |
| 729 } | |
| 730 | |
| 731 // Tests auto-translate on page. | |
| 732 TEST_F(TranslateManagerBrowserTest, AutoTranslateOnNavigate) { | |
| 733 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); | |
| 734 | |
| 735 // Simulate the user translating. | |
| 736 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); | |
| 737 ASSERT_TRUE(infobar != NULL); | |
| 738 infobar->Translate(); | |
| 739 // Simulate the translate script being retrieved. | |
| 740 SimulateTranslateScriptURLFetch(true); | |
| 741 SimulateOnPageTranslated("fr", "en"); | |
| 742 | |
| 743 // Now navigate to a new page in the same language. | |
| 744 process()->sink().ClearMessages(); | |
| 745 SimulateNavigation(GURL("http://news.google.fr"), "fr", true); | |
| 746 | |
| 747 // This should have automatically triggered a translation. | |
| 748 int page_id = 0; | |
| 749 std::string original_lang, target_lang; | |
| 750 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); | |
| 751 EXPECT_EQ(1, page_id); | |
| 752 EXPECT_EQ("fr", original_lang); | |
| 753 EXPECT_EQ("en", target_lang); | |
| 754 | |
| 755 // Now navigate to a page in a different language. | |
| 756 process()->sink().ClearMessages(); | |
| 757 SimulateNavigation(GURL("http://news.google.es"), "es", true); | |
| 758 | |
| 759 // This should not have triggered a translate. | |
| 760 EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); | |
| 761 } | |
| 762 | |
| 763 // Tests that multiple OnPageContents do not cause multiple infobars. | |
| 764 TEST_F(TranslateManagerBrowserTest, MultipleOnPageContents) { | |
| 765 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); | |
| 766 | |
| 767 // Simulate clicking 'Nope' (don't translate). | |
| 768 EXPECT_TRUE(DenyTranslation()); | |
| 769 EXPECT_EQ(0U, infobar_service()->infobar_count()); | |
| 770 | |
| 771 // Send a new PageContents, we should not show an infobar. | |
| 772 SimulateOnTranslateLanguageDetermined("fr", true); | |
| 773 EXPECT_EQ(0U, infobar_service()->infobar_count()); | |
| 774 | |
| 775 // Do the same steps but simulate closing the infobar this time. | |
| 776 SimulateNavigation(GURL("http://www.youtube.fr"), "fr", true); | |
| 777 EXPECT_TRUE(CloseTranslateInfoBar()); | |
| 778 EXPECT_EQ(0U, infobar_service()->infobar_count()); | |
| 779 SimulateOnTranslateLanguageDetermined("fr", true); | |
| 780 EXPECT_EQ(0U, infobar_service()->infobar_count()); | |
| 781 } | |
| 782 | |
| 783 // Test that reloading the page brings back the infobar if the | |
| 784 // reload succeeded and does not bring it back the reload fails. | |
| 785 TEST_F(TranslateManagerBrowserTest, Reload) { | |
| 786 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); | |
| 787 | |
| 788 EXPECT_TRUE(CloseTranslateInfoBar()); | |
| 789 | |
| 790 // Reload should bring back the infobar if the reload succeeds. | |
| 791 ReloadAndWait(true); | |
| 792 EXPECT_TRUE(GetTranslateInfoBar() != NULL); | |
| 793 EXPECT_TRUE(CloseTranslateInfoBar()); | |
| 794 | |
| 795 // ...But not show it if the reload fails. | |
| 796 ReloadAndWait(false); | |
| 797 EXPECT_TRUE(GetTranslateInfoBar() == NULL); | |
| 798 | |
| 799 // If we set reload attempts to a high value, we will not see the infobar | |
| 800 // immediately. | |
| 801 TranslateManager* manager = | |
| 802 TranslateTabHelper::GetManagerFromWebContents(web_contents()); | |
| 803 manager->set_translate_max_reload_attemps(100); | |
| 804 ReloadAndWait(true); | |
| 805 EXPECT_TRUE(GetTranslateInfoBar() == NULL); | |
| 806 } | |
| 807 | |
| 808 // Test that reloading the page by way of typing again the URL in the | |
| 809 // location bar brings back the infobar. | |
| 810 TEST_F(TranslateManagerBrowserTest, ReloadFromLocationBar) { | |
| 811 GURL url("http://www.google.fr"); | |
| 812 SimulateNavigation(url, "fr", true); | |
| 813 | |
| 814 EXPECT_TRUE(CloseTranslateInfoBar()); | |
| 815 | |
| 816 // Create a pending navigation and simulate a page load. That should be the | |
| 817 // equivalent of typing the URL again in the location bar. | |
| 818 NavEntryCommittedObserver nav_observer(web_contents()); | |
| 819 web_contents()->GetController().LoadURL(url, content::Referrer(), | |
| 820 content::PAGE_TRANSITION_TYPED, | |
| 821 std::string()); | |
| 822 rvh_tester()->SendNavigate(0, url); | |
| 823 | |
| 824 // Test that we are really getting a same page navigation, the test would be | |
| 825 // useless if it was not the case. | |
| 826 const content::LoadCommittedDetails& nav_details = | |
| 827 nav_observer.load_committed_details(); | |
| 828 EXPECT_TRUE(nav_details.entry != NULL); // There was a navigation. | |
| 829 EXPECT_EQ(content::NAVIGATION_TYPE_SAME_PAGE, nav_details.type); | |
| 830 | |
| 831 // The TranslateManager class processes the navigation entry committed | |
| 832 // notification in a posted task; process that task. | |
| 833 base::MessageLoop::current()->RunUntilIdle(); | |
| 834 EXPECT_TRUE(GetTranslateInfoBar() != NULL); | |
| 835 } | |
| 836 | |
| 837 // Tests that a closed translate infobar does not reappear when navigating | |
| 838 // in-page. | |
| 839 TEST_F(TranslateManagerBrowserTest, CloseInfoBarInPageNavigation) { | |
| 840 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); | |
| 841 | |
| 842 EXPECT_TRUE(CloseTranslateInfoBar()); | |
| 843 | |
| 844 // Navigate in page, no infobar should be shown. | |
| 845 SimulateNavigation(GURL("http://www.google.fr/#ref1"), "fr", true); | |
| 846 EXPECT_TRUE(GetTranslateInfoBar() == NULL); | |
| 847 | |
| 848 // Navigate out of page, a new infobar should show. | |
| 849 SimulateNavigation(GURL("http://www.google.fr/foot"), "fr", true); | |
| 850 EXPECT_TRUE(GetTranslateInfoBar() != NULL); | |
| 851 } | |
| 852 | |
| 853 // Tests that a closed translate infobar does not reappear when navigating | |
| 854 // in a subframe. (http://crbug.com/48215) | |
| 855 TEST_F(TranslateManagerBrowserTest, CloseInfoBarInSubframeNavigation) { | |
| 856 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); | |
| 857 | |
| 858 EXPECT_TRUE(CloseTranslateInfoBar()); | |
| 859 | |
| 860 // Simulate a sub-frame auto-navigating. | |
| 861 rvh_tester()->SendNavigateWithTransition( | |
| 862 1, GURL("http://pub.com"), content::PAGE_TRANSITION_AUTO_SUBFRAME); | |
| 863 EXPECT_TRUE(GetTranslateInfoBar() == NULL); | |
| 864 | |
| 865 // Simulate the user navigating in a sub-frame. | |
| 866 rvh_tester()->SendNavigateWithTransition( | |
| 867 2, GURL("http://pub.com"), content::PAGE_TRANSITION_MANUAL_SUBFRAME); | |
| 868 EXPECT_TRUE(GetTranslateInfoBar() == NULL); | |
| 869 | |
| 870 // Navigate out of page, a new infobar should show. | |
| 871 SimulateNavigation(GURL("http://www.google.fr/foot"), "fr", true); | |
| 872 EXPECT_TRUE(GetTranslateInfoBar() != NULL); | |
| 873 } | |
| 874 | |
| 875 // Tests that denying translation is sticky when navigating in page. | |
| 876 TEST_F(TranslateManagerBrowserTest, DenyTranslateInPageNavigation) { | |
| 877 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); | |
| 878 | |
| 879 // Simulate clicking 'Nope' (don't translate). | |
| 880 EXPECT_TRUE(DenyTranslation()); | |
| 881 | |
| 882 // Navigate in page, no infobar should be shown. | |
| 883 SimulateNavigation(GURL("http://www.google.fr/#ref1"), "fr", true); | |
| 884 EXPECT_TRUE(GetTranslateInfoBar() == NULL); | |
| 885 | |
| 886 // Navigate out of page, a new infobar should show. | |
| 887 SimulateNavigation(GURL("http://www.google.fr/foot"), "fr", true); | |
| 888 EXPECT_TRUE(GetTranslateInfoBar() != NULL); | |
| 889 } | |
| 890 | |
| 891 // Tests that after translating and closing the infobar, the infobar does not | |
| 892 // return when navigating in page. | |
| 893 TEST_F(TranslateManagerBrowserTest, TranslateCloseInfoBarInPageNavigation) { | |
| 894 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); | |
| 895 | |
| 896 // Simulate the user translating. | |
| 897 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); | |
| 898 ASSERT_TRUE(infobar != NULL); | |
| 899 infobar->Translate(); | |
| 900 // Simulate the translate script being retrieved. | |
| 901 SimulateTranslateScriptURLFetch(true); | |
| 902 SimulateOnPageTranslated("fr", "en"); | |
| 903 | |
| 904 EXPECT_TRUE(CloseTranslateInfoBar()); | |
| 905 | |
| 906 // Navigate in page, no infobar should be shown. | |
| 907 SimulateNavigation(GURL("http://www.google.fr/#ref1"), "fr", true); | |
| 908 EXPECT_TRUE(GetTranslateInfoBar() == NULL); | |
| 909 | |
| 910 // Navigate out of page, a new infobar should show. | |
| 911 // Note that we navigate to a page in a different language so we don't trigger | |
| 912 // the auto-translate feature (it would translate the page automatically and | |
| 913 // the before translate inforbar would not be shown). | |
| 914 SimulateNavigation(GURL("http://www.google.de"), "de", true); | |
| 915 EXPECT_TRUE(GetTranslateInfoBar() != NULL); | |
| 916 } | |
| 917 | |
| 918 // Tests that the after translate the infobar still shows when navigating | |
| 919 // in-page. | |
| 920 TEST_F(TranslateManagerBrowserTest, TranslateInPageNavigation) { | |
| 921 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); | |
| 922 | |
| 923 // Simulate the user translating. | |
| 924 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); | |
| 925 ASSERT_TRUE(infobar != NULL); | |
| 926 infobar->Translate(); | |
| 927 SimulateTranslateScriptURLFetch(true); | |
| 928 SimulateOnPageTranslated("fr", "en"); | |
| 929 // The after translate infobar is showing. | |
| 930 infobar = GetTranslateInfoBar(); | |
| 931 ASSERT_TRUE(infobar != NULL); | |
| 932 | |
| 933 // Navigate out of page, a new infobar should show. | |
| 934 // See note in TranslateCloseInfoBarInPageNavigation test on why it is | |
| 935 // important to navigate to a page in a different language for this test. | |
| 936 SimulateNavigation(GURL("http://www.google.de"), "de", true); | |
| 937 // The old infobar is gone. | |
| 938 EXPECT_TRUE(CheckInfoBarRemovedAndReset(infobar)); | |
| 939 // And there is a new one. | |
| 940 EXPECT_TRUE(GetTranslateInfoBar() != NULL); | |
| 941 } | |
| 942 | |
| 943 // Tests that no translate infobar is shown when navigating to a page in an | |
| 944 // unsupported language. | |
| 945 TEST_F(TranslateManagerBrowserTest, CLDReportsUnsupportedPageLanguage) { | |
| 946 // Simulate navigating to a page and getting an unsupported language. | |
| 947 SimulateNavigation(GURL("http://www.google.com"), "qbz", true); | |
| 948 | |
| 949 // No info-bar should be shown. | |
| 950 EXPECT_TRUE(GetTranslateInfoBar() == NULL); | |
| 951 } | |
| 952 | |
| 953 // Tests that we deal correctly with unsupported languages returned by the | |
| 954 // server. | |
| 955 // The translation server might return a language we don't support. | |
| 956 TEST_F(TranslateManagerBrowserTest, ServerReportsUnsupportedLanguage) { | |
| 957 SimulateNavigation(GURL("http://mail.google.fr"), "fr", true); | |
| 958 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); | |
| 959 ASSERT_TRUE(infobar != NULL); | |
| 960 process()->sink().ClearMessages(); | |
| 961 infobar->Translate(); | |
| 962 SimulateTranslateScriptURLFetch(true); | |
| 963 // Simulate the render notifying the translation has been done, but it | |
| 964 // reports a language we don't support. | |
| 965 SimulateOnPageTranslated("qbz", "en"); | |
| 966 | |
| 967 // An error infobar should be showing to report that we don't support this | |
| 968 // language. | |
| 969 infobar = GetTranslateInfoBar(); | |
| 970 ASSERT_TRUE(infobar != NULL); | |
| 971 EXPECT_EQ(TranslateTabHelper::TRANSLATE_ERROR, infobar->translate_step()); | |
| 972 | |
| 973 // This infobar should have a button (so the string should not be empty). | |
| 974 ASSERT_FALSE(infobar->GetMessageInfoBarButtonText().empty()); | |
| 975 | |
| 976 // Pressing the button on that infobar should revert to the original language. | |
| 977 process()->sink().ClearMessages(); | |
| 978 infobar->MessageInfoBarButtonPressed(); | |
| 979 const IPC::Message* message = | |
| 980 process()->sink().GetFirstMessageMatching( | |
| 981 ChromeViewMsg_RevertTranslation::ID); | |
| 982 EXPECT_TRUE(message != NULL); | |
| 983 // And it should have removed the infobar. | |
| 984 EXPECT_TRUE(GetTranslateInfoBar() == NULL); | |
| 985 } | |
| 986 | |
| 987 // Tests that no translate infobar is shown and context menu is disabled, when | |
| 988 // Chrome is in a language that the translate server does not support. | |
| 989 TEST_F(TranslateManagerBrowserTest, UnsupportedUILanguage) { | |
| 990 std::string original_lang = g_browser_process->GetApplicationLocale(); | |
| 991 SetApplicationLocale("qbz"); | |
| 992 | |
| 993 // Make sure that the accept language list only contains unsupported languages | |
| 994 Profile* profile = | |
| 995 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); | |
| 996 PrefService* prefs = profile->GetPrefs(); | |
| 997 prefs->SetString(prefs::kAcceptLanguages, "qbz"); | |
| 998 | |
| 999 // Simulate navigating to a page in a language supported by the translate | |
| 1000 // server. | |
| 1001 SimulateNavigation(GURL("http://www.google.com"), "en", true); | |
| 1002 | |
| 1003 // No info-bar should be shown. | |
| 1004 EXPECT_TRUE(GetTranslateInfoBar() == NULL); | |
| 1005 | |
| 1006 // And the context menu option should be disabled too. | |
| 1007 scoped_ptr<TestRenderViewContextMenu> menu( | |
| 1008 TestRenderViewContextMenu::CreateContextMenu(web_contents())); | |
| 1009 menu->Init(); | |
| 1010 EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE)); | |
| 1011 EXPECT_FALSE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE)); | |
| 1012 | |
| 1013 SetApplicationLocale(original_lang); | |
| 1014 } | |
| 1015 | |
| 1016 // Tests that the first supported accept language is selected | |
| 1017 TEST_F(TranslateManagerBrowserTest, TranslateAcceptLanguage) { | |
| 1018 // Set locate to non-existant language | |
| 1019 std::string original_lang = g_browser_process->GetApplicationLocale(); | |
| 1020 SetApplicationLocale("qbz"); | |
| 1021 | |
| 1022 // Set Qbz and French as the only accepted languages | |
| 1023 Profile* profile = | |
| 1024 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); | |
| 1025 PrefService* prefs = profile->GetPrefs(); | |
| 1026 prefs->SetString(prefs::kAcceptLanguages, "qbz,fr"); | |
| 1027 | |
| 1028 // Go to a German page | |
| 1029 SimulateNavigation(GURL("http://google.de"), "de", true); | |
| 1030 | |
| 1031 // Expect the infobar to pop up | |
| 1032 EXPECT_TRUE(GetTranslateInfoBar() != NULL); | |
| 1033 | |
| 1034 // Set Qbz and English-US as the only accepted languages to test the country | |
| 1035 // code removal code which was causing a crash as filed in Issue 90106, | |
| 1036 // a crash caused by a language with a country code that wasn't recognized. | |
| 1037 prefs->SetString(prefs::kAcceptLanguages, "qbz,en-us"); | |
| 1038 | |
| 1039 // Go to a German page | |
| 1040 SimulateNavigation(GURL("http://google.de"), "de", true); | |
| 1041 | |
| 1042 // Expect the infobar to pop up | |
| 1043 EXPECT_TRUE(GetTranslateInfoBar() != NULL); | |
| 1044 } | |
| 1045 | |
| 1046 // Tests that the translate enabled preference is honored. | |
| 1047 TEST_F(TranslateManagerBrowserTest, TranslateEnabledPref) { | |
| 1048 // Make sure the pref allows translate. | |
| 1049 Profile* profile = | |
| 1050 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); | |
| 1051 PrefService* prefs = profile->GetPrefs(); | |
| 1052 prefs->SetBoolean(prefs::kEnableTranslate, true); | |
| 1053 | |
| 1054 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); | |
| 1055 | |
| 1056 // An infobar should be shown. | |
| 1057 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); | |
| 1058 EXPECT_TRUE(infobar != NULL); | |
| 1059 | |
| 1060 // Disable translate. | |
| 1061 prefs->SetBoolean(prefs::kEnableTranslate, false); | |
| 1062 | |
| 1063 // Navigate to a new page, that should close the previous infobar. | |
| 1064 GURL url("http://www.youtube.fr"); | |
| 1065 NavigateAndCommit(url); | |
| 1066 infobar = GetTranslateInfoBar(); | |
| 1067 EXPECT_TRUE(infobar == NULL); | |
| 1068 | |
| 1069 // Simulate getting the page contents and language, that should not trigger | |
| 1070 // a translate infobar. | |
| 1071 SimulateOnTranslateLanguageDetermined("fr", true); | |
| 1072 infobar = GetTranslateInfoBar(); | |
| 1073 EXPECT_TRUE(infobar == NULL); | |
| 1074 } | |
| 1075 | |
| 1076 // Tests the "Never translate <language>" pref. | |
| 1077 TEST_F(TranslateManagerBrowserTest, NeverTranslateLanguagePref) { | |
| 1078 GURL url("http://www.google.fr"); | |
| 1079 SimulateNavigation(url, "fr", true); | |
| 1080 | |
| 1081 // An infobar should be shown. | |
| 1082 EXPECT_TRUE(GetTranslateInfoBar() != NULL); | |
| 1083 | |
| 1084 // Select never translate this language. | |
| 1085 Profile* profile = | |
| 1086 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); | |
| 1087 PrefService* prefs = profile->GetPrefs(); | |
| 1088 PrefChangeRegistrar registrar; | |
| 1089 registrar.Init(prefs); | |
| 1090 registrar.Add(TranslatePrefs::kPrefTranslateBlockedLanguages, | |
| 1091 pref_callback_); | |
| 1092 scoped_ptr<TranslatePrefs> translate_prefs( | |
| 1093 TranslateTabHelper::CreateTranslatePrefs(prefs)); | |
| 1094 EXPECT_FALSE(translate_prefs->IsBlockedLanguage("fr")); | |
| 1095 TranslateAcceptLanguages* accept_languages = | |
| 1096 TranslateTabHelper::GetTranslateAcceptLanguages(profile); | |
| 1097 EXPECT_TRUE(translate_prefs->CanTranslateLanguage(accept_languages, "fr")); | |
| 1098 SetPrefObserverExpectation(TranslatePrefs::kPrefTranslateBlockedLanguages); | |
| 1099 translate_prefs->BlockLanguage("fr"); | |
| 1100 EXPECT_TRUE(translate_prefs->IsBlockedLanguage("fr")); | |
| 1101 EXPECT_FALSE(translate_prefs->IsSiteBlacklisted(url.host())); | |
| 1102 EXPECT_FALSE(translate_prefs->CanTranslateLanguage(accept_languages, "fr")); | |
| 1103 | |
| 1104 EXPECT_TRUE(CloseTranslateInfoBar()); | |
| 1105 | |
| 1106 // Navigate to a new page also in French. | |
| 1107 SimulateNavigation(GURL("http://wwww.youtube.fr"), "fr", true); | |
| 1108 | |
| 1109 // There should not be a translate infobar. | |
| 1110 EXPECT_TRUE(GetTranslateInfoBar() == NULL); | |
| 1111 | |
| 1112 // Remove the language from the blacklist. | |
| 1113 SetPrefObserverExpectation(TranslatePrefs::kPrefTranslateBlockedLanguages); | |
| 1114 translate_prefs->UnblockLanguage("fr"); | |
| 1115 EXPECT_FALSE(translate_prefs->IsBlockedLanguage("fr")); | |
| 1116 EXPECT_FALSE(translate_prefs->IsSiteBlacklisted(url.host())); | |
| 1117 EXPECT_TRUE(translate_prefs->CanTranslateLanguage(accept_languages, "fr")); | |
| 1118 | |
| 1119 // Navigate to a page in French. | |
| 1120 SimulateNavigation(url, "fr", true); | |
| 1121 | |
| 1122 // There should be a translate infobar. | |
| 1123 EXPECT_TRUE(GetTranslateInfoBar() != NULL); | |
| 1124 } | |
| 1125 | |
| 1126 // Tests the "Never translate this site" pref. | |
| 1127 TEST_F(TranslateManagerBrowserTest, NeverTranslateSitePref) { | |
| 1128 GURL url("http://www.google.fr"); | |
| 1129 std::string host(url.host()); | |
| 1130 SimulateNavigation(url, "fr", true); | |
| 1131 | |
| 1132 // An infobar should be shown. | |
| 1133 EXPECT_TRUE(GetTranslateInfoBar() != NULL); | |
| 1134 | |
| 1135 // Select never translate this site. | |
| 1136 Profile* profile = | |
| 1137 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); | |
| 1138 PrefService* prefs = profile->GetPrefs(); | |
| 1139 PrefChangeRegistrar registrar; | |
| 1140 registrar.Init(prefs); | |
| 1141 registrar.Add(TranslatePrefs::kPrefTranslateSiteBlacklist, pref_callback_); | |
| 1142 scoped_ptr<TranslatePrefs> translate_prefs( | |
| 1143 TranslateTabHelper::CreateTranslatePrefs(prefs)); | |
| 1144 EXPECT_FALSE(translate_prefs->IsSiteBlacklisted(host)); | |
| 1145 TranslateAcceptLanguages* accept_languages = | |
| 1146 TranslateTabHelper::GetTranslateAcceptLanguages(profile); | |
| 1147 EXPECT_TRUE(translate_prefs->CanTranslateLanguage(accept_languages, "fr")); | |
| 1148 SetPrefObserverExpectation(TranslatePrefs::kPrefTranslateSiteBlacklist); | |
| 1149 translate_prefs->BlacklistSite(host); | |
| 1150 EXPECT_TRUE(translate_prefs->IsSiteBlacklisted(host)); | |
| 1151 EXPECT_TRUE(translate_prefs->CanTranslateLanguage(accept_languages, "fr")); | |
| 1152 | |
| 1153 EXPECT_TRUE(CloseTranslateInfoBar()); | |
| 1154 | |
| 1155 // Navigate to a new page also on the same site. | |
| 1156 SimulateNavigation(GURL("http://www.google.fr/hello"), "fr", true); | |
| 1157 | |
| 1158 // There should not be a translate infobar. | |
| 1159 EXPECT_TRUE(GetTranslateInfoBar() == NULL); | |
| 1160 | |
| 1161 // Remove the site from the blacklist. | |
| 1162 SetPrefObserverExpectation(TranslatePrefs::kPrefTranslateSiteBlacklist); | |
| 1163 translate_prefs->RemoveSiteFromBlacklist(host); | |
| 1164 EXPECT_FALSE(translate_prefs->IsSiteBlacklisted(host)); | |
| 1165 EXPECT_TRUE(translate_prefs->CanTranslateLanguage(accept_languages, "fr")); | |
| 1166 | |
| 1167 // Navigate to a page in French. | |
| 1168 SimulateNavigation(url, "fr", true); | |
| 1169 | |
| 1170 // There should be a translate infobar. | |
| 1171 EXPECT_TRUE(GetTranslateInfoBar() != NULL); | |
| 1172 } | |
| 1173 | |
| 1174 // Tests the "Always translate this language" pref. | |
| 1175 TEST_F(TranslateManagerBrowserTest, AlwaysTranslateLanguagePref) { | |
| 1176 // Select always translate French to English. | |
| 1177 Profile* profile = | |
| 1178 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); | |
| 1179 PrefService* prefs = profile->GetPrefs(); | |
| 1180 PrefChangeRegistrar registrar; | |
| 1181 registrar.Init(prefs); | |
| 1182 registrar.Add(TranslatePrefs::kPrefTranslateWhitelists, pref_callback_); | |
| 1183 scoped_ptr<TranslatePrefs> translate_prefs( | |
| 1184 TranslateTabHelper::CreateTranslatePrefs(prefs)); | |
| 1185 SetPrefObserverExpectation(TranslatePrefs::kPrefTranslateWhitelists); | |
| 1186 translate_prefs->WhitelistLanguagePair("fr", "en"); | |
| 1187 | |
| 1188 // Load a page in French. | |
| 1189 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); | |
| 1190 | |
| 1191 // It should have triggered an automatic translation to English. | |
| 1192 | |
| 1193 // The translating infobar should be showing. | |
| 1194 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); | |
| 1195 ASSERT_TRUE(infobar != NULL); | |
| 1196 EXPECT_EQ(TranslateTabHelper::TRANSLATING, infobar->translate_step()); | |
| 1197 SimulateTranslateScriptURLFetch(true); | |
| 1198 int page_id = 0; | |
| 1199 std::string original_lang, target_lang; | |
| 1200 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); | |
| 1201 EXPECT_EQ("fr", original_lang); | |
| 1202 EXPECT_EQ("en", target_lang); | |
| 1203 process()->sink().ClearMessages(); | |
| 1204 | |
| 1205 // Try another language, it should not be autotranslated. | |
| 1206 SimulateNavigation(GURL("http://www.google.es"), "es", true); | |
| 1207 EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); | |
| 1208 EXPECT_TRUE(GetTranslateInfoBar() != NULL); | |
| 1209 EXPECT_TRUE(CloseTranslateInfoBar()); | |
| 1210 | |
| 1211 // Let's switch to incognito mode, it should not be autotranslated in that | |
| 1212 // case either. | |
| 1213 TestingProfile* test_profile = | |
| 1214 static_cast<TestingProfile*>(web_contents()->GetBrowserContext()); | |
| 1215 test_profile->ForceIncognito(true); | |
| 1216 SimulateNavigation(GURL("http://www.youtube.fr"), "fr", true); | |
| 1217 EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); | |
| 1218 EXPECT_TRUE(GetTranslateInfoBar() != NULL); | |
| 1219 EXPECT_TRUE(CloseTranslateInfoBar()); | |
| 1220 test_profile->ForceIncognito(false); // Get back to non incognito. | |
| 1221 | |
| 1222 // Now revert the always translate pref and make sure we go back to expected | |
| 1223 // behavior, which is show a "before translate" infobar. | |
| 1224 SetPrefObserverExpectation(TranslatePrefs::kPrefTranslateWhitelists); | |
| 1225 translate_prefs->RemoveLanguagePairFromWhitelist("fr", "en"); | |
| 1226 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); | |
| 1227 EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); | |
| 1228 infobar = GetTranslateInfoBar(); | |
| 1229 ASSERT_TRUE(infobar != NULL); | |
| 1230 EXPECT_EQ(TranslateTabHelper::BEFORE_TRANSLATE, infobar->translate_step()); | |
| 1231 } | |
| 1232 | |
| 1233 // Context menu. | |
| 1234 TEST_F(TranslateManagerBrowserTest, ContextMenu) { | |
| 1235 // Blacklist www.google.fr and French for translation. | |
| 1236 GURL url("http://www.google.fr"); | |
| 1237 Profile* profile = | |
| 1238 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); | |
| 1239 scoped_ptr<TranslatePrefs> translate_prefs( | |
| 1240 TranslateTabHelper::CreateTranslatePrefs(profile->GetPrefs())); | |
| 1241 translate_prefs->BlockLanguage("fr"); | |
| 1242 translate_prefs->BlacklistSite(url.host()); | |
| 1243 EXPECT_TRUE(translate_prefs->IsBlockedLanguage("fr")); | |
| 1244 EXPECT_TRUE(translate_prefs->IsSiteBlacklisted(url.host())); | |
| 1245 | |
| 1246 // Simulate navigating to a page in French. The translate menu should show but | |
| 1247 // should only be enabled when the page language has been received. | |
| 1248 NavigateAndCommit(url); | |
| 1249 scoped_ptr<TestRenderViewContextMenu> menu( | |
| 1250 TestRenderViewContextMenu::CreateContextMenu(web_contents())); | |
| 1251 menu->Init(); | |
| 1252 EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE)); | |
| 1253 EXPECT_FALSE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE)); | |
| 1254 | |
| 1255 // Simulate receiving the language. | |
| 1256 SimulateOnTranslateLanguageDetermined("fr", true); | |
| 1257 menu.reset(TestRenderViewContextMenu::CreateContextMenu(web_contents())); | |
| 1258 menu->Init(); | |
| 1259 EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE)); | |
| 1260 EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE)); | |
| 1261 | |
| 1262 // Use the menu to translate the page. | |
| 1263 menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE, 0); | |
| 1264 | |
| 1265 // That should have triggered a translation. | |
| 1266 // The "translating..." infobar should be showing. | |
| 1267 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); | |
| 1268 ASSERT_TRUE(infobar != NULL); | |
| 1269 EXPECT_EQ(TranslateTabHelper::TRANSLATING, infobar->translate_step()); | |
| 1270 SimulateTranslateScriptURLFetch(true); | |
| 1271 int page_id = 0; | |
| 1272 std::string original_lang, target_lang; | |
| 1273 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); | |
| 1274 EXPECT_EQ("fr", original_lang); | |
| 1275 EXPECT_EQ("en", target_lang); | |
| 1276 process()->sink().ClearMessages(); | |
| 1277 | |
| 1278 // This should also have reverted the blacklisting of this site and language. | |
| 1279 EXPECT_FALSE(translate_prefs->IsBlockedLanguage("fr")); | |
| 1280 EXPECT_FALSE(translate_prefs->IsSiteBlacklisted(url.host())); | |
| 1281 | |
| 1282 // Let's simulate the page being translated. | |
| 1283 SimulateOnPageTranslated("fr", "en"); | |
| 1284 | |
| 1285 // The translate menu should now be disabled. | |
| 1286 menu.reset(TestRenderViewContextMenu::CreateContextMenu(web_contents())); | |
| 1287 menu->Init(); | |
| 1288 EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE)); | |
| 1289 EXPECT_FALSE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE)); | |
| 1290 | |
| 1291 // Test that selecting translate in the context menu WHILE the page is being | |
| 1292 // translated does nothing (this could happen if autotranslate kicks-in and | |
| 1293 // the user selects the menu while the translation is being performed). | |
| 1294 SimulateNavigation(GURL("http://www.google.es"), "es", true); | |
| 1295 infobar = GetTranslateInfoBar(); | |
| 1296 ASSERT_TRUE(infobar != NULL); | |
| 1297 infobar->Translate(); | |
| 1298 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); | |
| 1299 process()->sink().ClearMessages(); | |
| 1300 menu.reset(TestRenderViewContextMenu::CreateContextMenu(web_contents())); | |
| 1301 menu->Init(); | |
| 1302 EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE)); | |
| 1303 menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE, 0); | |
| 1304 // No message expected since the translation should have been ignored. | |
| 1305 EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); | |
| 1306 | |
| 1307 // Now test that selecting translate in the context menu AFTER the page has | |
| 1308 // been translated does nothing. | |
| 1309 SimulateNavigation(GURL("http://www.google.de"), "de", true); | |
| 1310 infobar = GetTranslateInfoBar(); | |
| 1311 ASSERT_TRUE(infobar != NULL); | |
| 1312 infobar->Translate(); | |
| 1313 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); | |
| 1314 process()->sink().ClearMessages(); | |
| 1315 menu.reset(TestRenderViewContextMenu::CreateContextMenu(web_contents())); | |
| 1316 menu->Init(); | |
| 1317 EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE)); | |
| 1318 SimulateOnPageTranslated("de", "en"); | |
| 1319 menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE, 0); | |
| 1320 // No message expected since the translation should have been ignored. | |
| 1321 EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); | |
| 1322 | |
| 1323 // Test that the translate context menu is enabled when the page is in an | |
| 1324 // unknown language. | |
| 1325 SimulateNavigation(url, "und", true); | |
| 1326 menu.reset(TestRenderViewContextMenu::CreateContextMenu(web_contents())); | |
| 1327 menu->Init(); | |
| 1328 EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE)); | |
| 1329 EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE)); | |
| 1330 | |
| 1331 // Test that the translate context menu is enabled even if the page is in an | |
| 1332 // unsupported language. | |
| 1333 SimulateNavigation(url, "qbz", true); | |
| 1334 menu.reset(TestRenderViewContextMenu::CreateContextMenu(web_contents())); | |
| 1335 menu->Init(); | |
| 1336 EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE)); | |
| 1337 EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE)); | |
| 1338 } | |
| 1339 | |
| 1340 // Tests that an extra always/never translate button is shown on the "before | |
| 1341 // translate" infobar when the translation is accepted/declined 3 times, | |
| 1342 // only when not in incognito mode. | |
| 1343 TEST_F(TranslateManagerBrowserTest, BeforeTranslateExtraButtons) { | |
| 1344 Profile* profile = | |
| 1345 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); | |
| 1346 scoped_ptr<TranslatePrefs> translate_prefs( | |
| 1347 TranslateTabHelper::CreateTranslatePrefs(profile->GetPrefs())); | |
| 1348 translate_prefs->ResetTranslationAcceptedCount("fr"); | |
| 1349 translate_prefs->ResetTranslationDeniedCount("fr"); | |
| 1350 translate_prefs->ResetTranslationAcceptedCount("de"); | |
| 1351 translate_prefs->ResetTranslationDeniedCount("de"); | |
| 1352 | |
| 1353 // We'll do 4 times in incognito mode first to make sure the button is not | |
| 1354 // shown in that case, then 4 times in normal mode. | |
| 1355 TranslateInfoBarDelegate* infobar; | |
| 1356 TestingProfile* test_profile = | |
| 1357 static_cast<TestingProfile*>(web_contents()->GetBrowserContext()); | |
| 1358 static_cast<extensions::TestExtensionSystem*>( | |
| 1359 extensions::ExtensionSystem::Get(test_profile))-> | |
| 1360 CreateProcessManager(); | |
| 1361 test_profile->ForceIncognito(true); | |
| 1362 for (int i = 0; i < 8; ++i) { | |
| 1363 SCOPED_TRACE(::testing::Message() << "Iteration " << i << | |
| 1364 " incognito mode=" << test_profile->IsOffTheRecord()); | |
| 1365 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); | |
| 1366 infobar = GetTranslateInfoBar(); | |
| 1367 ASSERT_TRUE(infobar != NULL); | |
| 1368 EXPECT_EQ(TranslateTabHelper::BEFORE_TRANSLATE, infobar->translate_step()); | |
| 1369 if (i < 7) { | |
| 1370 EXPECT_FALSE(infobar->ShouldShowAlwaysTranslateShortcut()); | |
| 1371 infobar->Translate(); | |
| 1372 process()->sink().ClearMessages(); | |
| 1373 } else { | |
| 1374 EXPECT_TRUE(infobar->ShouldShowAlwaysTranslateShortcut()); | |
| 1375 } | |
| 1376 if (i == 3) | |
| 1377 test_profile->ForceIncognito(false); | |
| 1378 } | |
| 1379 // Simulate the user pressing "Always translate French". | |
| 1380 infobar->AlwaysTranslatePageLanguage(); | |
| 1381 EXPECT_TRUE(translate_prefs->IsLanguagePairWhitelisted("fr", "en")); | |
| 1382 // Simulate the translate script being retrieved (it only needs to be done | |
| 1383 // once in the test as it is cached). | |
| 1384 SimulateTranslateScriptURLFetch(true); | |
| 1385 // That should have triggered a page translate. | |
| 1386 int page_id = 0; | |
| 1387 std::string original_lang, target_lang; | |
| 1388 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); | |
| 1389 process()->sink().ClearMessages(); | |
| 1390 | |
| 1391 // Now test that declining the translation causes a "never translate" button | |
| 1392 // to be shown (in non incognito mode only). | |
| 1393 test_profile->ForceIncognito(true); | |
| 1394 for (int i = 0; i < 8; ++i) { | |
| 1395 SCOPED_TRACE(::testing::Message() << "Iteration " << i << | |
| 1396 " incognito mode=" << test_profile->IsOffTheRecord()); | |
| 1397 SimulateNavigation(GURL("http://www.google.de"), "de", true); | |
| 1398 infobar = GetTranslateInfoBar(); | |
| 1399 ASSERT_TRUE(infobar != NULL); | |
| 1400 EXPECT_EQ(TranslateTabHelper::BEFORE_TRANSLATE, infobar->translate_step()); | |
| 1401 if (i < 7) { | |
| 1402 EXPECT_FALSE(infobar->ShouldShowNeverTranslateShortcut()); | |
| 1403 infobar->TranslationDeclined(); | |
| 1404 } else { | |
| 1405 EXPECT_TRUE(infobar->ShouldShowNeverTranslateShortcut()); | |
| 1406 } | |
| 1407 if (i == 3) | |
| 1408 test_profile->ForceIncognito(false); | |
| 1409 } | |
| 1410 // Simulate the user pressing "Never translate French". | |
| 1411 infobar->NeverTranslatePageLanguage(); | |
| 1412 EXPECT_TRUE(translate_prefs->IsBlockedLanguage("de")); | |
| 1413 // No translation should have occured and the infobar should be gone. | |
| 1414 EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); | |
| 1415 process()->sink().ClearMessages(); | |
| 1416 ASSERT_TRUE(GetTranslateInfoBar() == NULL); | |
| 1417 } | |
| 1418 | |
| 1419 // Tests that we don't show a translate infobar when a page instructs that it | |
| 1420 // should not be translated. | |
| 1421 TEST_F(TranslateManagerBrowserTest, NonTranslatablePage) { | |
| 1422 SimulateNavigation(GURL("http://mail.google.fr"), "fr", false); | |
| 1423 | |
| 1424 // We should not have an infobar. | |
| 1425 EXPECT_TRUE(GetTranslateInfoBar() == NULL); | |
| 1426 | |
| 1427 // The context menu is enabled to allow users to force translation. | |
| 1428 scoped_ptr<TestRenderViewContextMenu> menu( | |
| 1429 TestRenderViewContextMenu::CreateContextMenu(web_contents())); | |
| 1430 menu->Init(); | |
| 1431 EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE)); | |
| 1432 EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE)); | |
| 1433 } | |
| 1434 | |
| 1435 // Tests that the script is expired and refetched as expected. | |
| 1436 TEST_F(TranslateManagerBrowserTest, ScriptExpires) { | |
| 1437 ExpireTranslateScriptImmediately(); | |
| 1438 | |
| 1439 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); | |
| 1440 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); | |
| 1441 ASSERT_TRUE(infobar != NULL); | |
| 1442 process()->sink().ClearMessages(); | |
| 1443 infobar->Translate(); | |
| 1444 SimulateTranslateScriptURLFetch(true); | |
| 1445 SimulateOnPageTranslated("fr", "en"); | |
| 1446 | |
| 1447 // A task should have been posted to clear the script, run it. | |
| 1448 base::MessageLoop::current()->RunUntilIdle(); | |
| 1449 | |
| 1450 // Do another navigation and translation. | |
| 1451 SimulateNavigation(GURL("http://www.google.es"), "es", true); | |
| 1452 infobar = GetTranslateInfoBar(); | |
| 1453 ASSERT_TRUE(infobar != NULL); | |
| 1454 process()->sink().ClearMessages(); | |
| 1455 infobar->Translate(); | |
| 1456 // If we don't simulate the URL fetch, the TranslateManager should be waiting | |
| 1457 // for the script and no message should have been sent to the renderer. | |
| 1458 EXPECT_TRUE( | |
| 1459 process()->sink().GetFirstMessageMatching( | |
| 1460 ChromeViewMsg_TranslatePage::ID) == NULL); | |
| 1461 // Now simulate the URL fetch. | |
| 1462 SimulateTranslateScriptURLFetch(true); | |
| 1463 // Now the message should have been sent. | |
| 1464 int page_id = 0; | |
| 1465 std::string original_lang, target_lang; | |
| 1466 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); | |
| 1467 EXPECT_EQ("es", original_lang); | |
| 1468 EXPECT_EQ("en", target_lang); | |
| 1469 } | |
| 1470 | |
| 1471 TEST_F(TranslateManagerBrowserTest, DownloadsAndHistoryNotTranslated) { | |
| 1472 ASSERT_FALSE(TranslateManager::IsTranslatableURL( | |
| 1473 GURL(chrome::kChromeUIDownloadsURL))); | |
| 1474 ASSERT_FALSE(TranslateManager::IsTranslatableURL( | |
| 1475 GURL(chrome::kChromeUIHistoryURL))); | |
| 1476 } | |
| 1477 | |
| 1478 #if defined(USE_AURA) | |
| 1479 | |
| 1480 TEST_F(TranslateManagerBrowserTest, BubbleNormalTranslate) { | |
| 1481 // Prepare for the bubble | |
| 1482 TranslateService::SetUseInfobar(false); | |
| 1483 MockTranslateBubbleFactory* factory = new MockTranslateBubbleFactory; | |
| 1484 scoped_ptr<TranslateBubbleFactory> factory_ptr(factory); | |
| 1485 TranslateBubbleFactory::SetFactory(factory); | |
| 1486 | |
| 1487 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); | |
| 1488 | |
| 1489 // Check the bubble exists instead of the infobar. | |
| 1490 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); | |
| 1491 ASSERT_TRUE(infobar == NULL); | |
| 1492 TranslateBubbleModel* bubble = factory->model(); | |
| 1493 ASSERT_TRUE(bubble != NULL); | |
| 1494 EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_BEFORE_TRANSLATE, | |
| 1495 bubble->GetViewState()); | |
| 1496 | |
| 1497 // Simulate clicking translate. | |
| 1498 process()->sink().ClearMessages(); | |
| 1499 bubble->Translate(); | |
| 1500 | |
| 1501 // Check the bubble shows "Translating...". | |
| 1502 bubble = factory->model(); | |
| 1503 ASSERT_TRUE(bubble != NULL); | |
| 1504 EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_TRANSLATING, | |
| 1505 bubble->GetViewState()); | |
| 1506 | |
| 1507 // Simulate the translate script being retrieved (it only needs to be done | |
| 1508 // once in the test as it is cached). | |
| 1509 SimulateTranslateScriptURLFetch(true); | |
| 1510 | |
| 1511 // Simulate the render notifying the translation has been done. | |
| 1512 SimulateOnPageTranslated("fr", "en"); | |
| 1513 | |
| 1514 // Check the bubble shows "Translated." | |
| 1515 bubble = factory->model(); | |
| 1516 ASSERT_TRUE(bubble != NULL); | |
| 1517 EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_AFTER_TRANSLATE, | |
| 1518 bubble->GetViewState()); | |
| 1519 } | |
| 1520 | |
| 1521 TEST_F(TranslateManagerBrowserTest, BubbleTranslateScriptNotAvailable) { | |
| 1522 // Prepare for the bubble | |
| 1523 TranslateService::SetUseInfobar(false); | |
| 1524 MockTranslateBubbleFactory* factory = new MockTranslateBubbleFactory; | |
| 1525 scoped_ptr<TranslateBubbleFactory> factory_ptr(factory); | |
| 1526 TranslateBubbleFactory::SetFactory(factory); | |
| 1527 | |
| 1528 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); | |
| 1529 | |
| 1530 // Check the bubble exists instead of the infobar. | |
| 1531 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); | |
| 1532 ASSERT_TRUE(infobar == NULL); | |
| 1533 TranslateBubbleModel* bubble = factory->model(); | |
| 1534 ASSERT_TRUE(bubble != NULL); | |
| 1535 EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_BEFORE_TRANSLATE, | |
| 1536 bubble->GetViewState()); | |
| 1537 | |
| 1538 // Simulate clicking translate. | |
| 1539 process()->sink().ClearMessages(); | |
| 1540 bubble->Translate(); | |
| 1541 SimulateTranslateScriptURLFetch(false); | |
| 1542 | |
| 1543 // We should not have sent any message to translate to the renderer. | |
| 1544 EXPECT_FALSE(GetTranslateMessage(NULL, NULL, NULL)); | |
| 1545 | |
| 1546 // And we should have an error infobar showing. | |
| 1547 bubble = factory->model(); | |
| 1548 ASSERT_TRUE(bubble != NULL); | |
| 1549 EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_ERROR, | |
| 1550 bubble->GetViewState()); | |
| 1551 } | |
| 1552 | |
| 1553 TEST_F(TranslateManagerBrowserTest, BubbleUnknownLanguage) { | |
| 1554 // Prepare for the bubble | |
| 1555 TranslateService::SetUseInfobar(false); | |
| 1556 MockTranslateBubbleFactory* factory = new MockTranslateBubbleFactory; | |
| 1557 scoped_ptr<TranslateBubbleFactory> factory_ptr(factory); | |
| 1558 TranslateBubbleFactory::SetFactory(factory); | |
| 1559 | |
| 1560 // Simulate navigating to a page ("und" is the string returned by the CLD for | |
| 1561 // languages it does not recognize). | |
| 1562 SimulateNavigation(GURL("http://www.google.mys"), "und", true); | |
| 1563 | |
| 1564 // We should not have a bubble as we don't know the language. | |
| 1565 ASSERT_TRUE(factory->model() == NULL); | |
| 1566 | |
| 1567 // Translate the page anyway throught the context menu. | |
| 1568 scoped_ptr<TestRenderViewContextMenu> menu( | |
| 1569 TestRenderViewContextMenu::CreateContextMenu(web_contents())); | |
| 1570 menu->Init(); | |
| 1571 menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE, 0); | |
| 1572 | |
| 1573 // Check the bubble exists instead of the infobar. | |
| 1574 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); | |
| 1575 ASSERT_TRUE(infobar == NULL); | |
| 1576 TranslateBubbleModel* bubble = factory->model(); | |
| 1577 ASSERT_TRUE(bubble != NULL); | |
| 1578 EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_TRANSLATING, | |
| 1579 bubble->GetViewState()); | |
| 1580 } | |
| 1581 | |
| 1582 #endif // defined(USE_AURA) | |
| 1583 | 18 |
| 1584 // Test is flaky on Win http://crbug.com/166334 | 19 // Test is flaky on Win http://crbug.com/166334 |
| 1585 #if defined(OS_WIN) | 20 #if defined(OS_WIN) |
| 1586 #define MAYBE_PRE_TranslateSessionRestore DISABLED_PRE_TranslateSessionRestore | 21 #define MAYBE_PRE_TranslateSessionRestore DISABLED_PRE_TranslateSessionRestore |
| 1587 #else | 22 #else |
| 1588 #define MAYBE_PRE_TranslateSessionRestore PRE_TranslateSessionRestore | 23 #define MAYBE_PRE_TranslateSessionRestore PRE_TranslateSessionRestore |
| 1589 #endif | 24 #endif |
| 1590 // Test that session restore restores the translate infobar and other translate | 25 // Test that session restore restores the translate infobar and other translate |
| 1591 // settings. | 26 // settings. |
| 1592 IN_PROC_BROWSER_TEST_F(InProcessBrowserTest, | 27 IN_PROC_BROWSER_TEST_F(TranslateManagerBrowserTest, |
| 1593 MAYBE_PRE_TranslateSessionRestore) { | 28 MAYBE_PRE_TranslateSessionRestore) { |
| 1594 SessionStartupPref pref(SessionStartupPref::LAST); | 29 SessionStartupPref pref(SessionStartupPref::LAST); |
| 1595 SessionStartupPref::SetStartupPref(browser()->profile(), pref); | 30 SessionStartupPref::SetStartupPref(browser()->profile(), pref); |
| 1596 | 31 |
| 1597 content::WebContents* current_web_contents = | 32 content::WebContents* current_web_contents = |
| 1598 browser()->tab_strip_model()->GetActiveWebContents(); | 33 browser()->tab_strip_model()->GetActiveWebContents(); |
| 1599 TranslateTabHelper* translate_tab_helper = | 34 TranslateTabHelper* translate_tab_helper = |
| 1600 TranslateTabHelper::FromWebContents(current_web_contents); | 35 TranslateTabHelper::FromWebContents(current_web_contents); |
| 1601 content::Source<content::WebContents> source(current_web_contents); | 36 content::Source<content::WebContents> source(current_web_contents); |
| 1602 | 37 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 1614 source.map_key(), &details)); | 49 source.map_key(), &details)); |
| 1615 EXPECT_EQ("fr", details.adopted_language); | 50 EXPECT_EQ("fr", details.adopted_language); |
| 1616 EXPECT_EQ("fr", translate_tab_helper->GetLanguageState().original_language()); | 51 EXPECT_EQ("fr", translate_tab_helper->GetLanguageState().original_language()); |
| 1617 } | 52 } |
| 1618 | 53 |
| 1619 #if defined (OS_WIN) | 54 #if defined (OS_WIN) |
| 1620 #define MAYBE_TranslateSessionRestore DISABLED_TranslateSessionRestore | 55 #define MAYBE_TranslateSessionRestore DISABLED_TranslateSessionRestore |
| 1621 #else | 56 #else |
| 1622 #define MAYBE_TranslateSessionRestore TranslateSessionRestore | 57 #define MAYBE_TranslateSessionRestore TranslateSessionRestore |
| 1623 #endif | 58 #endif |
| 1624 IN_PROC_BROWSER_TEST_F(InProcessBrowserTest, MAYBE_TranslateSessionRestore) { | 59 IN_PROC_BROWSER_TEST_F(TranslateManagerBrowserTest, |
| 60 MAYBE_TranslateSessionRestore) { |
| 1625 content::WebContents* current_web_contents = | 61 content::WebContents* current_web_contents = |
| 1626 browser()->tab_strip_model()->GetActiveWebContents(); | 62 browser()->tab_strip_model()->GetActiveWebContents(); |
| 1627 content::Source<content::WebContents> source(current_web_contents); | 63 content::Source<content::WebContents> source(current_web_contents); |
| 1628 | 64 |
| 1629 ui_test_utils::WindowedNotificationObserverWithDetails< | 65 ui_test_utils::WindowedNotificationObserverWithDetails< |
| 1630 LanguageDetectionDetails> | 66 LanguageDetectionDetails> |
| 1631 fr_language_detected_signal(chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED, | 67 fr_language_detected_signal(chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED, |
| 1632 source); | 68 source); |
| 1633 fr_language_detected_signal.Wait(); | 69 fr_language_detected_signal.Wait(); |
| 1634 } | 70 } |
| OLD | NEW |