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