| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 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/renderer_host/test/test_render_view_host.h" | 5 #include "chrome/browser/renderer_host/test/test_render_view_host.h" |
| 6 | 6 |
| 7 #include "base/utf_string_conversions.h" | 7 #include "base/utf_string_conversions.h" |
| 8 #include "chrome/app/chrome_dll_resource.h" | 8 #include "chrome/app/chrome_dll_resource.h" |
| 9 #include "chrome/browser/renderer_host/mock_render_process_host.h" | 9 #include "chrome/browser/renderer_host/mock_render_process_host.h" |
| 10 #include "chrome/browser/tab_contents/render_view_context_menu.h" | 10 #include "chrome/browser/tab_contents/render_view_context_menu.h" |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 106 // Returns true if at least one infobar was closed. | 106 // Returns true if at least one infobar was closed. |
| 107 bool InfoBarRemoved() { | 107 bool InfoBarRemoved() { |
| 108 return !removed_infobars_.empty(); | 108 return !removed_infobars_.empty(); |
| 109 } | 109 } |
| 110 | 110 |
| 111 // Clears the list of stored removed infobars. | 111 // Clears the list of stored removed infobars. |
| 112 void ClearRemovedInfoBars() { | 112 void ClearRemovedInfoBars() { |
| 113 removed_infobars_.clear(); | 113 removed_infobars_.clear(); |
| 114 } | 114 } |
| 115 | 115 |
| 116 void ExpireTranslateScriptImmediately() { |
| 117 Singleton<TranslateManager>::get()-> |
| 118 set_translate_script_expiration_delay(0); |
| 119 } |
| 120 |
| 116 // If there is 1 infobar and it is a translate infobar, deny translation and | 121 // If there is 1 infobar and it is a translate infobar, deny translation and |
| 117 // returns true. Returns false otherwise. | 122 // returns true. Returns false otherwise. |
| 118 bool DenyTranslation() { | 123 bool DenyTranslation() { |
| 119 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); | 124 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); |
| 120 if (!infobar) | 125 if (!infobar) |
| 121 return false; | 126 return false; |
| 122 infobar->TranslationDeclined(); | 127 infobar->TranslationDeclined(); |
| 123 contents()->RemoveInfoBar(infobar); | 128 contents()->RemoveInfoBar(infobar); |
| 124 return true; | 129 return true; |
| 125 } | 130 } |
| 126 | 131 |
| 127 virtual void Observe(NotificationType type, | 132 virtual void Observe(NotificationType type, |
| 128 const NotificationSource& source, | 133 const NotificationSource& source, |
| 129 const NotificationDetails& details) { | 134 const NotificationDetails& details) { |
| 130 DCHECK(type == NotificationType::TAB_CONTENTS_INFOBAR_REMOVED); | 135 DCHECK(type == NotificationType::TAB_CONTENTS_INFOBAR_REMOVED); |
| 131 removed_infobars_.push_back(Details<InfoBarDelegate>(details).ptr()); | 136 removed_infobars_.push_back(Details<InfoBarDelegate>(details).ptr()); |
| 132 } | 137 } |
| 133 | 138 |
| 134 protected: | 139 protected: |
| 135 virtual void SetUp() { | 140 virtual void SetUp() { |
| 136 URLFetcher::set_factory(&url_fetcher_factory_); | 141 URLFetcher::set_factory(&url_fetcher_factory_); |
| 137 | 142 |
| 138 // Access the TranslateManager singleton so it is created before we call | 143 // Access the TranslateManager singleton so it is created before we call |
| 139 // RenderViewHostTestHarness::SetUp() to match what's done in Chrome, where | 144 // RenderViewHostTestHarness::SetUp() to match what's done in Chrome, where |
| 140 // the TranslateManager is created before the TabContents. This matters as | 145 // the TranslateManager is created before the TabContents. This matters as |
| 141 // they both register for similar events and we want the notifications to | 146 // they both register for similar events and we want the notifications to |
| 142 // happen in the same sequence (TranslateManager first, TabContents second). | 147 // happen in the same sequence (TranslateManager first, TabContents second). |
| 143 // Also clears the translate script so it is fetched everytime. | 148 // Also clears the translate script so it is fetched everytime and sets the |
| 149 // expiration delay to a large value by default (in case it was zeroed in |
| 150 // a previous test). |
| 144 Singleton<TranslateManager>::get()->ClearTranslateScript(); | 151 Singleton<TranslateManager>::get()->ClearTranslateScript(); |
| 152 Singleton<TranslateManager>::get()-> |
| 153 set_translate_script_expiration_delay(60 * 60 * 1000); |
| 145 | 154 |
| 146 RenderViewHostTestHarness::SetUp(); | 155 RenderViewHostTestHarness::SetUp(); |
| 147 | 156 |
| 148 notification_registrar_.Add( | 157 notification_registrar_.Add( |
| 149 this, | 158 this, |
| 150 NotificationType::TAB_CONTENTS_INFOBAR_REMOVED, | 159 NotificationType::TAB_CONTENTS_INFOBAR_REMOVED, |
| 151 Source<TabContents>(contents())); | 160 Source<TabContents>(contents())); |
| 152 } | 161 } |
| 153 | 162 |
| 154 virtual void TearDown() { | 163 virtual void TearDown() { |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 265 DISALLOW_COPY_AND_ASSIGN(TestRenderViewContextMenu); | 274 DISALLOW_COPY_AND_ASSIGN(TestRenderViewContextMenu); |
| 266 }; | 275 }; |
| 267 | 276 |
| 268 TEST_F(TranslateManagerTest, NormalTranslate) { | 277 TEST_F(TranslateManagerTest, NormalTranslate) { |
| 269 // Simulate navigating to a page. | 278 // Simulate navigating to a page. |
| 270 SimulateNavigation(GURL("http://www.google.fr"), 0, "Le Google", "fr", true); | 279 SimulateNavigation(GURL("http://www.google.fr"), 0, "Le Google", "fr", true); |
| 271 | 280 |
| 272 // We should have an infobar. | 281 // We should have an infobar. |
| 273 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); | 282 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); |
| 274 ASSERT_TRUE(infobar != NULL); | 283 ASSERT_TRUE(infobar != NULL); |
| 275 EXPECT_EQ(TranslateInfoBarDelegate::kBeforeTranslate, infobar->type()); | 284 EXPECT_EQ(TranslateInfoBarDelegate::BEFORE_TRANSLATE, infobar->type()); |
| 276 | 285 |
| 277 // Simulate clicking translate. | 286 // Simulate clicking translate. |
| 278 process()->sink().ClearMessages(); | 287 process()->sink().ClearMessages(); |
| 279 infobar->Translate(); | 288 infobar->Translate(); |
| 280 | 289 |
| 281 // The "Translating..." infobar should be showing. | 290 // The "Translating..." infobar should be showing. |
| 282 infobar = GetTranslateInfoBar(); | 291 infobar = GetTranslateInfoBar(); |
| 283 ASSERT_TRUE(infobar != NULL); | 292 ASSERT_TRUE(infobar != NULL); |
| 284 EXPECT_EQ(TranslateInfoBarDelegate::kTranslating, infobar->type()); | 293 EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATING, infobar->type()); |
| 285 | 294 |
| 286 // Simulate the translate script being retrieved (it only needs to be done | 295 // Simulate the translate script being retrieved (it only needs to be done |
| 287 // once in the test as it is cached). | 296 // once in the test as it is cached). |
| 288 SimulateURLFetch(true); | 297 SimulateURLFetch(true); |
| 289 | 298 |
| 290 // Test that we sent the right message to the renderer. | 299 // Test that we sent the right message to the renderer. |
| 291 int page_id = 0; | 300 int page_id = 0; |
| 292 std::string original_lang, target_lang; | 301 std::string original_lang, target_lang; |
| 293 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); | 302 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); |
| 294 EXPECT_EQ(0, page_id); | 303 EXPECT_EQ(0, page_id); |
| 295 EXPECT_EQ("fr", original_lang); | 304 EXPECT_EQ("fr", original_lang); |
| 296 EXPECT_EQ("en", target_lang); | 305 EXPECT_EQ("en", target_lang); |
| 297 | 306 |
| 298 // Simulate the render notifying the translation has been done. | 307 // Simulate the render notifying the translation has been done. |
| 299 rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(0, 0, "fr", "en", | 308 rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(0, 0, "fr", "en", |
| 300 TranslateErrors::NONE)); | 309 TranslateErrors::NONE)); |
| 301 | 310 |
| 302 // The after translate infobar should be showing. | 311 // The after translate infobar should be showing. |
| 303 infobar = GetTranslateInfoBar(); | 312 infobar = GetTranslateInfoBar(); |
| 304 ASSERT_TRUE(infobar != NULL); | 313 ASSERT_TRUE(infobar != NULL); |
| 305 EXPECT_EQ(TranslateInfoBarDelegate::kAfterTranslate, infobar->type()); | 314 EXPECT_EQ(TranslateInfoBarDelegate::AFTER_TRANSLATE, infobar->type()); |
| 306 | 315 |
| 307 // Simulate changing the original language, this should trigger a translation. | 316 // Simulate changing the original language, this should trigger a translation. |
| 308 process()->sink().ClearMessages(); | 317 process()->sink().ClearMessages(); |
| 309 std::string new_original_lang = infobar->GetLanguageCodeAt(0); | 318 std::string new_original_lang = infobar->GetLanguageCodeAt(0); |
| 310 infobar->SetOriginalLanguage(0); | 319 infobar->SetOriginalLanguage(0); |
| 311 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); | 320 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); |
| 312 EXPECT_EQ(0, page_id); | 321 EXPECT_EQ(0, page_id); |
| 313 EXPECT_EQ(new_original_lang, original_lang); | 322 EXPECT_EQ(new_original_lang, original_lang); |
| 314 EXPECT_EQ("en", target_lang); | 323 EXPECT_EQ("en", target_lang); |
| 315 // Simulate the render notifying the translation has been done. | 324 // Simulate the render notifying the translation has been done. |
| (...skipping 20 matching lines...) Expand all Loading... |
| 336 ASSERT_TRUE(new_infobar != NULL); | 345 ASSERT_TRUE(new_infobar != NULL); |
| 337 } | 346 } |
| 338 | 347 |
| 339 TEST_F(TranslateManagerTest, TranslateScriptNotAvailable) { | 348 TEST_F(TranslateManagerTest, TranslateScriptNotAvailable) { |
| 340 // Simulate navigating to a page. | 349 // Simulate navigating to a page. |
| 341 SimulateNavigation(GURL("http://www.google.fr"), 0, "Le Google", "fr", true); | 350 SimulateNavigation(GURL("http://www.google.fr"), 0, "Le Google", "fr", true); |
| 342 | 351 |
| 343 // We should have an infobar. | 352 // We should have an infobar. |
| 344 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); | 353 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); |
| 345 ASSERT_TRUE(infobar != NULL); | 354 ASSERT_TRUE(infobar != NULL); |
| 346 EXPECT_EQ(TranslateInfoBarDelegate::kBeforeTranslate, infobar->type()); | 355 EXPECT_EQ(TranslateInfoBarDelegate::BEFORE_TRANSLATE, infobar->type()); |
| 347 | 356 |
| 348 // Simulate clicking translate. | 357 // Simulate clicking translate. |
| 349 process()->sink().ClearMessages(); | 358 process()->sink().ClearMessages(); |
| 350 infobar->Translate(); | 359 infobar->Translate(); |
| 351 // Simulate a failure retrieving the translate script. | 360 // Simulate a failure retrieving the translate script. |
| 352 SimulateURLFetch(false); | 361 SimulateURLFetch(false); |
| 353 | 362 |
| 354 // We should not have sent any message to translate to the renderer. | 363 // We should not have sent any message to translate to the renderer. |
| 355 EXPECT_FALSE(GetTranslateMessage(NULL, NULL, NULL)); | 364 EXPECT_FALSE(GetTranslateMessage(NULL, NULL, NULL)); |
| 356 | 365 |
| 357 // And we should have an error infobar showing. | 366 // And we should have an error infobar showing. |
| 358 infobar = GetTranslateInfoBar(); | 367 infobar = GetTranslateInfoBar(); |
| 359 ASSERT_TRUE(infobar != NULL); | 368 ASSERT_TRUE(infobar != NULL); |
| 360 EXPECT_EQ(TranslateInfoBarDelegate::kTranslationError, infobar->type()); | 369 EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATION_ERROR, infobar->type()); |
| 361 } | 370 } |
| 362 | 371 |
| 363 // Ensures we deal correctly with pages for which the browser does not recognize | 372 // Ensures we deal correctly with pages for which the browser does not recognize |
| 364 // the language (the translate server may or not detect the language). | 373 // the language (the translate server may or not detect the language). |
| 365 TEST_F(TranslateManagerTest, TranslateUnknownLanguage) { | 374 TEST_F(TranslateManagerTest, TranslateUnknownLanguage) { |
| 366 // Simulate navigating to a page ("und" is the string returned by the CLD for | 375 // Simulate navigating to a page ("und" is the string returned by the CLD for |
| 367 // languages it does not recognize). | 376 // languages it does not recognize). |
| 368 SimulateNavigation(GURL("http://www.google.mys"), 0, "G00g1e", "und", true); | 377 SimulateNavigation(GURL("http://www.google.mys"), 0, "G00g1e", "und", true); |
| 369 | 378 |
| 370 // We should not have an infobar as we don't know the language. | 379 // We should not have an infobar as we don't know the language. |
| 371 ASSERT_TRUE(GetTranslateInfoBar() == NULL); | 380 ASSERT_TRUE(GetTranslateInfoBar() == NULL); |
| 372 | 381 |
| 373 // Translate the page anyway throught the context menu. | 382 // Translate the page anyway throught the context menu. |
| 374 scoped_ptr<TestRenderViewContextMenu> menu( | 383 scoped_ptr<TestRenderViewContextMenu> menu( |
| 375 TestRenderViewContextMenu::CreateContextMenu(contents())); | 384 TestRenderViewContextMenu::CreateContextMenu(contents())); |
| 376 menu->Init(); | 385 menu->Init(); |
| 377 menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE); | 386 menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE); |
| 378 SimulateURLFetch(true); // Simulare receiving the translate script. | 387 SimulateURLFetch(true); // Simulate receiving the translate script. |
| 379 | 388 |
| 380 // Simulate the render notifying the translation has been done, the server | 389 // Simulate the render notifying the translation has been done, the server |
| 381 // having detected the page was in a known and supported language. | 390 // having detected the page was in a known and supported language. |
| 382 rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(0, 0, "fr", "en", | 391 rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(0, 0, "fr", "en", |
| 383 TranslateErrors::NONE)); | 392 TranslateErrors::NONE)); |
| 384 | 393 |
| 385 // The after translate infobar should be showing. | 394 // The after translate infobar should be showing. |
| 386 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); | 395 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); |
| 387 ASSERT_TRUE(infobar != NULL); | 396 ASSERT_TRUE(infobar != NULL); |
| 388 EXPECT_EQ(TranslateInfoBarDelegate::kAfterTranslate, infobar->type()); | 397 EXPECT_EQ(TranslateInfoBarDelegate::AFTER_TRANSLATE, infobar->type()); |
| 389 EXPECT_EQ("fr", infobar->GetOriginalLanguageCode()); | 398 EXPECT_EQ("fr", infobar->GetOriginalLanguageCode()); |
| 390 EXPECT_EQ("en", infobar->GetTargetLanguageCode()); | 399 EXPECT_EQ("en", infobar->GetTargetLanguageCode()); |
| 391 | 400 |
| 392 // Let's run the same steps but this time the server detects the page is | 401 // Let's run the same steps but this time the server detects the page is |
| 393 // already in English. | 402 // already in English. |
| 394 SimulateNavigation(GURL("http://www.google.com"), 1, "The Google", "und", | 403 SimulateNavigation(GURL("http://www.google.com"), 1, "The Google", "und", |
| 395 true); | 404 true); |
| 396 menu.reset(TestRenderViewContextMenu::CreateContextMenu(contents())); | 405 menu.reset(TestRenderViewContextMenu::CreateContextMenu(contents())); |
| 397 menu->Init(); | 406 menu->Init(); |
| 398 menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE); | 407 menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE); |
| 399 rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(1, 0, "en", "en", | 408 rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(1, 0, "en", "en", |
| 400 TranslateErrors::IDENTICAL_LANGUAGES)); | 409 TranslateErrors::IDENTICAL_LANGUAGES)); |
| 401 infobar = GetTranslateInfoBar(); | 410 infobar = GetTranslateInfoBar(); |
| 402 ASSERT_TRUE(infobar != NULL); | 411 ASSERT_TRUE(infobar != NULL); |
| 403 EXPECT_EQ(TranslateInfoBarDelegate::kTranslationError, infobar->type()); | 412 EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATION_ERROR, infobar->type()); |
| 404 EXPECT_EQ(TranslateErrors::IDENTICAL_LANGUAGES, infobar->error()); | 413 EXPECT_EQ(TranslateErrors::IDENTICAL_LANGUAGES, infobar->error()); |
| 405 | 414 |
| 406 // Let's run the same steps again but this time the server fails to detect the | 415 // Let's run the same steps again but this time the server fails to detect the |
| 407 // page's language (it returns an empty string). | 416 // page's language (it returns an empty string). |
| 408 SimulateNavigation(GURL("http://www.google.com"), 2, "The Google", "und", | 417 SimulateNavigation(GURL("http://www.google.com"), 2, "The Google", "und", |
| 409 true); | 418 true); |
| 410 menu.reset(TestRenderViewContextMenu::CreateContextMenu(contents())); | 419 menu.reset(TestRenderViewContextMenu::CreateContextMenu(contents())); |
| 411 menu->Init(); | 420 menu->Init(); |
| 412 menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE); | 421 menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE); |
| 413 rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(2, 0, "", "en", | 422 rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(2, 0, "", "en", |
| 414 TranslateErrors::UNKNOWN_LANGUAGE)); | 423 TranslateErrors::UNKNOWN_LANGUAGE)); |
| 415 infobar = GetTranslateInfoBar(); | 424 infobar = GetTranslateInfoBar(); |
| 416 ASSERT_TRUE(infobar != NULL); | 425 ASSERT_TRUE(infobar != NULL); |
| 417 EXPECT_EQ(TranslateInfoBarDelegate::kTranslationError, infobar->type()); | 426 EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATION_ERROR, infobar->type()); |
| 418 EXPECT_EQ(TranslateErrors::UNKNOWN_LANGUAGE, infobar->error()); | 427 EXPECT_EQ(TranslateErrors::UNKNOWN_LANGUAGE, infobar->error()); |
| 419 } | 428 } |
| 420 | 429 |
| 421 // Tests that we show/don't show an info-bar for all languages the CLD can | 430 // Tests that we show/don't show an info-bar for all languages the CLD can |
| 422 // report. | 431 // report. |
| 423 TEST_F(TranslateManagerTest, TestAllLanguages) { | 432 TEST_F(TranslateManagerTest, TestAllLanguages) { |
| 424 // The index in kExpectation are the Language enum (see languages.pb.h). | 433 // The index in kExpectation are the Language enum (see languages.pb.h). |
| 425 // true if we expect a translate infobar for that language. | 434 // true if we expect a translate infobar for that language. |
| 426 // Note the supported languages are in translation_manager.cc, see | 435 // Note the supported languages are in translation_manager.cc, see |
| 427 // kSupportedLanguages. | 436 // kSupportedLanguages. |
| (...skipping 299 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 727 SimulateURLFetch(true); | 736 SimulateURLFetch(true); |
| 728 // Simulate the render notifying the translation has been done, but it | 737 // Simulate the render notifying the translation has been done, but it |
| 729 // reports a language we don't support. | 738 // reports a language we don't support. |
| 730 rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(0, 0, "qbz", "en", | 739 rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(0, 0, "qbz", "en", |
| 731 TranslateErrors::NONE)); | 740 TranslateErrors::NONE)); |
| 732 | 741 |
| 733 // An error infobar should be showing to report that we don't support this | 742 // An error infobar should be showing to report that we don't support this |
| 734 // language. | 743 // language. |
| 735 infobar = GetTranslateInfoBar(); | 744 infobar = GetTranslateInfoBar(); |
| 736 ASSERT_TRUE(infobar != NULL); | 745 ASSERT_TRUE(infobar != NULL); |
| 737 EXPECT_EQ(TranslateInfoBarDelegate::kTranslationError, infobar->type()); | 746 EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATION_ERROR, infobar->type()); |
| 738 | 747 |
| 739 // This infobar should have a button (so the string should not be empty). | 748 // This infobar should have a button (so the string should not be empty). |
| 740 ASSERT_FALSE(infobar->GetMessageInfoBarButtonText().empty()); | 749 ASSERT_FALSE(infobar->GetMessageInfoBarButtonText().empty()); |
| 741 | 750 |
| 742 // Pressing the button on that infobar should revert to the original language. | 751 // Pressing the button on that infobar should revert to the original language. |
| 743 process()->sink().ClearMessages(); | 752 process()->sink().ClearMessages(); |
| 744 infobar->MessageInfoBarButtonPressed(); | 753 infobar->MessageInfoBarButtonPressed(); |
| 745 const IPC::Message* message = | 754 const IPC::Message* message = |
| 746 process()->sink().GetFirstMessageMatching(ViewMsg_RevertTranslation::ID); | 755 process()->sink().GetFirstMessageMatching(ViewMsg_RevertTranslation::ID); |
| 747 EXPECT_TRUE(message != NULL); | 756 EXPECT_TRUE(message != NULL); |
| (...skipping 301 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1049 translate_prefs.ResetTranslationDeniedCount("fr"); | 1058 translate_prefs.ResetTranslationDeniedCount("fr"); |
| 1050 translate_prefs.ResetTranslationAcceptedCount("de"); | 1059 translate_prefs.ResetTranslationAcceptedCount("de"); |
| 1051 translate_prefs.ResetTranslationDeniedCount("de"); | 1060 translate_prefs.ResetTranslationDeniedCount("de"); |
| 1052 | 1061 |
| 1053 TranslateInfoBarDelegate* infobar; | 1062 TranslateInfoBarDelegate* infobar; |
| 1054 for (int i = 0; i < 4; ++i) { | 1063 for (int i = 0; i < 4; ++i) { |
| 1055 SimulateNavigation(GURL("http://www.google.fr"), 1, "Le Google", "fr", | 1064 SimulateNavigation(GURL("http://www.google.fr"), 1, "Le Google", "fr", |
| 1056 true); | 1065 true); |
| 1057 infobar = GetTranslateInfoBar(); | 1066 infobar = GetTranslateInfoBar(); |
| 1058 ASSERT_TRUE(infobar != NULL); | 1067 ASSERT_TRUE(infobar != NULL); |
| 1059 EXPECT_EQ(TranslateInfoBarDelegate::kBeforeTranslate, infobar->type()); | 1068 EXPECT_EQ(TranslateInfoBarDelegate::BEFORE_TRANSLATE, infobar->type()); |
| 1060 if (i < 3) { | 1069 if (i < 3) { |
| 1061 EXPECT_FALSE(infobar->ShouldShowAlwaysTranslateButton()); | 1070 EXPECT_FALSE(infobar->ShouldShowAlwaysTranslateButton()); |
| 1062 infobar->Translate(); | 1071 infobar->Translate(); |
| 1063 process()->sink().ClearMessages(); | 1072 process()->sink().ClearMessages(); |
| 1064 } else { | 1073 } else { |
| 1065 EXPECT_TRUE(infobar->ShouldShowAlwaysTranslateButton()); | 1074 EXPECT_TRUE(infobar->ShouldShowAlwaysTranslateButton()); |
| 1066 } | 1075 } |
| 1067 } | 1076 } |
| 1068 // Simulate the user pressing "Always translate French". | 1077 // Simulate the user pressing "Always translate French". |
| 1069 infobar->AlwaysTranslatePageLanguage(); | 1078 infobar->AlwaysTranslatePageLanguage(); |
| 1070 EXPECT_TRUE(translate_prefs.IsLanguagePairWhitelisted("fr", "en")); | 1079 EXPECT_TRUE(translate_prefs.IsLanguagePairWhitelisted("fr", "en")); |
| 1071 // Simulate the translate script being retrieved (it only needs to be done | 1080 // Simulate the translate script being retrieved (it only needs to be done |
| 1072 // once in the test as it is cached). | 1081 // once in the test as it is cached). |
| 1073 SimulateURLFetch(true); | 1082 SimulateURLFetch(true); |
| 1074 // That should have triggered a page translate. | 1083 // That should have triggered a page translate. |
| 1075 int page_id = 0; | 1084 int page_id = 0; |
| 1076 std::string original_lang, target_lang; | 1085 std::string original_lang, target_lang; |
| 1077 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); | 1086 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); |
| 1078 process()->sink().ClearMessages(); | 1087 process()->sink().ClearMessages(); |
| 1079 | 1088 |
| 1080 // Now test that declining the translation causes a "never translate" button | 1089 // Now test that declining the translation causes a "never translate" button |
| 1081 // to be shown. | 1090 // to be shown. |
| 1082 for (int i = 0; i < 4; ++i) { | 1091 for (int i = 0; i < 4; ++i) { |
| 1083 SimulateNavigation(GURL("http://www.google.de"), 1, "Das Google", "de", | 1092 SimulateNavigation(GURL("http://www.google.de"), 1, "Das Google", "de", |
| 1084 true); | 1093 true); |
| 1085 infobar = GetTranslateInfoBar(); | 1094 infobar = GetTranslateInfoBar(); |
| 1086 ASSERT_TRUE(infobar != NULL); | 1095 ASSERT_TRUE(infobar != NULL); |
| 1087 EXPECT_EQ(TranslateInfoBarDelegate::kBeforeTranslate, infobar->type()); | 1096 EXPECT_EQ(TranslateInfoBarDelegate::BEFORE_TRANSLATE, infobar->type()); |
| 1088 if (i < 3) { | 1097 if (i < 3) { |
| 1089 EXPECT_FALSE(infobar->ShouldShowNeverTranslateButton()); | 1098 EXPECT_FALSE(infobar->ShouldShowNeverTranslateButton()); |
| 1090 infobar->TranslationDeclined(); | 1099 infobar->TranslationDeclined(); |
| 1091 } else { | 1100 } else { |
| 1092 EXPECT_TRUE(infobar->ShouldShowNeverTranslateButton()); | 1101 EXPECT_TRUE(infobar->ShouldShowNeverTranslateButton()); |
| 1093 } | 1102 } |
| 1094 } | 1103 } |
| 1095 // Simulate the user pressing "Never translate French". | 1104 // Simulate the user pressing "Never translate French". |
| 1096 infobar->NeverTranslatePageLanguage(); | 1105 infobar->NeverTranslatePageLanguage(); |
| 1097 EXPECT_TRUE(translate_prefs.IsLanguageBlacklisted("de")); | 1106 EXPECT_TRUE(translate_prefs.IsLanguageBlacklisted("de")); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 1112 EXPECT_TRUE(GetTranslateInfoBar() == NULL); | 1121 EXPECT_TRUE(GetTranslateInfoBar() == NULL); |
| 1113 | 1122 |
| 1114 // The context menu should be disabled. | 1123 // The context menu should be disabled. |
| 1115 scoped_ptr<TestRenderViewContextMenu> menu( | 1124 scoped_ptr<TestRenderViewContextMenu> menu( |
| 1116 TestRenderViewContextMenu::CreateContextMenu(contents())); | 1125 TestRenderViewContextMenu::CreateContextMenu(contents())); |
| 1117 menu->Init(); | 1126 menu->Init(); |
| 1118 EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE)); | 1127 EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE)); |
| 1119 EXPECT_FALSE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE)); | 1128 EXPECT_FALSE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE)); |
| 1120 } | 1129 } |
| 1121 | 1130 |
| 1131 // Tests that the script is expired and refetched as expected. |
| 1132 TEST_F(TranslateManagerTest, ScriptExpires) { |
| 1133 ExpireTranslateScriptImmediately(); |
| 1134 |
| 1135 // Simulate navigating to a page and translating it. |
| 1136 SimulateNavigation(GURL("http://www.google.fr"), 0, "Le Google", "fr", true); |
| 1137 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); |
| 1138 ASSERT_TRUE(infobar != NULL); |
| 1139 process()->sink().ClearMessages(); |
| 1140 infobar->Translate(); |
| 1141 SimulateURLFetch(true); |
| 1142 rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(0, 0, "fr", "en", |
| 1143 TranslateErrors::NONE)); |
| 1144 |
| 1145 // A task should have been posted to clear the script, run it. |
| 1146 MessageLoop::current()->RunAllPending(); |
| 1147 |
| 1148 // Do another navigation and translation. |
| 1149 SimulateNavigation(GURL("http://www.google.es"), 1, "El Google", "es", true); |
| 1150 infobar = GetTranslateInfoBar(); |
| 1151 ASSERT_TRUE(infobar != NULL); |
| 1152 process()->sink().ClearMessages(); |
| 1153 infobar->Translate(); |
| 1154 // If we don't simulate the URL fetch, the TranslateManager should be waiting |
| 1155 // for the script and no message should have been sent to the renderer. |
| 1156 EXPECT_TRUE( |
| 1157 process()->sink().GetFirstMessageMatching(ViewMsg_TranslatePage::ID) == |
| 1158 NULL); |
| 1159 // Now simulate the URL fetch. |
| 1160 SimulateURLFetch(true); |
| 1161 // Now the message should have been sent. |
| 1162 int page_id = 0; |
| 1163 std::string original_lang, target_lang; |
| 1164 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); |
| 1165 EXPECT_EQ(1, page_id); |
| 1166 EXPECT_EQ("es", original_lang); |
| 1167 EXPECT_EQ("en", target_lang); |
| 1168 } |
| OLD | NEW |