diff options
12 files changed, 192 insertions, 88 deletions
diff --git a/chrome/app/generated_resources.grd b/chrome/app/generated_resources.grd index 483426a..a1854d7 100644 --- a/chrome/app/generated_resources.grd +++ b/chrome/app/generated_resources.grd @@ -7042,6 +7042,12 @@ Keep your key file in a safe place. You will need it to create new versions of y <message name="IDS_TRANSLATE_INFOBAR_ERROR_CANT_TRANSLATE" desc="Error message to show for translate infobar when translate server is unable to translate page"> The translation failed because of a server error. </message> + <message name="IDS_TRANSLATE_INFOBAR_UNKNOWN_PAGE_LANGUAGE" desc="Error message to show for translate infobar when the page language could not be determined."> + The translation failed because the page's language could not be determined. + </message> + <message name="IDS_TRANSLATE_INFOBAR_ERROR_SAME_LANGUAGE" desc="Error message to show for translate infobar when the original and target languages are identical"> + The translation failed because the page is already in <ph name="language">$1<ex>English</ex></ph>. + </message> <!-- Desktop notifications --> <message name="IDS_NOTIFICATION_BALLOON_DISMISS_LABEL" desc="Text on the button which dismisses the balloon."> diff --git a/chrome/browser/cocoa/translate/translate_infobar_base.mm b/chrome/browser/cocoa/translate/translate_infobar_base.mm index f93a733..7daf09a 100644 --- a/chrome/browser/cocoa/translate/translate_infobar_base.mm +++ b/chrome/browser/cocoa/translate/translate_infobar_base.mm @@ -114,16 +114,16 @@ InfoBar* TranslateInfoBarDelegate::CreateInfoBar() { InfoBar* TranslateInfoBarDelegate2::CreateInfoBar() { TranslateInfoBarControllerBase* infobar_controller = NULL; switch (type_) { - case BEFORE_TRANSLATE: + case kBeforeTranslate: infobar_controller = [[BeforeTranslateInfobarController alloc] initWithDelegate:this]; break; - case AFTER_TRANSLATE: + case kAfterTranslate: infobar_controller = [[AfterTranslateInfobarController alloc] initWithDelegate:this]; break; - case TRANSLATING: - case TRANSLATION_ERROR: + case kTranslating: + case kTranslationError: infobar_controller = [[TranslateMessageInfobarController alloc] initWithDelegate:this]; break; @@ -456,8 +456,8 @@ InfoBar* TranslateInfoBarDelegate2::CreateInfoBar() { - (IBAction)ok:(id)sender { TranslateInfoBarDelegate2* delegate = [self delegate]; TranslateInfoBarDelegate2::Type state = delegate->type(); - DCHECK(state == TranslateInfoBarDelegate2::BEFORE_TRANSLATE || - state == TranslateInfoBarDelegate2::TRANSLATION_ERROR); + DCHECK(state == TranslateInfoBarDelegate2::kBeforeTranslate || + state == TranslateInfoBarDelegate2::kTranslationError); delegate->Translate(); UMA_HISTOGRAM_COUNTS("Translate.Translate", 1); } @@ -465,7 +465,7 @@ InfoBar* TranslateInfoBarDelegate2::CreateInfoBar() { // Called when someone clicks on the "Nope" button. - (IBAction)cancel:(id)sender { DCHECK( - [self delegate]->type() == TranslateInfoBarDelegate2::BEFORE_TRANSLATE); + [self delegate]->type() == TranslateInfoBarDelegate2::kBeforeTranslate); [self delegate]->TranslationDeclined(); UMA_HISTOGRAM_COUNTS("Translate.DeclineTranslate", 1); [super dismiss:nil]; diff --git a/chrome/browser/cocoa/translate/translate_infobar_unittest.mm b/chrome/browser/cocoa/translate/translate_infobar_unittest.mm index 38eb1ac..dd3410e 100644 --- a/chrome/browser/cocoa/translate/translate_infobar_unittest.mm +++ b/chrome/browser/cocoa/translate/translate_infobar_unittest.mm @@ -26,10 +26,10 @@ namespace { // All states the translate toolbar can assume. TranslateInfoBarDelegate2::Type kTranslateToolbarStates[] = { - TranslateInfoBarDelegate2::BEFORE_TRANSLATE, - TranslateInfoBarDelegate2::AFTER_TRANSLATE, - TranslateInfoBarDelegate2::TRANSLATING, - TranslateInfoBarDelegate2::TRANSLATION_ERROR + TranslateInfoBarDelegate2::kBeforeTranslate, + TranslateInfoBarDelegate2::kAfterTranslate, + TranslateInfoBarDelegate2::kTranslating, + TranslateInfoBarDelegate2::kTranslationError }; class MockTranslateInfoBarDelegate : public TranslateInfoBarDelegate2 { @@ -82,7 +82,7 @@ class TranslationInfoBarTest : public CocoaTest { } void CreateInfoBar() { - CreateInfoBar(TranslateInfoBarDelegate2::BEFORE_TRANSLATE); + CreateInfoBar(TranslateInfoBarDelegate2::kBeforeTranslate); } void CreateInfoBar(TranslateInfoBarDelegate2::Type type) { @@ -94,7 +94,7 @@ class TranslationInfoBarTest : public CocoaTest { MSG_ROUTING_NONE, NULL)); TranslateErrors::Type error = TranslateErrors::NONE; - if (type == TranslateInfoBarDelegate2::TRANSLATION_ERROR) + if (type == TranslateInfoBarDelegate2::kTranslationError) error = TranslateErrors::NETWORK; infobar_delegate.reset( new MockTranslateInfoBarDelegate(type, error, tab_contents.get())); @@ -125,7 +125,7 @@ TEST_F(TranslationInfoBarTest, TranslateCalledOnButtonPress) { // Check that clicking the "Retry" button calls Translate() when we're // in the error mode - http://crbug.com/41315 . TEST_F(TranslationInfoBarTest, TranslateCalledInErrorMode) { - CreateInfoBar(TranslateInfoBarDelegate2::TRANSLATION_ERROR); + CreateInfoBar(TranslateInfoBarDelegate2::kTranslationError); EXPECT_CALL(*infobar_delegate, Translate()).Times(1); diff --git a/chrome/browser/cocoa/translate/translate_message_infobar_controller.mm b/chrome/browser/cocoa/translate/translate_message_infobar_controller.mm index 1087820..3288cea 100644 --- a/chrome/browser/cocoa/translate/translate_message_infobar_controller.mm +++ b/chrome/browser/cocoa/translate/translate_message_infobar_controller.mm @@ -14,9 +14,9 @@ using TranslateInfoBarUtilities::MoveControl; if ((self = [super initWithDelegate:delegate])) { TranslateInfoBarDelegate2* delegate = [self delegate]; if (delegate->IsError()) - state_ = TranslateInfoBarDelegate2::TRANSLATION_ERROR; + state_ = TranslateInfoBarDelegate2::kTranslationError; else - state_ = TranslateInfoBarDelegate2::TRANSLATING; + state_ = TranslateInfoBarDelegate2::kTranslating; } return self; } @@ -33,7 +33,7 @@ using TranslateInfoBarUtilities::MoveControl; - (NSArray*)visibleControls { NSMutableArray* visibleControls = [NSMutableArray arrayWithObjects:label1_.get(), nil]; - if (state_ == TranslateInfoBarDelegate2::TRANSLATION_ERROR) + if (state_ == TranslateInfoBarDelegate2::kTranslationError) [visibleControls addObject:tryAgainButton_]; return visibleControls; } diff --git a/chrome/browser/gtk/translate/translate_infobar_base_gtk.cc b/chrome/browser/gtk/translate/translate_infobar_base_gtk.cc index a2d46e0..80ee803 100644 --- a/chrome/browser/gtk/translate/translate_infobar_base_gtk.cc +++ b/chrome/browser/gtk/translate/translate_infobar_base_gtk.cc @@ -36,14 +36,14 @@ TranslateInfoBarBase::TranslateInfoBarBase(TranslateInfoBarDelegate2* delegate) : InfoBar(delegate) { TranslateInfoBarDelegate2::BackgroundAnimationType animation = delegate->background_animation_type(); - if (animation != TranslateInfoBarDelegate2::NONE) { + if (animation != TranslateInfoBarDelegate2::kNone) { background_color_animation_.reset(new SlideAnimation(this)); background_color_animation_->SetTweenType(Tween::LINEAR); background_color_animation_->SetSlideDuration(500); - if (animation == TranslateInfoBarDelegate2::NORMAL_TO_ERROR) { + if (animation == TranslateInfoBarDelegate2::kNormalToError) { background_color_animation_->Show(); } else { - DCHECK_EQ(TranslateInfoBarDelegate2::ERROR_TO_NORMAL, animation); + DCHECK_EQ(TranslateInfoBarDelegate2::kErrorToNormal, animation); // Hide() runs the animation in reverse. background_color_animation_->Reset(1.0); background_color_animation_->Hide(); @@ -216,14 +216,14 @@ void TranslateInfoBarBase::OnOptionsClicked(GtkWidget* sender) { InfoBar* TranslateInfoBarDelegate2::CreateInfoBar() { TranslateInfoBarBase* infobar = NULL; switch (type_) { - case BEFORE_TRANSLATE: + case kBeforeTranslate: infobar = new BeforeTranslateInfoBar(this); break; - case AFTER_TRANSLATE: + case kAfterTranslate: infobar = new AfterTranslateInfoBar(this); break; - case TRANSLATING: - case TRANSLATION_ERROR: + case kTranslating: + case kTranslationError: infobar = new TranslateMessageInfoBar(this); break; default: diff --git a/chrome/browser/translate/translate_infobar_delegate2.cc b/chrome/browser/translate/translate_infobar_delegate2.cc index 7733b5f..8172a0d 100644 --- a/chrome/browser/translate/translate_infobar_delegate2.cc +++ b/chrome/browser/translate/translate_infobar_delegate2.cc @@ -17,17 +17,31 @@ #include "grit/theme_resources.h" // static -TranslateInfoBarDelegate2* TranslateInfoBarDelegate2::CreateInstance( +TranslateInfoBarDelegate2* TranslateInfoBarDelegate2::CreateDelegate( Type type, - TranslateErrors::Type error, TabContents* tab_contents, const std::string& original_language, const std::string& target_language) { + DCHECK(type != kTranslationError); if (!TranslateManager2::IsSupportedLanguage(original_language) || !TranslateManager2::IsSupportedLanguage(target_language)) { return NULL; } - return new TranslateInfoBarDelegate2(type, error, tab_contents, + TranslateInfoBarDelegate2* delegate = + new TranslateInfoBarDelegate2(type, TranslateErrors::NONE, + tab_contents, + original_language, target_language); + DCHECK(delegate->original_language_index() != -1); + DCHECK(delegate->target_language_index() != -1); + return delegate; +} + +TranslateInfoBarDelegate2* TranslateInfoBarDelegate2::CreateErrorDelegate( + TranslateErrors::Type error, + TabContents* tab_contents, + const std::string& original_language, + const std::string& target_language) { + return new TranslateInfoBarDelegate2(kTranslationError, error, tab_contents, original_language, target_language); } @@ -39,15 +53,15 @@ TranslateInfoBarDelegate2::TranslateInfoBarDelegate2( const std::string& target_language) : InfoBarDelegate(tab_contents), type_(type), - background_animation_(NONE), + background_animation_(kNone), tab_contents_(tab_contents), original_language_index_(-1), target_language_index_(-1), error_(error), infobar_view_(NULL), prefs_(tab_contents_->profile()->GetPrefs()) { - DCHECK((type_ != TRANSLATION_ERROR && error == TranslateErrors::NONE) || - (type_ == TRANSLATION_ERROR && error != TranslateErrors::NONE)); + DCHECK((type_ != kTranslationError && error == TranslateErrors::NONE) || + (type_ == kTranslationError && error != TranslateErrors::NONE)); std::vector<std::string> language_codes; TranslateManager2::GetSupportedLanguages(&language_codes); @@ -71,12 +85,9 @@ TranslateInfoBarDelegate2::TranslateInfoBarDelegate2( std::string language_code = iter->first; if (language_code == original_language) original_language_index_ = iter - languages_.begin(); - else if (language_code == target_language) + if (language_code == target_language) target_language_index_ = iter - languages_.begin(); } - - DCHECK(original_language_index_ != -1); - DCHECK(target_language_index_ != -1); } int TranslateInfoBarDelegate2::GetLanguageCount() const { @@ -108,7 +119,7 @@ void TranslateInfoBarDelegate2::SetOriginalLanguage(int language_index) { original_language_index_ = language_index; if (infobar_view_) infobar_view_->OriginalLanguageChanged(); - if (type_ == AFTER_TRANSLATE) + if (type_ == kAfterTranslate) Translate(); } @@ -117,12 +128,12 @@ void TranslateInfoBarDelegate2::SetTargetLanguage(int language_index) { target_language_index_ = language_index; if (infobar_view_) infobar_view_->TargetLanguageChanged(); - if (type_ == AFTER_TRANSLATE) + if (type_ == kAfterTranslate) Translate(); } bool TranslateInfoBarDelegate2::IsError() { - return type_ == TRANSLATION_ERROR; + return type_ == kTranslationError; } void TranslateInfoBarDelegate2::Translate() { @@ -155,7 +166,7 @@ void TranslateInfoBarDelegate2::TranslationDeclined() { } void TranslateInfoBarDelegate2::InfoBarDismissed() { - if (type_ != BEFORE_TRANSLATE) + if (type_ != kBeforeTranslate) return; // The user closed the infobar without clicking the translate button. @@ -239,11 +250,11 @@ void TranslateInfoBarDelegate2::NeverTranslatePageLanguage() { string16 TranslateInfoBarDelegate2::GetMessageInfoBarText() { switch (type_) { - case TRANSLATING: + case kTranslating: return l10n_util::GetStringFUTF16( IDS_TRANSLATE_INFOBAR_TRANSLATING_TO, GetLanguageDisplayableNameAt(target_language_index_)); - case TRANSLATION_ERROR: + case kTranslationError: switch (error_) { case TranslateErrors::NETWORK: return l10n_util::GetStringUTF16( @@ -252,6 +263,13 @@ string16 TranslateInfoBarDelegate2::GetMessageInfoBarText() { case TranslateErrors::TRANSLATION_ERROR: return l10n_util::GetStringUTF16( IDS_TRANSLATE_INFOBAR_ERROR_CANT_TRANSLATE); + case TranslateErrors::UNKNOWN_LANGUAGE: + return l10n_util::GetStringUTF16( + IDS_TRANSLATE_INFOBAR_UNKNOWN_PAGE_LANGUAGE); + case TranslateErrors::IDENTICAL_LANGUAGES: + return l10n_util::GetStringFUTF16( + IDS_TRANSLATE_INFOBAR_ERROR_SAME_LANGUAGE, + GetLanguageDisplayableNameAt(target_language_index_)); default: NOTREACHED(); return string16(); @@ -264,9 +282,14 @@ string16 TranslateInfoBarDelegate2::GetMessageInfoBarText() { string16 TranslateInfoBarDelegate2::GetMessageInfoBarButtonText() { switch (type_) { - case TRANSLATING: + case kTranslating: return string16(); - case TRANSLATION_ERROR: + case kTranslationError: + if (error_ == TranslateErrors::IDENTICAL_LANGUAGES || + error_ == TranslateErrors::UNKNOWN_LANGUAGE) { + // No retry button, we would fail again with the same error. + return string16(); + } return l10n_util::GetStringUTF16(IDS_TRANSLATE_INFOBAR_RETRY); default: NOTREACHED(); @@ -275,7 +298,7 @@ string16 TranslateInfoBarDelegate2::GetMessageInfoBarButtonText() { } void TranslateInfoBarDelegate2::MessageInfoBarButtonPressed() { - DCHECK(type_ == TRANSLATION_ERROR); + DCHECK(type_ == kTranslationError); Singleton<TranslateManager2>::get()->TranslatePage( tab_contents_, GetOriginalLanguageCode(), GetTargetLanguageCode()); } @@ -293,10 +316,10 @@ bool TranslateInfoBarDelegate2::ShouldShowAlwaysTranslateButton() { void TranslateInfoBarDelegate2::UpdateBackgroundAnimation( TranslateInfoBarDelegate2* previous_infobar) { if (!previous_infobar || previous_infobar->IsError() == IsError()) { - background_animation_ = NONE; + background_animation_ = kNone; return; } - background_animation_ = IsError() ? NORMAL_TO_ERROR : ERROR_TO_NORMAL; + background_animation_ = IsError() ? kNormalToError: kErrorToNormal; } std::string TranslateInfoBarDelegate2::GetPageHost() { diff --git a/chrome/browser/translate/translate_infobar_delegate2.h b/chrome/browser/translate/translate_infobar_delegate2.h index 14c683a..7b5f691 100644 --- a/chrome/browser/translate/translate_infobar_delegate2.h +++ b/chrome/browser/translate/translate_infobar_delegate2.h @@ -19,28 +19,33 @@ class TranslateInfoBarDelegate2 : public InfoBarDelegate { public: // The different types of infobars that can be shown for translation. enum Type { - BEFORE_TRANSLATE, - TRANSLATING, - AFTER_TRANSLATE, - TRANSLATION_ERROR + kBeforeTranslate, + kTranslating, + kAfterTranslate, + kTranslationError }; // The types of background color animations. enum BackgroundAnimationType { - NONE, - NORMAL_TO_ERROR, - ERROR_TO_NORMAL + kNone, + kNormalToError, + kErrorToNormal }; - // Factory method. + // Factory method to create a non-error translate infobar. // The original and target language specified are the ASCII language codes // (ex: en, fr...). // Returns NULL if it failed, typically if |original_language| or // |target_language| is not a supported language. - // |error| should be set to NONE if |infobar_type| is not TRANSLATION_ERROR. - static TranslateInfoBarDelegate2* CreateInstance( + static TranslateInfoBarDelegate2* CreateDelegate( Type infobar_type, - TranslateErrors::Type error, + TabContents* tab_contents, + const std::string& original_language, + const std::string& target_language); + + // Factory method to create an error translate infobar. + static TranslateInfoBarDelegate2* CreateErrorDelegate( + TranslateErrors::Type error_type, TabContents* tab_contents, const std::string& original_language, const std::string& target_language); @@ -58,6 +63,8 @@ class TranslateInfoBarDelegate2 : public InfoBarDelegate { Type type() const { return type_; } + TranslateErrors::Type error() const { return error_; } + int original_language_index() const { return original_language_index_; } int target_language_index() const { return target_language_index_; } diff --git a/chrome/browser/translate/translate_manager2.cc b/chrome/browser/translate/translate_manager2.cc index 7f8cfa9..e7f70f0 100644 --- a/chrome/browser/translate/translate_manager2.cc +++ b/chrome/browser/translate/translate_manager2.cc @@ -275,11 +275,9 @@ void TranslateManager2::OnURLFetchComplete(const URLFetcher* source, } if (error) { - ShowInfoBar(tab, - TranslateInfoBarDelegate2::CreateInstance( - TranslateInfoBarDelegate2::TRANSLATION_ERROR, - TranslateErrors::NETWORK, - tab, request.source_lang, request.target_lang)); + ShowInfoBar(tab, TranslateInfoBarDelegate2::CreateErrorDelegate( + TranslateErrors::NETWORK, tab, + request.source_lang, request.target_lang)); } else { // Translate the page. DoTranslatePage(tab, translate_script_, @@ -359,9 +357,9 @@ void TranslateManager2::InitiateTranslation(TabContents* tab, } // Prompts the user if he/she wants the page translated. - tab->AddInfoBar(TranslateInfoBarDelegate2::CreateInstance( - TranslateInfoBarDelegate2::BEFORE_TRANSLATE, - TranslateErrors::NONE, tab, page_lang, target_lang)); + tab->AddInfoBar(TranslateInfoBarDelegate2::CreateDelegate( + TranslateInfoBarDelegate2::kBeforeTranslate, tab, + page_lang, target_lang)); } void TranslateManager2::InitiateTranslationPosted( @@ -426,9 +424,8 @@ void TranslateManager2::DoTranslatePage(TabContents* tab, // We don't show the translating infobar if no translate infobar is already // showing (that is the case when the translation was triggered by the // "always translate" for example). - infobar = TranslateInfoBarDelegate2::CreateInstance( - TranslateInfoBarDelegate2::TRANSLATING, TranslateErrors::NONE, - tab, source_lang, target_lang); + infobar = TranslateInfoBarDelegate2::CreateDelegate( + TranslateInfoBarDelegate2::kTranslating, tab, source_lang, target_lang); ShowInfoBar(tab, infobar); } tab->language_state().set_translation_pending(true); @@ -441,13 +438,13 @@ void TranslateManager2::PageTranslated(TabContents* tab, // Create the new infobar to display. TranslateInfoBarDelegate2* infobar; if (details->error_type != TranslateErrors::NONE) { - infobar = TranslateInfoBarDelegate2::CreateInstance( - TranslateInfoBarDelegate2::TRANSLATION_ERROR, details->error_type, - tab, details->source_language, details->target_language); + infobar = TranslateInfoBarDelegate2::CreateErrorDelegate( + details->error_type, tab, + details->source_language, details->target_language); } else { - infobar = TranslateInfoBarDelegate2::CreateInstance( - TranslateInfoBarDelegate2::AFTER_TRANSLATE, TranslateErrors::NONE, - tab, details->source_language, details->target_language); + infobar = TranslateInfoBarDelegate2::CreateDelegate( + TranslateInfoBarDelegate2::kAfterTranslate, tab, + details->source_language, details->target_language); } ShowInfoBar(tab, infobar); } diff --git a/chrome/browser/translate/translate_manager2_unittest.cc b/chrome/browser/translate/translate_manager2_unittest.cc index 4030b2a..bf72b52 100644 --- a/chrome/browser/translate/translate_manager2_unittest.cc +++ b/chrome/browser/translate/translate_manager2_unittest.cc @@ -152,6 +152,8 @@ class TranslateManager2Test : public RenderViewHostTestHarness, } virtual void TearDown() { + process()->sink().ClearMessages(); + notification_registrar_.Remove( this, NotificationType::TAB_CONTENTS_INFOBAR_REMOVED, @@ -271,7 +273,7 @@ TEST_F(TranslateManager2Test, NormalTranslate) { // We should have an infobar. TranslateInfoBarDelegate2* infobar = GetTranslateInfoBar(); ASSERT_TRUE(infobar != NULL); - EXPECT_EQ(TranslateInfoBarDelegate2::BEFORE_TRANSLATE, infobar->type()); + EXPECT_EQ(TranslateInfoBarDelegate2::kBeforeTranslate, infobar->type()); // Simulate clicking translate. process()->sink().ClearMessages(); @@ -292,7 +294,7 @@ TEST_F(TranslateManager2Test, NormalTranslate) { // The "Translating..." infobar should be showing. infobar = GetTranslateInfoBar(); ASSERT_TRUE(infobar != NULL); - EXPECT_EQ(TranslateInfoBarDelegate2::TRANSLATING, infobar->type()); + EXPECT_EQ(TranslateInfoBarDelegate2::kTranslating, infobar->type()); // Simulate the render notifying the translation has been done. rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(0, 0, "fr", "en", @@ -301,7 +303,7 @@ TEST_F(TranslateManager2Test, NormalTranslate) { // The after translate infobar should be showing. infobar = GetTranslateInfoBar(); ASSERT_TRUE(infobar != NULL); - EXPECT_EQ(TranslateInfoBarDelegate2::AFTER_TRANSLATE, infobar->type()); + EXPECT_EQ(TranslateInfoBarDelegate2::kAfterTranslate, infobar->type()); // Simulate changing the original language, this should trigger a translation. process()->sink().ClearMessages(); @@ -342,7 +344,7 @@ TEST_F(TranslateManager2Test, TranslateScriptNotAvailable) { // We should have an infobar. TranslateInfoBarDelegate2* infobar = GetTranslateInfoBar(); ASSERT_TRUE(infobar != NULL); - EXPECT_EQ(TranslateInfoBarDelegate2::BEFORE_TRANSLATE, infobar->type()); + EXPECT_EQ(TranslateInfoBarDelegate2::kBeforeTranslate, infobar->type()); // Simulate clicking translate. process()->sink().ClearMessages(); @@ -356,7 +358,63 @@ TEST_F(TranslateManager2Test, TranslateScriptNotAvailable) { // And we should have an error infobar showing. infobar = GetTranslateInfoBar(); ASSERT_TRUE(infobar != NULL); - EXPECT_EQ(TranslateInfoBarDelegate2::TRANSLATION_ERROR, infobar->type()); + EXPECT_EQ(TranslateInfoBarDelegate2::kTranslationError, infobar->type()); +} + +// Ensures we deal correctly with pages for which the browser does not recognize +// the language (the translate server may or not detect the language). +TEST_F(TranslateManager2Test, TranslateUnknownLanguage) { + // Simulate navigating to a page ("und" is the string returned by the CLD for + // languages it does not recognize). + SimulateNavigation(GURL("http://www.google.mys"), 0, "G00g1e", "und"); + + // We should not have an infobar as we don't know the language. + ASSERT_TRUE(GetTranslateInfoBar() == NULL); + + // Translate the page anyway throught the context menu. + scoped_ptr<TestRenderViewContextMenu> menu( + TestRenderViewContextMenu::CreateContextMenu(contents())); + menu->Init(); + menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE); + SimulateURLFetch(true); // Simulare receiving the translate script. + + // Simulate the render notifying the translation has been done, the server + // having detected the page was in a known and supported language. + rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(0, 0, "fr", "en", + TranslateErrors::NONE)); + + // The after translate infobar should be showing. + TranslateInfoBarDelegate2* infobar = GetTranslateInfoBar(); + ASSERT_TRUE(infobar != NULL); + EXPECT_EQ(TranslateInfoBarDelegate2::kAfterTranslate, infobar->type()); + EXPECT_EQ("fr", infobar->GetOriginalLanguageCode()); + EXPECT_EQ("en", infobar->GetTargetLanguageCode()); + + // Let's run the same steps but this time the server detects the page is + // already in English. + SimulateNavigation(GURL("http://www.google.com"), 1, "The Google", "und"); + menu.reset(TestRenderViewContextMenu::CreateContextMenu(contents())); + menu->Init(); + menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE); + rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(1, 0, "en", "en", + TranslateErrors::IDENTICAL_LANGUAGES)); + infobar = GetTranslateInfoBar(); + ASSERT_TRUE(infobar != NULL); + EXPECT_EQ(TranslateInfoBarDelegate2::kTranslationError, infobar->type()); + EXPECT_EQ(TranslateErrors::IDENTICAL_LANGUAGES, infobar->error()); + + // Let's run the same steps again but this time the server fails to detect the + // page's language (it returns an empty string). + SimulateNavigation(GURL("http://www.google.com"), 2, "The Google", "und"); + menu.reset(TestRenderViewContextMenu::CreateContextMenu(contents())); + menu->Init(); + menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE); + rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(2, 0, "", "en", + TranslateErrors::UNKNOWN_LANGUAGE)); + infobar = GetTranslateInfoBar(); + ASSERT_TRUE(infobar != NULL); + EXPECT_EQ(TranslateInfoBarDelegate2::kTranslationError, infobar->type()); + EXPECT_EQ(TranslateErrors::UNKNOWN_LANGUAGE, infobar->error()); } // Tests that we show/don't show an info-bar for all languages the CLD can @@ -925,6 +983,14 @@ TEST_F(TranslateManager2Test, FAILS_ContextMenu) { menu->Init(); EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE)); EXPECT_FALSE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE)); + + // Test that the translate context menu is enabled when the page is in an + // unknown language as the UI. + SimulateNavigation(url, 0, "G00g1e", "und"); + menu.reset(TestRenderViewContextMenu::CreateContextMenu(contents())); + menu->Init(); + EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE)); + EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE)); } // Tests that an extra always/never translate button is shown on the "before diff --git a/chrome/browser/views/infobars/translate_infobar_base.cc b/chrome/browser/views/infobars/translate_infobar_base.cc index b62a3ef..4347f0d 100644 --- a/chrome/browser/views/infobars/translate_infobar_base.cc +++ b/chrome/browser/views/infobars/translate_infobar_base.cc @@ -30,14 +30,14 @@ TranslateInfoBarBase::TranslateInfoBarBase( TranslateInfoBarDelegate2::BackgroundAnimationType animation = delegate->background_animation_type(); - if (animation != TranslateInfoBarDelegate2::NONE) { + if (animation != TranslateInfoBarDelegate2::kNone) { background_color_animation_.reset(new SlideAnimation(this)); background_color_animation_->SetTweenType(Tween::LINEAR); background_color_animation_->SetSlideDuration(500); - if (animation == TranslateInfoBarDelegate2::NORMAL_TO_ERROR) { + if (animation == TranslateInfoBarDelegate2::kNormalToError) { background_color_animation_->Show(); } else { - DCHECK_EQ(TranslateInfoBarDelegate2::ERROR_TO_NORMAL, animation); + DCHECK_EQ(TranslateInfoBarDelegate2::kErrorToNormal, animation); // Hide() runs the animation in reverse. background_color_animation_->Reset(1.0); background_color_animation_->Hide(); @@ -158,14 +158,14 @@ void TranslateInfoBarBase::FadeBackground(gfx::Canvas* canvas, InfoBar* TranslateInfoBarDelegate2::CreateInfoBar() { TranslateInfoBarBase* infobar = NULL; switch (type_) { - case BEFORE_TRANSLATE: + case kBeforeTranslate: infobar = new BeforeTranslateInfoBar(this); break; - case AFTER_TRANSLATE: + case kAfterTranslate: infobar = new AfterTranslateInfoBar(this); break; - case TRANSLATING: - case TRANSLATION_ERROR: + case kTranslating: + case kTranslationError: infobar = new TranslateMessageInfoBar(this); break; default: diff --git a/chrome/common/translate_errors.h b/chrome/common/translate_errors.h index c8f4225..56a104c 100644 --- a/chrome/common/translate_errors.h +++ b/chrome/common/translate_errors.h @@ -13,8 +13,10 @@ class TranslateErrors { NONE = 0, NETWORK, // No connectivity. INITIALIZATION_ERROR, // The translation script failed to initialize. - TRANSLATION_ERROR, // An error was reported by the translation script - // during translation. + UNKNOWN_LANGUAGE, // The page's language could not be detected. + IDENTICAL_LANGUAGES, // The original and target languages are the same. + TRANSLATION_ERROR, // An error was reported by the translation script + // during translation. }; private: diff --git a/chrome/renderer/translate_helper.cc b/chrome/renderer/translate_helper.cc index 7caeec4..5075917 100644 --- a/chrome/renderer/translate_helper.cc +++ b/chrome/renderer/translate_helper.cc @@ -182,7 +182,10 @@ void TranslateHelper::CheckTranslateStatus() { if (source_lang_ == kAutoDetectionLanguage) { actual_source_lang = GetOriginalPageLanguage(); if (actual_source_lang.empty()) { - NotifyBrowserTranslationFailed(TranslateErrors::TRANSLATION_ERROR); + NotifyBrowserTranslationFailed(TranslateErrors::UNKNOWN_LANGUAGE); + return; + } else if (actual_source_lang == target_lang_) { + NotifyBrowserTranslationFailed(TranslateErrors::IDENTICAL_LANGUAGES); return; } } else { |