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