OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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 "chrome/browser/renderer_host/test/test_render_view_host.h" |
| 6 |
| 7 #include "chrome/app/chrome_dll_resource.h" |
| 8 #include "chrome/browser/renderer_host/mock_render_process_host.h" |
| 9 #include "chrome/browser/tab_contents/render_view_context_menu.h" |
| 10 #include "chrome/browser/translate/translate_infobar_delegate2.h" |
| 11 #include "chrome/browser/translate/translate_manager2.h" |
| 12 #include "chrome/browser/translate/translate_prefs.h" |
| 13 #include "chrome/common/ipc_test_sink.h" |
| 14 #include "chrome/common/notification_details.h" |
| 15 #include "chrome/common/notification_observer_mock.h" |
| 16 #include "chrome/common/notification_registrar.h" |
| 17 #include "chrome/common/notification_service.h" |
| 18 #include "chrome/common/notification_type.h" |
| 19 #include "chrome/common/pref_names.h" |
| 20 #include "chrome/common/render_messages.h" |
| 21 #include "chrome/common/net/test_url_fetcher_factory.h" |
| 22 #include "chrome/test/testing_browser_process.h" |
| 23 #include "grit/generated_resources.h" |
| 24 #include "testing/gmock/include/gmock/gmock.h" |
| 25 #include "third_party/cld/languages/public/languages.h" |
| 26 |
| 27 using testing::_; |
| 28 using testing::Pointee; |
| 29 using testing::Property; |
| 30 |
| 31 class TestTranslateManager2 : public TranslateManager2 { |
| 32 public: |
| 33 TestTranslateManager2() {} |
| 34 }; |
| 35 |
| 36 class TranslateManager2Test : public RenderViewHostTestHarness, |
| 37 public NotificationObserver { |
| 38 public: |
| 39 TranslateManager2Test() {} |
| 40 |
| 41 // Simluates navigating to a page and getting the page contents and language |
| 42 // for that navigation. |
| 43 void SimulateNavigation(const GURL& url, int page_id, |
| 44 const std::wstring& contents, |
| 45 const std::string& lang) { |
| 46 NavigateAndCommit(url); |
| 47 SimulateOnPageContents(url, page_id, contents, lang); |
| 48 } |
| 49 |
| 50 void SimulateOnPageContents(const GURL& url, int page_id, |
| 51 const std::wstring& contents, |
| 52 const std::string& lang) { |
| 53 rvh()->TestOnMessageReceived(ViewHostMsg_PageContents(0, url, page_id, |
| 54 contents, lang)); |
| 55 } |
| 56 |
| 57 bool GetTranslateMessage(int* page_id, |
| 58 std::string* original_lang, |
| 59 std::string* target_lang) { |
| 60 const IPC::Message* message = |
| 61 process()->sink().GetFirstMessageMatching(ViewMsg_TranslatePage::ID); |
| 62 if (!message) |
| 63 return false; |
| 64 Tuple4<int, std::string, std::string, std::string> translate_param; |
| 65 ViewMsg_TranslatePage::Read(message, &translate_param); |
| 66 if (page_id) |
| 67 *page_id = translate_param.a; |
| 68 // Ignore translate_param.b which is the script injected in the page. |
| 69 if (original_lang) |
| 70 *original_lang = translate_param.c; |
| 71 if (target_lang) |
| 72 *target_lang = translate_param.d; |
| 73 return true; |
| 74 } |
| 75 |
| 76 // Returns the translate infobar if there is 1 infobar and it is a translate |
| 77 // infobar. |
| 78 TranslateInfoBarDelegate2* GetTranslateInfoBar() { |
| 79 if (contents()->infobar_delegate_count() != 1) |
| 80 return NULL; |
| 81 return contents()->GetInfoBarDelegateAt(0)->AsTranslateInfoBarDelegate2(); |
| 82 } |
| 83 |
| 84 // If there is 1 infobar and it is a translate infobar, closes it and returns |
| 85 // true. Returns false otherwise. |
| 86 bool CloseTranslateInfoBar() { |
| 87 TranslateInfoBarDelegate2* infobar = GetTranslateInfoBar(); |
| 88 if (!infobar) |
| 89 return false; |
| 90 infobar->InfoBarDismissed(); // Simulates closing the infobar. |
| 91 contents()->RemoveInfoBar(infobar); |
| 92 return true; |
| 93 } |
| 94 |
| 95 // Checks whether |infobar| has been removed and clears the removed infobar |
| 96 // list. |
| 97 bool CheckInfoBarRemovedAndReset(InfoBarDelegate* infobar) { |
| 98 bool found = std::find(removed_infobars_.begin(), removed_infobars_.end(), |
| 99 infobar) != removed_infobars_.end(); |
| 100 removed_infobars_.clear(); |
| 101 return found; |
| 102 } |
| 103 |
| 104 // Returns true if at least one infobar was closed. |
| 105 bool InfoBarRemoved() { |
| 106 return !removed_infobars_.empty(); |
| 107 } |
| 108 |
| 109 // Clears the list of stored removed infobars. |
| 110 void ClearRemovedInfoBars() { |
| 111 removed_infobars_.clear(); |
| 112 } |
| 113 |
| 114 // If there is 1 infobar and it is a translate infobar, deny translation and |
| 115 // returns true. Returns false otherwise. |
| 116 bool DenyTranslation() { |
| 117 TranslateInfoBarDelegate2* infobar = GetTranslateInfoBar(); |
| 118 if (!infobar) |
| 119 return false; |
| 120 infobar->TranslationDeclined(); |
| 121 contents()->RemoveInfoBar(infobar); |
| 122 return true; |
| 123 } |
| 124 |
| 125 virtual void Observe(NotificationType type, |
| 126 const NotificationSource& source, |
| 127 const NotificationDetails& details) { |
| 128 DCHECK(type == NotificationType::TAB_CONTENTS_INFOBAR_REMOVED); |
| 129 removed_infobars_.push_back(Details<InfoBarDelegate>(details).ptr()); |
| 130 } |
| 131 |
| 132 protected: |
| 133 virtual void SetUp() { |
| 134 URLFetcher::set_factory(&url_fetcher_factory_); |
| 135 |
| 136 // Access the TranslateManager singleton so it is created before we call |
| 137 // RenderViewHostTestHarness::SetUp() to match what's done in Chrome, where |
| 138 // the TranslateManager is created before the TabContents. This matters as |
| 139 // they both register for similar events and we want the notifications to |
| 140 // happen in the same sequence (TranslateManager first, TabContents second). |
| 141 // Also clears the translate script so it is fetched everytime. |
| 142 Singleton<TranslateManager2>::get()->ClearTranslateScript(); |
| 143 |
| 144 RenderViewHostTestHarness::SetUp(); |
| 145 |
| 146 notification_registrar_.Add( |
| 147 this, |
| 148 NotificationType::TAB_CONTENTS_INFOBAR_REMOVED, |
| 149 Source<TabContents>(contents())); |
| 150 } |
| 151 |
| 152 virtual void TearDown() { |
| 153 notification_registrar_.Remove( |
| 154 this, |
| 155 NotificationType::TAB_CONTENTS_INFOBAR_REMOVED, |
| 156 Source<TabContents>(contents())); |
| 157 |
| 158 RenderViewHostTestHarness::TearDown(); |
| 159 |
| 160 URLFetcher::set_factory(NULL); |
| 161 } |
| 162 |
| 163 void SimulateURLFetch(bool success) { |
| 164 TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); |
| 165 ASSERT_TRUE(fetcher); |
| 166 URLRequestStatus status; |
| 167 status.set_status(success ? URLRequestStatus::SUCCESS : |
| 168 URLRequestStatus::FAILED); |
| 169 fetcher->delegate()->OnURLFetchComplete(fetcher, fetcher->original_url(), |
| 170 status, success ? 200 : 500, |
| 171 ResponseCookies(), |
| 172 std::string()); |
| 173 } |
| 174 |
| 175 void SetPrefObserverExpectation(const wchar_t* path) { |
| 176 EXPECT_CALL( |
| 177 pref_observer_, |
| 178 Observe(NotificationType(NotificationType::PREF_CHANGED), |
| 179 _, |
| 180 Property(&Details<std::wstring>::ptr, Pointee(path)))); |
| 181 } |
| 182 |
| 183 NotificationObserverMock pref_observer_; |
| 184 |
| 185 private: |
| 186 NotificationRegistrar notification_registrar_; |
| 187 scoped_ptr<TestTranslateManager2> translate_manager_; |
| 188 TestURLFetcherFactory url_fetcher_factory_; |
| 189 |
| 190 // The list of infobars that have been removed. |
| 191 // WARNING: the pointers points to deleted objects, use only for comparison. |
| 192 std::vector<InfoBarDelegate*> removed_infobars_; |
| 193 |
| 194 DISALLOW_COPY_AND_ASSIGN(TranslateManager2Test); |
| 195 }; |
| 196 |
| 197 // An observer that keeps track of whether a navigation entry was committed. |
| 198 class NavEntryCommittedObserver : public NotificationObserver { |
| 199 public: |
| 200 explicit NavEntryCommittedObserver(TabContents* tab_contents) { |
| 201 registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED, |
| 202 Source<NavigationController>(&tab_contents->controller())); |
| 203 } |
| 204 |
| 205 virtual void Observe(NotificationType type, |
| 206 const NotificationSource& source, |
| 207 const NotificationDetails& details) { |
| 208 DCHECK(type == NotificationType::NAV_ENTRY_COMMITTED); |
| 209 details_ = |
| 210 *(Details<NavigationController::LoadCommittedDetails>(details).ptr()); |
| 211 } |
| 212 |
| 213 const NavigationController::LoadCommittedDetails& |
| 214 get_load_commited_details() const { |
| 215 return details_; |
| 216 } |
| 217 |
| 218 private: |
| 219 NavigationController::LoadCommittedDetails details_; |
| 220 NotificationRegistrar registrar_; |
| 221 |
| 222 DISALLOW_COPY_AND_ASSIGN(NavEntryCommittedObserver); |
| 223 }; |
| 224 |
| 225 class TestRenderViewContextMenu : public RenderViewContextMenu { |
| 226 public: |
| 227 static TestRenderViewContextMenu* CreateContextMenu( |
| 228 TabContents* tab_contents) { |
| 229 ContextMenuParams params; |
| 230 params.media_type = WebKit::WebContextMenuData::MediaTypeNone; |
| 231 params.x = 0; |
| 232 params.y = 0; |
| 233 params.is_image_blocked = false; |
| 234 params.media_flags = 0; |
| 235 params.spellcheck_enabled = false;; |
| 236 params.is_editable = false; |
| 237 params.page_url = tab_contents->controller().GetActiveEntry()->url(); |
| 238 #if defined(OS_MACOSX) |
| 239 params.writing_direction_default = 0; |
| 240 params.writing_direction_left_to_right = 0; |
| 241 params.writing_direction_right_to_left = 0; |
| 242 #endif // OS_MACOSX |
| 243 params.edit_flags = 0; |
| 244 return new TestRenderViewContextMenu(tab_contents, params); |
| 245 } |
| 246 |
| 247 bool IsItemPresent(int id) { |
| 248 return menu_model_.GetIndexOfCommandId(id) != -1; |
| 249 } |
| 250 |
| 251 virtual void PlatformInit() { } |
| 252 virtual bool GetAcceleratorForCommandId( |
| 253 int command_id, |
| 254 menus::Accelerator* accelerator) { return false; } |
| 255 |
| 256 private: |
| 257 TestRenderViewContextMenu(TabContents* tab_contents, |
| 258 const ContextMenuParams& params) |
| 259 : RenderViewContextMenu(tab_contents, params) { |
| 260 } |
| 261 |
| 262 DISALLOW_COPY_AND_ASSIGN(TestRenderViewContextMenu); |
| 263 }; |
| 264 |
| 265 TEST_F(TranslateManager2Test, NormalTranslate) { |
| 266 // Simulate navigating to a page. |
| 267 SimulateNavigation(GURL("http://www.google.fr"), 0, L"Le Google", "fr"); |
| 268 |
| 269 // We should have an infobar. |
| 270 TranslateInfoBarDelegate2* infobar = GetTranslateInfoBar(); |
| 271 ASSERT_TRUE(infobar != NULL); |
| 272 EXPECT_EQ(TranslateInfoBarDelegate2::BEFORE_TRANSLATE, infobar->type()); |
| 273 |
| 274 // Simulate clicking translate. |
| 275 process()->sink().ClearMessages(); |
| 276 infobar->Translate(); |
| 277 |
| 278 // Simulate the translate script being retrieved (it only needs to be done |
| 279 // once in the test as it is cached). |
| 280 SimulateURLFetch(true); |
| 281 |
| 282 // Test that we sent the right message to the renderer. |
| 283 int page_id = 0; |
| 284 std::string original_lang, target_lang; |
| 285 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); |
| 286 EXPECT_EQ(0, page_id); |
| 287 EXPECT_EQ("fr", original_lang); |
| 288 EXPECT_EQ("en", target_lang); |
| 289 |
| 290 // The "Translating..." infobar should be showing. |
| 291 infobar = GetTranslateInfoBar(); |
| 292 ASSERT_TRUE(infobar != NULL); |
| 293 EXPECT_EQ(TranslateInfoBarDelegate2::TRANSLATING, infobar->type()); |
| 294 |
| 295 // Simulate the render notifying the translation has been done. |
| 296 rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(0, 0, "fr", "en", |
| 297 TranslateErrors::NONE)); |
| 298 |
| 299 // The after translate infobar should be showing. |
| 300 infobar = GetTranslateInfoBar(); |
| 301 ASSERT_TRUE(infobar != NULL); |
| 302 EXPECT_EQ(TranslateInfoBarDelegate2::AFTER_TRANSLATE, infobar->type()); |
| 303 |
| 304 // Simulate translating again from there but with 2 different languages. |
| 305 infobar->SetOriginalLanguage(0); |
| 306 infobar->SetTargetLanguage(1); |
| 307 std::string new_original_lang = infobar->GetOriginalLanguageCode(); |
| 308 std::string new_target_lang = infobar->GetTargetLanguageCode(); |
| 309 process()->sink().ClearMessages(); |
| 310 infobar->Translate(); |
| 311 |
| 312 // Test that we sent the right message to the renderer. |
| 313 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); |
| 314 EXPECT_EQ(0, page_id); |
| 315 EXPECT_EQ(new_original_lang, original_lang); |
| 316 EXPECT_EQ(new_target_lang, target_lang); |
| 317 } |
| 318 |
| 319 TEST_F(TranslateManager2Test, TranslateScriptNotAvailable) { |
| 320 // Simulate navigating to a page. |
| 321 SimulateNavigation(GURL("http://www.google.fr"), 0, L"Le Google", "fr"); |
| 322 |
| 323 // We should have an infobar. |
| 324 TranslateInfoBarDelegate2* infobar = GetTranslateInfoBar(); |
| 325 ASSERT_TRUE(infobar != NULL); |
| 326 EXPECT_EQ(TranslateInfoBarDelegate2::BEFORE_TRANSLATE, infobar->type()); |
| 327 |
| 328 // Simulate clicking translate. |
| 329 process()->sink().ClearMessages(); |
| 330 infobar->Translate(); |
| 331 // Simulate a failure retrieving the translate script. |
| 332 SimulateURLFetch(false); |
| 333 |
| 334 // We should not have sent any message to translate to the renderer. |
| 335 EXPECT_FALSE(GetTranslateMessage(NULL, NULL, NULL)); |
| 336 |
| 337 // And we should have an error infobar showing. |
| 338 infobar = GetTranslateInfoBar(); |
| 339 ASSERT_TRUE(infobar != NULL); |
| 340 EXPECT_EQ(TranslateInfoBarDelegate2::TRANSLATION_ERROR, infobar->type()); |
| 341 } |
| 342 |
| 343 // Tests that we show/don't show an info-bar for all languages the CLD can |
| 344 // report. |
| 345 TEST_F(TranslateManager2Test, TestAllLanguages) { |
| 346 // The index in kExpectation are the Language enum (see languages.pb.h). |
| 347 // true if we expect a translate infobar for that language. |
| 348 // Note the supported languages are in translation_service.cc, see |
| 349 // kSupportedLanguages. |
| 350 bool kExpectations[] = { |
| 351 // 0-9 |
| 352 false, true, true, true, true, true, true, true, true, true, |
| 353 // 10-19 |
| 354 true, true, true, true, true, true, true, true, true, true, |
| 355 // 20-29 |
| 356 true, true, true, true, true, false, false, true, true, true, |
| 357 // 30-39 |
| 358 true, true, true, true, true, true, true, false, true, false, |
| 359 // 40-49 |
| 360 true, false, true, false, false, true, false, true, false, false, |
| 361 // 50-59 |
| 362 false, false, false, true, true, true, false, false, false, false, |
| 363 // 60-69 |
| 364 false, false, true, true, false, true, true, false, true, true, |
| 365 // 70-79 |
| 366 false, false, false, false, false, false, false, true, false, false, |
| 367 // 80-89 |
| 368 false, false, false, false, false, false, false, false, false, false, |
| 369 // 90-99 |
| 370 false, true, false, false, false, false, false, false, false, false, |
| 371 // 100-109 |
| 372 false, true, false, false, false, false, false, false, false, false, |
| 373 // 110-119 |
| 374 false, false, false, false, false, false, false, false, false, false, |
| 375 // 120-129 |
| 376 false, false, false, false, false, false, false, false, false, false, |
| 377 // 130-139 |
| 378 false, false, false, false, false, false, false, false, false, false, |
| 379 // 140-149 |
| 380 false, false, false, false, false, false, false, false, false, false, |
| 381 // 150-159 |
| 382 false, false, false, false, false, false, false, false, false, false, |
| 383 // 160 |
| 384 false |
| 385 }; |
| 386 |
| 387 GURL url("http://www.google.com"); |
| 388 for (size_t i = 0; i < arraysize(kExpectations); ++i) { |
| 389 ASSERT_LT(i, static_cast<size_t>(NUM_LANGUAGES)); |
| 390 |
| 391 std::string lang = LanguageCodeWithDialects(static_cast<Language>(i)); |
| 392 SCOPED_TRACE(::testing::Message::Message() << "Iteration " << i << |
| 393 " language=" << lang); |
| 394 |
| 395 // We should not have a translate infobar. |
| 396 TranslateInfoBarDelegate2* infobar = GetTranslateInfoBar(); |
| 397 ASSERT_TRUE(infobar == NULL); |
| 398 |
| 399 // Simulate navigating to a page. |
| 400 NavigateAndCommit(url); |
| 401 SimulateOnPageContents(url, i, L"", lang); |
| 402 |
| 403 // Verify we have/don't have an info-bar as expected. |
| 404 infobar = GetTranslateInfoBar(); |
| 405 EXPECT_EQ(kExpectations[i], infobar != NULL); |
| 406 |
| 407 // Close the info-bar if applicable. |
| 408 if (infobar != NULL) |
| 409 EXPECT_TRUE(CloseTranslateInfoBar()); |
| 410 } |
| 411 } |
| 412 |
| 413 // Tests auto-translate on page. |
| 414 TEST_F(TranslateManager2Test, AutoTranslateOnNavigate) { |
| 415 // Simulate navigating to a page and getting its language. |
| 416 SimulateNavigation(GURL("http://www.google.fr"), 0, L"Le Google", "fr"); |
| 417 |
| 418 // Simulate the user translating. |
| 419 TranslateInfoBarDelegate2* infobar = GetTranslateInfoBar(); |
| 420 ASSERT_TRUE(infobar != NULL); |
| 421 infobar->Translate(); |
| 422 SimulateURLFetch(true); // Simulate the translate script being retrieved. |
| 423 |
| 424 rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(0, 0, "fr", "en", |
| 425 TranslateErrors::NONE)); |
| 426 |
| 427 // Now navigate to a new page in the same language. |
| 428 process()->sink().ClearMessages(); |
| 429 SimulateNavigation(GURL("http://news.google.fr"), 1, L"Les news", "fr"); |
| 430 |
| 431 // This should have automatically triggered a translation. |
| 432 int page_id = 0; |
| 433 std::string original_lang, target_lang; |
| 434 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); |
| 435 EXPECT_EQ(1, page_id); |
| 436 EXPECT_EQ("fr", original_lang); |
| 437 EXPECT_EQ("en", target_lang); |
| 438 |
| 439 // Now navigate to a page in a different language. |
| 440 process()->sink().ClearMessages(); |
| 441 SimulateNavigation(GURL("http://news.google.es"), 1, L"Las news", "es"); |
| 442 |
| 443 // This should not have triggered a translate. |
| 444 EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); |
| 445 } |
| 446 |
| 447 // Tests that multiple OnPageContents do not cause multiple infobars. |
| 448 TEST_F(TranslateManager2Test, MultipleOnPageContents) { |
| 449 // Simulate navigating to a page and getting its language. |
| 450 SimulateNavigation(GURL("http://www.google.fr"), 0, L"Le Google", "fr"); |
| 451 |
| 452 // Simulate clicking 'Nope' (don't translate). |
| 453 EXPECT_TRUE(DenyTranslation()); |
| 454 EXPECT_EQ(0, contents()->infobar_delegate_count()); |
| 455 |
| 456 // Send a new PageContents, we should not show an infobar. |
| 457 SimulateOnPageContents(GURL("http://www.google.fr"), 0, L"Le Google", "fr"); |
| 458 EXPECT_EQ(0, contents()->infobar_delegate_count()); |
| 459 |
| 460 // Do the same steps but simulate closing the infobar this time. |
| 461 SimulateNavigation(GURL("http://www.youtube.fr"), 1, L"Le YouTube", "fr"); |
| 462 EXPECT_TRUE(CloseTranslateInfoBar()); |
| 463 EXPECT_EQ(0, contents()->infobar_delegate_count()); |
| 464 SimulateOnPageContents(GURL("http://www.youtube.fr"), 1, L"Le YouTube", "fr"); |
| 465 EXPECT_EQ(0, contents()->infobar_delegate_count()); |
| 466 } |
| 467 |
| 468 // Test that reloading the page brings back the infobar. |
| 469 TEST_F(TranslateManager2Test, Reload) { |
| 470 // Simulate navigating to a page and getting its language. |
| 471 SimulateNavigation(GURL("http://www.google.fr"), 0, L"Le Google", "fr"); |
| 472 |
| 473 // Close the infobar. |
| 474 EXPECT_TRUE(CloseTranslateInfoBar()); |
| 475 |
| 476 // Reload should bring back the infobar. |
| 477 NavEntryCommittedObserver nav_observer(contents()); |
| 478 Reload(); |
| 479 |
| 480 // Ensures it is really handled a reload. |
| 481 const NavigationController::LoadCommittedDetails& nav_details = |
| 482 nav_observer.get_load_commited_details(); |
| 483 EXPECT_TRUE(nav_details.entry != NULL); // There was a navigation. |
| 484 EXPECT_EQ(NavigationType::EXISTING_PAGE, nav_details.type); |
| 485 |
| 486 // The TranslateManager class processes the navigation entry committed |
| 487 // notification in a posted task; process that task. |
| 488 MessageLoop::current()->RunAllPending(); |
| 489 EXPECT_TRUE(GetTranslateInfoBar() != NULL); |
| 490 } |
| 491 |
| 492 // Test that reloading the page by way of typing again the URL in the |
| 493 // location bar brings back the infobar. |
| 494 TEST_F(TranslateManager2Test, ReloadFromLocationBar) { |
| 495 GURL url("http://www.google.fr"); |
| 496 |
| 497 // Simulate navigating to a page and getting its language. |
| 498 SimulateNavigation(url, 0, L"Le Google", "fr"); |
| 499 |
| 500 // Close the infobar. |
| 501 EXPECT_TRUE(CloseTranslateInfoBar()); |
| 502 |
| 503 // Create a pending navigation and simulate a page load. That should be the |
| 504 // equivalent of typing the URL again in the location bar. |
| 505 NavEntryCommittedObserver nav_observer(contents()); |
| 506 contents()->controller().LoadURL(url, GURL(), PageTransition::TYPED); |
| 507 rvh()->SendNavigate(0, url); |
| 508 |
| 509 // Test that we are really getting a same page navigation, the test would be |
| 510 // useless if it was not the case. |
| 511 const NavigationController::LoadCommittedDetails& nav_details = |
| 512 nav_observer.get_load_commited_details(); |
| 513 EXPECT_TRUE(nav_details.entry != NULL); // There was a navigation. |
| 514 EXPECT_EQ(NavigationType::SAME_PAGE, nav_details.type); |
| 515 |
| 516 // The TranslateManager class processes the navigation entry committed |
| 517 // notification in a posted task; process that task. |
| 518 MessageLoop::current()->RunAllPending(); |
| 519 EXPECT_TRUE(GetTranslateInfoBar() != NULL); |
| 520 } |
| 521 |
| 522 // Tests that a close translate infobar does not reappear when navigating |
| 523 // in-page. |
| 524 TEST_F(TranslateManager2Test, CloseInfoBarInPageNavigation) { |
| 525 // Simulate navigating to a page and getting its language. |
| 526 SimulateNavigation(GURL("http://www.google.fr"), 0, L"Le Google", "fr"); |
| 527 |
| 528 // Close the infobar. |
| 529 EXPECT_TRUE(CloseTranslateInfoBar()); |
| 530 |
| 531 // Navigate in page, no infobar should be shown. |
| 532 SimulateNavigation(GURL("http://www.google.fr/#ref1"), 0, L"Le Google", "fr"); |
| 533 EXPECT_TRUE(GetTranslateInfoBar() == NULL); |
| 534 |
| 535 // Navigate out of page, a new infobar should show. |
| 536 SimulateNavigation(GURL("http://www.google.fr/foot"), 0, L"Le Google", "fr"); |
| 537 EXPECT_TRUE(GetTranslateInfoBar() != NULL); |
| 538 } |
| 539 |
| 540 // Tests that denying translation is sticky when navigating in page. |
| 541 TEST_F(TranslateManager2Test, DenyTranslateInPageNavigation) { |
| 542 // Simulate navigating to a page and getting its language. |
| 543 SimulateNavigation(GURL("http://www.google.fr"), 0, L"Le Google", "fr"); |
| 544 |
| 545 // Simulate clicking 'Nope' (don't translate). |
| 546 EXPECT_TRUE(DenyTranslation()); |
| 547 |
| 548 // Navigate in page, no infobar should be shown. |
| 549 SimulateNavigation(GURL("http://www.google.fr/#ref1"), 0, L"Le Google", "fr"); |
| 550 EXPECT_TRUE(GetTranslateInfoBar() == NULL); |
| 551 |
| 552 // Navigate out of page, a new infobar should show. |
| 553 SimulateNavigation(GURL("http://www.google.fr/foot"), 0, L"Le Google", "fr"); |
| 554 EXPECT_TRUE(GetTranslateInfoBar() != NULL); |
| 555 } |
| 556 |
| 557 // Tests that after translating and closing the infobar, the infobar does not |
| 558 // return when navigating in page. |
| 559 TEST_F(TranslateManager2Test, TranslateCloseInfoBarInPageNavigation) { |
| 560 // Simulate navigating to a page and getting its language. |
| 561 SimulateNavigation(GURL("http://www.google.fr"), 0, L"Le Google", "fr"); |
| 562 |
| 563 // Simulate the user translating. |
| 564 TranslateInfoBarDelegate2* infobar = GetTranslateInfoBar(); |
| 565 ASSERT_TRUE(infobar != NULL); |
| 566 infobar->Translate(); |
| 567 SimulateURLFetch(true); // Simulate the translate script being retrieved. |
| 568 rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(0, 0, "fr", "en", |
| 569 TranslateErrors::NONE)); |
| 570 |
| 571 // Close the infobar. |
| 572 EXPECT_TRUE(CloseTranslateInfoBar()); |
| 573 |
| 574 // Navigate in page, no infobar should be shown. |
| 575 SimulateNavigation(GURL("http://www.google.fr/#ref1"), 0, L"Le Google", "fr"); |
| 576 EXPECT_TRUE(GetTranslateInfoBar() == NULL); |
| 577 |
| 578 // Navigate out of page, a new infobar should show. |
| 579 // Note that we navigate to a page in a different language so we don't trigger |
| 580 // the auto-translate feature (it would translate the page automatically and |
| 581 // the before translate inforbar would not be shown). |
| 582 SimulateNavigation(GURL("http://www.google.de"), 0, L"Das Google", "de"); |
| 583 EXPECT_TRUE(GetTranslateInfoBar() != NULL); |
| 584 } |
| 585 |
| 586 // Tests that the after translate the infobar still shows when navigating |
| 587 // in-page. |
| 588 TEST_F(TranslateManager2Test, TranslateInPageNavigation) { |
| 589 // Simulate navigating to a page and getting its language. |
| 590 SimulateNavigation(GURL("http://www.google.fr"), 0, L"Le Google", "fr"); |
| 591 |
| 592 // Simulate the user translating. |
| 593 TranslateInfoBarDelegate2* infobar = GetTranslateInfoBar(); |
| 594 ASSERT_TRUE(infobar != NULL); |
| 595 infobar->Translate(); |
| 596 SimulateURLFetch(true); // Simulate the translate script being retrieved. |
| 597 rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(0, 0, "fr", "en", |
| 598 TranslateErrors::NONE)); |
| 599 // The after translate infobar is showing. |
| 600 infobar = GetTranslateInfoBar(); |
| 601 ASSERT_TRUE(infobar != NULL); |
| 602 |
| 603 // Navigate in page, the same infobar should still be shown. |
| 604 ClearRemovedInfoBars(); |
| 605 SimulateNavigation(GURL("http://www.google.fr/#ref1"), 0, L"Le Google", "fr"); |
| 606 EXPECT_FALSE(InfoBarRemoved()); |
| 607 EXPECT_EQ(infobar, GetTranslateInfoBar()); |
| 608 |
| 609 // Navigate out of page, a new infobar should show. |
| 610 // See note in TranslateCloseInfoBarInPageNavigation test on why it is |
| 611 // important to navigate to a page in a different language for this test. |
| 612 SimulateNavigation(GURL("http://www.google.de"), 0, L"Das Google", "de"); |
| 613 // The old infobar is gone. |
| 614 EXPECT_TRUE(CheckInfoBarRemovedAndReset(infobar)); |
| 615 // And there is a new one. |
| 616 EXPECT_TRUE(GetTranslateInfoBar() != NULL); |
| 617 } |
| 618 |
| 619 // Tests that no translate infobar is shown when navigating to a page in an |
| 620 // unsupported language. |
| 621 TEST_F(TranslateManager2Test, UnsupportedPageLanguage) { |
| 622 // Simulate navigating to a page and getting an unsupported language. |
| 623 SimulateNavigation(GURL("http://www.google.com"), 0, L"Google", "qbz"); |
| 624 |
| 625 // No info-bar should be shown. |
| 626 EXPECT_TRUE(GetTranslateInfoBar() == NULL); |
| 627 } |
| 628 |
| 629 // Tests that no translate infobar is shown when Chrome is in a language that |
| 630 // the translate server does not support. |
| 631 TEST_F(TranslateManager2Test, UnsupportedUILanguage) { |
| 632 TestingBrowserProcess* browser_process = |
| 633 static_cast<TestingBrowserProcess*>(g_browser_process); |
| 634 std::string original_lang = browser_process->GetApplicationLocale(); |
| 635 browser_process->SetApplicationLocale("qbz"); |
| 636 |
| 637 // Simulate navigating to a page in a language supported by the translate |
| 638 // server. |
| 639 SimulateNavigation(GURL("http://www.google.com"), 0, L"Google", "en"); |
| 640 |
| 641 // No info-bar should be shown. |
| 642 EXPECT_TRUE(GetTranslateInfoBar() == NULL); |
| 643 |
| 644 browser_process->SetApplicationLocale(original_lang); |
| 645 } |
| 646 |
| 647 // Tests that the translate enabled preference is honored. |
| 648 TEST_F(TranslateManager2Test, TranslateEnabledPref) { |
| 649 // Make sure the pref allows translate. |
| 650 PrefService* prefs = contents()->profile()->GetPrefs(); |
| 651 prefs->SetBoolean(prefs::kEnableTranslate, true); |
| 652 |
| 653 // Simulate navigating to a page and getting its language. |
| 654 SimulateNavigation(GURL("http://www.google.fr"), 0, L"Le Google", "fr"); |
| 655 |
| 656 // An infobar should be shown. |
| 657 TranslateInfoBarDelegate2* infobar = GetTranslateInfoBar(); |
| 658 EXPECT_TRUE(infobar != NULL); |
| 659 |
| 660 // Disable translate. |
| 661 prefs->SetBoolean(prefs::kEnableTranslate, false); |
| 662 |
| 663 // Navigate to a new page, that should close the previous infobar. |
| 664 GURL url("http://www.youtube.fr"); |
| 665 NavigateAndCommit(url); |
| 666 infobar = GetTranslateInfoBar(); |
| 667 EXPECT_TRUE(infobar == NULL); |
| 668 |
| 669 // Simulate getting the page contents and language, that should not trigger |
| 670 // a translate infobar. |
| 671 SimulateOnPageContents(url, 1, L"Le YouTube", "fr"); |
| 672 infobar = GetTranslateInfoBar(); |
| 673 EXPECT_TRUE(infobar == NULL); |
| 674 } |
| 675 |
| 676 // Tests the "Never translate <language>" pref. |
| 677 TEST_F(TranslateManager2Test, NeverTranslateLanguagePref) { |
| 678 // Simulate navigating to a page and getting its language. |
| 679 GURL url("http://www.google.fr"); |
| 680 SimulateNavigation(url, 0, L"Le Google", "fr"); |
| 681 |
| 682 // An infobar should be shown. |
| 683 EXPECT_TRUE(GetTranslateInfoBar() != NULL); |
| 684 |
| 685 // Select never translate this language. |
| 686 PrefService* prefs = contents()->profile()->GetPrefs(); |
| 687 prefs->AddPrefObserver(TranslatePrefs::kPrefTranslateLanguageBlacklist, |
| 688 &pref_observer_); |
| 689 TranslatePrefs translate_prefs(prefs); |
| 690 EXPECT_FALSE(translate_prefs.IsLanguageBlacklisted("fr")); |
| 691 EXPECT_TRUE(translate_prefs.CanTranslate(prefs, "fr", url)); |
| 692 SetPrefObserverExpectation(TranslatePrefs::kPrefTranslateLanguageBlacklist); |
| 693 translate_prefs.BlacklistLanguage("fr"); |
| 694 EXPECT_TRUE(translate_prefs.IsLanguageBlacklisted("fr")); |
| 695 EXPECT_FALSE(translate_prefs.CanTranslate(prefs, "fr", url)); |
| 696 |
| 697 // Close the infobar. |
| 698 EXPECT_TRUE(CloseTranslateInfoBar()); |
| 699 |
| 700 // Navigate to a new page also in French. |
| 701 SimulateNavigation(GURL("http://wwww.youtube.fr"), 1, L"Le YouTube", "fr"); |
| 702 |
| 703 // There should not be a translate infobar. |
| 704 EXPECT_TRUE(GetTranslateInfoBar() == NULL); |
| 705 |
| 706 // Remove the language from the blacklist. |
| 707 SetPrefObserverExpectation(TranslatePrefs::kPrefTranslateLanguageBlacklist); |
| 708 translate_prefs.RemoveLanguageFromBlacklist("fr"); |
| 709 EXPECT_FALSE(translate_prefs.IsLanguageBlacklisted("fr")); |
| 710 EXPECT_TRUE(translate_prefs.CanTranslate(prefs, "fr", url)); |
| 711 |
| 712 // Navigate to a page in French. |
| 713 SimulateNavigation(url, 2, L"Le Google", "fr"); |
| 714 |
| 715 // There should be a translate infobar. |
| 716 EXPECT_TRUE(GetTranslateInfoBar() != NULL); |
| 717 prefs->RemovePrefObserver(TranslatePrefs::kPrefTranslateLanguageBlacklist, |
| 718 &pref_observer_); |
| 719 } |
| 720 |
| 721 // Tests the "Never translate this site" pref. |
| 722 TEST_F(TranslateManager2Test, NeverTranslateSitePref) { |
| 723 // Simulate navigating to a page and getting its language. |
| 724 GURL url("http://www.google.fr"); |
| 725 std::string host(url.host()); |
| 726 SimulateNavigation(url, 0, L"Le Google", "fr"); |
| 727 |
| 728 // An infobar should be shown. |
| 729 EXPECT_TRUE(GetTranslateInfoBar() != NULL); |
| 730 |
| 731 // Select never translate this site. |
| 732 PrefService* prefs = contents()->profile()->GetPrefs(); |
| 733 prefs->AddPrefObserver(TranslatePrefs::kPrefTranslateSiteBlacklist, |
| 734 &pref_observer_); |
| 735 TranslatePrefs translate_prefs(prefs); |
| 736 EXPECT_FALSE(translate_prefs.IsSiteBlacklisted(host)); |
| 737 EXPECT_TRUE(translate_prefs.CanTranslate(prefs, "fr", url)); |
| 738 SetPrefObserverExpectation(TranslatePrefs::kPrefTranslateSiteBlacklist); |
| 739 translate_prefs.BlacklistSite(host); |
| 740 EXPECT_TRUE(translate_prefs.IsSiteBlacklisted(host)); |
| 741 EXPECT_FALSE(translate_prefs.CanTranslate(prefs, "fr", url)); |
| 742 |
| 743 // Close the infobar. |
| 744 EXPECT_TRUE(CloseTranslateInfoBar()); |
| 745 |
| 746 // Navigate to a new page also on the same site. |
| 747 SimulateNavigation(GURL("http://www.google.fr/hello"), 1, L"Bonjour", "fr"); |
| 748 |
| 749 // There should not be a translate infobar. |
| 750 EXPECT_TRUE(GetTranslateInfoBar() == NULL); |
| 751 |
| 752 // Remove the site from the blacklist. |
| 753 SetPrefObserverExpectation(TranslatePrefs::kPrefTranslateSiteBlacklist); |
| 754 translate_prefs.RemoveSiteFromBlacklist(host); |
| 755 EXPECT_FALSE(translate_prefs.IsSiteBlacklisted(host)); |
| 756 EXPECT_TRUE(translate_prefs.CanTranslate(prefs, "fr", url)); |
| 757 |
| 758 // Navigate to a page in French. |
| 759 SimulateNavigation(url, 0, L"Le Google", "fr"); |
| 760 |
| 761 // There should be a translate infobar. |
| 762 EXPECT_TRUE(GetTranslateInfoBar() != NULL); |
| 763 prefs->RemovePrefObserver(TranslatePrefs::kPrefTranslateSiteBlacklist, |
| 764 &pref_observer_); |
| 765 } |
| 766 |
| 767 // Tests the "Always translate this language" pref. |
| 768 TEST_F(TranslateManager2Test, AlwaysTranslateLanguagePref) { |
| 769 // Select always translate French to English. |
| 770 PrefService* prefs = contents()->profile()->GetPrefs(); |
| 771 prefs->AddPrefObserver(TranslatePrefs::kPrefTranslateWhitelists, |
| 772 &pref_observer_); |
| 773 TranslatePrefs translate_prefs(prefs); |
| 774 SetPrefObserverExpectation(TranslatePrefs::kPrefTranslateWhitelists); |
| 775 translate_prefs.WhitelistLanguagePair("fr", "en"); |
| 776 |
| 777 // Load a page in French. |
| 778 SimulateNavigation(GURL("http://www.google.fr"), 0, L"Le Google", "fr"); |
| 779 |
| 780 // It should have triggered an automatic translation to English. |
| 781 SimulateURLFetch(true); // Simulate the translate script being retrieved. |
| 782 int page_id = 0; |
| 783 std::string original_lang, target_lang; |
| 784 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); |
| 785 EXPECT_EQ(0, page_id); |
| 786 EXPECT_EQ("fr", original_lang); |
| 787 EXPECT_EQ("en", target_lang); |
| 788 process()->sink().ClearMessages(); |
| 789 // And we should have no infobar (since we don't send the page translated |
| 790 // notification, the after translate infobar is not shown). |
| 791 EXPECT_TRUE(GetTranslateInfoBar() == NULL); |
| 792 |
| 793 // Try another language, it should not be autotranslated. |
| 794 SimulateNavigation(GURL("http://www.google.es"), 1, L"El Google", "es"); |
| 795 EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); |
| 796 EXPECT_TRUE(GetTranslateInfoBar() != NULL); |
| 797 EXPECT_TRUE(CloseTranslateInfoBar()); |
| 798 |
| 799 // Let's switch to incognito mode, it should not be autotranslated in that |
| 800 // case either. |
| 801 TestingProfile* test_profile = |
| 802 static_cast<TestingProfile*>(contents()->profile()); |
| 803 test_profile->set_off_the_record(true); |
| 804 SimulateNavigation(GURL("http://www.youtube.fr"), 2, L"Le YouTube", "fr"); |
| 805 EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); |
| 806 EXPECT_TRUE(GetTranslateInfoBar() != NULL); |
| 807 EXPECT_TRUE(CloseTranslateInfoBar()); |
| 808 test_profile->set_off_the_record(false); // Get back to non incognito. |
| 809 |
| 810 // Now revert the always translate pref and make sure we go back to expected |
| 811 // behavior, which is show an infobar. |
| 812 SetPrefObserverExpectation(TranslatePrefs::kPrefTranslateWhitelists); |
| 813 translate_prefs.RemoveLanguagePairFromWhitelist("fr", "en"); |
| 814 SimulateNavigation(GURL("http://www.google.fr"), 3, L"Le Google", "fr"); |
| 815 EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); |
| 816 EXPECT_TRUE(GetTranslateInfoBar() != NULL); |
| 817 prefs->RemovePrefObserver(TranslatePrefs::kPrefTranslateWhitelists, |
| 818 &pref_observer_); |
| 819 } |
| 820 /* |
| 821 |
| 822 // Context menu. |
| 823 TEST_F(TranslateManager2Test, ContextMenu) { |
| 824 // Blacklist www.google.fr and French for translation. |
| 825 GURL url("http://www.google.fr"); |
| 826 TranslatePrefs translate_prefs(contents()->profile()->GetPrefs()); |
| 827 translate_prefs.BlacklistLanguage("fr"); |
| 828 translate_prefs.BlacklistSite(url.host()); |
| 829 EXPECT_TRUE(translate_prefs.IsLanguageBlacklisted("fr")); |
| 830 EXPECT_TRUE(translate_prefs.IsSiteBlacklisted(url.host())); |
| 831 |
| 832 // Simulate navigating to a page in French. The translate menu should show. |
| 833 SimulateNavigation(url, 0, L"Le Google", "fr"); |
| 834 scoped_ptr<TestRenderViewContextMenu> menu( |
| 835 TestRenderViewContextMenu::CreateContextMenu(contents())); |
| 836 menu->Init(); |
| 837 EXPECT_TRUE(menu->IsItemPresent(IDS_CONTENT_CONTEXT_TRANSLATE)); |
| 838 EXPECT_TRUE(menu->TestIsCommandIdEnabled(IDS_CONTENT_CONTEXT_TRANSLATE)); |
| 839 |
| 840 // Use the menu to translate the page. |
| 841 menu->TestExecuteItemCommand(IDS_CONTENT_CONTEXT_TRANSLATE); |
| 842 |
| 843 // That should have triggered a translation. |
| 844 SimulateURLFetch(true); // Simulate the translate script being retrieved. |
| 845 int page_id = 0; |
| 846 std::string original_lang, target_lang; |
| 847 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); |
| 848 EXPECT_EQ(0, page_id); |
| 849 EXPECT_EQ("fr", original_lang); |
| 850 EXPECT_EQ("en", target_lang); |
| 851 process()->sink().ClearMessages(); |
| 852 |
| 853 // This should also have reverted the blacklisting of this site and language. |
| 854 EXPECT_FALSE(translate_prefs.IsLanguageBlacklisted("fr")); |
| 855 EXPECT_FALSE(translate_prefs.IsSiteBlacklisted(url.host())); |
| 856 |
| 857 // Let's simulate the page being translated. |
| 858 rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(0, 0, "fr", "en", |
| 859 TranslateErrors::NONE)); |
| 860 |
| 861 // The translate menu should now be disabled. |
| 862 menu.reset(TestRenderViewContextMenu::CreateContextMenu(contents())); |
| 863 menu->Init(); |
| 864 EXPECT_TRUE(menu->IsItemPresent(IDS_CONTENT_CONTEXT_TRANSLATE)); |
| 865 EXPECT_FALSE(menu->TestIsCommandIdEnabled(IDS_CONTENT_CONTEXT_TRANSLATE)); |
| 866 |
| 867 // Test that selecting translate in the context menu WHILE the page is being |
| 868 // translated does nothing (this could happen if autotranslate kicks-in and |
| 869 // the user selects the menu while the translation is being performed). |
| 870 SimulateNavigation(GURL("http://www.google.es"), 1, L"El Google", "es"); |
| 871 TranslateInfoBarDelegate2* infobar = GetTranslateInfoBar(); |
| 872 ASSERT_TRUE(infobar != NULL); |
| 873 infobar->Translate(); |
| 874 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); |
| 875 EXPECT_EQ(1, page_id); |
| 876 process()->sink().ClearMessages(); |
| 877 menu.reset(TestRenderViewContextMenu::CreateContextMenu(contents())); |
| 878 menu->Init(); |
| 879 EXPECT_TRUE(menu->TestIsCommandIdEnabled(IDS_CONTENT_CONTEXT_TRANSLATE)); |
| 880 menu->TestExecuteItemCommand(IDS_CONTENT_CONTEXT_TRANSLATE); |
| 881 // No message expected since the translation should have been ignored. |
| 882 EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); |
| 883 |
| 884 // Now test that selecting translate in the context menu AFTER the page has |
| 885 // been translated does nothing. |
| 886 SimulateNavigation(GURL("http://www.google.de"), 2, L"Das Google", "de"); |
| 887 infobar = GetTranslateInfoBar(); |
| 888 ASSERT_TRUE(infobar != NULL); |
| 889 infobar->Translate(); |
| 890 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); |
| 891 EXPECT_EQ(2, page_id); |
| 892 process()->sink().ClearMessages(); |
| 893 menu.reset(TestRenderViewContextMenu::CreateContextMenu(contents())); |
| 894 menu->Init(); |
| 895 EXPECT_TRUE(menu->TestIsCommandIdEnabled(IDS_CONTENT_CONTEXT_TRANSLATE)); |
| 896 rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(0, 0, "de", "en", |
| 897 TranslateErrors::NONE)); |
| 898 menu->TestExecuteItemCommand(IDS_CONTENT_CONTEXT_TRANSLATE); |
| 899 // No message expected since the translation should have been ignored. |
| 900 EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); |
| 901 |
| 902 // Test that the translate context menu is disabled when the page is in the |
| 903 // same language as the UI. |
| 904 SimulateNavigation(url, 0, L"Google", "en"); |
| 905 menu.reset(TestRenderViewContextMenu::CreateContextMenu(contents())); |
| 906 menu->Init(); |
| 907 EXPECT_TRUE(menu->IsItemPresent(IDS_CONTENT_CONTEXT_TRANSLATE)); |
| 908 EXPECT_FALSE(menu->TestIsCommandIdEnabled(IDS_CONTENT_CONTEXT_TRANSLATE)); |
| 909 } |
| 910 */ |
| 911 |
| 912 // Context menu. |
| 913 TEST_F(TranslateManager2Test, ContextMenu) { |
| 914 // Blacklist www.google.fr and French for translation. |
| 915 GURL url("http://www.google.fr"); |
| 916 TranslatePrefs translate_prefs(contents()->profile()->GetPrefs()); |
| 917 translate_prefs.BlacklistLanguage("fr"); |
| 918 translate_prefs.BlacklistSite(url.host()); |
| 919 EXPECT_TRUE(translate_prefs.IsLanguageBlacklisted("fr")); |
| 920 EXPECT_TRUE(translate_prefs.IsSiteBlacklisted(url.host())); |
| 921 |
| 922 // Simulate navigating to a page in French. The translate menu should show. |
| 923 SimulateNavigation(url, 0, L"Le Google", "fr"); |
| 924 scoped_ptr<TestRenderViewContextMenu> menu( |
| 925 TestRenderViewContextMenu::CreateContextMenu(contents())); |
| 926 menu->Init(); |
| 927 EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE)); |
| 928 EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE)); |
| 929 |
| 930 // Use the menu to translate the page. |
| 931 menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE); |
| 932 |
| 933 // That should have triggered a translation. |
| 934 SimulateURLFetch(true); // Simulate the translate script being retrieved. |
| 935 int page_id = 0; |
| 936 std::string original_lang, target_lang; |
| 937 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); |
| 938 EXPECT_EQ(0, page_id); |
| 939 EXPECT_EQ("fr", original_lang); |
| 940 EXPECT_EQ("en", target_lang); |
| 941 process()->sink().ClearMessages(); |
| 942 |
| 943 // This should also have reverted the blacklisting of this site and language. |
| 944 EXPECT_FALSE(translate_prefs.IsLanguageBlacklisted("fr")); |
| 945 EXPECT_FALSE(translate_prefs.IsSiteBlacklisted(url.host())); |
| 946 |
| 947 // Let's simulate the page being translated. |
| 948 rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(0, 0, "fr", "en", |
| 949 TranslateErrors::NONE)); |
| 950 |
| 951 // The translate menu should now be disabled. |
| 952 menu.reset(TestRenderViewContextMenu::CreateContextMenu(contents())); |
| 953 menu->Init(); |
| 954 EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE)); |
| 955 EXPECT_FALSE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE)); |
| 956 |
| 957 // Test that selecting translate in the context menu WHILE the page is being |
| 958 // translated does nothing (this could happen if autotranslate kicks-in and |
| 959 // the user selects the menu while the translation is being performed). |
| 960 SimulateNavigation(GURL("http://www.google.es"), 1, L"El Google", "es"); |
| 961 TranslateInfoBarDelegate2* infobar = GetTranslateInfoBar(); |
| 962 ASSERT_TRUE(infobar != NULL); |
| 963 infobar->Translate(); |
| 964 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); |
| 965 EXPECT_EQ(1, page_id); |
| 966 process()->sink().ClearMessages(); |
| 967 menu.reset(TestRenderViewContextMenu::CreateContextMenu(contents())); |
| 968 menu->Init(); |
| 969 EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE)); |
| 970 menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE); |
| 971 // No message expected since the translation should have been ignored. |
| 972 EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); |
| 973 |
| 974 // Now test that selecting translate in the context menu AFTER the page has |
| 975 // been translated does nothing. |
| 976 SimulateNavigation(GURL("http://www.google.de"), 2, L"Das Google", "de"); |
| 977 infobar = GetTranslateInfoBar(); |
| 978 ASSERT_TRUE(infobar != NULL); |
| 979 infobar->Translate(); |
| 980 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); |
| 981 EXPECT_EQ(2, page_id); |
| 982 process()->sink().ClearMessages(); |
| 983 menu.reset(TestRenderViewContextMenu::CreateContextMenu(contents())); |
| 984 menu->Init(); |
| 985 EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE)); |
| 986 rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(0, 0, "de", "en", |
| 987 TranslateErrors::NONE)); |
| 988 menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE); |
| 989 // No message expected since the translation should have been ignored. |
| 990 EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); |
| 991 |
| 992 // Test that the translate context menu is disabled when the page is in the |
| 993 // same language as the UI. |
| 994 SimulateNavigation(url, 0, L"Google", "en"); |
| 995 menu.reset(TestRenderViewContextMenu::CreateContextMenu(contents())); |
| 996 menu->Init(); |
| 997 EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE)); |
| 998 EXPECT_FALSE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE)); |
| 999 } |
OLD | NEW |